1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deployer\Component\PharUpdate; |
4
|
|
|
|
5
|
|
|
use Deployer\Component\PharUpdate\Exception\FileException; |
6
|
|
|
use Deployer\Component\PharUpdate\Exception\LogicException; |
7
|
|
|
use Deployer\Component\PharUpdate\Version\Comparator; |
8
|
|
|
use Deployer\Component\PharUpdate\Version\Version; |
9
|
|
|
use Phar; |
10
|
|
|
use SplFileObject; |
11
|
|
|
use UnexpectedValueException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Manages an individual update. |
15
|
|
|
* |
16
|
|
|
* @author Kevin Herrera <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Update |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The temporary file path. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $file; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The name of the update file. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $name; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The URL where the public key can be downloaded from. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $publicKey; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The SHA1 file checksum. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $sha1; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The URL where the update can be downloaded from. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $url; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The version of the update. |
57
|
|
|
* |
58
|
|
|
* @var Version |
59
|
|
|
*/ |
60
|
|
|
private $version; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the update information. |
64
|
|
|
* |
65
|
|
|
* @param string $name The name of the update file. |
66
|
|
|
* @param string $sha1 The SHA1 file checksum. |
67
|
|
|
* @param string $url The URL where the update can be downloaded from. |
68
|
|
|
* @param Version $version The version of the update. |
69
|
|
|
* @param string $key The URL where the public key can be downloaded |
70
|
|
|
* from. |
71
|
|
|
*/ |
72
|
|
|
public function __construct( |
73
|
|
|
$name, |
74
|
|
|
$sha1, |
75
|
|
|
$url, |
76
|
|
|
Version $version, |
77
|
|
|
$key = null |
78
|
|
|
) { |
79
|
|
|
$this->name = $name; |
80
|
|
|
$this->publicKey = $key; |
81
|
|
|
$this->sha1 = $sha1; |
82
|
|
|
$this->url = $url; |
83
|
|
|
$this->version = $version; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Copies the update file to the destination. |
88
|
|
|
* |
89
|
|
|
* @param string $file The target file. |
90
|
|
|
* |
91
|
|
|
* @throws Exception\Exception |
92
|
|
|
* @throws FileException If the file could not be replaced. |
93
|
|
|
*/ |
94
|
|
|
public function copyTo($file) |
95
|
|
|
{ |
96
|
|
|
if (null === $this->file) { |
97
|
|
|
throw LogicException::create( |
98
|
|
|
'The update file has not been downloaded.' |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$mode = 0755; |
103
|
|
|
|
104
|
|
|
if (file_exists($file)) { |
105
|
|
|
$mode = fileperms($file) & 511; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (false === @copy($this->file, $file)) { |
109
|
|
|
throw FileException::lastError(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (false === @chmod($file, $mode)) { |
113
|
|
|
throw FileException::lastError(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$key = $file . '.pubkey'; |
117
|
|
|
|
118
|
|
|
if (file_exists($this->file . '.pubkey')) { |
119
|
|
|
if (false === @copy($this->file . '.pubkey', $key)) { |
120
|
|
|
throw FileException::lastError(); |
121
|
|
|
} |
122
|
|
|
} elseif (file_exists($key)) { |
123
|
|
|
if (false === @unlink($key)) { |
124
|
|
|
throw FileException::lastError(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Cleans up by deleting the temporary update file. |
131
|
|
|
* |
132
|
|
|
* @throws FileException If the file could not be deleted. |
133
|
|
|
*/ |
134
|
|
|
public function deleteFile() |
135
|
|
|
{ |
136
|
|
|
if ($this->file) { |
137
|
|
|
if (file_exists($this->file)) { |
138
|
|
|
if (false === @unlink($this->file)) { |
139
|
|
|
throw FileException::lastError(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (file_exists($this->file . '.pubkey')) { |
144
|
|
|
if (false === @unlink($this->file . '.pubkey')) { |
145
|
|
|
throw FileException::lastError(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$dir = dirname($this->file); |
150
|
|
|
|
151
|
|
|
if (file_exists($dir)) { |
152
|
|
|
if (false === @rmdir($dir)) { |
153
|
|
|
throw FileException::lastError(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->file = null; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Downloads the update file to a temporary location. |
163
|
|
|
* |
164
|
|
|
* @return string The temporary file path. |
165
|
|
|
* |
166
|
|
|
* @throws Exception\Exception |
167
|
|
|
* @throws FileException If the SHA1 checksum differs. |
168
|
|
|
* @throws UnexpectedValueException If the Phar is corrupt. |
169
|
|
|
*/ |
170
|
|
|
public function getFile() |
171
|
|
|
{ |
172
|
|
|
if (null === $this->file) { |
173
|
|
|
unlink($this->file = tempnam(sys_get_temp_dir(), 'upd')); |
174
|
|
|
mkdir($this->file); |
175
|
|
|
|
176
|
|
|
$this->file .= DIRECTORY_SEPARATOR . $this->name; |
177
|
|
|
|
178
|
|
|
$in = new SplFileObject($this->url, 'rb', false); |
179
|
|
|
$out = new SplFileObject($this->file, 'wb', false); |
180
|
|
|
|
181
|
|
|
while (false === $in->eof()) { |
182
|
|
|
$out->fwrite($in->fgets()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
unset($in, $out); |
186
|
|
|
|
187
|
|
|
if ($this->publicKey) { |
188
|
|
|
$in = new SplFileObject($this->publicKey, 'r', false); |
189
|
|
|
$out = new SplFileObject($this->file . '.pubkey', 'w', false); |
190
|
|
|
|
191
|
|
|
while (false === $in->eof()) { |
192
|
|
|
$out->fwrite($in->fgets()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
unset($in, $out); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($this->sha1 !== ($sha1 = sha1_file($this->file))) { |
199
|
|
|
$this->deleteFile(); |
200
|
|
|
|
201
|
|
|
throw FileException::create( |
202
|
|
|
'Mismatch of the SHA1 checksum (%s) of the downloaded file (%s).', |
203
|
|
|
$this->sha1, |
204
|
|
|
$sha1 |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// double check |
209
|
|
|
try { |
210
|
|
|
new Phar($this->file); |
211
|
|
|
} catch (UnexpectedValueException $exception) { |
212
|
|
|
$this->deleteFile(); |
213
|
|
|
|
214
|
|
|
throw $exception; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $this->file; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns name of the update file. |
223
|
|
|
* |
224
|
|
|
* @return string The name. |
225
|
|
|
*/ |
226
|
|
|
public function getName() |
227
|
|
|
{ |
228
|
|
|
return $this->name; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the URL where the public key can be downloaded from. |
233
|
|
|
* |
234
|
|
|
* @return string The URL. |
235
|
|
|
*/ |
236
|
|
|
public function getPublicKey() |
237
|
|
|
{ |
238
|
|
|
return $this->publicKey; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns the SHA1 file checksum. |
243
|
|
|
* |
244
|
|
|
* @return string The checksum. |
245
|
|
|
*/ |
246
|
|
|
public function getSha1() |
247
|
|
|
{ |
248
|
|
|
return $this->sha1; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns the URL where the update can be downloaded from. |
253
|
|
|
* |
254
|
|
|
* @return string The URL. |
255
|
|
|
*/ |
256
|
|
|
public function getUrl() |
257
|
|
|
{ |
258
|
|
|
return $this->url; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the version of the update. |
263
|
|
|
* |
264
|
|
|
* @return Version The version. |
265
|
|
|
*/ |
266
|
|
|
public function getVersion() |
267
|
|
|
{ |
268
|
|
|
return $this->version; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Checks if this update is newer than the version given. |
273
|
|
|
* |
274
|
|
|
* @param Version $version The current version. |
275
|
|
|
* |
276
|
|
|
* @return boolean TRUE if the update is newer, FALSE if not. |
277
|
|
|
*/ |
278
|
|
|
public function isNewer(Version $version) |
279
|
|
|
{ |
280
|
|
|
return Comparator::isGreaterThan($this->version, $version); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|