1
|
|
|
<?php |
2
|
|
|
namespace TYPO3\PharStreamWrapper\Phar; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under the terms |
8
|
|
|
* of the MIT License (MIT). For the full copyright and license information, |
9
|
|
|
* please read the LICENSE file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* The TYPO3 project - inspiring people to share! |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
class Reader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $fileName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $fileType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $fileName |
28
|
|
|
*/ |
29
|
|
|
public function __construct($fileName) |
30
|
|
|
{ |
31
|
|
|
if (strpos($fileName, '://') !== false) { |
32
|
|
|
throw new ReaderException( |
33
|
|
|
'File name must not contain stream prefix', |
34
|
|
|
1539623708 |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->fileName = $fileName; |
39
|
|
|
$this->fileType = $this->determineFileType(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Container |
44
|
|
|
*/ |
45
|
|
|
public function resolveContainer() |
46
|
|
|
{ |
47
|
|
|
$data = $this->extractData($this->resolveStream() . $this->fileName); |
48
|
|
|
|
49
|
|
|
if ($data['stubContent'] === null) { |
50
|
|
|
throw new ReaderException( |
51
|
|
|
'Cannot resolve stub', |
52
|
|
|
1547807881 |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
if ($data['manifestContent'] === null || $data['manifestLength'] === null) { |
56
|
|
|
throw new ReaderException( |
57
|
|
|
'Cannot resolve manifest', |
58
|
|
|
1547807882 |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
if (strlen($data['manifestContent']) < $data['manifestLength']) { |
62
|
|
|
throw new ReaderException( |
63
|
|
|
sprintf( |
64
|
|
|
'Exected manifest length %d, got %d', |
65
|
|
|
strlen($data['manifestContent']), |
66
|
|
|
$data['manifestLength'] |
67
|
|
|
), |
68
|
|
|
1547807883 |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new Container( |
73
|
|
|
Stub::fromContent($data['stubContent']), |
74
|
|
|
Manifest::fromContent($data['manifestContent']) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $fileName e.g. '/path/file.phar' or 'compress.zlib:///path/file.phar' |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private function extractData($fileName) |
83
|
|
|
{ |
84
|
|
|
$stubContent = null; |
85
|
|
|
$manifestContent = null; |
86
|
|
|
$manifestLength = null; |
87
|
|
|
|
88
|
|
|
$resource = fopen($fileName, 'r'); |
89
|
|
|
if (!is_resource($resource)) { |
90
|
|
|
throw new ReaderException( |
91
|
|
|
sprintf('Resource %s could not be opened', $fileName), |
92
|
|
|
1547902055 |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
while (!feof($resource)) { |
97
|
|
|
$line = fgets($resource); |
98
|
|
|
// stop reading file when manifest can be extracted |
99
|
|
|
if ($manifestLength !== null && $manifestContent !== null && strlen($manifestContent) >= $manifestLength) { |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$stubPosition = strpos($line, '<?php'); |
104
|
|
|
$manifestPosition = strpos($line, '__HALT_COMPILER()'); |
105
|
|
|
|
106
|
|
|
// line contains both, start of (empty) stub and start of manifest |
107
|
|
|
if ($stubContent === null && $stubPosition !== false |
108
|
|
|
&& $manifestContent === null && $manifestPosition !== false) { |
109
|
|
|
$stubContent = substr($line, $stubPosition, $manifestPosition - $stubPosition - 1); |
110
|
|
|
$manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line); |
111
|
|
|
$manifestLength = $this->resolveManifestLength($manifestContent); |
112
|
|
|
// line contains start of stub |
113
|
|
|
} elseif ($stubContent === null && $stubPosition !== false) { |
114
|
|
|
$stubContent = substr($line, $stubPosition); |
115
|
|
|
// line contains start of manifest |
116
|
|
|
} elseif ($manifestContent === null && $manifestPosition !== false) { |
117
|
|
|
$manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line); |
118
|
|
|
$manifestLength = $this->resolveManifestLength($manifestContent); |
119
|
|
|
// manifest has been started (thus is cannot be stub anymore), add content |
120
|
|
|
} elseif ($manifestContent !== null) { |
121
|
|
|
$manifestContent .= $line; |
122
|
|
|
$manifestLength = $this->resolveManifestLength($manifestContent); |
123
|
|
|
// stub has been started (thus cannot be manifest here, yet), add content |
124
|
|
|
} elseif ($stubContent !== null) { |
125
|
|
|
$stubContent .= $line; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
fclose($resource); |
129
|
|
|
|
130
|
|
|
return array( |
131
|
|
|
'stubContent' => $stubContent, |
132
|
|
|
'manifestContent' => $manifestContent, |
133
|
|
|
'manifestLength' => $manifestLength, |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Resolves stream in order to handle compressed Phar archives. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
private function resolveStream() |
143
|
|
|
{ |
144
|
|
|
if ($this->fileType === 'application/x-gzip') { |
145
|
|
|
return 'compress.zlib://'; |
146
|
|
|
} elseif ($this->fileType === 'application/x-bzip2') { |
147
|
|
|
return 'compress.bzip2://'; |
148
|
|
|
} |
149
|
|
|
return ''; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
private function determineFileType() |
156
|
|
|
{ |
157
|
|
|
$fileInfo = new \finfo(); |
158
|
|
|
return $fileInfo->file($this->fileName, FILEINFO_MIME_TYPE); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $content |
163
|
|
|
* @return int|null |
164
|
|
|
*/ |
165
|
|
|
private function resolveManifestLength($content) |
166
|
|
|
{ |
167
|
|
|
if (strlen($content) < 4) { |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
return static::resolveFourByteLittleEndian($content, 0); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $content |
175
|
|
|
* @param int $start |
176
|
|
|
* @return int |
177
|
|
|
*/ |
178
|
|
|
public static function resolveFourByteLittleEndian($content, $start) |
179
|
|
|
{ |
180
|
|
|
$payload = substr($content, $start, 4); |
181
|
|
|
if (!is_string($payload)) { |
|
|
|
|
182
|
|
|
throw new ReaderException( |
183
|
|
|
sprintf('Cannot resolve value at offset %d', $start), |
184
|
|
|
1539614260 |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$value = unpack('V', $payload); |
189
|
|
|
if (!isset($value[1])) { |
190
|
|
|
throw new ReaderException( |
191
|
|
|
sprintf('Cannot resolve value at offset %d', $start), |
192
|
|
|
1539614261 |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
return $value[1]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $content |
200
|
|
|
* @param int $start |
201
|
|
|
* @return int |
202
|
|
|
*/ |
203
|
|
|
public static function resolveTwoByteBigEndian($content, $start) |
204
|
|
|
{ |
205
|
|
|
$payload = substr($content, $start, 2); |
206
|
|
|
if (!is_string($payload)) { |
|
|
|
|
207
|
|
|
throw new ReaderException( |
208
|
|
|
sprintf('Cannot resolve value at offset %d', $start), |
209
|
|
|
1539614263 |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$value = unpack('n', $payload); |
214
|
|
|
if (!isset($value[1])) { |
215
|
|
|
throw new ReaderException( |
216
|
|
|
sprintf('Cannot resolve value at offset %d', $start), |
217
|
|
|
1539614264 |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
return $value[1]; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|