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