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