xref: /dokuwiki/inc/media.php (revision 11d9dfa5f39e8005a998d25bca1885847c2cb561)
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
93df72098SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(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    usort($data,'sort_search_fulltext');
313df72098SAndreas Gohr    foreach($data as $row){
323df72098SAndreas Gohr        if(auth_quickaclcheck($row['id']) >= AUTH_READ){
333df72098SAndreas Gohr            echo '<div class="search_result">';
343df72098SAndreas Gohr            echo '<span class="mediaref_ref">'.$row['id'].'</span>';
353df72098SAndreas Gohr            echo ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
363df72098SAndreas Gohr            echo '<div class="search_snippet">'.$row['snippet'].'</div>';
373df72098SAndreas Gohr            echo '</div>';
383df72098SAndreas Gohr        }else
393df72098SAndreas Gohr        $hidden++;
403df72098SAndreas Gohr    }
413df72098SAndreas Gohr    if ($hidden){
423df72098SAndreas Gohr      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
433df72098SAndreas Gohr    }
443df72098SAndreas Gohr}
453df72098SAndreas Gohr
463df72098SAndreas Gohr/**
473df72098SAndreas Gohr * Handles the saving of image meta data
483df72098SAndreas Gohr *
493df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
503df72098SAndreas Gohr */
513df72098SAndreas Gohrfunction media_metasave($id,$auth,$data){
523df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
53f2ea8432SAndreas Gohr    if(!checkSecurityToken()) return false;
543df72098SAndreas Gohr    global $lang;
553df72098SAndreas Gohr    $src = mediaFN($id);
563df72098SAndreas Gohr
573df72098SAndreas Gohr    $meta = new JpegMeta($src);
583df72098SAndreas Gohr    $meta->_parseAll();
593df72098SAndreas Gohr
603df72098SAndreas Gohr    foreach($data as $key => $val){
613df72098SAndreas Gohr        $val=trim($val);
623df72098SAndreas Gohr        if(empty($val)){
633df72098SAndreas Gohr            $meta->deleteField($key);
643df72098SAndreas Gohr        }else{
653df72098SAndreas Gohr            $meta->setField($key,$val);
663df72098SAndreas Gohr        }
673df72098SAndreas Gohr    }
683df72098SAndreas Gohr
693df72098SAndreas Gohr    if($meta->save()){
703df72098SAndreas Gohr        msg($lang['metasaveok'],1);
713df72098SAndreas Gohr        return $id;
723df72098SAndreas Gohr    }else{
733df72098SAndreas Gohr        msg($lang['metasaveerr'],-1);
743df72098SAndreas Gohr        return false;
753df72098SAndreas Gohr    }
763df72098SAndreas Gohr}
773df72098SAndreas Gohr
783df72098SAndreas Gohr/**
793df72098SAndreas Gohr * Display the form to edit image meta data
803df72098SAndreas Gohr *
813df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
823df72098SAndreas Gohr */
833df72098SAndreas Gohrfunction media_metaform($id,$auth){
843df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
853df72098SAndreas Gohr    global $lang;
863df72098SAndreas Gohr
873df72098SAndreas Gohr    // load the field descriptions
883df72098SAndreas Gohr    static $fields = null;
893df72098SAndreas Gohr    if(is_null($fields)){
903df72098SAndreas Gohr        include(DOKU_CONF.'mediameta.php');
913df72098SAndreas Gohr        if(@file_exists(DOKU_CONF.'mediameta.local.php')){
923df72098SAndreas Gohr            include(DOKU_CONF.'mediameta.local.php');
933df72098SAndreas Gohr        }
943df72098SAndreas Gohr    }
953df72098SAndreas Gohr
963df72098SAndreas Gohr    $src = mediaFN($id);
973df72098SAndreas Gohr
983df72098SAndreas Gohr    // output
993df72098SAndreas Gohr    echo '<h1>'.hsc(noNS($id)).'</h1>'.NL;
1003df72098SAndreas Gohr    echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" accept-charset="utf-8" method="post" class="meta">'.NL;
101f2ea8432SAndreas Gohr    formSecurityToken();
1023df72098SAndreas Gohr    foreach($fields as $key => $field){
1033df72098SAndreas Gohr        // get current value
1043df72098SAndreas Gohr        $tags = array($field[0]);
1053df72098SAndreas Gohr        if(is_array($field[3])) $tags = array_merge($tags,$field[3]);
1063df72098SAndreas Gohr        $value = tpl_img_getTag($tags,'',$src);
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
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
1563df72098SAndreas Gohr    $mediareferences = array();
1573df72098SAndreas Gohr    if($conf['refcheck']){
1583df72098SAndreas Gohr        search($mediareferences,$conf['datadir'],'search_reference',array('query' => $id));
1593df72098SAndreas Gohr    }
1603df72098SAndreas Gohr
1613df72098SAndreas Gohr    if(!count($mediareferences)){
1623df72098SAndreas Gohr        $file = mediaFN($id);
1633df72098SAndreas Gohr        if(@unlink($file)){
1643df72098SAndreas Gohr            msg(str_replace('%s',noNS($id),$lang['deletesucc']),1);
1653df72098SAndreas Gohr            io_sweepNS($id,'mediadir');
1663df72098SAndreas Gohr            return true;
1673df72098SAndreas Gohr        }
1683df72098SAndreas Gohr        //something went wrong
1693df72098SAndreas Gohr        msg(str_replace('%s',$file,$lang['deletefail']),-1);
1703df72098SAndreas Gohr        return false;
1713df72098SAndreas Gohr    }elseif(!$conf['refshow']){
1723df72098SAndreas Gohr        msg(str_replace('%s',noNS($id),$lang['mediainuse']),0);
1733df72098SAndreas Gohr        return false;
1743df72098SAndreas Gohr    }
1753df72098SAndreas Gohr
1763df72098SAndreas Gohr    return $mediareferences;
1773df72098SAndreas Gohr}
1783df72098SAndreas Gohr
1793df72098SAndreas Gohr/**
1803df72098SAndreas Gohr * Handles media file uploads
1813df72098SAndreas Gohr *
182*11d9dfa5SMichael Klier * This generates an action event and delegates to _media_upload_action().
183*11d9dfa5SMichael Klier * Action plugins are allowed to pre/postprocess the uploaded file.
184*11d9dfa5SMichael Klier * (The triggered event is preventable.)
185*11d9dfa5SMichael Klier *
186*11d9dfa5SMichael Klier * Event data:
187*11d9dfa5SMichael Klier * $data[0]     fn_tmp: the temporary file name (read from $_FILES)
188*11d9dfa5SMichael Klier * $data[1]     fn: the file name of the uploaded file
189*11d9dfa5SMichael Klier * $data[2]     id: the future directory id of the uploaded file
190*11d9dfa5SMichael Klier * $data[3]     imime: the mimetype of the uploaded file
191*11d9dfa5SMichael Klier *
192*11d9dfa5SMichael Klier * @triggers MEDIA_UPLOAD_FINISH
1933df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
194*11d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de>
1953df72098SAndreas Gohr * @return mixed false on error, id of the new file on success
1963df72098SAndreas Gohr */
1973df72098SAndreas Gohrfunction media_upload($ns,$auth){
1983df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
199f2ea8432SAndreas Gohr    if(!checkSecurityToken()) return false;
2003df72098SAndreas Gohr    require_once(DOKU_INC.'inc/confutils.php');
2013df72098SAndreas Gohr    global $lang;
2023df72098SAndreas Gohr    global $conf;
2033df72098SAndreas Gohr
20444409c3dSAndreas Gohr    // get file and id
2053df72098SAndreas Gohr    $id   = $_POST['id'];
2063df72098SAndreas Gohr    $file = $_FILES['upload'];
2073df72098SAndreas Gohr    if(empty($id)) $id = $file['name'];
20844409c3dSAndreas Gohr
20944409c3dSAndreas Gohr    // check extensions
2108cb1eb01SAndreas Gohr    list($fext,$fmime) = mimetype($file['name']);
2118cb1eb01SAndreas Gohr    list($iext,$imime) = mimetype($id);
21244409c3dSAndreas Gohr    if($fext && !$iext){
2138cb1eb01SAndreas Gohr        // no extension specified in id - read original one
21444409c3dSAndreas Gohr        $id   .= '.'.$fext;
2158cb1eb01SAndreas Gohr        $imime = $fmime;
21644409c3dSAndreas Gohr    }elseif($fext && $fext != $iext){
21744409c3dSAndreas Gohr        // extension was changed, print warning
21844409c3dSAndreas Gohr        msg(sprintf($lang['mediaextchange'],$fext,$iext));
21944409c3dSAndreas Gohr    }
22044409c3dSAndreas Gohr
2213df72098SAndreas Gohr    // get filename
22244409c3dSAndreas Gohr    $id   = cleanID($ns.':'.$id);
2233df72098SAndreas Gohr    $fn   = mediaFN($id);
2243df72098SAndreas Gohr
2253df72098SAndreas Gohr    // get filetype regexp
2263df72098SAndreas Gohr    $types = array_keys(getMimeTypes());
2273df72098SAndreas Gohr    $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
2283df72098SAndreas Gohr    $regex = join('|',$types);
2293df72098SAndreas Gohr
2303df72098SAndreas Gohr    // because a temp file was created already
2313df72098SAndreas Gohr    if(preg_match('/\.('.$regex.')$/i',$fn)){
2323df72098SAndreas Gohr        //check for overwrite
2333df72098SAndreas Gohr        if(@file_exists($fn) && (!$_POST['ow'] || $auth < AUTH_DELETE)){
2343df72098SAndreas Gohr            msg($lang['uploadexist'],0);
2353df72098SAndreas Gohr            return false;
2363df72098SAndreas Gohr        }
2378cb1eb01SAndreas Gohr        // check for valid content
2388cb1eb01SAndreas Gohr        $ok = media_contentcheck($file['tmp_name'],$imime);
2398cb1eb01SAndreas Gohr        if($ok == -1){
2408cb1eb01SAndreas Gohr            msg(sprintf($lang['uploadbadcontent'],".$iext"),-1);
2418cb1eb01SAndreas Gohr            return false;
2428cb1eb01SAndreas Gohr        }elseif($ok == -2){
2438cb1eb01SAndreas Gohr            msg($lang['uploadspam'],-1);
2448cb1eb01SAndreas Gohr            return false;
24526ceae18SAndreas Gohr        }elseif($ok == -3){
24626ceae18SAndreas Gohr            msg($lang['uploadxss'],-1);
24726ceae18SAndreas Gohr            return false;
2488cb1eb01SAndreas Gohr        }
2498cb1eb01SAndreas Gohr
250*11d9dfa5SMichael Klier        // prepare event data
251*11d9dfa5SMichael Klier        $data[0] = $file['tmp_name'];
252*11d9dfa5SMichael Klier        $data[1] = $fn;
253*11d9dfa5SMichael Klier        $data[2] = $id;
254*11d9dfa5SMichael Klier        $data[3] = $imime;
255*11d9dfa5SMichael Klier
256*11d9dfa5SMichael Klier        // trigger event
257*11d9dfa5SMichael Klier        return trigger_event('MEDIA_UPLOAD_FINISH', $data, '_media_upload_action', true);
258*11d9dfa5SMichael Klier
259*11d9dfa5SMichael Klier    }else{
260*11d9dfa5SMichael Klier        msg($lang['uploadwrong'],-1);
261*11d9dfa5SMichael Klier    }
262*11d9dfa5SMichael Klier    return false;
263*11d9dfa5SMichael Klier}
264*11d9dfa5SMichael Klier
265*11d9dfa5SMichael Klier/**
266*11d9dfa5SMichael Klier * Callback adapter for media_upload_finish()
267*11d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de>
268*11d9dfa5SMichael Klier */
269*11d9dfa5SMichael Klierfunction _media_upload_action($data) {
270*11d9dfa5SMichael Klier    // fixme do further sanity tests of given data?
271*11d9dfa5SMichael Klier    if(is_array($data) && count($data)===4) {
272*11d9dfa5SMichael Klier        return media_upload_finish($data[0], $data[1], $data[2], $data[3]);
273*11d9dfa5SMichael Klier    } else {
274*11d9dfa5SMichael Klier        return false; //callback error
275*11d9dfa5SMichael Klier    }
276*11d9dfa5SMichael Klier}
277*11d9dfa5SMichael Klier
278*11d9dfa5SMichael Klier/**
279*11d9dfa5SMichael Klier * Saves an uploaded media file
280*11d9dfa5SMichael Klier *
281*11d9dfa5SMichael Klier * @author Andreas Gohr <andi@splitbrain.org>
282*11d9dfa5SMichael Klier * @author Michael Klier <chi@chimeric.de>
283*11d9dfa5SMichael Klier */
284*11d9dfa5SMichael Klierfunction media_upload_finish($fn_tmp, $fn, $id, $imime) {
285*11d9dfa5SMichael Klier    global $conf;
286*11d9dfa5SMichael Klier    global $lang;
287*11d9dfa5SMichael Klier
2883df72098SAndreas Gohr    // prepare directory
289cc7d0c94SBen Coburn    io_createNamespace($id, 'media');
290*11d9dfa5SMichael Klier
291*11d9dfa5SMichael Klier    if(move_uploaded_file($fn_tmp, $fn)) {
29274400ea5SBen Coburn        // Set the correct permission here.
29374400ea5SBen Coburn        // Always chmod media because they may be saved with different permissions than expected from the php umask.
29474400ea5SBen Coburn        // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
29574400ea5SBen Coburn        chmod($fn, $conf['fmode']);
2963df72098SAndreas Gohr        msg($lang['uploadsucc'],1);
29775030359SAndreas Gohr        media_notify($id,$fn,$imime);
2983df72098SAndreas Gohr        return $id;
2993df72098SAndreas Gohr    }else{
3003df72098SAndreas Gohr        msg($lang['uploadfail'],-1);
3013df72098SAndreas Gohr    }
3023df72098SAndreas Gohr}
3033df72098SAndreas Gohr
3048cb1eb01SAndreas Gohr/**
3058cb1eb01SAndreas Gohr * This function checks if the uploaded content is really what the
30626ceae18SAndreas Gohr * mimetype says it is. We also do spam checking for text types here.
3078cb1eb01SAndreas Gohr *
3088cb1eb01SAndreas Gohr * We need to do this stuff because we can not rely on the browser
3098cb1eb01SAndreas Gohr * to do this check correctly. Yes, IE is broken as usual.
3108cb1eb01SAndreas Gohr *
3118cb1eb01SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
31226ceae18SAndreas Gohr * @link   http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting
3138cb1eb01SAndreas Gohr * @fixme  check all 26 magic IE filetypes here?
3148cb1eb01SAndreas Gohr */
3158cb1eb01SAndreas Gohrfunction media_contentcheck($file,$mime){
31626ceae18SAndreas Gohr    global $conf;
31726ceae18SAndreas Gohr    if($conf['iexssprotect']){
31826ceae18SAndreas Gohr        $fh = @fopen($file, 'rb');
31926ceae18SAndreas Gohr        if($fh){
32026ceae18SAndreas Gohr            $bytes = fread($fh, 256);
32126ceae18SAndreas Gohr            fclose($fh);
32226ceae18SAndreas Gohr            if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){
32326ceae18SAndreas Gohr                return -3;
32426ceae18SAndreas Gohr            }
32526ceae18SAndreas Gohr        }
32626ceae18SAndreas Gohr    }
3278cb1eb01SAndreas Gohr    if(substr($mime,0,6) == 'image/'){
3288cb1eb01SAndreas Gohr        $info = @getimagesize($file);
3298cb1eb01SAndreas Gohr        if($mime == 'image/gif' && $info[2] != 1){
3308cb1eb01SAndreas Gohr            return -1;
3318cb1eb01SAndreas Gohr        }elseif($mime == 'image/jpeg' && $info[2] != 2){
3328cb1eb01SAndreas Gohr            return -1;
3338cb1eb01SAndreas Gohr        }elseif($mime == 'image/png' && $info[2] != 3){
3348cb1eb01SAndreas Gohr            return -1;
3358cb1eb01SAndreas Gohr        }
3368cb1eb01SAndreas Gohr        # fixme maybe check other images types as well
3378cb1eb01SAndreas Gohr    }elseif(substr($mime,0,5) == 'text/'){
3388cb1eb01SAndreas Gohr        global $TEXT;
3398cb1eb01SAndreas Gohr        $TEXT = io_readFile($file);
3408cb1eb01SAndreas Gohr        if(checkwordblock()){
3418cb1eb01SAndreas Gohr            return -2;
3428cb1eb01SAndreas Gohr        }
3438cb1eb01SAndreas Gohr    }
3448cb1eb01SAndreas Gohr    return 0;
3458cb1eb01SAndreas Gohr}
3463df72098SAndreas Gohr
3473df72098SAndreas Gohr/**
34875030359SAndreas Gohr * Send a notify mail on uploads
34975030359SAndreas Gohr *
35075030359SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
35175030359SAndreas Gohr */
35275030359SAndreas Gohrfunction media_notify($id,$file,$mime){
35375030359SAndreas Gohr    global $lang;
35475030359SAndreas Gohr    global $conf;
35575030359SAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
35675030359SAndreas Gohr
35775030359SAndreas Gohr    $text = rawLocale('uploadmail');
35875030359SAndreas Gohr    $text = str_replace('@DATE@',date($conf['dformat']),$text);
35975030359SAndreas Gohr    $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
36075030359SAndreas Gohr    $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
36175030359SAndreas Gohr    $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
36275030359SAndreas Gohr    $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
36375030359SAndreas Gohr    $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
36475030359SAndreas Gohr    $text = str_replace('@MIME@',$mime,$text);
36555b2b31bSAndreas Gohr    $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text);
36675030359SAndreas Gohr    $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text);
36775030359SAndreas Gohr
36875030359SAndreas Gohr    $from = $conf['mailfrom'];
36975030359SAndreas Gohr    $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from);
37075030359SAndreas Gohr    $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from);
37175030359SAndreas Gohr    $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from);
37275030359SAndreas Gohr
37375030359SAndreas Gohr    $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id;
37475030359SAndreas Gohr
37575030359SAndreas Gohr    mail_send($conf['notify'],$subject,$text,$from);
37675030359SAndreas Gohr}
37775030359SAndreas Gohr
37875030359SAndreas Gohr/**
3793df72098SAndreas Gohr * List all files in a given Media namespace
3803df72098SAndreas Gohr */
3813df72098SAndreas Gohrfunction media_filelist($ns,$auth=null,$jump=''){
3823df72098SAndreas Gohr    global $conf;
3833df72098SAndreas Gohr    global $lang;
3843df72098SAndreas Gohr    $ns = cleanID($ns);
3853df72098SAndreas Gohr
3863df72098SAndreas Gohr    // check auth our self if not given (needed for ajax calls)
3873df72098SAndreas Gohr    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
3883df72098SAndreas Gohr
3896c48a22eSAndreas Gohr    echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
3903df72098SAndreas Gohr
3913df72098SAndreas Gohr    if($auth < AUTH_READ){
3923df72098SAndreas Gohr        // FIXME: print permission warning here instead?
3933df72098SAndreas Gohr        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
3943df72098SAndreas Gohr        return;
3953df72098SAndreas Gohr    }
3963df72098SAndreas Gohr
3973df72098SAndreas Gohr    media_uploadform($ns, $auth);
3983df72098SAndreas Gohr
3993df72098SAndreas Gohr    $dir = utf8_encodeFN(str_replace(':','/',$ns));
4003df72098SAndreas Gohr    $data = array();
4013df72098SAndreas Gohr    search($data,$conf['mediadir'],'search_media',array(),$dir);
4023df72098SAndreas Gohr
4033df72098SAndreas Gohr    if(!count($data)){
4043df72098SAndreas Gohr        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
4053df72098SAndreas Gohr        return;
4063df72098SAndreas Gohr    }
4073df72098SAndreas Gohr
4083df72098SAndreas Gohr    foreach($data as $item){
4093df72098SAndreas Gohr        media_printfile($item,$auth,$jump);
4103df72098SAndreas Gohr    }
4113df72098SAndreas Gohr}
4123df72098SAndreas Gohr
4133df72098SAndreas Gohr/**
4143df72098SAndreas Gohr * Print action links for a file depending on filetype
4153df72098SAndreas Gohr * and available permissions
4163df72098SAndreas Gohr *
4173df72098SAndreas Gohr * @todo contains inline javascript
4183df72098SAndreas Gohr */
4193df72098SAndreas Gohrfunction media_fileactions($item,$auth){
4203df72098SAndreas Gohr    global $lang;
4213df72098SAndreas Gohr
422cf6894dfSAndreas Gohr    // view button
423cf6894dfSAndreas Gohr    $link = ml($item['id'],'',true);
424cf6894dfSAndreas Gohr    echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
425cf6894dfSAndreas Gohr         'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>';
426cf6894dfSAndreas Gohr
427cf6894dfSAndreas Gohr
428cf6894dfSAndreas Gohr    // no further actions if not writable
4293df72098SAndreas Gohr    if(!$item['writable']) return;
4303df72098SAndreas Gohr
4313df72098SAndreas Gohr    // delete button
4323df72098SAndreas Gohr    if($auth >= AUTH_DELETE){
4333df72098SAndreas Gohr        $ask  = addslashes($lang['del_confirm']).'\\n';
4343df72098SAndreas Gohr        $ask .= addslashes($item['id']);
4353df72098SAndreas Gohr
436f2ea8432SAndreas Gohr        echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).
437e681fae7SMichael Klier             '&amp;sectok='.getSecurityToken().'" '.
4383df72098SAndreas Gohr             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
4393df72098SAndreas Gohr             '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '.
4403df72098SAndreas Gohr             'title="'.$lang['btn_delete'].'" class="btn" /></a>';
4413df72098SAndreas Gohr    }
4423df72098SAndreas Gohr
4433df72098SAndreas Gohr    // edit button
4443df72098SAndreas Gohr    if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){
4453df72098SAndreas Gohr        echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'.
4463df72098SAndreas Gohr             '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '.
4473df72098SAndreas Gohr             'title="'.$lang['metaedit'].'" class="btn" /></a>';
4483df72098SAndreas Gohr    }
4493df72098SAndreas Gohr
4503df72098SAndreas Gohr}
4513df72098SAndreas Gohr
4523df72098SAndreas Gohr/**
4533df72098SAndreas Gohr * Formats and prints one file in the list
4543df72098SAndreas Gohr */
4553df72098SAndreas Gohrfunction media_printfile($item,$auth,$jump){
45664c9cfd5SAndreas Gohr    global $lang;
4575e7fa82eSAndreas Gohr    global $conf;
45864c9cfd5SAndreas Gohr
4593df72098SAndreas Gohr    // Prepare zebra coloring
4603df72098SAndreas Gohr    // I always wanted to use this variable name :-D
4613df72098SAndreas Gohr    static $twibble = 1;
4623df72098SAndreas Gohr    $twibble *= -1;
4633df72098SAndreas Gohr    $zebra = ($twibble == -1) ? 'odd' : 'even';
4643df72098SAndreas Gohr
4653df72098SAndreas Gohr    // Automatically jump to recent action
4663df72098SAndreas Gohr    if($jump == $item['id']) {
4673df72098SAndreas Gohr        $jump = ' id="scroll__here" ';
4683df72098SAndreas Gohr    }else{
4693df72098SAndreas Gohr        $jump = '';
4703df72098SAndreas Gohr    }
4713df72098SAndreas Gohr
4723df72098SAndreas Gohr    // Prepare fileicons
4733df72098SAndreas Gohr    list($ext,$mime) = mimetype($item['file']);
4743df72098SAndreas Gohr    $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
4753df72098SAndreas Gohr    $class = 'select mediafile mf_'.$class;
4763df72098SAndreas Gohr
4773df72098SAndreas Gohr    // Prepare filename
4783df72098SAndreas Gohr    $file = utf8_decodeFN($item['file']);
4793df72098SAndreas Gohr
4803df72098SAndreas Gohr    // Prepare info
4813df72098SAndreas Gohr    $info = '';
4823df72098SAndreas Gohr    if($item['isimg']){
4833df72098SAndreas Gohr        $info .= (int) $item['meta']->getField('File.Width');
4843df72098SAndreas Gohr        $info .= '&#215;';
4853df72098SAndreas Gohr        $info .= (int) $item['meta']->getField('File.Height');
4863df72098SAndreas Gohr        $info .= ' ';
4873df72098SAndreas Gohr    }
4885e7fa82eSAndreas Gohr    $info .= '<i>'.date($conf['dformat'],$item['mtime']).'</i>';
4895e7fa82eSAndreas Gohr    $info .= ' ';
4903df72098SAndreas Gohr    $info .= filesize_h($item['size']);
4913df72098SAndreas Gohr
4923df72098SAndreas Gohr    // ouput
4933df72098SAndreas Gohr    echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
4943df72098SAndreas Gohr    echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> ';
4953df72098SAndreas Gohr    echo '<span class="info">('.$info.')</span>'.NL;
4963df72098SAndreas Gohr    media_fileactions($item,$auth);
497f495da32SAnika Henke    echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">';
4980b173dceSAndreas Gohr    echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>';
49964c9cfd5SAndreas Gohr    echo '</div>';
5003df72098SAndreas Gohr    if($item['isimg']) media_printimgdetail($item);
5013df72098SAndreas Gohr    echo '<div class="clearer"></div>'.NL;
5023df72098SAndreas Gohr    echo '</div>'.NL;
5033df72098SAndreas Gohr}
5043df72098SAndreas Gohr
5053df72098SAndreas Gohr/**
5063df72098SAndreas Gohr * Prints a thumbnail and metainfos
5073df72098SAndreas Gohr */
5083df72098SAndreas Gohrfunction media_printimgdetail($item){
5093df72098SAndreas Gohr    // prepare thumbnail
5103df72098SAndreas Gohr    $w = (int) $item['meta']->getField('File.Width');
5113df72098SAndreas Gohr    $h = (int) $item['meta']->getField('File.Height');
5123df72098SAndreas Gohr    if($w>120 || $h>120){
5133df72098SAndreas Gohr        $ratio = $item['meta']->getResizeRatio(120);
5143df72098SAndreas Gohr        $w = floor($w * $ratio);
5153df72098SAndreas Gohr        $h = floor($h * $ratio);
5163df72098SAndreas Gohr    }
5173df72098SAndreas Gohr    $src = ml($item['id'],array('w'=>$w,'h'=>$h));
5183df72098SAndreas Gohr    $p = array();
5193df72098SAndreas Gohr    $p['width']  = $w;
5203df72098SAndreas Gohr    $p['height'] = $h;
5213df72098SAndreas Gohr    $p['alt']    = $item['id'];
5223df72098SAndreas Gohr    $p['class']  = 'thumb';
5233df72098SAndreas Gohr    $att = buildAttributes($p);
5243df72098SAndreas Gohr
5253df72098SAndreas Gohr    // output
5263df72098SAndreas Gohr    echo '<div class="detail">';
5273df72098SAndreas Gohr    echo '<div class="thumb">';
5283df72098SAndreas Gohr    echo '<a name="d_'.$item['id'].'" class="select">';
5293df72098SAndreas Gohr    echo '<img src="'.$src.'" '.$att.' />';
5303df72098SAndreas Gohr    echo '</a>';
5313df72098SAndreas Gohr    echo '</div>';
5323df72098SAndreas Gohr
5333df72098SAndreas Gohr    // read EXIF/IPTC data
5343df72098SAndreas Gohr    $t = $item['meta']->getField('IPTC.Headline');
5350b173dceSAndreas Gohr    $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
5363df72098SAndreas Gohr                                       'EXIF.TIFFImageDescription',
5373df72098SAndreas Gohr                                       'EXIF.TIFFUserComment'));
5380b173dceSAndreas Gohr    if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...';
5390b173dceSAndreas Gohr    $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
5403df72098SAndreas Gohr
5410b173dceSAndreas Gohr    // print EXIF/IPTC data
5420b173dceSAndreas Gohr    if($t || $d || $k ){
5430b173dceSAndreas Gohr        echo '<p>';
5440b173dceSAndreas Gohr        if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />';
5450b173dceSAndreas Gohr        if($d) echo htmlspecialchars($d).'<br />';
5460b173dceSAndreas Gohr        if($t) echo '<em>'.htmlspecialchars($k).'</em>';
5473df72098SAndreas Gohr        echo '</p>';
5480b173dceSAndreas Gohr    }
5493df72098SAndreas Gohr    echo '</div>';
5503df72098SAndreas Gohr}
5513df72098SAndreas Gohr
5523df72098SAndreas Gohr/**
5533df72098SAndreas Gohr * Print the media upload form if permissions are correct
5543df72098SAndreas Gohr *
5553df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5563df72098SAndreas Gohr */
5573df72098SAndreas Gohrfunction media_uploadform($ns, $auth){
5583df72098SAndreas Gohr    global $lang;
5593df72098SAndreas Gohr
5603df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions?
5613df72098SAndreas Gohr
5623df72098SAndreas Gohr    ?>
563cf6894dfSAndreas Gohr    <div class="upload"><?php echo $lang['mediaupload']?></div>
5643df72098SAndreas Gohr    <form action="<?php echo DOKU_BASE?>lib/exe/mediamanager.php"
5653df72098SAndreas Gohr          method="post" enctype="multipart/form-data" class="upload">
5668902ae3dSAnika Henke      <fieldset>
567f495da32SAnika Henke        <legend class="hidden"><?php echo $lang['btn_upload']?></legend>
5683df72098SAndreas Gohr        <input type="hidden" name="ns" value="<?php echo hsc($ns)?>" />
569f2ea8432SAndreas Gohr        <?php formSecurityToken();?>
5708902ae3dSAnika Henke        <p>
5718902ae3dSAnika Henke          <label for="upload__file"><?php echo $lang['txt_upload']?>:</label>
5728902ae3dSAnika Henke          <input type="file" name="upload" class="edit" id="upload__file" />
5738902ae3dSAnika Henke        </p>
574cf6894dfSAndreas Gohr
5758902ae3dSAnika Henke        <p>
5768902ae3dSAnika Henke          <label for="upload__name"><?php echo $lang['txt_filename']?>:</label>
57718d69493SAndreas Gohr          <span class="nowrap">
57844e099adSAndreas Gohr          <input type="text" name="id" class="edit" id="upload__name" /><input
57944e099adSAndreas Gohr                 type="submit" class="button" value="<?php echo $lang['btn_upload']?>"
58044e099adSAndreas Gohr                 accesskey="s" />
58144e099adSAndreas Gohr          </span>
5828902ae3dSAnika Henke        </p>
5833df72098SAndreas Gohr
5843df72098SAndreas Gohr        <?php if($auth >= AUTH_DELETE){?>
5858902ae3dSAnika Henke            <p>
5868902ae3dSAnika Henke              <input type="checkbox" name="ow" value="1" id="dw__ow" class="check" />
5878902ae3dSAnika Henke              <label for="dw__ow" class="check"><?php echo $lang['txt_overwrt']?></label>
5888902ae3dSAnika Henke            </p>
5893df72098SAndreas Gohr        <?php }?>
5908902ae3dSAnika Henke      </fieldset>
5913df72098SAndreas Gohr    </form>
5923df72098SAndreas Gohr    <?php
5933df72098SAndreas Gohr}
5943df72098SAndreas Gohr
5953df72098SAndreas Gohr
5963df72098SAndreas Gohr
5973df72098SAndreas Gohr/**
5983df72098SAndreas Gohr * Build a tree outline of available media namespaces
5993df72098SAndreas Gohr *
6003df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6013df72098SAndreas Gohr */
6023df72098SAndreas Gohrfunction media_nstree($ns){
6033df72098SAndreas Gohr    global $conf;
604256ca81eSAndreas Gohr    global $lang;
6053df72098SAndreas Gohr
6063df72098SAndreas Gohr    // currently selected namespace
6073df72098SAndreas Gohr    $ns  = cleanID($ns);
6083df72098SAndreas Gohr    if(empty($ns)){
6093df72098SAndreas Gohr        $ns = dirname(str_replace(':','/',$ID));
6103df72098SAndreas Gohr        if($ns == '.') $ns ='';
6113df72098SAndreas Gohr    }
6123df72098SAndreas Gohr    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
6133df72098SAndreas Gohr
6143df72098SAndreas Gohr    $data = array();
615ee7b5a62SAndreas Gohr    search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true));
6163df72098SAndreas Gohr
6173df72098SAndreas Gohr    // wrap a list with the root level around the other namespaces
618256ca81eSAndreas Gohr    $item = array( 'level' => 0, 'id' => '',
619256ca81eSAndreas Gohr                   'open' =>'true', 'label' => '['.$lang['mediaroot'].']');
6203df72098SAndreas Gohr
6213df72098SAndreas Gohr    echo '<ul class="idx">';
6223df72098SAndreas Gohr    echo media_nstree_li($item);
6233df72098SAndreas Gohr    echo media_nstree_item($item);
6243df72098SAndreas Gohr    echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li');
6253df72098SAndreas Gohr    echo '</li>';
6263df72098SAndreas Gohr    echo '</ul>';
6273df72098SAndreas Gohr}
6283df72098SAndreas Gohr
6293df72098SAndreas Gohr/**
6303df72098SAndreas Gohr * Userfunction for html_buildlist
6313df72098SAndreas Gohr *
6323df72098SAndreas Gohr * Prints a media namespace tree item
6333df72098SAndreas Gohr *
6343df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6353df72098SAndreas Gohr */
6363df72098SAndreas Gohrfunction media_nstree_item($item){
6373df72098SAndreas Gohr    $pos   = strrpos($item['id'], ':');
6383df72098SAndreas Gohr    $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
6393df72098SAndreas Gohr    if(!$item['label']) $item['label'] = $label;
6403df72098SAndreas Gohr
6413df72098SAndreas Gohr    $ret  = '';
6423df72098SAndreas Gohr    $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
6433df72098SAndreas Gohr    $ret .= $item['label'];
6443df72098SAndreas Gohr    $ret .= '</a>';
6453df72098SAndreas Gohr    return $ret;
6463df72098SAndreas Gohr}
6473df72098SAndreas Gohr
6483df72098SAndreas Gohr/**
6493df72098SAndreas Gohr * Userfunction for html_buildlist
6503df72098SAndreas Gohr *
6513df72098SAndreas Gohr * Prints a media namespace tree item opener
6523df72098SAndreas Gohr *
6533df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6543df72098SAndreas Gohr */
6553df72098SAndreas Gohrfunction media_nstree_li($item){
6563df72098SAndreas Gohr    $class='media level'.$item['level'];
6573df72098SAndreas Gohr    if($item['open']){
6583df72098SAndreas Gohr        $class .= ' open';
6593df72098SAndreas Gohr        $img   = DOKU_BASE.'lib/images/minus.gif';
6607af1b404SAnika Henke        $alt   = '&minus;';
6613df72098SAndreas Gohr    }else{
6623df72098SAndreas Gohr        $class .= ' closed';
6633df72098SAndreas Gohr        $img   = DOKU_BASE.'lib/images/plus.gif';
6647af1b404SAnika Henke        $alt   = '+';
6653df72098SAndreas Gohr    }
6663df72098SAndreas Gohr    return '<li class="'.$class.'">'.
6677af1b404SAnika Henke           '<img src="'.$img.'" alt="'.$alt.'" />';
6683df72098SAndreas Gohr}
669