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