[ Index ] |
|
Code source de CMS made simple 1.0.5 |
1 <?php 2 /** 3 * ImageManager, list images, directories, and thumbnails. 4 * @author $Author: Wei Zhuo $ 5 * @version $Id: ImageManager.php 2608 2006-02-19 20:09:34Z patricia $ 6 * @package ImageManager 7 */ 8 9 require_once(dirname(__FILE__).'/Files.php'); 10 11 /** 12 * ImageManager Class. 13 * @author $Author: Wei Zhuo $ 14 * @version $Id: ImageManager.php 2608 2006-02-19 20:09:34Z patricia $ 15 */ 16 class ImageManager 17 { 18 /** 19 * Configuration array. 20 */ 21 var $config; 22 23 /** 24 * Array of directory information. 25 */ 26 var $dirs; 27 28 /** 29 * Constructor. Create a new Image Manager instance. 30 * @param array $config configuration array, see config.inc.php 31 */ 32 function ImageManager($config) 33 { 34 $this->config = $config; 35 } 36 37 /** 38 * Get the base directory. 39 * @return string base dir, see config.inc.php 40 */ 41 function getBaseDir() 42 { 43 Return $this->config['base_dir']; 44 } 45 46 /** 47 * Get the base URL. 48 * @return string base url, see config.inc.php 49 */ 50 function getBaseURL() 51 { 52 Return $this->config['base_url']; 53 } 54 55 function isValidBase() 56 { 57 return is_dir($this->getBaseDir()); 58 } 59 60 /** 61 * Get the tmp file prefix. 62 * @return string tmp file prefix. 63 */ 64 function getTmpPrefix() 65 { 66 Return $this->config['tmp_prefix']; 67 } 68 69 /** 70 * Get the sub directories in the base dir. 71 * Each array element contain 72 * the relative path (relative to the base dir) as key and the 73 * full path as value. 74 * @return array of sub directries 75 * <code>array('path name' => 'full directory path', ...)</code> 76 */ 77 function getDirs() 78 { 79 if(is_null($this->dirs)) 80 { 81 $dirs = $this->_dirs($this->getBaseDir(),'/'); 82 ksort($dirs); 83 $this->dirs = $dirs; 84 } 85 return $this->dirs; 86 } 87 88 /** 89 * Recursively travese the directories to get a list 90 * of accessable directories. 91 * @param string $base the full path to the current directory 92 * @param string $path the relative path name 93 * @return array of accessiable sub-directories 94 * <code>array('path name' => 'full directory path', ...)</code> 95 */ 96 function _dirs($base, $path) 97 { 98 $base = Files::fixPath($base); 99 $dirs = array(); 100 101 if($this->isValidBase() == false) 102 return $dirs; 103 104 $d = @dir($base); 105 106 while (false !== ($entry = $d->read())) 107 { 108 //If it is a directory, and it doesn't start with 109 // a dot, and if is it not the thumbnail directory 110 if(is_dir($base.$entry) 111 && substr($entry,0,1) != '.' 112 && $this->isThumbDir($entry) == false) 113 { 114 $relative = Files::fixPath($path.$entry); 115 $fullpath = Files::fixPath($base.$entry); 116 $dirs[$relative] = $fullpath; 117 $dirs = array_merge($dirs, $this->_dirs($fullpath, $relative)); 118 } 119 } 120 $d->close(); 121 122 Return $dirs; 123 } 124 125 /** 126 * Get all the files and directories of a relative path. 127 * @param string $path relative path to be base path. 128 * @return array of file and path information. 129 * <code>array(0=>array('relative'=>'fullpath',...), 1=>array('filename'=>fileinfo array(),...)</code> 130 * fileinfo array: <code>array('url'=>'full url', 131 * 'relative'=>'relative to base', 132 * 'fullpath'=>'full file path', 133 * 'image'=>imageInfo array() false if not image, 134 * 'stat' => filestat)</code> 135 */ 136 function getFiles($path) 137 { 138 $files = array(); 139 $dirs = array(); 140 141 if($this->isValidBase() == false) 142 return array($files,$dirs); 143 144 145 $path = Files::fixPath($path); 146 $base = Files::fixPath($this->getBaseDir()); 147 $fullpath = Files::makePath($base,$path); 148 149 150 151 $d = @dir($fullpath); 152 153 while (false !== ($entry = $d->read())) 154 { 155 156 157 //not a dot file or directory 158 if(substr($entry,0,1) != '.') 159 { 160 if(is_dir($fullpath.$entry) 161 && $this->isThumbDir($entry) == false) 162 { 163 $relative = Files::fixPath($path.$entry); 164 $full = Files::fixPath($fullpath.$entry); 165 $count = $this->countFiles($full); 166 $dirs[$relative] = array('fullpath'=>$full,'entry'=>$entry,'count'=>$count); 167 } 168 else if(is_file($fullpath.$entry) && $this->isThumb($entry)==false && $this->isTmpFile($entry) == false) 169 { 170 $img = $this->getImageInfo($fullpath.$entry); 171 172 if(!(!is_array($img)&&$this->config['validate_images'])) 173 { 174 $file['url'] = Files::makePath($this->config['base_url'],$path).$entry; 175 $file['relative'] = $path.$entry; 176 $file['fullpath'] = $fullpath.$entry; 177 $file['image'] = $img; 178 $file['stat'] = stat($fullpath.$entry); 179 $files[$entry] = $file; 180 } 181 } 182 } 183 } 184 $d->close(); 185 186 //Add a back directory 187 188 $backpath = $fullpath.'/..'; 189 $backpath = realpath($backpath); 190 if (substr($backpath,0,strlen($this->getBaseDir())) == $this->getBaseDir()) 191 { 192 $backpath = substr($backpath,strlen($this->getBaseDir())); 193 } 194 echo '<p class="pageheader">'.$path.'</p>'; 195 if($path != '/') { 196 $relative = Files::fixPath($backpath); 197 $full = Files::fixPath($backpath); 198 $count = $this->countFiles($full); 199 $entry = '..'; 200 $dirs[$relative] = array('fullpath'=>$full,'entry'=>$entry,'count'=>$count); 201 } 202 203 ksort($dirs); 204 ksort($files); 205 206 Return array($dirs, $files); 207 } 208 209 /** 210 * Count the number of files and directories in a given folder 211 * minus the thumbnail folders and thumbnails. 212 */ 213 function countFiles($path) 214 { 215 $total = 0; 216 217 if(($path != "/") && (is_dir($path))) 218 { 219 $d = @dir($path); 220 221 while (false !== ($entry = $d->read())) 222 { 223 //echo $entry."<br>"; 224 if(substr($entry,0,1) != '.' 225 && $this->isThumbDir($entry) == false 226 && $this->isTmpFile($entry) == false 227 && $this->isThumb($entry) == false) 228 { 229 $total++; 230 } 231 } 232 $d->close(); 233 } 234 return $total; 235 } 236 237 /** 238 * Get image size information. 239 * @param string $file the image file 240 * @return array of getImageSize information, 241 * false if the file is not an image. 242 */ 243 function getImageInfo($file) 244 { 245 Return @getImageSize($file); 246 } 247 248 /** 249 * Check if the file contains the thumbnail prefix. 250 * @param string $file filename to be checked 251 * @return true if the file contains the thumbnail prefix, false otherwise. 252 */ 253 function isThumb($file) 254 { 255 $len = strlen($this->config['thumbnail_prefix']); 256 if(substr($file,0,$len)==$this->config['thumbnail_prefix']) 257 Return true; 258 else 259 Return false; 260 } 261 262 /** 263 * Check if the given directory is a thumbnail directory. 264 * @param string $entry directory name 265 * @return true if it is a thumbnail directory, false otherwise 266 */ 267 function isThumbDir($entry) 268 { 269 if($this->config['thumbnail_dir'] == false 270 || strlen(trim($this->config['thumbnail_dir'])) == 0) 271 Return false; 272 else 273 Return ($entry == $this->config['thumbnail_dir']); 274 } 275 276 /** 277 * Check if the given file is a tmp file. 278 * @param string $file file name 279 * @return boolean true if it is a tmp file, false otherwise 280 */ 281 function isTmpFile($file) 282 { 283 $len = strlen($this->config['tmp_prefix']); 284 if(substr($file,0,$len)==$this->config['tmp_prefix']) 285 Return true; 286 else 287 Return false; 288 } 289 290 /** 291 * For a given image file, get the respective thumbnail filename 292 * no file existence check is done. 293 * @param string $fullpathfile the full path to the image file 294 * @return string of the thumbnail file 295 */ 296 function getThumbName($fullpathfile) 297 { 298 $path_parts = pathinfo($fullpathfile); 299 300 $thumbnail = $this->config['thumbnail_prefix'].$path_parts['basename']; 301 302 if($this->config['safe_mode'] == true 303 || strlen(trim($this->config['thumbnail_dir'])) == 0) 304 { 305 306 Return Files::makeFile($path_parts['dirname'],$thumbnail); 307 } 308 else 309 { 310 if(strlen(trim($this->config['thumbnail_dir'])) > 0) 311 { 312 $path = Files::makePath($path_parts['dirname'],$this->config['thumbnail_dir']); 313 if(!is_dir($path)) 314 Files::createFolder($path); 315 Return Files::makeFile($path,$thumbnail); 316 } 317 else //should this ever happen? 318 { 319 //error_log('ImageManager: Error in creating thumbnail name'); 320 } 321 } 322 } 323 324 /** 325 * Similar to getThumbName, but returns the URL, base on the 326 * given base_url in config.inc.php 327 * @param string $relative the relative image file name, 328 * relative to the base_dir path 329 * @return string the url of the thumbnail 330 */ 331 function getThumbURL($relative) 332 { 333 $path_parts = pathinfo($relative); 334 $thumbnail = $this->config['thumbnail_prefix'].$path_parts['basename']; 335 if($path_parts['dirname']=='\\') $path_parts['dirname']='/'; 336 337 if($this->config['safe_mode'] == true 338 || strlen(trim($this->config['thumbnail_dir'])) == 0) 339 { 340 341 $path = Files::makePath($path_parts['dirname'],''); 342 $url_path = Files::makePath($this->getBaseURL(), $path); 343 Return Files::makeFile($url_path,$thumbnail); 344 // Return Files::makeFile($this->getBaseURL(),$thumbnail); 345 } 346 else 347 { 348 if(strlen(trim($this->config['thumbnail_dir'])) > 0) 349 { 350 351 $path = Files::makePath($path_parts['dirname'],$this->config['thumbnail_dir']); 352 $url_path = Files::makePath($this->getBaseURL(), $path); 353 Return Files::makeFile($url_path,$thumbnail); 354 } 355 else //should this ever happen? 356 { 357 //error_log('ImageManager: Error in creating thumbnail url'); 358 } 359 360 } 361 } 362 363 /** 364 * Check if the given path is part of the subdirectories 365 * under the base_dir. 366 * @param string $path the relative path to be checked 367 * @return boolean true if the path exists, false otherwise 368 */ 369 function validRelativePath($path) 370 { 371 $dirs = $this->getDirs(); 372 if($path == '/') 373 Return true; 374 //check the path given in the url against the 375 //list of paths in the system. 376 for($i = 0; $i < count($dirs); $i++) 377 { 378 $key = key($dirs); 379 //we found the path 380 if($key == $path) 381 Return true; 382 383 next($dirs); 384 } 385 Return false; 386 } 387 388 /** 389 * Process uploaded files, assumes the file is in 390 * $_FILES['upload'] and $_POST['dir'] is set. 391 * The dir must be relative to the base_dir and exists. 392 * If 'validate_images' is set to true, only file with 393 * image dimensions will be accepted. 394 * @return null 395 */ 396 function processUploads() 397 { 398 if($this->isValidBase() == false) 399 return; 400 401 $relative = null; 402 403 if(isset($_POST['dir'])) 404 $relative = rawurldecode($_POST['dir']); 405 else 406 return; 407 408 //check for the file, and must have valid relative path 409 if(isset($_FILES['upload']) && $this->validRelativePath($relative)) 410 { 411 $this->_processFiles($relative, $_FILES['upload']); 412 } 413 } 414 415 /** 416 * Process upload files. The file must be an 417 * uploaded file. If 'validate_images' is set to 418 * true, only images will be processed. Any duplicate 419 * file will be renamed. See Files::copyFile for details 420 * on renaming. 421 * @param string $relative the relative path where the file 422 * should be copied to. 423 * @param array $file the uploaded file from $_FILES 424 * @return boolean true if the file was processed successfully, 425 * false otherwise 426 */ 427 function _processFiles($relative, $file) 428 { 429 430 if($file['error']!=0) 431 { 432 Return false; 433 } 434 435 if(!is_file($file['tmp_name'])) 436 { 437 Return false; 438 } 439 440 if(!is_uploaded_file($file['tmp_name'])) 441 { 442 Files::delFile($file['tmp_name']); 443 Return false; 444 } 445 446 447 if($this->config['validate_images'] == true) 448 { 449 $imgInfo = @getImageSize($file['tmp_name']); 450 if(!is_array($imgInfo)) 451 { 452 Files::delFile($file['tmp_name']); 453 Return false; 454 } 455 } 456 457 //now copy the file 458 $path = Files::makePath($this->getBaseDir(),$relative); 459 460 $filespec = preg_replace('/\.php/','',$file['name']); 461 $result = Files::copyFile($file['tmp_name'], $path, $filespec); 462 463 //no copy error 464 if(!is_int($result)) 465 { 466 Files::delFile($file['tmp_name']); 467 Return true; 468 } 469 470 //delete tmp files. 471 Files::delFile($file['tmp_name']); 472 Return false; 473 } 474 475 /** 476 * Get the URL of the relative file. 477 * basically appends the relative file to the 478 * base_url given in config.inc.php 479 * @param string $relative a file the relative to the base_dir 480 * @return string the URL of the relative file. 481 */ 482 function getFileURL($relative) 483 { 484 Return Files::makeFile($this->getBaseURL(),$relative); 485 } 486 487 /** 488 * Get the fullpath to a relative file. 489 * @param string $relative the relative file. 490 * @return string the full path, .ie. the base_dir + relative. 491 */ 492 function getFullPath($relative) 493 { 494 Return Files::makeFile($this->getBaseDir(),$relative);; 495 } 496 497 /** 498 * Get the default thumbnail. 499 * @return string default thumbnail, empty string if 500 * the thumbnail doesn't exist. 501 */ 502 function getDefaultThumb() 503 { 504 if(is_file($this->config['default_thumbnail'])) 505 Return $this->config['default_thumbnail']; 506 else 507 Return ''; 508 } 509 510 511 /** 512 * Get the thumbnail url to be displayed. 513 * If the thumbnail exists, and it is up-to-date 514 * the thumbnail url will be returns. If the 515 * file is not an image, a default image will be returned. 516 * If it is an image file, and no thumbnail exists or 517 * the thumbnail is out-of-date (i.e. the thumbnail 518 * modified time is less than the original file) 519 * then a thumbs.php?img=filename.jpg is returned. 520 * The thumbs.php url will generate a new thumbnail 521 * on the fly. If the image is less than the dimensions 522 * of the thumbnails, the image will be display instead. 523 * @param string $relative the relative image file. 524 * @return string the url of the thumbnail, be it 525 * actually thumbnail or a script to generate the 526 * thumbnail on the fly. 527 */ 528 function getThumbnail($relative) 529 { 530 $fullpath = Files::makeFile($this->getBaseDir(),$relative); 531 //not a file??? 532 if(!is_file($fullpath)) 533 Return $this->getDefaultThumb(); 534 535 $imgInfo = @getImageSize($fullpath); 536 537 //not an image 538 if(!is_array($imgInfo)) 539 Return $this->getDefaultThumb(); 540 541 $thumbnail = $this->getThumbName($fullpath); 542 543 //check for thumbnails, if exists and 544 // it is up-to-date, return the thumbnail url 545 if(is_file($thumbnail)) 546 { 547 if(filemtime($thumbnail) >= filemtime($fullpath)) 548 Return $this->getThumbURL($relative); 549 } 550 551 //well, no thumbnail was found, so ask the thumbs.php 552 //to generate the thumbnail on the fly. 553 Return 'thumbs.php?img='.rawurlencode($relative); 554 } 555 556 /** 557 * Delete and specified files. 558 * @return boolean true if delete, false otherwise 559 */ 560 function deleteFiles() 561 { 562 if(isset($_GET['delf'])) 563 $this->_delFile(rawurldecode($_GET['delf'])); 564 } 565 566 /** 567 * Delete and specified directories. 568 * @return boolean true if delete, false otherwise 569 */ 570 function deleteDirs() 571 { 572 if(isset($_GET['deld'])) 573 return $this->_delDir(rawurldecode($_GET['deld'])); 574 else 575 Return false; 576 } 577 578 /** 579 * Delete the relative file, and any thumbnails. 580 * @param string $relative the relative file. 581 * @return boolean true if deleted, false otherwise. 582 */ 583 function _delFile($relative) 584 { 585 $fullpath = Files::makeFile($this->getBaseDir(),$relative); 586 587 //check that the file is an image 588 if($this->config['validate_images'] == true) 589 { 590 if(!is_array($this->getImageInfo($fullpath))) 591 return false; //hmmm not an Image!!??? 592 } 593 594 $thumbnail = $this->getThumbName($fullpath); 595 596 if(Files::delFile($fullpath)) 597 Return Files::delFile($thumbnail); 598 else 599 Return false; 600 } 601 602 /** 603 * Delete directories recursively. 604 * @param string $relative the relative path to be deleted. 605 * @return boolean true if deleted, false otherwise. 606 */ 607 function _delDir($relative) 608 { 609 $fullpath = Files::makePath($this->getBaseDir(),$relative); 610 if($this->countFiles($fullpath) <= 0) 611 return Files::delFolder($fullpath,true); //delete recursively. 612 else 613 Return false; 614 } 615 616 /** 617 * Create new directories. 618 * If in safe_mode, nothing happens. 619 * @return boolean true if created, false otherwise. 620 */ 621 function processNewDir() 622 { 623 if($this->config['safe_mode'] == true) 624 Return false; 625 626 if(isset($_GET['newDir']) && isset($_GET['dir'])) 627 { 628 $newDir = rawurldecode($_GET['newDir']); 629 $dir = rawurldecode($_GET['dir']); 630 $path = Files::makePath($this->getBaseDir(),$dir); 631 $fullpath = Files::makePath($path, Files::escape($newDir)); 632 if(is_dir($fullpath)) 633 Return false; 634 635 Return Files::createFolder($fullpath); 636 } 637 } 638 639 /** 640 * Do some graphic library method checkings 641 * @param string $library the graphics library, GD, NetPBM, or IM. 642 * @param string $method the method to check. 643 * @return boolean true if able, false otherwise. 644 */ 645 function validGraphicMethods($library,$method) 646 { 647 switch ($library) 648 { 649 case 'GD': 650 return $this->_checkGDLibrary($method); 651 break; 652 case 'NetPBM': 653 return $this->_checkNetPBMLibrary($method); 654 break; 655 case 'IM': 656 return $this->_checkIMLibrary($method); 657 } 658 return false; 659 } 660 661 function _checkIMLibrary($method) 662 { 663 //ImageMagick goes throught 1 single executable 664 if(is_file(Files::fixPath(IMAGE_TRANSFORM_LIB_PATH).'convert')) 665 return true; 666 else 667 return false; 668 } 669 670 /** 671 * Check the GD library functionality. 672 * @param string $library the graphics library, GD, NetPBM, or IM. 673 * @return boolean true if able, false otherwise. 674 */ 675 function _checkGDLibrary($method) 676 { 677 $errors = array(); 678 switch($method) 679 { 680 case 'create': 681 $errors['createjpeg'] = function_exists('imagecreatefromjpeg'); 682 $errors['creategif'] = function_exists('imagecreatefromgif'); 683 $errors['createpng'] = function_exists('imagecreatefrompng'); 684 break; 685 case 'modify': 686 $errors['create'] = function_exists('ImageCreateTrueColor') || function_exists('ImageCreate'); 687 $errors['copy'] = function_exists('ImageCopyResampled') || function_exists('ImageCopyResized'); 688 break; 689 case 'save': 690 $errors['savejpeg'] = function_exists('imagejpeg'); 691 $errors['savegif'] = function_exists('imagegif'); 692 $errors['savepng'] = function_exists('imagepng'); 693 break; 694 } 695 696 return $errors; 697 } 698 } 699 700 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Apr 3 18:50:37 2007 | par Balluche grâce à PHPXref 0.7 |