13df72098SAndreas Gohr<?php 23df72098SAndreas Gohr/** 33df72098SAndreas Gohr * All output and handler function needed for the media management popup 43df72098SAndreas Gohr * 53df72098SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 63df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 73df72098SAndreas Gohr */ 83df72098SAndreas Gohr 9*fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 103df72098SAndreas Gohrif(!defined('NL')) define('NL',"\n"); 113df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/html.php'); 123df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php'); 133df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/JpegMeta.php'); 143df72098SAndreas Gohr 153df72098SAndreas Gohr/** 163df72098SAndreas Gohr * Lists pages which currently use a media file selected for deletion 173df72098SAndreas Gohr * 183df72098SAndreas Gohr * References uses the same visual as search results and share 193df72098SAndreas Gohr * their CSS tags except pagenames won't be links. 203df72098SAndreas Gohr * 213df72098SAndreas Gohr * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 223df72098SAndreas Gohr */ 233df72098SAndreas Gohrfunction media_filesinuse($data,$id){ 243df72098SAndreas Gohr global $lang; 253df72098SAndreas Gohr echo '<h1>'.$lang['reference'].' <code>'.hsc(noNS($id)).'</code></h1>'; 263df72098SAndreas Gohr echo '<p>'.hsc($lang['ref_inuse']).'</p>'; 273df72098SAndreas Gohr 283df72098SAndreas Gohr $hidden=0; //count of hits without read permission 293df72098SAndreas Gohr foreach($data as $row){ 30a05e297aSAndreas Gohr if(auth_quickaclcheck($row) >= AUTH_READ && isVisiblePage($row)){ 313df72098SAndreas Gohr echo '<div class="search_result">'; 32a05e297aSAndreas Gohr echo '<span class="mediaref_ref">'.hsc($row).'</span>'; 333df72098SAndreas Gohr echo '</div>'; 343df72098SAndreas Gohr }else 353df72098SAndreas Gohr $hidden++; 363df72098SAndreas Gohr } 373df72098SAndreas Gohr if ($hidden){ 383df72098SAndreas Gohr print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>'; 393df72098SAndreas Gohr } 403df72098SAndreas Gohr} 413df72098SAndreas Gohr 423df72098SAndreas Gohr/** 433df72098SAndreas Gohr * Handles the saving of image meta data 443df72098SAndreas Gohr * 453df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 463df72098SAndreas Gohr */ 473df72098SAndreas Gohrfunction media_metasave($id,$auth,$data){ 483df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return false; 49f2ea8432SAndreas Gohr if(!checkSecurityToken()) return false; 503df72098SAndreas Gohr global $lang; 510b308644SOtto Vainio global $conf; 523df72098SAndreas Gohr $src = mediaFN($id); 533df72098SAndreas Gohr 543df72098SAndreas Gohr $meta = new JpegMeta($src); 553df72098SAndreas Gohr $meta->_parseAll(); 563df72098SAndreas Gohr 573df72098SAndreas Gohr foreach($data as $key => $val){ 583df72098SAndreas Gohr $val=trim($val); 593df72098SAndreas Gohr if(empty($val)){ 603df72098SAndreas Gohr $meta->deleteField($key); 613df72098SAndreas Gohr }else{ 623df72098SAndreas Gohr $meta->setField($key,$val); 633df72098SAndreas Gohr } 643df72098SAndreas Gohr } 653df72098SAndreas Gohr 663df72098SAndreas Gohr if($meta->save()){ 670b308644SOtto Vainio if($conf['fperm']) chmod($src, $conf['fperm']); 683df72098SAndreas Gohr msg($lang['metasaveok'],1); 693df72098SAndreas Gohr return $id; 703df72098SAndreas Gohr }else{ 713df72098SAndreas Gohr msg($lang['metasaveerr'],-1); 723df72098SAndreas Gohr return false; 733df72098SAndreas Gohr } 743df72098SAndreas Gohr} 753df72098SAndreas Gohr 763df72098SAndreas Gohr/** 773df72098SAndreas Gohr * Display the form to edit image meta data 783df72098SAndreas Gohr * 793df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 803df72098SAndreas Gohr */ 813df72098SAndreas Gohrfunction media_metaform($id,$auth){ 823df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return false; 833df72098SAndreas Gohr global $lang; 843df72098SAndreas Gohr 853df72098SAndreas Gohr // load the field descriptions 863df72098SAndreas Gohr static $fields = null; 873df72098SAndreas Gohr if(is_null($fields)){ 883df72098SAndreas Gohr include(DOKU_CONF.'mediameta.php'); 893df72098SAndreas Gohr if(@file_exists(DOKU_CONF.'mediameta.local.php')){ 903df72098SAndreas Gohr include(DOKU_CONF.'mediameta.local.php'); 913df72098SAndreas Gohr } 923df72098SAndreas Gohr } 933df72098SAndreas Gohr 943df72098SAndreas Gohr $src = mediaFN($id); 953df72098SAndreas Gohr 963df72098SAndreas Gohr // output 973df72098SAndreas Gohr echo '<h1>'.hsc(noNS($id)).'</h1>'.NL; 983df72098SAndreas Gohr echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" accept-charset="utf-8" method="post" class="meta">'.NL; 99f2ea8432SAndreas Gohr formSecurityToken(); 1003df72098SAndreas Gohr foreach($fields as $key => $field){ 1013df72098SAndreas Gohr // get current value 1023df72098SAndreas Gohr $tags = array($field[0]); 1033df72098SAndreas Gohr if(is_array($field[3])) $tags = array_merge($tags,$field[3]); 1043df72098SAndreas Gohr $value = tpl_img_getTag($tags,'',$src); 105ca6a0701SAndreas Gohr $value = cleanText($value); 1063df72098SAndreas Gohr 1073df72098SAndreas Gohr // prepare attributes 1083df72098SAndreas Gohr $p = array(); 1093df72098SAndreas Gohr $p['class'] = 'edit'; 1103df72098SAndreas Gohr $p['id'] = 'meta__'.$key; 1113df72098SAndreas Gohr $p['name'] = 'meta['.$field[0].']'; 1123df72098SAndreas Gohr 1133df72098SAndreas Gohr // put label 1143df72098SAndreas Gohr echo '<div class="metafield">'; 1153df72098SAndreas Gohr echo '<label for="meta__'.$key.'">'; 1163df72098SAndreas Gohr echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1]; 117cf6894dfSAndreas Gohr echo ':</label>'; 1183df72098SAndreas Gohr 1193df72098SAndreas Gohr // put input field 1203df72098SAndreas Gohr if($field[2] == 'text'){ 1213df72098SAndreas Gohr $p['value'] = $value; 1223df72098SAndreas Gohr $p['type'] = 'text'; 1233df72098SAndreas Gohr $att = buildAttributes($p); 1243df72098SAndreas Gohr echo "<input $att/>".NL; 1253df72098SAndreas Gohr }else{ 1263df72098SAndreas Gohr $att = buildAttributes($p); 1271440e523SAnika Henke echo "<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>'.NL; 1283df72098SAndreas Gohr } 1293df72098SAndreas Gohr echo '</div>'.NL; 1303df72098SAndreas Gohr } 1313df72098SAndreas Gohr echo '<div class="buttons">'.NL; 1321440e523SAnika Henke echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.NL; 1333df72098SAndreas Gohr echo '<input name="do[save]" type="submit" value="'.$lang['btn_save']. 13407493d05SAnika Henke '" title="'.$lang['btn_save'].' [S]" accesskey="s" class="button" />'.NL; 1353df72098SAndreas Gohr echo '<input name="do[cancel]" type="submit" value="'.$lang['btn_cancel']. 13607493d05SAnika Henke '" title="'.$lang['btn_cancel'].' [C]" accesskey="c" class="button" />'.NL; 1374868e1c6SAndreas Gohr echo '</div>'.NL; 1383df72098SAndreas Gohr echo '</form>'.NL; 1393df72098SAndreas Gohr} 1403df72098SAndreas Gohr 141a05e297aSAndreas Gohr 1423df72098SAndreas Gohr/** 1433df72098SAndreas Gohr * Handles media file deletions 1443df72098SAndreas Gohr * 1453df72098SAndreas Gohr * If configured, checks for media references before deletion 1463df72098SAndreas Gohr * 1473df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1483df72098SAndreas Gohr * @return mixed false on error, true on delete or array with refs 1493df72098SAndreas Gohr */ 1503df72098SAndreas Gohrfunction media_delete($id,$auth){ 1513df72098SAndreas Gohr if($auth < AUTH_DELETE) return false; 152f2ea8432SAndreas Gohr if(!checkSecurityToken()) return false; 1533df72098SAndreas Gohr global $conf; 1543df72098SAndreas Gohr global $lang; 1553df72098SAndreas Gohr 156a05e297aSAndreas Gohr // check for references if needed 1573df72098SAndreas Gohr $mediareferences = array(); 1583df72098SAndreas Gohr if($conf['refcheck']){ 159a05e297aSAndreas Gohr require_once(DOKU_INC.'inc/fulltext.php'); 160a05e297aSAndreas Gohr $mediareferences = ft_mediause($id,$conf['refshow']); 1613df72098SAndreas Gohr } 1623df72098SAndreas Gohr 1633df72098SAndreas Gohr if(!count($mediareferences)){ 1643df72098SAndreas Gohr $file = mediaFN($id); 1654a961e72SMichal Kolodziejski 1664a961e72SMichal Kolodziejski // trigger an event - MEDIA_DELETE_FILE 1674a961e72SMichal Kolodziejski $data['name'] = basename($file); 1684a961e72SMichal Kolodziejski $data['path'] = $file; 1694a961e72SMichal Kolodziejski $data['size'] = (@file_exists($file)) ? filesize($file) : 0; 1704a961e72SMichal Kolodziejski $evt = new Doku_Event('MEDIA_DELETE_FILE',$data); 1714a961e72SMichal Kolodziejski if ($evt->advise_before()) { 1723df72098SAndreas Gohr if(@unlink($file)){ 1733df72098SAndreas Gohr msg(str_replace('%s',noNS($id),$lang['deletesucc']),1); 174d186898bSAndreas Gohr $del = io_sweepNS($id,'mediadir'); 175d186898bSAndreas Gohr if($del){ 176d186898bSAndreas Gohr // current namespace was removed. redirecting to root ns passing msg along 177d186898bSAndreas Gohr header('Location: '.DOKU_URL.'lib/exe/mediamanager.php?msg1='. 178d186898bSAndreas Gohr rawurlencode(str_replace('%s',noNS($id),$lang['deletesucc']))); 179d186898bSAndreas Gohr exit; 180d186898bSAndreas Gohr } 1813df72098SAndreas Gohr return true; 1823df72098SAndreas Gohr } 1834a961e72SMichal Kolodziejski } 1844a961e72SMichal Kolodziejski $evt->advise_after(); 1854a961e72SMichal Kolodziejski unset($evt); 1864a961e72SMichal Kolodziejski 1873df72098SAndreas Gohr //something went wrong 1883df72098SAndreas Gohr msg(str_replace('%s',$file,$lang['deletefail']),-1); 1893df72098SAndreas Gohr return false; 1903df72098SAndreas Gohr }elseif(!$conf['refshow']){ 1913df72098SAndreas Gohr msg(str_replace('%s',noNS($id),$lang['mediainuse']),0); 1923df72098SAndreas Gohr return false; 1933df72098SAndreas Gohr } 1943df72098SAndreas Gohr 1953df72098SAndreas Gohr return $mediareferences; 1963df72098SAndreas Gohr} 1973df72098SAndreas Gohr 1983df72098SAndreas Gohr/** 1993df72098SAndreas Gohr * Handles media file uploads 2003df72098SAndreas Gohr * 20111d9dfa5SMichael Klier * This generates an action event and delegates to _media_upload_action(). 20211d9dfa5SMichael Klier * Action plugins are allowed to pre/postprocess the uploaded file. 20311d9dfa5SMichael Klier * (The triggered event is preventable.) 20411d9dfa5SMichael Klier * 20511d9dfa5SMichael Klier * Event data: 20611d9dfa5SMichael Klier * $data[0] fn_tmp: the temporary file name (read from $_FILES) 20711d9dfa5SMichael Klier * $data[1] fn: the file name of the uploaded file 20811d9dfa5SMichael Klier * $data[2] id: the future directory id of the uploaded file 20911d9dfa5SMichael Klier * $data[3] imime: the mimetype of the uploaded file 21011d9dfa5SMichael Klier * 21111d9dfa5SMichael Klier * @triggers MEDIA_UPLOAD_FINISH 2123df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 21311d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de> 2143df72098SAndreas Gohr * @return mixed false on error, id of the new file on success 2153df72098SAndreas Gohr */ 2163df72098SAndreas Gohrfunction media_upload($ns,$auth){ 2173df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return false; 218f2ea8432SAndreas Gohr if(!checkSecurityToken()) return false; 2193df72098SAndreas Gohr require_once(DOKU_INC.'inc/confutils.php'); 2203df72098SAndreas Gohr global $lang; 2213df72098SAndreas Gohr global $conf; 2223df72098SAndreas Gohr 22344409c3dSAndreas Gohr // get file and id 2243df72098SAndreas Gohr $id = $_POST['id']; 2253df72098SAndreas Gohr $file = $_FILES['upload']; 2263df72098SAndreas Gohr if(empty($id)) $id = $file['name']; 22744409c3dSAndreas Gohr 2289676dc23SAndreas Gohr // check for data 2299676dc23SAndreas Gohr if(!@filesize($file['tmp_name'])){ 2309676dc23SAndreas Gohr msg('No data uploaded. Disk full?',-1); 2319676dc23SAndreas Gohr return false; 2329676dc23SAndreas Gohr } 2339676dc23SAndreas Gohr 23444409c3dSAndreas Gohr // check extensions 235ecebf3a8SAndreas Gohr list($fext,$fmime,$dl) = mimetype($file['name']); 236ecebf3a8SAndreas Gohr list($iext,$imime,$dl) = mimetype($id); 23744409c3dSAndreas Gohr if($fext && !$iext){ 2388cb1eb01SAndreas Gohr // no extension specified in id - read original one 23944409c3dSAndreas Gohr $id .= '.'.$fext; 2408cb1eb01SAndreas Gohr $imime = $fmime; 24144409c3dSAndreas Gohr }elseif($fext && $fext != $iext){ 24244409c3dSAndreas Gohr // extension was changed, print warning 24344409c3dSAndreas Gohr msg(sprintf($lang['mediaextchange'],$fext,$iext)); 24444409c3dSAndreas Gohr } 24544409c3dSAndreas Gohr 2463df72098SAndreas Gohr // get filename 24763b0c1a7SGina Haeussge $id = cleanID($ns.':'.$id,false,true); 2483df72098SAndreas Gohr $fn = mediaFN($id); 2493df72098SAndreas Gohr 2503df72098SAndreas Gohr // get filetype regexp 2513df72098SAndreas Gohr $types = array_keys(getMimeTypes()); 2523df72098SAndreas Gohr $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types); 2533df72098SAndreas Gohr $regex = join('|',$types); 2543df72098SAndreas Gohr 2553df72098SAndreas Gohr // because a temp file was created already 2563df72098SAndreas Gohr if(preg_match('/\.('.$regex.')$/i',$fn)){ 2573df72098SAndreas Gohr //check for overwrite 258d00ec455SAndreas Gohr if(@file_exists($fn) && (!$_REQUEST['ow'] || $auth < AUTH_DELETE)){ 2593df72098SAndreas Gohr msg($lang['uploadexist'],0); 2603df72098SAndreas Gohr return false; 2613df72098SAndreas Gohr } 2628cb1eb01SAndreas Gohr // check for valid content 2638cb1eb01SAndreas Gohr $ok = media_contentcheck($file['tmp_name'],$imime); 2648cb1eb01SAndreas Gohr if($ok == -1){ 2658cb1eb01SAndreas Gohr msg(sprintf($lang['uploadbadcontent'],".$iext"),-1); 2668cb1eb01SAndreas Gohr return false; 2678cb1eb01SAndreas Gohr }elseif($ok == -2){ 2688cb1eb01SAndreas Gohr msg($lang['uploadspam'],-1); 2698cb1eb01SAndreas Gohr return false; 27026ceae18SAndreas Gohr }elseif($ok == -3){ 27126ceae18SAndreas Gohr msg($lang['uploadxss'],-1); 27226ceae18SAndreas Gohr return false; 2738cb1eb01SAndreas Gohr } 2748cb1eb01SAndreas Gohr 27511d9dfa5SMichael Klier // prepare event data 27611d9dfa5SMichael Klier $data[0] = $file['tmp_name']; 27711d9dfa5SMichael Klier $data[1] = $fn; 27811d9dfa5SMichael Klier $data[2] = $id; 27911d9dfa5SMichael Klier $data[3] = $imime; 28011d9dfa5SMichael Klier 28111d9dfa5SMichael Klier // trigger event 28211d9dfa5SMichael Klier return trigger_event('MEDIA_UPLOAD_FINISH', $data, '_media_upload_action', true); 28311d9dfa5SMichael Klier 28411d9dfa5SMichael Klier }else{ 28511d9dfa5SMichael Klier msg($lang['uploadwrong'],-1); 28611d9dfa5SMichael Klier } 28711d9dfa5SMichael Klier return false; 28811d9dfa5SMichael Klier} 28911d9dfa5SMichael Klier 29011d9dfa5SMichael Klier/** 29111d9dfa5SMichael Klier * Callback adapter for media_upload_finish() 29211d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de> 29311d9dfa5SMichael Klier */ 29411d9dfa5SMichael Klierfunction _media_upload_action($data) { 29511d9dfa5SMichael Klier // fixme do further sanity tests of given data? 29611d9dfa5SMichael Klier if(is_array($data) && count($data)===4) { 29711d9dfa5SMichael Klier return media_upload_finish($data[0], $data[1], $data[2], $data[3]); 29811d9dfa5SMichael Klier } else { 29911d9dfa5SMichael Klier return false; //callback error 30011d9dfa5SMichael Klier } 30111d9dfa5SMichael Klier} 30211d9dfa5SMichael Klier 30311d9dfa5SMichael Klier/** 30411d9dfa5SMichael Klier * Saves an uploaded media file 30511d9dfa5SMichael Klier * 30611d9dfa5SMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 30711d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de> 30811d9dfa5SMichael Klier */ 30911d9dfa5SMichael Klierfunction media_upload_finish($fn_tmp, $fn, $id, $imime) { 31011d9dfa5SMichael Klier global $conf; 31111d9dfa5SMichael Klier global $lang; 31211d9dfa5SMichael Klier 3133df72098SAndreas Gohr // prepare directory 314cc7d0c94SBen Coburn io_createNamespace($id, 'media'); 31511d9dfa5SMichael Klier 31611d9dfa5SMichael Klier if(move_uploaded_file($fn_tmp, $fn)) { 31774400ea5SBen Coburn // Set the correct permission here. 31874400ea5SBen Coburn // Always chmod media because they may be saved with different permissions than expected from the php umask. 31974400ea5SBen Coburn // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.) 32074400ea5SBen Coburn chmod($fn, $conf['fmode']); 3213df72098SAndreas Gohr msg($lang['uploadsucc'],1); 32275030359SAndreas Gohr media_notify($id,$fn,$imime); 3233df72098SAndreas Gohr return $id; 3243df72098SAndreas Gohr }else{ 3253df72098SAndreas Gohr msg($lang['uploadfail'],-1); 3263df72098SAndreas Gohr } 3273df72098SAndreas Gohr} 3283df72098SAndreas Gohr 3298cb1eb01SAndreas Gohr/** 3308cb1eb01SAndreas Gohr * This function checks if the uploaded content is really what the 33126ceae18SAndreas Gohr * mimetype says it is. We also do spam checking for text types here. 3328cb1eb01SAndreas Gohr * 3338cb1eb01SAndreas Gohr * We need to do this stuff because we can not rely on the browser 3348cb1eb01SAndreas Gohr * to do this check correctly. Yes, IE is broken as usual. 3358cb1eb01SAndreas Gohr * 3368cb1eb01SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 33726ceae18SAndreas Gohr * @link http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting 3388cb1eb01SAndreas Gohr * @fixme check all 26 magic IE filetypes here? 3398cb1eb01SAndreas Gohr */ 3408cb1eb01SAndreas Gohrfunction media_contentcheck($file,$mime){ 34126ceae18SAndreas Gohr global $conf; 34226ceae18SAndreas Gohr if($conf['iexssprotect']){ 34326ceae18SAndreas Gohr $fh = @fopen($file, 'rb'); 34426ceae18SAndreas Gohr if($fh){ 34526ceae18SAndreas Gohr $bytes = fread($fh, 256); 34626ceae18SAndreas Gohr fclose($fh); 34726ceae18SAndreas Gohr if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){ 34826ceae18SAndreas Gohr return -3; 34926ceae18SAndreas Gohr } 35026ceae18SAndreas Gohr } 35126ceae18SAndreas Gohr } 3528cb1eb01SAndreas Gohr if(substr($mime,0,6) == 'image/'){ 3538cb1eb01SAndreas Gohr $info = @getimagesize($file); 3548cb1eb01SAndreas Gohr if($mime == 'image/gif' && $info[2] != 1){ 3558cb1eb01SAndreas Gohr return -1; 3568cb1eb01SAndreas Gohr }elseif($mime == 'image/jpeg' && $info[2] != 2){ 3578cb1eb01SAndreas Gohr return -1; 3588cb1eb01SAndreas Gohr }elseif($mime == 'image/png' && $info[2] != 3){ 3598cb1eb01SAndreas Gohr return -1; 3608cb1eb01SAndreas Gohr } 3618cb1eb01SAndreas Gohr # fixme maybe check other images types as well 3628cb1eb01SAndreas Gohr }elseif(substr($mime,0,5) == 'text/'){ 3638cb1eb01SAndreas Gohr global $TEXT; 3648cb1eb01SAndreas Gohr $TEXT = io_readFile($file); 3658cb1eb01SAndreas Gohr if(checkwordblock()){ 3668cb1eb01SAndreas Gohr return -2; 3678cb1eb01SAndreas Gohr } 3688cb1eb01SAndreas Gohr } 3698cb1eb01SAndreas Gohr return 0; 3708cb1eb01SAndreas Gohr} 3713df72098SAndreas Gohr 3723df72098SAndreas Gohr/** 37375030359SAndreas Gohr * Send a notify mail on uploads 37475030359SAndreas Gohr * 37575030359SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 37675030359SAndreas Gohr */ 37775030359SAndreas Gohrfunction media_notify($id,$file,$mime){ 37875030359SAndreas Gohr global $lang; 37975030359SAndreas Gohr global $conf; 38075030359SAndreas Gohr if(empty($conf['notify'])) return; //notify enabled? 38175030359SAndreas Gohr 38275030359SAndreas Gohr $text = rawLocale('uploadmail'); 383e656dcd4SAndreas Gohr $text = str_replace('@DATE@',strftime($conf['dformat']),$text); 38475030359SAndreas Gohr $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 38575030359SAndreas Gohr $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 38675030359SAndreas Gohr $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 38775030359SAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 38875030359SAndreas Gohr $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 38975030359SAndreas Gohr $text = str_replace('@MIME@',$mime,$text); 39055b2b31bSAndreas Gohr $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text); 39175030359SAndreas Gohr $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text); 39275030359SAndreas Gohr 39375030359SAndreas Gohr $from = $conf['mailfrom']; 39475030359SAndreas Gohr $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from); 39575030359SAndreas Gohr $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from); 39675030359SAndreas Gohr $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from); 39775030359SAndreas Gohr 39875030359SAndreas Gohr $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id; 39975030359SAndreas Gohr 40075030359SAndreas Gohr mail_send($conf['notify'],$subject,$text,$from); 40175030359SAndreas Gohr} 40275030359SAndreas Gohr 40375030359SAndreas Gohr/** 4043df72098SAndreas Gohr * List all files in a given Media namespace 4053df72098SAndreas Gohr */ 4063df72098SAndreas Gohrfunction media_filelist($ns,$auth=null,$jump=''){ 4073df72098SAndreas Gohr global $conf; 4083df72098SAndreas Gohr global $lang; 4093df72098SAndreas Gohr $ns = cleanID($ns); 4103df72098SAndreas Gohr 4113df72098SAndreas Gohr // check auth our self if not given (needed for ajax calls) 4123df72098SAndreas Gohr if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 4133df72098SAndreas Gohr 4146c48a22eSAndreas Gohr echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL; 4153df72098SAndreas Gohr 4163df72098SAndreas Gohr if($auth < AUTH_READ){ 4173df72098SAndreas Gohr // FIXME: print permission warning here instead? 4183df72098SAndreas Gohr echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 4193df72098SAndreas Gohr return; 4203df72098SAndreas Gohr } 4213df72098SAndreas Gohr 4223df72098SAndreas Gohr media_uploadform($ns, $auth); 4233df72098SAndreas Gohr 4243df72098SAndreas Gohr $dir = utf8_encodeFN(str_replace(':','/',$ns)); 4253df72098SAndreas Gohr $data = array(); 42664807c84SAndreas Gohr search($data,$conf['mediadir'],'search_media',array('showmsg'=>true),$dir); 4273df72098SAndreas Gohr 4283df72098SAndreas Gohr if(!count($data)){ 4293df72098SAndreas Gohr echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 4303df72098SAndreas Gohr return; 4313df72098SAndreas Gohr } 4323df72098SAndreas Gohr 4333df72098SAndreas Gohr foreach($data as $item){ 4343df72098SAndreas Gohr media_printfile($item,$auth,$jump); 4353df72098SAndreas Gohr } 4363df72098SAndreas Gohr} 4373df72098SAndreas Gohr 4383df72098SAndreas Gohr/** 4393df72098SAndreas Gohr * Print action links for a file depending on filetype 4403df72098SAndreas Gohr * and available permissions 4413df72098SAndreas Gohr * 4423df72098SAndreas Gohr * @todo contains inline javascript 4433df72098SAndreas Gohr */ 4443df72098SAndreas Gohrfunction media_fileactions($item,$auth){ 4453df72098SAndreas Gohr global $lang; 4463df72098SAndreas Gohr 447cf6894dfSAndreas Gohr // view button 448cf6894dfSAndreas Gohr $link = ml($item['id'],'',true); 449cf6894dfSAndreas Gohr echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '. 450cf6894dfSAndreas Gohr 'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>'; 451cf6894dfSAndreas Gohr 452cf6894dfSAndreas Gohr 453cf6894dfSAndreas Gohr // no further actions if not writable 4543df72098SAndreas Gohr if(!$item['writable']) return; 4553df72098SAndreas Gohr 4563df72098SAndreas Gohr // delete button 4573df72098SAndreas Gohr if($auth >= AUTH_DELETE){ 458f2ea8432SAndreas Gohr echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']). 459c55fac47SMichael Klier '&sectok='.getSecurityToken().'" class="btn_media_delete" title="'.$item['id'].'">'. 4603df72098SAndreas Gohr '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '. 4613df72098SAndreas Gohr 'title="'.$lang['btn_delete'].'" class="btn" /></a>'; 4623df72098SAndreas Gohr } 4633df72098SAndreas Gohr 4643df72098SAndreas Gohr // edit button 4653df72098SAndreas Gohr if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){ 4663df72098SAndreas Gohr echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'. 4673df72098SAndreas Gohr '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '. 4683df72098SAndreas Gohr 'title="'.$lang['metaedit'].'" class="btn" /></a>'; 4693df72098SAndreas Gohr } 4703df72098SAndreas Gohr 4713df72098SAndreas Gohr} 4723df72098SAndreas Gohr 4733df72098SAndreas Gohr/** 4743df72098SAndreas Gohr * Formats and prints one file in the list 4753df72098SAndreas Gohr */ 4763df72098SAndreas Gohrfunction media_printfile($item,$auth,$jump){ 47764c9cfd5SAndreas Gohr global $lang; 4785e7fa82eSAndreas Gohr global $conf; 47964c9cfd5SAndreas Gohr 4803df72098SAndreas Gohr // Prepare zebra coloring 4813df72098SAndreas Gohr // I always wanted to use this variable name :-D 4823df72098SAndreas Gohr static $twibble = 1; 4833df72098SAndreas Gohr $twibble *= -1; 4843df72098SAndreas Gohr $zebra = ($twibble == -1) ? 'odd' : 'even'; 4853df72098SAndreas Gohr 4863df72098SAndreas Gohr // Automatically jump to recent action 4873df72098SAndreas Gohr if($jump == $item['id']) { 4883df72098SAndreas Gohr $jump = ' id="scroll__here" '; 4893df72098SAndreas Gohr }else{ 4903df72098SAndreas Gohr $jump = ''; 4913df72098SAndreas Gohr } 4923df72098SAndreas Gohr 4933df72098SAndreas Gohr // Prepare fileicons 494ecebf3a8SAndreas Gohr list($ext,$mime,$dl) = mimetype($item['file']); 4953df72098SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 4963df72098SAndreas Gohr $class = 'select mediafile mf_'.$class; 4973df72098SAndreas Gohr 4983df72098SAndreas Gohr // Prepare filename 4993df72098SAndreas Gohr $file = utf8_decodeFN($item['file']); 5003df72098SAndreas Gohr 5013df72098SAndreas Gohr // Prepare info 5023df72098SAndreas Gohr $info = ''; 5033df72098SAndreas Gohr if($item['isimg']){ 5043df72098SAndreas Gohr $info .= (int) $item['meta']->getField('File.Width'); 5053df72098SAndreas Gohr $info .= '×'; 5063df72098SAndreas Gohr $info .= (int) $item['meta']->getField('File.Height'); 5073df72098SAndreas Gohr $info .= ' '; 5083df72098SAndreas Gohr } 509e656dcd4SAndreas Gohr $info .= '<i>'.strftime($conf['dformat'],$item['mtime']).'</i>'; 5105e7fa82eSAndreas Gohr $info .= ' '; 5113df72098SAndreas Gohr $info .= filesize_h($item['size']); 5123df72098SAndreas Gohr 5133df72098SAndreas Gohr // ouput 5143df72098SAndreas Gohr echo '<div class="'.$zebra.'"'.$jump.'>'.NL; 5153df72098SAndreas Gohr echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> '; 5163df72098SAndreas Gohr echo '<span class="info">('.$info.')</span>'.NL; 5173df72098SAndreas Gohr media_fileactions($item,$auth); 518f495da32SAnika Henke echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">'; 5190b173dceSAndreas Gohr echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>'; 52064c9cfd5SAndreas Gohr echo '</div>'; 5213df72098SAndreas Gohr if($item['isimg']) media_printimgdetail($item); 5223df72098SAndreas Gohr echo '<div class="clearer"></div>'.NL; 5233df72098SAndreas Gohr echo '</div>'.NL; 5243df72098SAndreas Gohr} 5253df72098SAndreas Gohr 5263df72098SAndreas Gohr/** 5273df72098SAndreas Gohr * Prints a thumbnail and metainfos 5283df72098SAndreas Gohr */ 5293df72098SAndreas Gohrfunction media_printimgdetail($item){ 5303df72098SAndreas Gohr // prepare thumbnail 5313df72098SAndreas Gohr $w = (int) $item['meta']->getField('File.Width'); 5323df72098SAndreas Gohr $h = (int) $item['meta']->getField('File.Height'); 5333df72098SAndreas Gohr if($w>120 || $h>120){ 5343df72098SAndreas Gohr $ratio = $item['meta']->getResizeRatio(120); 5353df72098SAndreas Gohr $w = floor($w * $ratio); 5363df72098SAndreas Gohr $h = floor($h * $ratio); 5373df72098SAndreas Gohr } 5383df72098SAndreas Gohr $src = ml($item['id'],array('w'=>$w,'h'=>$h)); 5393df72098SAndreas Gohr $p = array(); 5403df72098SAndreas Gohr $p['width'] = $w; 5413df72098SAndreas Gohr $p['height'] = $h; 5423df72098SAndreas Gohr $p['alt'] = $item['id']; 5433df72098SAndreas Gohr $p['class'] = 'thumb'; 5443df72098SAndreas Gohr $att = buildAttributes($p); 5453df72098SAndreas Gohr 5463df72098SAndreas Gohr // output 5473df72098SAndreas Gohr echo '<div class="detail">'; 5483df72098SAndreas Gohr echo '<div class="thumb">'; 5493df72098SAndreas Gohr echo '<a name="d_'.$item['id'].'" class="select">'; 5503df72098SAndreas Gohr echo '<img src="'.$src.'" '.$att.' />'; 5513df72098SAndreas Gohr echo '</a>'; 5523df72098SAndreas Gohr echo '</div>'; 5533df72098SAndreas Gohr 5543df72098SAndreas Gohr // read EXIF/IPTC data 55549ac3837Shakan.sandell $t = $item['meta']->getField(array('IPTC.Headline','xmp.dc:title')); 5560b173dceSAndreas Gohr $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment', 5573df72098SAndreas Gohr 'EXIF.TIFFImageDescription', 5583df72098SAndreas Gohr 'EXIF.TIFFUserComment')); 5590b173dceSAndreas Gohr if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...'; 56049ac3837Shakan.sandell $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject')); 5613df72098SAndreas Gohr 5620b173dceSAndreas Gohr // print EXIF/IPTC data 5630b173dceSAndreas Gohr if($t || $d || $k ){ 5640b173dceSAndreas Gohr echo '<p>'; 5650b173dceSAndreas Gohr if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />'; 5660b173dceSAndreas Gohr if($d) echo htmlspecialchars($d).'<br />'; 5670b173dceSAndreas Gohr if($t) echo '<em>'.htmlspecialchars($k).'</em>'; 5683df72098SAndreas Gohr echo '</p>'; 5690b173dceSAndreas Gohr } 5703df72098SAndreas Gohr echo '</div>'; 5713df72098SAndreas Gohr} 5723df72098SAndreas Gohr 5733df72098SAndreas Gohr/** 5743df72098SAndreas Gohr * Print the media upload form if permissions are correct 5753df72098SAndreas Gohr * 5763df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 5773df72098SAndreas Gohr */ 5783df72098SAndreas Gohrfunction media_uploadform($ns, $auth){ 5793df72098SAndreas Gohr global $lang; 5803df72098SAndreas Gohr 5813df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions? 5823df72098SAndreas Gohr 583d00ec455SAndreas Gohr // The default HTML upload form 5849f5dde7fSMichael Klier $form = new Doku_Form('dw__upload', DOKU_BASE.'lib/exe/mediamanager.php', false, 'multipart/form-data'); 585d00ec455SAndreas Gohr $form->addElement('<div class="upload">' . $lang['mediaupload'] . '</div>'); 5869f5dde7fSMichael Klier $form->addElement(formSecurityToken()); 5879f5dde7fSMichael Klier $form->addHidden('ns', hsc($ns)); 5889f5dde7fSMichael Klier $form->addElement(form_makeOpenTag('p')); 5899f5dde7fSMichael Klier $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file')); 5909f5dde7fSMichael Klier $form->addElement(form_makeCloseTag('p')); 5919f5dde7fSMichael Klier $form->addElement(form_makeOpenTag('p')); 5929f5dde7fSMichael Klier $form->addElement(form_makeTextField('id', '', $lang['txt_filename'].':', 'upload__name')); 5939f5dde7fSMichael Klier $form->addElement(form_makeButton('submit', '', $lang['btn_upload'])); 5949f5dde7fSMichael Klier $form->addElement(form_makeCloseTag('p')); 595cf6894dfSAndreas Gohr 5969f5dde7fSMichael Klier if($auth >= AUTH_DELETE){ 5979f5dde7fSMichael Klier $form->addElement(form_makeOpenTag('p')); 5989f5dde7fSMichael Klier $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check')); 5999f5dde7fSMichael Klier $form->addElement(form_makeCloseTag('p')); 6003df72098SAndreas Gohr } 6019f5dde7fSMichael Klier html_form('upload', $form); 602d00ec455SAndreas Gohr 603d00ec455SAndreas Gohr // prepare flashvars for multiupload 604d00ec455SAndreas Gohr $opt = array( 605d00ec455SAndreas Gohr 'L_gridname' => $lang['mu_gridname'] , 606d00ec455SAndreas Gohr 'L_gridsize' => $lang['mu_gridsize'] , 607d00ec455SAndreas Gohr 'L_gridstat' => $lang['mu_gridstat'] , 608d00ec455SAndreas Gohr 'L_namespace' => $lang['mu_namespace'] , 609d00ec455SAndreas Gohr 'L_overwrite' => $lang['txt_overwrt'], 610d00ec455SAndreas Gohr 'L_browse' => $lang['mu_browse'], 611d00ec455SAndreas Gohr 'L_upload' => $lang['btn_upload'], 612d00ec455SAndreas Gohr 'L_toobig' => $lang['mu_toobig'], 613d00ec455SAndreas Gohr 'L_ready' => $lang['mu_ready'], 614d00ec455SAndreas Gohr 'L_done' => $lang['mu_done'], 615d00ec455SAndreas Gohr 'L_fail' => $lang['mu_fail'], 616d00ec455SAndreas Gohr 'L_authfail' => $lang['mu_authfail'], 617d00ec455SAndreas Gohr 'L_progress' => $lang['mu_progress'], 618d00ec455SAndreas Gohr 'L_filetypes' => $lang['mu_filetypes'], 619d00ec455SAndreas Gohr 620d00ec455SAndreas Gohr 'O_ns' => ":$ns", 621d00ec455SAndreas Gohr 'O_backend' => 'mediamanager.php?'.session_name().'='.session_id(), 622d00ec455SAndreas Gohr 'O_size' => php_to_byte(ini_get('upload_max_filesize')), 623d00ec455SAndreas Gohr 'O_extensions'=> join('|',array_keys(getMimeTypes())), 624d00ec455SAndreas Gohr 'O_overwrite' => ($auth >= AUTH_DELETE), 625d00ec455SAndreas Gohr 'O_sectok' => getSecurityToken(), 626d00ec455SAndreas Gohr 'O_authtok' => auth_createToken(), 627d00ec455SAndreas Gohr ); 6288c8dd6eaSAndreas Gohr $var = buildURLparams($opt); 629d00ec455SAndreas Gohr // output the flash uploader 630d00ec455SAndreas Gohr ?> 631d00ec455SAndreas Gohr <div id="dw__flashupload" style="display:none"> 632d00ec455SAndreas Gohr <div class="upload"><?php echo $lang['mu_intro']?></div> 633d00ec455SAndreas Gohr <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" 634d00ec455SAndreas Gohr codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 6358c8dd6eaSAndreas Gohr <param name="movie" value="multipleUpload.swf?t=<?php echo time()?>" /> 636d00ec455SAndreas Gohr <param name="quality" value="high" /> 637d00ec455SAndreas Gohr <param name="bgcolor" value="#ffffff" /> 638d00ec455SAndreas Gohr <param name="FlashVars" value="<?php echo $var?>" /> 6398c8dd6eaSAndreas Gohr <embed src="multipleUpload.swf?t=<?php echo time()?>" quality="high" bgcolor="#ffffff" 640d00ec455SAndreas Gohr width="100%" height="100%" name="fileUpload" align="middle" 641d00ec455SAndreas Gohr play="true" loop="false" quality="high" FlashVars="<?php echo $var?>" 642d00ec455SAndreas Gohr allowScriptAccess="sameDomain" 643d00ec455SAndreas Gohr type="application/x-shockwave-flash" 644d00ec455SAndreas Gohr pluginspage="http://www.macromedia.com/go/getflashplayer"> 645d00ec455SAndreas Gohr </embed> 646d00ec455SAndreas Gohr </object> 647d00ec455SAndreas Gohr </div> 648d00ec455SAndreas Gohr <?php 6499f5dde7fSMichael Klier} 6503df72098SAndreas Gohr 6513df72098SAndreas Gohr/** 6523df72098SAndreas Gohr * Build a tree outline of available media namespaces 6533df72098SAndreas Gohr * 6543df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6553df72098SAndreas Gohr */ 6563df72098SAndreas Gohrfunction media_nstree($ns){ 6573df72098SAndreas Gohr global $conf; 658256ca81eSAndreas Gohr global $lang; 6593df72098SAndreas Gohr 6603df72098SAndreas Gohr // currently selected namespace 6613df72098SAndreas Gohr $ns = cleanID($ns); 6623df72098SAndreas Gohr if(empty($ns)){ 6633df72098SAndreas Gohr $ns = dirname(str_replace(':','/',$ID)); 6643df72098SAndreas Gohr if($ns == '.') $ns =''; 6653df72098SAndreas Gohr } 6663df72098SAndreas Gohr $ns = utf8_encodeFN(str_replace(':','/',$ns)); 6673df72098SAndreas Gohr 6683df72098SAndreas Gohr $data = array(); 669ee7b5a62SAndreas Gohr search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true)); 6703df72098SAndreas Gohr 6713df72098SAndreas Gohr // wrap a list with the root level around the other namespaces 672256ca81eSAndreas Gohr $item = array( 'level' => 0, 'id' => '', 673256ca81eSAndreas Gohr 'open' =>'true', 'label' => '['.$lang['mediaroot'].']'); 6743df72098SAndreas Gohr 6753df72098SAndreas Gohr echo '<ul class="idx">'; 6763df72098SAndreas Gohr echo media_nstree_li($item); 6773df72098SAndreas Gohr echo media_nstree_item($item); 6783df72098SAndreas Gohr echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li'); 6793df72098SAndreas Gohr echo '</li>'; 6803df72098SAndreas Gohr echo '</ul>'; 6813df72098SAndreas Gohr} 6823df72098SAndreas Gohr 6833df72098SAndreas Gohr/** 6843df72098SAndreas Gohr * Userfunction for html_buildlist 6853df72098SAndreas Gohr * 6863df72098SAndreas Gohr * Prints a media namespace tree item 6873df72098SAndreas Gohr * 6883df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6893df72098SAndreas Gohr */ 6903df72098SAndreas Gohrfunction media_nstree_item($item){ 6913df72098SAndreas Gohr $pos = strrpos($item['id'], ':'); 6923df72098SAndreas Gohr $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0); 6933df72098SAndreas Gohr if(!$item['label']) $item['label'] = $label; 6943df72098SAndreas Gohr 6953df72098SAndreas Gohr $ret = ''; 6963df72098SAndreas Gohr $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">'; 6973df72098SAndreas Gohr $ret .= $item['label']; 6983df72098SAndreas Gohr $ret .= '</a>'; 6993df72098SAndreas Gohr return $ret; 7003df72098SAndreas Gohr} 7013df72098SAndreas Gohr 7023df72098SAndreas Gohr/** 7033df72098SAndreas Gohr * Userfunction for html_buildlist 7043df72098SAndreas Gohr * 7053df72098SAndreas Gohr * Prints a media namespace tree item opener 7063df72098SAndreas Gohr * 7073df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7083df72098SAndreas Gohr */ 7093df72098SAndreas Gohrfunction media_nstree_li($item){ 7103df72098SAndreas Gohr $class='media level'.$item['level']; 7113df72098SAndreas Gohr if($item['open']){ 7123df72098SAndreas Gohr $class .= ' open'; 7133df72098SAndreas Gohr $img = DOKU_BASE.'lib/images/minus.gif'; 7147af1b404SAnika Henke $alt = '−'; 7153df72098SAndreas Gohr }else{ 7163df72098SAndreas Gohr $class .= ' closed'; 7173df72098SAndreas Gohr $img = DOKU_BASE.'lib/images/plus.gif'; 7187af1b404SAnika Henke $alt = '+'; 7193df72098SAndreas Gohr } 7203df72098SAndreas Gohr return '<li class="'.$class.'">'. 7217af1b404SAnika Henke '<img src="'.$img.'" alt="'.$alt.'" />'; 7223df72098SAndreas Gohr} 72313c08e2fSMichael Klier 72413c08e2fSMichael Klier/** 72513c08e2fSMichael Klier * Resizes the given image to the given size 72613c08e2fSMichael Klier * 72713c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 72813c08e2fSMichael Klier */ 72913c08e2fSMichael Klierfunction media_resize_image($file, $ext, $w, $h=0){ 73013c08e2fSMichael Klier global $conf; 73113c08e2fSMichael Klier 73213c08e2fSMichael Klier $info = @getimagesize($file); //get original size 73313c08e2fSMichael Klier if($info == false) return $file; // that's no image - it's a spaceship! 73413c08e2fSMichael Klier 73513c08e2fSMichael Klier if(!$h) $h = round(($w * $info[1]) / $info[0]); 73613c08e2fSMichael Klier 73713c08e2fSMichael Klier // we wont scale up to infinity 73813c08e2fSMichael Klier if($w > 2000 || $h > 2000) return $file; 73913c08e2fSMichael Klier 74013c08e2fSMichael Klier //cache 74113c08e2fSMichael Klier $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext); 74213c08e2fSMichael Klier $mtime = @filemtime($local); // 0 if not exists 74313c08e2fSMichael Klier 74413c08e2fSMichael Klier if( $mtime > filemtime($file) || 74513c08e2fSMichael Klier media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) || 74613c08e2fSMichael Klier media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){ 74713c08e2fSMichael Klier if($conf['fperm']) chmod($local, $conf['fperm']); 74813c08e2fSMichael Klier return $local; 74913c08e2fSMichael Klier } 75013c08e2fSMichael Klier //still here? resizing failed 75113c08e2fSMichael Klier return $file; 75213c08e2fSMichael Klier} 75313c08e2fSMichael Klier 75413c08e2fSMichael Klier/** 75513c08e2fSMichael Klier * Crops the given image to the wanted ratio, then calls media_resize_image to scale it 75613c08e2fSMichael Klier * to the wanted size 75713c08e2fSMichael Klier * 75813c08e2fSMichael Klier * Crops are centered horizontally but prefer the upper third of an vertical 75913c08e2fSMichael Klier * image because most pics are more interesting in that area (rule of thirds) 76013c08e2fSMichael Klier * 76113c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 76213c08e2fSMichael Klier */ 76313c08e2fSMichael Klierfunction media_crop_image($file, $ext, $w, $h=0){ 76413c08e2fSMichael Klier global $conf; 76513c08e2fSMichael Klier 76613c08e2fSMichael Klier if(!$h) $h = $w; 76713c08e2fSMichael Klier $info = @getimagesize($file); //get original size 76813c08e2fSMichael Klier if($info == false) return $file; // that's no image - it's a spaceship! 76913c08e2fSMichael Klier 77013c08e2fSMichael Klier // calculate crop size 77113c08e2fSMichael Klier $fr = $info[0]/$info[1]; 77213c08e2fSMichael Klier $tr = $w/$h; 77313c08e2fSMichael Klier if($tr >= 1){ 77413c08e2fSMichael Klier if($tr > $fr){ 77513c08e2fSMichael Klier $cw = $info[0]; 77613c08e2fSMichael Klier $ch = (int) $info[0]/$tr; 77713c08e2fSMichael Klier }else{ 77813c08e2fSMichael Klier $cw = (int) $info[1]*$tr; 77913c08e2fSMichael Klier $ch = $info[1]; 78013c08e2fSMichael Klier } 78113c08e2fSMichael Klier }else{ 78213c08e2fSMichael Klier if($tr < $fr){ 78313c08e2fSMichael Klier $cw = (int) $info[1]*$tr; 78413c08e2fSMichael Klier $ch = $info[1]; 78513c08e2fSMichael Klier }else{ 78613c08e2fSMichael Klier $cw = $info[0]; 78713c08e2fSMichael Klier $ch = (int) $info[0]/$tr; 78813c08e2fSMichael Klier } 78913c08e2fSMichael Klier } 79013c08e2fSMichael Klier // calculate crop offset 79113c08e2fSMichael Klier $cx = (int) ($info[0]-$cw)/2; 79213c08e2fSMichael Klier $cy = (int) ($info[1]-$ch)/3; 79313c08e2fSMichael Klier 79413c08e2fSMichael Klier //cache 79513c08e2fSMichael Klier $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext); 79613c08e2fSMichael Klier $mtime = @filemtime($local); // 0 if not exists 79713c08e2fSMichael Klier 79813c08e2fSMichael Klier if( $mtime > filemtime($file) || 79913c08e2fSMichael Klier media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) || 80013c08e2fSMichael Klier media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){ 80113c08e2fSMichael Klier if($conf['fperm']) chmod($local, $conf['fperm']); 80213c08e2fSMichael Klier return media_resize_image($local,$ext, $w, $h); 80313c08e2fSMichael Klier } 80413c08e2fSMichael Klier 80513c08e2fSMichael Klier //still here? cropping failed 80613c08e2fSMichael Klier return media_resize_image($file,$ext, $w, $h); 80713c08e2fSMichael Klier} 80813c08e2fSMichael Klier 80913c08e2fSMichael Klier/** 81013c08e2fSMichael Klier * Download a remote file and return local filename 81113c08e2fSMichael Klier * 81213c08e2fSMichael Klier * returns false if download fails. Uses cached file if available and 81313c08e2fSMichael Klier * wanted 81413c08e2fSMichael Klier * 81513c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 81613c08e2fSMichael Klier * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 81713c08e2fSMichael Klier */ 81813c08e2fSMichael Klierfunction media_get_from_URL($url,$ext,$cache){ 81913c08e2fSMichael Klier global $conf; 82013c08e2fSMichael Klier 82113c08e2fSMichael Klier // if no cache or fetchsize just redirect 82213c08e2fSMichael Klier if ($cache==0) return false; 82313c08e2fSMichael Klier if (!$conf['fetchsize']) return false; 82413c08e2fSMichael Klier 82513c08e2fSMichael Klier $local = getCacheName(strtolower($url),".media.$ext"); 82613c08e2fSMichael Klier $mtime = @filemtime($local); // 0 if not exists 82713c08e2fSMichael Klier 82813c08e2fSMichael Klier //decide if download needed: 82913c08e2fSMichael Klier if( ($mtime == 0) || // cache does not exist 83013c08e2fSMichael Klier ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired 83113c08e2fSMichael Klier ){ 83213c08e2fSMichael Klier if(media_image_download($url,$local)){ 83313c08e2fSMichael Klier return $local; 83413c08e2fSMichael Klier }else{ 83513c08e2fSMichael Klier return false; 83613c08e2fSMichael Klier } 83713c08e2fSMichael Klier } 83813c08e2fSMichael Klier 83913c08e2fSMichael Klier //if cache exists use it else 84013c08e2fSMichael Klier if($mtime) return $local; 84113c08e2fSMichael Klier 84213c08e2fSMichael Klier //else return false 84313c08e2fSMichael Klier return false; 84413c08e2fSMichael Klier} 84513c08e2fSMichael Klier 84613c08e2fSMichael Klier/** 84713c08e2fSMichael Klier * Download image files 84813c08e2fSMichael Klier * 84913c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 85013c08e2fSMichael Klier */ 85113c08e2fSMichael Klierfunction media_image_download($url,$file){ 85213c08e2fSMichael Klier global $conf; 85313c08e2fSMichael Klier $http = new DokuHTTPClient(); 85413c08e2fSMichael Klier $http->max_bodysize = $conf['fetchsize']; 85513c08e2fSMichael Klier $http->timeout = 25; //max. 25 sec 85613c08e2fSMichael Klier $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i'; 85713c08e2fSMichael Klier 85813c08e2fSMichael Klier $data = $http->get($url); 85913c08e2fSMichael Klier if(!$data) return false; 86013c08e2fSMichael Klier 86113c08e2fSMichael Klier $fileexists = @file_exists($file); 86213c08e2fSMichael Klier $fp = @fopen($file,"w"); 86313c08e2fSMichael Klier if(!$fp) return false; 86413c08e2fSMichael Klier fwrite($fp,$data); 86513c08e2fSMichael Klier fclose($fp); 86613c08e2fSMichael Klier if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']); 86713c08e2fSMichael Klier 86813c08e2fSMichael Klier // check if it is really an image 86913c08e2fSMichael Klier $info = @getimagesize($file); 87013c08e2fSMichael Klier if(!$info){ 87113c08e2fSMichael Klier @unlink($file); 87213c08e2fSMichael Klier return false; 87313c08e2fSMichael Klier } 87413c08e2fSMichael Klier 87513c08e2fSMichael Klier return true; 87613c08e2fSMichael Klier} 87713c08e2fSMichael Klier 87813c08e2fSMichael Klier/** 87913c08e2fSMichael Klier * resize images using external ImageMagick convert program 88013c08e2fSMichael Klier * 88113c08e2fSMichael Klier * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 88213c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 88313c08e2fSMichael Klier */ 88413c08e2fSMichael Klierfunction media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 88513c08e2fSMichael Klier global $conf; 88613c08e2fSMichael Klier 88713c08e2fSMichael Klier // check if convert is configured 88813c08e2fSMichael Klier if(!$conf['im_convert']) return false; 88913c08e2fSMichael Klier 89013c08e2fSMichael Klier // prepare command 89113c08e2fSMichael Klier $cmd = $conf['im_convert']; 89213c08e2fSMichael Klier $cmd .= ' -resize '.$to_w.'x'.$to_h.'!'; 89313c08e2fSMichael Klier if ($ext == 'jpg' || $ext == 'jpeg') { 89413c08e2fSMichael Klier $cmd .= ' -quality '.$conf['jpg_quality']; 89513c08e2fSMichael Klier } 89613c08e2fSMichael Klier $cmd .= " $from $to"; 89713c08e2fSMichael Klier 89813c08e2fSMichael Klier @exec($cmd,$out,$retval); 89913c08e2fSMichael Klier if ($retval == 0) return true; 90013c08e2fSMichael Klier return false; 90113c08e2fSMichael Klier} 90213c08e2fSMichael Klier 90313c08e2fSMichael Klier/** 90413c08e2fSMichael Klier * crop images using external ImageMagick convert program 90513c08e2fSMichael Klier * 90613c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 90713c08e2fSMichael Klier */ 90813c08e2fSMichael Klierfunction media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){ 90913c08e2fSMichael Klier global $conf; 91013c08e2fSMichael Klier 91113c08e2fSMichael Klier // check if convert is configured 91213c08e2fSMichael Klier if(!$conf['im_convert']) return false; 91313c08e2fSMichael Klier 91413c08e2fSMichael Klier // prepare command 91513c08e2fSMichael Klier $cmd = $conf['im_convert']; 91613c08e2fSMichael Klier $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y; 91713c08e2fSMichael Klier if ($ext == 'jpg' || $ext == 'jpeg') { 91813c08e2fSMichael Klier $cmd .= ' -quality '.$conf['jpg_quality']; 91913c08e2fSMichael Klier } 92013c08e2fSMichael Klier $cmd .= " $from $to"; 92113c08e2fSMichael Klier 92213c08e2fSMichael Klier @exec($cmd,$out,$retval); 92313c08e2fSMichael Klier if ($retval == 0) return true; 92413c08e2fSMichael Klier return false; 92513c08e2fSMichael Klier} 92613c08e2fSMichael Klier 92713c08e2fSMichael Klier/** 92813c08e2fSMichael Klier * resize or crop images using PHP's libGD support 92913c08e2fSMichael Klier * 93013c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 93113c08e2fSMichael Klier * @author Sebastian Wienecke <s_wienecke@web.de> 93213c08e2fSMichael Klier */ 93313c08e2fSMichael Klierfunction media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){ 93413c08e2fSMichael Klier global $conf; 93513c08e2fSMichael Klier 93613c08e2fSMichael Klier if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 93713c08e2fSMichael Klier 93813c08e2fSMichael Klier // check available memory 93913c08e2fSMichael Klier if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){ 94013c08e2fSMichael Klier return false; 94113c08e2fSMichael Klier } 94213c08e2fSMichael Klier 94313c08e2fSMichael Klier // create an image of the given filetype 94413c08e2fSMichael Klier if ($ext == 'jpg' || $ext == 'jpeg'){ 94513c08e2fSMichael Klier if(!function_exists("imagecreatefromjpeg")) return false; 94613c08e2fSMichael Klier $image = @imagecreatefromjpeg($from); 94713c08e2fSMichael Klier }elseif($ext == 'png') { 94813c08e2fSMichael Klier if(!function_exists("imagecreatefrompng")) return false; 94913c08e2fSMichael Klier $image = @imagecreatefrompng($from); 95013c08e2fSMichael Klier 95113c08e2fSMichael Klier }elseif($ext == 'gif') { 95213c08e2fSMichael Klier if(!function_exists("imagecreatefromgif")) return false; 95313c08e2fSMichael Klier $image = @imagecreatefromgif($from); 95413c08e2fSMichael Klier } 95513c08e2fSMichael Klier if(!$image) return false; 95613c08e2fSMichael Klier 95713c08e2fSMichael Klier if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){ 95813c08e2fSMichael Klier $newimg = @imagecreatetruecolor ($to_w, $to_h); 95913c08e2fSMichael Klier } 96013c08e2fSMichael Klier if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 96113c08e2fSMichael Klier if(!$newimg){ 96213c08e2fSMichael Klier imagedestroy($image); 96313c08e2fSMichael Klier return false; 96413c08e2fSMichael Klier } 96513c08e2fSMichael Klier 96613c08e2fSMichael Klier //keep png alpha channel if possible 96713c08e2fSMichael Klier if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 96813c08e2fSMichael Klier imagealphablending($newimg, false); 96913c08e2fSMichael Klier imagesavealpha($newimg,true); 97013c08e2fSMichael Klier } 97113c08e2fSMichael Klier 97213c08e2fSMichael Klier //keep gif transparent color if possible 97313c08e2fSMichael Klier if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) { 97413c08e2fSMichael Klier if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) { 97513c08e2fSMichael Klier $transcolorindex = @imagecolortransparent($image); 97613c08e2fSMichael Klier if($transcolorindex >= 0 ) { //transparent color exists 97713c08e2fSMichael Klier $transcolor = @imagecolorsforindex($image, $transcolorindex); 97813c08e2fSMichael Klier $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']); 97913c08e2fSMichael Klier @imagefill($newimg, 0, 0, $transcolorindex); 98013c08e2fSMichael Klier @imagecolortransparent($newimg, $transcolorindex); 98113c08e2fSMichael Klier }else{ //filling with white 98213c08e2fSMichael Klier $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255); 98313c08e2fSMichael Klier @imagefill($newimg, 0, 0, $whitecolorindex); 98413c08e2fSMichael Klier } 98513c08e2fSMichael Klier }else{ //filling with white 98613c08e2fSMichael Klier $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255); 98713c08e2fSMichael Klier @imagefill($newimg, 0, 0, $whitecolorindex); 98813c08e2fSMichael Klier } 98913c08e2fSMichael Klier } 99013c08e2fSMichael Klier 99113c08e2fSMichael Klier //try resampling first 99213c08e2fSMichael Klier if(function_exists("imagecopyresampled")){ 99313c08e2fSMichael Klier if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) { 99413c08e2fSMichael Klier imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h); 99513c08e2fSMichael Klier } 99613c08e2fSMichael Klier }else{ 99713c08e2fSMichael Klier imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h); 99813c08e2fSMichael Klier } 99913c08e2fSMichael Klier 100013c08e2fSMichael Klier $okay = false; 100113c08e2fSMichael Klier if ($ext == 'jpg' || $ext == 'jpeg'){ 100213c08e2fSMichael Klier if(!function_exists('imagejpeg')){ 100313c08e2fSMichael Klier $okay = false; 100413c08e2fSMichael Klier }else{ 100513c08e2fSMichael Klier $okay = imagejpeg($newimg, $to, $conf['jpg_quality']); 100613c08e2fSMichael Klier } 100713c08e2fSMichael Klier }elseif($ext == 'png') { 100813c08e2fSMichael Klier if(!function_exists('imagepng')){ 100913c08e2fSMichael Klier $okay = false; 101013c08e2fSMichael Klier }else{ 101113c08e2fSMichael Klier $okay = imagepng($newimg, $to); 101213c08e2fSMichael Klier } 101313c08e2fSMichael Klier }elseif($ext == 'gif') { 101413c08e2fSMichael Klier if(!function_exists('imagegif')){ 101513c08e2fSMichael Klier $okay = false; 101613c08e2fSMichael Klier }else{ 101713c08e2fSMichael Klier $okay = imagegif($newimg, $to); 101813c08e2fSMichael Klier } 101913c08e2fSMichael Klier } 102013c08e2fSMichael Klier 102113c08e2fSMichael Klier // destroy GD image ressources 102213c08e2fSMichael Klier if($image) imagedestroy($image); 102313c08e2fSMichael Klier if($newimg) imagedestroy($newimg); 102413c08e2fSMichael Klier 102513c08e2fSMichael Klier return $okay; 102613c08e2fSMichael Klier} 102713c08e2fSMichael Klier 1028