/apps/libraries/swift_mailer/classes/Swift/ByteStream/FileByteStream.php

https://gitlab.com/dsasmita/talita-shop · PHP · 233 lines · 137 code · 30 blank · 66 comment · 25 complexity · 53c1a21b90fbb848f41f1689d1bf7c4c MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Allows reading and writing of bytes to and from a file.
  11. *
  12. * @package Swift
  13. * @subpackage ByteStream
  14. * @author Chris Corbyn
  15. */
  16. class Swift_ByteStream_FileByteStream extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_FileStream
  17. {
  18. /** The internal pointer offset */
  19. private $_offset = 0;
  20. /** The path to the file */
  21. private $_path;
  22. /** The mode this file is opened in for writing */
  23. private $_mode;
  24. /** A lazy-loaded resource handle for reading the file */
  25. private $_reader;
  26. /** A lazy-loaded resource handle for writing the file */
  27. private $_writer;
  28. /** If magic_quotes_runtime is on, this will be true */
  29. private $_quotes = false;
  30. /** If stream is seekable true/false, or null if not known */
  31. private $_seekable = null;
  32. /**
  33. * Create a new FileByteStream for $path.
  34. *
  35. * @param string $path
  36. * @param bool $writable if true
  37. */
  38. public function __construct($path, $writable = false)
  39. {
  40. if (empty($path)) {
  41. throw new Swift_IoException('The path cannot be empty');
  42. }
  43. $this->_path = $path;
  44. $this->_mode = $writable ? 'w+b' : 'rb';
  45. if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1) {
  46. $this->_quotes = true;
  47. }
  48. }
  49. /**
  50. * Get the complete path to the file.
  51. *
  52. * @return string
  53. */
  54. public function getPath()
  55. {
  56. return $this->_path;
  57. }
  58. /**
  59. * Reads $length bytes from the stream into a string and moves the pointer
  60. * through the stream by $length.
  61. *
  62. * If less bytes exist than are requested the
  63. * remaining bytes are given instead. If no bytes are remaining at all, boolean
  64. * false is returned.
  65. *
  66. * @param int $length
  67. *
  68. * @return string|bool
  69. *
  70. * @throws Swift_IoException
  71. */
  72. public function read($length)
  73. {
  74. $fp = $this->_getReadHandle();
  75. if (!feof($fp)) {
  76. if ($this->_quotes) {
  77. ini_set('magic_quotes_runtime', 0);
  78. }
  79. $bytes = fread($fp, $length);
  80. if ($this->_quotes) {
  81. ini_set('magic_quotes_runtime', 1);
  82. }
  83. $this->_offset = ftell($fp);
  84. // If we read one byte after reaching the end of the file
  85. // feof() will return false and an empty string is returned
  86. if ($bytes === '' && feof($fp)) {
  87. $this->_resetReadHandle();
  88. return false;
  89. }
  90. return $bytes;
  91. }
  92. $this->_resetReadHandle();
  93. return false;
  94. }
  95. /**
  96. * Move the internal read pointer to $byteOffset in the stream.
  97. *
  98. * @param int $byteOffset
  99. *
  100. * @return bool
  101. */
  102. public function setReadPointer($byteOffset)
  103. {
  104. if (isset($this->_reader)) {
  105. $this->_seekReadStreamToPosition($byteOffset);
  106. }
  107. $this->_offset = $byteOffset;
  108. }
  109. // -- Private methods
  110. /** Just write the bytes to the file */
  111. protected function _commit($bytes)
  112. {
  113. fwrite($this->_getWriteHandle(), $bytes);
  114. $this->_resetReadHandle();
  115. }
  116. /** Not used */
  117. protected function _flush()
  118. {
  119. }
  120. /** Get the resource for reading */
  121. private function _getReadHandle()
  122. {
  123. if (!isset($this->_reader)) {
  124. if (!$this->_reader = fopen($this->_path, 'rb')) {
  125. throw new Swift_IoException(
  126. 'Unable to open file for reading [' . $this->_path . ']'
  127. );
  128. }
  129. if ($this->_offset <> 0) {
  130. $this->_getReadStreamSeekableStatus();
  131. $this->_seekReadStreamToPosition($this->_offset);
  132. }
  133. }
  134. return $this->_reader;
  135. }
  136. /** Get the resource for writing */
  137. private function _getWriteHandle()
  138. {
  139. if (!isset($this->_writer)) {
  140. if (!$this->_writer = fopen($this->_path, $this->_mode)) {
  141. throw new Swift_IoException(
  142. 'Unable to open file for writing [' . $this->_path . ']'
  143. );
  144. }
  145. }
  146. return $this->_writer;
  147. }
  148. /** Force a reload of the resource for reading */
  149. private function _resetReadHandle()
  150. {
  151. if (isset($this->_reader)) {
  152. fclose($this->_reader);
  153. $this->_reader = null;
  154. }
  155. }
  156. /** Check if ReadOnly Stream is seekable */
  157. private function _getReadStreamSeekableStatus()
  158. {
  159. $metas = stream_get_meta_data($this->_reader);
  160. $this->_seekable = $metas['seekable'];
  161. }
  162. /** Streams in a readOnly stream ensuring copy if needed */
  163. private function _seekReadStreamToPosition($offset)
  164. {
  165. if ($this->_seekable===null) {
  166. $this->_getReadStreamSeekableStatus();
  167. }
  168. if ($this->_seekable === false) {
  169. $currentPos = ftell($this->_reader);
  170. if ($currentPos<$offset) {
  171. $toDiscard = $offset-$currentPos;
  172. fread($this->_reader, $toDiscard);
  173. return;
  174. }
  175. $this->_copyReadStream();
  176. }
  177. fseek($this->_reader, $offset, SEEK_SET);
  178. }
  179. /** Copy a readOnly Stream to ensure seekability */
  180. private function _copyReadStream()
  181. {
  182. if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) {
  183. /* We have opened a php:// Stream Should work without problem */
  184. } elseif (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) {
  185. /* We have opened a tmpfile */
  186. } else {
  187. throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
  188. }
  189. $currentPos = ftell($this->_reader);
  190. fclose($this->_reader);
  191. $source = fopen($this->_path, 'rb');
  192. if (!$source) {
  193. throw new Swift_IoException('Unable to open file for copying [' . $this->_path . ']');
  194. }
  195. fseek($tmpFile, 0, SEEK_SET);
  196. while (!feof($source)) {
  197. fwrite($tmpFile, fread($source, 4096));
  198. }
  199. fseek($tmpFile, $currentPos, SEEK_SET);
  200. fclose($source);
  201. $this->_reader = $tmpFile;
  202. }
  203. }