FileByteStream.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $this->_resetReadHandle();
  84. return false;
  85. }
  86. }
  87. /**
  88. * Move the internal read pointer to $byteOffset in the stream.
  89. * @param int $byteOffset
  90. * @return boolean
  91. */
  92. public function setReadPointer($byteOffset)
  93. {
  94. if (isset($this->_reader))
  95. {
  96. $this->_seekReadStreamToPosition($byteOffset);
  97. }
  98. $this->_offset = $byteOffset;
  99. }
  100. // -- Private methods
  101. /** Just write the bytes to the file */
  102. protected function _commit($bytes)
  103. {
  104. fwrite($this->_getWriteHandle(), $bytes);
  105. $this->_resetReadHandle();
  106. }
  107. /** Not used */
  108. protected function _flush()
  109. {
  110. }
  111. /** Get the resource for reading */
  112. private function _getReadHandle()
  113. {
  114. if (!isset($this->_reader))
  115. {
  116. if (!$this->_reader = fopen($this->_path, 'rb'))
  117. {
  118. throw new Swift_IoException(
  119. 'Unable to open file for reading [' . $this->_path . ']'
  120. );
  121. }
  122. if ($this->_offset <> 0)
  123. {
  124. $this->_getReadStreamSeekableStatus();
  125. $this->_seekReadStreamToPosition($this->_offset);
  126. }
  127. }
  128. return $this->_reader;
  129. }
  130. /** Get the resource for writing */
  131. private function _getWriteHandle()
  132. {
  133. if (!isset($this->_writer))
  134. {
  135. if (!$this->_writer = fopen($this->_path, $this->_mode))
  136. {
  137. throw new Swift_IoException(
  138. 'Unable to open file for writing [' . $this->_path . ']'
  139. );
  140. }
  141. }
  142. return $this->_writer;
  143. }
  144. /** Force a reload of the resource for reading */
  145. private function _resetReadHandle()
  146. {
  147. if (isset($this->_reader))
  148. {
  149. fclose($this->_reader);
  150. $this->_reader = null;
  151. }
  152. }
  153. /** Check if ReadOnly Stream is seekable */
  154. private function _getReadStreamSeekableStatus()
  155. {
  156. $metas = stream_get_meta_data($this->_reader);
  157. $this->_seekable = $metas['seekable'];
  158. }
  159. /** Streams in a readOnly stream ensuring copy if needed */
  160. private function _seekReadStreamToPosition($offset)
  161. {
  162. if ($this->_seekable===null)
  163. {
  164. $this->_getReadStreamSeekableStatus();
  165. }
  166. if ($this->_seekable === false)
  167. {
  168. $currentPos = ftell($this->_reader);
  169. if ($currentPos<$offset)
  170. {
  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. {
  184. /* We have opened a php:// Stream Should work without problem */
  185. }
  186. elseif (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile()))
  187. {
  188. /* We have opened a tmpfile */
  189. }
  190. else
  191. {
  192. throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
  193. }
  194. $currentPos = ftell($this->_reader);
  195. fclose($this->_reader);
  196. $source = fopen($this->_path, 'rb');
  197. if (!$source)
  198. {
  199. throw new Swift_IoException('Unable to open file for copying [' . $this->_path . ']');
  200. }
  201. fseek($tmpFile, 0, SEEK_SET);
  202. while (!feof($source))
  203. {
  204. fwrite($tmpFile, fread($source, 4096));
  205. }
  206. fseek($tmpFile, $currentPos, SEEK_SET);
  207. fclose($source);
  208. $this->_reader = $tmpFile;
  209. }
  210. }