PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jonathanselander/magento2
PHP | 276 lines | 194 code | 11 blank | 71 comment | 5 complexity | a735bd2cd6ee522b710a467d9bae2f4a MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Unlicense
  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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Magento_Archive
  23. * @copyright Copyright (c) 2013 X.commerce, Inc. (http://www.magentocommerce.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 Magento
  30. * @package Magento_Archive
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. namespace Magento\Archive\Helper;
  34. class File
  35. {
  36. /**
  37. * Full path to directory where file located
  38. *
  39. * @var string
  40. */
  41. protected $_fileLocation;
  42. /**
  43. * File name
  44. *
  45. * @var string
  46. */
  47. protected $_fileName;
  48. /**
  49. * Full path (directory + filename) to file
  50. *
  51. * @var string
  52. */
  53. protected $_filePath;
  54. /**
  55. * File permissions that will be set if file opened in write mode
  56. *
  57. * @var int
  58. */
  59. protected $_chmod;
  60. /**
  61. * File handler
  62. *
  63. * @var pointer
  64. */
  65. protected $_fileHandler;
  66. /**
  67. * Set file path via constructor
  68. *
  69. * @param string $filePath
  70. */
  71. public function __construct($filePath)
  72. {
  73. $pathInfo = pathinfo($filePath);
  74. $this->_filePath = $filePath;
  75. $this->_fileLocation = isset($pathInfo['dirname']) ? $pathInfo['dirname'] : '';
  76. $this->_fileName = isset($pathInfo['basename']) ? $pathInfo['basename'] : '';
  77. }
  78. /**
  79. * Close file if it's not closed before object destruction
  80. */
  81. public function __destruct()
  82. {
  83. if ($this->_fileHandler) {
  84. $this->_close();
  85. }
  86. }
  87. /**
  88. * Open file
  89. *
  90. * @param string $mode
  91. * @param int $chmod
  92. * @throws \Magento\Exception
  93. */
  94. public function open($mode = 'w+', $chmod = 0666)
  95. {
  96. if ($this->_isWritableMode($mode)) {
  97. if (!is_writable($this->_fileLocation)) {
  98. throw new \Magento\Exception('Permission denied to write to ' . $this->_fileLocation);
  99. }
  100. if (is_file($this->_filePath) && !is_writable($this->_filePath)) {
  101. throw new \Magento\Exception("Can't open file " . $this->_fileName . " for writing. Permission denied.");
  102. }
  103. }
  104. if ($this->_isReadableMode($mode) && (!is_file($this->_filePath) || !is_readable($this->_filePath))) {
  105. if (!is_file($this->_filePath)) {
  106. throw new \Magento\Exception('File ' . $this->_filePath . ' does not exist');
  107. }
  108. if (!is_readable($this->_filePath)) {
  109. throw new \Magento\Exception('Permission denied to read file ' . $this->_filePath);
  110. }
  111. }
  112. $this->_open($mode);
  113. $this->_chmod = $chmod;
  114. }
  115. /**
  116. * Write data to file
  117. *
  118. * @param string $data
  119. */
  120. public function write($data)
  121. {
  122. $this->_checkFileOpened();
  123. $this->_write($data);
  124. }
  125. /**
  126. * Read data from file
  127. *
  128. * @param int $length
  129. * @return string|boolean
  130. */
  131. public function read($length = 4096)
  132. {
  133. $data = false;
  134. $this->_checkFileOpened();
  135. if ($length > 0) {
  136. $data = $this->_read($length);
  137. }
  138. return $data;
  139. }
  140. /**
  141. * Check whether end of file reached
  142. *
  143. * @return boolean
  144. */
  145. public function eof()
  146. {
  147. $this->_checkFileOpened();
  148. return $this->_eof();
  149. }
  150. /**
  151. * Close file
  152. */
  153. public function close()
  154. {
  155. $this->_checkFileOpened();
  156. $this->_close();
  157. $this->_fileHandler = false;
  158. @chmod($this->_filePath, $this->_chmod);
  159. }
  160. /**
  161. * Implementation of file opening
  162. *
  163. * @param string $mode
  164. * @throws \Magento\Exception
  165. */
  166. protected function _open($mode)
  167. {
  168. $this->_fileHandler = @fopen($this->_filePath, $mode);
  169. if (false === $this->_fileHandler) {
  170. throw new \Magento\Exception('Failed to open file ' . $this->_filePath);
  171. }
  172. }
  173. /**
  174. * Implementation of writing data to file
  175. *
  176. * @param string $data
  177. * @throws \Magento\Exception
  178. */
  179. protected function _write($data)
  180. {
  181. $result = @fwrite($this->_fileHandler, $data);
  182. if (false === $result) {
  183. throw new \Magento\Exception('Failed to write data to ' . $this->_filePath);
  184. }
  185. }
  186. /**
  187. * Implementation of file reading
  188. *
  189. * @param int $length
  190. * @throws \Magento\Exception
  191. */
  192. protected function _read($length)
  193. {
  194. $result = fread($this->_fileHandler, $length);
  195. if (false === $result) {
  196. throw new \Magento\Exception('Failed to read data from ' . $this->_filePath);
  197. }
  198. return $result;
  199. }
  200. /**
  201. * Implementation of EOF indicator
  202. *
  203. * @return boolean
  204. */
  205. protected function _eof()
  206. {
  207. return feof($this->_fileHandler);
  208. }
  209. /**
  210. * Implementation of file closing
  211. */
  212. protected function _close()
  213. {
  214. fclose($this->_fileHandler);
  215. }
  216. /**
  217. * Check whether requested mode is writable mode
  218. *
  219. * @param string $mode
  220. */
  221. protected function _isWritableMode($mode)
  222. {
  223. return preg_match('/(^[waxc])|(\+$)/', $mode);
  224. }
  225. /**
  226. * Check whether requested mode is readable mode
  227. *
  228. * @param string $mode
  229. */
  230. protected function _isReadableMode($mode) {
  231. return !$this->_isWritableMode($mode);
  232. }
  233. /**
  234. * Check whether file is opened
  235. *
  236. * @throws \Magento\Exception
  237. */
  238. protected function _checkFileOpened()
  239. {
  240. if (!$this->_fileHandler) {
  241. throw new \Magento\Exception('File not opened');
  242. }
  243. }
  244. }