3 * All output and handler function needed for the media management popup
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 if(!defined('DOKU_INC')) die('meh.');
10 if(!defined('NL')) define('NL',"\n");
13 * Lists pages which currently use a media file selected for deletion
15 * References uses the same visual as search results and share
16 * their CSS tags except pagenames won't be links.
18 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
20 function media_filesinuse($data,$id){
22 echo '<h1>'.$lang['reference'].' <code>'.hsc(noNS($id)).'</code></h1>';
23 echo '<p>'.hsc($lang['ref_inuse']).'</p>';
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>';
35 print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
40 * Handles the saving of image meta data
42 * @author Andreas Gohr <andi@splitbrain.org>
43 * @author Kate Arzamastseva <pshns@ukr.net>
45 function media_metasave($id,$auth,$data){
46 if($auth < AUTH_UPLOAD) return false;
47 if(!checkSecurityToken()) return false;
52 $meta = new JpegMeta($src);
55 foreach($data as $key => $val){
58 $meta->deleteField($key);
60 $meta->setField($key,$val);
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);
71 if($conf['fperm']) chmod($src, $conf['fperm']);
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']);
77 msg($lang['metasaveok'],1);
80 msg($lang['metasaveerr'],-1);
86 * Display the form to edit image meta data
88 * @author Andreas Gohr <andi@splitbrain.org>
89 * @author Kate Arzamastseva <pshns@ukr.net>
91 function media_metaform($id,$auth){
92 global $lang, $config_cascade;
94 if($auth < AUTH_UPLOAD) {
95 echo '<div class="nothing">'.$lang['media_perm_upload'].'</div>'.NL;
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);
111 $form = new Doku_Form(array('action' => media_managerURL(array('tab_details' => 'view'), '&'),
113 $form->addHidden('img', $id);
114 $form->addHidden('mediado', 'save');
115 foreach($fields as $key => $field){
117 if (empty($field[0])) continue;
118 $tags = array($field[0]);
119 if(is_array($field[3])) $tags = array_merge($tags,$field[3]);
120 $value = tpl_img_getTag($tags,'',$src);
121 $value = cleanText($value);
123 // prepare attributes
125 $p['class'] = 'edit';
126 $p['id'] = 'meta__'.$key;
127 $p['name'] = 'meta['.$field[0].']';
128 $p_attrs = array('class' => 'edit');
130 $form->addElement('<div class="row">');
131 if($field[2] == 'text'){
132 $form->addElement(form_makeField('text', $p['name'], $value, ($lang[$field[1]]) ? $lang[$field[1]] : $field[1] . ':', $p['id'], $p['class'], $p_attrs));
134 $att = buildAttributes($p);
135 $form->addElement('<label for="meta__'.$key.'">'.$lang[$field[1]].'</label>');
136 $form->addElement("<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>');
138 $form->addElement('</div>'.NL);
140 $form->addElement('<div class="buttons">');
141 $form->addElement(form_makeButton('submit', '', $lang['btn_save'], array('accesskey' => 's', 'name' => 'mediado[save]')));
142 $form->addElement('</div>'.NL);
147 * Convenience function to check if a media file is still in use
149 * @author Michael Klier <chi@chimeric.de>
151 function media_inuse($id) {
153 $mediareferences = array();
154 if($conf['refcheck']){
155 $mediareferences = ft_mediause($id,$conf['refshow']);
156 if(!count($mediareferences)) {
159 return $mediareferences;
166 define('DOKU_MEDIA_DELETED', 1);
167 define('DOKU_MEDIA_NOT_AUTH', 2);
168 define('DOKU_MEDIA_INUSE', 4);
169 define('DOKU_MEDIA_EMPTY_NS', 8);
172 * Handles media file deletions
174 * If configured, checks for media references before deletion
176 * @author Andreas Gohr <andi@splitbrain.org>
177 * @return int One of: 0,
179 DOKU_MEDIA_DELETED | DOKU_MEDIA_EMPTY_NS,
183 function media_delete($id,$auth){
185 if($auth < AUTH_DELETE) return DOKU_MEDIA_NOT_AUTH;
186 if(media_inuse($id)) return DOKU_MEDIA_INUSE;
188 $file = mediaFN($id);
190 // trigger an event - MEDIA_DELETE_FILE
192 $data['name'] = basename($file);
193 $data['path'] = $file;
194 $data['size'] = (@file_exists($file)) ? filesize($file) : 0;
196 $data['unl'] = false;
197 $data['del'] = false;
198 $evt = new Doku_Event('MEDIA_DELETE_FILE',$data);
199 if ($evt->advise_before()) {
200 $old = @filemtime($file);
201 if(!@file_exists(mediaFN($id, $old)) && @file_exists($file)) {
202 // add old revision to the attic
203 media_saveOldRevision($id);
206 $data['unl'] = @unlink($file);
208 addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_DELETE, $lang['deleted']);
209 $data['del'] = io_sweepNS($id,'mediadir');
212 $evt->advise_after();
215 if($data['unl'] && $data['del']){
216 return DOKU_MEDIA_DELETED | DOKU_MEDIA_EMPTY_NS;
219 return $data['unl'] ? DOKU_MEDIA_DELETED : 0;
223 * Handle file uploads via XMLHttpRequest
225 * @return mixed false on error, id of the new file on success
227 function media_upload_xhr($ns,$auth){
228 if(!checkSecurityToken()) return false;
230 $id = $_GET['qqfile'];
231 list($ext,$mime,$dl) = mimetype($id);
232 $input = fopen("php://input", "r");
233 if (!($tmp = io_mktmpdir())) return false;
234 $path = $tmp.'/'.md5($id);
235 $target = fopen($path, "w");
236 $realSize = stream_copy_to_stream($input, $target);
239 if ($realSize != (int)$_SERVER["CONTENT_LENGTH"]){
246 array('name' => $path,
250 (($_REQUEST['ow'] == 'checked') ? true : false),
255 if ($tmp) dir_delete($tmp);
256 if (is_array($res)) {
257 msg($res[0], $res[1]);
264 * Handles media file uploads
266 * @author Andreas Gohr <andi@splitbrain.org>
267 * @author Michael Klier <chi@chimeric.de>
268 * @return mixed false on error, id of the new file on success
270 function media_upload($ns,$auth,$file=false){
271 if(!checkSecurityToken()) return false;
275 $id = $_POST['mediaid'];
276 if (!$file) $file = $_FILES['upload'];
277 if(empty($id)) $id = $file['name'];
279 // check for errors (messages are done in lib/exe/mediamanager.php)
280 if($file['error']) return false;
283 list($fext,$fmime,$dl) = mimetype($file['name']);
284 list($iext,$imime,$dl) = mimetype($id);
286 // no extension specified in id - read original one
289 }elseif($fext && $fext != $iext){
290 // extension was changed, print warning
291 msg(sprintf($lang['mediaextchange'],$fext,$iext));
294 $res = media_save(array('name' => $file['tmp_name'],
296 'ext' => $iext), $ns.':'.$id,
297 $_REQUEST['ow'], $auth, 'move_uploaded_file');
298 if (is_array($res)) {
299 msg($res[0], $res[1]);
306 * This generates an action event and delegates to _media_upload_action().
307 * Action plugins are allowed to pre/postprocess the uploaded file.
308 * (The triggered event is preventable.)
311 * $data[0] fn_tmp: the temporary file name (read from $_FILES)
312 * $data[1] fn: the file name of the uploaded file
313 * $data[2] id: the future directory id of the uploaded file
314 * $data[3] imime: the mimetype of the uploaded file
315 * $data[4] overwrite: if an existing file is going to be overwritten
317 * @triggers MEDIA_UPLOAD_FINISH
319 function media_save($file, $id, $ow, $auth, $move) {
320 if($auth < AUTH_UPLOAD) {
321 return array("You don't have permissions to upload files.", -1);
324 if (!isset($file['mime']) || !isset($file['ext'])) {
325 list($ext, $mime) = mimetype($id);
326 if (!isset($file['mime'])) {
327 $file['mime'] = $mime;
329 if (!isset($file['ext'])) {
340 // get filetype regexp
341 $types = array_keys(getMimeTypes());
342 $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
343 $regex = join('|',$types);
345 // because a temp file was created already
346 if(!preg_match('/\.('.$regex.')$/i',$fn)) {
347 return array($lang['uploadwrong'],-1);
350 //check for overwrite
351 $overwrite = @file_exists($fn);
352 $auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
353 if($overwrite && (!$ow || $auth < $auth_ow)) {
354 return array($lang['uploadexist'], 0);
356 // check for valid content
357 $ok = media_contentcheck($file['name'], $file['mime']);
359 return array(sprintf($lang['uploadbadcontent'],'.' . $file['ext']),-1);
361 return array($lang['uploadspam'],-1);
363 return array($lang['uploadxss'],-1);
366 // prepare event data
367 $data[0] = $file['name'];
370 $data[3] = $file['mime'];
371 $data[4] = $overwrite;
375 return trigger_event('MEDIA_UPLOAD_FINISH', $data, '_media_upload_action', true);
379 * Callback adapter for media_upload_finish()
380 * @author Michael Klier <chi@chimeric.de>
382 function _media_upload_action($data) {
383 // fixme do further sanity tests of given data?
384 if(is_array($data) && count($data)===6) {
385 return media_upload_finish($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]);
387 return false; //callback error
392 * Saves an uploaded media file
394 * @author Andreas Gohr <andi@splitbrain.org>
395 * @author Michael Klier <chi@chimeric.de>
396 * @author Kate Arzamastseva <pshns@ukr.net>
398 function media_upload_finish($fn_tmp, $fn, $id, $imime, $overwrite, $move = 'move_uploaded_file') {
403 $old = @filemtime($fn);
404 if(!@file_exists(mediaFN($id, $old)) && @file_exists($fn)) {
405 // add old revision to the attic if missing
406 media_saveOldRevision($id);
410 io_createNamespace($id, 'media');
412 if($move($fn_tmp, $fn)) {
413 @clearstatcache(true,$fn);
414 $new = @filemtime($fn);
415 // Set the correct permission here.
416 // Always chmod media because they may be saved with different permissions than expected from the php umask.
417 // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
418 chmod($fn, $conf['fmode']);
419 msg($lang['uploadsucc'],1);
420 media_notify($id,$fn,$imime,$old);
421 // add a log entry to the media changelog
423 addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_REVERT, $lang['restored'], $REV);
424 } elseif ($overwrite) {
425 addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_EDIT);
427 addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_CREATE, $lang['created']);
431 return array($lang['uploadfail'],-1);
436 * Moves the current version of media file to the media_attic
439 * @author Kate Arzamastseva <pshns@ukr.net>
441 * @return int - revision date
443 function media_saveOldRevision($id){
446 $oldf = mediaFN($id);
447 if(!@file_exists($oldf)) return '';
448 $date = filemtime($oldf);
449 if (!$conf['mediarevisions']) return $date;
451 if (!getRevisionInfo($id, $date, 8192, true)) {
452 // there was an external edit,
453 // there is no log entry for current version of file
454 if (!@file_exists(mediaMetaFN($id,'.changes'))) {
455 addMediaLogEntry($date, $id, DOKU_CHANGE_TYPE_CREATE, $lang['created']);
457 addMediaLogEntry($date, $id, DOKU_CHANGE_TYPE_EDIT);
461 $newf = mediaFN($id,$date);
462 io_makeFileDir($newf);
463 if(copy($oldf, $newf)) {
464 // Set the correct permission here.
465 // Always chmod media because they may be saved with different permissions than expected from the php umask.
466 // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
467 chmod($newf, $conf['fmode']);
473 * This function checks if the uploaded content is really what the
474 * mimetype says it is. We also do spam checking for text types here.
476 * We need to do this stuff because we can not rely on the browser
477 * to do this check correctly. Yes, IE is broken as usual.
479 * @author Andreas Gohr <andi@splitbrain.org>
480 * @link http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting
481 * @fixme check all 26 magic IE filetypes here?
483 function media_contentcheck($file,$mime){
485 if($conf['iexssprotect']){
486 $fh = @fopen($file, 'rb');
488 $bytes = fread($fh, 256);
490 if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){
495 if(substr($mime,0,6) == 'image/'){
496 $info = @getimagesize($file);
497 if($mime == 'image/gif' && $info[2] != 1){
499 }elseif($mime == 'image/jpeg' && $info[2] != 2){
501 }elseif($mime == 'image/png' && $info[2] != 3){
504 # fixme maybe check other images types as well
505 }elseif(substr($mime,0,5) == 'text/'){
507 $TEXT = io_readFile($file);
508 if(checkwordblock()){
516 * Send a notify mail on uploads
518 * @author Andreas Gohr <andi@splitbrain.org>
520 function media_notify($id,$file,$mime,$old_rev=false){
524 if(empty($conf['notify'])) return; //notify enabled?
528 $text = rawLocale('uploadmail');
529 $text = str_replace('@DATE@',dformat(),$text);
530 $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
531 $text = str_replace('@IPADDRESS@',$ip,$text);
532 $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text);
533 $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
534 $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
535 $text = str_replace('@MIME@',$mime,$text);
536 $text = str_replace('@MEDIA@',ml($id,'',true,'&',true),$text);
537 $text = str_replace('@SIZE@',filesize_h(filesize($file)),$text);
538 if ($old_rev && $conf['mediarevisions']) {
539 $text = str_replace('@OLD@', ml($id, "rev=$old_rev", true, '&', true), $text);
541 $text = str_replace('@OLD@', '', $text);
544 if(empty($conf['mailprefix'])) {
545 $subject = '['.$conf['title'].'] '.$lang['mail_upload'].' '.$id;
547 $subject = '['.$conf['mailprefix'].'] '.$lang['mail_upload'].' '.$id;
550 mail_send($conf['notify'],$subject,$text,$conf['mailfrom']);
554 * List all files in a given Media namespace
556 function media_filelist($ns,$auth=null,$jump='',$fullscreenview=false,$sort=false){
561 // check auth our self if not given (needed for ajax calls)
562 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
564 if (!$fullscreenview) echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
566 if($auth < AUTH_READ){
567 // FIXME: print permission warning here instead?
568 echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
570 if (!$fullscreenview) media_uploadform($ns, $auth);
572 $dir = utf8_encodeFN(str_replace(':','/',$ns));
574 search($data,$conf['mediadir'],'search_media',
575 array('showmsg'=>true,'depth'=>1),$dir,1,$sort);
578 echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
580 if ($fullscreenview) {
581 echo '<ul class="' . _media_get_list_type() . '">';
583 foreach($data as $item){
584 if (!$fullscreenview) {
585 media_printfile($item,$auth,$jump);
587 media_printfile_thumbs($item,$auth,$jump);
590 if ($fullscreenview) echo '</ul>'.NL;
593 if (!$fullscreenview) media_searchform($ns);
597 * Prints tabs for files list actions
599 * @author Kate Arzamastseva <pshns@ukr.net>
600 * @author Adrian Lang <mail@adrianlang.de>
602 * @param string $selected_tab - opened tab
605 function media_tabs_files($selected_tab = ''){
608 foreach(array('files' => 'mediaselect',
609 'upload' => 'media_uploadtab',
610 'search' => 'media_searchtab') as $tab => $caption) {
611 $tabs[$tab] = array('href' => media_managerURL(array('tab_files' => $tab), '&'),
612 'caption' => $lang[$caption]);
615 html_tabs($tabs, $selected_tab);
619 * Prints tabs for files details actions
621 * @author Kate Arzamastseva <pshns@ukr.net>
622 * @param string $selected_tab - opened tab
624 function media_tabs_details($image, $selected_tab = ''){
628 $tabs['view'] = array('href' => media_managerURL(array('tab_details' => 'view'), '&'),
629 'caption' => $lang['media_viewtab']);
631 list($ext, $mime) = mimetype($image);
632 if ($mime == 'image/jpeg' && @file_exists(mediaFN($image))) {
633 $tabs['edit'] = array('href' => media_managerURL(array('tab_details' => 'edit'), '&'),
634 'caption' => $lang['media_edittab']);
636 if ($conf['mediarevisions']) {
637 $tabs['history'] = array('href' => media_managerURL(array('tab_details' => 'history'), '&'),
638 'caption' => $lang['media_historytab']);
641 html_tabs($tabs, $selected_tab);
645 * Prints options for the tab that displays a list of all files
647 * @author Kate Arzamastseva <pshns@ukr.net>
649 function media_tab_files_options(){
651 $form = new Doku_Form(array('class' => 'options', 'method' => 'get',
652 'action' => wl($ID)));
653 $media_manager_params = media_managerURL(array(), '', false, true);
654 foreach($media_manager_params as $pKey => $pVal){
655 $form->addHidden($pKey, $pVal);
657 $form->addHidden('sectok', null);
658 if (isset($_REQUEST['q'])) {
659 $form->addHidden('q', $_REQUEST['q']);
661 $form->addElement('<ul>'.NL);
662 foreach(array('list' => array('listType', array('thumbs', 'rows')),
663 'sort' => array('sortBy', array('name', 'date')))
664 as $group => $content) {
665 $checked = "_media_get_${group}_type";
666 $checked = $checked();
668 $form->addElement('<li class="' . $content[0] . '">');
669 foreach($content[1] as $option) {
671 if ($checked == $option) {
672 $attrs['checked'] = 'checked';
674 $form->addElement(form_makeRadioField($group, $option,
675 $lang['media_' . $group . '_' . $option],
676 $content[0] . '__' . $option,
679 $form->addElement('</li>'.NL);
681 $form->addElement('<li>');
682 $form->addElement(form_makeButton('submit', '', $lang['btn_apply']));
683 $form->addElement('</li>'.NL);
684 $form->addElement('</ul>'.NL);
689 * Returns type of sorting for the list of files in media manager
691 * @author Kate Arzamastseva <pshns@ukr.net>
692 * @return string - sort type
694 function _media_get_sort_type() {
695 return _media_get_display_param('sort', array('default' => 'name', 'date'));
698 function _media_get_list_type() {
699 return _media_get_display_param('list', array('default' => 'thumbs', 'rows'));
702 function _media_get_display_param($param, $values) {
703 if (isset($_REQUEST[$param]) && in_array($_REQUEST[$param], $values)) {
705 return $_REQUEST[$param];
707 $val = get_doku_pref($param, $values['default']);
708 if (!in_array($val, $values)) {
709 $val = $values['default'];
716 * Prints tab that displays a list of all files
718 * @author Kate Arzamastseva <pshns@ukr.net>
720 function media_tab_files($ns,$auth=null,$jump='') {
722 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
724 if($auth < AUTH_READ){
725 echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
727 media_filelist($ns,$auth,$jump,true,_media_get_sort_type());
732 * Prints tab that displays uploading form
734 * @author Kate Arzamastseva <pshns@ukr.net>
736 function media_tab_upload($ns,$auth=null,$jump='') {
738 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
740 echo '<div class="upload">'.NL;
741 if ($auth >= AUTH_UPLOAD) {
742 echo '<p>' . $lang['mediaupload'] . '</p>';
744 media_uploadform($ns, $auth, true);
749 * Prints tab that displays search form
751 * @author Kate Arzamastseva <pshns@ukr.net>
753 function media_tab_search($ns,$auth=null) {
756 $do = $_REQUEST['mediado'];
757 $query = $_REQUEST['q'];
758 if (!$query) $query = '';
759 echo '<div class="search">'.NL;
761 media_searchform($ns, $query, true);
762 if ($do == 'searchlist' || $query) {
763 media_searchlist($query,$ns,$auth,true,_media_get_sort_type());
769 * Prints tab that displays mediafile details
771 * @author Kate Arzamastseva <pshns@ukr.net>
773 function media_tab_view($image, $ns, $auth=null, $rev=false) {
775 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
777 if ($image && $auth >= AUTH_READ) {
778 $meta = new JpegMeta(mediaFN($image, $rev));
779 media_preview($image, $auth, $rev, $meta);
780 media_preview_buttons($image, $auth, $rev);
781 media_details($image, $auth, $rev, $meta);
784 echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
789 * Prints tab that displays form for editing mediafile metadata
791 * @author Kate Arzamastseva <pshns@ukr.net>
793 function media_tab_edit($image, $ns, $auth=null) {
795 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
798 list($ext, $mime) = mimetype($image);
799 if ($mime == 'image/jpeg') media_metaform($image,$auth);
804 * Prints tab that displays mediafile revisions
806 * @author Kate Arzamastseva <pshns@ukr.net>
808 function media_tab_history($image, $ns, $auth=null) {
810 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
811 $do = $_REQUEST['mediado'];
813 if ($auth >= AUTH_READ && $image) {
815 media_diff($image, $ns, $auth);
817 $first = isset($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
818 html_revisions($first, $image);
821 echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
826 * Prints mediafile details
828 * @author Kate Arzamastseva <pshns@ukr.net>
830 function media_preview($image, $auth, $rev=false, $meta=false) {
832 $size = media_image_preview_size($image, $rev, $meta);
836 echo '<div class="image">';
842 $t = @filemtime(mediaFN($image));
846 $more['w'] = $size[0];
847 $more['h'] = $size[1];
848 $src = ml($image, $more);
850 echo '<a href="'.$src.'" target="_blank" title="'.$lang['mediaview'].'">';
851 echo '<img src="'.$src.'" alt="" style="max-width: '.$size[0].'px;" />';
859 * Prints mediafile action buttons
861 * @author Kate Arzamastseva <pshns@ukr.net>
863 function media_preview_buttons($image, $auth, $rev=false) {
866 echo '<ul class="actions">'.NL;
868 if($auth >= AUTH_DELETE && !$rev && @file_exists(mediaFN($image))){
871 $form = new Doku_Form(array('id' => 'mediamanager__btn_delete',
872 'action'=>media_managerURL(array('delete' => $image), '&')));
873 $form->addElement(form_makeButton('submit','',$lang['btn_delete']));
879 $auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
880 if($auth >= $auth_ow && !$rev){
882 // upload new version button
883 $form = new Doku_Form(array('id' => 'mediamanager__btn_update',
884 'action'=>media_managerURL(array('image' => $image, 'mediado' => 'update'), '&')));
885 $form->addElement(form_makeButton('submit','',$lang['media_update']));
891 if($auth >= AUTH_UPLOAD && $rev && $conf['mediarevisions'] && @file_exists(mediaFN($image, $rev))){
894 $form = new Doku_Form(array('id' => 'mediamanager__btn_restore',
895 'action'=>media_managerURL(array('image' => $image), '&')));
896 $form->addHidden('mediado','restore');
897 $form->addHidden('rev',$rev);
898 $form->addElement(form_makeButton('submit','',$lang['media_restore']));
908 * Returns image width and height for mediamanager preview panel
910 * @author Kate Arzamastseva <pshns@ukr.net>
911 * @param string $image
913 * @param JpegMeta $meta
916 function media_image_preview_size($image, $rev, $meta, $size = 500) {
917 if (!preg_match("/\.(jpe?g|gif|png)$/", $image) || !file_exists(mediaFN($image, $rev))) return false;
919 $info = getimagesize(mediaFN($image, $rev));
923 if($meta && ($w > $size || $h > $size)){
924 $ratio = $meta->getResizeRatio($size, $size);
925 $w = floor($w * $ratio);
926 $h = floor($h * $ratio);
928 return array($w, $h);
932 * Returns the requested EXIF/IPTC tag from the image meta
934 * @author Kate Arzamastseva <pshns@ukr.net>
936 * @param JpegMeta $meta
940 function media_getTag($tags,$meta,$alt=''){
941 if($meta === false) return $alt;
942 $info = $meta->getField($tags);
943 if($info == false) return $alt;
948 * Returns mediafile tags
950 * @author Kate Arzamastseva <pshns@ukr.net>
951 * @param JpegMeta $meta
954 function media_file_tags($meta) {
955 global $config_cascade;
957 // load the field descriptions
958 static $fields = null;
959 if(is_null($fields)){
960 $config_files = getConfigFiles('mediameta');
961 foreach ($config_files as $config_file) {
962 if(@file_exists($config_file)) include($config_file);
968 foreach($fields as $key => $tag){
970 if (!empty($tag[0])) $t = array($tag[0]);
971 if(is_array($tag[3])) $t = array_merge($t,$tag[3]);
972 $value = media_getTag($t, $meta);
973 $tags[] = array('tag' => $tag, 'value' => $value);
980 * Prints mediafile tags
982 * @author Kate Arzamastseva <pshns@ukr.net>
984 function media_details($image, $auth, $rev=false, $meta=false) {
987 if (!$meta) $meta = new JpegMeta(mediaFN($image, $rev));
988 $tags = media_file_tags($meta);
991 foreach($tags as $tag){
993 $value = cleanText($tag['value']);
994 echo '<dt>'.$lang[$tag['tag'][1]].':</dt><dd>';
995 if ($tag['tag'][2] == 'date') echo dformat($value);
996 else echo hsc($value);
1004 * Shows difference between two revisions of file
1006 * @author Kate Arzamastseva <pshns@ukr.net>
1008 function media_diff($image, $ns, $auth, $fromajax = false) {
1012 if ($auth < AUTH_READ || !$image || !$conf['mediarevisions']) return '';
1014 $rev1 = (int) $_REQUEST['rev'];
1016 if(is_array($_REQUEST['rev2'])){
1017 $rev1 = (int) $_REQUEST['rev2'][0];
1018 $rev2 = (int) $_REQUEST['rev2'][1];
1025 $rev2 = (int) $_REQUEST['rev2'];
1028 if ($rev1 && !file_exists(mediaFN($image, $rev1))) $rev1 = false;
1029 if ($rev2 && !file_exists(mediaFN($image, $rev2))) $rev2 = false;
1031 if($rev1 && $rev2){ // two specific revisions wanted
1032 // make sure order is correct (older on the left)
1040 }elseif($rev1){ // single revision given, compare to current
1043 }else{ // no revision was given, compare previous to current
1045 $revs = getRevisions($image, 0, 1, 8192, true);
1046 if (file_exists(mediaFN($image, $revs[0]))) {
1053 // prepare event data
1059 $data[5] = $fromajax;
1062 return trigger_event('MEDIA_DIFF', $data, '_media_file_diff', true);
1066 function _media_file_diff($data) {
1067 if(is_array($data) && count($data)===6) {
1068 return media_file_diff($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]);
1075 * Shows difference between two revisions of image
1077 * @author Kate Arzamastseva <pshns@ukr.net>
1079 function media_file_diff($image, $l_rev, $r_rev, $ns, $auth, $fromajax){
1080 global $lang, $config_cascade;
1082 $l_meta = new JpegMeta(mediaFN($image, $l_rev));
1083 $r_meta = new JpegMeta(mediaFN($image, $r_rev));
1085 $is_img = preg_match("/\.(jpe?g|gif|png)$/", $image);
1087 $l_size = media_image_preview_size($image, $l_rev, $l_meta);
1088 $r_size = media_image_preview_size($image, $r_rev, $r_meta);
1089 $is_img = ($l_size && $r_size && ($l_size[0] >= 30 || $r_size[0] >= 30));
1091 $difftype = $_REQUEST['difftype'];
1094 $form = new Doku_Form(array(
1095 'action' => media_managerURL(array(), '&'),
1097 'id' => 'mediamanager__form_diffview',
1098 'class' => 'diffView'
1100 $form->addHidden('sectok', null);
1101 $form->addElement('<input type="hidden" name="rev2[]" value="'.$l_rev.'" ></input>');
1102 $form->addElement('<input type="hidden" name="rev2[]" value="'.$r_rev.'" ></input>');
1103 $form->addHidden('mediado', 'diff');
1106 echo NL.'<div id="mediamanager__diff" >'.NL;
1109 if ($difftype == 'opacity' || $difftype == 'portions') {
1110 media_image_diff($image, $l_rev, $r_rev, $l_size, $r_size, $difftype);
1111 if (!$fromajax) echo '</div>';
1116 list($l_head, $r_head) = html_diff_head($l_rev, $r_rev, $image, true);
1121 <th><?php echo $l_head; ?></th>
1122 <th><?php echo $r_head; ?></th>
1126 echo '<tr class="image">';
1128 media_preview($image, $auth, $l_rev, $l_meta);
1132 media_preview($image, $auth, $r_rev, $r_meta);
1136 echo '<tr class="actions">';
1138 media_preview_buttons($image, $auth, $l_rev);
1142 media_preview_buttons($image, $auth, $r_rev);
1146 $l_tags = media_file_tags($l_meta);
1147 $r_tags = media_file_tags($r_meta);
1148 // FIXME r_tags-only stuff
1149 foreach ($l_tags as $key => $l_tag) {
1150 if ($l_tag['value'] != $r_tags[$key]['value']) {
1151 $r_tags[$key]['highlighted'] = true;
1152 $l_tags[$key]['highlighted'] = true;
1153 } else if (!$l_tag['value'] || !$r_tags[$key]['value']) {
1154 unset($r_tags[$key]);
1155 unset($l_tags[$key]);
1160 foreach(array($l_tags,$r_tags) as $tags){
1163 echo '<dl class="img_tags">';
1164 foreach($tags as $tag){
1165 $value = cleanText($tag['value']);
1166 if (!$value) $value = '-';
1167 echo '<dt>'.$lang[$tag['tag'][1]].':</dt>';
1169 if ($tag['highlighted']) {
1172 if ($tag['tag'][2] == 'date') echo dformat($value);
1173 else echo hsc($value);
1174 if ($tag['highlighted']) {
1187 if ($is_img && !$fromajax) echo '</div>';
1191 * Prints two images side by side
1194 * @author Kate Arzamastseva <pshns@ukr.net>
1195 * @param string $image
1198 * @param array $l_size
1199 * @param array $r_size
1200 * @param string $type
1202 function media_image_diff($image, $l_rev, $r_rev, $l_size, $r_size, $type) {
1203 if ($l_size != $r_size) {
1204 if ($r_size[0] > $l_size[0]) {
1209 $l_more = array('rev' => $l_rev, 'h' => $l_size[1], 'w' => $l_size[0]);
1210 $r_more = array('rev' => $r_rev, 'h' => $l_size[1], 'w' => $l_size[0]);
1212 $l_src = ml($image, $l_more);
1213 $r_src = ml($image, $r_more);
1216 echo '<div class="slider" style="max-width: '.($l_size[0]-20).'px;" ></div>'.NL;
1218 // two images in divs
1219 echo '<div class="imageDiff ' . $type . '">'.NL;
1220 echo '<div class="image1" style="max-width: '.$l_size[0].'px;">';
1221 echo '<img src="'.$l_src.'" alt="" />';
1223 echo '<div class="image2" style="max-width: '.$l_size[0].'px;">';
1224 echo '<img src="'.$r_src.'" alt="" />';
1230 * Restores an old revision of a media file
1232 * @param string $image
1235 * @return string - file's id
1236 * @author Kate Arzamastseva <pshns@ukr.net>
1238 function media_restore($image, $rev, $auth){
1240 if ($auth < AUTH_UPLOAD || !$conf['mediarevisions']) return false;
1241 $removed = (!file_exists(mediaFN($image)) && file_exists(mediaMetaFN($image, '.changes')));
1242 if (!$image || (!file_exists(mediaFN($image)) && !$removed)) return false;
1243 if (!$rev || !file_exists(mediaFN($image, $rev))) return false;
1244 list($iext,$imime,$dl) = mimetype($image);
1245 $res = media_upload_finish(mediaFN($image, $rev),
1251 if (is_array($res)) {
1252 msg($res[0], $res[1]);
1259 * List all files found by the search request
1261 * @author Tobias Sarnowski <sarnowski@cosmocode.de>
1262 * @author Andreas Gohr <gohr@cosmocode.de>
1263 * @author Kate Arzamastseva <pshns@ukr.net>
1264 * @triggers MEDIA_SEARCH
1266 function media_searchlist($query,$ns,$auth=null,$fullscreen=false,$sort=''){
1278 $evt = new Doku_Event('MEDIA_SEARCH', $evdata);
1279 if ($evt->advise_before()) {
1280 $dir = utf8_encodeFN(str_replace(':','/',$evdata['ns']));
1281 $pattern = '/'.preg_quote($evdata['query'],'/').'/i';
1282 search($evdata['data'],
1285 array('showmsg'=>false,'pattern'=>$pattern),
1290 foreach ($evdata['data'] as $k => $v) {
1291 $data[$k] = ($sort == 'date') ? $v['mtime'] : $v['id'];
1293 array_multisort($data, SORT_DESC, SORT_NUMERIC, $evdata['data']);
1295 $evt->advise_after();
1300 echo '<h1 id="media__ns">'.sprintf($lang['searchmedia_in'],hsc($ns).':*').'</h1>'.NL;
1301 media_searchform($ns,$query);
1304 if(!count($evdata['data'])){
1305 echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
1308 echo '<ul class="' . _media_get_list_type() . '">';
1310 foreach($evdata['data'] as $item){
1311 if (!$fullscreen) media_printfile($item,$item['perm'],'',true);
1312 else media_printfile_thumbs($item,$item['perm'],false,true);
1314 if ($fullscreen) echo '</ul>'.NL;
1319 * Formats and prints one file in the list
1321 function media_printfile($item,$auth,$jump,$display_namespace=false){
1325 // Prepare zebra coloring
1326 // I always wanted to use this variable name :-D
1327 static $twibble = 1;
1329 $zebra = ($twibble == -1) ? 'odd' : 'even';
1331 // Automatically jump to recent action
1332 if($jump == $item['id']) {
1333 $jump = ' id="scroll__here" ';
1338 // Prepare fileicons
1339 list($ext,$mime,$dl) = mimetype($item['file'],false);
1340 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
1341 $class = 'select mediafile mf_'.$class;
1344 $file = utf8_decodeFN($item['file']);
1349 $info .= (int) $item['meta']->getField('File.Width');
1351 $info .= (int) $item['meta']->getField('File.Height');
1354 $info .= '<i>'.dformat($item['mtime']).'</i>';
1356 $info .= filesize_h($item['size']);
1359 echo '<div class="'.$zebra.'"'.$jump.' title="'.hsc($item['id']).'">'.NL;
1360 if (!$display_namespace) {
1361 echo '<a name="h_:'.$item['id'].'" class="'.$class.'">'.hsc($file).'</a> ';
1363 echo '<a name="h_:'.$item['id'].'" class="'.$class.'">'.hsc($item['id']).'</a><br/>';
1365 echo '<span class="info">('.$info.')</span>'.NL;
1368 $link = ml($item['id'],'',true);
1369 echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
1370 'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>';
1372 // mediamanager button
1373 $link = wl('',array('do'=>'media','image'=>$item['id'],'ns'=>getNS($item['id'])));
1374 echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/mediamanager.png" '.
1375 'alt="'.$lang['btn_media'].'" title="'.$lang['btn_media'].'" class="btn" /></a>';
1378 if($item['writable'] && $auth >= AUTH_DELETE){
1379 $link = DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).
1380 '&sectok='.getSecurityToken();
1381 echo ' <a href="'.$link.'" class="btn_media_delete" title="'.$item['id'].'">'.
1382 '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '.
1383 'title="'.$lang['btn_delete'].'" class="btn" /></a>';
1386 echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">';
1387 echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>';
1389 if($item['isimg']) media_printimgdetail($item);
1390 echo '<div class="clearer"></div>'.NL;
1394 function media_printicon($filename){
1395 list($ext,$mime,$dl) = mimetype(mediaFN($filename),false);
1397 if (@file_exists(DOKU_INC.'lib/images/fileicons/'.$ext.'.png')) {
1398 $icon = DOKU_BASE.'lib/images/fileicons/'.$ext.'.png';
1400 $icon = DOKU_BASE.'lib/images/fileicons/file.png';
1403 return '<img src="'.$icon.'" alt="'.$filename.'" class="icon" />';
1408 * Formats and prints one file in the list in the thumbnails view
1410 * @author Kate Arzamastseva <pshns@ukr.net>
1412 function media_printfile_thumbs($item,$auth,$jump=false,$display_namespace=false){
1417 $file = utf8_decodeFN($item['file']);
1420 echo '<li><dl title="'.hsc($item['id']).'">'.NL;
1423 if($item['isimg']) {
1424 media_printimgdetail($item, true);
1427 echo '<a name="d_:'.$item['id'].'" class="image" title="'.$item['id'].'" href="'.
1428 media_managerURL(array('image' => hsc($item['id']), 'ns' => getNS($item['id']),
1429 'tab_details' => 'view')).'">';
1430 echo media_printicon($item['id']);
1434 if (!$display_namespace) {
1437 $name = hsc($item['id']);
1439 echo '<dd class="name"><a href="'.media_managerURL(array('image' => hsc($item['id']), 'ns' => getNS($item['id']),
1440 'tab_details' => 'view')).'" name="h_:'.$item['id'].'">'.$name.'</a></dd>'.NL;
1444 $size .= (int) $item['meta']->getField('File.Width');
1446 $size .= (int) $item['meta']->getField('File.Height');
1447 echo '<dd class="size">'.$size.'</dd>'.NL;
1449 echo '<dd class="size"> </dd>'.NL;
1451 $date = dformat($item['mtime']);
1452 echo '<dd class="date">'.$date.'</dd>'.NL;
1453 $filesize = filesize_h($item['size']);
1454 echo '<dd class="filesize">'.$filesize.'</dd>'.NL;
1455 echo '</dl></li>'.NL;
1459 * Prints a thumbnail and metainfos
1461 function media_printimgdetail($item, $fullscreen=false){
1462 // prepare thumbnail
1463 $size = $fullscreen ? 90 : 120;
1465 $w = (int) $item['meta']->getField('File.Width');
1466 $h = (int) $item['meta']->getField('File.Height');
1467 if($w>$size || $h>$size){
1469 $ratio = $item['meta']->getResizeRatio($size);
1471 $ratio = $item['meta']->getResizeRatio($size,$size);
1473 $w = floor($w * $ratio);
1474 $h = floor($h * $ratio);
1476 $src = ml($item['id'],array('w'=>$w,'h'=>$h,'t'=>$item['mtime']));
1479 // In fullscreen mediamanager view, image resizing is done via CSS.
1483 $p['alt'] = $item['id'];
1484 $att = buildAttributes($p);
1488 echo '<a name="l_:'.$item['id'].'" class="image thumb" href="'.
1489 media_managerURL(array('image' => hsc($item['id']), 'ns' => getNS($item['id']), 'tab_details' => 'view')).'">';
1490 echo '<img src="'.$src.'" '.$att.' />';
1494 if ($fullscreen) return;
1496 echo '<div class="detail">';
1497 echo '<div class="thumb">';
1498 echo '<a name="d_:'.$item['id'].'" class="select">';
1499 echo '<img src="'.$src.'" '.$att.' />';
1503 // read EXIF/IPTC data
1504 $t = $item['meta']->getField(array('IPTC.Headline','xmp.dc:title'));
1505 $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
1506 'EXIF.TIFFImageDescription',
1507 'EXIF.TIFFUserComment'));
1508 if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...';
1509 $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject'));
1511 // print EXIF/IPTC data
1512 if($t || $d || $k ){
1514 if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />';
1515 if($d) echo htmlspecialchars($d).'<br />';
1516 if($t) echo '<em>'.htmlspecialchars($k).'</em>';
1523 * Build link based on the current, adding/rewriting
1526 * @author Kate Arzamastseva <pshns@ukr.net>
1527 * @param array $params
1528 * @param string $amp - separator
1529 * @return string - link
1531 function media_managerURL($params=false, $amp='&', $abs=false, $params_array=false) {
1535 $gets = array('do' => 'media');
1536 $media_manager_params = array('tab_files', 'tab_details', 'image', 'ns', 'list', 'sort');
1537 foreach ($media_manager_params as $x) {
1538 if (isset($_REQUEST[$x])) $gets[$x] = $_REQUEST[$x];
1542 $gets = $params + $gets;
1545 if (isset($gets['delete'])) {
1546 unset($gets['image']);
1547 unset($gets['tab_details']);
1550 if ($params_array) return $gets;
1552 return wl($ID,$gets,$abs,$amp);
1556 * Print the media upload form if permissions are correct
1558 * @author Andreas Gohr <andi@splitbrain.org>
1559 * @author Kate Arzamastseva <pshns@ukr.net>
1561 function media_uploadform($ns, $auth, $fullscreen = false){
1562 global $lang, $conf;
1564 if($auth < AUTH_UPLOAD) {
1565 echo '<div class="nothing">'.$lang['media_perm_upload'].'</div>'.NL;
1568 $auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
1572 if ($auth >= $auth_ow && $fullscreen && $_REQUEST['mediado'] == 'update') {
1574 $id = cleanID($_REQUEST['image']);
1577 // The default HTML upload form
1578 $params = array('id' => 'dw__upload',
1579 'enctype' => 'multipart/form-data');
1581 $params['action'] = DOKU_BASE.'lib/exe/mediamanager.php';
1583 $params['action'] = media_managerURL(array('tab_files' => 'files',
1584 'tab_details' => 'view'), '&');
1587 $form = new Doku_Form($params);
1588 if (!$fullscreen) echo '<div class="upload">' . $lang['mediaupload'] . '</div>';
1589 $form->addElement(formSecurityToken());
1590 $form->addHidden('ns', hsc($ns));
1591 $form->addElement(form_makeOpenTag('p'));
1592 $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file'));
1593 $form->addElement(form_makeCloseTag('p'));
1594 $form->addElement(form_makeOpenTag('p'));
1595 $form->addElement(form_makeTextField('mediaid', noNS($id), $lang['txt_filename'].':', 'upload__name'));
1596 $form->addElement(form_makeButton('submit', '', $lang['btn_upload']));
1597 $form->addElement(form_makeCloseTag('p'));
1599 if($auth >= $auth_ow){
1600 $form->addElement(form_makeOpenTag('p'));
1602 if ($update) $attrs['checked'] = 'checked';
1603 $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check', $attrs));
1604 $form->addElement(form_makeCloseTag('p'));
1607 echo NL.'<div id="mediamanager__uploader">'.NL;
1608 html_form('upload', $form);
1613 * Print the search field form
1615 * @author Tobias Sarnowski <sarnowski@cosmocode.de>
1616 * @author Kate Arzamastseva <pshns@ukr.net>
1618 function media_searchform($ns,$query='',$fullscreen=false){
1621 // The default HTML search form
1622 $params = array('id' => 'dw__mediasearch');
1624 $params['action'] = DOKU_BASE.'lib/exe/mediamanager.php';
1626 $params['action'] = media_managerURL(array(), '&');
1628 $form = new Doku_Form($params);
1629 $form->addHidden('ns', $ns);
1630 $form->addHidden($fullscreen ? 'mediado' : 'do', 'searchlist');
1632 if (!$fullscreen) $form->addElement('<div class="upload">' . $lang['mediasearch'] . '</div>'.NL);
1633 $form->addElement(form_makeOpenTag('p'));
1634 $form->addElement(form_makeTextField('q', $query,$lang['searchmedia'],'','',array('title'=>sprintf($lang['searchmedia_in'],hsc($ns).':*'))));
1635 $form->addElement(form_makeButton('submit', '', $lang['btn_search']));
1636 $form->addElement(form_makeCloseTag('p'));
1637 html_form('searchmedia', $form);
1641 * Build a tree outline of available media namespaces
1643 * @author Andreas Gohr <andi@splitbrain.org>
1645 function media_nstree($ns){
1649 // currently selected namespace
1653 $ns = dirname(str_replace(':','/',$ID));
1654 if($ns == '.') $ns ='';
1656 $ns = utf8_encodeFN(str_replace(':','/',$ns));
1659 search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true));
1661 // wrap a list with the root level around the other namespaces
1662 array_unshift($data, array('level' => 0, 'id' => '', 'open' =>'true',
1663 'label' => '['.$lang['mediaroot'].']'));
1665 echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li');
1669 * Userfunction for html_buildlist
1671 * Prints a media namespace tree item
1673 * @author Andreas Gohr <andi@splitbrain.org>
1675 function media_nstree_item($item){
1676 $pos = strrpos($item['id'], ':');
1677 $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
1678 if(!$item['label']) $item['label'] = $label;
1681 if (!($_REQUEST['do'] == 'media'))
1682 $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
1683 else $ret .= '<a href="'.media_managerURL(array('ns' => idfilter($item['id'], false), 'tab_files' => 'files'))
1684 .'" class="idx_dir">';
1685 $ret .= $item['label'];
1691 * Userfunction for html_buildlist
1693 * Prints a media namespace tree item opener
1695 * @author Andreas Gohr <andi@splitbrain.org>
1697 function media_nstree_li($item){
1698 $class='media level'.$item['level'];
1701 $img = DOKU_BASE.'lib/images/minus.gif';
1704 $class .= ' closed';
1705 $img = DOKU_BASE.'lib/images/plus.gif';
1708 // TODO: only deliver an image if it actually has a subtree...
1709 return '<li class="'.$class.'">'.
1710 '<img src="'.$img.'" alt="'.$alt.'" />';
1714 * Resizes the given image to the given size
1716 * @author Andreas Gohr <andi@splitbrain.org>
1718 function media_resize_image($file, $ext, $w, $h=0){
1721 $info = @getimagesize($file); //get original size
1722 if($info == false) return $file; // that's no image - it's a spaceship!
1724 if(!$h) $h = round(($w * $info[1]) / $info[0]);
1726 // we wont scale up to infinity
1727 if($w > 2000 || $h > 2000) return $file;
1730 $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
1731 $mtime = @filemtime($local); // 0 if not exists
1733 if( $mtime > filemtime($file) ||
1734 media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
1735 media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
1736 if($conf['fperm']) chmod($local, $conf['fperm']);
1739 //still here? resizing failed
1744 * Crops the given image to the wanted ratio, then calls media_resize_image to scale it
1745 * to the wanted size
1747 * Crops are centered horizontally but prefer the upper third of an vertical
1748 * image because most pics are more interesting in that area (rule of thirds)
1750 * @author Andreas Gohr <andi@splitbrain.org>
1752 function media_crop_image($file, $ext, $w, $h=0){
1756 $info = @getimagesize($file); //get original size
1757 if($info == false) return $file; // that's no image - it's a spaceship!
1759 // calculate crop size
1760 $fr = $info[0]/$info[1];
1765 $ch = (int) $info[0]/$tr;
1767 $cw = (int) $info[1]*$tr;
1772 $cw = (int) $info[1]*$tr;
1776 $ch = (int) $info[0]/$tr;
1779 // calculate crop offset
1780 $cx = (int) ($info[0]-$cw)/2;
1781 $cy = (int) ($info[1]-$ch)/3;
1784 $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext);
1785 $mtime = @filemtime($local); // 0 if not exists
1787 if( $mtime > @filemtime($file) ||
1788 media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
1789 media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
1790 if($conf['fperm']) chmod($local, $conf['fperm']);
1791 return media_resize_image($local,$ext, $w, $h);
1794 //still here? cropping failed
1795 return media_resize_image($file,$ext, $w, $h);
1799 * Download a remote file and return local filename
1801 * returns false if download fails. Uses cached file if available and
1804 * @author Andreas Gohr <andi@splitbrain.org>
1805 * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
1807 function media_get_from_URL($url,$ext,$cache){
1810 // if no cache or fetchsize just redirect
1811 if ($cache==0) return false;
1812 if (!$conf['fetchsize']) return false;
1814 $local = getCacheName(strtolower($url),".media.$ext");
1815 $mtime = @filemtime($local); // 0 if not exists
1817 //decide if download needed:
1818 if( ($mtime == 0) || // cache does not exist
1819 ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired
1821 if(media_image_download($url,$local)){
1828 //if cache exists use it else
1829 if($mtime) return $local;
1836 * Download image files
1838 * @author Andreas Gohr <andi@splitbrain.org>
1840 function media_image_download($url,$file){
1842 $http = new DokuHTTPClient();
1843 $http->max_bodysize = $conf['fetchsize'];
1844 $http->timeout = 25; //max. 25 sec
1845 $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
1847 $data = $http->get($url);
1848 if(!$data) return false;
1850 $fileexists = @file_exists($file);
1851 $fp = @fopen($file,"w");
1852 if(!$fp) return false;
1855 if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
1857 // check if it is really an image
1858 $info = @getimagesize($file);
1868 * resize images using external ImageMagick convert program
1870 * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
1871 * @author Andreas Gohr <andi@splitbrain.org>
1873 function media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
1876 // check if convert is configured
1877 if(!$conf['im_convert']) return false;
1880 $cmd = $conf['im_convert'];
1881 $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
1882 if ($ext == 'jpg' || $ext == 'jpeg') {
1883 $cmd .= ' -quality '.$conf['jpg_quality'];
1885 $cmd .= " $from $to";
1887 @exec($cmd,$out,$retval);
1888 if ($retval == 0) return true;
1893 * crop images using external ImageMagick convert program
1895 * @author Andreas Gohr <andi@splitbrain.org>
1897 function media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
1900 // check if convert is configured
1901 if(!$conf['im_convert']) return false;
1904 $cmd = $conf['im_convert'];
1905 $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y;
1906 if ($ext == 'jpg' || $ext == 'jpeg') {
1907 $cmd .= ' -quality '.$conf['jpg_quality'];
1909 $cmd .= " $from $to";
1911 @exec($cmd,$out,$retval);
1912 if ($retval == 0) return true;
1917 * resize or crop images using PHP's libGD support
1919 * @author Andreas Gohr <andi@splitbrain.org>
1920 * @author Sebastian Wienecke <s_wienecke@web.de>
1922 function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
1925 if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
1927 // check available memory
1928 if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
1932 // create an image of the given filetype
1933 if ($ext == 'jpg' || $ext == 'jpeg'){
1934 if(!function_exists("imagecreatefromjpeg")) return false;
1935 $image = @imagecreatefromjpeg($from);
1936 }elseif($ext == 'png') {
1937 if(!function_exists("imagecreatefrompng")) return false;
1938 $image = @imagecreatefrompng($from);
1940 }elseif($ext == 'gif') {
1941 if(!function_exists("imagecreatefromgif")) return false;
1942 $image = @imagecreatefromgif($from);
1944 if(!$image) return false;
1946 if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
1947 $newimg = @imagecreatetruecolor ($to_w, $to_h);
1949 if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
1951 imagedestroy($image);
1955 //keep png alpha channel if possible
1956 if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
1957 imagealphablending($newimg, false);
1958 imagesavealpha($newimg,true);
1961 //keep gif transparent color if possible
1962 if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
1963 if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
1964 $transcolorindex = @imagecolortransparent($image);
1965 if($transcolorindex >= 0 ) { //transparent color exists
1966 $transcolor = @imagecolorsforindex($image, $transcolorindex);
1967 $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
1968 @imagefill($newimg, 0, 0, $transcolorindex);
1969 @imagecolortransparent($newimg, $transcolorindex);
1970 }else{ //filling with white
1971 $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
1972 @imagefill($newimg, 0, 0, $whitecolorindex);
1974 }else{ //filling with white
1975 $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
1976 @imagefill($newimg, 0, 0, $whitecolorindex);
1980 //try resampling first
1981 if(function_exists("imagecopyresampled")){
1982 if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
1983 imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
1986 imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
1990 if ($ext == 'jpg' || $ext == 'jpeg'){
1991 if(!function_exists('imagejpeg')){
1994 $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
1996 }elseif($ext == 'png') {
1997 if(!function_exists('imagepng')){
2000 $okay = imagepng($newimg, $to);
2002 }elseif($ext == 'gif') {
2003 if(!function_exists('imagegif')){
2006 $okay = imagegif($newimg, $to);
2010 // destroy GD image ressources
2011 if($image) imagedestroy($image);
2012 if($newimg) imagedestroy($newimg);
2017 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */