1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
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 segment of request data stream, this way we avoid data duplication |
14
|
|
|
* |
15
|
|
|
* Usage: `fopen('request-data://offset:size', 'r')` |
16
|
|
|
* |
17
|
|
|
* `offset` and `size` are used to specify segment of data within request data stream, only `r` mode is supported |
18
|
|
|
*/ |
19
|
|
|
class Data_sub_stream { |
20
|
|
|
/** |
21
|
|
|
* Offset of current file inside parent input stream |
22
|
|
|
* |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $offset; |
26
|
|
|
/** |
27
|
|
|
* Size of current file in bytes |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $size; |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $stat = []; |
36
|
|
|
/** |
37
|
|
|
* Current position of a stream |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $position = 0; |
42
|
|
|
/** |
43
|
|
|
* @var resource |
44
|
|
|
*/ |
45
|
|
|
protected $data_stream; |
46
|
|
|
|
47
|
|
|
function stream_open ($path, $mode) { |
48
|
|
|
if ($mode != 'r' && $mode != 'rb') { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
$this->data_stream = Request::instance()->data_stream; |
52
|
|
|
list($this->offset, $this->size) = explode(':', explode('://', $path)[1]); |
53
|
|
|
$stat = &$this->stat; |
54
|
|
|
$stat[0] = $stat['dev'] = 0; |
55
|
|
|
$stat[1] = $stat['ino'] = 0; |
56
|
|
|
$stat[2] = $stat['mode'] = 0; |
57
|
|
|
$stat[3] = $stat['nlink'] = 0; |
58
|
|
|
$stat[4] = $stat['uid'] = 0; |
59
|
|
|
$stat[5] = $stat['gid'] = 0; |
60
|
|
|
$stat[6] = $stat['rdev'] = 0; |
61
|
|
|
$stat[7] = $stat['size'] = $this->size; |
62
|
|
|
$time = time(); |
63
|
|
|
$stat[8] = $stat['atime'] = $time; |
64
|
|
|
$stat[9] = $stat['mtime'] = $time; |
65
|
|
|
$stat[10] = $stat['ctime'] = $time; |
66
|
|
|
$stat[11] = $stat['blksize'] = -1; |
67
|
|
|
$stat[12] = $stat['blocks'] = -1; |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
/** |
71
|
|
|
* @param int $length |
72
|
|
|
* |
73
|
|
|
* @return false|string |
74
|
|
|
*/ |
75
|
|
|
function stream_read ($length) { |
76
|
|
|
if ($this->stream_eof()) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
return $this->stream( |
80
|
|
|
function ($stream) use ($length) { |
81
|
|
|
fseek($stream, $this->offset + $this->position); |
82
|
|
|
/** |
83
|
|
|
* Avoid going out of file boundary |
84
|
|
|
*/ |
85
|
|
|
$to_read = min($length, $this->size - $this->position); |
86
|
|
|
$this->position += $to_read; |
87
|
|
|
return fread($stream, $to_read); |
88
|
|
|
} |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* @param callable $callback |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
protected function stream ($callback) { |
97
|
|
|
$position = ftell($this->data_stream); |
98
|
|
|
$result = $callback($this->data_stream); |
99
|
|
|
fseek($this->data_stream, $position); |
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
function stream_tell () { |
106
|
|
|
return $this->position; |
107
|
|
|
} |
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
function stream_eof () { |
112
|
|
|
return $this->position == $this->size; |
113
|
|
|
} |
114
|
|
|
/** |
115
|
|
|
* @param int $offset |
116
|
|
|
* @param int $whence |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
function stream_seek ($offset, $whence = SEEK_SET) { |
121
|
|
|
if ($whence == SEEK_SET && $offset >= 0 && $offset < $this->size) { |
122
|
|
|
$this->position = $offset; |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
$position = $this->position + $offset; |
126
|
|
|
if ($whence == SEEK_CUR && $offset >= 0 && $position < $this->size) { |
127
|
|
|
$this->position = $position; |
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
$position = $this->size + $offset; |
131
|
|
|
if ($whence == SEEK_END && $offset <= 0 && $position >= 0) { |
132
|
|
|
$this->position = $position; |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
function stream_stat () { |
141
|
|
|
return $this->stat; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|