FileByteStream.php 5.9 KB

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