|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use WebPConvert\Helpers\FileExists; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Discover multiple paths of a binary |
|
9
|
|
|
* |
|
10
|
|
|
* @package WebPConvert |
|
11
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
12
|
|
|
* @since Class available since Release 2.3.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class BinaryDiscovery |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
// Common system paths |
|
18
|
|
|
private static $commonSystemPaths = [ |
|
19
|
|
|
'/usr/bin', |
|
20
|
|
|
'/usr/local/bin', |
|
21
|
|
|
'/usr/gnu/bin', |
|
22
|
|
|
'/usr/syno/bin' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Discover binaries by looking in common system paths. |
|
27
|
|
|
* |
|
28
|
|
|
* We try a small set of common system paths, such as "/usr/bin". |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $binary the binary to look for (ie "cwebp") |
|
31
|
|
|
* |
|
32
|
|
|
* @return array binaries found in common system locations |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public static function discoverInCommonSystemPaths($binary) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$binaries = []; |
|
37
|
1 |
|
foreach (self::$commonSystemPaths as $dir) { |
|
38
|
|
|
// PS: FileExists might throw if exec() is unavailable. We let it. |
|
39
|
|
|
// - this class assumes exec is available |
|
40
|
1 |
|
if (FileExists::fileExistsTryHarder($dir . '/' . $binary)) { |
|
41
|
1 |
|
$binaries[] = $dir . '/' . $binary; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
1 |
|
return $binaries; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Discover installed binaries using ie "whereis -b cwebp" |
|
49
|
|
|
* |
|
50
|
|
|
* @return array Array of cwebp paths discovered (possibly empty) |
|
51
|
|
|
*/ |
|
52
|
1 |
|
private static function discoverBinariesUsingWhereIs($binary) |
|
53
|
|
|
{ |
|
54
|
|
|
// This method was added due to #226. |
|
55
|
1 |
|
exec('whereis -b ' . $binary, $output, $returnCode); |
|
56
|
1 |
|
if (($returnCode == 0) && (isset($output[0]))) { |
|
57
|
1 |
|
$result = $output[0]; |
|
58
|
|
|
// Ie: "cwebp: /usr/bin/cwebp /usr/local/bin/cwebp" |
|
59
|
1 |
|
if (preg_match('#^' . $binary . ':\s(.*)$#', $result, $matches)) { |
|
60
|
|
|
return explode(' ', $matches[1]); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
1 |
|
return []; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Discover installed binaries using "which -a cwebp" |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $binary the binary to look for (ie "cwebp") |
|
70
|
|
|
* |
|
71
|
|
|
* @return array Array of paths discovered (possibly empty) |
|
72
|
|
|
*/ |
|
73
|
1 |
|
private static function discoverBinariesUsingWhich($binary) |
|
74
|
|
|
{ |
|
75
|
|
|
// As suggested by @cantoute here: |
|
76
|
|
|
// https://wordpress.org/support/topic/sh-1-usr-local-bin-cwebp-not-found/ |
|
77
|
1 |
|
exec('which -a ' . $binary, $output, $returnCode); |
|
78
|
1 |
|
if ($returnCode == 0) { |
|
79
|
|
|
return $output; |
|
80
|
|
|
} |
|
81
|
1 |
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Discover binaries using "which -a" or, if that fails "whereis -b" |
|
86
|
|
|
* |
|
87
|
|
|
* These commands only searces within $PATH. So it only finds installed binaries (which is good, |
|
88
|
|
|
* as it would be unsafe to deal with binaries found scattered around) |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $binary the binary to look for (ie "cwebp") |
|
91
|
|
|
* |
|
92
|
|
|
* @return array binaries found in common system locations |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public static function discoverInstalledBinaries($binary) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$paths = self::discoverBinariesUsingWhich($binary); |
|
97
|
1 |
|
if (count($paths) > 0) { |
|
98
|
|
|
return $paths; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$paths = self::discoverBinariesUsingWhereIs($binary); |
|
102
|
1 |
|
if (count($paths) > 0) { |
|
103
|
|
|
return $paths; |
|
104
|
|
|
} |
|
105
|
1 |
|
return []; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|