FileByteStream.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 boolean $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. $this->_quotes = true;
  44. }
  45. }
  46. /**
  47. * Get the complete path to the file.
  48. *
  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.
  58. *
  59. * If less bytes exist than are requested the
  60. * remaining bytes are given instead. If no bytes are remaining at all, boolean
  61. * false is returned.
  62. *
  63. * @param integer $length
  64. *
  65. * @return string
  66. *
  67. * @throws Swift_IoException
  68. */
  69. public function read($length)
  70. {
  71. $fp = $this->_getReadHandle();
  72. if (!feof($fp)) {
  73. if ($this->_quotes) {
  74. ini_set('magic_quotes_runtime', 0);
  75. }
  76. $bytes = fread($fp, $length);
  77. if ($this->_quotes) {
  78. ini_set('magic_quotes_runtime', 1);
  79. }
  80. $this->_offset = ftell($fp);
  81. return $bytes;
  82. } else {
  83. $this->_resetReadHandle();
  84. return false;
  85. }
  86. }
  87. /**
  88. * Move the internal read pointer to $byteOffset in the stream.
  89. *
  90. * @param integer $byteOffset
  91. *
  92. * @return boolean
  93. */
  94. public function setReadPointer($byteOffset)
  95. {
  96. if (isset($this->_reader)) {
  97. $this->_seekReadStreamToPosition($byteOffset);
  98. }
  99. $this->_offset = $byteOffset;
  100. }
  101. // -- Private methods
  102. /** Just write the bytes to the file */
  103. protected function _commit($bytes)
  104. {
  105. fwrite($this->_getWriteHandle(), $bytes);
  106. $this->_resetReadHandle();
  107. }
  108. /** Not used */
  109. protected function _flush()
  110. {
  111. }
  112. /** Get the resource for reading */
  113. private function _getReadHandle()
  114. {
  115. if (!isset($this->_reader)) {
  116. if (!$this->_reader = fopen($this->_path, 'rb')) {
  117. throw new Swift_IoException(
  118. 'Unable to open file for reading [' . $this->_path . ']'
  119. );
  120. }
  121. if ($this->_offset <> 0) {
  122. $this->_getReadStreamSeekableStatus();
  123. $this->_seekReadStreamToPosition($this->_offset);
  124. }
  125. }
  126. return $this->_reader;
  127. }
  128. /** Get the resource for writing */
  129. private function _getWriteHandle()
  130. {
  131. if (!isset($this->_writer)) {
  132. if (!$this->_writer = fopen($this->_path, $this->_mode)) {
  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. fclose($this->_reader);
  145. $this->_reader = null;
  146. }
  147. }
  148. /** Check if ReadOnly Stream is seekable */
  149. private function _getReadStreamSeekableStatus()
  150. {
  151. $metas = stream_get_meta_data($this->_reader);
  152. $this->_seekable = $metas['seekable'];
  153. }
  154. /** Streams in a readOnly stream ensuring copy if needed */
  155. private function _seekReadStreamToPosition($offset)
  156. {
  157. if ($this->_seekable===null) {
  158. $this->_getReadStreamSeekableStatus();
  159. }
  160. if ($this->_seekable === false) {
  161. $currentPos = ftell($this->_reader);
  162. if ($currentPos<$offset) {
  163. $toDiscard = $offset-$currentPos;
  164. fread($this->_reader, $toDiscard);
  165. return;
  166. }
  167. $this->_copyReadStream();
  168. }
  169. fseek($this->_reader, $offset, SEEK_SET);
  170. }
  171. /** Copy a readOnly Stream to ensure seekability */
  172. private function _copyReadStream()
  173. {
  174. if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) {
  175. /* We have opened a php:// Stream Should work without problem */
  176. } elseif (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) {
  177. /* We have opened a tmpfile */
  178. } else {
  179. throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
  180. }
  181. $currentPos = ftell($this->_reader);
  182. fclose($this->_reader);
  183. $source = fopen($this->_path, 'rb');
  184. if (!$source) {
  185. throw new Swift_IoException('Unable to open file for copying [' . $this->_path . ']');
  186. }
  187. fseek($tmpFile, 0, SEEK_SET);
  188. while (!feof($source)) {
  189. fwrite($tmpFile, fread($source, 4096));
  190. }
  191. fseek($tmpFile, $currentPos, SEEK_SET);
  192. fclose($source);
  193. $this->_reader = $tmpFile;
  194. }
  195. }