Timestamps.php
5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
* This file is part of PHAR Utils.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Seld\PharUtils;
class Timestamps
{
private $contents;
/**
* @param string $file path to the phar file to use
*/
public function __construct($file)
{
$this->contents = file_get_contents($file);
}
/**
* Updates each file's unix timestamps in the PHAR
*
* The PHAR signature can then be produced in a reproducible manner.
*
* @param int|DateTime|string $timestamp Date string or DateTime or unix timestamp to use
*/
public function updateTimestamps($timestamp = null)
{
if ($timestamp instanceof \DateTime) {
$timestamp = $timestamp->getTimestamp();
} elseif (is_string($timestamp)) {
$timestamp = strtotime($timestamp);
} elseif (!is_int($timestamp)) {
$timestamp = strtotime('1984-12-24T00:00:00Z');
}
// detect manifest offset / end of stub
if (!preg_match('{__HALT_COMPILER\(\);(?: +\?>)?\r?\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) {
throw new \RuntimeException('Could not detect the stub\'s end in the phar');
}
// set starting position and skip past manifest length
$pos = $match[0][1] + strlen($match[0][0]);
$stubEnd = $pos + $this->readUint($pos, 4);
$pos += 4;
$numFiles = $this->readUint($pos, 4);
$pos += 4;
// skip API version (YOLO)
$pos += 2;
// skip PHAR flags
$pos += 4;
$aliasLength = $this->readUint($pos, 4);
$pos += 4 + $aliasLength;
$metadataLength = $this->readUint($pos, 4);
$pos += 4 + $metadataLength;
while ($pos < $stubEnd) {
$filenameLength = $this->readUint($pos, 4);
$pos += 4 + $filenameLength;
// skip filesize
$pos += 4;
// update timestamp to a fixed value
$this->contents = substr_replace($this->contents, pack('L', $timestamp), $pos, 4);
// skip timestamp, compressed file size, crc32 checksum and file flags
$pos += 4*4;
$metadataLength = $this->readUint($pos, 4);
$pos += 4 + $metadataLength;
$numFiles--;
}
if ($numFiles !== 0) {
throw new \LogicException('All files were not processed, something must have gone wrong');
}
}
/**
* Saves the updated phar file, optionally with an updated signature.
*
* @param string $path
* @param int $signatureAlgo One of Phar::MD5, Phar::SHA1, Phar::SHA256 or Phar::SHA512
* @return bool
*/
public function save($path, $signatureAlgo)
{
$pos = $this->determineSignatureBegin();
$algos = array(
\Phar::MD5 => 'md5',
\Phar::SHA1 => 'sha1',
\Phar::SHA256 => 'sha256',
\Phar::SHA512 => 'sha512',
);
if (!isset($algos[$signatureAlgo])) {
throw new \UnexpectedValueException('Invalid hash algorithm given: '.$signatureAlgo.' expected one of Phar::MD5, Phar::SHA1, Phar::SHA256 or Phar::SHA512');
}
$algo = $algos[$signatureAlgo];
// re-sign phar
// signature
$signature = hash($algo, substr($this->contents, 0, $pos), true)
// sig type
. pack('L', $signatureAlgo)
// ohai Greg & Marcus
. 'GBMB';
$this->contents = substr($this->contents, 0, $pos) . $signature;
return file_put_contents($path, $this->contents);
}
private function readUint($pos, $bytes)
{
$res = unpack("L", substr($this->contents, $pos, $bytes));
return $res[1];
}
/**
* Determine the beginning of the signature.
*
* @return int
*/
private function determineSignatureBegin()
{
// detect signature position
if (!preg_match('{__HALT_COMPILER\(\);(?: +\?>)?\r?\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) {
throw new \RuntimeException('Could not detect the stub\'s end in the phar');
}
// set starting position and skip past manifest length
$pos = $match[0][1] + strlen($match[0][0]);
$stubEnd = $pos + $this->readUint($pos, 4);
$pos += 4;
$numFiles = $this->readUint($pos, 4);
$pos += 4;
// skip API version (YOLO)
$pos += 2;
// skip PHAR flags
$pos += 4;
$aliasLength = $this->readUint($pos, 4);
$pos += 4 + $aliasLength;
$metadataLength = $this->readUint($pos, 4);
$pos += 4 + $metadataLength;
$compressedSizes = 0;
while ($pos < $stubEnd) {
$filenameLength = $this->readUint($pos, 4);
$pos += 4 + $filenameLength;
// skip filesize and timestamp
$pos += 2*4;
$compressedSizes += $this->readUint($pos, 4);
// skip compressed file size, crc32 checksum and file flags
$pos += 3*4;
$metadataLength = $this->readUint($pos, 4);
$pos += 4 + $metadataLength;
$numFiles--;
}
if ($numFiles !== 0) {
throw new \LogicException('All files were not processed, something must have gone wrong');
}
return $pos + $compressedSizes;
}
}