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