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