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 900976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 103df72098SAndreas Gohrif(!defined('NL')) define('NL',"\n"); 113df72098SAndreas Gohr 123df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/html.php'); 133df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/search.php'); 143df72098SAndreas Gohrrequire_once(DOKU_INC.'inc/JpegMeta.php'); 153df72098SAndreas Gohr 163df72098SAndreas Gohr/** 173df72098SAndreas Gohr * Lists pages which currently use a media file selected for deletion 183df72098SAndreas Gohr * 193df72098SAndreas Gohr * References uses the same visual as search results and share 203df72098SAndreas Gohr * their CSS tags except pagenames won't be links. 213df72098SAndreas Gohr * 223df72098SAndreas Gohr * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 233df72098SAndreas Gohr */ 243df72098SAndreas Gohrfunction media_filesinuse($data,$id){ 253df72098SAndreas Gohr global $lang; 263df72098SAndreas Gohr echo '<h1>'.$lang['reference'].' <code>'.hsc(noNS($id)).'</code></h1>'; 273df72098SAndreas Gohr echo '<p>'.hsc($lang['ref_inuse']).'</p>'; 283df72098SAndreas Gohr 293df72098SAndreas Gohr $hidden=0; //count of hits without read permission 303df72098SAndreas Gohr foreach($data as $row){ 31a05e297aSAndreas Gohr if(auth_quickaclcheck($row) >= AUTH_READ && isVisiblePage($row)){ 323df72098SAndreas Gohr echo '<div class="search_result">'; 33a05e297aSAndreas Gohr echo '<span class="mediaref_ref">'.hsc($row).'</span>'; 343df72098SAndreas Gohr echo '</div>'; 353df72098SAndreas Gohr }else 363df72098SAndreas Gohr $hidden++; 373df72098SAndreas Gohr } 383df72098SAndreas Gohr if ($hidden){ 393df72098SAndreas Gohr print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>'; 403df72098SAndreas Gohr } 413df72098SAndreas Gohr} 423df72098SAndreas Gohr 433df72098SAndreas Gohr/** 443df72098SAndreas Gohr * Handles the saving of image meta data 453df72098SAndreas Gohr * 463df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 473df72098SAndreas Gohr */ 483df72098SAndreas Gohrfunction media_metasave($id,$auth,$data){ 493df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return false; 50f2ea8432SAndreas Gohr if(!checkSecurityToken()) return false; 513df72098SAndreas Gohr global $lang; 520b308644SOtto Vainio global $conf; 533df72098SAndreas Gohr $src = mediaFN($id); 543df72098SAndreas Gohr 553df72098SAndreas Gohr $meta = new JpegMeta($src); 563df72098SAndreas Gohr $meta->_parseAll(); 573df72098SAndreas Gohr 583df72098SAndreas Gohr foreach($data as $key => $val){ 593df72098SAndreas Gohr $val=trim($val); 603df72098SAndreas Gohr if(empty($val)){ 613df72098SAndreas Gohr $meta->deleteField($key); 623df72098SAndreas Gohr }else{ 633df72098SAndreas Gohr $meta->setField($key,$val); 643df72098SAndreas Gohr } 653df72098SAndreas Gohr } 663df72098SAndreas Gohr 673df72098SAndreas Gohr if($meta->save()){ 680b308644SOtto Vainio if($conf['fperm']) chmod($src, $conf['fperm']); 693df72098SAndreas Gohr msg($lang['metasaveok'],1); 703df72098SAndreas Gohr return $id; 713df72098SAndreas Gohr }else{ 723df72098SAndreas Gohr msg($lang['metasaveerr'],-1); 733df72098SAndreas Gohr return false; 743df72098SAndreas Gohr } 753df72098SAndreas Gohr} 763df72098SAndreas Gohr 773df72098SAndreas Gohr/** 783df72098SAndreas Gohr * Display the form to edit image meta data 793df72098SAndreas Gohr * 803df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 813df72098SAndreas Gohr */ 823df72098SAndreas Gohrfunction media_metaform($id,$auth){ 833df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return false; 843df72098SAndreas Gohr global $lang; 853df72098SAndreas Gohr 863df72098SAndreas Gohr // load the field descriptions 873df72098SAndreas Gohr static $fields = null; 883df72098SAndreas Gohr if(is_null($fields)){ 893df72098SAndreas Gohr include(DOKU_CONF.'mediameta.php'); 903df72098SAndreas Gohr if(@file_exists(DOKU_CONF.'mediameta.local.php')){ 913df72098SAndreas Gohr include(DOKU_CONF.'mediameta.local.php'); 923df72098SAndreas Gohr } 933df72098SAndreas Gohr } 943df72098SAndreas Gohr 953df72098SAndreas Gohr $src = mediaFN($id); 963df72098SAndreas Gohr 973df72098SAndreas Gohr // output 983df72098SAndreas Gohr echo '<h1>'.hsc(noNS($id)).'</h1>'.NL; 993df72098SAndreas Gohr echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" accept-charset="utf-8" method="post" class="meta">'.NL; 100f2ea8432SAndreas Gohr formSecurityToken(); 1013df72098SAndreas Gohr foreach($fields as $key => $field){ 1023df72098SAndreas Gohr // get current value 1033df72098SAndreas Gohr $tags = array($field[0]); 1043df72098SAndreas Gohr if(is_array($field[3])) $tags = array_merge($tags,$field[3]); 1053df72098SAndreas Gohr $value = tpl_img_getTag($tags,'',$src); 106ca6a0701SAndreas Gohr $value = cleanText($value); 1073df72098SAndreas Gohr 1083df72098SAndreas Gohr // prepare attributes 1093df72098SAndreas Gohr $p = array(); 1103df72098SAndreas Gohr $p['class'] = 'edit'; 1113df72098SAndreas Gohr $p['id'] = 'meta__'.$key; 1123df72098SAndreas Gohr $p['name'] = 'meta['.$field[0].']'; 1133df72098SAndreas Gohr 1143df72098SAndreas Gohr // put label 1153df72098SAndreas Gohr echo '<div class="metafield">'; 1163df72098SAndreas Gohr echo '<label for="meta__'.$key.'">'; 1173df72098SAndreas Gohr echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1]; 118cf6894dfSAndreas Gohr echo ':</label>'; 1193df72098SAndreas Gohr 1203df72098SAndreas Gohr // put input field 1213df72098SAndreas Gohr if($field[2] == 'text'){ 1223df72098SAndreas Gohr $p['value'] = $value; 1233df72098SAndreas Gohr $p['type'] = 'text'; 1243df72098SAndreas Gohr $att = buildAttributes($p); 1253df72098SAndreas Gohr echo "<input $att/>".NL; 1263df72098SAndreas Gohr }else{ 1273df72098SAndreas Gohr $att = buildAttributes($p); 1281440e523SAnika Henke echo "<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>'.NL; 1293df72098SAndreas Gohr } 1303df72098SAndreas Gohr echo '</div>'.NL; 1313df72098SAndreas Gohr } 1323df72098SAndreas Gohr echo '<div class="buttons">'.NL; 1331440e523SAnika Henke echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.NL; 1343df72098SAndreas Gohr echo '<input name="do[save]" type="submit" value="'.$lang['btn_save']. 1353df72098SAndreas Gohr '" title="ALT+S" accesskey="s" class="button" />'.NL; 1363df72098SAndreas Gohr echo '<input name="do[cancel]" type="submit" value="'.$lang['btn_cancel']. 1373df72098SAndreas Gohr '" title="ALT+C" accesskey="c" class="button" />'.NL; 1384868e1c6SAndreas Gohr echo '</div>'.NL; 1393df72098SAndreas Gohr echo '</form>'.NL; 1403df72098SAndreas Gohr} 1413df72098SAndreas Gohr 142a05e297aSAndreas Gohr 1433df72098SAndreas Gohr/** 1443df72098SAndreas Gohr * Handles media file deletions 1453df72098SAndreas Gohr * 1463df72098SAndreas Gohr * If configured, checks for media references before deletion 1473df72098SAndreas Gohr * 1483df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1493df72098SAndreas Gohr * @return mixed false on error, true on delete or array with refs 1503df72098SAndreas Gohr */ 1513df72098SAndreas Gohrfunction media_delete($id,$auth){ 1523df72098SAndreas Gohr if($auth < AUTH_DELETE) return false; 153f2ea8432SAndreas Gohr if(!checkSecurityToken()) return false; 1543df72098SAndreas Gohr global $conf; 1553df72098SAndreas Gohr global $lang; 1563df72098SAndreas Gohr 157a05e297aSAndreas Gohr // check for references if needed 1583df72098SAndreas Gohr $mediareferences = array(); 1593df72098SAndreas Gohr if($conf['refcheck']){ 160a05e297aSAndreas Gohr require_once(DOKU_INC.'inc/fulltext.php'); 161a05e297aSAndreas Gohr $mediareferences = ft_mediause($id,$conf['refshow']); 1623df72098SAndreas Gohr } 1633df72098SAndreas Gohr 1643df72098SAndreas Gohr if(!count($mediareferences)){ 1653df72098SAndreas Gohr $file = mediaFN($id); 1663df72098SAndreas Gohr if(@unlink($file)){ 1673df72098SAndreas Gohr msg(str_replace('%s',noNS($id),$lang['deletesucc']),1); 168d186898bSAndreas Gohr $del = io_sweepNS($id,'mediadir'); 169d186898bSAndreas Gohr if($del){ 170d186898bSAndreas Gohr // current namespace was removed. redirecting to root ns passing msg along 171d186898bSAndreas Gohr header('Location: '.DOKU_URL.'lib/exe/mediamanager.php?msg1='. 172d186898bSAndreas Gohr rawurlencode(str_replace('%s',noNS($id),$lang['deletesucc']))); 173d186898bSAndreas Gohr exit; 174d186898bSAndreas Gohr } 1753df72098SAndreas Gohr return true; 1763df72098SAndreas Gohr } 1773df72098SAndreas Gohr //something went wrong 1783df72098SAndreas Gohr msg(str_replace('%s',$file,$lang['deletefail']),-1); 1793df72098SAndreas Gohr return false; 1803df72098SAndreas Gohr }elseif(!$conf['refshow']){ 1813df72098SAndreas Gohr msg(str_replace('%s',noNS($id),$lang['mediainuse']),0); 1823df72098SAndreas Gohr return false; 1833df72098SAndreas Gohr } 1843df72098SAndreas Gohr 1853df72098SAndreas Gohr return $mediareferences; 1863df72098SAndreas Gohr} 1873df72098SAndreas Gohr 1883df72098SAndreas Gohr/** 1893df72098SAndreas Gohr * Handles media file uploads 1903df72098SAndreas Gohr * 19111d9dfa5SMichael Klier * This generates an action event and delegates to _media_upload_action(). 19211d9dfa5SMichael Klier * Action plugins are allowed to pre/postprocess the uploaded file. 19311d9dfa5SMichael Klier * (The triggered event is preventable.) 19411d9dfa5SMichael Klier * 19511d9dfa5SMichael Klier * Event data: 19611d9dfa5SMichael Klier * $data[0] fn_tmp: the temporary file name (read from $_FILES) 19711d9dfa5SMichael Klier * $data[1] fn: the file name of the uploaded file 19811d9dfa5SMichael Klier * $data[2] id: the future directory id of the uploaded file 19911d9dfa5SMichael Klier * $data[3] imime: the mimetype of the uploaded file 20011d9dfa5SMichael Klier * 20111d9dfa5SMichael Klier * @triggers MEDIA_UPLOAD_FINISH 2023df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 20311d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de> 2043df72098SAndreas Gohr * @return mixed false on error, id of the new file on success 2053df72098SAndreas Gohr */ 2063df72098SAndreas Gohrfunction media_upload($ns,$auth){ 2073df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return false; 208f2ea8432SAndreas Gohr if(!checkSecurityToken()) return false; 2093df72098SAndreas Gohr require_once(DOKU_INC.'inc/confutils.php'); 2103df72098SAndreas Gohr global $lang; 2113df72098SAndreas Gohr global $conf; 2123df72098SAndreas Gohr 21344409c3dSAndreas Gohr // get file and id 2143df72098SAndreas Gohr $id = $_POST['id']; 2153df72098SAndreas Gohr $file = $_FILES['upload']; 2163df72098SAndreas Gohr if(empty($id)) $id = $file['name']; 21744409c3dSAndreas Gohr 218*9676dc23SAndreas Gohr // check for data 219*9676dc23SAndreas Gohr if(!@filesize($file['tmp_name'])){ 220*9676dc23SAndreas Gohr msg('No data uploaded. Disk full?',-1); 221*9676dc23SAndreas Gohr return false; 222*9676dc23SAndreas Gohr } 223*9676dc23SAndreas Gohr 22444409c3dSAndreas Gohr // check extensions 2258cb1eb01SAndreas Gohr list($fext,$fmime) = mimetype($file['name']); 2268cb1eb01SAndreas Gohr list($iext,$imime) = mimetype($id); 22744409c3dSAndreas Gohr if($fext && !$iext){ 2288cb1eb01SAndreas Gohr // no extension specified in id - read original one 22944409c3dSAndreas Gohr $id .= '.'.$fext; 2308cb1eb01SAndreas Gohr $imime = $fmime; 23144409c3dSAndreas Gohr }elseif($fext && $fext != $iext){ 23244409c3dSAndreas Gohr // extension was changed, print warning 23344409c3dSAndreas Gohr msg(sprintf($lang['mediaextchange'],$fext,$iext)); 23444409c3dSAndreas Gohr } 23544409c3dSAndreas Gohr 2363df72098SAndreas Gohr // get filename 23744409c3dSAndreas Gohr $id = cleanID($ns.':'.$id); 2383df72098SAndreas Gohr $fn = mediaFN($id); 2393df72098SAndreas Gohr 2403df72098SAndreas Gohr // get filetype regexp 2413df72098SAndreas Gohr $types = array_keys(getMimeTypes()); 2423df72098SAndreas Gohr $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types); 2433df72098SAndreas Gohr $regex = join('|',$types); 2443df72098SAndreas Gohr 2453df72098SAndreas Gohr // because a temp file was created already 2463df72098SAndreas Gohr if(preg_match('/\.('.$regex.')$/i',$fn)){ 2473df72098SAndreas Gohr //check for overwrite 248d00ec455SAndreas Gohr if(@file_exists($fn) && (!$_REQUEST['ow'] || $auth < AUTH_DELETE)){ 2493df72098SAndreas Gohr msg($lang['uploadexist'],0); 2503df72098SAndreas Gohr return false; 2513df72098SAndreas Gohr } 2528cb1eb01SAndreas Gohr // check for valid content 2538cb1eb01SAndreas Gohr $ok = media_contentcheck($file['tmp_name'],$imime); 2548cb1eb01SAndreas Gohr if($ok == -1){ 2558cb1eb01SAndreas Gohr msg(sprintf($lang['uploadbadcontent'],".$iext"),-1); 2568cb1eb01SAndreas Gohr return false; 2578cb1eb01SAndreas Gohr }elseif($ok == -2){ 2588cb1eb01SAndreas Gohr msg($lang['uploadspam'],-1); 2598cb1eb01SAndreas Gohr return false; 26026ceae18SAndreas Gohr }elseif($ok == -3){ 26126ceae18SAndreas Gohr msg($lang['uploadxss'],-1); 26226ceae18SAndreas Gohr return false; 2638cb1eb01SAndreas Gohr } 2648cb1eb01SAndreas Gohr 26511d9dfa5SMichael Klier // prepare event data 26611d9dfa5SMichael Klier $data[0] = $file['tmp_name']; 26711d9dfa5SMichael Klier $data[1] = $fn; 26811d9dfa5SMichael Klier $data[2] = $id; 26911d9dfa5SMichael Klier $data[3] = $imime; 27011d9dfa5SMichael Klier 27111d9dfa5SMichael Klier // trigger event 27211d9dfa5SMichael Klier return trigger_event('MEDIA_UPLOAD_FINISH', $data, '_media_upload_action', true); 27311d9dfa5SMichael Klier 27411d9dfa5SMichael Klier }else{ 27511d9dfa5SMichael Klier msg($lang['uploadwrong'],-1); 27611d9dfa5SMichael Klier } 27711d9dfa5SMichael Klier return false; 27811d9dfa5SMichael Klier} 27911d9dfa5SMichael Klier 28011d9dfa5SMichael Klier/** 28111d9dfa5SMichael Klier * Callback adapter for media_upload_finish() 28211d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de> 28311d9dfa5SMichael Klier */ 28411d9dfa5SMichael Klierfunction _media_upload_action($data) { 28511d9dfa5SMichael Klier // fixme do further sanity tests of given data? 28611d9dfa5SMichael Klier if(is_array($data) && count($data)===4) { 28711d9dfa5SMichael Klier return media_upload_finish($data[0], $data[1], $data[2], $data[3]); 28811d9dfa5SMichael Klier } else { 28911d9dfa5SMichael Klier return false; //callback error 29011d9dfa5SMichael Klier } 29111d9dfa5SMichael Klier} 29211d9dfa5SMichael Klier 29311d9dfa5SMichael Klier/** 29411d9dfa5SMichael Klier * Saves an uploaded media file 29511d9dfa5SMichael Klier * 29611d9dfa5SMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 29711d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de> 29811d9dfa5SMichael Klier */ 29911d9dfa5SMichael Klierfunction media_upload_finish($fn_tmp, $fn, $id, $imime) { 30011d9dfa5SMichael Klier global $conf; 30111d9dfa5SMichael Klier global $lang; 30211d9dfa5SMichael Klier 3033df72098SAndreas Gohr // prepare directory 304cc7d0c94SBen Coburn io_createNamespace($id, 'media'); 30511d9dfa5SMichael Klier 30611d9dfa5SMichael Klier if(move_uploaded_file($fn_tmp, $fn)) { 30774400ea5SBen Coburn // Set the correct permission here. 30874400ea5SBen Coburn // Always chmod media because they may be saved with different permissions than expected from the php umask. 30974400ea5SBen Coburn // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.) 31074400ea5SBen Coburn chmod($fn, $conf['fmode']); 3113df72098SAndreas Gohr msg($lang['uploadsucc'],1); 31275030359SAndreas Gohr media_notify($id,$fn,$imime); 3133df72098SAndreas Gohr return $id; 3143df72098SAndreas Gohr }else{ 3153df72098SAndreas Gohr msg($lang['uploadfail'],-1); 3163df72098SAndreas Gohr } 3173df72098SAndreas Gohr} 3183df72098SAndreas Gohr 3198cb1eb01SAndreas Gohr/** 3208cb1eb01SAndreas Gohr * This function checks if the uploaded content is really what the 32126ceae18SAndreas Gohr * mimetype says it is. We also do spam checking for text types here. 3228cb1eb01SAndreas Gohr * 3238cb1eb01SAndreas Gohr * We need to do this stuff because we can not rely on the browser 3248cb1eb01SAndreas Gohr * to do this check correctly. Yes, IE is broken as usual. 3258cb1eb01SAndreas Gohr * 3268cb1eb01SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 32726ceae18SAndreas Gohr * @link http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting 3288cb1eb01SAndreas Gohr * @fixme check all 26 magic IE filetypes here? 3298cb1eb01SAndreas Gohr */ 3308cb1eb01SAndreas Gohrfunction media_contentcheck($file,$mime){ 33126ceae18SAndreas Gohr global $conf; 33226ceae18SAndreas Gohr if($conf['iexssprotect']){ 33326ceae18SAndreas Gohr $fh = @fopen($file, 'rb'); 33426ceae18SAndreas Gohr if($fh){ 33526ceae18SAndreas Gohr $bytes = fread($fh, 256); 33626ceae18SAndreas Gohr fclose($fh); 33726ceae18SAndreas Gohr if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){ 33826ceae18SAndreas Gohr return -3; 33926ceae18SAndreas Gohr } 34026ceae18SAndreas Gohr } 34126ceae18SAndreas Gohr } 3428cb1eb01SAndreas Gohr if(substr($mime,0,6) == 'image/'){ 3438cb1eb01SAndreas Gohr $info = @getimagesize($file); 3448cb1eb01SAndreas Gohr if($mime == 'image/gif' && $info[2] != 1){ 3458cb1eb01SAndreas Gohr return -1; 3468cb1eb01SAndreas Gohr }elseif($mime == 'image/jpeg' && $info[2] != 2){ 3478cb1eb01SAndreas Gohr return -1; 3488cb1eb01SAndreas Gohr }elseif($mime == 'image/png' && $info[2] != 3){ 3498cb1eb01SAndreas Gohr return -1; 3508cb1eb01SAndreas Gohr } 3518cb1eb01SAndreas Gohr # fixme maybe check other images types as well 3528cb1eb01SAndreas Gohr }elseif(substr($mime,0,5) == 'text/'){ 3538cb1eb01SAndreas Gohr global $TEXT; 3548cb1eb01SAndreas Gohr $TEXT = io_readFile($file); 3558cb1eb01SAndreas Gohr if(checkwordblock()){ 3568cb1eb01SAndreas Gohr return -2; 3578cb1eb01SAndreas Gohr } 3588cb1eb01SAndreas Gohr } 3598cb1eb01SAndreas Gohr return 0; 3608cb1eb01SAndreas Gohr} 3613df72098SAndreas Gohr 3623df72098SAndreas Gohr/** 36375030359SAndreas Gohr * Send a notify mail on uploads 36475030359SAndreas Gohr * 36575030359SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 36675030359SAndreas Gohr */ 36775030359SAndreas Gohrfunction media_notify($id,$file,$mime){ 36875030359SAndreas Gohr global $lang; 36975030359SAndreas Gohr global $conf; 37075030359SAndreas Gohr if(empty($conf['notify'])) return; //notify enabled? 37175030359SAndreas Gohr 37275030359SAndreas Gohr $text = rawLocale('uploadmail'); 373e656dcd4SAndreas Gohr $text = str_replace('@DATE@',strftime($conf['dformat']),$text); 37475030359SAndreas Gohr $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 37575030359SAndreas Gohr $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 37675030359SAndreas Gohr $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 37775030359SAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 37875030359SAndreas Gohr $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 37975030359SAndreas Gohr $text = str_replace('@MIME@',$mime,$text); 38055b2b31bSAndreas Gohr $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text); 38175030359SAndreas Gohr $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text); 38275030359SAndreas Gohr 38375030359SAndreas Gohr $from = $conf['mailfrom']; 38475030359SAndreas Gohr $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from); 38575030359SAndreas Gohr $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from); 38675030359SAndreas Gohr $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from); 38775030359SAndreas Gohr 38875030359SAndreas Gohr $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id; 38975030359SAndreas Gohr 39075030359SAndreas Gohr mail_send($conf['notify'],$subject,$text,$from); 39175030359SAndreas Gohr} 39275030359SAndreas Gohr 39375030359SAndreas Gohr/** 3943df72098SAndreas Gohr * List all files in a given Media namespace 3953df72098SAndreas Gohr */ 3963df72098SAndreas Gohrfunction media_filelist($ns,$auth=null,$jump=''){ 3973df72098SAndreas Gohr global $conf; 3983df72098SAndreas Gohr global $lang; 3993df72098SAndreas Gohr $ns = cleanID($ns); 4003df72098SAndreas Gohr 4013df72098SAndreas Gohr // check auth our self if not given (needed for ajax calls) 4023df72098SAndreas Gohr if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 4033df72098SAndreas Gohr 4046c48a22eSAndreas Gohr echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL; 4053df72098SAndreas Gohr 4063df72098SAndreas Gohr if($auth < AUTH_READ){ 4073df72098SAndreas Gohr // FIXME: print permission warning here instead? 4083df72098SAndreas Gohr echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 4093df72098SAndreas Gohr return; 4103df72098SAndreas Gohr } 4113df72098SAndreas Gohr 4123df72098SAndreas Gohr media_uploadform($ns, $auth); 4133df72098SAndreas Gohr 4143df72098SAndreas Gohr $dir = utf8_encodeFN(str_replace(':','/',$ns)); 4153df72098SAndreas Gohr $data = array(); 41664807c84SAndreas Gohr search($data,$conf['mediadir'],'search_media',array('showmsg'=>true),$dir); 4173df72098SAndreas Gohr 4183df72098SAndreas Gohr if(!count($data)){ 4193df72098SAndreas Gohr echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 4203df72098SAndreas Gohr return; 4213df72098SAndreas Gohr } 4223df72098SAndreas Gohr 4233df72098SAndreas Gohr foreach($data as $item){ 4243df72098SAndreas Gohr media_printfile($item,$auth,$jump); 4253df72098SAndreas Gohr } 4263df72098SAndreas Gohr} 4273df72098SAndreas Gohr 4283df72098SAndreas Gohr/** 4293df72098SAndreas Gohr * Print action links for a file depending on filetype 4303df72098SAndreas Gohr * and available permissions 4313df72098SAndreas Gohr * 4323df72098SAndreas Gohr * @todo contains inline javascript 4333df72098SAndreas Gohr */ 4343df72098SAndreas Gohrfunction media_fileactions($item,$auth){ 4353df72098SAndreas Gohr global $lang; 4363df72098SAndreas Gohr 437cf6894dfSAndreas Gohr // view button 438cf6894dfSAndreas Gohr $link = ml($item['id'],'',true); 439cf6894dfSAndreas Gohr echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '. 440cf6894dfSAndreas Gohr 'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>'; 441cf6894dfSAndreas Gohr 442cf6894dfSAndreas Gohr 443cf6894dfSAndreas Gohr // no further actions if not writable 4443df72098SAndreas Gohr if(!$item['writable']) return; 4453df72098SAndreas Gohr 4463df72098SAndreas Gohr // delete button 4473df72098SAndreas Gohr if($auth >= AUTH_DELETE){ 4483df72098SAndreas Gohr $ask = addslashes($lang['del_confirm']).'\\n'; 4493df72098SAndreas Gohr $ask .= addslashes($item['id']); 4503df72098SAndreas Gohr 451f2ea8432SAndreas Gohr echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']). 452e681fae7SMichael Klier '&sectok='.getSecurityToken().'" '. 4533df72098SAndreas Gohr 'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'. 4543df72098SAndreas Gohr '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '. 4553df72098SAndreas Gohr 'title="'.$lang['btn_delete'].'" class="btn" /></a>'; 4563df72098SAndreas Gohr } 4573df72098SAndreas Gohr 4583df72098SAndreas Gohr // edit button 4593df72098SAndreas Gohr if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){ 4603df72098SAndreas Gohr echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'. 4613df72098SAndreas Gohr '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '. 4623df72098SAndreas Gohr 'title="'.$lang['metaedit'].'" class="btn" /></a>'; 4633df72098SAndreas Gohr } 4643df72098SAndreas Gohr 4653df72098SAndreas Gohr} 4663df72098SAndreas Gohr 4673df72098SAndreas Gohr/** 4683df72098SAndreas Gohr * Formats and prints one file in the list 4693df72098SAndreas Gohr */ 4703df72098SAndreas Gohrfunction media_printfile($item,$auth,$jump){ 47164c9cfd5SAndreas Gohr global $lang; 4725e7fa82eSAndreas Gohr global $conf; 47364c9cfd5SAndreas Gohr 4743df72098SAndreas Gohr // Prepare zebra coloring 4753df72098SAndreas Gohr // I always wanted to use this variable name :-D 4763df72098SAndreas Gohr static $twibble = 1; 4773df72098SAndreas Gohr $twibble *= -1; 4783df72098SAndreas Gohr $zebra = ($twibble == -1) ? 'odd' : 'even'; 4793df72098SAndreas Gohr 4803df72098SAndreas Gohr // Automatically jump to recent action 4813df72098SAndreas Gohr if($jump == $item['id']) { 4823df72098SAndreas Gohr $jump = ' id="scroll__here" '; 4833df72098SAndreas Gohr }else{ 4843df72098SAndreas Gohr $jump = ''; 4853df72098SAndreas Gohr } 4863df72098SAndreas Gohr 4873df72098SAndreas Gohr // Prepare fileicons 4883df72098SAndreas Gohr list($ext,$mime) = mimetype($item['file']); 4893df72098SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 4903df72098SAndreas Gohr $class = 'select mediafile mf_'.$class; 4913df72098SAndreas Gohr 4923df72098SAndreas Gohr // Prepare filename 4933df72098SAndreas Gohr $file = utf8_decodeFN($item['file']); 4943df72098SAndreas Gohr 4953df72098SAndreas Gohr // Prepare info 4963df72098SAndreas Gohr $info = ''; 4973df72098SAndreas Gohr if($item['isimg']){ 4983df72098SAndreas Gohr $info .= (int) $item['meta']->getField('File.Width'); 4993df72098SAndreas Gohr $info .= '×'; 5003df72098SAndreas Gohr $info .= (int) $item['meta']->getField('File.Height'); 5013df72098SAndreas Gohr $info .= ' '; 5023df72098SAndreas Gohr } 503e656dcd4SAndreas Gohr $info .= '<i>'.strftime($conf['dformat'],$item['mtime']).'</i>'; 5045e7fa82eSAndreas Gohr $info .= ' '; 5053df72098SAndreas Gohr $info .= filesize_h($item['size']); 5063df72098SAndreas Gohr 5073df72098SAndreas Gohr // ouput 5083df72098SAndreas Gohr echo '<div class="'.$zebra.'"'.$jump.'>'.NL; 5093df72098SAndreas Gohr echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> '; 5103df72098SAndreas Gohr echo '<span class="info">('.$info.')</span>'.NL; 5113df72098SAndreas Gohr media_fileactions($item,$auth); 512f495da32SAnika Henke echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">'; 5130b173dceSAndreas Gohr echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>'; 51464c9cfd5SAndreas Gohr echo '</div>'; 5153df72098SAndreas Gohr if($item['isimg']) media_printimgdetail($item); 5163df72098SAndreas Gohr echo '<div class="clearer"></div>'.NL; 5173df72098SAndreas Gohr echo '</div>'.NL; 5183df72098SAndreas Gohr} 5193df72098SAndreas Gohr 5203df72098SAndreas Gohr/** 5213df72098SAndreas Gohr * Prints a thumbnail and metainfos 5223df72098SAndreas Gohr */ 5233df72098SAndreas Gohrfunction media_printimgdetail($item){ 5243df72098SAndreas Gohr // prepare thumbnail 5253df72098SAndreas Gohr $w = (int) $item['meta']->getField('File.Width'); 5263df72098SAndreas Gohr $h = (int) $item['meta']->getField('File.Height'); 5273df72098SAndreas Gohr if($w>120 || $h>120){ 5283df72098SAndreas Gohr $ratio = $item['meta']->getResizeRatio(120); 5293df72098SAndreas Gohr $w = floor($w * $ratio); 5303df72098SAndreas Gohr $h = floor($h * $ratio); 5313df72098SAndreas Gohr } 5323df72098SAndreas Gohr $src = ml($item['id'],array('w'=>$w,'h'=>$h)); 5333df72098SAndreas Gohr $p = array(); 5343df72098SAndreas Gohr $p['width'] = $w; 5353df72098SAndreas Gohr $p['height'] = $h; 5363df72098SAndreas Gohr $p['alt'] = $item['id']; 5373df72098SAndreas Gohr $p['class'] = 'thumb'; 5383df72098SAndreas Gohr $att = buildAttributes($p); 5393df72098SAndreas Gohr 5403df72098SAndreas Gohr // output 5413df72098SAndreas Gohr echo '<div class="detail">'; 5423df72098SAndreas Gohr echo '<div class="thumb">'; 5433df72098SAndreas Gohr echo '<a name="d_'.$item['id'].'" class="select">'; 5443df72098SAndreas Gohr echo '<img src="'.$src.'" '.$att.' />'; 5453df72098SAndreas Gohr echo '</a>'; 5463df72098SAndreas Gohr echo '</div>'; 5473df72098SAndreas Gohr 5483df72098SAndreas Gohr // read EXIF/IPTC data 54949ac3837Shakan.sandell $t = $item['meta']->getField(array('IPTC.Headline','xmp.dc:title')); 5500b173dceSAndreas Gohr $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment', 5513df72098SAndreas Gohr 'EXIF.TIFFImageDescription', 5523df72098SAndreas Gohr 'EXIF.TIFFUserComment')); 5530b173dceSAndreas Gohr if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...'; 55449ac3837Shakan.sandell $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject')); 5553df72098SAndreas Gohr 5560b173dceSAndreas Gohr // print EXIF/IPTC data 5570b173dceSAndreas Gohr if($t || $d || $k ){ 5580b173dceSAndreas Gohr echo '<p>'; 5590b173dceSAndreas Gohr if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />'; 5600b173dceSAndreas Gohr if($d) echo htmlspecialchars($d).'<br />'; 5610b173dceSAndreas Gohr if($t) echo '<em>'.htmlspecialchars($k).'</em>'; 5623df72098SAndreas Gohr echo '</p>'; 5630b173dceSAndreas Gohr } 5643df72098SAndreas Gohr echo '</div>'; 5653df72098SAndreas Gohr} 5663df72098SAndreas Gohr 5673df72098SAndreas Gohr/** 5683df72098SAndreas Gohr * Print the media upload form if permissions are correct 5693df72098SAndreas Gohr * 5703df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 5713df72098SAndreas Gohr */ 5723df72098SAndreas Gohrfunction media_uploadform($ns, $auth){ 5733df72098SAndreas Gohr global $lang; 5743df72098SAndreas Gohr 5753df72098SAndreas Gohr if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions? 5763df72098SAndreas Gohr 577d00ec455SAndreas Gohr // The default HTML upload form 5789f5dde7fSMichael Klier $form = new Doku_Form('dw__upload', DOKU_BASE.'lib/exe/mediamanager.php', false, 'multipart/form-data'); 579d00ec455SAndreas Gohr $form->addElement('<div class="upload">' . $lang['mediaupload'] . '</div>'); 5809f5dde7fSMichael Klier $form->addElement(formSecurityToken()); 5819f5dde7fSMichael Klier $form->addHidden('ns', hsc($ns)); 5829f5dde7fSMichael Klier $form->addElement(form_makeOpenTag('p')); 5839f5dde7fSMichael Klier $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file')); 5849f5dde7fSMichael Klier $form->addElement(form_makeCloseTag('p')); 5859f5dde7fSMichael Klier $form->addElement(form_makeOpenTag('p')); 5869f5dde7fSMichael Klier $form->addElement(form_makeTextField('id', '', $lang['txt_filename'].':', 'upload__name')); 5879f5dde7fSMichael Klier $form->addElement(form_makeButton('submit', '', $lang['btn_upload'])); 5889f5dde7fSMichael Klier $form->addElement(form_makeCloseTag('p')); 589cf6894dfSAndreas Gohr 5909f5dde7fSMichael Klier if($auth >= AUTH_DELETE){ 5919f5dde7fSMichael Klier $form->addElement(form_makeOpenTag('p')); 5929f5dde7fSMichael Klier $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check')); 5939f5dde7fSMichael Klier $form->addElement(form_makeCloseTag('p')); 5943df72098SAndreas Gohr } 5959f5dde7fSMichael Klier html_form('upload', $form); 596d00ec455SAndreas Gohr 597d00ec455SAndreas Gohr // prepare flashvars for multiupload 598d00ec455SAndreas Gohr $opt = array( 599d00ec455SAndreas Gohr 'L_gridname' => $lang['mu_gridname'] , 600d00ec455SAndreas Gohr 'L_gridsize' => $lang['mu_gridsize'] , 601d00ec455SAndreas Gohr 'L_gridstat' => $lang['mu_gridstat'] , 602d00ec455SAndreas Gohr 'L_namespace' => $lang['mu_namespace'] , 603d00ec455SAndreas Gohr 'L_overwrite' => $lang['txt_overwrt'], 604d00ec455SAndreas Gohr 'L_browse' => $lang['mu_browse'], 605d00ec455SAndreas Gohr 'L_upload' => $lang['btn_upload'], 606d00ec455SAndreas Gohr 'L_toobig' => $lang['mu_toobig'], 607d00ec455SAndreas Gohr 'L_ready' => $lang['mu_ready'], 608d00ec455SAndreas Gohr 'L_done' => $lang['mu_done'], 609d00ec455SAndreas Gohr 'L_fail' => $lang['mu_fail'], 610d00ec455SAndreas Gohr 'L_authfail' => $lang['mu_authfail'], 611d00ec455SAndreas Gohr 'L_progress' => $lang['mu_progress'], 612d00ec455SAndreas Gohr 'L_filetypes' => $lang['mu_filetypes'], 613d00ec455SAndreas Gohr 614d00ec455SAndreas Gohr 'O_ns' => ":$ns", 615d00ec455SAndreas Gohr 'O_backend' => 'mediamanager.php?'.session_name().'='.session_id(), 616d00ec455SAndreas Gohr 'O_size' => php_to_byte(ini_get('upload_max_filesize')), 617d00ec455SAndreas Gohr 'O_extensions'=> join('|',array_keys(getMimeTypes())), 618d00ec455SAndreas Gohr 'O_overwrite' => ($auth >= AUTH_DELETE), 619d00ec455SAndreas Gohr 'O_sectok' => getSecurityToken(), 620d00ec455SAndreas Gohr 'O_authtok' => auth_createToken(), 621d00ec455SAndreas Gohr ); 622d00ec455SAndreas Gohr $var = buildURLparams($opt,'&'); 623d00ec455SAndreas Gohr // output the flash uploader 624d00ec455SAndreas Gohr ?> 625d00ec455SAndreas Gohr <div id="dw__flashupload" style="display:none"> 626d00ec455SAndreas Gohr <div class="upload"><?php echo $lang['mu_intro']?></div> 627d00ec455SAndreas Gohr <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" 628d00ec455SAndreas Gohr codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> 629d00ec455SAndreas Gohr <param name="movie" value="multipleUpload.swf?t=<?=time()?>" /> 630d00ec455SAndreas Gohr <param name="quality" value="high" /> 631d00ec455SAndreas Gohr <param name="bgcolor" value="#ffffff" /> 632d00ec455SAndreas Gohr <param name="FlashVars" value="<?php echo $var?>" /> 633d00ec455SAndreas Gohr <embed src="multipleUpload.swf?t=<?=time()?>" quality="high" bgcolor="#ffffff" 634d00ec455SAndreas Gohr width="100%" height="100%" name="fileUpload" align="middle" 635d00ec455SAndreas Gohr play="true" loop="false" quality="high" FlashVars="<?php echo $var?>" 636d00ec455SAndreas Gohr allowScriptAccess="sameDomain" 637d00ec455SAndreas Gohr type="application/x-shockwave-flash" 638d00ec455SAndreas Gohr pluginspage="http://www.macromedia.com/go/getflashplayer"> 639d00ec455SAndreas Gohr </embed> 640d00ec455SAndreas Gohr </object> 641d00ec455SAndreas Gohr </div> 642d00ec455SAndreas Gohr <?php 6439f5dde7fSMichael Klier} 6443df72098SAndreas Gohr 6453df72098SAndreas Gohr/** 6463df72098SAndreas Gohr * Build a tree outline of available media namespaces 6473df72098SAndreas Gohr * 6483df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6493df72098SAndreas Gohr */ 6503df72098SAndreas Gohrfunction media_nstree($ns){ 6513df72098SAndreas Gohr global $conf; 652256ca81eSAndreas Gohr global $lang; 6533df72098SAndreas Gohr 6543df72098SAndreas Gohr // currently selected namespace 6553df72098SAndreas Gohr $ns = cleanID($ns); 6563df72098SAndreas Gohr if(empty($ns)){ 6573df72098SAndreas Gohr $ns = dirname(str_replace(':','/',$ID)); 6583df72098SAndreas Gohr if($ns == '.') $ns =''; 6593df72098SAndreas Gohr } 6603df72098SAndreas Gohr $ns = utf8_encodeFN(str_replace(':','/',$ns)); 6613df72098SAndreas Gohr 6623df72098SAndreas Gohr $data = array(); 663ee7b5a62SAndreas Gohr search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true)); 6643df72098SAndreas Gohr 6653df72098SAndreas Gohr // wrap a list with the root level around the other namespaces 666256ca81eSAndreas Gohr $item = array( 'level' => 0, 'id' => '', 667256ca81eSAndreas Gohr 'open' =>'true', 'label' => '['.$lang['mediaroot'].']'); 6683df72098SAndreas Gohr 6693df72098SAndreas Gohr echo '<ul class="idx">'; 6703df72098SAndreas Gohr echo media_nstree_li($item); 6713df72098SAndreas Gohr echo media_nstree_item($item); 6723df72098SAndreas Gohr echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li'); 6733df72098SAndreas Gohr echo '</li>'; 6743df72098SAndreas Gohr echo '</ul>'; 6753df72098SAndreas Gohr} 6763df72098SAndreas Gohr 6773df72098SAndreas Gohr/** 6783df72098SAndreas Gohr * Userfunction for html_buildlist 6793df72098SAndreas Gohr * 6803df72098SAndreas Gohr * Prints a media namespace tree item 6813df72098SAndreas Gohr * 6823df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6833df72098SAndreas Gohr */ 6843df72098SAndreas Gohrfunction media_nstree_item($item){ 6853df72098SAndreas Gohr $pos = strrpos($item['id'], ':'); 6863df72098SAndreas Gohr $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0); 6873df72098SAndreas Gohr if(!$item['label']) $item['label'] = $label; 6883df72098SAndreas Gohr 6893df72098SAndreas Gohr $ret = ''; 6903df72098SAndreas Gohr $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">'; 6913df72098SAndreas Gohr $ret .= $item['label']; 6923df72098SAndreas Gohr $ret .= '</a>'; 6933df72098SAndreas Gohr return $ret; 6943df72098SAndreas Gohr} 6953df72098SAndreas Gohr 6963df72098SAndreas Gohr/** 6973df72098SAndreas Gohr * Userfunction for html_buildlist 6983df72098SAndreas Gohr * 6993df72098SAndreas Gohr * Prints a media namespace tree item opener 7003df72098SAndreas Gohr * 7013df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7023df72098SAndreas Gohr */ 7033df72098SAndreas Gohrfunction media_nstree_li($item){ 7043df72098SAndreas Gohr $class='media level'.$item['level']; 7053df72098SAndreas Gohr if($item['open']){ 7063df72098SAndreas Gohr $class .= ' open'; 7073df72098SAndreas Gohr $img = DOKU_BASE.'lib/images/minus.gif'; 7087af1b404SAnika Henke $alt = '−'; 7093df72098SAndreas Gohr }else{ 7103df72098SAndreas Gohr $class .= ' closed'; 7113df72098SAndreas Gohr $img = DOKU_BASE.'lib/images/plus.gif'; 7127af1b404SAnika Henke $alt = '+'; 7133df72098SAndreas Gohr } 7143df72098SAndreas Gohr return '<li class="'.$class.'">'. 7157af1b404SAnika Henke '<img src="'.$img.'" alt="'.$alt.'" />'; 7163df72098SAndreas Gohr} 717