|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\StreamDecorators project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
|
|
|
|
|
6
|
|
|
*/ |
|
|
|
|
|
|
7
|
|
|
namespace ZBateson\StreamDecorators; |
|
8
|
|
|
|
|
9
|
|
|
use Psr\Http\Message\StreamInterface; |
|
10
|
|
|
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
|
11
|
|
|
use GuzzleHttp\Psr7\BufferStream; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* GuzzleHttp\Psr7 stream decoder extension for base64 streams. |
|
16
|
|
|
* |
|
17
|
|
|
* Note that it's expected the underlying stream will only contain valid base64 |
|
18
|
|
|
* characters (normally the stream should be wrapped in a |
|
19
|
|
|
* PregReplaceFilterStream to filter out non-base64 characters for reading). |
|
20
|
|
|
* |
|
21
|
|
|
* ``` |
|
22
|
|
|
* $f = fopen(...); |
|
23
|
|
|
* $stream = new Base64Stream(new PregReplaceFilterStream( |
|
24
|
|
|
* Psr7\stream_for($f), '/[^a-zA-Z0-9\/\+=]/', '' |
|
25
|
|
|
* )); |
|
26
|
|
|
* //... |
|
27
|
|
|
* ``` |
|
28
|
|
|
* |
|
29
|
|
|
* For writing, a ChunkSplitStream could come in handy so the output is split |
|
30
|
|
|
* into lines: |
|
31
|
|
|
* |
|
32
|
|
|
* ``` |
|
33
|
|
|
* $f = fopen(...); |
|
34
|
|
|
* $stream = new Base64Stream(new ChunkSplitStream(new PregReplaceFilterStream( |
|
35
|
|
|
* Psr7\stream_for($f), '/[^a-zA-Z0-9\/\+=]/', '' |
|
36
|
|
|
* ))); |
|
37
|
|
|
* //... |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* @author Zaahid Bateson |
|
|
|
|
|
|
41
|
|
|
*/ |
|
|
|
|
|
|
42
|
|
|
class Base64Stream implements StreamInterface |
|
43
|
|
|
{ |
|
|
|
|
|
|
44
|
|
|
use StreamDecoratorTrait; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @var BufferStream buffered bytes |
|
48
|
|
|
*/ |
|
49
|
|
|
private $buffer; |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @var string remainder of write operation if the bytes didn't align to 3 |
|
53
|
|
|
* bytes |
|
54
|
|
|
*/ |
|
55
|
|
|
private $remainder = ''; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
|
|
|
|
|
58
|
|
|
* @var int current number of read/written bytes (for tell()) |
|
59
|
|
|
*/ |
|
60
|
|
|
private $position = 0; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
|
|
|
|
|
63
|
|
|
* @param StreamInterface $stream |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
9 |
|
public function __construct(StreamInterface $stream) |
|
|
|
|
|
|
66
|
|
|
{ |
|
|
|
|
|
|
67
|
9 |
|
$this->stream = $stream; |
|
68
|
9 |
|
$this->buffer = new BufferStream(); |
|
69
|
9 |
|
} |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the current position of the file read/write pointer |
|
73
|
|
|
* |
|
74
|
|
|
* @return int |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function tell() |
|
77
|
|
|
{ |
|
|
|
|
|
|
78
|
1 |
|
return $this->position; |
|
79
|
|
|
} |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns null, getSize isn't supported |
|
83
|
|
|
* |
|
84
|
|
|
* @return null |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function getSize() |
|
87
|
|
|
{ |
|
|
|
|
|
|
88
|
3 |
|
return null; |
|
|
|
|
|
|
89
|
|
|
} |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Not implemented (yet). |
|
93
|
|
|
* |
|
94
|
|
|
* Seek position can be calculated. |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $offset |
|
|
|
|
|
|
97
|
|
|
* @param int $whence |
|
|
|
|
|
|
98
|
|
|
* @throws RuntimeException |
|
|
|
|
|
|
99
|
|
|
*/ |
|
|
|
|
|
|
100
|
1 |
|
public function seek($offset, $whence = SEEK_SET) |
|
|
|
|
|
|
101
|
|
|
{ |
|
|
|
|
|
|
102
|
1 |
|
throw new RuntimeException('Cannot seek a Base64Stream'); |
|
103
|
|
|
} |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Overridden to return false |
|
107
|
|
|
* |
|
108
|
|
|
* @return boolean |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function isSeekable() |
|
111
|
|
|
{ |
|
|
|
|
|
|
112
|
1 |
|
return false; |
|
|
|
|
|
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns true if the end of stream has been reached. |
|
117
|
|
|
* |
|
118
|
|
|
* @return boolean |
|
119
|
|
|
*/ |
|
120
|
7 |
|
public function eof() |
|
121
|
|
|
{ |
|
|
|
|
|
|
122
|
7 |
|
return ($this->buffer->eof() && $this->stream->eof()); |
|
|
|
|
|
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Fills the internal byte buffer after reading and decoding data from the |
|
127
|
|
|
* underlying stream. |
|
128
|
|
|
* |
|
129
|
|
|
* Note that it's expected the underlying stream will only contain valid |
|
130
|
|
|
* base64 characters (normally the stream should be wrapped in a |
|
131
|
|
|
* PregReplaceFilterStream to filter out non-base64 characters). |
|
132
|
|
|
* |
|
133
|
|
|
* @param int $length |
|
|
|
|
|
|
134
|
|
|
*/ |
|
|
|
|
|
|
135
|
7 |
|
private function fillBuffer($length) |
|
|
|
|
|
|
136
|
|
|
{ |
|
|
|
|
|
|
137
|
7 |
|
$fill = 8192; |
|
138
|
7 |
|
while ($this->buffer->getSize() < $length) { |
|
139
|
7 |
|
$read = $this->stream->read($fill); |
|
140
|
7 |
|
if ($read === false || $read === '') { |
|
|
|
|
|
|
141
|
6 |
|
break; |
|
142
|
|
|
} |
|
143
|
7 |
|
$this->buffer->write(base64_decode($read)); |
|
144
|
|
|
} |
|
145
|
7 |
|
} |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Attempts to read $length bytes after decoding them, and returns them. |
|
149
|
|
|
* |
|
150
|
|
|
* Note that reading and writing to the same stream may result in wrongly |
|
151
|
|
|
* encoded data and is not supported. |
|
152
|
|
|
* |
|
153
|
|
|
* @param int $length |
|
|
|
|
|
|
154
|
|
|
* @return string |
|
|
|
|
|
|
155
|
|
|
*/ |
|
156
|
7 |
|
public function read($length) |
|
|
|
|
|
|
157
|
|
|
{ |
|
|
|
|
|
|
158
|
|
|
// let Guzzle decide what to do. |
|
|
|
|
|
|
159
|
7 |
|
if ($length <= 0 || $this->eof()) { |
|
160
|
2 |
|
return $this->stream->read($length); |
|
161
|
|
|
} |
|
162
|
7 |
|
$this->fillBuffer($length); |
|
163
|
7 |
|
$ret = $this->buffer->read($length); |
|
|
|
|
|
|
164
|
7 |
|
$this->position += strlen($ret); |
|
165
|
7 |
|
return $ret; |
|
166
|
|
|
} |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Writes the passed string to the underlying stream after encoding it to |
|
170
|
|
|
* base64. |
|
171
|
|
|
* |
|
172
|
|
|
* Base64Stream::close or detach must be called. Failing to do so may |
|
173
|
|
|
* result in 1-2 bytes missing from the end of the stream if there's a |
|
174
|
|
|
* remainder. Note that the default Stream destructor calls close as well. |
|
175
|
|
|
* |
|
176
|
|
|
* Note that reading and writing to the same stream may result in wrongly |
|
177
|
|
|
* encoded data and is not supported. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $string |
|
|
|
|
|
|
180
|
|
|
* @return int the number of bytes written |
|
|
|
|
|
|
181
|
|
|
*/ |
|
182
|
2 |
|
public function write($string) |
|
|
|
|
|
|
183
|
|
|
{ |
|
|
|
|
|
|
184
|
2 |
|
$bytes = $this->remainder . $string; |
|
|
|
|
|
|
185
|
2 |
|
$len = strlen($bytes); |
|
|
|
|
|
|
186
|
2 |
|
if (($len % 3) !== 0) { |
|
187
|
2 |
|
$this->remainder = substr($bytes, -($len % 3)); |
|
188
|
2 |
|
$bytes = substr($bytes, 0, $len - ($len % 3)); |
|
|
|
|
|
|
189
|
|
|
} else { |
|
190
|
2 |
|
$this->remainder = ''; |
|
191
|
|
|
} |
|
192
|
2 |
|
$this->stream->write(base64_encode($bytes)); |
|
193
|
2 |
|
$written = strlen($string); |
|
|
|
|
|
|
194
|
2 |
|
$this->position += $len; |
|
195
|
2 |
|
return $written; |
|
196
|
|
|
} |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Writes out any remaining bytes at the end of the stream and closes. |
|
200
|
|
|
*/ |
|
|
|
|
|
|
201
|
2 |
|
private function beforeClose() |
|
|
|
|
|
|
202
|
|
|
{ |
|
|
|
|
|
|
203
|
2 |
|
if ($this->isWritable() && $this->remainder !== '') { |
|
204
|
2 |
|
$this->stream->write(base64_encode($this->remainder)); |
|
205
|
2 |
|
$this->remainder = ''; |
|
206
|
|
|
} |
|
207
|
2 |
|
} |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Closes the underlying stream after writing out any remaining bytes |
|
211
|
|
|
* needing to be encoded. |
|
212
|
|
|
*/ |
|
|
|
|
|
|
213
|
1 |
|
public function close() |
|
214
|
|
|
{ |
|
|
|
|
|
|
215
|
1 |
|
$this->beforeClose(); |
|
216
|
1 |
|
$this->stream->close(); |
|
217
|
1 |
|
} |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Detaches the underlying stream after writing out any remaining bytes |
|
221
|
|
|
* needing to be encoded. |
|
222
|
|
|
*/ |
|
|
|
|
|
|
223
|
1 |
|
public function detach() |
|
224
|
|
|
{ |
|
|
|
|
|
|
225
|
1 |
|
$this->beforeClose(); |
|
226
|
1 |
|
$this->stream->detach(); |
|
227
|
1 |
|
} |
|
|
|
|
|
|
228
|
|
|
} |
|
|
|
|
|
|
229
|
|
|
|