xref: /dokuwiki/inc/media.php (revision 3e98e6857f2c48127d4169d02e341d1013c00bac)
1<?php
2/**
3 * All output and handler function needed for the media management popup
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) die('meh.');
10if(!defined('NL')) define('NL',"\n");
11
12/**
13 * Lists pages which currently use a media file selected for deletion
14 *
15 * References uses the same visual as search results and share
16 * their CSS tags except pagenames won't be links.
17 *
18 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
19 */
20function media_filesinuse($data,$id){
21    global $lang;
22    echo '<h1>'.$lang['reference'].' <code>'.hsc(noNS($id)).'</code></h1>';
23    echo '<p>'.hsc($lang['ref_inuse']).'</p>';
24
25    $hidden=0; //count of hits without read permission
26    foreach($data as $row){
27        if(auth_quickaclcheck($row) >= AUTH_READ && isVisiblePage($row)){
28            echo '<div class="search_result">';
29            echo '<span class="mediaref_ref">'.hsc($row).'</span>';
30            echo '</div>';
31        }else
32            $hidden++;
33    }
34    if ($hidden){
35        print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
36    }
37}
38
39/**
40 * Handles the saving of image meta data
41 *
42 * @author Andreas Gohr <andi@splitbrain.org>
43 * @author Kate Arzamastseva <pshns@ukr.net>
44 */
45function media_metasave($id,$auth,$data){
46    if($auth < AUTH_UPLOAD) return false;
47    if(!checkSecurityToken()) return false;
48    global $lang;
49    global $conf;
50    $src = mediaFN($id);
51
52    $meta = new JpegMeta($src);
53    $meta->_parseAll();
54
55    foreach($data as $key => $val){
56        $val=trim($val);
57        if(empty($val)){
58            $meta->deleteField($key);
59        }else{
60            $meta->setField($key,$val);
61        }
62    }
63
64    $old = @filemtime($src);
65    if(!@file_exists(mediaFN($id, $old)) && @file_exists($src)) {
66        // add old revision to the attic
67        media_saveOldRevision($id);
68    }
69
70    if($meta->save()){
71        if($conf['fperm']) chmod($src, $conf['fperm']);
72
73        $new = @filemtime($src);
74        // add a log entry to the media changelog
75        addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_EDIT, $lang['media_meta_edited']);
76
77        msg($lang['metasaveok'],1);
78        return $id;
79    }else{
80        msg($lang['metasaveerr'],-1);
81        return false;
82    }
83}
84
85/**
86 * Display the form to edit image meta data
87 *
88 * @author Andreas Gohr <andi@splitbrain.org>
89 * @author Kate Arzamastseva <pshns@ukr.net>
90 */
91function media_metaform($id,$auth,$fullscreen = false){
92    global $lang, $config_cascade;
93
94    if($auth < AUTH_UPLOAD) {
95        echo '<div class="nothing">'.$lang['media_perm_upload'].'</div>'.NL;
96        return false;
97    }
98
99    // load the field descriptions
100    static $fields = null;
101    if(is_null($fields)){
102        $config_files = getConfigFiles('mediameta');
103        foreach ($config_files as $config_file) {
104            if(@file_exists($config_file)) include($config_file);
105        }
106    }
107
108    $src = mediaFN($id);
109
110    // output
111    if (!$fullscreen) {
112        echo '<h1>'.hsc(noNS($id)).'</h1>'.NL;
113        echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" accept-charset="utf-8" method="post" class="meta">'.NL;
114    } else {
115        echo '<form action="'.media_managerURL(array('tab_details' => 'view')).
116            '" accept-charset="utf-8" method="post" class="meta">'.NL;
117    }
118    formSecurityToken();
119    foreach($fields as $key => $field){
120        // get current value
121        if (empty($field[0])) continue;
122        $tags = array($field[0]);
123        if(is_array($field[3])) $tags = array_merge($tags,$field[3]);
124        $value = tpl_img_getTag($tags,'',$src);
125        $value = cleanText($value);
126
127        // prepare attributes
128        $p = array();
129        $p['class'] = 'edit';
130        $p['id']    = 'meta__'.$key;
131        $p['name']  = 'meta['.$field[0].']';
132
133        // put label
134        echo '<div class="metafield">';
135        echo '<label for="meta__'.$key.'">';
136        echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1];
137        echo ':</label>';
138
139        // put input field
140        if($field[2] == 'text'){
141            $p['value'] = $value;
142            $p['type']  = 'text';
143            $att = buildAttributes($p);
144            echo "<input $att/>".NL;
145        }else{
146            $att = buildAttributes($p);
147            echo "<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>'.NL;
148        }
149        echo '</div>'.NL;
150    }
151    echo '<div class="buttons">'.NL;
152    echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.NL;
153    if (!$fullscreen) $do = 'do';
154    else $do = 'mediado';
155    echo '<input name="'.$do.'[save]" type="submit" value="'.$lang['btn_save'].
156        '" title="'.$lang['btn_save'].' [S]" accesskey="s" class="button" />'.NL;
157    if (!$fullscreen)
158    echo '<input name="do[cancel]" type="submit" value="'.$lang['btn_cancel'].
159        '" title="'.$lang['btn_cancel'].' [C]" accesskey="c" class="button" />'.NL;
160    echo '</div>'.NL;
161    echo '</form>'.NL;
162}
163
164/**
165 * Convenience function to check if a media file is still in use
166 *
167 * @author Michael Klier <chi@chimeric.de>
168 */
169function media_inuse($id) {
170    global $conf;
171    $mediareferences = array();
172    if($conf['refcheck']){
173        $mediareferences = ft_mediause($id,$conf['refshow']);
174        if(!count($mediareferences)) {
175            return false;
176        } else {
177            return $mediareferences;
178        }
179    } else {
180        return false;
181    }
182}
183
184define('DOKU_MEDIA_DELETED', 1);
185define('DOKU_MEDIA_NOT_AUTH', 2);
186define('DOKU_MEDIA_INUSE', 4);
187define('DOKU_MEDIA_EMPTY_NS', 8);
188
189/**
190 * Handles media file deletions
191 *
192 * If configured, checks for media references before deletion
193 *
194 * @author Andreas Gohr <andi@splitbrain.org>
195 * @return int One of: 0,
196                       DOKU_MEDIA_DELETED,
197                       DOKU_MEDIA_DELETED | DOKU_MEDIA_EMPTY_NS,
198                       DOKU_MEDIA_NOT_AUTH,
199                       DOKU_MEDIA_INUSE
200 */
201function media_delete($id,$auth){
202    if($auth < AUTH_DELETE) return DOKU_MEDIA_NOT_AUTH;
203    if(media_inuse($id)) return DOKU_MEDIA_INUSE;
204
205    $file = mediaFN($id);
206
207    // trigger an event - MEDIA_DELETE_FILE
208    $data['id']   = $id;
209    $data['name'] = basename($file);
210    $data['path'] = $file;
211    $data['size'] = (@file_exists($file)) ? filesize($file) : 0;
212
213    $data['unl'] = false;
214    $data['del'] = false;
215    $evt = new Doku_Event('MEDIA_DELETE_FILE',$data);
216    if ($evt->advise_before()) {
217        $data['unl'] = @unlink($file);
218        if($data['unl']){
219            addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_DELETE);
220            $data['del'] = io_sweepNS($id,'mediadir');
221        }
222    }
223    $evt->advise_after();
224    unset($evt);
225
226    if($data['unl'] && $data['del']){
227        return DOKU_MEDIA_DELETED | DOKU_MEDIA_EMPTY_NS;
228    }
229
230    return $data['unl'] ? DOKU_MEDIA_DELETED : 0;
231}
232
233/**
234 * Handles media file uploads
235 *
236 * @author Andreas Gohr <andi@splitbrain.org>
237 * @author Michael Klier <chi@chimeric.de>
238 * @return mixed false on error, id of the new file on success
239 */
240function media_upload($ns,$auth){
241    if(!checkSecurityToken()) return false;
242    global $lang;
243
244    // get file and id
245    $id   = $_POST['id'];
246    $file = $_FILES['upload'];
247    if(empty($id)) $id = $file['name'];
248
249    // check for errors (messages are done in lib/exe/mediamanager.php)
250    if($file['error']) return false;
251
252    // check extensions
253    list($fext,$fmime,$dl) = mimetype($file['name']);
254    list($iext,$imime,$dl) = mimetype($id);
255    if($fext && !$iext){
256        // no extension specified in id - read original one
257        $id   .= '.'.$fext;
258        $imime = $fmime;
259    }elseif($fext && $fext != $iext){
260        // extension was changed, print warning
261        msg(sprintf($lang['mediaextchange'],$fext,$iext));
262    }
263
264    $res = media_save(array('name' => $file['tmp_name'],
265                            'mime' => $imime,
266                            'ext'  => $iext), $ns.':'.$id,
267                      $_REQUEST['ow'], $auth, 'move_uploaded_file');
268    if (is_array($res)) {
269        msg($res[0], $res[1]);
270        return false;
271    }
272    return $res;
273}
274
275/**
276 * This generates an action event and delegates to _media_upload_action().
277 * Action plugins are allowed to pre/postprocess the uploaded file.
278 * (The triggered event is preventable.)
279 *
280 * Event data:
281 * $data[0]     fn_tmp: the temporary file name (read from $_FILES)
282 * $data[1]     fn: the file name of the uploaded file
283 * $data[2]     id: the future directory id of the uploaded file
284 * $data[3]     imime: the mimetype of the uploaded file
285 * $data[4]     overwrite: if an existing file is going to be overwritten
286 *
287 * @triggers MEDIA_UPLOAD_FINISH
288 */
289function media_save($file, $id, $ow, $auth, $move) {
290    if($auth < AUTH_UPLOAD) {
291        return array("You don't have permissions to upload files.", -1);
292    }
293
294    if (!isset($file['mime']) || !isset($file['ext'])) {
295        list($ext, $mime) = mimetype($id);
296        if (!isset($file['mime'])) {
297            $file['mime'] = $mime;
298        }
299        if (!isset($file['ext'])) {
300            $file['ext'] = $ext;
301        }
302    }
303
304    global $lang;
305
306    // get filename
307    $id   = cleanID($id,false,true);
308    $fn   = mediaFN($id);
309
310    // get filetype regexp
311    $types = array_keys(getMimeTypes());
312    $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
313    $regex = join('|',$types);
314
315    // because a temp file was created already
316    if(!preg_match('/\.('.$regex.')$/i',$fn)) {
317        return array($lang['uploadwrong'],-1);
318    }
319
320    //check for overwrite
321    $overwrite = @file_exists($fn);
322    if($overwrite && (!$ow || $auth < AUTH_DELETE)) {
323        return array($lang['uploadexist'], 0);
324    }
325    // check for valid content
326    $ok = media_contentcheck($file['name'], $file['mime']);
327    if($ok == -1){
328        return array(sprintf($lang['uploadbadcontent'],'.' . $file['ext']),-1);
329    }elseif($ok == -2){
330        return array($lang['uploadspam'],-1);
331    }elseif($ok == -3){
332        return array($lang['uploadxss'],-1);
333    }
334
335    // prepare event data
336    $data[0] = $file['name'];
337    $data[1] = $fn;
338    $data[2] = $id;
339    $data[3] = $file['mime'];
340    $data[4] = $overwrite;
341    $data[5] = $move;
342
343    // trigger event
344    return trigger_event('MEDIA_UPLOAD_FINISH', $data, '_media_upload_action', true);
345}
346
347/**
348 * Callback adapter for media_upload_finish()
349 * @author Michael Klier <chi@chimeric.de>
350 */
351function _media_upload_action($data) {
352    // fixme do further sanity tests of given data?
353    if(is_array($data) && count($data)===6) {
354        return media_upload_finish($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]);
355    } else {
356        return false; //callback error
357    }
358}
359
360/**
361 * Saves an uploaded media file
362 *
363 * @author Andreas Gohr <andi@splitbrain.org>
364 * @author Michael Klier <chi@chimeric.de>
365 * @author Kate Arzamastseva <pshns@ukr.net>
366 */
367function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'move_uploaded_file') {
368    global $conf;
369    global $lang;
370
371    $old = @filemtime($fn);
372    if(!@file_exists(mediaFN($id, $old)) && @file_exists($fn)) {
373        // add old revision to the attic if missing
374        media_saveOldRevision($id);
375    }
376
377    // prepare directory
378    io_createNamespace($id, 'media');
379
380    if($move($fn_tmp, $fn)) {
381        $new = @filemtime($fn);
382        // Set the correct permission here.
383        // Always chmod media because they may be saved with different permissions than expected from the php umask.
384        // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
385        chmod($fn, $conf['fmode']);
386        msg($lang['uploadsucc'],1);
387        media_notify($id,$fn,$imime);
388        // add a log entry to the media changelog
389        if ($overwrite) {
390            addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_EDIT);
391        } else {
392            addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_CREATE, $lang['created']);
393        }
394        return $id;
395    }else{
396        return array($lang['uploadfail'],-1);
397    }
398}
399
400/**
401 * Moves the current version of media file to the media_attic
402 * directory
403 *
404 * @author Kate Arzamastseva <pshns@ukr.net>
405 * @param string $id
406 * @return int - revision date
407 */
408function media_saveOldRevision($id){
409    global $conf;
410    $oldf = mediaFN($id);
411    if(!@file_exists($oldf)) return '';
412    $date = filemtime($oldf);
413    $newf = mediaFN($id,$date);
414    io_makeFileDir($newf);
415    if(copy($oldf, $newf)) {
416        // Set the correct permission here.
417        // Always chmod media because they may be saved with different permissions than expected from the php umask.
418        // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
419        chmod($newf, $conf['fmode']);
420    }
421    return $date;
422}
423
424/**
425 * This function checks if the uploaded content is really what the
426 * mimetype says it is. We also do spam checking for text types here.
427 *
428 * We need to do this stuff because we can not rely on the browser
429 * to do this check correctly. Yes, IE is broken as usual.
430 *
431 * @author Andreas Gohr <andi@splitbrain.org>
432 * @link   http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting
433 * @fixme  check all 26 magic IE filetypes here?
434 */
435function media_contentcheck($file,$mime){
436    global $conf;
437    if($conf['iexssprotect']){
438        $fh = @fopen($file, 'rb');
439        if($fh){
440            $bytes = fread($fh, 256);
441            fclose($fh);
442            if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){
443                return -3;
444            }
445        }
446    }
447    if(substr($mime,0,6) == 'image/'){
448        $info = @getimagesize($file);
449        if($mime == 'image/gif' && $info[2] != 1){
450            return -1;
451        }elseif($mime == 'image/jpeg' && $info[2] != 2){
452            return -1;
453        }elseif($mime == 'image/png' && $info[2] != 3){
454            return -1;
455        }
456        # fixme maybe check other images types as well
457    }elseif(substr($mime,0,5) == 'text/'){
458        global $TEXT;
459        $TEXT = io_readFile($file);
460        if(checkwordblock()){
461            return -2;
462        }
463    }
464    return 0;
465}
466
467/**
468 * Send a notify mail on uploads
469 *
470 * @author Andreas Gohr <andi@splitbrain.org>
471 */
472function media_notify($id,$file,$mime){
473    global $lang;
474    global $conf;
475    global $INFO;
476    if(empty($conf['notify'])) return; //notify enabled?
477
478    $ip = clientIP();
479
480    $text = rawLocale('uploadmail');
481    $text = str_replace('@DATE@',dformat(),$text);
482    $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
483    $text = str_replace('@IPADDRESS@',$ip,$text);
484    $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text);
485    $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
486    $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
487    $text = str_replace('@MIME@',$mime,$text);
488    $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text);
489    $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text);
490
491    if(empty($conf['mailprefix'])) {
492        $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id;
493    } else {
494        $subject = '['.$conf['mailprefix'].'] '.$lang['mail_upload'].' '.$id;
495    }
496
497    mail_send($conf['notify'],$subject,$text,$conf['mailfrom']);
498}
499
500/**
501 * List all files in a given Media namespace
502 */
503function media_filelist($ns,$auth=null,$jump='',$fullscreenview=false){
504    global $conf;
505    global $lang;
506    $ns = cleanID($ns);
507
508    // check auth our self if not given (needed for ajax calls)
509    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
510
511    if (!$fullscreenview) echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
512
513    if($auth < AUTH_READ){
514        // FIXME: print permission warning here instead?
515        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
516    }else{
517        if (!$fullscreenview) media_uploadform($ns, $auth);
518
519        $dir = utf8_encodeFN(str_replace(':','/',$ns));
520        $data = array();
521        search($data,$conf['mediadir'],'search_media',
522                array('showmsg'=>true,'depth'=>1),$dir);
523
524        if(!count($data)){
525            echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
526        }else foreach($data as $item){
527            if (!$fullscreenview) media_printfile($item,$auth,$jump);
528            else if ($fullscreenview == 'thumbs') media_printfile_thumbs($item,$auth,$jump);
529        }
530    }
531    if (!$fullscreenview) media_searchform($ns);
532}
533
534/**
535 * Prints tabs for files list actions
536 *
537 * @author Kate Arzamastseva <pshns@ukr.net>
538 * @param string $selected - opened tab
539 */
540function media_tabs_files($selected=false){
541    global $lang;
542
543    echo '<div class="mediamanager-tabs" id="id-mediamanager-tabs">';
544    $tab = '<a href="'.media_managerURL(array('tab_files' => 'files')).
545        '" rel=".mediamanager-tab-files"';
546    if (!empty($selected) && $selected == 'files') $class = 'files selected';
547    else $class = 'files';
548    $tab .= ' class="'.$class.'" >'.$lang['mediaselect'].'</a>';
549    echo $tab;
550
551    $tab = '<a href="'.media_managerURL(array('tab_files' => 'upload')).
552        '" rel=".mediamanager-tab-upload"';
553    if (!empty($selected) && $selected == 'upload') $class = 'upload selected';
554    else $class = 'upload';
555    $tab .= ' class="'.$class.'" >'.$lang['media_uploadtab'].'</a>';
556    echo $tab;
557
558    $tab = '<a href="'.media_managerURL(array('tab_files' => 'search')).
559        '" rel=".mediamanager-tab-search"';
560    if (!empty($selected) && $selected == 'search') $class = 'search selected';
561    else $class = 'search';
562    $tab .= ' class="'.$class.'" >'.$lang['media_searchtab'].'</a>';
563    echo $tab;
564
565    echo '<div class="clearer"></div>';
566    echo '</div>';
567}
568
569/**
570 * Prints tabs for files details actions
571 *
572 * @author Kate Arzamastseva <pshns@ukr.net>
573 * @param string $selected - opened tab
574 */
575function media_tabs_details($selected=false){
576    global $lang;
577
578    echo '<div class="mediamanager-tabs" id="id-mediamanager-tabs-detail">';
579    $tab = '<a href="'.media_managerURL(array('tab_details' => 'view')).
580        '" rel=".mediamanager-tab-view"';
581    if (!empty($selected) && $selected == 'view') $class = 'view selected';
582    else $class = 'view';
583    $tab .= ' class="'.$class.'" >'.$lang['media_viewtab'].'</a>';
584    echo $tab;
585
586    $tab = '<a href="'.media_managerURL(array('tab_details' => 'edit')).
587        '" rel=".mediamanager-tab-edit"';
588    if (!empty($selected) && $selected == 'edit') $class = 'edit selected';
589    else $class = 'edit';
590    $tab .= ' class="'.$class.'" >'.$lang['media_edittab'].'</a>';
591    echo $tab;
592
593    $tab = '<a href="'.media_managerURL(array('tab_details' => 'history')).
594        '" rel=".mediamanager-tab-history"';
595    if (!empty($selected) && $selected == 'history') $class = 'history selected';
596    else $class = 'history';
597    $tab .= ' class="'.$class.'" >'.$lang['media_historytab'].'</a>';
598    echo $tab;
599
600    echo '<div class="clearer"></div>';
601    echo '</div>';
602}
603
604/**
605 * Prints options for the tab that displays a list of all files
606 *
607 * @author Kate Arzamastseva <pshns@ukr.net>
608 */
609function media_tab_files_options(){
610    global $lang;
611
612    echo '<div class="background-container">';
613    echo '<div id="id-mediamanager-tabs-files" style="display: inline;">';
614    echo '<a href="'.media_managerURL(array('view' => 'thumbs')).'"
615        rel=".mediamanager-files-thumbnails-tab" class="mediamanager-link-thumbnails">'.
616        $lang['media_thumbsview'].'</a>';
617    echo '<a href="'.media_managerURL(array('view' => 'list')).'"
618        rel=".mediamanager-files-list-tab" class="mediamanager-link-list"
619        title="View as list">'.$lang['media_listview'].'</a>';
620
621    echo '</div>';
622    echo '<div class="mediamanager-block-sort">'.$lang['media_sort'];
623    //select
624    echo '</div>';
625    echo '<div class="clearer"></div>';
626    echo '</div>';
627}
628
629/**
630 * Prints tab that displays a list of all files
631 *
632 * @author Kate Arzamastseva <pshns@ukr.net>
633 */
634function media_tab_files($ns,$auth=null,$jump='') {
635    global $lang;
636    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
637
638    echo '<div class="mediamanager-tab-files">';
639    media_tab_files_options();
640    echo '<div class="scroll-container">';
641
642    $view = $_REQUEST['view'];
643    if($auth < AUTH_READ){
644        echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
645    }else{
646        if ($view == 'list') {
647            echo '<ul class="mediamanager-file-list mediamanager-list" id="id-mediamanager-file-list">';
648        } else {
649            echo '<ul class="mediamanager-file-list mediamanager-thumbs" id="id-mediamanager-file-list">';
650        }
651        media_filelist($ns,$auth,$jump,'thumbs');
652        echo '</ul>';
653    }
654    echo '</div>';
655    echo '</div>';
656}
657
658/**
659 * Prints tab that displays uploading form
660 *
661 * @author Kate Arzamastseva <pshns@ukr.net>
662 */
663function media_tab_upload($ns,$auth=null,$jump='') {
664    global $lang;
665    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
666
667    echo '<div class="mediamanager-tab-upload"">';
668    echo '<div class="background-container">';
669    echo $lang['mediaupload'];
670    echo '</div>';
671
672    echo '<div class="scroll-container">';
673    media_uploadform($ns, $auth, true);
674    echo '</div>';
675    echo '</div>';
676}
677
678/**
679 * Prints tab that displays search form
680 *
681 * @author Kate Arzamastseva <pshns@ukr.net>
682 */
683function media_tab_search($ns,$auth=null) {
684    global $lang;
685
686    $do = $_REQUEST['mediado'];
687    $query = $_REQUEST['q'];
688    if (!$query) $query = '';
689
690    echo '<div class="mediamanager-tab-search">';
691    echo '<div class="background-container">';
692    echo $lang['media_search'];
693    echo'</div>';
694
695    echo '<div class="scroll-container">';
696    media_searchform($ns, $query, true);
697
698    if($do == 'searchlist'){
699        media_searchlist($query,$ns,$auth,true);
700    }
701    echo '</div>';
702    echo '</div>';
703}
704
705/**
706 * Prints tab that displays mediafile details
707 *
708 * @author Kate Arzamastseva <pshns@ukr.net>
709 */
710function media_tab_view($image, $ns, $auth=null) {
711    global $lang, $conf;
712    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
713
714    echo '<div class="mediamanager-tab-detail-view">';
715    echo '<div class="background-container">';
716    echo $image;
717    echo '</div>';
718
719    echo '<div class="scroll-container">';
720    $rev = (int) $_REQUEST['rev'];
721    media_preview($image, $auth, $rev);
722    media_details($image, $auth, $rev);
723    echo '</div>';
724    echo '</div>';
725}
726
727/**
728 * Prints tab that displays form for editing mediafile metadata
729 *
730 * @author Kate Arzamastseva <pshns@ukr.net>
731 */
732function media_tab_edit($image, $ns, $auth=null) {
733    global $lang;
734    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
735
736    echo '<div class="mediamanager-tab-detail-edit">';
737    echo '<div class="background-container">';
738    echo $lang['media_edit'];
739    echo '</div>';
740
741    echo '<div class="scroll-container">';
742    if ($image) {
743        list($ext, $mime) = mimetype($image);
744        if ($mime == 'image/jpeg') media_metaform($image,$auth,true);
745    }
746    echo '</div>';
747    echo '</div>';
748}
749
750/**
751 * Prints tab that displays mediafile revisions
752 *
753 * @author Kate Arzamastseva <pshns@ukr.net>
754 */
755function media_tab_history($image, $ns, $auth=null) {
756    global $lang;
757    if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
758    $do = $_REQUEST['mediado'];
759
760    echo '<div class="mediamanager-tab-detail-history">';
761    echo '<div class="background-container">';
762    echo $lang['media_history'];
763    echo '</div>';
764
765    echo '<div class="scroll-container">';
766    if ($auth >= AUTH_READ && $image) {
767        if ($do == 'diff'){
768            media_diff($image, $ns, $auth);
769        } else {
770            $first = isset($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
771            html_revisions($first, $image);
772        }
773    } else {
774        echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
775    }
776    echo '</div>';
777    echo '</div>';
778}
779
780/**
781 * Prints mediafile details
782 *
783 * @author Kate Arzamastseva <pshns@ukr.net>
784 */
785function media_preview($image, $auth, $rev=false) {
786    global $lang;
787    if (!$image) return '';
788    if ($auth < AUTH_READ) {
789        echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
790        return '';
791    }
792    $info = getimagesize(mediaFN($image));
793    $w = (int) $info[0];
794
795    $more = '';
796    if ($rev) $more = "rev=$rev";
797    $src = ml($image, $more);
798    echo '<div class="mediamanager-preview">';
799    echo '<img src="'.$src.'" alt="" width="99%" style="max-width: '.$w.'px;" /><br /><br />';
800
801    $link = ml($image,$more,true,'&');
802
803    $form = new Doku_Form(array('action'=>$link, 'target'=>'_blank'));
804    $form->addElement(form_makeButton('submit','',$lang['mediaview']));
805    $form->printForm();
806
807    // delete button
808    if($auth >= AUTH_DELETE && !$rev){
809        $form = new Doku_Form(array('action'=>media_managerURL(array('delete' => $image))));
810        $form->addElement(form_makeButton('submit','',$lang['btn_delete']));
811        $form->printForm();
812
813        $form = new Doku_Form(array('action'=>media_managerURL()));
814        $form->addHidden('mediado','update');
815        $form->addElement(form_makeButton('submit','',$lang['media_update']));
816        $form->printForm();
817    }
818    echo '</div>';
819}
820
821/**
822 * Prints mediafile tags
823 *
824 * @author Kate Arzamastseva <pshns@ukr.net>
825 */
826function media_details($image, $auth, $rev=false) {
827    global $lang, $config_cascade;;
828
829    if (!$image) return '';
830    if ($auth < AUTH_READ) {
831        echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
832        return '';
833    }
834
835    // load the field descriptions
836    static $fields = null;
837    if(is_null($fields)){
838        $config_files = getConfigFiles('mediameta');
839        foreach ($config_files as $config_file) {
840            if(@file_exists($config_file)) include($config_file);
841        }
842    }
843
844    $src = mediaFN($image, $rev);
845    $meta = new JpegMeta($src);
846    echo '<dl class="img_tags">';
847    foreach($fields as $key => $tag){
848        $t = array();
849        if (!empty($tag[0])) $t = array($tag[0]);
850        if(is_array($tag[3])) $t = array_merge($t,$tag[3]);
851        $value = media_getTag($t, $meta, '-');
852        $value = cleanText($value);
853        echo '<dt>'.$lang[$tag[1]].':</dt><dd>';
854        if ($tag[2] == 'date') echo dformat($value);
855        else echo hsc($value);
856        echo '</dd>';
857    }
858    echo '</dl>';
859}
860
861/**
862 * Returns the requested EXIF/IPTC tag from the image meta
863 *
864 * @author Kate Arzamastseva <pshns@ukr.net>
865 * @param array $tags
866 * @param JpegMeta $meta
867 * @param string $alt
868 * @return string
869 */
870function media_getTag($tags,$meta,$alt=''){
871    if($meta === false) return $alt;
872    $info = $meta->getField($tags);
873    if($info == false) return $alt;
874    return $info;
875}
876
877/**
878 * Shows difference between two revisions of file
879 *
880 * @author Kate Arzamastseva <pshns@ukr.net>
881 */
882function media_diff($image, $ns, $auth) {
883    global $lang;
884    global $conf;
885
886    $rev1 = (int) $_REQUEST['rev'];
887
888    if(is_array($_REQUEST['rev2'])){
889        $rev1 = (int) $_REQUEST['rev2'][0];
890        $rev2 = (int) $_REQUEST['rev2'][1];
891
892        if(!$rev1){
893            $rev1 = $rev2;
894            unset($rev2);
895        }
896    }else{
897        $rev2 = (int) $_REQUEST['rev2'];
898    }
899    if($rev1 && $rev2){            // two specific revisions wanted
900        // make sure order is correct (older on the left)
901        if($rev1 < $rev2){
902            $l_rev = $rev1;
903            $r_rev = $rev2;
904        }else{
905            $l_rev = $rev2;
906            $r_rev = $rev1;
907        }
908    }elseif($rev1){                // single revision given, compare to current
909        $r_rev = '';
910        $l_rev = $rev1;
911    }else{                        // no revision was given, compare previous to current
912        $r_rev = '';
913        $revs = getRevisions($image, 0, 1, 8192, true);
914        $l_rev = $revs[0];
915    }
916    echo '<ul class="mediamanager-table-50"><li><div>';
917    media_preview($image, $auth, $l_rev);
918    echo '</div></li>';
919    echo '<li><div>';
920    media_preview($image, $auth, $r_rev);
921    echo '</div></li><li><div>';
922    media_details($image, $auth, $l_rev);
923    echo '</div></li>';
924    echo '<li><div>';
925    media_details($image, $auth, $r_rev);
926    echo '</div></li></ul>';
927}
928
929/**
930 * List all files found by the search request
931 *
932 * @author Tobias Sarnowski <sarnowski@cosmocode.de>
933 * @author Andreas Gohr <gohr@cosmocode.de>
934 * @author Kate Arzamastseva <pshns@ukr.net>
935 * @triggers MEDIA_SEARCH
936 */
937function media_searchlist($query,$ns,$auth=null,$fullscreen=false){
938    global $conf;
939    global $lang;
940
941    $ns = cleanID($ns);
942
943    if ($query) {
944        $evdata = array(
945                'ns'    => $ns,
946                'data'  => array(),
947                'query' => $query
948                );
949        $evt = new Doku_Event('MEDIA_SEARCH', $evdata);
950        if ($evt->advise_before()) {
951            $dir = utf8_encodeFN(str_replace(':','/',$evdata['ns']));
952            $pattern = '/'.preg_quote($evdata['query'],'/').'/i';
953            search($evdata['data'],
954                    $conf['mediadir'],
955                    'search_media',
956                    array('showmsg'=>false,'pattern'=>$pattern),
957                    $dir);
958        }
959        $evt->advise_after();
960        unset($evt);
961    }
962
963    if (!$fullscreen) {
964        echo '<h1 id="media__ns">'.sprintf($lang['searchmedia_in'],hsc($ns).':*').'</h1>'.NL;
965        media_searchform($ns,$query);
966    }
967
968    if(!count($evdata['data'])){
969        echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
970    }else foreach($evdata['data'] as $item){
971        if (!$fullscreen) media_printfile($item,$item['perm'],'',true);
972        else media_printfile_thumbs($item,$item['perm'],'',true);
973    }
974}
975
976/**
977 * Print action links for a file depending on filetype
978 * and available permissions
979 */
980function media_fileactions($item,$auth){
981    global $lang;
982
983    // view button
984    $link = ml($item['id'],'',true);
985    echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
986        'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>';
987
988    // no further actions if not writable
989    if(!$item['writable']) return;
990
991    // delete button
992    if($auth >= AUTH_DELETE){
993        $link = DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).
994            '&amp;sectok='.getSecurityToken();
995        echo ' <a href="'.$link.'" class="btn_media_delete" title="'.$item['id'].'">'.
996            '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '.
997            'title="'.$lang['btn_delete'].'" class="btn" /></a>';
998    }
999
1000    // edit button
1001    if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){
1002        $link = DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']);
1003        echo ' <a href="'.$link.'">'.
1004            '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '.
1005            'title="'.$lang['metaedit'].'" class="btn" /></a>';
1006    }
1007
1008}
1009
1010/**
1011 * Formats and prints one file in the list
1012 */
1013function media_printfile($item,$auth,$jump,$display_namespace=false){
1014    global $lang;
1015    global $conf;
1016
1017    // Prepare zebra coloring
1018    // I always wanted to use this variable name :-D
1019    static $twibble = 1;
1020    $twibble *= -1;
1021    $zebra = ($twibble == -1) ? 'odd' : 'even';
1022
1023    // Automatically jump to recent action
1024    if($jump == $item['id']) {
1025        $jump = ' id="scroll__here" ';
1026    }else{
1027        $jump = '';
1028    }
1029
1030    // Prepare fileicons
1031    list($ext,$mime,$dl) = mimetype($item['file'],false);
1032    $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
1033    $class = 'select mediafile mf_'.$class;
1034
1035    // Prepare filename
1036    $file = utf8_decodeFN($item['file']);
1037
1038    // Prepare info
1039    $info = '';
1040    if($item['isimg']){
1041        $info .= (int) $item['meta']->getField('File.Width');
1042        $info .= '&#215;';
1043        $info .= (int) $item['meta']->getField('File.Height');
1044        $info .= ' ';
1045    }
1046    $info .= '<i>'.dformat($item['mtime']).'</i>';
1047    $info .= ' ';
1048    $info .= filesize_h($item['size']);
1049
1050    // output
1051    echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
1052    if (!$display_namespace) {
1053        echo '<a name="h_:'.$item['id'].'" class="'.$class.'">'.hsc($file).'</a> ';
1054    } else {
1055        echo '<a name="h_:'.$item['id'].'" class="'.$class.'">'.hsc($item['id']).'</a><br/>';
1056    }
1057    echo '<span class="info">('.$info.')</span>'.NL;
1058    media_fileactions($item,$auth);
1059    echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">';
1060    echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>';
1061    echo '</div>';
1062    if($item['isimg']) media_printimgdetail($item);
1063    echo '<div class="clearer"></div>'.NL;
1064    echo '</div>'.NL;
1065}
1066
1067/**
1068 * Formats and prints one file in the list in the thumbnails view
1069 *
1070 * @author Kate Arzamastseva <pshns@ukr.net>
1071 */
1072function media_printfile_thumbs($item,$auth,$jump){
1073    global $lang;
1074    global $conf;
1075
1076    // Prepare filename
1077    $file = utf8_decodeFN($item['file']);
1078
1079    // output
1080    echo '<li><div>';
1081    if($item['isimg']) {
1082        media_printimgdetail($item, true);
1083    } else {
1084        echo '<a name="d_:'.$item['id'].'" class="image" title="'.$item['id'].'" href="'.
1085            media_managerURL(array('image' => hsc($item['id']))).'">';
1086        echo '<img src="'.DOKU_BASE.'lib/images/icon-file.png" width="90px" />';
1087        echo '</a>';
1088    }
1089    //echo '<input type=checkbox />';
1090    echo '<a href="'.media_managerURL(array('image' => hsc($item['id']))).'" name=
1091        "h_:'.$item['id'].'" class="name">'.hsc($file).'</a>';
1092    if($item['isimg']){
1093        $size = '';
1094        $size .= (int) $item['meta']->getField('File.Width');
1095        $size .= '&#215;';
1096        $size .= (int) $item['meta']->getField('File.Height');
1097        echo '<span class="size">'.$size.'</span>';
1098    } else {
1099        echo '<span class="size">&nbsp;</span>';
1100    }
1101    $date = dformat($item['mtime']);
1102    echo '<span class="date">'.$date.'</span>';
1103    $filesize = filesize_h($item['size']);
1104    echo '<span class="filesize">'.$filesize.'</span>';
1105    echo '<div class="clearer"></div>';
1106    echo '</div></li>'.NL;
1107}
1108
1109/**
1110 * Prints a thumbnail and metainfos
1111 */
1112function media_printimgdetail($item, $fullscreen=false){
1113    // prepare thumbnail
1114    if (!$fullscreen) $size = 120;
1115    else $size = 90;
1116    $w = (int) $item['meta']->getField('File.Width');
1117    $h = (int) $item['meta']->getField('File.Height');
1118    if($w>$size || $h>$size){
1119        if (!$fullscreen) {
1120            $ratio = $item['meta']->getResizeRatio($size);
1121        } else {
1122            $ratio = $item['meta']->getResizeRatio($size,$size);
1123        }
1124        $w = floor($w * $ratio);
1125        $h = floor($h * $ratio);
1126    }
1127    $src = ml($item['id'],array('w'=>$w,'h'=>$h));
1128    $p = array();
1129    $p['width']  = $w;
1130    if (!$fullscreen) $p['height'] = $h;
1131    $p['alt']    = $item['id'];
1132    $p['class']  = 'thumb';
1133    $att = buildAttributes($p);
1134
1135    // output
1136    if ($fullscreen) {
1137        echo '<a name="d_:'.$item['id'].'" class="image" title="'.$item['id'].'" href="'.
1138            media_managerURL(array('image' => hsc($item['id']))).'">';
1139        echo '<img src="'.$src.'" '.$att.' />';
1140        echo '</a>';
1141        return 1;
1142    }
1143
1144    echo '<div class="detail">';
1145    echo '<div class="thumb">';
1146    echo '<a name="d_:'.$item['id'].'" class="select">';
1147    echo '<img src="'.$src.'" '.$att.' />';
1148    echo '</a>';
1149    echo '</div>';
1150
1151    // read EXIF/IPTC data
1152    $t = $item['meta']->getField(array('IPTC.Headline','xmp.dc:title'));
1153    $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
1154                'EXIF.TIFFImageDescription',
1155                'EXIF.TIFFUserComment'));
1156    if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...';
1157    $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject'));
1158
1159    // print EXIF/IPTC data
1160    if($t || $d || $k ){
1161        echo '<p>';
1162        if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />';
1163        if($d) echo htmlspecialchars($d).'<br />';
1164        if($t) echo '<em>'.htmlspecialchars($k).'</em>';
1165        echo '</p>';
1166    }
1167    echo '</div>';
1168}
1169
1170/**
1171 * Build link based on the current, adding/rewriting
1172 * parameters
1173 *
1174 * @author Kate Arzamastseva <pshns@ukr.net>
1175 * @param array $params
1176 * @param string $amp - separator
1177 * @return string - link
1178 */
1179function media_managerURL($params=false, $amp='&') {
1180    global $conf;
1181    global $ID;
1182
1183    $url = $_SERVER['REQUEST_URI'];
1184
1185    $urlArray = explode('?', $url, 2);
1186    $gets = @$urlArray[1];
1187    parse_str($gets, $gets);
1188
1189    if ($gets['edit']) $gets['image'] = $gets['edit'];
1190    unset($gets['edit']);
1191    unset($gets['sectok']);
1192    unset($gets['delete']);
1193    unset($gets['rev']);
1194    unset($gets['mediado']);
1195
1196    if ($params) {
1197        foreach ($params as $k => $v) {
1198            $gets[$k] = $v;
1199        }
1200    }
1201    unset($gets['id']);
1202    if ($gets['delete']) {
1203        unset($gets['image']);
1204        unset($gets['tab_details']);
1205    }
1206
1207    return wl($ID,$gets,false,$amp);
1208}
1209
1210/**
1211 * Print the media upload form if permissions are correct
1212 *
1213 * @author Andreas Gohr <andi@splitbrain.org>
1214 * @author Kate Arzamastseva <pshns@ukr.net>
1215 */
1216function media_uploadform($ns, $auth, $fullscreen = false){
1217    global $lang;
1218
1219    if($auth < AUTH_UPLOAD) {
1220        echo '<div class="nothing">'.$lang['media_perm_upload'].'</div>'.NL;
1221        return;
1222    }
1223
1224    $update = false;
1225    $id = '';
1226    if ($auth >= AUTH_DELETE && $fullscreen && $_REQUEST['mediado'] == 'update') {
1227        $update = true;
1228        $id = cleanID($_REQUEST['image']);
1229    }
1230
1231    // The default HTML upload form
1232    $params = array('id'      => 'dw__upload',
1233                    'enctype' => 'multipart/form-data');
1234    if (!$fullscreen) $params['action'] = DOKU_BASE.'lib/exe/mediamanager.php';
1235    else $params['action'] = media_managerURL(array('tab_files' => 'files'));
1236
1237    $form = new Doku_Form($params);
1238    if (!$fullscreen) $form->addElement('<div class="upload">' . $lang['mediaupload'] . '</div>');
1239    $form->addElement(formSecurityToken());
1240    $form->addHidden('ns', hsc($ns));
1241    $form->addElement(form_makeOpenTag('p'));
1242    $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file'));
1243    $form->addElement(form_makeCloseTag('p'));
1244    $form->addElement(form_makeOpenTag('p'));
1245    $form->addElement(form_makeTextField('id', $id, $lang['txt_filename'].':', 'upload__name'));
1246    $form->addElement(form_makeButton('submit', '', $lang['btn_upload']));
1247    $form->addElement(form_makeCloseTag('p'));
1248
1249    if($auth >= AUTH_DELETE){
1250        $form->addElement(form_makeOpenTag('p'));
1251        $attrs = array();
1252        if ($update) $attrs['checked'] = 'checked';
1253        $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check', $attrs));
1254        $form->addElement(form_makeCloseTag('p'));
1255    }
1256    html_form('upload', $form);
1257
1258    // prepare flashvars for multiupload
1259    $opt = array(
1260            'L_gridname'  => $lang['mu_gridname'] ,
1261            'L_gridsize'  => $lang['mu_gridsize'] ,
1262            'L_gridstat'  => $lang['mu_gridstat'] ,
1263            'L_namespace' => $lang['mu_namespace'] ,
1264            'L_overwrite' => $lang['txt_overwrt'],
1265            'L_browse'    => $lang['mu_browse'],
1266            'L_upload'    => $lang['btn_upload'],
1267            'L_toobig'    => $lang['mu_toobig'],
1268            'L_ready'     => $lang['mu_ready'],
1269            'L_done'      => $lang['mu_done'],
1270            'L_fail'      => $lang['mu_fail'],
1271            'L_authfail'  => $lang['mu_authfail'],
1272            'L_progress'  => $lang['mu_progress'],
1273            'L_filetypes' => $lang['mu_filetypes'],
1274            'L_info'      => $lang['mu_info'],
1275            'L_lasterr'   => $lang['mu_lasterr'],
1276
1277            'O_ns'        => ":$ns",
1278            'O_backend'   => 'mediamanager.php?'.session_name().'='.session_id(),
1279            'O_maxsize'   => php_to_byte(ini_get('upload_max_filesize')),
1280            'O_extensions'=> join('|',array_keys(getMimeTypes())),
1281            'O_overwrite' => ($auth >= AUTH_DELETE),
1282            'O_sectok'    => getSecurityToken(),
1283            'O_authtok'   => auth_createToken(),
1284            );
1285    $var = buildURLparams($opt);
1286    // output the flash uploader
1287    ?>
1288        <div id="dw__flashupload" style="display:none">
1289        <div class="upload"><?php echo $lang['mu_intro']?></div>
1290        <?php echo html_flashobject('multipleUpload.swf','500','190',null,$opt); ?>
1291        </div>
1292        <?php
1293}
1294
1295/**
1296 * Print the search field form
1297 *
1298 * @author Tobias Sarnowski <sarnowski@cosmocode.de>
1299 * @author Kate Arzamastseva <pshns@ukr.net>
1300 */
1301function media_searchform($ns,$query='',$fullscreen=false){
1302    global $lang;
1303
1304    // The default HTML search form
1305    $params = array('id' => 'dw__mediasearch');
1306    if (!$fullscreen) $params['action'] = DOKU_BASE.'lib/exe/mediamanager.php';
1307    else $params['action'] = media_managerURL();
1308    $form = new Doku_Form($params);
1309    if (!$fullscreen) $form->addElement('<div class="upload">' . $lang['mediasearch'] . '</div>');
1310    $form->addElement(formSecurityToken());
1311    $form->addHidden('ns', $ns);
1312    if (!$fullscreen) $form->addHidden('do', 'searchlist');
1313    else $form->addHidden('mediado', 'searchlist');
1314    $form->addElement(form_makeOpenTag('p'));
1315    $form->addElement(form_makeTextField('q', $query,$lang['searchmedia'],'','',array('title'=>sprintf($lang['searchmedia_in'],hsc($ns).':*'))));
1316    $form->addElement(form_makeButton('submit', '', $lang['btn_search']));
1317    $form->addElement(form_makeCloseTag('p'));
1318    html_form('searchmedia', $form);
1319}
1320
1321/**
1322 * Build a tree outline of available media namespaces
1323 *
1324 * @author Andreas Gohr <andi@splitbrain.org>
1325 */
1326function media_nstree($ns){
1327    global $conf;
1328    global $lang;
1329
1330    // currently selected namespace
1331    $ns  = cleanID($ns);
1332    if(empty($ns)){
1333        global $ID;
1334        $ns = dirname(str_replace(':','/',$ID));
1335        if($ns == '.') $ns ='';
1336    }
1337    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
1338
1339    $data = array();
1340    search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true));
1341
1342    // wrap a list with the root level around the other namespaces
1343    $item = array( 'level' => 0, 'id' => '',
1344            'open' =>'true', 'label' => '['.$lang['mediaroot'].']');
1345
1346    echo '<ul class="idx">';
1347    echo media_nstree_li($item);
1348    echo media_nstree_item($item);
1349    echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li');
1350    echo '</li>';
1351    echo '</ul>';
1352}
1353
1354/**
1355 * Userfunction for html_buildlist
1356 *
1357 * Prints a media namespace tree item
1358 *
1359 * @author Andreas Gohr <andi@splitbrain.org>
1360 */
1361function media_nstree_item($item){
1362    $pos   = strrpos($item['id'], ':');
1363    $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
1364    if(!$item['label']) $item['label'] = $label;
1365
1366    $ret  = '';
1367    if (!($_REQUEST['do'] == 'media'))
1368    $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
1369    else $ret .= '<a href="'.media_managerURL(array('ns' => idfilter($item['id']))).'" class="idx_dir">';
1370    $ret .= $item['label'];
1371    $ret .= '</a>';
1372    return $ret;
1373}
1374
1375/**
1376 * Userfunction for html_buildlist
1377 *
1378 * Prints a media namespace tree item opener
1379 *
1380 * @author Andreas Gohr <andi@splitbrain.org>
1381 */
1382function media_nstree_li($item){
1383    $class='media level'.$item['level'];
1384    if($item['open']){
1385        $class .= ' open';
1386        $img   = DOKU_BASE.'lib/images/minus.gif';
1387        $alt   = '&minus;';
1388    }else{
1389        $class .= ' closed';
1390        $img   = DOKU_BASE.'lib/images/plus.gif';
1391        $alt   = '+';
1392    }
1393    // TODO: only deliver an image if it actually has a subtree...
1394    return '<li class="'.$class.'">'.
1395        '<img src="'.$img.'" alt="'.$alt.'" />';
1396}
1397
1398/**
1399 * Resizes the given image to the given size
1400 *
1401 * @author  Andreas Gohr <andi@splitbrain.org>
1402 */
1403function media_resize_image($file, $ext, $w, $h=0){
1404    global $conf;
1405
1406    $info = @getimagesize($file); //get original size
1407    if($info == false) return $file; // that's no image - it's a spaceship!
1408
1409    if(!$h) $h = round(($w * $info[1]) / $info[0]);
1410
1411    // we wont scale up to infinity
1412    if($w > 2000 || $h > 2000) return $file;
1413
1414    //cache
1415    $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
1416    $mtime = @filemtime($local); // 0 if not exists
1417
1418    if( $mtime > filemtime($file) ||
1419            media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
1420            media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
1421        if($conf['fperm']) chmod($local, $conf['fperm']);
1422        return $local;
1423    }
1424    //still here? resizing failed
1425    return $file;
1426}
1427
1428/**
1429 * Crops the given image to the wanted ratio, then calls media_resize_image to scale it
1430 * to the wanted size
1431 *
1432 * Crops are centered horizontally but prefer the upper third of an vertical
1433 * image because most pics are more interesting in that area (rule of thirds)
1434 *
1435 * @author  Andreas Gohr <andi@splitbrain.org>
1436 */
1437function media_crop_image($file, $ext, $w, $h=0){
1438    global $conf;
1439
1440    if(!$h) $h = $w;
1441    $info = @getimagesize($file); //get original size
1442    if($info == false) return $file; // that's no image - it's a spaceship!
1443
1444    // calculate crop size
1445    $fr = $info[0]/$info[1];
1446    $tr = $w/$h;
1447    if($tr >= 1){
1448        if($tr > $fr){
1449            $cw = $info[0];
1450            $ch = (int) $info[0]/$tr;
1451        }else{
1452            $cw = (int) $info[1]*$tr;
1453            $ch = $info[1];
1454        }
1455    }else{
1456        if($tr < $fr){
1457            $cw = (int) $info[1]*$tr;
1458            $ch = $info[1];
1459        }else{
1460            $cw = $info[0];
1461            $ch = (int) $info[0]/$tr;
1462        }
1463    }
1464    // calculate crop offset
1465    $cx = (int) ($info[0]-$cw)/2;
1466    $cy = (int) ($info[1]-$ch)/3;
1467
1468    //cache
1469    $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext);
1470    $mtime = @filemtime($local); // 0 if not exists
1471
1472    if( $mtime > filemtime($file) ||
1473            media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
1474            media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
1475        if($conf['fperm']) chmod($local, $conf['fperm']);
1476        return media_resize_image($local,$ext, $w, $h);
1477    }
1478
1479    //still here? cropping failed
1480    return media_resize_image($file,$ext, $w, $h);
1481}
1482
1483/**
1484 * Download a remote file and return local filename
1485 *
1486 * returns false if download fails. Uses cached file if available and
1487 * wanted
1488 *
1489 * @author  Andreas Gohr <andi@splitbrain.org>
1490 * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
1491 */
1492function media_get_from_URL($url,$ext,$cache){
1493    global $conf;
1494
1495    // if no cache or fetchsize just redirect
1496    if ($cache==0)           return false;
1497    if (!$conf['fetchsize']) return false;
1498
1499    $local = getCacheName(strtolower($url),".media.$ext");
1500    $mtime = @filemtime($local); // 0 if not exists
1501
1502    //decide if download needed:
1503    if( ($mtime == 0) ||                           // cache does not exist
1504            ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
1505      ){
1506        if(media_image_download($url,$local)){
1507            return $local;
1508        }else{
1509            return false;
1510        }
1511    }
1512
1513    //if cache exists use it else
1514    if($mtime) return $local;
1515
1516    //else return false
1517    return false;
1518}
1519
1520/**
1521 * Download image files
1522 *
1523 * @author Andreas Gohr <andi@splitbrain.org>
1524 */
1525function media_image_download($url,$file){
1526    global $conf;
1527    $http = new DokuHTTPClient();
1528    $http->max_bodysize = $conf['fetchsize'];
1529    $http->timeout = 25; //max. 25 sec
1530    $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
1531
1532    $data = $http->get($url);
1533    if(!$data) return false;
1534
1535    $fileexists = @file_exists($file);
1536    $fp = @fopen($file,"w");
1537    if(!$fp) return false;
1538    fwrite($fp,$data);
1539    fclose($fp);
1540    if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
1541
1542    // check if it is really an image
1543    $info = @getimagesize($file);
1544    if(!$info){
1545        @unlink($file);
1546        return false;
1547    }
1548
1549    return true;
1550}
1551
1552/**
1553 * resize images using external ImageMagick convert program
1554 *
1555 * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
1556 * @author Andreas Gohr <andi@splitbrain.org>
1557 */
1558function media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
1559    global $conf;
1560
1561    // check if convert is configured
1562    if(!$conf['im_convert']) return false;
1563
1564    // prepare command
1565    $cmd  = $conf['im_convert'];
1566    $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
1567    if ($ext == 'jpg' || $ext == 'jpeg') {
1568        $cmd .= ' -quality '.$conf['jpg_quality'];
1569    }
1570    $cmd .= " $from $to";
1571
1572    @exec($cmd,$out,$retval);
1573    if ($retval == 0) return true;
1574    return false;
1575}
1576
1577/**
1578 * crop images using external ImageMagick convert program
1579 *
1580 * @author Andreas Gohr <andi@splitbrain.org>
1581 */
1582function media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
1583    global $conf;
1584
1585    // check if convert is configured
1586    if(!$conf['im_convert']) return false;
1587
1588    // prepare command
1589    $cmd  = $conf['im_convert'];
1590    $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y;
1591    if ($ext == 'jpg' || $ext == 'jpeg') {
1592        $cmd .= ' -quality '.$conf['jpg_quality'];
1593    }
1594    $cmd .= " $from $to";
1595
1596    @exec($cmd,$out,$retval);
1597    if ($retval == 0) return true;
1598    return false;
1599}
1600
1601/**
1602 * resize or crop images using PHP's libGD support
1603 *
1604 * @author Andreas Gohr <andi@splitbrain.org>
1605 * @author Sebastian Wienecke <s_wienecke@web.de>
1606 */
1607function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
1608    global $conf;
1609
1610    if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
1611
1612    // check available memory
1613    if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
1614        return false;
1615    }
1616
1617    // create an image of the given filetype
1618    if ($ext == 'jpg' || $ext == 'jpeg'){
1619        if(!function_exists("imagecreatefromjpeg")) return false;
1620        $image = @imagecreatefromjpeg($from);
1621    }elseif($ext == 'png') {
1622        if(!function_exists("imagecreatefrompng")) return false;
1623        $image = @imagecreatefrompng($from);
1624
1625    }elseif($ext == 'gif') {
1626        if(!function_exists("imagecreatefromgif")) return false;
1627        $image = @imagecreatefromgif($from);
1628    }
1629    if(!$image) return false;
1630
1631    if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
1632        $newimg = @imagecreatetruecolor ($to_w, $to_h);
1633    }
1634    if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
1635    if(!$newimg){
1636        imagedestroy($image);
1637        return false;
1638    }
1639
1640    //keep png alpha channel if possible
1641    if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
1642        imagealphablending($newimg, false);
1643        imagesavealpha($newimg,true);
1644    }
1645
1646    //keep gif transparent color if possible
1647    if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
1648        if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
1649            $transcolorindex = @imagecolortransparent($image);
1650            if($transcolorindex >= 0 ) { //transparent color exists
1651                $transcolor = @imagecolorsforindex($image, $transcolorindex);
1652                $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
1653                @imagefill($newimg, 0, 0, $transcolorindex);
1654                @imagecolortransparent($newimg, $transcolorindex);
1655            }else{ //filling with white
1656                $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
1657                @imagefill($newimg, 0, 0, $whitecolorindex);
1658            }
1659        }else{ //filling with white
1660            $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
1661            @imagefill($newimg, 0, 0, $whitecolorindex);
1662        }
1663    }
1664
1665    //try resampling first
1666    if(function_exists("imagecopyresampled")){
1667        if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
1668            imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
1669        }
1670    }else{
1671        imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
1672    }
1673
1674    $okay = false;
1675    if ($ext == 'jpg' || $ext == 'jpeg'){
1676        if(!function_exists('imagejpeg')){
1677            $okay = false;
1678        }else{
1679            $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
1680        }
1681    }elseif($ext == 'png') {
1682        if(!function_exists('imagepng')){
1683            $okay = false;
1684        }else{
1685            $okay =  imagepng($newimg, $to);
1686        }
1687    }elseif($ext == 'gif') {
1688        if(!function_exists('imagegif')){
1689            $okay = false;
1690        }else{
1691            $okay = imagegif($newimg, $to);
1692        }
1693    }
1694
1695    // destroy GD image ressources
1696    if($image) imagedestroy($image);
1697    if($newimg) imagedestroy($newimg);
1698
1699    return $okay;
1700}
1701
1702/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
1703