PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/CliMulti/CliPhp.php

https://github.com/CodeYellowBV/piwik
PHP | 99 lines | 71 code | 18 blank | 10 comment | 19 complexity | d88fa67e0861857ddc63d0587be78e8f MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. namespace Piwik\CliMulti;
  9. use Piwik\CliMulti;
  10. use Piwik\Common;
  11. use Piwik\Plugins\Installation\SystemCheck;
  12. class CliPhp
  13. {
  14. public function findPhpBinary()
  15. {
  16. if (defined('PHP_BINARY')) {
  17. if($this->isValidPhpType(PHP_BINARY)) {
  18. return PHP_BINARY;
  19. }
  20. if($this->isHhvmBinary(PHP_BINARY)) {
  21. return PHP_BINARY . ' --php';
  22. }
  23. }
  24. $bin = '';
  25. if (!empty($_SERVER['_']) && Common::isPhpCliMode()) {
  26. $bin = $this->getPhpCommandIfValid($_SERVER['_']);
  27. }
  28. if (empty($bin) && !empty($_SERVER['argv'][0]) && Common::isPhpCliMode()) {
  29. $bin = $this->getPhpCommandIfValid($_SERVER['argv'][0]);
  30. }
  31. if (!$this->isValidPhpType($bin)) {
  32. $bin = shell_exec('which php');
  33. }
  34. if (!$this->isValidPhpType($bin)) {
  35. $bin = shell_exec('which php5');
  36. }
  37. if (!$this->isValidPhpType($bin)) {
  38. return false;
  39. }
  40. $bin = trim($bin);
  41. if (!$this->isValidPhpVersion($bin)) {
  42. return false;
  43. }
  44. return $bin;
  45. }
  46. private function isHhvmBinary($bin)
  47. {
  48. return false !== strpos($bin, 'hhvm');
  49. }
  50. private function isValidPhpVersion($bin)
  51. {
  52. $cliVersion = $this->getPhpVersion($bin);
  53. $isCliVersionValid = SystemCheck::isPhpVersionValid($cliVersion);
  54. return $isCliVersionValid;
  55. }
  56. private function isValidPhpType($path)
  57. {
  58. return !empty($path)
  59. && false === strpos($path, 'fpm')
  60. && false === strpos($path, 'cgi')
  61. && false === strpos($path, 'phpunit');
  62. }
  63. private function getPhpCommandIfValid($path)
  64. {
  65. if (!empty($path) && is_executable($path)) {
  66. if (0 === strpos($path, PHP_BINDIR) && $this->isValidPhpType($path)) {
  67. return $path;
  68. }
  69. }
  70. }
  71. /**
  72. * @param $bin PHP binary
  73. * @return string
  74. */
  75. private function getPhpVersion($bin)
  76. {
  77. $command = sprintf("%s -r 'echo phpversion();'", $bin);
  78. $version = shell_exec($command);
  79. return $version;
  80. }
  81. }