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 if (!class_exists('Object')) {
00030 uses('object');
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040 class Folder extends Object {
00041
00042
00043
00044
00045
00046
00047 var $path = null;
00048
00049
00050
00051
00052
00053
00054 var $sort = false;
00055
00056
00057
00058
00059
00060
00061 var $mode = 0755;
00062
00063
00064
00065
00066
00067
00068 var $__messages = array();
00069
00070
00071
00072
00073
00074
00075 var $__errors = false;
00076
00077
00078
00079
00080
00081
00082 var $__directories;
00083
00084
00085
00086
00087
00088
00089 var $__files;
00090
00091
00092
00093
00094
00095
00096
00097 function __construct($path = false, $create = false, $mode = false) {
00098 parent::__construct();
00099 if (empty($path)) {
00100 $path = TMP;
00101 }
00102 if ($mode) {
00103 $this->mode = $mode;
00104 }
00105
00106 if (!file_exists($path) && $create === true) {
00107 $this->create($path, $this->mode);
00108 }
00109 if (!Folder::isAbsolute($path)) {
00110 $path = realpath($path);
00111 }
00112 if (!empty($path)) {
00113 $this->cd($path);
00114 }
00115 }
00116
00117
00118
00119
00120
00121
00122 function pwd() {
00123 return $this->path;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 function cd($path) {
00133 $path = $this->realpath($path);
00134 if (is_dir($path)) {
00135 return $this->path = $path;
00136 }
00137 return false;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 function read($sort = true, $exceptions = false, $fullPath = false) {
00150 $dirs = $files = array();
00151
00152 if (is_array($exceptions)) {
00153 $exceptions = array_flip($exceptions);
00154 }
00155 $skipHidden = isset($exceptions['.']) || $exceptions === true;
00156
00157 if (false === ($dir = @opendir($this->path))) {
00158 return array($dirs, $files);
00159 }
00160
00161 while (false !== ($item = readdir($dir))) {
00162 if ($item === '.' || $item === '..' || ($skipHidden && $item[0] === '.') || isset($exceptions[$item])) {
00163 continue;
00164 }
00165
00166 $path = Folder::addPathElement($this->path, $item);
00167 if (is_dir($path)) {
00168 $dirs[] = $fullPath ? $path : $item;
00169 } else {
00170 $files[] = $fullPath ? $path : $item;
00171 }
00172 }
00173
00174 if ($sort || $this->sort) {
00175 sort($dirs);
00176 sort($files);
00177 }
00178
00179 closedir($dir);
00180 return array($dirs, $files);
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 function find($regexpPattern = '.*', $sort = false) {
00190 list($dirs, $files) = $this->read($sort);
00191 return array_values(preg_grep('/^' . $regexpPattern . '$/i', $files)); ;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 function findRecursive($pattern = '.*', $sort = false) {
00201 $startsOn = $this->path;
00202 $out = $this->_findRecursive($pattern, $sort);
00203 $this->cd($startsOn);
00204 return $out;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 function _findRecursive($pattern, $sort = false) {
00214 list($dirs, $files) = $this->read($sort);
00215 $found = array();
00216
00217 foreach ($files as $file) {
00218 if (preg_match('/^' . $pattern . '$/i', $file)) {
00219 $found[] = Folder::addPathElement($this->path, $file);
00220 }
00221 }
00222 $start = $this->path;
00223
00224 foreach ($dirs as $dir) {
00225 $this->cd(Folder::addPathElement($start, $dir));
00226 $found = array_merge($found, $this->findRecursive($pattern, $sort));
00227 }
00228 return $found;
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238 function isWindowsPath($path) {
00239 if (preg_match('/^[A-Z]:\\\\/i', $path)) {
00240 return true;
00241 }
00242 return false;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 function isAbsolute($path) {
00253 $match = preg_match('/^\\
00254 return $match;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264 function normalizePath($path) {
00265 return Folder::correctSlashFor($path);
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 function correctSlashFor($path) {
00276 if (Folder::isWindowsPath($path)) {
00277 return '\\';
00278 }
00279 return '/';
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289 function slashTerm($path) {
00290 if (Folder::isSlashTerm($path)) {
00291 return $path;
00292 }
00293 return $path . Folder::correctSlashFor($path);
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 function addPathElement($path, $element) {
00305 return Folder::slashTerm($path) . $element;
00306 }
00307
00308
00309
00310
00311
00312
00313 function inCakePath($path = '') {
00314 $dir = substr(Folder::slashTerm(ROOT), 0, -1);
00315 $newdir = $dir . $path;
00316
00317 return $this->inPath($newdir);
00318 }
00319
00320
00321
00322
00323
00324
00325 function inPath($path = '', $reverse = false) {
00326 $dir = Folder::slashTerm($path);
00327 $current = Folder::slashTerm($this->pwd());
00328
00329 if (!$reverse) {
00330 $return = preg_match('/^(.*)' . preg_quote($dir, '/') . '(.*)/', $current);
00331 } else {
00332 $return = preg_match('/^(.*)' . preg_quote($current, '/') . '(.*)/', $dir);
00333 }
00334 if ($return == 1) {
00335 return true;
00336 } else {
00337 return false;
00338 }
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 function chmod($path, $mode = false, $recursive = true, $exceptions = array()) {
00351 if (!$mode) {
00352 $mode = $this->mode;
00353 }
00354
00355 if ($recursive === false && is_dir($path)) {
00356 if (@chmod($path, intval($mode, 8))) {
00357 $this->__messages[] = sprintf(__('%s changed to %s', true), $path, $mode);
00358 return true;
00359 }
00360
00361 $this->__errors[] = sprintf(__('%s NOT changed to %s', true), $path, $mode);
00362 return false;
00363 }
00364
00365 if (is_dir($path)) {
00366 $paths = $this->tree($path);
00367
00368 foreach ($paths as $type) {
00369 foreach ($type as $key => $fullpath) {
00370 $check = explode(DS, $fullpath);
00371 $count = count($check);
00372
00373 if (in_array($check[$count - 1], $exceptions)) {
00374 continue;
00375 }
00376
00377 if (@chmod($fullpath, intval($mode, 8))) {
00378 $this->__messages[] = sprintf(__('%s changed to %s', true), $fullpath, $mode);
00379 } else {
00380 $this->__errors[] = sprintf(__('%s NOT changed to %s', true), $fullpath, $mode);
00381 }
00382 }
00383 }
00384
00385 if (empty($this->__errors)) {
00386 return true;
00387 }
00388 }
00389 return false;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 function tree($path, $exceptions = true, $type = null) {
00401 $original = $this->path;
00402 $path = rtrim($path, DS);
00403 $this->__files = array();
00404 $this->__directories = array($path);
00405 $directories = array();
00406
00407 if ($exceptions === false) {
00408 $exceptions = true;
00409 }
00410 while (count($this->__directories)) {
00411 $dir = array_pop($this->__directories);
00412 $this->__tree($dir, $exceptions);
00413 $directories[] = $dir;
00414 }
00415
00416 if ($type === null) {
00417 return array($directories, $this->__files);
00418 }
00419 if ($type === 'dir') {
00420 return $directories;
00421 }
00422 $this->cd($original);
00423
00424 return $this->__files;
00425 }
00426
00427
00428
00429
00430
00431
00432
00433 function __tree($path, $exceptions) {
00434 if ($this->cd($path)) {
00435 list($dirs, $files) = $this->read(false, $exceptions, true);
00436 $this->__directories = array_merge($this->__directories, $dirs);
00437 $this->__files = array_merge($this->__files, $files);
00438 }
00439 }
00440
00441
00442
00443
00444
00445
00446
00447
00448 function create($pathname, $mode = false) {
00449 if (is_dir($pathname) || empty($pathname)) {
00450 return true;
00451 }
00452
00453 if (!$mode) {
00454 $mode = $this->mode;
00455 }
00456
00457 if (is_file($pathname)) {
00458 $this->__errors[] = sprintf(__('%s is a file', true), $pathname);
00459 return false;
00460 }
00461 $nextPathname = substr($pathname, 0, strrpos($pathname, DS));
00462
00463 if ($this->create($nextPathname, $mode)) {
00464 if (!file_exists($pathname)) {
00465 $old = umask(0);
00466 if (mkdir($pathname, $mode)) {
00467 umask($old);
00468 $this->__messages[] = sprintf(__('%s created', true), $pathname);
00469 return true;
00470 } else {
00471 umask($old);
00472 $this->__errors[] = sprintf(__('%s NOT created', true), $pathname);
00473 return false;
00474 }
00475 }
00476 }
00477 return true;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486 function dirsize() {
00487 $size = 0;
00488 $directory = Folder::slashTerm($this->path);
00489 $stack = array($directory);
00490 $count = count($stack);
00491 for ($i = 0, $j = $count; $i < $j; ++$i) {
00492 if (is_file($stack[$i])) {
00493 $size += filesize($stack[$i]);
00494 } elseif (is_dir($stack[$i])) {
00495 $dir = dir($stack[$i]);
00496 if ($dir) {
00497 while (false !== ($entry = $dir->read())) {
00498 if ($entry === '.' || $entry === '..') {
00499 continue;
00500 }
00501 $add = $stack[$i] . $entry;
00502
00503 if (is_dir($stack[$i] . $entry)) {
00504 $add = Folder::slashTerm($add);
00505 }
00506 $stack[] = $add;
00507 }
00508 $dir->close();
00509 }
00510 }
00511 $j = count($stack);
00512 }
00513 return $size;
00514 }
00515
00516
00517
00518
00519
00520
00521
00522 function delete($path = null) {
00523 if (!$path) {
00524 $path = $this->pwd();
00525 }
00526 $path = Folder::slashTerm($path);
00527 if (is_dir($path) === true) {
00528 $normalFiles = glob($path . '*');
00529 $hiddenFiles = glob($path . '\.?*');
00530
00531 $normalFiles = $normalFiles ? $normalFiles : array();
00532 $hiddenFiles = $hiddenFiles ? $hiddenFiles : array();
00533
00534 $files = array_merge($normalFiles, $hiddenFiles);
00535 if (is_array($files)) {
00536 foreach ($files as $file) {
00537 if (preg_match('/(\.|\.\.)$/', $file)) {
00538 continue;
00539 }
00540 if (is_file($file) === true) {
00541 if (@unlink($file)) {
00542 $this->__messages[] = sprintf(__('%s removed', true), $file);
00543 } else {
00544 $this->__errors[] = sprintf(__('%s NOT removed', true), $file);
00545 }
00546 } elseif (is_dir($file) === true && $this->delete($file) === false) {
00547 return false;
00548 }
00549 }
00550 }
00551 $path = substr($path, 0, strlen($path) - 1);
00552 if (rmdir($path) === false) {
00553 $this->__errors[] = sprintf(__('%s NOT removed', true), $path);
00554 return false;
00555 } else {
00556 $this->__messages[] = sprintf(__('%s removed', true), $path);
00557 }
00558 }
00559 return true;
00560 }
00561
00562
00563
00564
00565
00566
00567
00568 function copy($options = array()) {
00569 $to = null;
00570 if (is_string($options)) {
00571 $to = $options;
00572 $options = array();
00573 }
00574 $options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array()), $options);
00575
00576 $fromDir = $options['from'];
00577 $toDir = $options['to'];
00578 $mode = $options['mode'];
00579
00580 if (!$this->cd($fromDir)) {
00581 $this->__errors[] = sprintf(__('%s not found', true), $fromDir);
00582 return false;
00583 }
00584
00585 if (!is_dir($toDir)) {
00586 $this->mkdir($toDir, $mode);
00587 }
00588
00589 if (!is_writable($toDir)) {
00590 $this->__errors[] = sprintf(__('%s not writable', true), $toDir);
00591 return false;
00592 }
00593
00594 $exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
00595 if ($handle = @opendir($fromDir)) {
00596 while (false !== ($item = readdir($handle))) {
00597 if (!in_array($item, $exceptions)) {
00598 $from = Folder::addPathElement($fromDir, $item);
00599 $to = Folder::addPathElement($toDir, $item);
00600 if (is_file($from)) {
00601 if (copy($from, $to)) {
00602 chmod($to, intval($mode, 8));
00603 touch($to, filemtime($from));
00604 $this->__messages[] = sprintf(__('%s copied to %s', true), $from, $to);
00605 } else {
00606 $this->__errors[] = sprintf(__('%s NOT copied to %s', true), $from, $to);
00607 }
00608 }
00609
00610 if (is_dir($from) && !file_exists($to)) {
00611 $old = umask(0);
00612 if (mkdir($to, $mode)) {
00613 umask($old);
00614 $old = umask(0);
00615 chmod($to, $mode);
00616 umask($old);
00617 $this->__messages[] = sprintf(__('%s created', true), $to);
00618 $options = array_merge($options, array('to'=> $to, 'from'=> $from));
00619 $this->copy($options);
00620 } else {
00621 $this->__errors[] = sprintf(__('%s not created', true), $to);
00622 }
00623 }
00624 }
00625 }
00626 closedir($handle);
00627 } else {
00628 return false;
00629 }
00630
00631 if (!empty($this->__errors)) {
00632 return false;
00633 }
00634 return true;
00635 }
00636
00637
00638
00639
00640
00641
00642
00643 function move($options) {
00644 $to = null;
00645 if (is_string($options)) {
00646 $to = $options;
00647 $options = (array)$options;
00648 }
00649 $options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array()), $options);
00650
00651 if ($this->copy($options)) {
00652 if ($this->delete($options['from'])) {
00653 return $this->cd($options['to']);
00654 }
00655 }
00656 return false;
00657 }
00658
00659
00660
00661
00662
00663
00664 function messages() {
00665 return $this->__messages;
00666 }
00667
00668
00669
00670
00671
00672
00673 function errors() {
00674 return $this->__errors;
00675 }
00676
00677
00678
00679
00680
00681
00682 function ls($sort = true, $exceptions = false) {
00683 return $this->read($sort, $exceptions);
00684 }
00685
00686
00687
00688
00689
00690
00691 function mkdir($pathname, $mode = 0755) {
00692 return $this->create($pathname, $mode);
00693 }
00694
00695
00696
00697
00698
00699
00700 function cp($options) {
00701 return $this->copy($options);
00702 }
00703
00704
00705
00706
00707
00708
00709 function mv($options) {
00710 return $this->move($options);
00711 }
00712
00713
00714
00715
00716
00717
00718 function rm($path) {
00719 return $this->delete($path);
00720 }
00721
00722
00723
00724
00725
00726
00727 function realpath($path) {
00728 $path = str_replace('/', DS, trim($path));
00729 if (strpos($path, '..') === false) {
00730 if (!Folder::isAbsolute($path)) {
00731 $path = Folder::addPathElement($this->path, $path);
00732 }
00733 return $path;
00734 }
00735 $parts = explode(DS, $path);
00736 $newparts = array();
00737 $newpath = '';
00738 if ($path[0] === DS) {
00739 $newpath = DS;
00740 }
00741
00742 while (($part = array_shift($parts)) !== NULL) {
00743 if ($part === '.' || $part === '') {
00744 continue;
00745 }
00746 if ($part === '..') {
00747 if (count($newparts) > 0) {
00748 array_pop($newparts);
00749 continue;
00750 } else {
00751 return false;
00752 }
00753 }
00754 $newparts[] = $part;
00755 }
00756 $newpath .= implode(DS, $newparts);
00757
00758 return Folder::slashTerm($newpath);
00759 }
00760
00761
00762
00763
00764
00765
00766
00767
00768 function isSlashTerm($path) {
00769 $lastChar = $path[strlen($path) - 1];
00770 return $lastChar === '/' || $lastChar === '\\';
00771 }
00772 }
00773 ?>