xref: /dokuwiki/inc/media.php (revision 7503035957fa441a0911bd9f96dc4f554875123a)
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;
533df72098SAndreas Gohr    global $lang;
543df72098SAndreas Gohr    $src = mediaFN($id);
553df72098SAndreas Gohr
563df72098SAndreas Gohr    $meta = new JpegMeta($src);
573df72098SAndreas Gohr    $meta->_parseAll();
583df72098SAndreas Gohr
593df72098SAndreas Gohr    foreach($data as $key => $val){
603df72098SAndreas Gohr        $val=trim($val);
613df72098SAndreas Gohr        if(empty($val)){
623df72098SAndreas Gohr            $meta->deleteField($key);
633df72098SAndreas Gohr        }else{
643df72098SAndreas Gohr            $meta->setField($key,$val);
653df72098SAndreas Gohr        }
663df72098SAndreas Gohr    }
673df72098SAndreas Gohr
683df72098SAndreas Gohr    if($meta->save()){
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;
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);
1053df72098SAndreas Gohr
1063df72098SAndreas Gohr        // prepare attributes
1073df72098SAndreas Gohr        $p = array();
1083df72098SAndreas Gohr        $p['class'] = 'edit';
1093df72098SAndreas Gohr        $p['id']    = 'meta__'.$key;
1103df72098SAndreas Gohr        $p['name']  = 'meta['.$field[0].']';
1113df72098SAndreas Gohr
1123df72098SAndreas Gohr        // put label
1133df72098SAndreas Gohr        echo '<div class="metafield">';
1143df72098SAndreas Gohr        echo '<label for="meta__'.$key.'">';
1153df72098SAndreas Gohr        echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1];
116cf6894dfSAndreas Gohr        echo ':</label>';
1173df72098SAndreas Gohr
1183df72098SAndreas Gohr        // put input field
1193df72098SAndreas Gohr        if($field[2] == 'text'){
1203df72098SAndreas Gohr            $p['value'] = $value;
1213df72098SAndreas Gohr            $p['type']  = 'text';
1223df72098SAndreas Gohr            $att = buildAttributes($p);
1233df72098SAndreas Gohr            echo "<input $att/>".NL;
1243df72098SAndreas Gohr        }else{
1253df72098SAndreas Gohr            $att = buildAttributes($p);
1261440e523SAnika Henke            echo "<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>'.NL;
1273df72098SAndreas Gohr        }
1283df72098SAndreas Gohr        echo '</div>'.NL;
1293df72098SAndreas Gohr    }
1303df72098SAndreas Gohr    echo '<div class="buttons">'.NL;
1311440e523SAnika Henke    echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.NL;
1323df72098SAndreas Gohr    echo '<input name="do[save]" type="submit" value="'.$lang['btn_save'].
1333df72098SAndreas Gohr         '" title="ALT+S" accesskey="s" class="button" />'.NL;
1343df72098SAndreas Gohr    echo '<input name="do[cancel]" type="submit" value="'.$lang['btn_cancel'].
1353df72098SAndreas Gohr         '" title="ALT+C" accesskey="c" class="button" />'.NL;
1364868e1c6SAndreas Gohr    echo '</div>'.NL;
1373df72098SAndreas Gohr    echo '</form>'.NL;
1383df72098SAndreas Gohr}
1393df72098SAndreas Gohr
1403df72098SAndreas Gohr/**
1413df72098SAndreas Gohr * Handles media file deletions
1423df72098SAndreas Gohr *
1433df72098SAndreas Gohr * If configured, checks for media references before deletion
1443df72098SAndreas Gohr *
1453df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1463df72098SAndreas Gohr * @return mixed false on error, true on delete or array with refs
1473df72098SAndreas Gohr */
1483df72098SAndreas Gohrfunction media_delete($id,$auth){
1493df72098SAndreas Gohr    if($auth < AUTH_DELETE) return false;
1503df72098SAndreas Gohr    global $conf;
1513df72098SAndreas Gohr    global $lang;
1523df72098SAndreas Gohr
1533df72098SAndreas Gohr    $mediareferences = array();
1543df72098SAndreas Gohr    if($conf['refcheck']){
1553df72098SAndreas Gohr        search($mediareferences,$conf['datadir'],'search_reference',array('query' => $id));
1563df72098SAndreas Gohr    }
1573df72098SAndreas Gohr
1583df72098SAndreas Gohr    if(!count($mediareferences)){
1593df72098SAndreas Gohr        $file = mediaFN($id);
1603df72098SAndreas Gohr        if(@unlink($file)){
1613df72098SAndreas Gohr            msg(str_replace('%s',noNS($id),$lang['deletesucc']),1);
1623df72098SAndreas Gohr            io_sweepNS($id,'mediadir');
1633df72098SAndreas Gohr            return true;
1643df72098SAndreas Gohr        }
1653df72098SAndreas Gohr        //something went wrong
1663df72098SAndreas Gohr        msg(str_replace('%s',$file,$lang['deletefail']),-1);
1673df72098SAndreas Gohr        return false;
1683df72098SAndreas Gohr    }elseif(!$conf['refshow']){
1693df72098SAndreas Gohr        msg(str_replace('%s',noNS($id),$lang['mediainuse']),0);
1703df72098SAndreas Gohr        return false;
1713df72098SAndreas Gohr    }
1723df72098SAndreas Gohr
1733df72098SAndreas Gohr    return $mediareferences;
1743df72098SAndreas Gohr}
1753df72098SAndreas Gohr
1763df72098SAndreas Gohr/**
1773df72098SAndreas Gohr * Handles media file uploads
1783df72098SAndreas Gohr *
1793df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1803df72098SAndreas Gohr * @return mixed false on error, id of the new file on success
1813df72098SAndreas Gohr */
1823df72098SAndreas Gohrfunction media_upload($ns,$auth){
1833df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return false;
1843df72098SAndreas Gohr    require_once(DOKU_INC.'inc/confutils.php');
1853df72098SAndreas Gohr    global $lang;
1863df72098SAndreas Gohr    global $conf;
1873df72098SAndreas Gohr
18844409c3dSAndreas Gohr    // get file and id
1893df72098SAndreas Gohr    $id   = $_POST['id'];
1903df72098SAndreas Gohr    $file = $_FILES['upload'];
1913df72098SAndreas Gohr    if(empty($id)) $id = $file['name'];
19244409c3dSAndreas Gohr
19344409c3dSAndreas Gohr    // check extensions
1948cb1eb01SAndreas Gohr    list($fext,$fmime) = mimetype($file['name']);
1958cb1eb01SAndreas Gohr    list($iext,$imime) = mimetype($id);
19644409c3dSAndreas Gohr    if($fext && !$iext){
1978cb1eb01SAndreas Gohr        // no extension specified in id - read original one
19844409c3dSAndreas Gohr        $id   .= '.'.$fext;
1998cb1eb01SAndreas Gohr        $imime = $fmime;
20044409c3dSAndreas Gohr    }elseif($fext && $fext != $iext){
20144409c3dSAndreas Gohr        // extension was changed, print warning
20244409c3dSAndreas Gohr        msg(sprintf($lang['mediaextchange'],$fext,$iext));
20344409c3dSAndreas Gohr    }
20444409c3dSAndreas Gohr
2053df72098SAndreas Gohr    // get filename
20644409c3dSAndreas Gohr    $id   = cleanID($ns.':'.$id);
2073df72098SAndreas Gohr    $fn   = mediaFN($id);
2083df72098SAndreas Gohr
2093df72098SAndreas Gohr    // get filetype regexp
2103df72098SAndreas Gohr    $types = array_keys(getMimeTypes());
2113df72098SAndreas Gohr    $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
2123df72098SAndreas Gohr    $regex = join('|',$types);
2133df72098SAndreas Gohr
2143df72098SAndreas Gohr    // because a temp file was created already
2153df72098SAndreas Gohr    if(preg_match('/\.('.$regex.')$/i',$fn)){
2163df72098SAndreas Gohr        //check for overwrite
2173df72098SAndreas Gohr        if(@file_exists($fn) && (!$_POST['ow'] || $auth < AUTH_DELETE)){
2183df72098SAndreas Gohr            msg($lang['uploadexist'],0);
2193df72098SAndreas Gohr            return false;
2203df72098SAndreas Gohr        }
2218cb1eb01SAndreas Gohr        // check for valid content
2228cb1eb01SAndreas Gohr        $ok = media_contentcheck($file['tmp_name'],$imime);
2238cb1eb01SAndreas Gohr        if($ok == -1){
2248cb1eb01SAndreas Gohr            msg(sprintf($lang['uploadbadcontent'],".$iext"),-1);
2258cb1eb01SAndreas Gohr            return false;
2268cb1eb01SAndreas Gohr        }elseif($ok == -2){
2278cb1eb01SAndreas Gohr            msg($lang['uploadspam'],-1);
2288cb1eb01SAndreas Gohr            return false;
2298cb1eb01SAndreas Gohr        }
2308cb1eb01SAndreas Gohr
2313df72098SAndreas Gohr        // prepare directory
232cc7d0c94SBen Coburn        io_createNamespace($id, 'media');
2333df72098SAndreas Gohr        if(move_uploaded_file($file['tmp_name'], $fn)) {
23474400ea5SBen Coburn            // Set the correct permission here.
23574400ea5SBen Coburn            // Always chmod media because they may be saved with different permissions than expected from the php umask.
23674400ea5SBen Coburn            // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
23774400ea5SBen Coburn            chmod($fn, $conf['fmode']);
2383df72098SAndreas Gohr            msg($lang['uploadsucc'],1);
239*75030359SAndreas Gohr            media_notify($id,$fn,$imime);
2403df72098SAndreas Gohr            return $id;
2413df72098SAndreas Gohr        }else{
2423df72098SAndreas Gohr            msg($lang['uploadfail'],-1);
2433df72098SAndreas Gohr        }
2443df72098SAndreas Gohr    }else{
2453df72098SAndreas Gohr        msg($lang['uploadwrong'],-1);
2463df72098SAndreas Gohr    }
2473df72098SAndreas Gohr    return false;
2483df72098SAndreas Gohr}
2493df72098SAndreas Gohr
2508cb1eb01SAndreas Gohr/**
2518cb1eb01SAndreas Gohr * This function checks if the uploaded content is really what the
2528cb1eb01SAndreas Gohr * mimetype says it is. We also do spam checking for text types here
2538cb1eb01SAndreas Gohr *
2548cb1eb01SAndreas Gohr * We need to do this stuff because we can not rely on the browser
2558cb1eb01SAndreas Gohr * to do this check correctly. Yes, IE is broken as usual.
2568cb1eb01SAndreas Gohr *
2578cb1eb01SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2588cb1eb01SAndreas Gohr * @link   http://weblog.philringnalda.com/2004/04/06/getting-around-ies-mime-type-mangling
2598cb1eb01SAndreas Gohr * @fixme  check all 26 magic IE filetypes here?
2608cb1eb01SAndreas Gohr */
2618cb1eb01SAndreas Gohrfunction media_contentcheck($file,$mime){
2628cb1eb01SAndreas Gohr    if(substr($mime,0,6) == 'image/'){
2638cb1eb01SAndreas Gohr        $info = @getimagesize($file);
2648cb1eb01SAndreas Gohr        if($mime == 'image/gif' && $info[2] != 1){
2658cb1eb01SAndreas Gohr            return -1;
2668cb1eb01SAndreas Gohr        }elseif($mime == 'image/jpeg' && $info[2] != 2){
2678cb1eb01SAndreas Gohr            return -1;
2688cb1eb01SAndreas Gohr        }elseif($mime == 'image/png' && $info[2] != 3){
2698cb1eb01SAndreas Gohr            return -1;
2708cb1eb01SAndreas Gohr        }
2718cb1eb01SAndreas Gohr        # fixme maybe check other images types as well
2728cb1eb01SAndreas Gohr    }elseif(substr($mime,0,5) == 'text/'){
2738cb1eb01SAndreas Gohr        global $TEXT;
2748cb1eb01SAndreas Gohr        $TEXT = io_readFile($file);
2758cb1eb01SAndreas Gohr        if(checkwordblock()){
2768cb1eb01SAndreas Gohr            msg('Content seems to be spam',-1);
2778cb1eb01SAndreas Gohr            return -2;
2788cb1eb01SAndreas Gohr        }
2798cb1eb01SAndreas Gohr    }
2808cb1eb01SAndreas Gohr    return 0;
2818cb1eb01SAndreas Gohr}
2823df72098SAndreas Gohr
2833df72098SAndreas Gohr/**
284*75030359SAndreas Gohr * Send a notify mail on uploads
285*75030359SAndreas Gohr *
286*75030359SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
287*75030359SAndreas Gohr */
288*75030359SAndreas Gohrfunction media_notify($id,$file,$mime){
289*75030359SAndreas Gohr    global $lang;
290*75030359SAndreas Gohr    global $conf;
291*75030359SAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
292*75030359SAndreas Gohr
293*75030359SAndreas Gohr    $text = rawLocale('uploadmail');
294*75030359SAndreas Gohr    $text = str_replace('@DATE@',date($conf['dformat']),$text);
295*75030359SAndreas Gohr    $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
296*75030359SAndreas Gohr    $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
297*75030359SAndreas Gohr    $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
298*75030359SAndreas Gohr    $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
299*75030359SAndreas Gohr    $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
300*75030359SAndreas Gohr    $text = str_replace('@MIME@',$mime,$text);
301*75030359SAndreas Gohr    $text = str_replace('@MEDIA@',DOKU_URL.ml($id,'',true,'&'),$text);
302*75030359SAndreas Gohr    $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text);
303*75030359SAndreas Gohr
304*75030359SAndreas Gohr    $from = $conf['mailfrom'];
305*75030359SAndreas Gohr    $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from);
306*75030359SAndreas Gohr    $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from);
307*75030359SAndreas Gohr    $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from);
308*75030359SAndreas Gohr
309*75030359SAndreas Gohr    $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id;
310*75030359SAndreas Gohr
311*75030359SAndreas Gohr    mail_send($conf['notify'],$subject,$text,$from);
312*75030359SAndreas Gohr}
313*75030359SAndreas Gohr
314*75030359SAndreas Gohr/**
3153df72098SAndreas Gohr * List all files in a given Media namespace
3163df72098SAndreas Gohr */
3173df72098SAndreas Gohrfunction media_filelist($ns,$auth=null,$jump=''){
3183df72098SAndreas Gohr    global $conf;
3193df72098SAndreas Gohr    global $lang;
3203df72098SAndreas Gohr    $ns = cleanID($ns);
3213df72098SAndreas Gohr
3223df72098SAndreas Gohr    // check auth our self if not given (needed for ajax calls)
3233df72098SAndreas Gohr    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
3243df72098SAndreas Gohr
3256c48a22eSAndreas Gohr    echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
3263df72098SAndreas Gohr
3273df72098SAndreas Gohr    if($auth < AUTH_READ){
3283df72098SAndreas Gohr        // FIXME: print permission warning here instead?
3293df72098SAndreas Gohr        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
3303df72098SAndreas Gohr        return;
3313df72098SAndreas Gohr    }
3323df72098SAndreas Gohr
3333df72098SAndreas Gohr    media_uploadform($ns, $auth);
3343df72098SAndreas Gohr
3353df72098SAndreas Gohr    $dir = utf8_encodeFN(str_replace(':','/',$ns));
3363df72098SAndreas Gohr    $data = array();
3373df72098SAndreas Gohr    search($data,$conf['mediadir'],'search_media',array(),$dir);
3383df72098SAndreas Gohr
3393df72098SAndreas Gohr    if(!count($data)){
3403df72098SAndreas Gohr        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
3413df72098SAndreas Gohr        return;
3423df72098SAndreas Gohr    }
3433df72098SAndreas Gohr
3443df72098SAndreas Gohr    foreach($data as $item){
3453df72098SAndreas Gohr        media_printfile($item,$auth,$jump);
3463df72098SAndreas Gohr    }
3473df72098SAndreas Gohr}
3483df72098SAndreas Gohr
3493df72098SAndreas Gohr/**
3503df72098SAndreas Gohr * Print action links for a file depending on filetype
3513df72098SAndreas Gohr * and available permissions
3523df72098SAndreas Gohr *
3533df72098SAndreas Gohr * @todo contains inline javascript
3543df72098SAndreas Gohr */
3553df72098SAndreas Gohrfunction media_fileactions($item,$auth){
3563df72098SAndreas Gohr    global $lang;
3573df72098SAndreas Gohr
358cf6894dfSAndreas Gohr    // view button
359cf6894dfSAndreas Gohr    $link = ml($item['id'],'',true);
360cf6894dfSAndreas Gohr    echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
361cf6894dfSAndreas Gohr         'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>';
362cf6894dfSAndreas Gohr
363cf6894dfSAndreas Gohr
364cf6894dfSAndreas Gohr    // no further actions if not writable
3653df72098SAndreas Gohr    if(!$item['writable']) return;
3663df72098SAndreas Gohr
3673df72098SAndreas Gohr    // delete button
3683df72098SAndreas Gohr    if($auth >= AUTH_DELETE){
3693df72098SAndreas Gohr        $ask  = addslashes($lang['del_confirm']).'\\n';
3703df72098SAndreas Gohr        $ask .= addslashes($item['id']);
3713df72098SAndreas Gohr
3723df72098SAndreas Gohr        echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).'" '.
3733df72098SAndreas Gohr             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
3743df72098SAndreas Gohr             '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '.
3753df72098SAndreas Gohr             'title="'.$lang['btn_delete'].'" class="btn" /></a>';
3763df72098SAndreas Gohr    }
3773df72098SAndreas Gohr
3783df72098SAndreas Gohr    // edit button
3793df72098SAndreas Gohr    if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){
3803df72098SAndreas Gohr        echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'.
3813df72098SAndreas Gohr             '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '.
3823df72098SAndreas Gohr             'title="'.$lang['metaedit'].'" class="btn" /></a>';
3833df72098SAndreas Gohr    }
3843df72098SAndreas Gohr
3853df72098SAndreas Gohr}
3863df72098SAndreas Gohr
3873df72098SAndreas Gohr/**
3883df72098SAndreas Gohr * Formats and prints one file in the list
3893df72098SAndreas Gohr */
3903df72098SAndreas Gohrfunction media_printfile($item,$auth,$jump){
39164c9cfd5SAndreas Gohr    global $lang;
3925e7fa82eSAndreas Gohr    global $conf;
39364c9cfd5SAndreas Gohr
3943df72098SAndreas Gohr    // Prepare zebra coloring
3953df72098SAndreas Gohr    // I always wanted to use this variable name :-D
3963df72098SAndreas Gohr    static $twibble = 1;
3973df72098SAndreas Gohr    $twibble *= -1;
3983df72098SAndreas Gohr    $zebra = ($twibble == -1) ? 'odd' : 'even';
3993df72098SAndreas Gohr
4003df72098SAndreas Gohr    // Automatically jump to recent action
4013df72098SAndreas Gohr    if($jump == $item['id']) {
4023df72098SAndreas Gohr        $jump = ' id="scroll__here" ';
4033df72098SAndreas Gohr    }else{
4043df72098SAndreas Gohr        $jump = '';
4053df72098SAndreas Gohr    }
4063df72098SAndreas Gohr
4073df72098SAndreas Gohr    // Prepare fileicons
4083df72098SAndreas Gohr    list($ext,$mime) = mimetype($item['file']);
4093df72098SAndreas Gohr    $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
4103df72098SAndreas Gohr    $class = 'select mediafile mf_'.$class;
4113df72098SAndreas Gohr
4123df72098SAndreas Gohr    // Prepare filename
4133df72098SAndreas Gohr    $file = utf8_decodeFN($item['file']);
4143df72098SAndreas Gohr
4153df72098SAndreas Gohr    // Prepare info
4163df72098SAndreas Gohr    $info = '';
4173df72098SAndreas Gohr    if($item['isimg']){
4183df72098SAndreas Gohr        $info .= (int) $item['meta']->getField('File.Width');
4193df72098SAndreas Gohr        $info .= '&#215;';
4203df72098SAndreas Gohr        $info .= (int) $item['meta']->getField('File.Height');
4213df72098SAndreas Gohr        $info .= ' ';
4223df72098SAndreas Gohr    }
4235e7fa82eSAndreas Gohr    $info .= '<i>'.date($conf['dformat'],$item['mtime']).'</i>';
4245e7fa82eSAndreas Gohr    $info .= ' ';
4253df72098SAndreas Gohr    $info .= filesize_h($item['size']);
4263df72098SAndreas Gohr
4273df72098SAndreas Gohr    // ouput
4283df72098SAndreas Gohr    echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
4293df72098SAndreas Gohr    echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> ';
4303df72098SAndreas Gohr    echo '<span class="info">('.$info.')</span>'.NL;
4313df72098SAndreas Gohr    media_fileactions($item,$auth);
43264c9cfd5SAndreas Gohr    echo '<div class="example" id="ex_'.$item['id'].'">';
4330b173dceSAndreas Gohr    echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>';
43464c9cfd5SAndreas Gohr    echo '</div>';
4353df72098SAndreas Gohr    if($item['isimg']) media_printimgdetail($item);
4363df72098SAndreas Gohr    echo '<div class="clearer"></div>'.NL;
4373df72098SAndreas Gohr    echo '</div>'.NL;
4383df72098SAndreas Gohr}
4393df72098SAndreas Gohr
4403df72098SAndreas Gohr/**
4413df72098SAndreas Gohr * Prints a thumbnail and metainfos
4423df72098SAndreas Gohr */
4433df72098SAndreas Gohrfunction media_printimgdetail($item){
4443df72098SAndreas Gohr    // prepare thumbnail
4453df72098SAndreas Gohr    $w = (int) $item['meta']->getField('File.Width');
4463df72098SAndreas Gohr    $h = (int) $item['meta']->getField('File.Height');
4473df72098SAndreas Gohr    if($w>120 || $h>120){
4483df72098SAndreas Gohr        $ratio = $item['meta']->getResizeRatio(120);
4493df72098SAndreas Gohr        $w = floor($w * $ratio);
4503df72098SAndreas Gohr        $h = floor($h * $ratio);
4513df72098SAndreas Gohr    }
4523df72098SAndreas Gohr    $src = ml($item['id'],array('w'=>$w,'h'=>$h));
4533df72098SAndreas Gohr    $p = array();
4543df72098SAndreas Gohr    $p['width']  = $w;
4553df72098SAndreas Gohr    $p['height'] = $h;
4563df72098SAndreas Gohr    $p['alt']    = $item['id'];
4573df72098SAndreas Gohr    $p['class']  = 'thumb';
4583df72098SAndreas Gohr    $att = buildAttributes($p);
4593df72098SAndreas Gohr
4603df72098SAndreas Gohr    // output
4613df72098SAndreas Gohr    echo '<div class="detail">';
4623df72098SAndreas Gohr    echo '<div class="thumb">';
4633df72098SAndreas Gohr    echo '<a name="d_'.$item['id'].'" class="select">';
4643df72098SAndreas Gohr    echo '<img src="'.$src.'" '.$att.' />';
4653df72098SAndreas Gohr    echo '</a>';
4663df72098SAndreas Gohr    echo '</div>';
4673df72098SAndreas Gohr
4683df72098SAndreas Gohr    // read EXIF/IPTC data
4693df72098SAndreas Gohr    $t = $item['meta']->getField('IPTC.Headline');
4700b173dceSAndreas Gohr    $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
4713df72098SAndreas Gohr                                       'EXIF.TIFFImageDescription',
4723df72098SAndreas Gohr                                       'EXIF.TIFFUserComment'));
4730b173dceSAndreas Gohr    if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...';
4740b173dceSAndreas Gohr    $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
4753df72098SAndreas Gohr
4760b173dceSAndreas Gohr    // print EXIF/IPTC data
4770b173dceSAndreas Gohr    if($t || $d || $k ){
4780b173dceSAndreas Gohr        echo '<p>';
4790b173dceSAndreas Gohr        if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />';
4800b173dceSAndreas Gohr        if($d) echo htmlspecialchars($d).'<br />';
4810b173dceSAndreas Gohr        if($t) echo '<em>'.htmlspecialchars($k).'</em>';
4823df72098SAndreas Gohr        echo '</p>';
4830b173dceSAndreas Gohr    }
4843df72098SAndreas Gohr    echo '</div>';
4853df72098SAndreas Gohr}
4863df72098SAndreas Gohr
4873df72098SAndreas Gohr/**
4883df72098SAndreas Gohr * Print the media upload form if permissions are correct
4893df72098SAndreas Gohr *
4903df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4913df72098SAndreas Gohr */
4923df72098SAndreas Gohrfunction media_uploadform($ns, $auth){
4933df72098SAndreas Gohr    global $lang;
4943df72098SAndreas Gohr
4953df72098SAndreas Gohr    if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions?
4963df72098SAndreas Gohr
4973df72098SAndreas Gohr    ?>
498cf6894dfSAndreas Gohr    <div class="upload"><?php echo $lang['mediaupload']?></div>
4993df72098SAndreas Gohr    <form action="<?php echo DOKU_BASE?>lib/exe/mediamanager.php"
5003df72098SAndreas Gohr          method="post" enctype="multipart/form-data" class="upload">
5018902ae3dSAnika Henke      <fieldset>
5023df72098SAndreas Gohr        <input type="hidden" name="ns" value="<?php echo hsc($ns)?>" />
5033df72098SAndreas Gohr
5048902ae3dSAnika Henke        <p>
5058902ae3dSAnika Henke          <label for="upload__file"><?php echo $lang['txt_upload']?>:</label>
5068902ae3dSAnika Henke          <input type="file" name="upload" class="edit" id="upload__file" />
5078902ae3dSAnika Henke        </p>
508cf6894dfSAndreas Gohr
5098902ae3dSAnika Henke        <p>
5108902ae3dSAnika Henke          <label for="upload__name"><?php echo $lang['txt_filename']?>:</label>
51118d69493SAndreas Gohr          <span class="nowrap">
51244e099adSAndreas Gohr          <input type="text" name="id" class="edit" id="upload__name" /><input
51344e099adSAndreas Gohr                 type="submit" class="button" value="<?php echo $lang['btn_upload']?>"
51444e099adSAndreas Gohr                 accesskey="s" />
51544e099adSAndreas Gohr          </span>
5168902ae3dSAnika Henke        </p>
5173df72098SAndreas Gohr
5183df72098SAndreas Gohr        <?php if($auth >= AUTH_DELETE){?>
5198902ae3dSAnika Henke            <p>
5208902ae3dSAnika Henke              <input type="checkbox" name="ow" value="1" id="dw__ow" class="check" />
5218902ae3dSAnika Henke              <label for="dw__ow" class="check"><?php echo $lang['txt_overwrt']?></label>
5228902ae3dSAnika Henke            </p>
5233df72098SAndreas Gohr        <?php }?>
5248902ae3dSAnika Henke      </fieldset>
5253df72098SAndreas Gohr    </form>
5263df72098SAndreas Gohr    <?php
5273df72098SAndreas Gohr}
5283df72098SAndreas Gohr
5293df72098SAndreas Gohr
5303df72098SAndreas Gohr
5313df72098SAndreas Gohr/**
5323df72098SAndreas Gohr * Build a tree outline of available media namespaces
5333df72098SAndreas Gohr *
5343df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5353df72098SAndreas Gohr */
5363df72098SAndreas Gohrfunction media_nstree($ns){
5373df72098SAndreas Gohr    global $conf;
538256ca81eSAndreas Gohr    global $lang;
5393df72098SAndreas Gohr
5403df72098SAndreas Gohr    // currently selected namespace
5413df72098SAndreas Gohr    $ns  = cleanID($ns);
5423df72098SAndreas Gohr    if(empty($ns)){
5433df72098SAndreas Gohr        $ns = dirname(str_replace(':','/',$ID));
5443df72098SAndreas Gohr        if($ns == '.') $ns ='';
5453df72098SAndreas Gohr    }
5463df72098SAndreas Gohr    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
5473df72098SAndreas Gohr
5483df72098SAndreas Gohr    $data = array();
549ee7b5a62SAndreas Gohr    search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true));
5503df72098SAndreas Gohr
5513df72098SAndreas Gohr    // wrap a list with the root level around the other namespaces
552256ca81eSAndreas Gohr    $item = array( 'level' => 0, 'id' => '',
553256ca81eSAndreas Gohr                   'open' =>'true', 'label' => '['.$lang['mediaroot'].']');
5543df72098SAndreas Gohr
5553df72098SAndreas Gohr    echo '<ul class="idx">';
5563df72098SAndreas Gohr    echo media_nstree_li($item);
5573df72098SAndreas Gohr    echo media_nstree_item($item);
5583df72098SAndreas Gohr    echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li');
5593df72098SAndreas Gohr    echo '</li>';
5603df72098SAndreas Gohr    echo '</ul>';
5613df72098SAndreas Gohr}
5623df72098SAndreas Gohr
5633df72098SAndreas Gohr/**
5643df72098SAndreas Gohr * Userfunction for html_buildlist
5653df72098SAndreas Gohr *
5663df72098SAndreas Gohr * Prints a media namespace tree item
5673df72098SAndreas Gohr *
5683df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5693df72098SAndreas Gohr */
5703df72098SAndreas Gohrfunction media_nstree_item($item){
5713df72098SAndreas Gohr    $pos   = strrpos($item['id'], ':');
5723df72098SAndreas Gohr    $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
5733df72098SAndreas Gohr    if(!$item['label']) $item['label'] = $label;
5743df72098SAndreas Gohr
5753df72098SAndreas Gohr    $ret  = '';
5763df72098SAndreas Gohr    $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
5773df72098SAndreas Gohr    $ret .= $item['label'];
5783df72098SAndreas Gohr    $ret .= '</a>';
5793df72098SAndreas Gohr    return $ret;
5803df72098SAndreas Gohr}
5813df72098SAndreas Gohr
5823df72098SAndreas Gohr/**
5833df72098SAndreas Gohr * Userfunction for html_buildlist
5843df72098SAndreas Gohr *
5853df72098SAndreas Gohr * Prints a media namespace tree item opener
5863df72098SAndreas Gohr *
5873df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5883df72098SAndreas Gohr */
5893df72098SAndreas Gohrfunction media_nstree_li($item){
5903df72098SAndreas Gohr    $class='media level'.$item['level'];
5913df72098SAndreas Gohr    if($item['open']){
5923df72098SAndreas Gohr        $class .= ' open';
5933df72098SAndreas Gohr        $img   = DOKU_BASE.'lib/images/minus.gif';
5947af1b404SAnika Henke        $alt   = '&minus;';
5953df72098SAndreas Gohr    }else{
5963df72098SAndreas Gohr        $class .= ' closed';
5973df72098SAndreas Gohr        $img   = DOKU_BASE.'lib/images/plus.gif';
5987af1b404SAnika Henke        $alt   = '+';
5993df72098SAndreas Gohr    }
6003df72098SAndreas Gohr    return '<li class="'.$class.'">'.
6017af1b404SAnika Henke           '<img src="'.$img.'" alt="'.$alt.'" />';
6023df72098SAndreas Gohr}
603