FileByteStream.php 5.5KB

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