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