Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdir.php
Go to the documentation of this file.
1 <?php
16 class eZDir
17 {
18 const SEPARATOR_LOCAL = 1;
19 const SEPARATOR_UNIX = 2;
20 const SEPARATOR_DOS = 3;
21
34 static function createMultiLevelPath( $key, $maxDepth = -1 )
35 {
36 $parts = preg_split("//", (string) $key, $maxDepth, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
37 $sep = eZDir::separator( self::SEPARATOR_LOCAL );
38 return $sep . join($sep, $parts);
39 }
40
41 static function getPathFromFilename( $filename )
42 {
43 $ini = eZINI::instance();
44 $dirDepth = $ini->variable( "FileSettings" , "DirDepth" );
45 $pathArray = array();
46 for ( $i = 0; $i < $dirDepth and $i < strlen( $filename ); $i++ )
47 {
48 $pathArray[] = substr( $filename, $i, 1 );
49 }
50 $path = implode( '/', $pathArray );
51
52 return $path;
53 }
54
55 static function filenamePath( $filename, $maxCharLen = 2 )
56 {
57 $path = '';
58 for ( $i = 0; $i < strlen( $filename ) and ( strlen( $filename ) - $i ) > $maxCharLen;
59 $i++ )
60 {
61 $path = $path . substr( $filename, $i, 1 ) . '/';
62 }
63
64 return $path;
65 }
66
73 static function mkdir( $dir, $perm = false, $recursive = false )
74 {
75 if ( file_exists( $dir ) )
76 return false;
77
78 if ( $perm === false )
79 {
80 $perm = eZDir::directoryPermission();
81 }
82 $dir = eZDir::cleanPath( $dir, self::SEPARATOR_UNIX );
83
84 $oldumask = umask( 0 );
85 $success = @mkdir( $dir, $perm, $recursive );
86 umask( $oldumask );
87 return $success;
88 }
89
95 static function cleanupEmptyDirectories( $dir )
96 {
97 $dir = self::cleanPath( $dir, self::SEPARATOR_UNIX );
98 $dirElements = explode( '/', $dir );
99 if ( count( $dirElements ) == 0 )
100 return true;
101 $currentDir = $dirElements[0];
102 $result = true;
103 if ( !file_exists( $currentDir ) and $currentDir != "" )
104 $result = self::doMkdir( $currentDir, self::directoryPermission() );
105 if ( !$result )
106 return false;
107
108 for ( $i = count( $dirElements ); $i > 0; --$i )
109 {
110 $dirpath = implode( '/', array_slice( $dirElements, 0, $i ) );
111 if ( file_exists( $dirpath ) and
112 is_dir( $dirpath ) )
113 {
114 $rmdirStatus = @rmdir( $dirpath );
115 if ( !$rmdirStatus )
116 return true;
117 }
118 }
119 return true;
120 }
121
129 static function dirpath( $filepath )
130 {
131 $filepath = eZDir::cleanPath( $filepath, self::SEPARATOR_UNIX );
132 $dirPosition = strrpos( $filepath, '/' );
133 if ( $dirPosition !== false )
134 return substr( $filepath, 0, $dirPosition );
135 return $filepath;
136 }
137
142 static function directoryPermission()
143 {
144 return octdec( eZINI::instance()->variable( 'FileSettings', 'StorageDirPermissions' ) );
145 }
146
152 static function doMkdir( $dir, $perm, $recursive = false )
153 {
154 $oldumask = umask( 0 );
155 $success = @mkdir( $dir, $perm, $recursive );
156 umask( $oldumask );
157 return $success;
158 }
159
169 static function separator( $type )
170 {
171 switch ( $type )
172 {
173 case self::SEPARATOR_LOCAL:
174 return eZSys::fileSeparator();
175 case self::SEPARATOR_UNIX:
176 return '/';
177 case self::SEPARATOR_DOS:
178 return "\\";
179 }
180 return null;
181 }
182
188 static function convertSeparators( $path, $toType = self::SEPARATOR_UNIX )
189 {
190 $separator = eZDir::separator( $toType );
191 return str_replace( array( '/', '\\' ), $separator, $path );
192 }
193
202 static function cleanPath( $path, $toType = self::SEPARATOR_UNIX )
203 {
204 $path = eZDir::convertSeparators( $path, $toType );
205 $separator = eZDir::separator( $toType );
206 if ( strpos( $path, $separator . $separator ) !== false )
207 {
208 $path = preg_replace( "#$separator{2,}#", $separator, $path );
209 }
210 $pathElements = explode( $separator, $path );
211 $newPathElements = array();
212 foreach ( $pathElements as $pathElement )
213 {
214 if ( $pathElement === '.' )
215 continue;
216 if ( $pathElement === '..' and
217 count( $newPathElements ) > 0 )
218 array_pop( $newPathElements );
219 else
220 $newPathElements[] = $pathElement;
221 }
222 if ( !isset( $newPathElements[0] ) )
223 $newPathElements[] = '.';
224 $path = implode( $separator, $newPathElements );
225 return $path;
226 }
227
237 static function path( $names, $includeEndSeparator = false, $type = self::SEPARATOR_UNIX )
238 {
239 $separator = eZDir::separator( $type );
240 $path = implode( $separator, $names );
241 $path = eZDir::cleanPath( $path, $type );
242 $pathLen = strlen( $path );
243 $hasEndSeparator = ( $pathLen > 0 and
244 $path[$pathLen - 1] == $separator );
245 if ( $includeEndSeparator and
246 !$hasEndSeparator )
247 $path .= $separator;
248 else if ( !$includeEndSeparator &&
249 $hasEndSeparator &&
250 $pathLen > 1 )
251 $path = substr( $path, 0, $pathLen - 1 );
252 return $path;
253 }
254
255
264 static function recursiveDelete( $dir, $rootCheck = true )
265 {
266 // RecursiveDelete fails if ...
267 // $dir is not a directory
268 if ( !is_dir( $dir ) )
269 {
270 eZDebug::writeError( "The path: $dir is not a folder" , __METHOD__ );
271 return false;
272 }
273
274 if ( $rootCheck )
275 {
276 // rootCheck is enabled, check if $dir is part of authorized directories
277 $allowedDirs = eZINI::instance()->variable( 'FileSettings', 'AllowedDeletionDirs' );
278 // Also adding eZ Publish root dir.
279 $rootDir = eZSys::rootDir() . DIRECTORY_SEPARATOR;
280 array_unshift( $allowedDirs, $rootDir );
281
282 $dirRealPath = dirname( realpath( $dir ) ) . DIRECTORY_SEPARATOR;
283 $canDelete = false;
284 foreach ( $allowedDirs as $allowedDir )
285 {
286 if ( strpos( $dirRealPath, realpath( $allowedDir ) ) === 0 )
287 {
288 $canDelete = true;
289 break;
290 }
291 }
292
293 if ( !$canDelete )
294 {
295 eZDebug::writeError(
296 "Recursive delete denied for '$dir' as its realpath '$dirRealPath' is outside eZ Publish root and not registered in AllowedDeletionDirs."
297 );
298 return false;
299 }
300 }
301
302 // the directory cannot be opened
303 if ( ! ( $handle = @opendir( $dir ) ) )
304 {
305 eZDebug::writeError( "Cannot access folder:". dirname( $dir) , __METHOD__ );
306 return false;
307 }
308
309 while ( ( $file = readdir( $handle ) ) !== false )
310 {
311 if ( ( $file == "." ) || ( $file == ".." ) )
312 {
313 continue;
314 }
315 if ( is_dir( $dir . '/' . $file ) )
316 {
317 eZDir::recursiveDelete( $dir . '/' . $file, false );
318 }
319 else
320 {
321 unlink( $dir . '/' . $file );
322 }
323 }
324 @closedir( $handle );
325 return rmdir( $dir );
326 }
327
332 static function recursiveList( $dir, $path, &$fileList )
333 {
334 if ( $handle = @opendir( $dir ) )
335 {
336 while ( ( $file = readdir( $handle ) ) !== false )
337 {
338 if ( ( $file == "." ) || ( $file == ".." ) )
339 {
340 continue;
341 }
342 if ( is_dir( $dir . '/' . $file ) )
343 {
344 $fileList[] = array( 'path' => $path, 'name' => $file, 'type' => 'dir' );
345 eZDir::recursiveList( $dir . '/' . $file, $path . '/' . $file, $fileList );
346 }
347 else
348 {
349 $fileList[] = array( 'path' => $path, 'name' => $file, 'type' => 'file' );
350 }
351 }
352 @closedir( $handle );
353 }
354 }
355
361 static function recursiveFind( $dir, $suffix )
362 {
363 $returnFiles = array();
364 if ( $handle = @opendir( $dir ) )
365 {
366 while ( ( $file = readdir( $handle ) ) !== false )
367 {
368 if ( ( $file == "." ) || ( $file == ".." ) )
369 {
370 continue;
371 }
372 if ( is_dir( $dir . '/' . $file ) )
373 {
374 if ( $file[0] != "." )
375 {
376 $files = eZDir::recursiveFind( $dir . '/' . $file, $suffix );
377 $returnFiles = array_merge( $files, $returnFiles );
378 }
379 }
380 else
381 {
382 if ( preg_match( "/$suffix$/", $file ) )
383 $returnFiles[] = $dir . '/' . $file;
384 }
385 }
386 @closedir( $handle );
387 }
388 return $returnFiles;
389 }
390
395 static function unlinkWildcard( $dir, $pattern )
396 {
397 $availableFiles = array();
398 if ( $handle = @opendir( $dir ) )
399 {
400 while ( ( $file = readdir( $handle ) ) !== false )
401 {
402 if ( $file != "." && $file != ".." )
403 {
404 $availableFiles[] = $file;
405 }
406 }
407 @closedir( $handle );
408
409 if( strpos( $pattern, "." ) )
410 {
411 $baseexp = substr( $pattern, 0, strpos( $pattern, "." ) );
412 $typeexp = substr( $pattern, ( strpos( $pattern, "." ) + 1 ), strlen( $pattern ) );
413 }
414 else
415 {
416 $baseexp = $pattern;
417 $typeexp = "";
418 }
419
420 $baseexp=preg_quote( $baseexp );
421 $typeexp=preg_quote( $typeexp );
422
423 $baseexp = str_replace( array( "\*", "\?" ), array( ".*", "." ), $baseexp );
424 $typeexp = str_replace(array( "\*", "\?" ), array( ".*", "." ), $typeexp );
425
426 $i=0;
427 $matchedFileArray = array();
428 foreach( $availableFiles as $file )
429 {
430 $fileName = basename( $file );
431
432 if( strpos( $fileName, "." ) )
433 {
434 $base = substr( $fileName, 0, strpos( $fileName, "."));
435 $type = substr( $fileName, ( strpos( $fileName,"." ) + 1 ), strlen( $fileName ) );
436 }
437 else
438 {
439 $base = $fileName;
440 $type = "";
441 }
442
443 if( preg_match( "/^".$baseexp."$/i", $base ) && preg_match( "/^".$typeexp."$/i", $type ) )
444 {
445 $matchedFileArray[$i] = $file;
446 $i++;
447 }
448 }
449
450 foreach ( array_keys( $matchedFileArray ) as $key )
451 {
452 $matchedFile =& $matchedFileArray[$key];
453 if ( substr( $dir,-1 ) == "/")
454 {
455 unlink( $dir.$matchedFile );
456 }
457 else
458 {
459 unlink( $dir."/".$matchedFile );
460 }
461 }
462 }
463 }
464
471 static function recursiveFindRelative( $baseDir, $subDir, $suffix )
472 {
473 $dir = $baseDir;
474 if ( $subDir != "" )
475 {
476 if ( $dir != '' )
477 $dir .= "/" . $subDir;
478 else
479 $dir .= $subDir;
480 }
481
482 if ( !is_dir( $dir ) )
483 {
484 return array();
485 }
486
487 $returnFiles = array();
488 if ( $handle = @opendir( $dir ) )
489 {
490 while ( ( $file = readdir( $handle ) ) !== false )
491 {
492 if ( ( $file == "." ) || ( $file == ".." ) )
493 {
494 continue;
495 }
496 if ( is_dir( $dir . '/' . $file ) )
497 {
498 if ( $file[0] != "." )
499 {
500 $files = eZDir::recursiveFindRelative( $baseDir, $subDir . '/' . $file, $suffix );
501 $returnFiles = array_merge( $files, $returnFiles );
502 }
503 }
504 else
505 {
506 if ( preg_match( "/$suffix$/", $file ) )
507 $returnFiles[] = $subDir . '/' . $file;
508 }
509 }
510 @closedir( $handle );
511 }
512 return $returnFiles;
513 }
514
519 static function findSubdirs( $dir, $includeHidden = false, $excludeItems = false )
520 {
521 return eZDir::findSubitems( $dir, 'd', false, $includeHidden, $excludeItems );
522 }
523
528 static function findSubitems( $dir, $types = false, $fullPath = false, $includeHidden = false, $excludeItems = false )
529 {
530 if ( !$types )
531 $types = 'dfl';
532 $dirArray = array();
533 if ( $handle = @opendir( $dir ) )
534 {
535 while ( ( $element = readdir( $handle ) ) !== false )
536 {
537 if ( $element === '.' || $element === '..' )
538 continue;
539 if ( !$includeHidden && $element[0] === '.' )
540 continue;
541 if ( $excludeItems && preg_match( $excludeItems, $element ) )
542 continue;
543 if ( strpos( $types, 'd' ) === false && is_dir( $dir . '/' . $element ) )
544 continue;
545 if ( strpos( $types, 'l' ) === false && is_link( $dir . '/' . $element ) )
546 continue;
547 if ( strpos( $types, 'f' ) === false && is_file( $dir . '/' . $element ))
548 continue;
549 if ( $fullPath )
550 {
551 if ( is_string( $fullPath ) )
552 $dirArray[] = $fullPath . '/' . $element;
553 else
554 $dirArray[] = $dir . '/' . $element;
555 }
556 else
557 $dirArray[] = $element;
558 }
559 @closedir( $handle );
560 }
561 return $dirArray;
562 }
563
577 static function copy( $sourceDirectory, &$destinationDirectory,
578 $asChild = true, $recursive = true, $includeHidden = false, $excludeItems = false )
579 {
580 if ( !is_dir( $sourceDirectory ) )
581 {
582 eZDebug::writeError( "Source $sourceDirectory is not a directory, cannot copy from it", __METHOD__ );
583 return false;
584 }
585 if ( !is_dir( $destinationDirectory ) )
586 {
587 eZDebug::writeError( "Destination $destinationDirectory is not a directory, cannot copy to it", __METHOD__ );
588 return false;
589 }
590 if ( $asChild )
591 {
592 if ( preg_match( "#^.+/([^/]+)$#", $sourceDirectory, $matches ) )
593 {
594 eZDir::mkdir( $destinationDirectory . '/' . $matches[1], eZDir::directoryPermission(), false );
595 $destinationDirectory .= '/' . $matches[1];
596 }
597 }
598 $items = eZDir::findSubitems( $sourceDirectory, 'df', false, $includeHidden, $excludeItems );
599 $totalItems = $items;
600 while ( count( $items ) > 0 )
601 {
602 $currentItems = $items;
603 $items = array();
604 foreach ( $currentItems as $item )
605 {
606 $fullPath = $sourceDirectory . '/' . $item;
607 if ( is_file( $fullPath ) )
608 eZFileHandler::copy( $fullPath, $destinationDirectory . '/' . $item );
609 else if ( is_dir( $fullPath ) )
610 {
611 eZDir::mkdir( $destinationDirectory . '/' . $item, eZDir::directoryPermission(), false );
612 $newItems = eZDir::findSubitems( $fullPath, 'df', $item, $includeHidden, $excludeItems );
613 $items = array_merge( $items, $newItems );
614 $totalItems = array_merge( $totalItems, $newItems );
615 unset( $newItems );
616 }
617 }
618 }
619 // eZDebugSetting::writeNotice( 'lib-ezfile-copy',
620 // "Copied directory $sourceDirectory to destination $destinationDirectory",
621 // 'eZDir::copy' );
622 return $totalItems;
623 }
624
628 static function temporaryFileRegexp( $standalone = true )
629 {
630 $preg = '';
631 if ( $standalone )
632 $preg .= "/^";
633 $preg .= "(.*~|#.+#|.*\.bak|.svn|CVS|.revive.el|.cvsignore)";
634 if ( $standalone )
635 $preg .= "$/";
636 return $preg;
637 }
638
645 static function isWriteable( $dirname )
646 {
647 if ( eZSys::osType() != 'win32' )
648 return is_writable( $dirname );
649
650 /* PHP function is_writable() doesn't work correctly on Windows NT descendants.
651 * So we have to use the following hack on those OSes.
652 */
653 $tmpfname = $dirname . eZSys::fileSeparator() . "ezsetup_" . md5( microtime() ) . ".tmp";
654
655 // try to create temporary file
656 if ( !( $fp = @fopen( $tmpfname, "w" ) ) )
657 return FALSE;
658
659 fclose( $fp );
660 unlink( $tmpfname );
661
662 return TRUE;
663 }
664 }
665
666 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDir
The class eZDir does.
Definition: ezdir.php:17
eZDir\createMultiLevelPath
static createMultiLevelPath( $key, $maxDepth=-1)
Definition: ezdir.php:34
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZDir\cleanPath
static cleanPath( $path, $toType=self::SEPARATOR_UNIX)
Definition: ezdir.php:202
eZDir\recursiveList
static recursiveList( $dir, $path, &$fileList)
Definition: ezdir.php:332
eZDir\dirpath
static dirpath( $filepath)
Definition: ezdir.php:129
eZDir\findSubitems
static findSubitems( $dir, $types=false, $fullPath=false, $includeHidden=false, $excludeItems=false)
Definition: ezdir.php:528
eZDir\copy
static copy( $sourceDirectory, &$destinationDirectory, $asChild=true, $recursive=true, $includeHidden=false, $excludeItems=false)
Definition: ezdir.php:577
eZDir\doMkdir
static doMkdir( $dir, $perm, $recursive=false)
Definition: ezdir.php:152
eZDir\SEPARATOR_UNIX
const SEPARATOR_UNIX
Definition: ezdir.php:19
eZDir\SEPARATOR_LOCAL
const SEPARATOR_LOCAL
Definition: ezdir.php:18
eZDir\recursiveFind
static recursiveFind( $dir, $suffix)
Definition: ezdir.php:361
eZDir\SEPARATOR_DOS
const SEPARATOR_DOS
Definition: ezdir.php:20
eZDir\isWriteable
static isWriteable( $dirname)
Definition: ezdir.php:645
eZDir\findSubdirs
static findSubdirs( $dir, $includeHidden=false, $excludeItems=false)
Definition: ezdir.php:519
eZDir\recursiveDelete
static recursiveDelete( $dir, $rootCheck=true)
Definition: ezdir.php:264
eZDir\filenamePath
static filenamePath( $filename, $maxCharLen=2)
Definition: ezdir.php:55
eZDir\convertSeparators
static convertSeparators( $path, $toType=self::SEPARATOR_UNIX)
Definition: ezdir.php:188
eZDir\unlinkWildcard
static unlinkWildcard( $dir, $pattern)
Definition: ezdir.php:395
eZDir\separator
static separator( $type)
Definition: ezdir.php:169
eZDir\getPathFromFilename
static getPathFromFilename( $filename)
Definition: ezdir.php:41
eZDir\temporaryFileRegexp
static temporaryFileRegexp( $standalone=true)
Definition: ezdir.php:628
eZDir\cleanupEmptyDirectories
static cleanupEmptyDirectories( $dir)
Definition: ezdir.php:95
eZDir\path
static path( $names, $includeEndSeparator=false, $type=self::SEPARATOR_UNIX)
Definition: ezdir.php:237
eZDir\recursiveFindRelative
static recursiveFindRelative( $baseDir, $subDir, $suffix)
Definition: ezdir.php:471
eZDir\directoryPermission
static directoryPermission()
Definition: ezdir.php:142
eZFileHandler\copy
static copy( $sourceFilename, $destinationFilename)
Definition: ezfilehandler.php:356
eZSys\rootDir
static rootDir()
Definition: ezsys.php:547
eZSys\osType
static osType()
Definition: ezsys.php:221
eZSys\fileSeparator
static fileSeparator()
Definition: ezsys.php:255
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.