00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 include_once( "lib/ezutils/classes/ezini.php" );
00040 include_once( "lib/ezutils/classes/ezsys.php" );
00041
00042 define( 'EZ_DIR_SEPARATOR_LOCAL', 1 );
00043 define( 'EZ_DIR_SEPARATOR_UNIX', 2 );
00044 define( 'EZ_DIR_SEPARATOR_DOS', 3 );
00045
00046 class eZDir
00047 {
00048
00049
00050
00051 function eZDir()
00052 {
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 function createMultiLevelPath( $key, $maxDepth = -1 )
00069 {
00070 $parts = preg_split("//", (string) $key, $maxDepth, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
00071 $sep = eZDir::separator( EZ_DIR_SEPARATOR_LOCAL );
00072 return $sep . join($sep, $parts);
00073 }
00074
00075 function getPathFromFilename( $filename )
00076 {
00077 $ini =& eZINI::instance();
00078 $dirDepth = $ini->variable( "FileSettings" , "DirDepth" );
00079 $pathArray = array();
00080 for ( $i = 0; $i < $dirDepth and $i < strlen( $filename ); $i++ )
00081 {
00082 $pathArray[] = substr( $filename, $i, 1 );
00083 }
00084 $path = implode( '/', $pathArray );
00085
00086 return $path;
00087 }
00088
00089 function filenamePath( $filename, $maxCharLen = 2 )
00090 {
00091 $path = '';
00092 for ( $i = 0; $i < strlen( $filename ) and ( strlen( $filename ) - $i ) > $maxCharLen;
00093 $i++ )
00094 {
00095 $path = $path . substr( $filename, $i, 1 ) . '/';
00096 }
00097
00098 return $path;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 function mkdir( $dir, $perm = false, $parents = false )
00108 {
00109 if ( $perm === false )
00110 {
00111 $perm = eZDir::directoryPermission();
00112 }
00113 $dir = eZDir::cleanPath( $dir, EZ_DIR_SEPARATOR_UNIX );
00114 if ( !$parents )
00115 return eZDir::doMkdir( $dir, $perm );
00116 else
00117 {
00118 $dirElements = explode( '/', $dir );
00119 if ( count( $dirElements ) == 0 )
00120 return true;
00121 $currentDir = $dirElements[0];
00122 $result = true;
00123 if ( !file_exists( $currentDir ) and $currentDir != "" )
00124 $result = eZDir::doMkdir( $currentDir, $perm );
00125 if ( !$result )
00126 return false;
00127
00128 for ( $i = 1; $i < count( $dirElements ); ++$i )
00129 {
00130 $dirElement = $dirElements[$i];
00131 if ( strlen( $dirElement ) == 0 )
00132 continue;
00133 $currentDir .= '/' . $dirElement;
00134 $result = true;
00135 if ( !file_exists( $currentDir ) )
00136 $result = eZDir::doMkdir( $currentDir, $perm );
00137 if ( !$result )
00138 return false;
00139 }
00140 return true;
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149 function cleanupEmptyDirectories( $dir )
00150 {
00151 $dir = eZDir::cleanPath( $dir, EZ_DIR_SEPARATOR_UNIX );
00152 $dirElements = explode( '/', $dir );
00153 if ( count( $dirElements ) == 0 )
00154 return true;
00155 $currentDir = $dirElements[0];
00156 $result = true;
00157 if ( !file_exists( $currentDir ) and $currentDir != "" )
00158 $result = eZDir::doMkdir( $currentDir, $perm );
00159 if ( !$result )
00160 return false;
00161
00162 for ( $i = count( $dirElements ); $i > 0; --$i )
00163 {
00164 $dirpath = implode( '/', array_slice( $dirElements, 0, $i ) );
00165 if ( file_exists( $dirpath ) and
00166 is_dir( $dirpath ) )
00167 {
00168 $rmdirStatus = @rmdir( $dirpath );
00169 if ( !$rmdirStatus )
00170 return true;
00171 }
00172 }
00173 return true;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 function dirpath( $filepath )
00184 {
00185 $filepath = eZDir::cleanPath( $filepath, EZ_DIR_SEPARATOR_UNIX );
00186 $dirPosition = strrpos( $filepath, '/' );
00187 if ( $dirPosition !== false )
00188 return substr( $filepath, 0, $dirPosition );
00189 return $filepath;
00190 }
00191
00192
00193
00194
00195
00196 function directoryPermission()
00197 {
00198 $ini =& eZINI::instance();
00199 return octdec( $ini->variable( 'FileSettings', 'StorageDirPermissions' ) );
00200 }
00201
00202
00203
00204
00205
00206
00207 function doMkdir( $dir, $perm )
00208 {
00209 include_once( "lib/ezutils/classes/ezdebugsetting.php" );
00210
00211 $oldumask = umask( 0 );
00212 if ( ! @mkdir( $dir, $perm ) )
00213 {
00214 umask( $oldumask );
00215
00216 return false;
00217 }
00218 umask( $oldumask );
00219
00220 return true;
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 function separator( $type )
00233 {
00234 switch ( $type )
00235 {
00236 case EZ_DIR_SEPARATOR_LOCAL:
00237 return eZSys::fileSeparator();
00238 case EZ_DIR_SEPARATOR_UNIX:
00239 return '/';
00240 case EZ_DIR_SEPARATOR_DOS:
00241 return "\\";
00242 }
00243 return null;
00244 }
00245
00246
00247
00248
00249
00250
00251 function convertSeparators( $path, $toType = EZ_DIR_SEPARATOR_UNIX )
00252 {
00253 $separator = eZDir::separator( $toType );
00254 return str_replace( array( '/', '\\' ), $separator, $path );
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 function cleanPath( $path, $toType = EZ_DIR_SEPARATOR_UNIX )
00266 {
00267 $path = eZDir::convertSeparators( $path, $toType );
00268 $separator = eZDir::separator( $toType );
00269 $path = preg_replace( "#$separator{2,}#", $separator, $path );
00270 $pathElements = explode( $separator, $path );
00271 $newPathElements = array();
00272 foreach ( $pathElements as $pathElement )
00273 {
00274 if ( $pathElement == '.' )
00275 continue;
00276 if ( $pathElement == '..' and
00277 count( $newPathElements ) > 0 )
00278 array_pop( $newPathElements );
00279 else
00280 $newPathElements[] = $pathElement;
00281 }
00282 if ( count( $newPathElements ) == 0 )
00283 $newPathElements[] = '.';
00284 $path = implode( $separator, $newPathElements );
00285 return $path;
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 function path( $names, $includeEndSeparator = false, $type = EZ_DIR_SEPARATOR_UNIX )
00298 {
00299 $separator = eZDir::separator( $type );
00300 $path = implode( $separator, $names );
00301 $path = eZDir::cleanPath( $path, $type );
00302 $pathLen = strlen( $path );
00303 $hasEndSeparator = ( $pathLen > 0 and
00304 $path[$pathLen - 1] == $separator );
00305 if ( $includeEndSeparator and
00306 !$hasEndSeparator )
00307 $path .= $separator;
00308 else if ( !$includeEndSeparator and
00309 $hasEndSeparator )
00310 $path = substr( $path, 0, $pathLen - 1 );
00311 return $path;
00312 }
00313
00314
00315
00316
00317
00318
00319 function recursiveDelete( $dir )
00320 {
00321 if ( $handle = @opendir( $dir ) )
00322 {
00323 while ( ( $file = readdir( $handle ) ) !== false )
00324 {
00325 if ( ( $file == "." ) || ( $file == ".." ) )
00326 {
00327 continue;
00328 }
00329 if ( is_dir( $dir . '/' . $file ) )
00330 {
00331 eZDir::recursiveDelete( $dir . '/' . $file );
00332 }
00333 else
00334 {
00335 unlink( $dir . '/' . $file );
00336 }
00337 }
00338 @closedir( $handle );
00339 rmdir( $dir );
00340 }
00341 }
00342
00343
00344
00345
00346
00347 function recursiveList( $dir, $path, &$fileList )
00348 {
00349 if ( $handle = @opendir( $dir ) )
00350 {
00351 while ( ( $file = readdir( $handle ) ) !== false )
00352 {
00353 if ( ( $file == "." ) || ( $file == ".." ) )
00354 {
00355 continue;
00356 }
00357 if ( is_dir( $dir . '/' . $file ) )
00358 {
00359 $fileList[] = array( 'path' => $path, 'name' => $file, 'type' => 'dir' );
00360 eZDir::recursiveList( $dir . '/' . $file, $path . '/' . $file, $fileList );
00361 }
00362 else
00363 {
00364 $fileList[] = array( 'path' => $path, 'name' => $file, 'type' => 'file' );
00365 }
00366 }
00367 @closedir( $handle );
00368 }
00369 }
00370
00371
00372
00373
00374
00375
00376 function recursiveFind( $dir, $suffix )
00377 {
00378 $returnFiles = array();
00379 if ( $handle = @opendir( $dir ) )
00380 {
00381 while ( ( $file = readdir( $handle ) ) !== false )
00382 {
00383 if ( ( $file == "." ) || ( $file == ".." ) )
00384 {
00385 continue;
00386 }
00387 if ( is_dir( $dir . '/' . $file ) )
00388 {
00389 if ( $file[0] != "." )
00390 {
00391 $files = eZDir::recursiveFind( $dir . '/' . $file, $suffix );
00392 $returnFiles = array_merge( $files, $returnFiles );
00393 }
00394 }
00395 else
00396 {
00397 if ( preg_match( "/$suffix$/", $file ) )
00398 $returnFiles[] = $dir . '/' . $file;
00399 }
00400 }
00401 @closedir( $handle );
00402 }
00403 return $returnFiles;
00404 }
00405
00406
00407
00408
00409
00410 function unlinkWildcard( $dir, $pattern )
00411 {
00412 $availableFiles = array();
00413 if ( $handle = @opendir( $dir ) )
00414 {
00415 while ( ( $file = readdir( $handle ) ) !== false )
00416 {
00417 if ( $file != "." && $file != ".." )
00418 {
00419 $availableFiles[] = $file;
00420 }
00421 }
00422 @closedir( $handle );
00423
00424 if( strpos( $pattern, "." ) )
00425 {
00426 $baseexp = substr( $pattern, 0, strpos( $pattern, "." ) );
00427 $typeexp = substr( $pattern, ( strpos( $pattern, "." ) + 1 ), strlen( $pattern ) );
00428 }
00429 else
00430 {
00431 $baseexp = $pattern;
00432 $typeexp = "";
00433 }
00434
00435 $baseexp=preg_quote( $baseexp );
00436 $typeexp=preg_quote( $typeexp );
00437
00438 $baseexp = str_replace( array( "\*", "\?" ), array( ".*", "." ), $baseexp );
00439 $typeexp = str_replace(array( "\*", "\?" ), array( ".*", "." ), $typeexp );
00440
00441 $i=0;
00442 $matchedFileArray = array();
00443 foreach( $availableFiles as $file )
00444 {
00445 $fileName = basename( $file );
00446
00447 if( strpos( $fileName, "." ) )
00448 {
00449 $base = substr( $fileName, 0, strpos( $fileName, "."));
00450 $type = substr( $fileName, ( strpos( $fileName,"." ) + 1 ), strlen( $fileName ) );
00451 }
00452 else
00453 {
00454 $base = $fileName;
00455 $type = "";
00456 }
00457
00458 if( preg_match( "/^".$baseexp."$/i", $base ) && preg_match( "/^".$typeexp."$/i", $type ) )
00459 {
00460 $matchedFileArray[$i] = $file;
00461 $i++;
00462 }
00463 }
00464
00465 foreach ( array_keys( $matchedFileArray ) as $key )
00466 {
00467 $matchedFile =& $matchedFileArray[$key];
00468 if ( substr( $dir,-1 ) == "/")
00469 {
00470 unlink( $dir.$matchedFile );
00471 }
00472 else
00473 {
00474 unlink( $dir."/".$matchedFile );
00475 }
00476 }
00477 }
00478 }
00479
00480
00481
00482
00483
00484
00485
00486 function recursiveFindRelative( $baseDir, $subDir, $suffix )
00487 {
00488 $returnFiles = array();
00489 $dir = $baseDir;
00490 if ( $subDir != "" )
00491 {
00492 if ( $dir != '' )
00493 $dir .= "/" . $subDir;
00494 else
00495 $dir .= $subDir;
00496 }
00497 if ( $handle = @opendir( $dir ) )
00498 {
00499 while ( ( $file = readdir( $handle ) ) !== false )
00500 {
00501 if ( ( $file == "." ) || ( $file == ".." ) )
00502 {
00503 continue;
00504 }
00505 if ( is_dir( $dir . '/' . $file ) )
00506 {
00507 if ( $file[0] != "." )
00508 {
00509 $files = eZDir::recursiveFindRelative( $baseDir, $subDir . '/' . $file, $suffix );
00510 $returnFiles = array_merge( $files, $returnFiles );
00511 }
00512 }
00513 else
00514 {
00515 if ( preg_match( "/$suffix$/", $file ) )
00516 $returnFiles[] = $subDir . '/' . $file;
00517 }
00518 }
00519 @closedir( $handle );
00520 }
00521 return $returnFiles;
00522 }
00523
00524
00525
00526
00527
00528 function findSubdirs( $dir, $includeHidden = false, $excludeItems = false )
00529 {
00530 return eZDir::findSubitems( $dir, 'd', false, $includeHidden, $excludeItems );
00531 }
00532
00533
00534
00535
00536
00537 function findSubitems( $dir, $types = false, $fullPath = false, $includeHidden = false, $excludeItems = false )
00538 {
00539 if ( !$types )
00540 $types = 'dfl';
00541 $dirArray = array();
00542 if ( $handle = @opendir( $dir ) )
00543 {
00544 while ( ( $element = readdir( $handle ) ) !== false )
00545 {
00546 if ( $element == '.' or $element == '..' )
00547 continue;
00548 if ( !$includeHidden and $element[0] == "." )
00549 continue;
00550 if ( $excludeItems and preg_match( $excludeItems, $element ) )
00551 continue;
00552 if ( @is_dir( $dir . '/' . $element ) and strpos( $types, 'd' ) === false )
00553 continue;
00554 if ( @is_link( $dir . '/' . $element ) and strpos( $types, 'l' ) === false )
00555 continue;
00556 if ( @is_file( $dir . '/' . $element ) and strpos( $types, 'f' ) === false )
00557 continue;
00558 if ( $fullPath )
00559 {
00560 if ( is_string( $fullPath ) )
00561 $dirArray[] = $fullPath . '/' . $element;
00562 else
00563 $dirArray[] = $dir . '/' . $element;
00564 }
00565 else
00566 $dirArray[] = $element;
00567 }
00568 @closedir( $handle );
00569 }
00570 return $dirArray;
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586 function copy( $sourceDirectory, &$destinationDirectory,
00587 $asChild = true, $recursive = true, $includeHidden = false, $excludeItems = false )
00588 {
00589 if ( !is_dir( $sourceDirectory ) )
00590 {
00591 eZDebug::writeError( "Source $sourceDirectory is not a directory, cannot copy from it",
00592 'eZDir::copy' );
00593 return false;
00594 }
00595 if ( !is_dir( $destinationDirectory ) )
00596 {
00597 eZDebug::writeError( "Destination $destinationDirectory is not a directory, cannot copy to it",
00598 'eZDir::copy' );
00599 return false;
00600 }
00601 if ( $asChild )
00602 {
00603 if ( preg_match( "#^.+/([^/]+)$#", $sourceDirectory, $matches ) )
00604 {
00605 eZDir::mkdir( $destinationDirectory . '/' . $matches[1], eZDir::directoryPermission(), false );
00606 $destinationDirectory .= '/' . $matches[1];
00607 }
00608 }
00609 $items = eZDir::findSubitems( $sourceDirectory, 'df', false, $includeHidden, $excludeItems );
00610 $totalItems = $items;
00611 include_once( 'lib/ezfile/classes/ezfilehandler.php' );
00612 while ( count( $items ) > 0 )
00613 {
00614 $currentItems = $items;
00615 $items = array();
00616 foreach ( $currentItems as $item )
00617 {
00618 $fullPath = $sourceDirectory . '/' . $item;
00619 if ( is_file( $fullPath ) )
00620 eZFileHandler::copy( $fullPath, $destinationDirectory . '/' . $item );
00621 else if ( is_dir( $fullPath ) )
00622 {
00623 eZDir::mkdir( $destinationDirectory . '/' . $item, eZDir::directoryPermission(), false );
00624 $newItems = eZDir::findSubitems( $fullPath, 'df', $item, $includeHidden, $excludeItems );
00625 $items = array_merge( $items, $newItems );
00626 $totalItems = array_merge( $totalItems, $newItems );
00627 unset( $newItems );
00628 }
00629 }
00630 }
00631
00632
00633
00634 return $totalItems;
00635 }
00636
00637
00638
00639
00640 function temporaryFileRegexp( $standalone = true )
00641 {
00642 $preg = '';
00643 if ( $standalone )
00644 $preg .= "/^";
00645 $preg .= "(.*~|#.+#|.*\.bak|.svn|CVS|.revive.el|.cvsignore)";
00646 if ( $standalone )
00647 $preg .= "$/";
00648 return $preg;
00649 }
00650
00651
00652
00653
00654
00655
00656
00657 function isWriteable( $dirname )
00658 {
00659 if ( eZSys::osType() != 'win32' )
00660 return is_writable( $dirname );
00661
00662
00663
00664
00665
00666 $tmpfname = $dirname . eZSys::fileSeparator() . "ezsetup_" . md5( microtime() ) . ".tmp";
00667
00668
00669 if ( !( $fp = @fopen( $tmpfname, "w" ) ) )
00670 return FALSE;
00671
00672 fclose( $fp );
00673 unlink( $tmpfname );
00674
00675 return TRUE;
00676 }
00677 }
00678
00679 ?>