[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZDir class 4 // 5 // Created on: <02-Jul-2002 15:33:41 sp> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \file ezdir.php 30 */ 31 32 /*! 33 \class eZDir ezdir.php 34 \brief The class eZDir does 35 36 */ 37 include_once ( "lib/ezutils/classes/ezini.php" ); 38 include_once ( "lib/ezutils/classes/ezsys.php" ); 39 40 define( 'EZ_DIR_SEPARATOR_LOCAL', 1 ); 41 define( 'EZ_DIR_SEPARATOR_UNIX', 2 ); 42 define( 'EZ_DIR_SEPARATOR_DOS', 3 ); 43 44 class eZDir 45 { 46 /*! 47 Constructor 48 */ 49 function eZDir() 50 { 51 } 52 53 54 /*! 55 \return a multi-level path from a specific key. For example: 56 \code 57 echo createMultiLevelPath( "42abce", 3 ); 58 \endcode 59 returns "/4/2/abce" 60 61 Parameters: 62 $key: the key to be used as path 63 $maxDepth: the maximum number of path elements to be created (-1 is unlimited) 64 \static 65 */ 66 function createMultiLevelPath( $key, $maxDepth = -1 ) 67 { 68 $parts = preg_split("//", (string) $key, $maxDepth, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 69 $sep = eZDir::separator( EZ_DIR_SEPARATOR_LOCAL ); 70 return $sep . join($sep, $parts); 71 } 72 73 function getPathFromFilename( $filename ) 74 { 75 $ini =& eZINI::instance(); 76 $dirDepth = $ini->variable( "FileSettings" , "DirDepth" ); 77 $pathArray = array(); 78 for ( $i = 0; $i < $dirDepth and $i < strlen( $filename ); $i++ ) 79 { 80 $pathArray[] = substr( $filename, $i, 1 ); 81 } 82 $path = implode( '/', $pathArray ); 83 84 return $path; 85 } 86 87 function filenamePath( $filename, $maxCharLen = 2 ) 88 { 89 $path = ''; 90 for ( $i = 0; $i < strlen( $filename ) and ( strlen( $filename ) - $i ) > $maxCharLen; 91 $i++ ) 92 { 93 $path = $path . substr( $filename, $i, 1 ) . '/'; 94 } 95 96 return $path; 97 } 98 99 /*! 100 \static 101 Creates the directory \a $dir with permissions \a $perm. 102 If \a $parents is true it will create any missing parent directories, 103 just like 'mkdir -p'. 104 */ 105 function mkdir( $dir, $perm = false, $parents = false ) 106 { 107 if ( $perm === false ) 108 { 109 $perm = eZDir::directoryPermission(); 110 } 111 $dir = eZDir::cleanPath( $dir, EZ_DIR_SEPARATOR_UNIX ); 112 if ( !$parents ) 113 return eZDir::doMkdir( $dir, $perm ); 114 else 115 { 116 $dirElements = explode( '/', $dir ); 117 if ( count( $dirElements ) == 0 ) 118 return true; 119 $currentDir = $dirElements[0]; 120 $result = true; 121 if ( !file_exists( $currentDir ) and $currentDir != "" ) 122 $result = eZDir::doMkdir( $currentDir, $perm ); 123 if ( !$result ) 124 return false; 125 126 for ( $i = 1; $i < count( $dirElements ); ++$i ) 127 { 128 $dirElement = $dirElements[$i]; 129 if ( strlen( $dirElement ) == 0 ) 130 continue; 131 $currentDir .= '/' . $dirElement; 132 $result = true; 133 if ( !file_exists( $currentDir ) ) 134 $result = eZDir::doMkdir( $currentDir, $perm ); 135 if ( !$result ) 136 return false; 137 } 138 return true; 139 } 140 } 141 142 /*! 143 \static 144 Goes trough the directory path \a $dir and removes empty directories. 145 \note This is just the opposite of mkdir() with \a $parents set to \c true. 146 */ 147 function cleanupEmptyDirectories( $dir ) 148 { 149 $dir = eZDir::cleanPath( $dir, EZ_DIR_SEPARATOR_UNIX ); 150 $dirElements = explode( '/', $dir ); 151 if ( count( $dirElements ) == 0 ) 152 return true; 153 $currentDir = $dirElements[0]; 154 $result = true; 155 if ( !file_exists( $currentDir ) and $currentDir != "" ) 156 $result = eZDir::doMkdir( $currentDir, $perm ); 157 if ( !$result ) 158 return false; 159 160 for ( $i = count( $dirElements ); $i > 0; --$i ) 161 { 162 $dirpath = implode( '/', array_slice( $dirElements, 0, $i ) ); 163 if ( file_exists( $dirpath ) and 164 is_dir( $dirpath ) ) 165 { 166 $rmdirStatus = @rmdir( $dirpath ); 167 if ( !$rmdirStatus ) 168 return true; 169 } 170 } 171 return true; 172 } 173 174 /*! 175 \return the dirpath portion of the filepath \a $filepath. 176 \code 177 $dirpath = eZDir::dirpath( "path/to/some/file.txt" ); 178 print( $dirpath ); // prints out path/to/some 179 \endcode 180 */ 181 function dirpath( $filepath ) 182 { 183 $filepath = eZDir::cleanPath( $filepath, EZ_DIR_SEPARATOR_UNIX ); 184 $dirPosition = strrpos( $filepath, '/' ); 185 if ( $dirPosition !== false ) 186 return substr( $filepath, 0, $dirPosition ); 187 return $filepath; 188 } 189 190 /*! 191 \return the default permissions to use for directories. 192 \note The permission is converted from octal text to decimal value. 193 */ 194 function directoryPermission() 195 { 196 $ini =& eZINI::instance(); 197 return octdec( $ini->variable( 'FileSettings', 'StorageDirPermissions' ) ); 198 } 199 200 /*! 201 \static 202 \private 203 Creates the directory \a $dir with permission \a $perm. 204 */ 205 function doMkdir( $dir, $perm ) 206 { 207 include_once ( "lib/ezutils/classes/ezdebugsetting.php" ); 208 209 $oldumask = umask( 0 ); 210 if ( ! @mkdir( $dir, $perm ) ) 211 { 212 umask( $oldumask ); 213 // eZDebug::writeError( "Couldn't create the directory \"$dir\".", "eZDir::doMkdir()" ); 214 return false; 215 } 216 umask( $oldumask ); 217 218 return true; 219 } 220 221 /*! 222 \static 223 \return the separator used between directories and files according to \a $type. 224 225 Type can be one of the following: 226 - EZ_DIR_SEPARATOR_LOCAL - Returns whatever is applicable for the current machine. 227 - EZ_DIR_SEPARATOR_UNIX - Returns a / 228 - EZ_DIR_SEPARATOR_DOS - Returns a \ 229 */ 230 function separator( $type ) 231 { 232 switch ( $type ) 233 { 234 case EZ_DIR_SEPARATOR_LOCAL: 235 return eZSys::fileSeparator(); 236 case EZ_DIR_SEPARATOR_UNIX: 237 return '/'; 238 case EZ_DIR_SEPARATOR_DOS: 239 return "\\"; 240 } 241 return null; 242 } 243 244 /*! 245 \static 246 Converts any directory separators found in \a $path, in both unix and dos style, into 247 the separator type specified by \a $toType and returns it. 248 */ 249 function convertSeparators( $path, $toType = EZ_DIR_SEPARATOR_UNIX ) 250 { 251 $separator = eZDir::separator( $toType ); 252 return str_replace( array( '/', '\\' ), $separator, $path ); 253 } 254 255 /*! 256 \static 257 Removes all unneeded directory separators and resolves any "."s and ".."s found in \a $path. 258 259 For instance: "var/../lib/ezdb" becomes "lib/ezdb", while "../site/var" will not be changed. 260 \note Will also convert separators 261 \sa convertSeparators. 262 */ 263 function cleanPath( $path, $toType = EZ_DIR_SEPARATOR_UNIX ) 264 { 265 $path = eZDir::convertSeparators( $path, $toType ); 266 $separator = eZDir::separator( $toType ); 267 $path = preg_replace( "#$separator{2,}#", $separator, $path ); 268 $pathElements = explode( $separator, $path ); 269 $newPathElements = array(); 270 foreach ( $pathElements as $pathElement ) 271 { 272 if ( $pathElement == '.' ) 273 continue; 274 if ( $pathElement == '..' and 275 count( $newPathElements ) > 0 ) 276 array_pop( $newPathElements ); 277 else 278 $newPathElements[] = $pathElement; 279 } 280 if ( count( $newPathElements ) == 0 ) 281 $newPathElements[] = '.'; 282 $path = implode( $separator, $newPathElements ); 283 return $path; 284 } 285 286 /*! 287 \static 288 Creates a path out of all the dir and file items in the array \a $names 289 with correct separators in between them. 290 It will also remove unneeded separators. 291 \a $type is used to determine the separator type, see eZDir::separator. 292 If \a $includeEndSeparator is true then it will make sure that the path ends with a 293 separator if false it make sure there are no end separator. 294 */ 295 function path( $names, $includeEndSeparator = false, $type = EZ_DIR_SEPARATOR_UNIX ) 296 { 297 $separator = eZDir::separator( $type ); 298 $path = implode( $separator, $names ); 299 $path = eZDir::cleanPath( $path, $type ); 300 $pathLen = strlen( $path ); 301 $hasEndSeparator = ( $pathLen > 0 and 302 $path[$pathLen - 1] == $separator ); 303 if ( $includeEndSeparator and 304 !$hasEndSeparator ) 305 $path .= $separator; 306 else if ( !$includeEndSeparator and 307 $hasEndSeparator ) 308 $path = substr( $path, 0, $pathLen - 1 ); 309 return $path; 310 } 311 312 313 /*! 314 \static 315 Removes the directory and all it's contents, recursive. 316 */ 317 function recursiveDelete( $dir ) 318 { 319 if ( $handle = @opendir( $dir ) ) 320 { 321 while ( ( $file = readdir( $handle ) ) !== false ) 322 { 323 if ( ( $file == "." ) || ( $file == ".." ) ) 324 { 325 continue; 326 } 327 if ( is_dir( $dir . '/' . $file ) ) 328 { 329 eZDir::recursiveDelete( $dir . '/' . $file ); 330 } 331 else 332 { 333 unlink( $dir . '/' . $file ); 334 } 335 } 336 @closedir( $handle ); 337 rmdir( $dir ); 338 } 339 } 340 341 /*! 342 \static 343 Creates a list of all files and dirs in the directory. 344 */ 345 function recursiveList( $dir, $path, &$fileList ) 346 { 347 if ( $handle = @opendir( $dir ) ) 348 { 349 while ( ( $file = readdir( $handle ) ) !== false ) 350 { 351 if ( ( $file == "." ) || ( $file == ".." ) ) 352 { 353 continue; 354 } 355 if ( is_dir( $dir . '/' . $file ) ) 356 { 357 $fileList[] = array( 'path' => $path, 'name' => $file, 'type' => 'dir' ); 358 eZDir::recursiveList( $dir . '/' . $file, $path . '/' . $file, $fileList ); 359 } 360 else 361 { 362 $fileList[] = array( 'path' => $path, 'name' => $file, 'type' => 'file' ); 363 } 364 } 365 @closedir( $handle ); 366 } 367 } 368 369 /*! 370 \static 371 Recurses through the directory and returns the files that matches the given suffix 372 \note This function will not traverse . (hidden) folders 373 */ 374 function recursiveFind( $dir, $suffix ) 375 { 376 $returnFiles = array(); 377 if ( $handle = @opendir( $dir ) ) 378 { 379 while ( ( $file = readdir( $handle ) ) !== false ) 380 { 381 if ( ( $file == "." ) || ( $file == ".." ) ) 382 { 383 continue; 384 } 385 if ( is_dir( $dir . '/' . $file ) ) 386 { 387 if ( $file[0] != "." ) 388 { 389 $files = eZDir::recursiveFind( $dir . '/' . $file, $suffix ); 390 $returnFiles = array_merge( $files, $returnFiles ); 391 } 392 } 393 else 394 { 395 if ( preg_match( "/$suffix$/", $file ) ) 396 $returnFiles[] = $dir . '/' . $file; 397 } 398 } 399 @closedir( $handle ); 400 } 401 return $returnFiles; 402 } 403 404 /*! 405 \static 406 Unlink files match the given pattern in the given directory. 407 */ 408 function unlinkWildcard( $dir, $pattern ) 409 { 410 $availableFiles = array(); 411 if ( $handle = @opendir( $dir ) ) 412 { 413 while ( ( $file = readdir( $handle ) ) !== false ) 414 { 415 if ( $file != "." && $file != ".." ) 416 { 417 $availableFiles[] = $file; 418 } 419 } 420 @closedir( $handle ); 421 422 if( strpos( $pattern, "." ) ) 423 { 424 $baseexp = substr( $pattern, 0, strpos( $pattern, "." ) ); 425 $typeexp = substr( $pattern, ( strpos( $pattern, "." ) + 1 ), strlen( $pattern ) ); 426 } 427 else 428 { 429 $baseexp = $pattern; 430 $typeexp = ""; 431 } 432 433 $baseexp=preg_quote( $baseexp ); 434 $typeexp=preg_quote( $typeexp ); 435 436 $baseexp = str_replace( array( "\*", "\?" ), array( ".*", "." ), $baseexp ); 437 $typeexp = str_replace(array( "\*", "\?" ), array( ".*", "." ), $typeexp ); 438 439 $i=0; 440 $matchedFileArray = array(); 441 foreach( $availableFiles as $file ) 442 { 443 $fileName = basename( $file ); 444 445 if( strpos( $fileName, "." ) ) 446 { 447 $base = substr( $fileName, 0, strpos( $fileName, ".")); 448 $type = substr( $fileName, ( strpos( $fileName,"." ) + 1 ), strlen( $fileName ) ); 449 } 450 else 451 { 452 $base = $fileName; 453 $type = ""; 454 } 455 456 if( preg_match( "/^".$baseexp."$/i", $base ) && preg_match( "/^".$typeexp."$/i", $type ) ) 457 { 458 $matchedFileArray[$i] = $file; 459 $i++; 460 } 461 } 462 463 foreach ( array_keys( $matchedFileArray ) as $key ) 464 { 465 $matchedFile =& $matchedFileArray[$key]; 466 if ( substr( $dir,-1 ) == "/") 467 { 468 unlink( $dir.$matchedFile ); 469 } 470 else 471 { 472 unlink( $dir."/".$matchedFile ); 473 } 474 } 475 } 476 } 477 478 /*! 479 \static 480 Recurses through the directory and returns the files that matches the given suffix. 481 This function will store the relative path from the given base only. 482 Note: this function will not traverse . (hidden) folders 483 */ 484 function recursiveFindRelative( $baseDir, $subDir, $suffix ) 485 { 486 $returnFiles = array(); 487 $dir = $baseDir; 488 if ( $subDir != "" ) 489 { 490 if ( $dir != '' ) 491 $dir .= "/" . $subDir; 492 else 493 $dir .= $subDir; 494 } 495 if ( $handle = @opendir( $dir ) ) 496 { 497 while ( ( $file = readdir( $handle ) ) !== false ) 498 { 499 if ( ( $file == "." ) || ( $file == ".." ) ) 500 { 501 continue; 502 } 503 if ( is_dir( $dir . '/' . $file ) ) 504 { 505 if ( $file[0] != "." ) 506 { 507 $files = eZDir::recursiveFindRelative( $baseDir, $subDir . '/' . $file, $suffix ); 508 $returnFiles = array_merge( $files, $returnFiles ); 509 } 510 } 511 else 512 { 513 if ( preg_match( "/$suffix$/", $file ) ) 514 $returnFiles[] = $subDir . '/' . $file; 515 } 516 } 517 @closedir( $handle ); 518 } 519 return $returnFiles; 520 } 521 522 /*! 523 \static 524 Returns all subdirectories in a folder 525 */ 526 function findSubdirs( $dir, $includeHidden = false, $excludeItems = false ) 527 { 528 return eZDir::findSubitems( $dir, 'd', false, $includeHidden, $excludeItems ); 529 } 530 531 /*! 532 \static 533 Returns all subdirectories in a folder 534 */ 535 function findSubitems( $dir, $types = false, $fullPath = false, $includeHidden = false, $excludeItems = false ) 536 { 537 if ( !$types ) 538 $types = 'dfl'; 539 $dirArray = array(); 540 if ( $handle = @opendir( $dir ) ) 541 { 542 while ( ( $element = readdir( $handle ) ) !== false ) 543 { 544 if ( $element == '.' or $element == '..' ) 545 continue; 546 if ( !$includeHidden and $element[0] == "." ) 547 continue; 548 if ( $excludeItems and preg_match( $excludeItems, $element ) ) 549 continue; 550 if ( @is_dir( $dir . '/' . $element ) and strpos( $types, 'd' ) === false ) 551 continue; 552 if ( @is_link( $dir . '/' . $element ) and strpos( $types, 'l' ) === false ) 553 continue; 554 if ( @is_file( $dir . '/' . $element ) and strpos( $types, 'f' ) === false ) 555 continue; 556 if ( $fullPath ) 557 { 558 if ( is_string( $fullPath ) ) 559 $dirArray[] = $fullPath . '/' . $element; 560 else 561 $dirArray[] = $dir . '/' . $element; 562 } 563 else 564 $dirArray[] = $element; 565 } 566 @closedir( $handle ); 567 } 568 return $dirArray; 569 } 570 571 /*! 572 Copies a directory (and optionally all it's subitems) to another directory. 573 \param $sourceDirectory The source directory which should be copied, this location must exist. 574 \param $destinationDirectory The location for the copied directory structure, this location must exist. 575 This parameter will be modified if \a $asChild is \c true. 576 \param If \c true then it will use last part of the \a $sourceDirectory as a sub-folder to \a $destinationDirectory. 577 e.g. copying /etc/httpd to /var/ will create /var/httpd and place all folders/files under it. 578 \param $recursive If \c true then it will copy folders/files recursively from folders found in \a $sourceDirectory. 579 \param $includeHidden If \c true it will include files or folders beginning with a dot (.). 580 \param $excludeItems A regular expression used to exclude files or folders in the subtree, use \c false for no exclusion. 581 582 \note The parameter \a $recursive is currently unused, it will always copy recursively. 583 */ 584 function copy( $sourceDirectory, &$destinationDirectory, 585 $asChild = true, $recursive = true, $includeHidden = false, $excludeItems = false ) 586 { 587 if ( !is_dir( $sourceDirectory ) ) 588 { 589 eZDebug::writeError( "Source $sourceDirectory is not a directory, cannot copy from it", 590 'eZDir::copy' ); 591 return false; 592 } 593 if ( !is_dir( $destinationDirectory ) ) 594 { 595 eZDebug::writeError( "Destination $destinationDirectory is not a directory, cannot copy to it", 596 'eZDir::copy' ); 597 return false; 598 } 599 if ( $asChild ) 600 { 601 if ( preg_match( "#^.+/([^/]+)$#", $sourceDirectory, $matches ) ) 602 { 603 eZDir::mkdir( $destinationDirectory . '/' . $matches[1], eZDir::directoryPermission(), false ); 604 $destinationDirectory .= '/' . $matches[1]; 605 } 606 } 607 $items = eZDir::findSubitems( $sourceDirectory, 'df', false, $includeHidden, $excludeItems ); 608 $totalItems = $items; 609 include_once ( 'lib/ezfile/classes/ezfilehandler.php' ); 610 while ( count( $items ) > 0 ) 611 { 612 $currentItems = $items; 613 $items = array(); 614 foreach ( $currentItems as $item ) 615 { 616 $fullPath = $sourceDirectory . '/' . $item; 617 if ( is_file( $fullPath ) ) 618 eZFileHandler::copy( $fullPath, $destinationDirectory . '/' . $item ); 619 else if ( is_dir( $fullPath ) ) 620 { 621 eZDir::mkdir( $destinationDirectory . '/' . $item, eZDir::directoryPermission(), false ); 622 $newItems = eZDir::findSubitems( $fullPath, 'df', $item, $includeHidden, $excludeItems ); 623 $items = array_merge( $items, $newItems ); 624 $totalItems = array_merge( $totalItems, $newItems ); 625 unset( $newItems ); 626 } 627 } 628 } 629 // eZDebugSetting::writeNotice( 'lib-ezfile-copy', 630 // "Copied directory $sourceDirectory to destination $destinationDirectory", 631 // 'eZDir::copy' ); 632 return $totalItems; 633 } 634 635 /*! 636 \return a regexp which will match certain temporary files. 637 */ 638 function temporaryFileRegexp( $standalone = true ) 639 { 640 $preg = ''; 641 if ( $standalone ) 642 $preg .= "/^"; 643 $preg .= "(.*~|#.+#|.*\.bak|.svn|CVS|.revive.el|.cvsignore)"; 644 if ( $standalone ) 645 $preg .= "$/"; 646 return $preg; 647 } 648 649 /*! 650 \static 651 Check if a given directory is writeable 652 653 \return TRUE/FALSE 654 */ 655 function isWriteable( $dirname ) 656 { 657 if ( eZSys::osType() != 'win32' ) 658 return is_writable( $dirname ); 659 660 /* PHP function is_writable() doesn't work correctly on Windows NT descendants. 661 * So we have to use the following hack on those OSes. 662 * FIXME: maybe on Win9x we shouldn't do this? 663 */ 664 $tmpfname = $dirname . eZSys::fileSeparator() . "ezsetup_" . md5( microtime() ) . ".tmp"; 665 666 // try to create temporary file 667 if ( !( $fp = @fopen( $tmpfname, "w" ) ) ) 668 return FALSE; 669 670 fclose( $fp ); 671 unlink( $tmpfname ); 672 673 return TRUE; 674 } 675 } 676 677 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |