Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CliPhp.php « CliMulti « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b1661510e46169204e86cc607028b360f3b5a18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\CliMulti;

use Piwik\Common;

class CliPhp
{

    public function findPhpBinary()
    {
        if (defined('PHP_BINARY')) {
            if ($this->isHhvmBinary(PHP_BINARY)) {
                return PHP_BINARY . ' --php';
            }

            if ($this->isValidPhpType(PHP_BINARY)) {
                return PHP_BINARY . ' -q';
            }
        }

        $bin = '';

        if (!empty($_SERVER['_']) && Common::isPhpCliMode()) {
            $bin = $this->getPhpCommandIfValid($_SERVER['_']);
        }

        if (empty($bin) && !empty($_SERVER['argv'][0]) && Common::isPhpCliMode()) {
            $bin = $this->getPhpCommandIfValid($_SERVER['argv'][0]);
        }

        if (empty($bin)) {
            $possiblePhpPath = PHP_BINDIR . ('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php');
            $bin = $this->getPhpCommandIfValid($possiblePhpPath);
        }

        if (!$this->isValidPhpType($bin)) {
            $bin = shell_exec('which php');
        }

        if (!$this->isValidPhpType($bin)) {
            $bin = shell_exec('which php5');
        }

        if (!$this->isValidPhpType($bin)) {
            return false;
        }

        $bin = trim($bin);

        if (!$this->isValidPhpVersion($bin)) {
            return false;
        }

        $bin .= ' -q';

        return $bin;
    }

    private function isHhvmBinary($bin)
    {
        return false !== strpos($bin, 'hhvm');
    }

    private function isValidPhpVersion($bin)
    {
        global $piwik_minimumPHPVersion;
        $cliVersion = $this->getPhpVersion($bin);
        $isCliVersionValid = version_compare($piwik_minimumPHPVersion, $cliVersion) <= 0;
        return $isCliVersionValid;
    }

    private function isValidPhpType($path)
    {
        return !empty($path)
        && false === strpos($path, 'fpm')
        && false === strpos($path, 'cgi')
        && false === strpos($path, 'phpunit')
        && false === strpos($path, 'lsphp');
    }

    private function getPhpCommandIfValid($path)
    {
        if (!empty($path) && @is_executable($path)) {
            if (0 === strpos($path, PHP_BINDIR) && $this->isValidPhpType($path)) {
                return $path;
            }
        }
        return null;
    }

    /**
     * @param string $bin PHP binary
     * @return string
     */
    private function getPhpVersion($bin)
    {
        $command = sprintf("%s -r 'echo phpversion();'", $bin);
        $version = shell_exec($command);
        return $version;
    }
}