FileByteStream.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_FileStream
  16. {
  17. /** The internal pointer offset */
  18. private $_offset = 0;
  19. /** The path to the file */
  20. private $_path;
  21. /** The mode this file is opened in for writing */
  22. private $_mode;
  23. /** A lazy-loaded resource handle for reading the file */
  24. private $_reader;
  25. /** A lazy-loaded resource handle for writing the file */
  26. private $_writer;
  27. /** If magic_quotes_runtime is on, this will be true */
  28. private $_quotes = false;
  29. /** If stream is seekable true/false, or null if not known */
  30. private $_seekable = null;
  31. /**
  32. * Create a new FileByteStream for $path.
  33. * @param string $path
  34. * @param string $writable if true
  35. */
  36. public function __construct($path, $writable = false)
  37. {
  38. $this->_path = $path;
  39. $this->_mode = $writable ? 'w+b' : 'rb';
  40. if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1) {
  41. $this->_quotes = true;
  42. }
  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. if ($this->_quotes) {
  66. ini_set('magic_quotes_runtime', 0);
  67. }
  68. $bytes = fread($fp, $length);
  69. if ($this->_quotes) {
  70. ini_set('magic_quotes_runtime', 1);
  71. }
  72. $this->_offset = ftell($fp);
  73. return $bytes;
  74. } else {
  75. $this->_resetReadHandle();
  76. return false;
  77. }
  78. }
  79. /**
  80. * Move the internal read pointer to $byteOffset in the stream.
  81. * @param int $byteOffset
  82. * @return boolean
  83. */
  84. public function setReadPointer($byteOffset)
  85. {
  86. if (isset($this->_reader)) {
  87. $this->_seekReadStreamToPosition($byteOffset);
  88. }
  89. $this->_offset = $byteOffset;
  90. }
  91. // -- Private methods
  92. /** Just write the bytes to the file */
  93. protected function _commit($bytes)
  94. {
  95. fwrite($this->_getWriteHandle(), $bytes);
  96. $this->_resetReadHandle();
  97. }
  98. /** Not used */
  99. protected function _flush()
  100. {
  101. }
  102. /** Get the resource for reading */
  103. private function _getReadHandle()
  104. {
  105. if (!isset($this->_reader)) {
  106. if (!$this->_reader = fopen($this->_path, 'rb')) {
  107. throw new Swift_IoException(
  108. 'Unable to open file for reading [' . $this->_path . ']'
  109. );
  110. }
  111. if ($this->_offset <> 0) {
  112. $this->_getReadStreamSeekableStatus();
  113. $this->_seekReadStreamToPosition($this->_offset);
  114. }
  115. }
  116. return $this->_reader;
  117. }
  118. /** Get the resource for writing */
  119. private function _getWriteHandle()
  120. {
  121. if (!isset($this->_writer)) {
  122. if (!$this->_writer = fopen($this->_path, $this->_mode)) {
  123. throw new Swift_IoException(
  124. 'Unable to open file for writing [' . $this->_path . ']'
  125. );
  126. }
  127. }
  128. return $this->_writer;
  129. }
  130. /** Force a reload of the resource for reading */
  131. private function _resetReadHandle()
  132. {
  133. if (isset($this->_reader)) {
  134. fclose($this->_reader);
  135. $this->_reader = null;
  136. }
  137. }
  138. /** Check if ReadOnly Stream is seekable */
  139. private function _getReadStreamSeekableStatus()
  140. {
  141. $metas = stream_get_meta_data($this->_reader);
  142. $this->_seekable = $metas['seekable'];
  143. }
  144. /** Streams in a readOnly stream ensuring copy if needed */
  145. private function _seekReadStreamToPosition($offset)
  146. {
  147. if ($this->_seekable===null) {
  148. $this->_getReadStreamSeekableStatus();
  149. }
  150. if ($this->_seekable === false) {
  151. $currentPos = ftell($this->_reader);
  152. if ($currentPos<$offset) {
  153. $toDiscard = $offset-$currentPos;
  154. fread($this->_reader, $toDiscard);
  155. return;
  156. }
  157. $this->_copyReadStream();
  158. }
  159. fseek($this->_reader, $offset, SEEK_SET);
  160. }
  161. /** Copy a readOnly Stream to ensure seekability */
  162. private function _copyReadStream()
  163. {
  164. if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) {
  165. /* We have opened a php:// Stream Should work without problem */
  166. } elseif (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) {
  167. /* We have opened a tmpfile */
  168. } else {
  169. throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
  170. }
  171. $currentPos = ftell($this->_reader);
  172. fclose($this->_reader);
  173. $source = fopen($this->_path, 'rb');
  174. if (!$source) {
  175. throw new Swift_IoException('Unable to open file for copying [' . $this->_path . ']');
  176. }
  177. fseek($tmpFile, 0, SEEK_SET);
  178. while (!feof($source)) {
  179. fwrite($tmpFile, fread($source, 4096));
  180. }
  181. fseek($tmpFile, $currentPos, SEEK_SET);
  182. fclose($source);
  183. $this->_reader = $tmpFile;
  184. }
  185. }