Completed
Push — master ( f044cb...2a21fa )
by Bjørn
02:41
created

Cwebp::checkAllHashes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 4
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 20
rs 10
1
<?php
2
3
namespace WebPConvert\Convert\Converters;
4
5
use WebPConvert\Convert\Converters\AbstractConverter;
6
use WebPConvert\Convert\Converters\ConverterTraits\EncodingAutoTrait;
7
use WebPConvert\Convert\Converters\ConverterTraits\ExecTrait;
8
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
9
use WebPConvert\Convert\Exceptions\ConversionFailedException;
10
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperationalException;
11
use WebPConvert\Options\BooleanOption;
12
use WebPConvert\Options\SensitiveStringOption;
13
use WebPConvert\Options\StringOption;
14
15
/**
16
 * Convert images to webp by calling cwebp binary.
17
 *
18
 * @package    WebPConvert
19
 * @author     Bjørn Rosell <[email protected]>
20
 * @since      Class available since Release 2.0.0
21
 */
22
class Cwebp extends AbstractConverter
23
{
24
25
    use EncodingAutoTrait;
26
    use ExecTrait;
27
28
    protected function getUnsupportedDefaultOptions()
29
    {
30
        return [];
31
    }
32
33 8
    protected function createOptions()
34
    {
35 8
        parent::createOptions();
36
37 8
        $this->options2->addOptions(
38 8
            new StringOption('command-line-options', ''),
39 8
            new SensitiveStringOption('rel-path-to-precompiled-binaries', './Binaries'),
40 8
            new BooleanOption('try-common-system-paths', true),
41 8
            new BooleanOption('try-supplied-binary-for-os', true)
42
        );
43 8
    }
44
45
    // System paths to look for cwebp binary
46
    private static $cwebpDefaultPaths = [
47
        'cwebp',
48
        '/usr/bin/cwebp',
49
        '/usr/local/bin/cwebp',
50
        '/usr/gnu/bin/cwebp',
51
        '/usr/syno/bin/cwebp'
52
    ];
53
54
    // OS-specific binaries included in this library, along with hashes
55
    // If other binaries are going to be added, notice that the first argument is what PHP_OS returns.
56
    // (possible values, see here: https://stackoverflow.com/questions/738823/possible-values-for-php-os)
57
    // Got the precompiled binaries here: https://developers.google.com/speed/webp/docs/precompiled
58
    private static $suppliedBinariesInfo = [
59
        'WINNT' => [
60
            ['cwebp-1.0.3-windows-x64.exe', 'b3aaab03ca587e887f11f6ae612293d034ee04f4f7f6bc7a175321bb47a10169'],
61
        ],
62
        'Darwin' => [
63
            ['cwebp-1.0.3-mac-10.14', '7332ed5f0d4091e2379b1eaa32a764f8c0d51b7926996a1dc8b4ef4e3c441a12'],
64
        ],
65
        'SunOS' => [
66
            // Got this from ewww Wordpress plugin, which unfortunately still uses the old 0.6.0 versions
67
            // Can you help me get a 1.0.3 version?
68
            ['cwebp-0.6.0-solaris', '1febaffbb18e52dc2c524cda9eefd00c6db95bc388732868999c0f48deb73b4f']
69
        ],
70
        'FreeBSD' => [
71
            // Got this from ewww Wordpress plugin, which unfortunately still uses the old 0.6.0 versions
72
            // Can you help me get a 1.0.3 version?
73
            ['cwebp-0.6.0-fbsd', 'e5cbea11c97fadffe221fdf57c093c19af2737e4bbd2cb3cd5e908de64286573']
74
        ],
75
        'Linux' => [
76
            // Dynamically linked executable.
77
            // It seems it is slightly faster than the statically linked
78
            ['cwebp-1.0.3-linux-x86-64', 'a663215a46d347f63e1ca641c18527a1ae7a2c9a0ae85ca966a97477ea13dfe0'],
79
80
            // Statically linked executable
81
            // It may be that it on some systems works, where the dynamically linked does not (see #196)
82
            ['cwebp-1.0.3-linux-x86-64-static', 'ab96f01b49336da8b976c498528080ff614112d5985da69943b48e0cb1c5228a'],
83
84
            // Old executable for systems in case both of the above fails
85
            ['cwebp-0.6.1-linux-x86-64', '916623e5e9183237c851374d969aebdb96e0edc0692ab7937b95ea67dc3b2568'],
86
        ]
87
    ];
88
89
    /**
90
     *  Check all hashes of the precompiled binaries.
91
     *
92
     *  This isn't used when converting, but can be used as a startup check.
93
     */
94
    public function checkAllHashes()
95
    {
96
        foreach (self::$suppliedBinariesInfo as $os => $arr) {
97
            foreach ($arr as $i => list($filename, $hash)) {
98
                if ($hash != hash_file("sha256", __DIR__ . '/Binaries/' . $filename)) {
99
                    throw new \Exception('Hash for ' . $filename . ' is incorrect!');
100
                }
101
            }
102
        }
103
    }
104
105 3
    public function checkOperationality()
106
    {
107 3
        $this->checkOperationalityExecTrait();
108
109 3
        $options = $this->options;
110 3
        if (!$options['try-supplied-binary-for-os'] && !$options['try-common-system-paths']) {
111 1
            throw new ConverterNotOperationalException(
112
                'Configured to neither look for cweb binaries in common system locations, ' .
113
                'nor to use one of the supplied precompiled binaries. But these are the only ways ' .
114 1
                'this converter can convert images. No conversion can be made!'
115
            );
116
        }
117 2
    }
118
119 2
    private function executeBinary($binary, $commandOptions, $useNice)
120
    {
121
        //$version = $this->detectVersion($binary);
122
123 2
        $command = ($useNice ? 'nice ' : '') . $binary . ' ' . $commandOptions;
124
125
        //$logger->logLn('command options:' . $commandOptions);
126 2
        $this->logLn('Trying to convert by executing the following command:');
127 2
        $this->logLn($command);
128 2
        exec($command, $output, $returnCode);
129 2
        $this->logExecOutput($output);
130
        /*
131
        if ($returnCode == 255) {
132
            if (isset($output[0])) {
133
                // Could be an error like 'Error! Cannot open output file' or 'Error! ...preset... '
134
                $this->logLn(print_r($output[0], true));
135
            }
136
        }*/
137
        //$logger->logLn(self::msgForExitCode($returnCode));
138 2
        return intval($returnCode);
139
    }
140
141
    /**
142
     *  Use "escapeshellarg()" on all arguments in a commandline string of options
143
     *
144
     *  For example, passing '-sharpness 5 -crop 10 10 40 40 -low_memory' will result in:
145
     *  [
146
     *    "-sharpness '5'"
147
     *    "-crop '10' '10' '40' '40'"
148
     *    "-low_memory"
149
     *  ]
150
     * @param  string $commandLineOptions  string which can contain multiple commandline options
151
     * @return array  Array of command options
152
     */
153 1
    private static function escapeShellArgOnCommandLineOptions($commandLineOptions)
154
    {
155 1
        if (!ctype_print($commandLineOptions)) {
156
            throw new ConversionFailedException(
157
                'Non-printable characters are not allowed in the extra command line options'
158
            );
159
        }
160
161 1
        if (preg_match('#[^a-zA-Z0-9_\s\-]#', $commandLineOptions)) {
162
            throw new ConversionFailedException('The extra command line options contains inacceptable characters');
163
        }
164
165 1
        $cmdOptions = [];
166 1
        $arr = explode(' -', ' ' . $commandLineOptions);
167 1
        foreach ($arr as $cmdOption) {
168 1
            $pos = strpos($cmdOption, ' ');
169 1
            $cName = '';
170 1
            if (!$pos) {
171 1
                $cName = $cmdOption;
172 1
                if ($cName == '') {
173 1
                    continue;
174
                }
175 1
                $cmdOptions[] = '-' . $cName;
176
            } else {
177 1
                $cName = substr($cmdOption, 0, $pos);
178 1
                $cValues = substr($cmdOption, $pos + 1);
179 1
                $cValuesArr = explode(' ', $cValues);
180 1
                foreach ($cValuesArr as &$cArg) {
181 1
                    $cArg = escapeshellarg($cArg);
182
                }
183 1
                $cValues = implode(' ', $cValuesArr);
184 1
                $cmdOptions[] = '-' . $cName . ' ' . $cValues;
185
            }
186
        }
187 1
        return $cmdOptions;
188
    }
189
190
    /**
191
     * Build command line options for a given version of cwebp.
192
     *
193
     * The "-near_lossless" param is not supported on older versions of cwebp, so skip on those.
194
     *
195
     * @param  string $version  Version of cwebp.
196
     * @return string
197
     */
198 6
    private function createCommandLineOptions($version)
199
    {
200
201 6
        $this->logLn('Creating command line options for version: ' . $version);
202
203
        // we only need two decimal places for version.
204
        // convert to number to make it easier to compare
205 6
        $version = preg_match('#^\d+\.\d+#', $version, $matches);
206 6
        $versionNum = 0;
207 6
        if (isset($matches[0])) {
208 6
            $versionNum = floatval($matches[0]);
209
        } else {
210
            $this->logLn(
211
                'Could not extract version number from the following version string: ' . $version,
212
                'bold'
213
            );
214
        }
215
216
        //$this->logLn('version:' . strval($versionNum));
217
218 6
        $options = $this->options;
219
220 6
        $cmdOptions = [];
221
222
        // Metadata (all, exif, icc, xmp or none (default))
223
        // Comma-separated list of existing metadata to copy from input to output
224 6
        if ($versionNum >= 0.3) {
225 6
            $cmdOptions[] = '-metadata ' . $options['metadata'];
226
        }
227
228
        // preset. Appears first in the list as recommended in the docs
229 6
        if (!is_null($options['preset'])) {
230 6
            if ($options['preset'] != 'none') {
231 1
                $cmdOptions[] = '-preset ' . $options['preset'];
232
            }
233
        }
234
235
        // Size
236 6
        $addedSizeOption = false;
237 6
        if (!is_null($options['size-in-percentage'])) {
238 1
            $sizeSource = filesize($this->source);
239 1
            if ($sizeSource !== false) {
240 1
                $targetSize = floor($sizeSource * $options['size-in-percentage'] / 100);
241 1
                $cmdOptions[] = '-size ' . $targetSize;
242 1
                $addedSizeOption = true;
243
            }
244
        }
245
246
        // quality
247 6
        if (!$addedSizeOption) {
248 5
            $cmdOptions[] = '-q ' . $this->getCalculatedQuality();
249
        }
250
251
        // alpha-quality
252 6
        if ($this->options['alpha-quality'] !== 100) {
253 6
            $cmdOptions[] = '-alpha_q ' . escapeshellarg($this->options['alpha-quality']);
254
        }
255
256
        // Losless PNG conversion
257 6
        if ($options['encoding'] == 'lossless') {
258
            // No need to add -lossless when near-lossless is used (on version >= 0.5)
259 4
            if (($options['near-lossless'] === 100) || ($versionNum < 0.5)) {
260 1
                $cmdOptions[] = '-lossless';
261
            }
262
        }
263
264
        // Near-lossles
265 6
        if ($options['near-lossless'] !== 100) {
266 5
            if ($versionNum < 0.5) {
267
                $this->logLn(
268
                    'The near-lossless option is not supported on this (rather old) version of cwebp' .
269
                        '- skipping it.',
270
                    'italic'
271
                );
272
            } else {
273
                // We only let near_lossless have effect when encoding is set to "lossless"
274
                // otherwise encoding=auto would not work as expected
275
276 5
                if ($options['encoding'] == 'lossless') {
277 3
                    $cmdOptions[] ='-near_lossless ' . $options['near-lossless'];
278
                } else {
279 4
                    $this->logLn(
280 4
                        'The near-lossless option ignored for lossy'
281
                    );
282
                }
283
            }
284
        }
285
286 6
        if ($options['auto-filter'] === true) {
287 1
            $cmdOptions[] = '-af';
288
        }
289
290
        // Built-in method option
291 6
        $cmdOptions[] = '-m ' . strval($options['method']);
292
293
        // Built-in low memory option
294 6
        if ($options['low-memory']) {
295 1
            $cmdOptions[] = '-low_memory';
296
        }
297
298
        // command-line-options
299 6
        if ($options['command-line-options']) {
300
            /*
301
            In some years, we can use the splat instead (requires PHP 5.6)
302
            array_push(
303
                $cmdOptions,
304
                ...self::escapeShellArgOnCommandLineOptions($options['command-line-options'])
305
            );
306
            */
307 1
            foreach (self::escapeShellArgOnCommandLineOptions($options['command-line-options']) as $cmdLineOption) {
308 1
                array_push($cmdOptions, $cmdLineOption);
309
            }
310
        }
311
312
        // Source file
313 6
        $cmdOptions[] = escapeshellarg($this->source);
314
315
        // Output
316 6
        $cmdOptions[] = '-o ' . escapeshellarg($this->destination);
317
318
        // Redirect stderr to same place as stdout
319
        // https://www.brianstorti.com/understanding-shell-script-idiom-redirect/
320 6
        $cmdOptions[] = '2>&1';
321
322 6
        $commandOptions = implode(' ', $cmdOptions);
323
        //$this->logLn('command line options:' . $commandOptions);
324
325 6
        return $commandOptions;
326
    }
327
328
    /**
329
     *  Get path for supplied binary for current OS - and validate hash.
330
     *
331
     *  @return  array  Array of supplied binaries (which actually exists, and where hash validates)
332
     */
333 2
    private function getSuppliedBinaryPathForOS()
334
    {
335 2
        $this->log('Checking if we have a supplied binary for OS: ' . PHP_OS . '... ');
336
337
        // Try supplied binary (if available for OS, and hash is correct)
338 2
        $options = $this->options;
339 2
        if (!isset(self::$suppliedBinariesInfo[PHP_OS])) {
340
            $this->logLn('No we dont - not for that OS');
341
            return [];
342
        }
343 2
        $this->logLn('We do.');
344
345 2
        $result = [];
346 2
        $files = self::$suppliedBinariesInfo[PHP_OS];
347 2
        if (count($files) > 0) {
348 2
            $this->logLn('We in fact have ' . count($files));
349
        }
350
351 2
        foreach ($files as $i => list($file, $hash)) {
352
            //$file = $info[0];
353
            //$hash = $info[1];
354
355 2
            $binaryFile = __DIR__ . '/' . $options['rel-path-to-precompiled-binaries'] . '/' . $file;
356
357
            // Replace "/./" with "/" in path (we could alternatively use realpath)
358
            //$binaryFile = preg_replace('#\/\.\/#', '/', $binaryFile);
359
            // The file should exist, but may have been removed manually.
360
            /*
361
            if (!file_exists($binaryFile)) {
362
                $this->logLn('Supplied binary not found! It ought to be here:' . $binaryFile, 'italic');
363
                return false;
364
            }*/
365
366 2
            $realPathResult = realpath($binaryFile);
367 2
            if ($realPathResult === false) {
368
                $this->logLn('Supplied binary not found! It ought to be here:' . $binaryFile, 'italic');
369
                continue;
370
            }
371 2
            $binaryFile = $realPathResult;
372
373
            // File exists, now generate its hash
374
            // hash_file() is normally available, but it is not always
375
            // - https://stackoverflow.com/questions/17382712/php-5-3-20-undefined-function-hash
376
            // If available, validate that hash is correct.
377
378 2
            if (function_exists('hash_file')) {
379 2
                $binaryHash = hash_file('sha256', $binaryFile);
380
381 2
                if ($binaryHash != $hash) {
382
                    $this->logLn(
383
                        'Binary checksum of supplied binary is invalid! ' .
384
                        'Did you transfer with FTP, but not in binary mode? ' .
385
                        'File:' . $binaryFile . '. ' .
386
                        'Expected checksum: ' . $hash . '. ' .
387
                        'Actual checksum:' . $binaryHash . '.',
388
                        'bold'
389
                    );
390
                    continue;
391
                }
392
            }
393 2
            $result[] = $binaryFile;
394
        }
395
396 2
        return $result;
397
    }
398
399 2
    private function discoverBinaries()
400
    {
401 2
        $this->logLn('Locating cwebp binaries');
402
403 2
        if (defined('WEBPCONVERT_CWEBP_PATH')) {
404
            $this->logLn('WEBPCONVERT_CWEBP_PATH was defined, so using that path and ignoring any other');
405
            //$this->logLn('Value: "' . getenv('WEBPCONVERT_CWEBP_PATH') . '"');
406
            return [constant('WEBPCONVERT_CWEBP_PATH')];
407
        }
408 2
        if (!empty(getenv('WEBPCONVERT_CWEBP_PATH'))) {
409
            $this->logLn(
410
                'WEBPCONVERT_CWEBP_PATH environment variable was set, so using that path and ignoring any other'
411
            );
412
            //$this->logLn('Value: "' . getenv('WEBPCONVERT_CWEBP_PATH') . '"');
413
            return [getenv('WEBPCONVERT_CWEBP_PATH')];
414
        }
415
416 2
        $binaries = [];
417 2
        if ($this->options['try-common-system-paths']) {
418 1
            foreach (self::$cwebpDefaultPaths as $binary) {
419 1
                if (@file_exists($binary)) {
420 1
                    $binaries[] = $binary;
421
                }
422
            }
423 1
            if (count($binaries) == 0) {
424 1
                $this->logLn('No cwebp binaries where located in common system locations');
425
            } else {
426
                $this->logLn(strval(count($binaries)) . ' cwebp binaries found in common system locations');
427
            }
428
        }
429
        // TODO: exec('whereis cwebp');
430 2
        if ($this->options['try-supplied-binary-for-os']) {
431 2
            $suppliedBinaries = $this->getSuppliedBinaryPathForOS();
432 2
            foreach ($suppliedBinaries as $suppliedBinary) {
433 2
                $binaries[] = $suppliedBinary;
434
            }
435
        } else {
436
            $this->logLn('Configured not to try the cwebp binary that comes bundled with webp-convert');
437
        }
438
439 2
        if (count($binaries) == 0) {
440
            $this->logLn('No cwebp binaries to try!');
441
        }
442 2
        $this->logLn('A total of ' . strval(count($binaries)) . ' cwebp binaries where found');
443 2
        return $binaries;
444
    }
445
446
    /**
447
     *
448
     * @return  string|int  Version string (ie "1.0.2") OR return code, in case of failure
449
     */
450 2
    private function detectVersion($binary)
451
    {
452
        //$this->logLn('Examining binary: ' . $binary);
453 2
        $command = $binary . ' -version';
454 2
        $this->log('Executing: ' . $command);
455 2
        exec($command, $output, $returnCode);
456
457 2
        if ($returnCode == 0) {
458
            //$this->logLn('Success');
459 2
            if (isset($output[0])) {
460 2
                $this->logLn('. Result: version: ' . $output[0]);
461 2
                return $output[0];
462
            }
463
        } else {
464
            $this->logExecOutput($output);
465
            $this->logLn('');
466
            if ($returnCode == 127) {
467
                $this->logLn('Exec failed (the cwebp binary was not found at path: ' . $binary. ')');
468
            } else {
469
                $this->logLn(
470
                    'Exec failed (return code: ' . $returnCode . ')'
471
                );
472
                if ($returnCode == 126) {
473
                    $this->logLn(
474
                        'PS: Return code 126 means "Permission denied". The user that the command was run with does ' .
475
                            'not have permission to execute that binary.'
476
                    );
477
                    // TODO: further info: shell_exec('whoami')
478
                }
479
            }
480
            return $returnCode;
481
        }
482
    }
483
484
    /**
485
     *  Check versions for binaries, and return array (indexed by the binary, value being the version of the binary).
486
     *
487
     *  @return  array
488
     */
489 2
    private function detectVersions($binaries)
490
    {
491 2
        $binariesWithVersions = [];
492 2
        $binariesWithFailCodes = [];
493
494 2
        $this->logLn(
495 2
            'Detecting versions of the cwebp binaries found (and verifying that they can be executed in the process)'
496
        );
497 2
        foreach ($binaries as $binary) {
498 2
            $versionStringOrFailCode = $this->detectVersion($binary);
499
        //    $this->logLn($binary . ': ' . $versionString);
500 2
            if (gettype($versionStringOrFailCode) == 'string') {
501 2
                $binariesWithVersions[$binary] = $versionStringOrFailCode;
502
            } else {
503 2
                $binariesWithFailCodes[$binary] = $versionStringOrFailCode;
504
            }
505
        }
506 2
        return ['detected' => $binariesWithVersions, 'failed' => $binariesWithFailCodes];
507
    }
508
509
    /**
510
     * @return  boolean  success or not.
511
     */
512 2
    private function tryBinary($binary, $version, $useNice)
513
    {
514
515
        //$this->logLn('Trying binary: ' . $binary);
516 2
        $commandOptions = $this->createCommandLineOptions($version);
517
518 2
        $returnCode = $this->executeBinary($binary, $commandOptions, $useNice);
519 2
        if ($returnCode == 0) {
520
            // It has happened that even with return code 0, there was no file at destination.
521 2
            if (!file_exists($this->destination)) {
522
                $this->logLn('executing cweb returned success code - but no file was found at destination!');
523
                return false;
524
            } else {
525 2
                $this->logLn('Success');
526 2
                return true;
527
            }
528
        } else {
529
            $this->logLn(
530
                'Exec failed (return code: ' . $returnCode . ')'
531
            );
532
            return false;
533
        }
534
    }
535
536 2
    protected function doActualConvert()
537
    {
538 2
        $binaries = $this->discoverBinaries();
539
540 2
        if (count($binaries) == 0) {
541
            throw new SystemRequirementsNotMetException(
542
                'No cwebp binaries located. Check the conversion log for details.'
543
            );
544
        }
545
546 2
        $versions = $this->detectVersions($binaries);
547 2
        if (count($versions['detected']) == 0) {
548
            //$this->logLn('None of the cwebp files located can be executed.');
549
            if (count($binaries) == 1) {
550
                $errorMsg = 'The cwebp file found cannot be can be executed.';
551
            } else {
552
                $errorMsg = 'None of the cwebp files located can be executed.';
553
            }
554
            $uniqueFailCodes = array_unique(array_values($versions['failed']));
555
            if (count($uniqueFailCodes) == 1) {
556
                $errorMsg .= ' ' . (count($binaries) == 1 ? 'It' : 'All') .
557
                    ' failed with return code ' . $uniqueFailCodes[0];
558
                if ($uniqueFailCodes[0] == 126) {
559
                    $errorMsg .= ' (permission denied)';
560
                }
561
            } else {
562
                $errorMsg .= ' Failure codes : ' . implode(', ', $uniqueFailCodes);
563
            }
564
565
            throw new SystemRequirementsNotMetException($errorMsg);
566
        }
567
568 2
        $binaryVersions = $versions['detected'];
569
570 2
        if (count($binaries) > 1) {
571 2
            $this->logLn(
572 2
                'Trying executing the cwebs found until success. Starting with the ones with highest version number.'
573
            );
574
        }
575
        //$this->logLn('binary versions: ' . print_r($binaryVersions, true));
576
577
        // Sort binaries so those with highest numbers comes first
578 2
        arsort($binaryVersions);
579
580
        //$this->logLn('binary versions (ordered by version): ' . print_r($binaryVersions, true));
581
582 2
        $useNice = (($this->options['use-nice']) && self::hasNiceSupport());
583
584 2
        $success = false;
585 2
        foreach ($binaryVersions as $binary => $version) {
586 2
            if ($this->tryBinary($binary, $version, $useNice)) {
587 2
                $success = true;
588 2
                break;
589
            }
590
        }
591
592
        // cwebp sets file permissions to 664 but instead ..
593
        // .. $destination's parent folder's permissions should be used (except executable bits)
594
        // (or perhaps the current umask instead? https://www.php.net/umask)
595
596 2
        if ($success) {
597 2
            $destinationParent = dirname($this->destination);
598 2
            $fileStatistics = stat($destinationParent);
599 2
            if ($fileStatistics !== false) {
600
                // Apply same permissions as parent folder but strip off the executable bits
601 2
                $permissions = $fileStatistics['mode'] & 0000666;
602 2
                chmod($this->destination, $permissions);
603
            }
604
        } else {
605
            throw new SystemRequirementsNotMetException('Failed converting. Check the conversion log for details.');
606
        }
607 2
    }
608
}
609