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="clearer"></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="clearer"></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="clearer"></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 '<ul class="mediamanager-file-list mediamanager-list" id="id-mediamanager-file-list">'; 632 } else { 633 echo '<ul class="mediamanager-file-list mediamanager-thumbs" id="id-mediamanager-file-list">'; 634 } 635 media_filelist($ns,$auth,$jump,'thumbs'); 636 echo '</ul>'; 637 } 638 echo '</div>'; 639 echo '</div>'; 640} 641 642/** 643 * Prints tab that displays uploading form 644 * 645 * @author Kate Arzamastseva <pshns@ukr.net> 646 */ 647function media_tab_upload($ns,$auth=null,$jump='') { 648 global $lang; 649 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 650 651 echo '<div class="mediamanager-tab-upload"">'; 652 echo '<div class="background-container">'; 653 echo $lang['mediaupload']; 654 echo '</div>'; 655 656 echo '<div class="scroll-container">'; 657 media_uploadform($ns, $auth, true); 658 echo '</div>'; 659 echo '</div>'; 660} 661 662/** 663 * Prints tab that displays search form 664 * 665 * @author Kate Arzamastseva <pshns@ukr.net> 666 */ 667function media_tab_search($ns,$auth=null) { 668 global $lang; 669 670 $do = $_REQUEST['mediado']; 671 $query = $_REQUEST['q']; 672 if (!$query) $query = ''; 673 674 echo '<div class="mediamanager-tab-search">'; 675 echo '<div class="background-container">'; 676 echo $lang['media_search']; 677 echo'</div>'; 678 679 echo '<div class="scroll-container">'; 680 media_searchform($ns, $query, true); 681 682 if($do == 'searchlist'){ 683 media_searchlist($query,$ns,$auth,true); 684 } 685 echo '</div>'; 686 echo '</div>'; 687} 688 689/** 690 * Prints tab that displays mediafile details 691 * 692 * @author Kate Arzamastseva <pshns@ukr.net> 693 */ 694function media_tab_view($image, $ns, $auth=null) { 695 global $lang, $conf; 696 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 697 698 echo '<div class="mediamanager-tab-detail-view">'; 699 echo '<div class="background-container">'; 700 echo $image; 701 echo '</div>'; 702 703 echo '<div class="scroll-container">'; 704 $rev = (int) $_REQUEST['rev']; 705 media_preview($image, $auth, $rev); 706 media_details($image, $auth, $rev); 707 echo '</div>'; 708 echo '</div>'; 709} 710 711/** 712 * Prints tab that displays form for editing mediafile metadata 713 * 714 * @author Kate Arzamastseva <pshns@ukr.net> 715 */ 716function media_tab_edit($image, $ns, $auth=null) { 717 global $lang; 718 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 719 720 echo '<div class="mediamanager-tab-detail-edit">'; 721 echo '<div class="background-container">'; 722 echo $lang['media_edit']; 723 echo '</div>'; 724 725 echo '<div class="scroll-container">'; 726 if ($image) { 727 $info = new JpegMeta(mediaFN($image)); 728 if ($info->getField('File.Mime') == 'image/jpeg') 729 media_metaform($image,$auth,true); 730 } 731 echo '</div>'; 732 echo '</div>'; 733} 734 735/** 736 * Prints tab that displays mediafile revisions 737 * 738 * @author Kate Arzamastseva <pshns@ukr.net> 739 */ 740function media_tab_history($image, $ns, $auth=null) { 741 global $lang; 742 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 743 $do = $_REQUEST['mediado']; 744 745 echo '<div class="mediamanager-tab-detail-history">'; 746 echo '<div class="background-container">'; 747 echo $lang['media_history']; 748 echo '</div>'; 749 750 echo '<div class="scroll-container">'; 751 if ($auth >= AUTH_READ && $image) { 752 if ($do == 'diff'){ 753 media_diff($image, $ns, $auth); 754 } else { 755 $first = isset($_REQUEST['first']) ? intval($_REQUEST['first']) : 0; 756 html_revisions($first, $image); 757 } 758 } 759 echo '</div>'; 760 echo '</div>'; 761} 762 763/** 764 * Prints mediafile details 765 * 766 * @author Kate Arzamastseva <pshns@ukr.net> 767 */ 768function media_preview($image, $auth, $rev=false) { 769 global $lang; 770 if ($auth < AUTH_READ || !$image) return ''; 771 $info = new JpegMeta(mediaFN($image)); 772 $w = (int) $info->getField('File.Width'); 773 774 $more = ''; 775 if ($rev) $more = "rev=$rev"; 776 $src = ml($image, $more); 777 778 echo '<img src="'.$src.'" alt="" width="99%" style="max-width: '.$w.'px;" /><br /><br />'; 779 780 $link = ml($image,$more,true); 781 echo $image.' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '. 782 'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>'; 783 784 // delete button 785 if($auth >= AUTH_DELETE && !$rev){ 786 $link = media_managerURL(array('delete' => $image,'sectok' => getSecurityToken())); 787 echo ' <a href="'.$link.'" class="btn_media_delete" title="'.$image.'">'. 788 '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '. 789 'title="'.$lang['btn_delete'].'" class="btn" /></a>'; 790 } 791 792} 793 794/** 795 * Prints mediafile tags 796 * 797 * @author Kate Arzamastseva <pshns@ukr.net> 798 */ 799function media_details($image, $auth, $rev=false) { 800 global $lang; 801 802 $tags = array( 803 array('simple.title','img_title','text'), 804 array('Date.EarliestTime','img_date','date'), 805 array('File.Name','img_fname','text'), 806 array(array('Iptc.Byline','Exif.TIFFArtist','Exif.Artist','Iptc.Credit'),'img_artist','text'), 807 array(array('Iptc.CopyrightNotice','Exif.TIFFCopyright','Exif.Copyright'),'img_copyr','text'), 808 array('File.Format','img_format','text'), 809 array('File.NiceSize','img_fsize','text'), 810 array('File.Width','img_width','text'), 811 array('File.Height','img_height','text'), 812 array('Simple.Camera','img_camera','text'), 813 array(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject'),'img_keywords','text') 814 ); 815 816 $src = mediaFN($image, $rev); 817 echo '<dl class="img_tags">'; 818 foreach($tags as $key => $tag){ 819 $t = $tag[0]; 820 if (!is_array($t)) $t = array($tag[0]); 821 $value = media_getTag($t,$src); 822 $value = cleanText($value); 823 if (!$value) $value='-'; 824 echo '<dt>'.$lang[$tag[1]].':</dt><dd>'; 825 if ($tag[2] == 'text') echo hsc($value); 826 if ($tag[2] == 'date') echo dformat($value); 827 echo '</dd>'; 828 } 829 echo '</dl>'; 830} 831 832/** 833 * Returns the requested EXIF/IPTC tag from the current image 834 * 835 */ 836function media_getTag($tags,$src,$alt=''){ 837 $meta = new JpegMeta($src); 838 if($meta === false) return $alt; 839 $info = $meta->getField($tags); 840 if($info == false) return $alt; 841 return $info; 842} 843 844/** 845 * Shows difference between two revisions of file 846 * 847 * @author Kate Arzamastseva <pshns@ukr.net> 848 */ 849function media_diff($image, $ns, $auth) { 850 global $lang; 851 global $conf; 852 853 $rev1 = (int) $_REQUEST['rev']; 854 855 if(is_array($_REQUEST['rev2'])){ 856 $rev1 = (int) $_REQUEST['rev2'][0]; 857 $rev2 = (int) $_REQUEST['rev2'][1]; 858 859 if(!$rev1){ 860 $rev1 = $rev2; 861 unset($rev2); 862 } 863 }else{ 864 $rev2 = (int) $_REQUEST['rev2']; 865 } 866 if($rev1 && $rev2){ // two specific revisions wanted 867 // make sure order is correct (older on the left) 868 if($rev1 < $rev2){ 869 $l_rev = $rev1; 870 $r_rev = $rev2; 871 }else{ 872 $l_rev = $rev2; 873 $r_rev = $rev1; 874 } 875 }elseif($rev1){ // single revision given, compare to current 876 $r_rev = ''; 877 $l_rev = $rev1; 878 }else{ // no revision was given, compare previous to current 879 $r_rev = ''; 880 $revs = getRevisions($image, 0, 1, 8192, true); 881 $l_rev = $revs[0]; 882 } 883 echo '<ul class="mediamanager-table-50"><li><div>'; 884 media_preview($image, $auth, $l_rev); 885 echo '</div></li>'; 886 echo '<li><div>'; 887 media_preview($image, $auth, $r_rev); 888 echo '</div></li><li><div>'; 889 media_details($image, $auth, $l_rev); 890 echo '</div></li>'; 891 echo '<li><div>'; 892 media_details($image, $auth, $r_rev); 893 echo '</div></li></ul>'; 894} 895 896/** 897 * List all files found by the search request 898 * 899 * @author Tobias Sarnowski <sarnowski@cosmocode.de> 900 * @author Andreas Gohr <gohr@cosmocode.de> 901 * @author Kate Arzamastseva <pshns@ukr.net> 902 * @triggers MEDIA_SEARCH 903 */ 904function media_searchlist($query,$ns,$auth=null,$fullscreen=false){ 905 global $conf; 906 global $lang; 907 908 $ns = cleanID($ns); 909 910 if ($query) { 911 $evdata = array( 912 'ns' => $ns, 913 'data' => array(), 914 'query' => $query 915 ); 916 $evt = new Doku_Event('MEDIA_SEARCH', $evdata); 917 if ($evt->advise_before()) { 918 $dir = utf8_encodeFN(str_replace(':','/',$evdata['ns'])); 919 $pattern = '/'.preg_quote($evdata['query'],'/').'/i'; 920 search($evdata['data'], 921 $conf['mediadir'], 922 'search_media', 923 array('showmsg'=>false,'pattern'=>$pattern), 924 $dir); 925 } 926 $evt->advise_after(); 927 unset($evt); 928 } 929 930 if (!$fullscreen) { 931 echo '<h1 id="media__ns">'.sprintf($lang['searchmedia_in'],hsc($ns).':*').'</h1>'.NL; 932 media_searchform($ns,$query); 933 } 934 935 if(!count($evdata['data'])){ 936 echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 937 }else foreach($evdata['data'] as $item){ 938 if (!$fullscreen) media_printfile($item,$item['perm'],'',true); 939 else media_printfile_thumbs($item,$item['perm'],'',true); 940 } 941} 942 943/** 944 * Print action links for a file depending on filetype 945 * and available permissions 946 */ 947function media_fileactions($item,$auth){ 948 global $lang; 949 950 // view button 951 $link = ml($item['id'],'',true); 952 echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '. 953 'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>'; 954 955 // no further actions if not writable 956 if(!$item['writable']) return; 957 958 // delete button 959 if($auth >= AUTH_DELETE){ 960 $link = DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']). 961 '&sectok='.getSecurityToken(); 962 echo ' <a href="'.$link.'" class="btn_media_delete" title="'.$item['id'].'">'. 963 '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '. 964 'title="'.$lang['btn_delete'].'" class="btn" /></a>'; 965 } 966 967 // edit button 968 if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){ 969 $link = DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']); 970 echo ' <a href="'.$link.'">'. 971 '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '. 972 'title="'.$lang['metaedit'].'" class="btn" /></a>'; 973 } 974 975} 976 977/** 978 * Formats and prints one file in the list 979 */ 980function media_printfile($item,$auth,$jump,$display_namespace=false){ 981 global $lang; 982 global $conf; 983 984 // Prepare zebra coloring 985 // I always wanted to use this variable name :-D 986 static $twibble = 1; 987 $twibble *= -1; 988 $zebra = ($twibble == -1) ? 'odd' : 'even'; 989 990 // Automatically jump to recent action 991 if($jump == $item['id']) { 992 $jump = ' id="scroll__here" '; 993 }else{ 994 $jump = ''; 995 } 996 997 // Prepare fileicons 998 list($ext,$mime,$dl) = mimetype($item['file'],false); 999 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 1000 $class = 'select mediafile mf_'.$class; 1001 1002 // Prepare filename 1003 $file = utf8_decodeFN($item['file']); 1004 1005 // Prepare info 1006 $info = ''; 1007 if($item['isimg']){ 1008 $info .= (int) $item['meta']->getField('File.Width'); 1009 $info .= '×'; 1010 $info .= (int) $item['meta']->getField('File.Height'); 1011 $info .= ' '; 1012 } 1013 $info .= '<i>'.dformat($item['mtime']).'</i>'; 1014 $info .= ' '; 1015 $info .= filesize_h($item['size']); 1016 1017 // output 1018 echo '<div class="'.$zebra.'"'.$jump.'>'.NL; 1019 if (!$display_namespace) { 1020 echo '<a name="h_:'.$item['id'].'" class="'.$class.'">'.hsc($file).'</a> '; 1021 } else { 1022 echo '<a name="h_:'.$item['id'].'" class="'.$class.'">'.hsc($item['id']).'</a><br/>'; 1023 } 1024 echo '<span class="info">('.$info.')</span>'.NL; 1025 media_fileactions($item,$auth); 1026 echo '<div class="example" id="ex_'.str_replace(':','_',$item['id']).'">'; 1027 echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>'; 1028 echo '</div>'; 1029 if($item['isimg']) media_printimgdetail($item); 1030 echo '<div class="clearer"></div>'.NL; 1031 echo '</div>'.NL; 1032} 1033 1034/** 1035 * Formats and prints one file in the list in the thumbnails view 1036 * 1037 * @author Kate Arzamastseva <pshns@ukr.net> 1038 */ 1039function media_printfile_thumbs($item,$auth,$jump){ 1040 global $lang; 1041 global $conf; 1042 1043 // Prepare filename 1044 $file = utf8_decodeFN($item['file']); 1045 1046 // output 1047 echo '<li><div>'; 1048 if($item['isimg']) { 1049 media_printimgdetail($item, true); 1050 } else { 1051 echo '<a name="d_:'.$item['id'].'" class="image" title="'.$item['id'].'" href="'. 1052 media_managerURL(array('image' => hsc($item['id']))).'">'; 1053 echo '<img src="'.DOKU_BASE.'lib/images/icon-file.png" width="90px" />'; 1054 echo '</a>'; 1055 } 1056 echo '<a href="'.media_managerURL(array('image' => hsc($item['id']))).'" name= 1057 "h_:'.$item['id'].'" class="info" >'.hsc($file).'</a>'; 1058 if($item['isimg']){ 1059 $info = ''; 1060 $info .= (int) $item['meta']->getField('File.Width'); 1061 $info .= '×'; 1062 $info .= (int) $item['meta']->getField('File.Height'); 1063 echo '<span class="info">'.$info.'</span>'; 1064 } else { 1065 echo '<span class="info"> </span>'; 1066 } 1067 $info = '<i>'.dformat($item['mtime']).'</i>'; 1068 echo '<span class="info">'.$info.'</span>'; 1069 $info = filesize_h($item['size']); 1070 echo '<span class="info">'.$info.'</span>'; 1071 echo '<div class="clearer"></div>'; 1072 echo '</div></li>'.NL; 1073} 1074 1075/** 1076 * Prints a thumbnail and metainfos 1077 */ 1078function media_printimgdetail($item, $fullscreen=false){ 1079 // prepare thumbnail 1080 if (!$fullscreen) $size = 120; 1081 else $size = 90; 1082 $w = (int) $item['meta']->getField('File.Width'); 1083 $h = (int) $item['meta']->getField('File.Height'); 1084 if($w>$size || $h>$size){ 1085 if (!$fullscreen) { 1086 $ratio = $item['meta']->getResizeRatio($size); 1087 } else { 1088 $ratio = $item['meta']->getResizeRatio($size,$size); 1089 } 1090 $w = floor($w * $ratio); 1091 $h = floor($h * $ratio); 1092 } 1093 $src = ml($item['id'],array('w'=>$w,'h'=>$h)); 1094 $p = array(); 1095 $p['width'] = $w; 1096 if (!$fullscreen) $p['height'] = $h; 1097 $p['alt'] = $item['id']; 1098 $p['class'] = 'thumb'; 1099 $att = buildAttributes($p); 1100 1101 // output 1102 if ($fullscreen) { 1103 echo '<a name="d_:'.$item['id'].'" class="image" title="'.$item['id'].'" href="'. 1104 media_managerURL(array('image' => hsc($item['id']))).'">'; 1105 echo '<img src="'.$src.'" '.$att.' />'; 1106 echo '</a>'; 1107 return 1; 1108 } 1109 1110 echo '<div class="detail">'; 1111 echo '<div class="thumb">'; 1112 echo '<a name="d_:'.$item['id'].'" class="select">'; 1113 echo '<img src="'.$src.'" '.$att.' />'; 1114 echo '</a>'; 1115 echo '</div>'; 1116 1117 // read EXIF/IPTC data 1118 $t = $item['meta']->getField(array('IPTC.Headline','xmp.dc:title')); 1119 $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment', 1120 'EXIF.TIFFImageDescription', 1121 'EXIF.TIFFUserComment')); 1122 if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...'; 1123 $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category','xmp.dc:subject')); 1124 1125 // print EXIF/IPTC data 1126 if($t || $d || $k ){ 1127 echo '<p>'; 1128 if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />'; 1129 if($d) echo htmlspecialchars($d).'<br />'; 1130 if($t) echo '<em>'.htmlspecialchars($k).'</em>'; 1131 echo '</p>'; 1132 } 1133 echo '</div>'; 1134} 1135 1136/** 1137 * Build link based on the current, adding/rewriting 1138 * parameters 1139 * 1140 * @author Kate Arzamastseva <pshns@ukr.net> 1141 * @param array $params 1142 * @param string $amp - separator 1143 * @return string - link 1144 */ 1145function media_managerURL($params=false, $amp='&') { 1146 global $conf; 1147 global $ID; 1148 1149 $url = $_SERVER['REQUEST_URI']; 1150 1151 $urlArray = explode('?', $url, 2); 1152 $gets = @$urlArray[1]; 1153 parse_str($gets, $gets); 1154 1155 if ($gets['edit']) $gets['image'] = $gets['edit']; 1156 unset($gets['edit']); 1157 unset($gets['sectok']); 1158 unset($gets['delete']); 1159 unset($gets['rev']); 1160 unset($gets['mediado']); 1161 1162 if ($params) { 1163 foreach ($params as $k => $v) { 1164 $gets[$k] = $v; 1165 } 1166 } 1167 unset($gets['id']); 1168 if ($gets['delete']) { 1169 unset($gets['image']); 1170 unset($gets['tab_details']); 1171 } 1172 1173 return wl($ID,$gets,false,$amp); 1174} 1175 1176/** 1177 * Print the media upload form if permissions are correct 1178 * 1179 * @author Andreas Gohr <andi@splitbrain.org> 1180 * @author Kate Arzamastseva <pshns@ukr.net> 1181 */ 1182function media_uploadform($ns, $auth, $fullscreen = false){ 1183 global $lang; 1184 1185 if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions? 1186 1187 // The default HTML upload form 1188 $params = array('id' => 'dw__upload', 1189 'enctype' => 'multipart/form-data'); 1190 if (!$fullscreen) $params['action'] = DOKU_BASE.'lib/exe/mediamanager.php'; 1191 else $params['action'] = media_managerURL(array('tab_files' => 'files')); 1192 1193 $form = new Doku_Form($params); 1194 if (!$fullscreen) $form->addElement('<div class="upload">' . $lang['mediaupload'] . '</div>'); 1195 $form->addElement(formSecurityToken()); 1196 $form->addHidden('ns', hsc($ns)); 1197 $form->addElement(form_makeOpenTag('p')); 1198 $form->addElement(form_makeFileField('upload', $lang['txt_upload'].':', 'upload__file')); 1199 $form->addElement(form_makeCloseTag('p')); 1200 $form->addElement(form_makeOpenTag('p')); 1201 $form->addElement(form_makeTextField('id', '', $lang['txt_filename'].':', 'upload__name')); 1202 $form->addElement(form_makeButton('submit', '', $lang['btn_upload'])); 1203 $form->addElement(form_makeCloseTag('p')); 1204 1205 if($auth >= AUTH_DELETE){ 1206 $form->addElement(form_makeOpenTag('p')); 1207 $form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check')); 1208 $form->addElement(form_makeCloseTag('p')); 1209 } 1210 html_form('upload', $form); 1211 1212 // prepare flashvars for multiupload 1213 $opt = array( 1214 'L_gridname' => $lang['mu_gridname'] , 1215 'L_gridsize' => $lang['mu_gridsize'] , 1216 'L_gridstat' => $lang['mu_gridstat'] , 1217 'L_namespace' => $lang['mu_namespace'] , 1218 'L_overwrite' => $lang['txt_overwrt'], 1219 'L_browse' => $lang['mu_browse'], 1220 'L_upload' => $lang['btn_upload'], 1221 'L_toobig' => $lang['mu_toobig'], 1222 'L_ready' => $lang['mu_ready'], 1223 'L_done' => $lang['mu_done'], 1224 'L_fail' => $lang['mu_fail'], 1225 'L_authfail' => $lang['mu_authfail'], 1226 'L_progress' => $lang['mu_progress'], 1227 'L_filetypes' => $lang['mu_filetypes'], 1228 'L_info' => $lang['mu_info'], 1229 'L_lasterr' => $lang['mu_lasterr'], 1230 1231 'O_ns' => ":$ns", 1232 'O_backend' => 'mediamanager.php?'.session_name().'='.session_id(), 1233 'O_maxsize' => php_to_byte(ini_get('upload_max_filesize')), 1234 'O_extensions'=> join('|',array_keys(getMimeTypes())), 1235 'O_overwrite' => ($auth >= AUTH_DELETE), 1236 'O_sectok' => getSecurityToken(), 1237 'O_authtok' => auth_createToken(), 1238 ); 1239 $var = buildURLparams($opt); 1240 // output the flash uploader 1241 ?> 1242 <div id="dw__flashupload" style="display:none"> 1243 <div class="upload"><?php echo $lang['mu_intro']?></div> 1244 <?php echo html_flashobject('multipleUpload.swf','500','190',null,$opt); ?> 1245 </div> 1246 <?php 1247} 1248 1249/** 1250 * Print the search field form 1251 * 1252 * @author Tobias Sarnowski <sarnowski@cosmocode.de> 1253 * @author Kate Arzamastseva <pshns@ukr.net> 1254 */ 1255function media_searchform($ns,$query='',$fullscreen=false){ 1256 global $lang; 1257 1258 // The default HTML search form 1259 $params = array('id' => 'dw__mediasearch'); 1260 if (!$fullscreen) $params['action'] = DOKU_BASE.'lib/exe/mediamanager.php'; 1261 else $params['action'] = media_managerURL(); 1262 $form = new Doku_Form($params); 1263 if (!$fullscreen) $form->addElement('<div class="upload">' . $lang['mediasearch'] . '</div>'); 1264 $form->addElement(formSecurityToken()); 1265 $form->addHidden('ns', $ns); 1266 if (!$fullscreen) $form->addHidden('do', 'searchlist'); 1267 else $form->addHidden('mediado', 'searchlist'); 1268 $form->addElement(form_makeOpenTag('p')); 1269 $form->addElement(form_makeTextField('q', $query,$lang['searchmedia'],'','',array('title'=>sprintf($lang['searchmedia_in'],hsc($ns).':*')))); 1270 $form->addElement(form_makeButton('submit', '', $lang['btn_search'])); 1271 $form->addElement(form_makeCloseTag('p')); 1272 html_form('searchmedia', $form); 1273} 1274 1275/** 1276 * Build a tree outline of available media namespaces 1277 * 1278 * @author Andreas Gohr <andi@splitbrain.org> 1279 */ 1280function media_nstree($ns){ 1281 global $conf; 1282 global $lang; 1283 1284 // currently selected namespace 1285 $ns = cleanID($ns); 1286 if(empty($ns)){ 1287 global $ID; 1288 $ns = dirname(str_replace(':','/',$ID)); 1289 if($ns == '.') $ns =''; 1290 } 1291 $ns = utf8_encodeFN(str_replace(':','/',$ns)); 1292 1293 $data = array(); 1294 search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true)); 1295 1296 // wrap a list with the root level around the other namespaces 1297 $item = array( 'level' => 0, 'id' => '', 1298 'open' =>'true', 'label' => '['.$lang['mediaroot'].']'); 1299 1300 echo '<ul class="idx">'; 1301 echo media_nstree_li($item); 1302 echo media_nstree_item($item); 1303 echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li'); 1304 echo '</li>'; 1305 echo '</ul>'; 1306} 1307 1308/** 1309 * Userfunction for html_buildlist 1310 * 1311 * Prints a media namespace tree item 1312 * 1313 * @author Andreas Gohr <andi@splitbrain.org> 1314 */ 1315function media_nstree_item($item){ 1316 $pos = strrpos($item['id'], ':'); 1317 $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0); 1318 if(!$item['label']) $item['label'] = $label; 1319 1320 $ret = ''; 1321 if (!($_REQUEST['do'] == 'media')) 1322 $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">'; 1323 else $ret .= '<a href="'.media_managerURL(array('ns' => idfilter($item['id']))).'" class="idx_dir">'; 1324 $ret .= $item['label']; 1325 $ret .= '</a>'; 1326 return $ret; 1327} 1328 1329/** 1330 * Userfunction for html_buildlist 1331 * 1332 * Prints a media namespace tree item opener 1333 * 1334 * @author Andreas Gohr <andi@splitbrain.org> 1335 */ 1336function media_nstree_li($item){ 1337 $class='media level'.$item['level']; 1338 if($item['open']){ 1339 $class .= ' open'; 1340 $img = DOKU_BASE.'lib/images/minus.gif'; 1341 $alt = '−'; 1342 }else{ 1343 $class .= ' closed'; 1344 $img = DOKU_BASE.'lib/images/plus.gif'; 1345 $alt = '+'; 1346 } 1347 // TODO: only deliver an image if it actually has a subtree... 1348 return '<li class="'.$class.'">'. 1349 '<img src="'.$img.'" alt="'.$alt.'" />'; 1350} 1351 1352/** 1353 * Resizes the given image to the given size 1354 * 1355 * @author Andreas Gohr <andi@splitbrain.org> 1356 */ 1357function media_resize_image($file, $ext, $w, $h=0){ 1358 global $conf; 1359 1360 $info = @getimagesize($file); //get original size 1361 if($info == false) return $file; // that's no image - it's a spaceship! 1362 1363 if(!$h) $h = round(($w * $info[1]) / $info[0]); 1364 1365 // we wont scale up to infinity 1366 if($w > 2000 || $h > 2000) return $file; 1367 1368 //cache 1369 $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext); 1370 $mtime = @filemtime($local); // 0 if not exists 1371 1372 if( $mtime > filemtime($file) || 1373 media_resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) || 1374 media_resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){ 1375 if($conf['fperm']) chmod($local, $conf['fperm']); 1376 return $local; 1377 } 1378 //still here? resizing failed 1379 return $file; 1380} 1381 1382/** 1383 * Crops the given image to the wanted ratio, then calls media_resize_image to scale it 1384 * to the wanted size 1385 * 1386 * Crops are centered horizontally but prefer the upper third of an vertical 1387 * image because most pics are more interesting in that area (rule of thirds) 1388 * 1389 * @author Andreas Gohr <andi@splitbrain.org> 1390 */ 1391function media_crop_image($file, $ext, $w, $h=0){ 1392 global $conf; 1393 1394 if(!$h) $h = $w; 1395 $info = @getimagesize($file); //get original size 1396 if($info == false) return $file; // that's no image - it's a spaceship! 1397 1398 // calculate crop size 1399 $fr = $info[0]/$info[1]; 1400 $tr = $w/$h; 1401 if($tr >= 1){ 1402 if($tr > $fr){ 1403 $cw = $info[0]; 1404 $ch = (int) $info[0]/$tr; 1405 }else{ 1406 $cw = (int) $info[1]*$tr; 1407 $ch = $info[1]; 1408 } 1409 }else{ 1410 if($tr < $fr){ 1411 $cw = (int) $info[1]*$tr; 1412 $ch = $info[1]; 1413 }else{ 1414 $cw = $info[0]; 1415 $ch = (int) $info[0]/$tr; 1416 } 1417 } 1418 // calculate crop offset 1419 $cx = (int) ($info[0]-$cw)/2; 1420 $cy = (int) ($info[1]-$ch)/3; 1421 1422 //cache 1423 $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext); 1424 $mtime = @filemtime($local); // 0 if not exists 1425 1426 if( $mtime > filemtime($file) || 1427 media_crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) || 1428 media_resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){ 1429 if($conf['fperm']) chmod($local, $conf['fperm']); 1430 return media_resize_image($local,$ext, $w, $h); 1431 } 1432 1433 //still here? cropping failed 1434 return media_resize_image($file,$ext, $w, $h); 1435} 1436 1437/** 1438 * Download a remote file and return local filename 1439 * 1440 * returns false if download fails. Uses cached file if available and 1441 * wanted 1442 * 1443 * @author Andreas Gohr <andi@splitbrain.org> 1444 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 1445 */ 1446function media_get_from_URL($url,$ext,$cache){ 1447 global $conf; 1448 1449 // if no cache or fetchsize just redirect 1450 if ($cache==0) return false; 1451 if (!$conf['fetchsize']) return false; 1452 1453 $local = getCacheName(strtolower($url),".media.$ext"); 1454 $mtime = @filemtime($local); // 0 if not exists 1455 1456 //decide if download needed: 1457 if( ($mtime == 0) || // cache does not exist 1458 ($cache != -1 && $mtime < time()-$cache) // 'recache' and cache has expired 1459 ){ 1460 if(media_image_download($url,$local)){ 1461 return $local; 1462 }else{ 1463 return false; 1464 } 1465 } 1466 1467 //if cache exists use it else 1468 if($mtime) return $local; 1469 1470 //else return false 1471 return false; 1472} 1473 1474/** 1475 * Download image files 1476 * 1477 * @author Andreas Gohr <andi@splitbrain.org> 1478 */ 1479function media_image_download($url,$file){ 1480 global $conf; 1481 $http = new DokuHTTPClient(); 1482 $http->max_bodysize = $conf['fetchsize']; 1483 $http->timeout = 25; //max. 25 sec 1484 $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i'; 1485 1486 $data = $http->get($url); 1487 if(!$data) return false; 1488 1489 $fileexists = @file_exists($file); 1490 $fp = @fopen($file,"w"); 1491 if(!$fp) return false; 1492 fwrite($fp,$data); 1493 fclose($fp); 1494 if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']); 1495 1496 // check if it is really an image 1497 $info = @getimagesize($file); 1498 if(!$info){ 1499 @unlink($file); 1500 return false; 1501 } 1502 1503 return true; 1504} 1505 1506/** 1507 * resize images using external ImageMagick convert program 1508 * 1509 * @author Pavel Vitis <Pavel.Vitis@seznam.cz> 1510 * @author Andreas Gohr <andi@splitbrain.org> 1511 */ 1512function media_resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){ 1513 global $conf; 1514 1515 // check if convert is configured 1516 if(!$conf['im_convert']) return false; 1517 1518 // prepare command 1519 $cmd = $conf['im_convert']; 1520 $cmd .= ' -resize '.$to_w.'x'.$to_h.'!'; 1521 if ($ext == 'jpg' || $ext == 'jpeg') { 1522 $cmd .= ' -quality '.$conf['jpg_quality']; 1523 } 1524 $cmd .= " $from $to"; 1525 1526 @exec($cmd,$out,$retval); 1527 if ($retval == 0) return true; 1528 return false; 1529} 1530 1531/** 1532 * crop images using external ImageMagick convert program 1533 * 1534 * @author Andreas Gohr <andi@splitbrain.org> 1535 */ 1536function media_crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){ 1537 global $conf; 1538 1539 // check if convert is configured 1540 if(!$conf['im_convert']) return false; 1541 1542 // prepare command 1543 $cmd = $conf['im_convert']; 1544 $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y; 1545 if ($ext == 'jpg' || $ext == 'jpeg') { 1546 $cmd .= ' -quality '.$conf['jpg_quality']; 1547 } 1548 $cmd .= " $from $to"; 1549 1550 @exec($cmd,$out,$retval); 1551 if ($retval == 0) return true; 1552 return false; 1553} 1554 1555/** 1556 * resize or crop images using PHP's libGD support 1557 * 1558 * @author Andreas Gohr <andi@splitbrain.org> 1559 * @author Sebastian Wienecke <s_wienecke@web.de> 1560 */ 1561function media_resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){ 1562 global $conf; 1563 1564 if($conf['gdlib'] < 1) return false; //no GDlib available or wanted 1565 1566 // check available memory 1567 if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){ 1568 return false; 1569 } 1570 1571 // create an image of the given filetype 1572 if ($ext == 'jpg' || $ext == 'jpeg'){ 1573 if(!function_exists("imagecreatefromjpeg")) return false; 1574 $image = @imagecreatefromjpeg($from); 1575 }elseif($ext == 'png') { 1576 if(!function_exists("imagecreatefrompng")) return false; 1577 $image = @imagecreatefrompng($from); 1578 1579 }elseif($ext == 'gif') { 1580 if(!function_exists("imagecreatefromgif")) return false; 1581 $image = @imagecreatefromgif($from); 1582 } 1583 if(!$image) return false; 1584 1585 if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){ 1586 $newimg = @imagecreatetruecolor ($to_w, $to_h); 1587 } 1588 if(!$newimg) $newimg = @imagecreate($to_w, $to_h); 1589 if(!$newimg){ 1590 imagedestroy($image); 1591 return false; 1592 } 1593 1594 //keep png alpha channel if possible 1595 if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){ 1596 imagealphablending($newimg, false); 1597 imagesavealpha($newimg,true); 1598 } 1599 1600 //keep gif transparent color if possible 1601 if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) { 1602 if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) { 1603 $transcolorindex = @imagecolortransparent($image); 1604 if($transcolorindex >= 0 ) { //transparent color exists 1605 $transcolor = @imagecolorsforindex($image, $transcolorindex); 1606 $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']); 1607 @imagefill($newimg, 0, 0, $transcolorindex); 1608 @imagecolortransparent($newimg, $transcolorindex); 1609 }else{ //filling with white 1610 $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255); 1611 @imagefill($newimg, 0, 0, $whitecolorindex); 1612 } 1613 }else{ //filling with white 1614 $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255); 1615 @imagefill($newimg, 0, 0, $whitecolorindex); 1616 } 1617 } 1618 1619 //try resampling first 1620 if(function_exists("imagecopyresampled")){ 1621 if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) { 1622 imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h); 1623 } 1624 }else{ 1625 imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h); 1626 } 1627 1628 $okay = false; 1629 if ($ext == 'jpg' || $ext == 'jpeg'){ 1630 if(!function_exists('imagejpeg')){ 1631 $okay = false; 1632 }else{ 1633 $okay = imagejpeg($newimg, $to, $conf['jpg_quality']); 1634 } 1635 }elseif($ext == 'png') { 1636 if(!function_exists('imagepng')){ 1637 $okay = false; 1638 }else{ 1639 $okay = imagepng($newimg, $to); 1640 } 1641 }elseif($ext == 'gif') { 1642 if(!function_exists('imagegif')){ 1643 $okay = false; 1644 }else{ 1645 $okay = imagegif($newimg, $to); 1646 } 1647 } 1648 1649 // destroy GD image ressources 1650 if($image) imagedestroy($image); 1651 if($newimg) imagedestroy($newimg); 1652 1653 return $okay; 1654} 1655 1656/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 1657