/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php

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