PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/downloader/lib/Mage/Archive/Helper/File.php

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 274 lines | 193 code | 10 blank | 71 comment | 5 complexity | 4c30a28a27cad11c32445cd077515b8f MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Archive
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Helper class that simplifies files stream reading and writing
  28. *
  29. * @category Mage
  30. * @package Mage_Archive
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Archive_Helper_File
  34. {
  35. /**
  36. * Full path to directory where file located
  37. *
  38. * @var string
  39. */
  40. protected $_fileLocation;
  41. /**
  42. * File name
  43. *
  44. * @var string
  45. */
  46. protected $_fileName;
  47. /**
  48. * Full path (directory + filename) to file
  49. *
  50. * @var string
  51. */
  52. protected $_filePath;
  53. /**
  54. * File permissions that will be set if file opened in write mode
  55. *
  56. * @var int
  57. */
  58. protected $_chmod;
  59. /**
  60. * File handler
  61. *
  62. * @var pointer
  63. */
  64. protected $_fileHandler;
  65. /**
  66. * Set file path via constructor
  67. *
  68. * @param string $filePath
  69. */
  70. public function __construct($filePath)
  71. {
  72. $pathInfo = pathinfo($filePath);
  73. $this->_filePath = $filePath;
  74. $this->_fileLocation = isset($pathInfo['dirname']) ? $pathInfo['dirname'] : '';
  75. $this->_fileName = isset($pathInfo['basename']) ? $pathInfo['basename'] : '';
  76. }
  77. /**
  78. * Close file if it's not closed before object destruction
  79. */
  80. public function __destruct()
  81. {
  82. if ($this->_fileHandler) {
  83. $this->_close();
  84. }
  85. }
  86. /**
  87. * Open file
  88. *
  89. * @param string $mode
  90. * @param int $chmod
  91. * @throws Mage_Exception
  92. */
  93. public function open($mode = 'w+', $chmod = 0666)
  94. {
  95. if ($this->_isWritableMode($mode)) {
  96. if (!is_writable($this->_fileLocation)) {
  97. throw new Mage_Exception('Permission denied to write to ' . $this->_fileLocation);
  98. }
  99. if (is_file($this->_filePath) && !is_writable($this->_filePath)) {
  100. throw new Mage_Exception("Can't open file " . $this->_fileName . " for writing. Permission denied.");
  101. }
  102. }
  103. if ($this->_isReadableMode($mode) && (!is_file($this->_filePath) || !is_readable($this->_filePath))) {
  104. if (!is_file($this->_filePath)) {
  105. throw new Mage_Exception('File ' . $this->_filePath . ' does not exist');
  106. }
  107. if (!is_readable($this->_filePath)) {
  108. throw new Mage_Exception('Permission denied to read file ' . $this->_filePath);
  109. }
  110. }
  111. $this->_open($mode);
  112. $this->_chmod = $chmod;
  113. }
  114. /**
  115. * Write data to file
  116. *
  117. * @param string $data
  118. */
  119. public function write($data)
  120. {
  121. $this->_checkFileOpened();
  122. $this->_write($data);
  123. }
  124. /**
  125. * Read data from file
  126. *
  127. * @param int $length
  128. * @return string|boolean
  129. */
  130. public function read($length = 4096)
  131. {
  132. $data = false;
  133. $this->_checkFileOpened();
  134. if ($length > 0) {
  135. $data = $this->_read($length);
  136. }
  137. return $data;
  138. }
  139. /**
  140. * Check whether end of file reached
  141. *
  142. * @return boolean
  143. */
  144. public function eof()
  145. {
  146. $this->_checkFileOpened();
  147. return $this->_eof();
  148. }
  149. /**
  150. * Close file
  151. */
  152. public function close()
  153. {
  154. $this->_checkFileOpened();
  155. $this->_close();
  156. $this->_fileHandler = false;
  157. @chmod($this->_filePath, $this->_chmod);
  158. }
  159. /**
  160. * Implementation of file opening
  161. *
  162. * @param string $mode
  163. * @throws Mage_Exception
  164. */
  165. protected function _open($mode)
  166. {
  167. $this->_fileHandler = @fopen($this->_filePath, $mode);
  168. if (false === $this->_fileHandler) {
  169. throw new Mage_Exception('Failed to open file ' . $this->_filePath);
  170. }
  171. }
  172. /**
  173. * Implementation of writing data to file
  174. *
  175. * @param string $data
  176. * @throws Mage_Exception
  177. */
  178. protected function _write($data)
  179. {
  180. $result = @fwrite($this->_fileHandler, $data);
  181. if (false === $result) {
  182. throw new Mage_Exception('Failed to write data to ' . $this->_filePath);
  183. }
  184. }
  185. /**
  186. * Implementation of file reading
  187. *
  188. * @param int $length
  189. * @throws Mage_Exception
  190. */
  191. protected function _read($length)
  192. {
  193. $result = fread($this->_fileHandler, $length);
  194. if (false === $result) {
  195. throw new Mage_Exception('Failed to read data from ' . $this->_filePath);
  196. }
  197. return $result;
  198. }
  199. /**
  200. * Implementation of EOF indicator
  201. *
  202. * @return boolean
  203. */
  204. protected function _eof()
  205. {
  206. return feof($this->_fileHandler);
  207. }
  208. /**
  209. * Implementation of file closing
  210. */
  211. protected function _close()
  212. {
  213. fclose($this->_fileHandler);
  214. }
  215. /**
  216. * Check whether requested mode is writable mode
  217. *
  218. * @param string $mode
  219. */
  220. protected function _isWritableMode($mode)
  221. {
  222. return preg_match('/(^[waxc])|(\+$)/', $mode);
  223. }
  224. /**
  225. * Check whether requested mode is readable mode
  226. *
  227. * @param string $mode
  228. */
  229. protected function _isReadableMode($mode) {
  230. return !$this->_isWritableMode($mode);
  231. }
  232. /**
  233. * Check whether file is opened
  234. *
  235. * @throws Mage_Exception
  236. */
  237. protected function _checkFileOpened()
  238. {
  239. if (!$this->_fileHandler) {
  240. throw new Mage_Exception('File not opened');
  241. }
  242. }
  243. }