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