1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the ReCaptcha Library. |
4
|
|
|
* |
5
|
|
|
* (c) Ilya Pokamestov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DS\Library\ReCaptcha\Http; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Describes a PSR7 data stream. |
17
|
|
|
* |
18
|
|
|
* Typically, an instance will wrap a PHP stream; this interface provides |
19
|
|
|
* a wrapper around the most common operations, including serialization of |
20
|
|
|
* the entire stream to a string. |
21
|
|
|
* |
22
|
|
|
* Class Stream |
23
|
|
|
* @package DS\Library\ReCaptcha\Http |
24
|
|
|
*/ |
25
|
|
|
class Stream implements StreamInterface |
26
|
|
|
{ |
27
|
|
|
/** @var resource */ |
28
|
|
|
protected $stream; |
29
|
|
|
/** @var array|mixed|null */ |
30
|
|
|
protected $seekable; |
31
|
|
|
/** @var bool */ |
32
|
|
|
protected $readable; |
33
|
|
|
/** @var bool */ |
34
|
|
|
protected $writable; |
35
|
|
|
/** @var int */ |
36
|
|
|
protected $size; |
37
|
|
|
/** @var array */ |
38
|
|
|
protected $meta; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create new instance of Stream from string, array or callable. |
42
|
|
|
* |
43
|
|
|
* Stream constructor. |
44
|
|
|
* @param $resource array|string|callable |
45
|
|
|
*/ |
46
|
21 |
|
public function __construct($resource) |
47
|
|
|
{ |
48
|
21 |
|
if (is_array($resource)) { |
49
|
3 |
|
$resource = http_build_query($resource); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
21 |
|
if (is_callable($resource)) { |
53
|
1 |
|
$resource = call_user_func($resource); |
54
|
1 |
|
if (!is_string($resource)) { |
55
|
1 |
|
throw new \RuntimeException('Callable must return string value.'); |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
21 |
|
if (!is_string($resource)) { |
60
|
1 |
|
throw new \InvalidArgumentException('$resource type is invalid, support array and string only'); |
61
|
|
|
} |
62
|
|
|
|
63
|
20 |
|
$stream = fopen('php://temp', 'r+'); |
64
|
20 |
|
if ($resource !== '') { |
65
|
20 |
|
fwrite($stream, $resource); |
66
|
20 |
|
fseek($stream, 0); |
67
|
20 |
|
} |
68
|
|
|
|
69
|
20 |
|
$this->stream = $stream; |
70
|
20 |
|
$this->seekable = $this->getMetadata('seekable'); |
71
|
|
|
//Readable and writable is true for r+ mode |
72
|
20 |
|
$this->readable = true; |
73
|
20 |
|
$this->writable = true; |
74
|
20 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set stream resource. |
78
|
|
|
* |
79
|
|
|
* @param $stream resource |
80
|
|
|
*/ |
81
|
5 |
|
public function setStream($stream) |
82
|
|
|
{ |
83
|
5 |
|
$this->stream = $stream; |
84
|
5 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Closes the stream when the destructed |
88
|
|
|
*/ |
89
|
20 |
|
public function __destruct() |
90
|
|
|
{ |
91
|
20 |
|
$this->close(); |
92
|
20 |
|
} |
93
|
|
|
|
94
|
|
|
/** @inheritdoc */ |
95
|
6 |
|
public function __toString() |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
6 |
|
$this->seek(0); |
99
|
5 |
|
return (string)stream_get_contents($this->stream); |
100
|
1 |
|
} catch (\Exception $e) { |
101
|
1 |
|
return ''; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @inheritdoc */ |
106
|
9 |
|
public function getContents() |
107
|
|
|
{ |
108
|
9 |
|
if (!is_resource($this->stream)) { |
109
|
1 |
|
throw new \RuntimeException('Unable to read stream contents'); |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
$contents = stream_get_contents($this->stream); |
113
|
|
|
|
114
|
8 |
|
return $contents; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** @inheritdoc */ |
118
|
20 |
|
public function close() |
119
|
|
|
{ |
120
|
20 |
|
if (isset($this->stream)) { |
121
|
18 |
|
if (is_resource($this->stream)) { |
122
|
16 |
|
fclose($this->stream); |
123
|
16 |
|
} |
124
|
18 |
|
$this->detach(); |
125
|
18 |
|
} |
126
|
20 |
|
} |
127
|
|
|
|
128
|
|
|
/** @inheritdoc */ |
129
|
20 |
|
public function detach() |
130
|
|
|
{ |
131
|
20 |
|
if (!isset($this->stream)) { |
132
|
1 |
|
return null; |
133
|
|
|
} |
134
|
|
|
|
135
|
20 |
|
$result = $this->stream; |
136
|
20 |
|
unset($this->stream); |
137
|
20 |
|
$this->readable = $this->writable = $this->seekable = false; |
138
|
|
|
|
139
|
20 |
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** @inheritdoc */ |
143
|
2 |
|
public function getSize() |
144
|
|
|
{ |
145
|
2 |
|
if (!isset($this->stream)) { |
146
|
1 |
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
if ($this->size !== null) { |
150
|
1 |
|
return $this->size; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
$stats = fstat($this->stream); |
154
|
1 |
|
if (isset($stats['size'])) { |
155
|
1 |
|
$this->size = $stats['size']; |
156
|
1 |
|
return $this->size; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
return null; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** @inheritdoc */ |
163
|
2 |
|
public function isReadable() |
164
|
|
|
{ |
165
|
2 |
|
return $this->readable; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** @inheritdoc */ |
169
|
2 |
|
public function isWritable() |
170
|
|
|
{ |
171
|
2 |
|
return $this->writable; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** @inheritdoc */ |
175
|
2 |
|
public function isSeekable() |
176
|
|
|
{ |
177
|
2 |
|
return $this->seekable; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** @inheritdoc */ |
181
|
1 |
|
public function eof() |
182
|
|
|
{ |
183
|
1 |
|
return !$this->stream || feof($this->stream); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** @inheritdoc */ |
187
|
2 |
|
public function tell() |
188
|
|
|
{ |
189
|
2 |
|
if (!is_resource($this->stream)) { |
190
|
1 |
|
throw new \RuntimeException('Unable to read stream contents'); |
191
|
|
|
} |
192
|
|
|
|
193
|
2 |
|
$result = ftell($this->stream); |
194
|
|
|
|
195
|
2 |
|
return $result; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** @inheritdoc */ |
199
|
1 |
|
public function rewind() |
200
|
|
|
{ |
201
|
1 |
|
$this->seek(0); |
202
|
1 |
|
} |
203
|
|
|
|
204
|
|
|
/** @inheritdoc */ |
205
|
8 |
|
public function seek($offset, $whence = SEEK_SET) |
206
|
|
|
{ |
207
|
8 |
|
if (!$this->seekable) { |
208
|
1 |
|
throw new \RuntimeException('Stream is not seekable'); |
209
|
7 |
|
} elseif (fseek($this->stream, $offset, $whence) === -1) { |
210
|
1 |
|
throw new \RuntimeException( |
211
|
1 |
|
sprintf('Unable to seek to stream position %s with whence %s', $offset, var_export($whence, true)) |
212
|
1 |
|
); |
213
|
|
|
} |
214
|
6 |
|
} |
215
|
|
|
|
216
|
|
|
/** @inheritdoc */ |
217
|
1 |
|
public function read($length) |
218
|
|
|
{ |
219
|
1 |
|
if (!$this->readable) { |
220
|
1 |
|
throw new \RuntimeException('Cannot read from non-readable stream'); |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
return fread($this->stream, $length); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** @inheritdoc */ |
227
|
2 |
|
public function write($string) |
228
|
|
|
{ |
229
|
2 |
|
if (!$this->writable) { |
230
|
1 |
|
throw new \RuntimeException('Cannot write to a non-writable stream'); |
231
|
|
|
} |
232
|
|
|
|
233
|
2 |
|
$this->size = null; |
234
|
2 |
|
$result = fwrite($this->stream, $string); |
235
|
|
|
|
236
|
2 |
|
return $result; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** @inheritdoc */ |
240
|
20 |
|
public function getMetadata($key = null) |
241
|
|
|
{ |
242
|
20 |
|
if (!isset($this->stream)) { |
243
|
1 |
|
return $this->meta = $key ? null : array(); |
244
|
20 |
|
} elseif (isset($this->meta[$key])) { |
245
|
1 |
|
return $this->meta[$key]; |
246
|
|
|
} |
247
|
20 |
|
$this->meta = stream_get_meta_data($this->stream); |
248
|
|
|
|
249
|
20 |
|
return isset($this->meta[$key]) ? $this->meta[$key] : null; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|