1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Request; |
9
|
|
|
use |
10
|
|
|
cs\Request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Stream wrapper created in order to be used as virtual stream which actually represents uploaded file, which is only available in form of stream, but we |
14
|
|
|
* sometimes need to handle it like regular file |
15
|
|
|
* |
16
|
|
|
* Usage: |
17
|
|
|
* * `fopen('request-file:///file', 'r')` |
18
|
|
|
* * `fopen('request-file:///file/0', 'r')` |
19
|
|
|
* |
20
|
|
|
* `/file` basically represents `cs\Request::files['file']` and `/file/0` `cs\Request::files['file'][0]` |
21
|
|
|
*/ |
22
|
|
|
class File_stream { |
23
|
|
|
/** |
24
|
|
|
* @var resource |
25
|
|
|
*/ |
26
|
|
|
protected $stream; |
27
|
|
|
|
28
|
2 |
|
function stream_open ($path, $mode) { |
29
|
2 |
|
if ($mode != 'r' && $mode != 'rb') { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
2 |
|
$files = Request::instance()->files; |
33
|
2 |
|
foreach (explode('/', explode(':///', $path)[1]) as $file_path) { |
34
|
2 |
|
if (!isset($files[$file_path])) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
2 |
|
$files = $files[$file_path]; |
38
|
|
|
} |
39
|
2 |
|
$this->stream = $files['stream']; |
40
|
2 |
|
return true; |
41
|
|
|
} |
42
|
|
|
/** |
43
|
|
|
* @param int $length |
44
|
|
|
* |
45
|
|
|
* @return false|string |
|
|
|
|
46
|
|
|
*/ |
47
|
2 |
|
function stream_read ($length) { |
48
|
2 |
|
return fread($this->stream, $length); |
49
|
|
|
} |
50
|
|
|
/** |
51
|
|
|
* @return false|int |
52
|
|
|
*/ |
53
|
|
|
function stream_tell () { |
54
|
|
|
return ftell($this->stream); |
55
|
|
|
} |
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
2 |
|
function stream_eof () { |
60
|
2 |
|
return feof($this->stream); |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* @param int $offset |
64
|
|
|
* @param int $whence |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
function stream_seek ($offset, $whence = SEEK_SET) { |
69
|
|
|
return fseek($this->stream, $offset, $whence); |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
2 |
|
function stream_stat () { |
75
|
2 |
|
return fstat($this->stream); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.