Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

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