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