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