1 | <?php |
||
16 | class Swift_ByteStream_FileByteStream extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_FileStream |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * The internal pointer offset |
||
20 | * |
||
21 | * @var int |
||
22 | */ |
||
23 | private $_offset = 0; |
||
24 | |||
25 | /** |
||
26 | * The path to the file |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $_path; |
||
31 | |||
32 | /** |
||
33 | * The mode this file is opened in for writing |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $_mode; |
||
38 | |||
39 | /** |
||
40 | * A lazy-loaded resource handle for reading the file |
||
41 | * |
||
42 | * @var resource |
||
43 | */ |
||
44 | private $_reader; |
||
45 | |||
46 | /** |
||
47 | * A lazy-loaded resource handle for writing the file |
||
48 | * |
||
49 | * @var resource |
||
50 | */ |
||
51 | private $_writer; |
||
52 | |||
53 | /** |
||
54 | * If magic_quotes_runtime is on, this will be true |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $_quotes = false; |
||
59 | |||
60 | /** |
||
61 | * If stream is seekable true/false, or null if not known |
||
62 | * |
||
63 | * @var null|boolean |
||
64 | */ |
||
65 | private $_seekable = null; |
||
66 | |||
67 | /** |
||
68 | * Create a new FileByteStream for $path. |
||
69 | * |
||
70 | * @param string $path |
||
71 | * @param bool|false $writable |
||
72 | * |
||
73 | * @throws Swift_IoException |
||
74 | */ |
||
75 | 30 | public function __construct($path, $writable = false) |
|
76 | { |
||
77 | 30 | if (empty($path)) { |
|
78 | 1 | throw new Swift_IoException('The path cannot be empty'); |
|
79 | } |
||
80 | |||
81 | 29 | if (strpos($path, '?') !== false) { |
|
82 | $parameter = parse_url($path, PHP_URL_QUERY); |
||
83 | |||
84 | $this->_path = str_replace('?' . $parameter, '', $path); |
||
85 | } else { |
||
86 | 29 | $this->_path = $path; |
|
87 | } |
||
88 | |||
89 | 29 | $this->_mode = $writable ? 'w+b' : 'rb'; |
|
90 | |||
91 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
92 | if ( |
||
93 | 29 | function_exists('get_magic_quotes_runtime') |
|
94 | 29 | && |
|
95 | 29 | @get_magic_quotes_runtime() === 1 |
|
96 | 29 | ) { |
|
97 | $this->_quotes = true; |
||
98 | } |
||
99 | 29 | } |
|
100 | |||
101 | /** |
||
102 | * Get the complete path to the file. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 18 | public function getPath() |
|
107 | { |
||
108 | 18 | return $this->_path; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Reads $length bytes from the stream into a string and moves the pointer |
||
113 | * through the stream by $length. |
||
114 | * |
||
115 | * If less bytes exist than are requested the |
||
116 | * remaining bytes are given instead. If no bytes are remaining at all, boolean |
||
117 | * false is returned. |
||
118 | * |
||
119 | * @param int $length |
||
120 | * |
||
121 | * @throws Swift_IoException |
||
122 | * |
||
123 | * @return string|bool |
||
124 | */ |
||
125 | 22 | public function read($length) |
|
126 | { |
||
127 | 22 | $fp = $this->_getReadHandle(); |
|
128 | 21 | if ($fp && !feof($fp)) { |
|
129 | |||
130 | 21 | if ($this->_quotes) { |
|
131 | /** @noinspection DeprecatedIniOptionsInspection */ |
||
132 | ini_set('magic_quotes_runtime', 0); |
||
133 | } |
||
134 | |||
135 | 21 | $bytes = fread($fp, $length); |
|
136 | |||
137 | 21 | if ($this->_quotes) { |
|
138 | /** @noinspection DeprecatedIniOptionsInspection */ |
||
139 | ini_set('magic_quotes_runtime', 1); |
||
140 | } |
||
141 | |||
142 | 21 | $this->_offset = ftell($fp); |
|
143 | |||
144 | // If we read one byte after reaching the end of the file |
||
145 | // feof() will return false and an empty string is returned. |
||
146 | 21 | if ($bytes === '' && feof($fp)) { |
|
147 | 1 | $this->_resetReadHandle(); |
|
148 | |||
149 | 1 | return false; |
|
150 | } |
||
151 | |||
152 | 21 | return $bytes; |
|
153 | } |
||
154 | |||
155 | 18 | $this->_resetReadHandle(); |
|
156 | |||
157 | 18 | return false; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Move the internal read pointer to $byteOffset in the stream. |
||
162 | * |
||
163 | * @param int $byteOffset |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 16 | public function setReadPointer($byteOffset) |
|
168 | { |
||
169 | 16 | if (isset($this->_reader)) { |
|
170 | 8 | $this->_seekReadStreamToPosition($byteOffset); |
|
171 | 8 | } |
|
172 | 16 | $this->_offset = $byteOffset; |
|
173 | 16 | } |
|
174 | |||
175 | /** |
||
176 | * Just write the bytes to the file |
||
177 | * |
||
178 | * @param string $bytes |
||
179 | * |
||
180 | * @throws Swift_IoException |
||
181 | */ |
||
182 | 16 | protected function _commit($bytes) |
|
183 | { |
||
184 | 16 | fwrite($this->_getWriteHandle(), $bytes); |
|
185 | 16 | $this->_resetReadHandle(); |
|
186 | 16 | } |
|
187 | |||
188 | /** Not used */ |
||
189 | 11 | protected function _flush() |
|
190 | { |
||
191 | 11 | } |
|
192 | |||
193 | /** |
||
194 | * Get the resource for reading |
||
195 | * |
||
196 | * @return resource |
||
197 | * @throws Swift_IoException |
||
198 | */ |
||
199 | 22 | private function _getReadHandle() |
|
228 | |||
229 | /** Get the resource for writing */ |
||
230 | 16 | private function _getWriteHandle() |
|
231 | { |
||
232 | 16 | if (!isset($this->_writer)) { |
|
233 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
234 | 16 | $pointer = @fopen($this->_path, $this->_mode); |
|
245 | |||
246 | /** Force a reload of the resource for reading */ |
||
247 | 25 | private function _resetReadHandle() |
|
254 | |||
255 | /** Check if ReadOnly Stream is seekable */ |
||
256 | 9 | private function _getReadStreamSeekableStatus() |
|
261 | |||
262 | /** |
||
263 | * Streams in a readOnly stream ensuring copy if needed |
||
264 | * |
||
265 | * @param $offset |
||
266 | * |
||
267 | * @throws Swift_IoException |
||
268 | */ |
||
269 | 9 | private function _seekReadStreamToPosition($offset) |
|
286 | |||
287 | /** Copy a readOnly Stream to ensure seekability */ |
||
288 | private function _copyReadStream() |
||
320 | } |
||
321 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.