PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/SwiftMailer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php

https://gitlab.com/fiesta-framework/Mail
PHP | 229 lines | 137 code | 29 blank | 63 comment | 26 complexity | 4830365f62bfe3711905b645395b3572 MD5 | raw file
  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 magic_quotes_runtime is on, this will be true */
  27. private $_quotes = false;
  28. /** If stream is seekable true/false, or null if not known */
  29. private $_seekable = null;
  30. /**
  31. * Create a new FileByteStream for $path.
  32. *
  33. * @param string $path
  34. * @param bool $writable if true
  35. */
  36. public function __construct($path, $writable = false)
  37. {
  38. if (empty($path)) {
  39. throw new Swift_IoException('The path cannot be empty');
  40. }
  41. $this->_path = $path;
  42. $this->_mode = $writable ? 'w+b' : 'rb';
  43. if (function_exists('get_magic_quotes_runtime') && @get_magic_quotes_runtime() == 1) {
  44. $this->_quotes = true;
  45. }
  46. }
  47. /**
  48. * Get the complete path to the file.
  49. *
  50. * @return string
  51. */
  52. public function getPath()
  53. {
  54. return $this->_path;
  55. }
  56. /**
  57. * Reads $length bytes from the stream into a string and moves the pointer
  58. * through the stream by $length.
  59. *
  60. * If less bytes exist than are requested the
  61. * remaining bytes are given instead. If no bytes are remaining at all, boolean
  62. * false is returned.
  63. *
  64. * @param int $length
  65. *
  66. * @return string|bool
  67. *
  68. * @throws Swift_IoException
  69. */
  70. public function read($length)
  71. {
  72. $fp = $this->_getReadHandle();
  73. if (!feof($fp)) {
  74. if ($this->_quotes) {
  75. ini_set('magic_quotes_runtime', 0);
  76. }
  77. $bytes = fread($fp, $length);
  78. if ($this->_quotes) {
  79. ini_set('magic_quotes_runtime', 1);
  80. }
  81. $this->_offset = ftell($fp);
  82. // If we read one byte after reaching the end of the file
  83. // feof() will return false and an empty string is returned
  84. if ($bytes === '' && feof($fp)) {
  85. $this->_resetReadHandle();
  86. return false;
  87. }
  88. return $bytes;
  89. }
  90. $this->_resetReadHandle();
  91. return false;
  92. }
  93. /**
  94. * Move the internal read pointer to $byteOffset in the stream.
  95. *
  96. * @param int $byteOffset
  97. *
  98. * @return bool
  99. */
  100. public function setReadPointer($byteOffset)
  101. {
  102. if (isset($this->_reader)) {
  103. $this->_seekReadStreamToPosition($byteOffset);
  104. }
  105. $this->_offset = $byteOffset;
  106. }
  107. /** Just write the bytes to the file */
  108. protected function _commit($bytes)
  109. {
  110. fwrite($this->_getWriteHandle(), $bytes);
  111. $this->_resetReadHandle();
  112. }
  113. /** Not used */
  114. protected function _flush()
  115. {
  116. }
  117. /** Get the resource for reading */
  118. private function _getReadHandle()
  119. {
  120. if (!isset($this->_reader)) {
  121. if (!$this->_reader = fopen($this->_path, 'rb')) {
  122. throw new Swift_IoException(
  123. 'Unable to open file for reading ['.$this->_path.']'
  124. );
  125. }
  126. if ($this->_offset != 0) {
  127. $this->_getReadStreamSeekableStatus();
  128. $this->_seekReadStreamToPosition($this->_offset);
  129. }
  130. }
  131. return $this->_reader;
  132. }
  133. /** Get the resource for writing */
  134. private function _getWriteHandle()
  135. {
  136. if (!isset($this->_writer)) {
  137. if (!$this->_writer = fopen($this->_path, $this->_mode)) {
  138. throw new Swift_IoException(
  139. 'Unable to open file for writing ['.$this->_path.']'
  140. );
  141. }
  142. }
  143. return $this->_writer;
  144. }
  145. /** Force a reload of the resource for reading */
  146. private function _resetReadHandle()
  147. {
  148. if (isset($this->_reader)) {
  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. $this->_getReadStreamSeekableStatus();
  164. }
  165. if ($this->_seekable === false) {
  166. $currentPos = ftell($this->_reader);
  167. if ($currentPos<$offset) {
  168. $toDiscard = $offset-$currentPos;
  169. fread($this->_reader, $toDiscard);
  170. return;
  171. }
  172. $this->_copyReadStream();
  173. }
  174. fseek($this->_reader, $offset, SEEK_SET);
  175. }
  176. /** Copy a readOnly Stream to ensure seekability */
  177. private function _copyReadStream()
  178. {
  179. if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) {
  180. /* We have opened a php:// Stream Should work without problem */
  181. } elseif (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) {
  182. /* We have opened a tmpfile */
  183. } else {
  184. throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
  185. }
  186. $currentPos = ftell($this->_reader);
  187. fclose($this->_reader);
  188. $source = fopen($this->_path, 'rb');
  189. if (!$source) {
  190. throw new Swift_IoException('Unable to open file for copying ['.$this->_path.']');
  191. }
  192. fseek($tmpFile, 0, SEEK_SET);
  193. while (!feof($source)) {
  194. fwrite($tmpFile, fread($source, 4096));
  195. }
  196. fseek($tmpFile, $currentPos, SEEK_SET);
  197. fclose($source);
  198. $this->_reader = $tmpFile;
  199. }
  200. }