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