Test Setup Failed
Push — master ( 1041de...2c179f )
by Julien
02:37
created

Package::hasWizardDir()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

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