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 class Varien_Io_File extends Varien_Io_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_iwd;
00043
00044
00045
00046
00047
00048
00049 protected $_cwd;
00050
00051
00052
00053
00054
00055
00056 const GREP_FILES = 'files_only';
00057
00058
00059
00060
00061
00062
00063 const GREP_DIRS = 'dirs_only';
00064
00065
00066
00067
00068
00069
00070
00071
00072 protected $_allowCreateFolders = false;
00073
00074
00075
00076
00077
00078
00079 protected $_streamHandler;
00080
00081
00082
00083
00084
00085
00086 protected $_streamFileName;
00087
00088
00089
00090
00091
00092
00093 protected $_streamChmod;
00094
00095
00096
00097
00098
00099
00100 protected $_streamLocked = false;
00101
00102
00103
00104
00105 public function __destruct()
00106 {
00107 if ($this->_streamHandler) {
00108 $this->streamClose();
00109 }
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 public function streamOpen($fileName, $mode = 'w+', $chmod = 0666)
00121 {
00122 $writeableMode = preg_match('#^[wax]#i', $mode);
00123 if ($writeableMode && !is_writeable($this->_cwd)) {
00124 throw new Exception('Permission denied for write to ' . $this->_cwd);
00125 }
00126 @chdir($this->_cwd);
00127 $this->_streamHandler = @fopen($fileName, $mode);
00128 @chdir($this->_iwd);
00129 if ($this->_streamHandler === false) {
00130 throw new Exception('Error write to file ' . $fileName);
00131 }
00132
00133 $this->_streamFileName = $fileName;
00134 $this->_streamChmod = $chmod;
00135 return true;
00136 }
00137
00138
00139
00140
00141
00142
00143 public function streamLock($exclusive = true)
00144 {
00145 if (!$this->_streamHandler) {
00146 return false;
00147 }
00148 $this->_streamLocked = true;
00149 $lock = $exclusive ? LOCK_EX : LOCK_SH;
00150 return flock($this->_streamHandler, $lock);
00151 }
00152
00153
00154
00155
00156
00157
00158 public function streamUnlock()
00159 {
00160 if (!$this->_streamHandler || !$this->_streamLocked) {
00161 return false;
00162 }
00163 $this->_streamLocked = false;
00164 return flock($this->_streamHandler, LOCK_UN);
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 public function streamRead($length = 1024)
00174 {
00175 if (!$this->_streamHandler) {
00176 return false;
00177 }
00178 if (feof($this->_streamHandler)) {
00179 return false;
00180 }
00181 return @fgets($this->_streamHandler, $length);
00182 }
00183
00184
00185
00186
00187
00188
00189 public function streamReadCsv($delimiter = ',', $enclosure = '"')
00190 {
00191 if (!$this->_streamHandler) {
00192 return false;
00193 }
00194 if (!ini_get('auto_detect_line_endings')) {
00195 ini_set('auto_detect_line_endings', 1);
00196 }
00197 return @fgetcsv($this->_streamHandler, 0, $delimiter, $enclosure);
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 public function streamWrite($str)
00207 {
00208 if (!$this->_streamHandler) {
00209 return false;
00210 }
00211 return @fwrite($this->_streamHandler, $str);
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 public function streamClose()
00221 {
00222 if (!$this->_streamHandler) {
00223 return false;
00224 }
00225
00226 if ($this->_streamLocked) {
00227 $this->streamUnlock();
00228 }
00229 @fclose($this->_streamHandler);
00230 @chmod($this->_streamFileName, $this->_streamChmod);
00231 return true;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241 public function streamStat($part = null, $default = null)
00242 {
00243 if (!$this->_streamHandler) {
00244 return false;
00245 }
00246 $stat = @fstat($this->_streamHandler);
00247 if (!is_null($part)) {
00248 return isset($stat[$part]) ? $stat[$part] : $default;
00249 }
00250 return $stat;
00251 }
00252
00253
00254
00255
00256
00257
00258 public function getStreamException()
00259 {
00260 return $this->_streamException;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 public function open(array $args=array())
00273 {
00274 if (!empty($args['path'])) {
00275 if ($args['path']) {
00276 if($this->_allowCreateFolders ) {
00277 $this->_createDestinationFolder($args['path']);
00278 }
00279 }
00280 }
00281
00282 $this->_iwd = getcwd();
00283 $this->cd(!empty($args['path']) ? $args['path'] : $this->_iwd);
00284 return true;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 public function setAllowCreateFolders($flag)
00295 {
00296 $this->_allowCreateFolders = $flag;
00297 return $this;
00298 }
00299
00300
00301
00302
00303
00304
00305 public function close()
00306 {
00307 return true;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 public function mkdir($dir, $mode=0777, $recursive=true)
00319 {
00320 if ($this->_cwd) {
00321 chdir($this->_cwd);
00322 }
00323
00324 $result = @mkdir($dir, $mode, $recursive);
00325 if ($result) {
00326 @chmod($dir, $mode);
00327 }
00328 if ($this->_iwd) {
00329 chdir($this->_iwd);
00330 }
00331 return $result;
00332 }
00333
00334
00335
00336
00337
00338
00339
00340 public function rmdir($dir, $recursive=false)
00341 {
00342 if( $this->_cwd ) {
00343 @chdir($this->_cwd);
00344 }
00345
00346 if( $recursive ) {
00347 if( is_dir( $dir ) ){
00348 foreach( scandir( $dir ) as $item ){
00349 if( !strcmp( $item, '.' ) || !strcmp( $item, '..' ) )
00350 continue;
00351 $this->rmdir( $dir . "/" . $item, $recursive );
00352 }
00353 $result = @rmdir( $dir );
00354 } else {
00355 $result = @unlink( $dir );
00356 }
00357 } else {
00358 $result = @rmdir($dir);
00359 }
00360
00361 @chdir($this->_iwd);
00362 return $result;
00363 }
00364
00365
00366
00367
00368
00369
00370 public function pwd()
00371 {
00372 return $this->_cwd;
00373 }
00374
00375
00376
00377
00378
00379
00380
00381 public function cd($dir)
00382 {
00383 if( is_dir($dir) ) {
00384 @chdir($this->_iwd);
00385 $this->_cwd = realpath($dir);
00386 return true;
00387 } else {
00388 throw new Exception('Unable to list current working directory.');
00389 return false;
00390 }
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 public function read($filename, $dest=null)
00404 {
00405 if (!is_null($dest)) {
00406 chdir($this->_cwd);
00407 $result = @copy($filename, $dest);
00408 chdir($this->_iwd);
00409 return $result;
00410 }
00411
00412 chdir($this->_cwd);
00413 $result = @file_get_contents($filename);
00414 chdir($this->_iwd);
00415
00416 return $result;
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426 public function write($filename, $src, $mode=null)
00427 {
00428 if (is_string($src) && is_readable($src)) {
00429 $src = realpath($src);
00430 $srcIsFile = true;
00431 } elseif (is_string($src) || is_resource($src)) {
00432 $srcIsFile = false;
00433 } else {
00434 return false;
00435 }
00436 @chdir($this->_cwd);
00437
00438 if (file_exists($filename)) {
00439 if (!is_writeable($filename)) {
00440 printf('File %s don\'t writeable', $filename);
00441 return false;
00442 }
00443 } else {
00444 if (!is_writable(dirname($filename))) {
00445 printf('Folder %s don\'t writeable', dirname($filename));
00446 return false;
00447 }
00448 }
00449 if ($srcIsFile) {
00450 $result = @copy($src, $filename);
00451 } else {
00452 $result = @file_put_contents($filename, $src);
00453 }
00454 if (!is_null($mode)) {
00455 @chmod($filename, $mode);
00456 }
00457 chdir($this->_iwd);
00458 return $result;
00459 }
00460
00461 public function fileExists($file, $onlyFile = true)
00462 {
00463 @chdir($this->_cwd);
00464 $result = file_exists($file);
00465 if ($result && $onlyFile) {
00466 $result = is_file($file);
00467 }
00468 @chdir($this->_iwd);
00469 return $result;
00470 }
00471
00472 public function isWriteable($path)
00473 {
00474 @chdir($this->_cwd);
00475 $result = is_writeable($path);
00476 @chdir($this->_iwd);
00477 return $result;
00478 }
00479
00480 public function getDestinationFolder($filepath)
00481 {
00482 preg_match('/^(.*[!\/])/', $filepath, $mathces);
00483 if (isset($mathces[0])) {
00484 return $mathces[0];
00485 }
00486 return false;
00487 }
00488
00489
00490
00491
00492
00493
00494
00495 public function createDestinationDir($path)
00496 {
00497 if (!$this->_allowCreateFolders) {
00498 return false;
00499 }
00500 return $this->_createDestinationFolder($this->getCleanPath($path));
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510 public function checkAndCreateFolder($folder, $mode = 0777)
00511 {
00512 if (is_dir($folder)) {
00513 return true;
00514 }
00515 if (!is_dir(dirname($folder))) {
00516 $this->checkAndCreateFolder(dirname($folder), $mode);
00517 }
00518 if (!is_dir($folder) && !@mkdir($folder, $mode)) {
00519 throw new Exception("Unable to create directory '{$folder}'. Access forbidden.");
00520 }
00521 return true;
00522 }
00523
00524 private function _createDestinationFolder($destinationFolder)
00525 {
00526 return $this->checkAndCreateFolder($destinationFolder);
00527
00528 if( !$destinationFolder ) {
00529 return $this;
00530 }
00531 if (!(@is_dir($destinationFolder) || $this->mkdir($destinationFolder, 0777, true))) {
00532 throw new Exception("Unable to create directory '{$destinationFolder}'.");
00533 }
00534
00535
00536 $destinationFolder = str_replace('/', DIRECTORY_SEPARATOR, $destinationFolder);
00537 $path = explode(DIRECTORY_SEPARATOR, $destinationFolder);
00538 $newPath = null;
00539 $oldPath = null;
00540 foreach( $path as $key => $directory ) {
00541 if (trim($directory)=='') {
00542 continue;
00543 }
00544 if (strlen($directory)===2 && $directory{1}===':') {
00545 $newPath = $directory;
00546 continue;
00547 }
00548 $newPath.= ( $newPath != DIRECTORY_SEPARATOR ) ? DIRECTORY_SEPARATOR . $directory : $directory;
00549 if( is_dir($newPath) ) {
00550 $oldPath = $newPath;
00551 continue;
00552 } else {
00553 if( is_writable($oldPath) ) {
00554 $this->mkdir($newPath, 0777);
00555 } else {
00556 throw new Exception("Unable to create directory '{$newPath}'. Access forbidden.");
00557 }
00558 }
00559 $oldPath = $newPath;
00560 }
00561 return $this;
00562 }
00563
00564
00565
00566
00567
00568
00569 public function rm($filename)
00570 {
00571 @chdir($this->_cwd);
00572 $result = @unlink($filename);
00573 @chdir($this->_iwd);
00574 return $result;
00575 }
00576
00577
00578
00579
00580
00581
00582
00583
00584 public function mv($src, $dest)
00585 {
00586 chdir($this->_cwd);
00587 $result = @rename($src, $dest);
00588 chdir($this->_iwd);
00589 return $result;
00590 }
00591
00592
00593
00594
00595
00596
00597
00598
00599 public function cp($src, $dest)
00600 {
00601 @chdir($this->_cwd);
00602 $result = @copy($src, $dest);
00603 @chdir($this->_iwd);
00604 return $result;
00605 }
00606
00607
00608
00609
00610
00611
00612
00613
00614 public function chmod($filename, $mode)
00615 {
00616 if ($this->_cwd) {
00617 chdir($this->_cwd);
00618 }
00619 $result = @chmod($filename, $mode);
00620 if ($this->_iwd) {
00621 chdir($this->_iwd);
00622 }
00623 return $result;
00624 }
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 public function ls($grep=null)
00641 {
00642 $ignoredDirectories = Array('.', '..');
00643
00644 if( is_dir($this->_cwd) ) {
00645 $dir = $this->_cwd;
00646 } elseif( is_dir($this->_iwd) ) {
00647 $dir = $this->_iwd;
00648 } else {
00649 throw new Exception('Unable to list current working directory.');
00650 }
00651
00652 $list = Array();
00653
00654 if ($dh = opendir($dir)) {
00655 while (($entry = readdir($dh)) !== false) {
00656 $list_item = Array();
00657
00658 $fullpath = $dir . DIRECTORY_SEPARATOR . $entry;
00659
00660 if( ($grep == self::GREP_DIRS) && (!is_dir($fullpath)) ) {
00661 continue;
00662 } elseif( ($grep == self::GREP_FILES) && (!is_file($fullpath)) ) {
00663 continue;
00664 } elseif( in_array($entry, $ignoredDirectories) ) {
00665 continue;
00666 }
00667
00668 $list_item['text'] = $entry;
00669 $list_item['mod_date'] = date ('Y-m-d H:i:s', filectime($fullpath));
00670 $list_item['permissions'] = $this->_parsePermissions(fileperms($fullpath));
00671 $list_item['owner'] = $this->_getFileOwner($fullpath);
00672
00673 if( is_file($fullpath) ) {
00674 $pathinfo = pathinfo($fullpath);
00675 $list_item['size'] = filesize($fullpath);
00676 $list_item['leaf'] = true;
00677 if( isset($pathinfo['extension']) && in_array(strtolower($pathinfo['extension']), Array('jpg', 'jpeg', 'gif', 'bmp', 'png')) && $list_item['size'] > 0 ) {
00678 $list_item['is_image'] = true;
00679 $list_item['filetype'] = $pathinfo['extension'];
00680 } elseif( $list_item['size'] == 0 ) {
00681 $list_item['is_image'] = false;
00682 $list_item['filetype'] = 'unknown';
00683 } elseif( isset($pathinfo['extension']) ) {
00684 $list_item['is_image'] = false;
00685 $list_item['filetype'] = $pathinfo['extension'];
00686 } else {
00687 $list_item['is_image'] = false;
00688 $list_item['filetype'] = 'unknown';
00689 }
00690 } else {
00691 $list_item['leaf'] = false;
00692 $list_item['id'] = $fullpath;
00693 }
00694
00695 $list[] = $list_item;
00696 }
00697 closedir($dh);
00698 } else {
00699 throw new Exception('Unable to list current working directory. Access forbidden.');
00700 }
00701
00702 return $list;
00703 }
00704
00705
00706
00707
00708
00709
00710
00711
00712 protected function _parsePermissions($mode)
00713 {
00714 if( $mode & 0x1000 )
00715 $type='p';
00716 else if( $mode & 0x2000 )
00717 $type='c';
00718 else if( $mode & 0x4000 )
00719 $type='d';
00720 else if( $mode & 0x6000 )
00721 $type='b';
00722 else if( $mode & 0x8000 )
00723 $type='-';
00724 else if( $mode & 0xA000 )
00725 $type='l';
00726 else if( $mode & 0xC000 )
00727 $type='s';
00728 else
00729 $type='u';
00730
00731
00732 $owner['read'] = ($mode & 00400) ? 'r' : '-';
00733 $owner['write'] = ($mode & 00200) ? 'w' : '-';
00734 $owner['execute'] = ($mode & 00100) ? 'x' : '-';
00735 $group['read'] = ($mode & 00040) ? 'r' : '-';
00736 $group['write'] = ($mode & 00020) ? 'w' : '-';
00737 $group['execute'] = ($mode & 00010) ? 'x' : '-';
00738 $world['read'] = ($mode & 00004) ? 'r' : '-';
00739 $world['write'] = ($mode & 00002) ? 'w' : '-';
00740 $world['execute'] = ($mode & 00001) ? 'x' : '-';
00741
00742
00743 if( $mode & 0x800 )
00744 $owner["execute"] = ($owner['execute']=='x') ? 's' : 'S';
00745 if( $mode & 0x400 )
00746 $group["execute"] = ($group['execute']=='x') ? 's' : 'S';
00747 if( $mode & 0x200 )
00748 $world["execute"] = ($world['execute']=='x') ? 't' : 'T';
00749
00750 $s=sprintf('%1s', $type);
00751 $s.=sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
00752 $s.=sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
00753 $s.=sprintf('%1s%1s%1s', $world['read'], $world['write'], $world['execute']);
00754 return trim($s);
00755 }
00756
00757
00758
00759
00760
00761
00762
00763
00764 protected function _getFileOwner($filename)
00765 {
00766 if( !function_exists('posix_getpwuid') ) {
00767 return 'n/a';
00768 }
00769
00770 $owner = posix_getpwuid(fileowner($filename));
00771 $groupinfo = posix_getgrnam(filegroup($filename));
00772
00773 return $owner['name'] . ' / ' . $groupinfo;
00774 }
00775
00776 public function dirsep()
00777 {
00778 return DIRECTORY_SEPARATOR;
00779 }
00780
00781 public function dirname($file)
00782 {
00783 return $this->getCleanPath(dirname($file));
00784 }
00785 }