FileByteStream.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1)
  43. {
  44. $this->_quotes = true;
  45. }
  46. }
  47. /**
  48. * Get the complete path to the file.
  49. * @return string
  50. */
  51. public function getPath()
  52. {
  53. return $this->_path;
  54. }
  55. /**
  56. * Reads $length bytes from the stream into a string and moves the pointer
  57. * through the stream by $length. If less bytes exist than are requested the
  58. * remaining bytes are given instead. If no bytes are remaining at all, boolean
  59. * false is returned.
  60. * @param int $length
  61. * @return string
  62. * @throws Swift_IoException
  63. */
  64. public function read($length)
  65. {
  66. $fp = $this->_getReadHandle();
  67. if (!feof($fp))
  68. {
  69. if ($this->_quotes)
  70. {
  71. ini_set('magic_quotes_runtime', 0);
  72. }
  73. $bytes = fread($fp, $length);
  74. if ($this->_quotes)
  75. {
  76. ini_set('magic_quotes_runtime', 1);
  77. }
  78. $this->_offset = ftell($fp);
  79. return $bytes;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Move the internal read pointer to $byteOffset in the stream.
  88. * @param int $byteOffset
  89. * @return boolean
  90. */
  91. public function setReadPointer($byteOffset)
  92. {
  93. if (isset($this->_reader))
  94. {
  95. $this->_seekReadStreamToPosition($byteOffset);
  96. }
  97. $this->_offset = $byteOffset;
  98. }
  99. // -- Private methods
  100. /** Just write the bytes to the file */
  101. protected function _commit($bytes)
  102. {
  103. fwrite($this->_getWriteHandle(), $bytes);
  104. $this->_resetReadHandle();
  105. }
  106. /** Not used */
  107. protected function _flush()
  108. {
  109. }
  110. /** Get the resource for reading */
  111. private function _getReadHandle()
  112. {
  113. if (!isset($this->_reader))
  114. {
  115. if (!$this->_reader = fopen($this->_path, 'rb'))
  116. {
  117. throw new Swift_IoException(
  118. 'Unable to open file for reading [' . $this->_path . ']'
  119. );
  120. }
  121. if ($this->_offset <> 0)
  122. {
  123. $this->_getReadStreamSeekableStatus();
  124. $this->_seekReadStreamToPosition($this->_offset);
  125. }
  126. }
  127. return $this->_reader;
  128. }
  129. /** Get the resource for writing */
  130. private function _getWriteHandle()
  131. {
  132. if (!isset($this->_writer))
  133. {
  134. if (!$this->_writer = fopen($this->_path, $this->_mode))
  135. {
  136. throw new Swift_IoException(
  137. 'Unable to open file for writing [' . $this->_path . ']'
  138. );
  139. }
  140. }
  141. return $this->_writer;
  142. }
  143. /** Force a reload of the resource for reading */
  144. private function _resetReadHandle()
  145. {
  146. if (isset($this->_reader))
  147. {
  148. fclose($this->_reader);
  149. $this->_reader = null;
  150. }
  151. }
  152. /** Check if ReadOnly Stream is seekable */
  153. private function _getReadStreamSeekableStatus()
  154. {
  155. $metas = stream_get_meta_data($this->_reader);
  156. $this->_seekable = $metas['seekable'];
  157. }
  158. /** Streams in a readOnly stream ensuring copy if needed */
  159. private function _seekReadStreamToPosition($offset)
  160. {
  161. if ($this->_seekable===null)
  162. {
  163. $this->_getReadStreamSeekableStatus();
  164. }
  165. if ($this->_seekable === false)
  166. {
  167. $currentPos = ftell($this->_reader);
  168. if ($currentPos<$offset)
  169. {
  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. {
  183. /* We have opened a php:// Stream Should work without problem */
  184. }
  185. elseif (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile()))
  186. {
  187. /* We have opened a tmpfile */
  188. }
  189. else
  190. {
  191. throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
  192. }
  193. $currentPos = ftell($this->_reader);
  194. fclose($this->_reader);
  195. $source = fopen($this->_path, 'rb');
  196. if (!$source)
  197. {
  198. throw new Swift_IoException('Unable to open file for copying [' . $this->_path . ']');
  199. }
  200. fseek($tmpFile, 0, SEEK_SET);
  201. while (!feof($source))
  202. {
  203. fwrite($tmpFile, fread($source, 4096));
  204. }
  205. fseek($tmpFile, $currentPos, SEEK_SET);
  206. fclose($source);
  207. $this->_reader = $tmpFile;
  208. }
  209. }