|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SSpkS\Package; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPK Package class |
|
7
|
|
|
* |
|
8
|
|
|
* @property string $spk Path to SPK file |
|
9
|
|
|
* @property string $spk_url URL to SPK file |
|
10
|
|
|
* @property string $displayname Pretty printed name of package (falls back to $package if not present) |
|
11
|
|
|
* @property string $package Package name |
|
12
|
|
|
* @property string $version Package version |
|
13
|
|
|
* @property string $description Package description |
|
14
|
|
|
* @property string $maintainer Package maintainer |
|
15
|
|
|
* @property string $maintainer_url URL of maintainer's web page |
|
16
|
|
|
* @property string $distributor Package distributor |
|
17
|
|
|
* @property string $distributor_url URL of distributor's web page |
|
18
|
|
|
* @property string $support_url URL of support web page |
|
19
|
|
|
* @property array $arch List of supported architectures, or 'noarch' |
|
20
|
|
|
* @property array $thumbnail List of thumbnail files |
|
21
|
|
|
* @property array $thumbnail_url List of thumbnail URLs |
|
22
|
|
|
* @property array $snapshot List of screenshot files |
|
23
|
|
|
* @property array $snapshot_url List of screenshot URLs |
|
24
|
|
|
* @property bool $beta TRUE if this is a beta package. |
|
25
|
|
|
* @property string $firmware Minimum firmware needed on device. |
|
26
|
|
|
* @property string $install_dep_services Dependencies required by this package. |
|
27
|
|
|
* @property bool $silent_install Allow silent install |
|
28
|
|
|
* @property bool $silent_uninstall Allow silent uninstall |
|
29
|
|
|
* @property bool $silent_upgrade Allow silent upgrade |
|
30
|
|
|
* @property bool $auto_upgrade_from Allow auto upgrade if version is newer than this field |
|
31
|
|
|
* @property bool $qinst Allow silent install |
|
32
|
|
|
* @property bool $qupgrade Allow silent upgrade |
|
33
|
|
|
* @property bool $qstart Allow automatic start after install |
|
34
|
|
|
*/ |
|
35
|
|
|
class Package |
|
36
|
|
|
{ |
|
37
|
|
|
private $config; |
|
38
|
|
|
private $filepath; |
|
39
|
|
|
private $filepathNoExt; |
|
40
|
|
|
private $filename; |
|
41
|
|
|
private $filenameNoExt; |
|
42
|
|
|
private $metafile; |
|
43
|
|
|
private $wizfile; |
|
44
|
|
|
private $nowizfile; |
|
45
|
|
|
private $metadata; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param \SSpkS\Config $config Config object |
|
49
|
|
|
* @param string $filename Filename of SPK file |
|
50
|
|
|
*/ |
|
51
|
17 |
|
public function __construct(\SSpkS\Config $config, $filename) |
|
52
|
|
|
{ |
|
53
|
17 |
|
$this->config = $config; |
|
54
|
17 |
|
if (!preg_match('/\.spk$/', $filename)) { |
|
55
|
1 |
|
throw new \Exception('File ' . $filename . ' doesn\'t have .spk extension!'); |
|
56
|
|
|
} |
|
57
|
16 |
|
if (!file_exists($filename)) { |
|
58
|
1 |
|
throw new \Exception('File ' . $filename . ' not found!'); |
|
59
|
|
|
} |
|
60
|
15 |
|
$this->filepath = $filename; |
|
61
|
15 |
|
$this->filename = basename($filename); |
|
62
|
15 |
|
$this->filenameNoExt = basename($filename, '.spk'); |
|
63
|
15 |
|
$this->filepathNoExt = $this->config->paths['cache'] . $this->filenameNoExt; |
|
64
|
15 |
|
$this->metafile = $this->filepathNoExt . '.nfo'; |
|
65
|
15 |
|
$this->wizfile = $this->filepathNoExt . '.wiz'; |
|
66
|
15 |
|
$this->nowizfile = $this->filepathNoExt . '.nowiz'; |
|
67
|
|
|
// Make sure we have metadata available |
|
68
|
15 |
|
$this->collectMetadata(); |
|
69
|
15 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Getter magic method. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $name Name of requested value. |
|
75
|
|
|
* @return mixed Requested value. |
|
76
|
|
|
*/ |
|
77
|
9 |
|
public function __get($name) |
|
78
|
|
|
{ |
|
79
|
9 |
|
return $this->metadata[$name]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Setter magic method. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $name Name of variable to set. |
|
86
|
|
|
* @param mixed $value Value to set. |
|
87
|
|
|
*/ |
|
88
|
5 |
|
public function __set($name, $value) |
|
89
|
|
|
{ |
|
90
|
5 |
|
$this->metadata[$name] = $value; |
|
91
|
5 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Isset feature magic method. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $name Name of requested value. |
|
97
|
|
|
* @return bool TRUE if value exists, FALSE otherwise. |
|
98
|
|
|
*/ |
|
99
|
5 |
|
public function __isset($name) |
|
100
|
|
|
{ |
|
101
|
5 |
|
return isset($this->metadata[$name]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Unset feature magic method. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name Name of value to unset. |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function __unset($name) |
|
110
|
|
|
{ |
|
111
|
1 |
|
unset($this->metadata[$name]); |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Parses boolean value ('yes', '1', 'true') into |
|
116
|
|
|
* boolean type. |
|
117
|
|
|
* |
|
118
|
|
|
* @param mixed $value Input value |
|
119
|
|
|
* @return bool Boolean interpretation of $value. |
|
120
|
|
|
*/ |
|
121
|
15 |
|
public function parseBool($value) |
|
122
|
|
|
{ |
|
123
|
15 |
|
return in_array($value, array('true', 'yes', '1', 1)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Checks if given property $prop exists and converts it |
|
128
|
|
|
* into a boolean value. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $prop Property to convert |
|
131
|
|
|
*/ |
|
132
|
15 |
|
private function fixBoolIfExist($prop) |
|
133
|
|
|
{ |
|
134
|
15 |
|
if (isset($this->metadata[$prop])) { |
|
135
|
9 |
|
$this->metadata[$prop] = $this->parseBool($this->metadata[$prop]); |
|
136
|
|
|
} |
|
137
|
15 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Gathers metadata from package. Extracts INFO file if neccessary. |
|
141
|
|
|
*/ |
|
142
|
15 |
|
private function collectMetadata() |
|
143
|
|
|
{ |
|
144
|
15 |
|
if (!is_null($this->metadata)) { |
|
145
|
|
|
// metadata already collected |
|
146
|
|
|
return; |
|
147
|
|
|
} |
|
148
|
15 |
|
$this->extractIfMissing('INFO', $this->metafile); |
|
149
|
15 |
|
$this->metadata = parse_ini_file($this->metafile); |
|
150
|
15 |
|
if (!isset($this->metadata['displayname'])) { |
|
151
|
15 |
|
$this->metadata['displayname'] = $this->metadata['package']; |
|
152
|
|
|
} |
|
153
|
15 |
|
$this->metadata['spk'] = $this->filepath; |
|
154
|
|
|
|
|
155
|
|
|
// Convert architecture(s) to array, as multiple architectures can be specified |
|
156
|
15 |
|
$this->metadata['arch'] = explode(' ', $this->metadata['arch']); |
|
157
|
|
|
|
|
158
|
15 |
|
$this->fixBoolIfExist('silent_install'); |
|
159
|
15 |
|
$this->fixBoolIfExist('silent_uninstall'); |
|
160
|
15 |
|
$this->fixBoolIfExist('silent_upgrade'); |
|
161
|
|
|
|
|
162
|
15 |
|
if ($this->isBeta()) { |
|
163
|
6 |
|
$this->metadata['beta'] = true; |
|
164
|
|
|
} else { |
|
165
|
15 |
|
$this->metadata['beta'] = false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
15 |
|
$qValue = $this->hasWizardDir() ? false : true; |
|
169
|
15 |
|
$this->metadata['thumbnail'] = $this->getThumbnails(); |
|
170
|
15 |
|
$this->metadata['snapshot'] = $this->getSnapshots(); |
|
171
|
15 |
|
$this->metadata['qinst'] = !empty($this->metadata['qinst']) ? $this->parseBool($this->metadata['qinst']) : $qValue; |
|
172
|
15 |
|
$this->metadata['qupgrade'] = !empty($this->metadata['qupgrade']) ? $this->parseBool($this->metadata['qupgrade']) : $qValue; |
|
173
|
15 |
|
$this->metadata['qstart'] = !empty($this->metadata['qstart']) ? $this->parseBool($this->metadata['qstart']) : $qValue; |
|
174
|
15 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns metadata for this package. |
|
178
|
|
|
* |
|
179
|
|
|
* @return array Metadata. |
|
180
|
|
|
*/ |
|
181
|
3 |
|
public function getMetadata() |
|
182
|
|
|
{ |
|
183
|
3 |
|
return $this->metadata; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Extracts $inPkgName from package to $targetFile, if it doesn't |
|
188
|
|
|
* already exist. Needs the phar.so extension and allow_url_fopen. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $inPkgName Filename in package |
|
191
|
|
|
* @param string $targetFile Path to destination |
|
192
|
|
|
* @throws \Exception if the file couldn't get extracted. |
|
193
|
|
|
* @return bool TRUE if successful or no action needed. |
|
194
|
|
|
*/ |
|
195
|
15 |
|
public function extractIfMissing($inPkgName, $targetFile) |
|
196
|
|
|
{ |
|
197
|
15 |
|
if (file_exists($targetFile)) { |
|
198
|
|
|
// Everything in working order |
|
199
|
8 |
|
return true; |
|
200
|
|
|
} |
|
201
|
|
|
// Try to extract file |
|
202
|
15 |
|
$tmp_dir = sys_get_temp_dir(); |
|
203
|
15 |
|
self::ensureAvailableSpace($tmp_dir, 'TMP'); |
|
204
|
15 |
|
self::ensureAvailableSpace(dirname($targetFile), 'Package'); |
|
205
|
|
|
try { |
|
206
|
15 |
|
$p = new \PharData($this->filepath, \Phar::CURRENT_AS_FILEINFO | \Phar::KEY_AS_FILENAME); |
|
207
|
|
|
} catch (\UnexpectedValueException $e) { |
|
208
|
|
|
rename($this->filepath, $this->filepath . '.invalid'); |
|
209
|
|
|
throw new \Exception('Package ' . $this->filepath . ' not readable! Will be ignored in the future. Please try again!'); |
|
210
|
|
|
} |
|
211
|
15 |
|
$tmpExtractedFilepath = $tmp_dir . DIRECTORY_SEPARATOR . $inPkgName; |
|
212
|
15 |
|
if (file_exists($tmpExtractedFilepath)) { |
|
213
|
|
|
// stale file from before - unlink first |
|
214
|
|
|
unlink($tmpExtractedFilepath); |
|
215
|
|
|
} |
|
216
|
15 |
|
$p->extractTo($tmp_dir, $inPkgName); |
|
217
|
15 |
|
rename($tmpExtractedFilepath, $targetFile); |
|
218
|
15 |
|
return true; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Ensures there is enough free space on target drive |
|
223
|
|
|
* @param string $dir the directory whose volume has to be tested |
|
224
|
|
|
* @param string $friendlyName a nice name to display in case of error |
|
225
|
|
|
* @throws \Exception |
|
226
|
|
|
*/ |
|
227
|
15 |
|
private static function ensureAvailableSpace($dir, $friendlyName) |
|
228
|
|
|
{ |
|
229
|
15 |
|
$free = @disk_free_space($dir); |
|
230
|
15 |
|
if (!empty($free) && $free < 2048) { |
|
231
|
|
|
throw new \Exception($friendlyName . ' folder only has ' . $free . ' Bytes available. Disk full!'); |
|
232
|
|
|
} |
|
233
|
15 |
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Returns a true if the package contains WIZARD_UIFILES. |
|
237
|
|
|
* |
|
238
|
|
|
* @return bool Package has a wizard |
|
239
|
|
|
*/ |
|
240
|
15 |
|
public function hasWizardDir() |
|
241
|
|
|
{ |
|
242
|
15 |
|
if (file_exists($this->wizfile)) { |
|
243
|
|
|
return true; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
15 |
|
if (file_exists($this->nowizfile)) { |
|
247
|
5 |
|
return false; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
try { |
|
251
|
10 |
|
$p = new \PharData($this->filepath, \Phar::CURRENT_AS_FILEINFO | \Phar::KEY_AS_FILENAME); |
|
252
|
|
|
} catch (\UnexpectedValueException $e) { |
|
253
|
|
|
rename($this->filepath, $this->filepath . '.invalid'); |
|
254
|
|
|
throw new \Exception('Package ' . $this->filepath . ' not readable! Will be ignored in the future. Please try again!'); |
|
255
|
|
|
} |
|
256
|
10 |
|
foreach ($p as $file) { |
|
257
|
10 |
|
if (substr($file, strrpos($file, '/') + 1) == 'WIZARD_UIFILES') { |
|
258
|
|
|
touch($this->wizfile); |
|
259
|
|
|
return true; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
10 |
|
touch($this->nowizfile); |
|
263
|
10 |
|
return false; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Returns a list of thumbnails for the specified package. |
|
268
|
|
|
* |
|
269
|
|
|
* @param string $pathPrefix Prefix to put before file path |
|
270
|
|
|
* @return array List of thumbnail urls |
|
271
|
|
|
*/ |
|
272
|
15 |
|
public function getThumbnails($pathPrefix = '') |
|
273
|
|
|
{ |
|
274
|
|
|
$thumbnailSources = array( |
|
275
|
|
|
'72' => array( |
|
276
|
15 |
|
'file' => 'PACKAGE_ICON.PNG', |
|
277
|
|
|
'info' => 'package_icon', |
|
278
|
|
|
), |
|
279
|
|
|
'120' => array( |
|
280
|
|
|
'file' => 'PACKAGE_ICON_256.PNG', |
|
281
|
|
|
'info' => 'package_icon_256', |
|
282
|
|
|
), |
|
283
|
|
|
); |
|
284
|
15 |
|
$thumbnails = array(); |
|
285
|
15 |
|
foreach ($thumbnailSources as $size => $sourceList) { |
|
286
|
15 |
|
$thumbName = $this->filepathNoExt . '_thumb_' . $size . '.png'; |
|
287
|
|
|
// Try to find file in package, otherwise check if defined in INFO |
|
288
|
|
|
try { |
|
289
|
15 |
|
$this->extractIfMissing($sourceList['file'], $thumbName); |
|
290
|
15 |
|
} catch (\Exception $e) { |
|
291
|
|
|
// Check if icon is in metadata |
|
292
|
15 |
|
if (isset($this->metadata[$sourceList['info']])) { |
|
293
|
11 |
|
file_put_contents($thumbName, base64_decode($this->metadata[$sourceList['info']])); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
// Use $size px thumbnail, if available |
|
298
|
15 |
|
if (file_exists($thumbName)) { |
|
299
|
15 |
|
$thumbnails[] = $pathPrefix . $thumbName; |
|
300
|
|
|
} else { |
|
301
|
|
|
// Use theme's default pictures |
|
302
|
15 |
|
$themeUrl = $this->config->paths['themes'] . $this->config->site['theme'] . '/'; |
|
303
|
15 |
|
$thumbnails[] = $pathPrefix . $themeUrl . 'images/default_package_icon_' . $size . '.png'; |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
15 |
|
return $thumbnails; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Returns a list of screenshots for the specified package. |
|
311
|
|
|
* |
|
312
|
|
|
* @param string $pathPrefix Prefix to put before file path |
|
313
|
|
|
* @return array List of screenshots |
|
314
|
|
|
*/ |
|
315
|
15 |
|
public function getSnapshots($pathPrefix = '') |
|
316
|
|
|
{ |
|
317
|
|
|
/* Let's first try to extract screenshots from package (SSpkS feature) */ |
|
318
|
15 |
|
$i = 1; |
|
319
|
15 |
|
while (true) { |
|
320
|
|
|
try { |
|
321
|
15 |
|
$this->extractIfMissing('screen_' . $i . '.png', $this->filepathNoExt . '_screen_' . $i . '.png'); |
|
322
|
8 |
|
$i++; |
|
323
|
15 |
|
} catch (\Exception $e) { |
|
324
|
15 |
|
break; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
15 |
|
$snapshots = array(); |
|
328
|
|
|
// Add screenshots, if available |
|
329
|
15 |
|
foreach (glob($this->filepathNoExt . '*_screen_*.png') as $snapshot) { |
|
330
|
8 |
|
$snapshots[] = $pathPrefix . $snapshot; |
|
331
|
|
|
} |
|
332
|
15 |
|
return $snapshots; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Checks compatibility to the given $arch-itecture. |
|
337
|
|
|
* |
|
338
|
|
|
* @param string $arch Architecture to check against (or "noarch") |
|
339
|
|
|
* @return bool TRUE if compatible, otherwise FALSE. |
|
340
|
|
|
*/ |
|
341
|
1 |
|
public function isCompatibleToArch($arch) |
|
342
|
|
|
{ |
|
343
|
|
|
// TODO: Check arch family, too? |
|
344
|
1 |
|
return (in_array($arch, $this->metadata['arch']) || in_array('noarch', $this->metadata['arch'])); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Checks compatibility to the given firmware $version. |
|
349
|
|
|
* |
|
350
|
|
|
* @param string $version Target firmware version. |
|
351
|
|
|
* @return bool TRUE if compatible, otherwise FALSE. |
|
352
|
|
|
*/ |
|
353
|
1 |
|
public function isCompatibleToFirmware($version) |
|
354
|
|
|
{ |
|
355
|
1 |
|
return version_compare($this->metadata['firmware'], $version, '<='); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Checks if this package is a beta version or not. |
|
360
|
|
|
* |
|
361
|
|
|
* @return bool TRUE if this is a beta version, FALSE otherwise. |
|
362
|
|
|
*/ |
|
363
|
15 |
|
public function isBeta() |
|
364
|
|
|
{ |
|
365
|
15 |
|
return (isset($this->metadata['beta']) && $this->parseBool($this->metadata['beta'])); |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
|