CliPhp.php
5 years ago
Output.php
5 years ago
Process.php
5 years ago
RequestCommand.php
5 years ago
RequestParser.php
5 years ago
CliPhp.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | namespace Piwik\CliMulti; |
| 9 | |
| 10 | use Piwik\Common; |
| 11 | use Piwik\Config; |
| 12 | |
| 13 | class CliPhp |
| 14 | { |
| 15 | |
| 16 | public function findPhpBinary() |
| 17 | { |
| 18 | $general = Config::getInstance()->General; |
| 19 | if (!empty($general['php_binary_path']) && file_exists($general['php_binary_path'])) { |
| 20 | return $general['php_binary_path']; |
| 21 | } |
| 22 | |
| 23 | if (defined('PHP_BINARY')) { |
| 24 | if ($this->isHhvmBinary(PHP_BINARY)) { |
| 25 | return PHP_BINARY . ' --php'; |
| 26 | } |
| 27 | |
| 28 | if ($this->isValidPhpType(PHP_BINARY)) { |
| 29 | return PHP_BINARY . ' -q'; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | $bin = ''; |
| 34 | |
| 35 | if (!empty($_SERVER['_']) && Common::isPhpCliMode()) { |
| 36 | $bin = $this->getPhpCommandIfValid($_SERVER['_']); |
| 37 | } |
| 38 | |
| 39 | if (empty($bin) && !empty($_SERVER['argv'][0]) && Common::isPhpCliMode()) { |
| 40 | $bin = $this->getPhpCommandIfValid($_SERVER['argv'][0]); |
| 41 | } |
| 42 | |
| 43 | if (empty($bin)) { |
| 44 | $possiblePhpPath = PHP_BINDIR . ('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php'); |
| 45 | $bin = $this->getPhpCommandIfValid($possiblePhpPath); |
| 46 | } |
| 47 | |
| 48 | if (!$this->isValidPhpType($bin) && function_exists('shell_exec')) { |
| 49 | $bin = @shell_exec('which php'); |
| 50 | } |
| 51 | |
| 52 | if (!$this->isValidPhpType($bin)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | $bin = trim($bin); |
| 57 | |
| 58 | if (!$this->isValidPhpVersion($bin)) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | $bin .= ' -q'; |
| 63 | |
| 64 | return $bin; |
| 65 | } |
| 66 | |
| 67 | private function isHhvmBinary($bin) |
| 68 | { |
| 69 | return false !== strpos($bin, 'hhvm'); |
| 70 | } |
| 71 | |
| 72 | private function isValidPhpVersion($bin) |
| 73 | { |
| 74 | global $piwik_minimumPHPVersion; |
| 75 | $cliVersion = $this->getPhpVersion($bin); |
| 76 | $isCliVersionValid = $cliVersion && version_compare($piwik_minimumPHPVersion, $cliVersion) <= 0; |
| 77 | return $isCliVersionValid; |
| 78 | } |
| 79 | |
| 80 | private function isValidPhpType($path) |
| 81 | { |
| 82 | if (empty($path)) { |
| 83 | return false; |
| 84 | } |
| 85 | $path = basename($path); |
| 86 | |
| 87 | return false === strpos($path, 'fpm') |
| 88 | && false === strpos($path, 'cgi') |
| 89 | && false === strpos($path, 'phpunit') |
| 90 | && false === strpos($path, 'lsphp'); |
| 91 | } |
| 92 | |
| 93 | private function getPhpCommandIfValid($path) |
| 94 | { |
| 95 | if (!empty($path) && @is_executable($path)) { |
| 96 | if (0 === strpos($path, PHP_BINDIR) && $this->isValidPhpType($path)) { |
| 97 | return $path; |
| 98 | } |
| 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param string $bin PHP binary |
| 105 | * @return string |
| 106 | */ |
| 107 | private function getPhpVersion($bin) |
| 108 | { |
| 109 | $command = sprintf("%s -r 'echo phpversion();'", $bin); |
| 110 | $version = null; |
| 111 | if (function_exists('shell_exec')) { |
| 112 | $version = @shell_exec($command); |
| 113 | } |
| 114 | return $version; |
| 115 | } |
| 116 | } |
| 117 |