File.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem\Io;
  7. /**
  8. * Filesystem client
  9. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  10. */
  11. class File extends AbstractIo
  12. {
  13. /**
  14. * Save initial working directory
  15. *
  16. * @var string
  17. */
  18. protected $_iwd;
  19. /**
  20. * Use virtual current working directory for application integrity
  21. *
  22. * @var string
  23. */
  24. protected $_cwd;
  25. /**
  26. * Used to grep ls() output
  27. *
  28. * @const
  29. */
  30. const GREP_FILES = 'files_only';
  31. /**
  32. * Used to grep ls() output
  33. *
  34. * @const
  35. */
  36. const GREP_DIRS = 'dirs_only';
  37. /**
  38. * If this variable is set to TRUE, our library will be able to automatically create
  39. * non-existent directories.
  40. *
  41. * @var bool
  42. * @access protected
  43. */
  44. protected $_allowCreateFolders = false;
  45. /**
  46. * Stream open file pointer
  47. *
  48. * @var resource
  49. */
  50. protected $_streamHandler;
  51. /**
  52. * Stream mode filename
  53. *
  54. * @var string
  55. */
  56. protected $_streamFileName;
  57. /**
  58. * Stream mode chmod
  59. *
  60. * @var string
  61. */
  62. protected $_streamChmod;
  63. /**
  64. * Lock file
  65. *
  66. * @var bool
  67. */
  68. protected $_streamLocked = false;
  69. /**
  70. * @var \Exception
  71. */
  72. private $_streamException;
  73. /**
  74. * Destruct
  75. *
  76. * @return void
  77. */
  78. public function __destruct()
  79. {
  80. if ($this->_streamHandler) {
  81. $this->streamClose();
  82. }
  83. }
  84. /**
  85. * Lock file
  86. *
  87. * @param bool $exclusive
  88. * @return bool
  89. */
  90. public function streamLock($exclusive = true)
  91. {
  92. if (!$this->_streamHandler) {
  93. return false;
  94. }
  95. $this->_streamLocked = true;
  96. $lock = $exclusive ? LOCK_EX : LOCK_SH;
  97. return flock($this->_streamHandler, $lock);
  98. }
  99. /**
  100. * Unlock file
  101. *
  102. * @return bool
  103. */
  104. public function streamUnlock()
  105. {
  106. if (!$this->_streamHandler || !$this->_streamLocked) {
  107. return false;
  108. }
  109. $this->_streamLocked = false;
  110. return flock($this->_streamHandler, LOCK_UN);
  111. }
  112. /**
  113. * Binary-safe file read
  114. *
  115. * @param int $length
  116. * @return string|false
  117. */
  118. public function streamRead($length = 1024)
  119. {
  120. if (!$this->_streamHandler) {
  121. return false;
  122. }
  123. if (feof($this->_streamHandler)) {
  124. return false;
  125. }
  126. return @fgets($this->_streamHandler, $length);
  127. }
  128. /**
  129. * Gets line from file pointer and parse for CSV fields
  130. *
  131. * @param string $delimiter
  132. * @param string $enclosure
  133. * @return string|false
  134. */
  135. public function streamReadCsv($delimiter = ',', $enclosure = '"')
  136. {
  137. if (!$this->_streamHandler) {
  138. return false;
  139. }
  140. return @fgetcsv($this->_streamHandler, 0, $delimiter, $enclosure);
  141. }
  142. /**
  143. * Binary-safe file write
  144. *
  145. * @param string $str
  146. * @return int|false
  147. */
  148. public function streamWrite($str)
  149. {
  150. if (!$this->_streamHandler) {
  151. return false;
  152. }
  153. return @fwrite($this->_streamHandler, $str);
  154. }
  155. /**
  156. * Format line as CSV and write to file pointer
  157. *
  158. * @param array $row
  159. * @param string $delimiter
  160. * @param string $enclosure
  161. * @return int|false The length of the written string or false
  162. */
  163. public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')
  164. {
  165. if (!$this->_streamHandler) {
  166. return false;
  167. }
  168. /**
  169. * Security enhancement for CSV data processing by Excel-like applications.
  170. * @see https://bugzilla.mozilla.org/show_bug.cgi?id=1054702
  171. *
  172. * @var $value string|\Magento\Framework\Phrase
  173. */
  174. foreach ($row as $key => $value) {
  175. if (!is_string($value)) {
  176. $value = (string)$value;
  177. }
  178. if (isset($value[0]) && in_array($value[0], ['=', '+', '-'])) {
  179. $row[$key] = ' ' . $value;
  180. }
  181. }
  182. return @fputcsv($this->_streamHandler, $row, $delimiter, $enclosure);
  183. }
  184. /**
  185. * Close an open file pointer
  186. * Set chmod on a file
  187. *
  188. * @return bool
  189. */
  190. public function streamClose()
  191. {
  192. if (!$this->_streamHandler) {
  193. return false;
  194. }
  195. if ($this->_streamLocked) {
  196. $this->streamUnlock();
  197. }
  198. @fclose($this->_streamHandler);
  199. $this->chmod($this->_streamFileName, $this->_streamChmod);
  200. return true;
  201. }
  202. /**
  203. * Retrieve open file statistic
  204. *
  205. * @param string $part the part of statistic
  206. * @param mixed $default default value for part
  207. * @return array|bool
  208. */
  209. public function streamStat($part = null, $default = null)
  210. {
  211. if (!$this->_streamHandler) {
  212. return false;
  213. }
  214. $stat = @fstat($this->_streamHandler);
  215. if ($part !== null) {
  216. return $stat[$part] ?? $default;
  217. }
  218. return $stat;
  219. }
  220. /**
  221. * Retrieve stream methods exception
  222. *
  223. * @return \Exception
  224. */
  225. public function getStreamException()
  226. {
  227. return $this->_streamException;
  228. }
  229. /**
  230. * Open a connection
  231. *
  232. * Possible arguments:
  233. * - path default current path
  234. *
  235. * @param array $args
  236. * @return true
  237. */
  238. public function open(array $args = [])
  239. {
  240. if (!empty($args['path'])) {
  241. if ($args['path']) {
  242. if ($this->_allowCreateFolders) {
  243. $this->_createDestinationFolder($args['path']);
  244. }
  245. }
  246. }
  247. $this->_iwd = getcwd();
  248. $this->cd(!empty($args['path']) ? $args['path'] : $this->_iwd);
  249. return true;
  250. }
  251. /**
  252. * Used to set {@link _allowCreateFolders} value
  253. *
  254. * @param bool $flag
  255. * @access public
  256. * @return $this
  257. */
  258. public function setAllowCreateFolders($flag)
  259. {
  260. $this->_allowCreateFolders = $flag;
  261. return $this;
  262. }
  263. /**
  264. * Close a connection
  265. *
  266. * @return true
  267. */
  268. public function close()
  269. {
  270. return true;
  271. }
  272. /**
  273. * Create a directory
  274. *
  275. * @param string $dir
  276. * @param int $mode
  277. * @param bool $recursive
  278. * @return bool
  279. */
  280. public function mkdir($dir, $mode = 0777, $recursive = true)
  281. {
  282. $this->_cwd();
  283. $result = @mkdir($dir, $mode, $recursive);
  284. $this->_iwd();
  285. return $result;
  286. }
  287. /**
  288. * Delete a directory
  289. *
  290. * @param string $dir
  291. * @param bool $recursive
  292. * @return bool
  293. */
  294. public function rmdir($dir, $recursive = false)
  295. {
  296. $this->_cwd();
  297. $result = self::rmdirRecursive($dir, $recursive);
  298. $this->_iwd();
  299. return $result;
  300. }
  301. /**
  302. * Delete a directory recursively
  303. * @param string $dir
  304. * @param bool $recursive
  305. * @return bool
  306. */
  307. public static function rmdirRecursive($dir, $recursive = true)
  308. {
  309. if ($recursive) {
  310. $result = self::_recursiveCallback($dir, ['unlink'], ['rmdir']);
  311. } else {
  312. $result = @rmdir($dir);
  313. }
  314. return $result;
  315. }
  316. /**
  317. * Applies specified callback for a directory/file recursively
  318. *
  319. * $fileCallback and $dirCallback format: array($callback, $parameters)
  320. * - $callback - callable
  321. * - $parameters (optional) - array with parameters to be passed to the $callback
  322. *
  323. * @param string $dir
  324. * @param array $fileCallback
  325. * @param array $dirCallback
  326. * @return mixed
  327. * @throws \InvalidArgumentException
  328. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  329. * @SuppressWarnings(PHPMD.NPathComplexity)
  330. */
  331. protected static function _recursiveCallback($dir, array $fileCallback, array $dirCallback = [])
  332. {
  333. if (empty($fileCallback) || !is_array($fileCallback) || !is_array($dirCallback)) {
  334. throw new \InvalidArgumentException("file/dir callback is not specified");
  335. }
  336. if (empty($dirCallback)) {
  337. $dirCallback = $fileCallback;
  338. }
  339. if (is_dir($dir)) {
  340. foreach (scandir($dir, SCANDIR_SORT_NONE) as $item) {
  341. if (!strcmp($item, '.') || !strcmp($item, '..')) {
  342. continue;
  343. }
  344. self::_recursiveCallback($dir . '/' . $item, $fileCallback, $dirCallback);
  345. }
  346. $callback = $dirCallback[0];
  347. if (!is_callable($callback)) {
  348. throw new \InvalidArgumentException("'dirCallback' parameter is not callable");
  349. }
  350. $parameters = isset($dirCallback[1]) ? $dirCallback[1] : [];
  351. } else {
  352. $callback = $fileCallback[0];
  353. if (!is_callable($callback)) {
  354. throw new \InvalidArgumentException("'fileCallback' parameter is not callable");
  355. }
  356. $parameters = isset($fileCallback[1]) ? $fileCallback[1] : [];
  357. }
  358. array_unshift($parameters, $dir);
  359. $result = @call_user_func_array($callback, $parameters);
  360. return $result;
  361. }
  362. /**
  363. * Get current working directory
  364. *
  365. * @return string
  366. */
  367. public function pwd()
  368. {
  369. return $this->_cwd;
  370. }
  371. /**
  372. * Change current working directory
  373. *
  374. * @param string $dir
  375. * @return true
  376. * @throws \Exception
  377. * @SuppressWarnings(PHPMD.ShortMethodName)
  378. */
  379. public function cd($dir)
  380. {
  381. if (is_dir($dir)) {
  382. $this->_iwd();
  383. $this->_cwd = realpath($dir);
  384. return true;
  385. } else {
  386. throw new \Exception('Unable to list current working directory.');
  387. }
  388. }
  389. /**
  390. * Read a file to result, file or stream
  391. *
  392. * If $dest is null the output will be returned.
  393. * Otherwise it will be saved to the file or stream and operation result is returned.
  394. *
  395. * @param string $filename
  396. * @param string|resource $dest
  397. * @return bool|string
  398. */
  399. public function read($filename, $dest = null)
  400. {
  401. $this->_cwd();
  402. if ($dest !== null) {
  403. $result = @copy($filename, $dest);
  404. } else {
  405. $result = @file_get_contents($filename);
  406. }
  407. $this->_iwd();
  408. return $result;
  409. }
  410. /**
  411. * Write a file from string, file or stream
  412. *
  413. * @param string $filename
  414. * @param string|resource $src
  415. * @param int $mode
  416. * @return int|bool
  417. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  418. */
  419. public function write($filename, $src, $mode = null)
  420. {
  421. if (is_string($src) && @is_readable($src)) {
  422. $src = realpath($src);
  423. $srcIsFile = true;
  424. } elseif (is_string($src) || is_resource($src)) {
  425. $srcIsFile = false;
  426. } else {
  427. return false;
  428. }
  429. $this->_cwd();
  430. if (file_exists($filename)) {
  431. if (!is_writeable($filename)) {
  432. printf('The file %s is not writable', $filename);
  433. return false;
  434. }
  435. } else {
  436. if (!is_writable(dirname($filename))) {
  437. printf('The directory %s is not writable', dirname($filename));
  438. return false;
  439. }
  440. }
  441. if ($srcIsFile) {
  442. $result = @copy($src, $filename);
  443. } else {
  444. $result = @file_put_contents($filename, $src);
  445. }
  446. if ($mode !== null && $result) {
  447. @chmod($filename, $mode);
  448. }
  449. $this->_iwd();
  450. return $result;
  451. }
  452. /**
  453. * Is file exists
  454. *
  455. * @param string $file
  456. * @param bool $onlyFile
  457. * @return bool
  458. */
  459. public function fileExists($file, $onlyFile = true)
  460. {
  461. $this->_cwd();
  462. $result = file_exists($file);
  463. if ($result && $onlyFile) {
  464. $result = is_file($file);
  465. }
  466. $this->_iwd();
  467. return $result;
  468. }
  469. /**
  470. * Tells whether the filename is writable
  471. *
  472. * @param string $path
  473. * @return bool
  474. */
  475. public function isWriteable($path)
  476. {
  477. $this->_cwd();
  478. $result = is_writeable($path);
  479. $this->_iwd();
  480. return $result;
  481. }
  482. /**
  483. * Get destination folder
  484. *
  485. * @param string $filePath
  486. * @return bool|string
  487. */
  488. public function getDestinationFolder($filePath)
  489. {
  490. preg_match('/^(.*[!\/])/', $filePath, $matches);
  491. if (isset($matches[0])) {
  492. return $matches[0];
  493. }
  494. return false;
  495. }
  496. /**
  497. * Create destination folder
  498. *
  499. * @param string $path
  500. * @return bool
  501. */
  502. public function createDestinationDir($path)
  503. {
  504. if (!$this->_allowCreateFolders) {
  505. return false;
  506. }
  507. return $this->_createDestinationFolder($this->getCleanPath($path));
  508. }
  509. /**
  510. * Check and create if not exists folder
  511. *
  512. * @param string $folder
  513. * @param int $mode
  514. * @return true
  515. * @throws \Exception
  516. */
  517. public function checkAndCreateFolder($folder, $mode = 0777)
  518. {
  519. if (is_dir($folder)) {
  520. return true;
  521. }
  522. if (!is_dir(dirname($folder))) {
  523. $this->checkAndCreateFolder(dirname($folder), $mode);
  524. }
  525. if (!is_dir($folder) && !$this->mkdir($folder, $mode)) {
  526. throw new \Exception("Unable to create directory '{$folder}'. Access forbidden.");
  527. }
  528. return true;
  529. }
  530. /**
  531. * Create destination folder
  532. *
  533. * @param string $destinationFolder
  534. * @return bool
  535. */
  536. private function _createDestinationFolder($destinationFolder)
  537. {
  538. return $this->checkAndCreateFolder($destinationFolder);
  539. }
  540. /**
  541. * Delete a file
  542. *
  543. * @param string $filename
  544. * @return bool
  545. * @SuppressWarnings(PHPMD.ShortMethodName)
  546. */
  547. public function rm($filename)
  548. {
  549. $this->_cwd();
  550. $result = @unlink($filename);
  551. $this->_iwd();
  552. return $result;
  553. }
  554. /**
  555. * Rename or move a directory or a file
  556. *
  557. * @param string $src
  558. * @param string $destination
  559. * @return bool
  560. * @SuppressWarnings(PHPMD.ShortMethodName)
  561. */
  562. public function mv($src, $destination)
  563. {
  564. $this->_cwd();
  565. $result = @rename($src, $destination);
  566. $this->_iwd();
  567. return $result;
  568. }
  569. /**
  570. * Copy a file
  571. *
  572. * @param string $src
  573. * @param string $destination
  574. * @return bool
  575. * @SuppressWarnings(PHPMD.ShortMethodName)
  576. */
  577. public function cp($src, $destination)
  578. {
  579. $this->_cwd();
  580. $result = @copy($src, $destination);
  581. $this->_iwd();
  582. return $result;
  583. }
  584. /**
  585. * Change mode of a directory or a file
  586. *
  587. * @param string $filename
  588. * @param int $mode
  589. * @param bool $recursive
  590. * @return bool
  591. */
  592. public function chmod($filename, $mode, $recursive = false)
  593. {
  594. $this->_cwd();
  595. if ($recursive) {
  596. $result = self::chmodRecursive($filename, $mode);
  597. } else {
  598. $result = @chmod($filename, $mode);
  599. }
  600. $this->_iwd();
  601. return $result;
  602. }
  603. /**
  604. * Change mode of a directory/file recursively
  605. *
  606. * @static
  607. * @param string $dir
  608. * @param int $mode
  609. * @return bool
  610. */
  611. public static function chmodRecursive($dir, $mode)
  612. {
  613. return self::_recursiveCallback($dir, ['chmod', [$mode]]);
  614. }
  615. /**
  616. * Get list of cwd subdirectories and files
  617. *
  618. * Suggestions (from moshe):
  619. * - Use filemtime instead of filectime for performance
  620. * - Change $grep to $flags and use binary flags
  621. * - LS_DIRS = 1
  622. * - LS_FILES = 2
  623. * - LS_ALL = 3
  624. *
  625. * @param string|null $grep
  626. * @return array
  627. * @throws \Exception
  628. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  629. * @SuppressWarnings(PHPMD.ShortMethodName)
  630. */
  631. public function ls($grep = null)
  632. {
  633. $ignoredDirectories = ['.', '..'];
  634. if (is_dir($this->_cwd)) {
  635. $dir = $this->_cwd;
  636. } elseif (is_dir($this->_iwd)) {
  637. $dir = $this->_iwd;
  638. } else {
  639. throw new \Exception('Unable to list current working directory.');
  640. }
  641. $list = [];
  642. $dirHandler = opendir($dir);
  643. if ($dirHandler) {
  644. while (($entry = readdir($dirHandler)) !== false) {
  645. $listItem = [];
  646. $fullPath = $dir . '/' . $entry;
  647. if ($grep == self::GREP_DIRS && !is_dir($fullPath)) {
  648. continue;
  649. } elseif ($grep == self::GREP_FILES && !is_file($fullPath)) {
  650. continue;
  651. } elseif (in_array($entry, $ignoredDirectories)) {
  652. continue;
  653. }
  654. $listItem['text'] = $entry;
  655. $listItem['mod_date'] = date('Y-m-d H:i:s', filectime($fullPath));
  656. $listItem['permissions'] = $this->_parsePermissions(fileperms($fullPath));
  657. $listItem['owner'] = $this->_getFileOwner($fullPath);
  658. if (is_file($fullPath)) {
  659. $pathInfo = pathinfo($fullPath);
  660. $listItem['size'] = filesize($fullPath);
  661. $listItem['leaf'] = true;
  662. if (isset(
  663. $pathInfo['extension']
  664. ) && in_array(
  665. strtolower($pathInfo['extension']),
  666. ['jpg', 'jpeg', 'gif', 'bmp', 'png']
  667. ) && $listItem['size'] > 0
  668. ) {
  669. $listItem['is_image'] = true;
  670. $listItem['filetype'] = $pathInfo['extension'];
  671. } elseif ($listItem['size'] == 0) {
  672. $listItem['is_image'] = false;
  673. $listItem['filetype'] = 'unknown';
  674. } elseif (isset($pathInfo['extension'])) {
  675. $listItem['is_image'] = false;
  676. $listItem['filetype'] = $pathInfo['extension'];
  677. } else {
  678. $listItem['is_image'] = false;
  679. $listItem['filetype'] = 'unknown';
  680. }
  681. } else {
  682. $listItem['leaf'] = false;
  683. $listItem['id'] = $fullPath;
  684. }
  685. $list[] = $listItem;
  686. }
  687. closedir($dirHandler);
  688. } else {
  689. throw new \Exception('Unable to list current working directory. Access forbidden.');
  690. }
  691. return $list;
  692. }
  693. /**
  694. * Change directory to current working directory
  695. *
  696. * @return void
  697. */
  698. protected function _cwd()
  699. {
  700. if ($this->_cwd) {
  701. chdir($this->_cwd);
  702. }
  703. }
  704. /**
  705. * Change directory to initial directory
  706. *
  707. * @return void
  708. */
  709. protected function _iwd()
  710. {
  711. if ($this->_iwd) {
  712. chdir($this->_iwd);
  713. }
  714. }
  715. /**
  716. * Convert integer permissions format into human readable
  717. *
  718. * @param int $mode
  719. * @access protected
  720. * @return string
  721. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  722. * @SuppressWarnings(PHPMD.NPathComplexity)
  723. */
  724. protected function _parsePermissions($mode)
  725. {
  726. if ($mode & 0x1000) {
  727. $type = 'p'; /* FIFO pipe */
  728. } elseif ($mode & 0x2000) {
  729. $type = 'c'; /* Character special */
  730. } elseif ($mode & 0x4000) {
  731. $type = 'd'; /* \Directory */
  732. } elseif ($mode & 0x6000) {
  733. $type = 'b'; /* Block special */
  734. } elseif ($mode & 0x8000) {
  735. $type = '-'; /* Regular */
  736. } elseif ($mode & 0xA000) {
  737. $type = 'l'; /* Symbolic Link */
  738. } elseif ($mode & 0xC000) {
  739. $type = 's'; /* Socket */
  740. } else {
  741. $type = 'u'; /* UNKNOWN */
  742. }
  743. /* Determine permissions */
  744. $owner['read'] = $mode & 00400 ? 'r' : '-';
  745. $owner['write'] = $mode & 00200 ? 'w' : '-';
  746. $owner['execute'] = $mode & 00100 ? 'x' : '-';
  747. $group['read'] = $mode & 00040 ? 'r' : '-';
  748. $group['write'] = $mode & 00020 ? 'w' : '-';
  749. $group['execute'] = $mode & 00010 ? 'x' : '-';
  750. $world['read'] = $mode & 00004 ? 'r' : '-';
  751. $world['write'] = $mode & 00002 ? 'w' : '-';
  752. $world['execute'] = $mode & 00001 ? 'x' : '-';
  753. /* Adjust for SUID, SGID and sticky bit */
  754. if ($mode & 0x800) {
  755. $owner["execute"] = $owner['execute'] == 'x' ? 's' : 'S';
  756. }
  757. if ($mode & 0x400) {
  758. $group["execute"] = $group['execute'] == 'x' ? 's' : 'S';
  759. }
  760. if ($mode & 0x200) {
  761. $world["execute"] = $world['execute'] == 'x' ? 't' : 'T';
  762. }
  763. $s = sprintf('%1s', $type);
  764. $s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
  765. $s .= sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
  766. $s .= sprintf('%1s%1s%1s', $world['read'], $world['write'], $world['execute']);
  767. return trim($s);
  768. }
  769. /**
  770. * Get file owner
  771. *
  772. * @param string $filename
  773. * @return string
  774. */
  775. protected function _getFileOwner($filename)
  776. {
  777. if (!function_exists('posix_getpwuid')) {
  778. return 'n/a';
  779. }
  780. $owner = posix_getpwuid(fileowner($filename));
  781. $groupInfo = posix_getgrnam(filegroup($filename));
  782. return $owner['name'] . ' / ' . $groupInfo;
  783. }
  784. /**
  785. * Get directory separator
  786. *
  787. * @return string
  788. */
  789. public function dirsep()
  790. {
  791. return '/';
  792. }
  793. /**
  794. * Get directory name
  795. *
  796. * @param string $file
  797. * @return string
  798. */
  799. public function dirname($file)
  800. {
  801. return $this->getCleanPath(dirname($file));
  802. }
  803. /**
  804. * Get directories list by path\
  805. *
  806. * @param string $path
  807. * @param int $flag
  808. * @return array
  809. */
  810. public function getDirectoriesList($path, $flag = GLOB_ONLYDIR)
  811. {
  812. return glob($this->getCleanPath($path) . '*', $flag);
  813. }
  814. /**
  815. * Get path info
  816. *
  817. * @param string $path
  818. * @return mixed
  819. */
  820. public function getPathInfo($path)
  821. {
  822. return pathinfo($path);
  823. }
  824. }