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