1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared\OLE; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\OLE; |
6
|
|
|
|
7
|
|
|
class ChainedBlockStream |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The OLE container of the file that is being read. |
11
|
|
|
* |
12
|
|
|
* @var null|OLE |
13
|
|
|
*/ |
14
|
|
|
public $ole; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Parameters specified by fopen(). |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public $params; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The binary data of the file. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The file pointer. |
32
|
|
|
* |
33
|
|
|
* @var int byte offset |
34
|
|
|
*/ |
35
|
|
|
public $pos; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Implements support for fopen(). |
39
|
|
|
* For creating streams using this wrapper, use OLE_PPS_File::getStream(). |
40
|
|
|
* |
41
|
|
|
* @param string $path resource name including scheme, e.g. |
42
|
|
|
* ole-chainedblockstream://oleInstanceId=1 |
43
|
|
|
* @param string $mode only "r" is supported |
44
|
|
|
* @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH |
45
|
|
|
* @param string $openedPath absolute path of the opened stream (out parameter) |
46
|
|
|
* |
47
|
|
|
* @return bool true on success |
48
|
|
|
*/ |
49
|
3 |
|
public function stream_open($path, $mode, $options, &$openedPath) // @codingStandardsIgnoreLine |
50
|
|
|
{ |
51
|
3 |
|
if ($mode[0] !== 'r') { |
52
|
1 |
|
if ($options & STREAM_REPORT_ERRORS) { |
53
|
1 |
|
trigger_error('Only reading is supported', E_USER_WARNING); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// 25 is length of "ole-chainedblockstream://" |
60
|
2 |
|
parse_str(substr($path, 25), $this->params); |
61
|
2 |
|
if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) { |
62
|
1 |
|
if ($options & STREAM_REPORT_ERRORS) { |
63
|
1 |
|
trigger_error('OLE stream not found', E_USER_WARNING); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return false; |
67
|
|
|
} |
68
|
1 |
|
$this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']]; |
69
|
|
|
|
70
|
1 |
|
$blockId = $this->params['blockId']; |
71
|
1 |
|
$this->data = ''; |
72
|
1 |
|
if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->startBlock) { |
73
|
|
|
// Block id refers to small blocks |
74
|
|
|
$rootPos = $this->ole->getBlockOffset($this->ole->root->startBlock); |
75
|
|
|
while ($blockId != -2) { |
76
|
|
|
$pos = $rootPos + $blockId * $this->ole->bigBlockSize; |
77
|
|
|
$blockId = $this->ole->sbat[$blockId]; |
78
|
|
|
fseek($this->ole->_file_handle, $pos); |
79
|
|
|
$this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
// Block id refers to big blocks |
83
|
1 |
|
while ($blockId != -2) { |
84
|
1 |
|
$pos = $this->ole->getBlockOffset($blockId); |
85
|
1 |
|
fseek($this->ole->_file_handle, $pos); |
86
|
1 |
|
$this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize); |
87
|
1 |
|
$blockId = $this->ole->bbat[$blockId]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
1 |
|
if (isset($this->params['size'])) { |
91
|
|
|
$this->data = substr($this->data, 0, $this->params['size']); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
if ($options & STREAM_USE_PATH) { |
95
|
|
|
$openedPath = $path; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Implements support for fclose(). |
103
|
|
|
*/ |
104
|
1 |
|
public function stream_close(): void // @codingStandardsIgnoreLine |
105
|
|
|
{ |
106
|
1 |
|
$this->ole = null; |
107
|
1 |
|
unset($GLOBALS['_OLE_INSTANCES']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Implements support for fread(), fgets() etc. |
112
|
|
|
* |
113
|
|
|
* @param int $count maximum number of bytes to read |
114
|
|
|
* |
115
|
|
|
* @return false|string |
116
|
|
|
*/ |
117
|
1 |
|
public function stream_read($count) // @codingStandardsIgnoreLine |
118
|
|
|
{ |
119
|
1 |
|
if ($this->stream_eof()) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
1 |
|
$s = substr($this->data, (int) $this->pos, $count); |
123
|
1 |
|
$this->pos += $count; |
124
|
|
|
|
125
|
1 |
|
return $s; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Implements support for feof(). |
130
|
|
|
* |
131
|
|
|
* @return bool TRUE if the file pointer is at EOF; otherwise FALSE |
132
|
|
|
*/ |
133
|
1 |
|
public function stream_eof() // @codingStandardsIgnoreLine |
134
|
|
|
{ |
135
|
1 |
|
return $this->pos >= strlen($this->data); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the position of the file pointer, i.e. its offset into the file |
140
|
|
|
* stream. Implements support for ftell(). |
141
|
|
|
* |
142
|
|
|
* @return int |
143
|
|
|
*/ |
144
|
1 |
|
public function stream_tell() // @codingStandardsIgnoreLine |
145
|
|
|
{ |
146
|
1 |
|
return $this->pos; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Implements support for fseek(). |
151
|
|
|
* |
152
|
|
|
* @param int $offset byte offset |
153
|
|
|
* @param int $whence SEEK_SET, SEEK_CUR or SEEK_END |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
1 |
|
public function stream_seek($offset, $whence) // @codingStandardsIgnoreLine |
158
|
|
|
{ |
159
|
1 |
|
if ($whence == SEEK_SET && $offset >= 0) { |
160
|
1 |
|
$this->pos = $offset; |
161
|
|
|
} elseif ($whence == SEEK_CUR && -$offset <= $this->pos) { |
162
|
|
|
$this->pos += $offset; |
163
|
|
|
// @phpstan-ignore-next-line |
164
|
|
|
} elseif ($whence == SEEK_END && -$offset <= count(/** @scrutinizer ignore-type */ $this->data)) { |
165
|
|
|
$this->pos = strlen($this->data) + $offset; |
166
|
|
|
} else { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Implements support for fstat(). Currently the only supported field is |
175
|
|
|
* "size". |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function stream_stat() // @codingStandardsIgnoreLine |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
'size' => strlen($this->data), |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// Methods used by stream_wrapper_register() that are not implemented: |
187
|
|
|
// bool stream_flush ( void ) |
188
|
|
|
// int stream_write ( string data ) |
189
|
|
|
// bool rename ( string path_from, string path_to ) |
190
|
|
|
// bool mkdir ( string path, int mode, int options ) |
191
|
|
|
// bool rmdir ( string path, int options ) |
192
|
|
|
// bool dir_opendir ( string path, int options ) |
193
|
|
|
// array url_stat ( string path, int flags ) |
194
|
|
|
// string dir_readdir ( void ) |
195
|
|
|
// bool dir_rewinddir ( void ) |
196
|
|
|
// bool dir_closedir ( void ) |
197
|
|
|
} |
198
|
|
|
|