1 |
cd5c298a
|
Geoffroy Desvernay
|
<?php
|
2 |
|
|
namespace TYPO3\PharStreamWrapper;
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
fbb66ca6
|
Assos Assos
|
use TYPO3\PharStreamWrapper\Resolver\PharInvocation;
|
15 |
|
|
|
16 |
cd5c298a
|
Geoffroy Desvernay
|
class PharStreamWrapper
|
17 |
|
|
{
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
const STREAM_OPEN_FOR_INCLUDE = 128;
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
public $context;
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
protected $internalResource;
|
33 |
|
|
|
34 |
fbb66ca6
|
Assos Assos
|
|
35 |
|
|
|
36 |
|
|
|
37 |
|
|
protected $invocation;
|
38 |
|
|
|
39 |
cd5c298a
|
Geoffroy Desvernay
|
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
public function dir_closedir()
|
43 |
|
|
{
|
44 |
|
|
if (!is_resource($this->internalResource)) {
|
45 |
|
|
return false;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
$this->invokeInternalStreamWrapper(
|
49 |
|
|
'closedir',
|
50 |
|
|
$this->internalResource
|
51 |
|
|
);
|
52 |
|
|
return !is_resource($this->internalResource);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
public function dir_opendir($path, $options)
|
61 |
|
|
{
|
62 |
|
|
$this->assert($path, Behavior::COMMAND_DIR_OPENDIR);
|
63 |
|
|
$this->internalResource = $this->invokeInternalStreamWrapper(
|
64 |
|
|
'opendir',
|
65 |
|
|
$path,
|
66 |
|
|
$this->context
|
67 |
|
|
);
|
68 |
|
|
return is_resource($this->internalResource);
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
public function dir_readdir()
|
75 |
|
|
{
|
76 |
|
|
return $this->invokeInternalStreamWrapper(
|
77 |
|
|
'readdir',
|
78 |
|
|
$this->internalResource
|
79 |
|
|
);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
public function dir_rewinddir()
|
86 |
|
|
{
|
87 |
|
|
if (!is_resource($this->internalResource)) {
|
88 |
|
|
return false;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
$this->invokeInternalStreamWrapper(
|
92 |
|
|
'rewinddir',
|
93 |
|
|
$this->internalResource
|
94 |
|
|
);
|
95 |
|
|
return is_resource($this->internalResource);
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
public function mkdir($path, $mode, $options)
|
105 |
|
|
{
|
106 |
|
|
$this->assert($path, Behavior::COMMAND_MKDIR);
|
107 |
|
|
return $this->invokeInternalStreamWrapper(
|
108 |
|
|
'mkdir',
|
109 |
|
|
$path,
|
110 |
|
|
$mode,
|
111 |
|
|
(bool) ($options & STREAM_MKDIR_RECURSIVE),
|
112 |
|
|
$this->context
|
113 |
|
|
);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
public function rename($path_from, $path_to)
|
122 |
|
|
{
|
123 |
|
|
$this->assert($path_from, Behavior::COMMAND_RENAME);
|
124 |
|
|
$this->assert($path_to, Behavior::COMMAND_RENAME);
|
125 |
|
|
return $this->invokeInternalStreamWrapper(
|
126 |
|
|
'rename',
|
127 |
|
|
$path_from,
|
128 |
|
|
$path_to,
|
129 |
|
|
$this->context
|
130 |
|
|
);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
public function rmdir($path, $options)
|
139 |
|
|
{
|
140 |
|
|
$this->assert($path, Behavior::COMMAND_RMDIR);
|
141 |
|
|
return $this->invokeInternalStreamWrapper(
|
142 |
|
|
'rmdir',
|
143 |
|
|
$path,
|
144 |
|
|
$this->context
|
145 |
|
|
);
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
public function stream_cast($cast_as)
|
152 |
|
|
{
|
153 |
|
|
throw new Exception(
|
154 |
|
|
'Method stream_select() cannot be used',
|
155 |
|
|
1530103999
|
156 |
|
|
);
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
public function stream_close()
|
160 |
|
|
{
|
161 |
|
|
$this->invokeInternalStreamWrapper(
|
162 |
|
|
'fclose',
|
163 |
|
|
$this->internalResource
|
164 |
|
|
);
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
public function stream_eof()
|
171 |
|
|
{
|
172 |
|
|
return $this->invokeInternalStreamWrapper(
|
173 |
|
|
'feof',
|
174 |
|
|
$this->internalResource
|
175 |
|
|
);
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
public function stream_flush()
|
182 |
|
|
{
|
183 |
|
|
return $this->invokeInternalStreamWrapper(
|
184 |
|
|
'fflush',
|
185 |
|
|
$this->internalResource
|
186 |
|
|
);
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
public function stream_lock($operation)
|
194 |
|
|
{
|
195 |
|
|
return $this->invokeInternalStreamWrapper(
|
196 |
|
|
'flock',
|
197 |
|
|
$this->internalResource,
|
198 |
|
|
$operation
|
199 |
|
|
);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
public function stream_metadata($path, $option, $value)
|
209 |
|
|
{
|
210 |
|
|
$this->assert($path, Behavior::COMMAND_STEAM_METADATA);
|
211 |
|
|
if ($option === STREAM_META_TOUCH) {
|
212 |
|
|
return call_user_func_array(
|
213 |
|
|
array($this, 'invokeInternalStreamWrapper'),
|
214 |
|
|
array_merge(array('touch', $path), (array) $value)
|
215 |
|
|
);
|
216 |
|
|
}
|
217 |
|
|
if ($option === STREAM_META_OWNER_NAME || $option === STREAM_META_OWNER) {
|
218 |
|
|
return $this->invokeInternalStreamWrapper(
|
219 |
|
|
'chown',
|
220 |
|
|
$path,
|
221 |
|
|
$value
|
222 |
|
|
);
|
223 |
|
|
}
|
224 |
|
|
if ($option === STREAM_META_GROUP_NAME || $option === STREAM_META_GROUP) {
|
225 |
|
|
return $this->invokeInternalStreamWrapper(
|
226 |
|
|
'chgrp',
|
227 |
|
|
$path,
|
228 |
|
|
$value
|
229 |
|
|
);
|
230 |
|
|
}
|
231 |
|
|
if ($option === STREAM_META_ACCESS) {
|
232 |
|
|
return $this->invokeInternalStreamWrapper(
|
233 |
|
|
'chmod',
|
234 |
|
|
$path,
|
235 |
|
|
$value
|
236 |
|
|
);
|
237 |
|
|
}
|
238 |
|
|
return false;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
public function stream_open(
|
249 |
|
|
$path,
|
250 |
|
|
$mode,
|
251 |
|
|
$options,
|
252 |
|
|
&$opened_path = null
|
253 |
|
|
) {
|
254 |
|
|
$this->assert($path, Behavior::COMMAND_STREAM_OPEN);
|
255 |
|
|
$arguments = array($path, $mode, (bool) ($options & STREAM_USE_PATH));
|
256 |
|
|
|
257 |
|
|
if (!($options & static::STREAM_OPEN_FOR_INCLUDE)) {
|
258 |
|
|
$arguments[] = $this->context;
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
} else {
|
262 |
|
|
Helper::resetOpCache();
|
263 |
|
|
}
|
264 |
|
|
$this->internalResource = call_user_func_array(
|
265 |
|
|
array($this, 'invokeInternalStreamWrapper'),
|
266 |
|
|
array_merge(array('fopen'), $arguments)
|
267 |
|
|
);
|
268 |
|
|
if (!is_resource($this->internalResource)) {
|
269 |
|
|
return false;
|
270 |
|
|
}
|
271 |
|
|
if ($opened_path !== null) {
|
272 |
|
|
$metaData = stream_get_meta_data($this->internalResource);
|
273 |
|
|
$opened_path = $metaData['uri'];
|
274 |
|
|
}
|
275 |
|
|
return true;
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
public function stream_read($count)
|
283 |
|
|
{
|
284 |
|
|
return $this->invokeInternalStreamWrapper(
|
285 |
|
|
'fread',
|
286 |
|
|
$this->internalResource,
|
287 |
|
|
$count
|
288 |
|
|
);
|
289 |
|
|
}
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
public function stream_seek($offset, $whence = SEEK_SET)
|
297 |
|
|
{
|
298 |
|
|
return $this->invokeInternalStreamWrapper(
|
299 |
|
|
'fseek',
|
300 |
|
|
$this->internalResource,
|
301 |
|
|
$offset,
|
302 |
|
|
$whence
|
303 |
|
|
) !== -1;
|
304 |
|
|
}
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
public function stream_set_option($option, $arg1, $arg2)
|
313 |
|
|
{
|
314 |
|
|
if ($option === STREAM_OPTION_BLOCKING) {
|
315 |
|
|
return $this->invokeInternalStreamWrapper(
|
316 |
|
|
'stream_set_blocking',
|
317 |
|
|
$this->internalResource,
|
318 |
|
|
$arg1
|
319 |
|
|
);
|
320 |
|
|
}
|
321 |
|
|
if ($option === STREAM_OPTION_READ_TIMEOUT) {
|
322 |
|
|
return $this->invokeInternalStreamWrapper(
|
323 |
|
|
'stream_set_timeout',
|
324 |
|
|
$this->internalResource,
|
325 |
|
|
$arg1,
|
326 |
|
|
$arg2
|
327 |
|
|
);
|
328 |
|
|
}
|
329 |
|
|
if ($option === STREAM_OPTION_WRITE_BUFFER) {
|
330 |
|
|
return $this->invokeInternalStreamWrapper(
|
331 |
|
|
'stream_set_write_buffer',
|
332 |
|
|
$this->internalResource,
|
333 |
|
|
$arg2
|
334 |
|
|
) === 0;
|
335 |
|
|
}
|
336 |
|
|
return false;
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
public function stream_stat()
|
343 |
|
|
{
|
344 |
|
|
return $this->invokeInternalStreamWrapper(
|
345 |
|
|
'fstat',
|
346 |
|
|
$this->internalResource
|
347 |
|
|
);
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
public function stream_tell()
|
354 |
|
|
{
|
355 |
|
|
return $this->invokeInternalStreamWrapper(
|
356 |
|
|
'ftell',
|
357 |
|
|
$this->internalResource
|
358 |
|
|
);
|
359 |
|
|
}
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
public function stream_truncate($new_size)
|
366 |
|
|
{
|
367 |
|
|
return $this->invokeInternalStreamWrapper(
|
368 |
|
|
'ftruncate',
|
369 |
|
|
$this->internalResource,
|
370 |
|
|
$new_size
|
371 |
|
|
);
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
|
|
|
378 |
|
|
public function stream_write($data)
|
379 |
|
|
{
|
380 |
|
|
return $this->invokeInternalStreamWrapper(
|
381 |
|
|
'fwrite',
|
382 |
|
|
$this->internalResource,
|
383 |
|
|
$data
|
384 |
|
|
);
|
385 |
|
|
}
|
386 |
|
|
|
387 |
|
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
|
|
public function unlink($path)
|
392 |
|
|
{
|
393 |
|
|
$this->assert($path, Behavior::COMMAND_UNLINK);
|
394 |
|
|
return $this->invokeInternalStreamWrapper(
|
395 |
|
|
'unlink',
|
396 |
|
|
$path,
|
397 |
|
|
$this->context
|
398 |
|
|
);
|
399 |
|
|
}
|
400 |
|
|
|
401 |
|
|
|
402 |
|
|
|
403 |
|
|
|
404 |
|
|
|
405 |
|
|
|
406 |
|
|
public function url_stat($path, $flags)
|
407 |
|
|
{
|
408 |
|
|
$this->assert($path, Behavior::COMMAND_URL_STAT);
|
409 |
|
|
$functionName = $flags & STREAM_URL_STAT_QUIET ? '@stat' : 'stat';
|
410 |
|
|
return $this->invokeInternalStreamWrapper($functionName, $path);
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
|
414 |
|
|
|
415 |
|
|
|
416 |
|
|
|
417 |
|
|
protected function assert($path, $command)
|
418 |
|
|
{
|
419 |
fbb66ca6
|
Assos Assos
|
if (Manager::instance()->assert($path, $command) === true) {
|
420 |
|
|
$this->collectInvocation($path);
|
421 |
cd5c298a
|
Geoffroy Desvernay
|
return;
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
throw new Exception(
|
425 |
|
|
sprintf(
|
426 |
|
|
'Denied invocation of "%s" for command "%s"',
|
427 |
|
|
$path,
|
428 |
|
|
$command
|
429 |
|
|
),
|
430 |
|
|
1535189880
|
431 |
|
|
);
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
|
435 |
fbb66ca6
|
Assos Assos
|
|
436 |
|
|
|
437 |
|
|
protected function collectInvocation($path)
|
438 |
|
|
{
|
439 |
|
|
if (isset($this->invocation)) {
|
440 |
|
|
return;
|
441 |
|
|
}
|
442 |
|
|
|
443 |
|
|
$manager = Manager::instance();
|
444 |
|
|
$this->invocation = $manager->resolve($path);
|
445 |
|
|
if ($this->invocation === null) {
|
446 |
|
|
throw new Exception(
|
447 |
|
|
'Expected invocation could not be resolved',
|
448 |
|
|
1556389591
|
449 |
|
|
);
|
450 |
|
|
}
|
451 |
|
|
|
452 |
|
|
$this->invocation->confirm();
|
453 |
|
|
$collection = $manager->getCollection();
|
454 |
|
|
if (!$collection->has($this->invocation)) {
|
455 |
|
|
$collection->collect($this->invocation);
|
456 |
|
|
}
|
457 |
|
|
}
|
458 |
|
|
|
459 |
|
|
|
460 |
|
|
|
461 |
|
|
|
462 |
cd5c298a
|
Geoffroy Desvernay
|
|
463 |
|
|
protected function resolveAssertable()
|
464 |
|
|
{
|
465 |
|
|
return Manager::instance();
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
|
469 |
|
|
|
470 |
|
|
|
471 |
|
|
|
472 |
|
|
|
473 |
|
|
|
474 |
|
|
|
475 |
|
|
private function invokeInternalStreamWrapper($functionName)
|
476 |
|
|
{
|
477 |
|
|
$arguments = func_get_args();
|
478 |
|
|
array_shift($arguments);
|
479 |
6b24a280
|
Assos Assos
|
$silentExecution = $functionName[0] === '@';
|
480 |
cd5c298a
|
Geoffroy Desvernay
|
$functionName = ltrim($functionName, '@');
|
481 |
|
|
$this->restoreInternalSteamWrapper();
|
482 |
|
|
|
483 |
|
|
try {
|
484 |
|
|
if ($silentExecution) {
|
485 |
|
|
$result = @call_user_func_array($functionName, $arguments);
|
486 |
|
|
} else {
|
487 |
|
|
$result = call_user_func_array($functionName, $arguments);
|
488 |
|
|
}
|
489 |
|
|
} catch (\Exception $exception) {
|
490 |
|
|
$this->registerStreamWrapper();
|
491 |
|
|
throw $exception;
|
492 |
|
|
} catch (\Throwable $throwable) {
|
493 |
|
|
$this->registerStreamWrapper();
|
494 |
|
|
throw $throwable;
|
495 |
|
|
}
|
496 |
|
|
|
497 |
|
|
$this->registerStreamWrapper();
|
498 |
|
|
return $result;
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
private function restoreInternalSteamWrapper()
|
502 |
|
|
{
|
503 |
|
|
stream_wrapper_restore('phar');
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
private function registerStreamWrapper()
|
507 |
|
|
{
|
508 |
|
|
stream_wrapper_unregister('phar');
|
509 |
|
|
stream_wrapper_register('phar', get_class($this));
|
510 |
|
|
}
|
511 |
|
|
} |