PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/public/external/pydio/core/tests/test.PHPCLI.php

https://github.com/costinu/cms
PHP | 135 lines | 93 code | 13 blank | 29 comment | 28 complexity | 15427239db748835145aa2d4f385e67b MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /*
  3. * Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
  4. * This file is part of Pydio.
  5. *
  6. * Pydio is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Pydio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Pydio. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * The latest code can be found at <http://pyd.io/>.
  20. */
  21. defined('AJXP_EXEC') or die( 'Access not allowed');
  22. require_once('../classes/class.AbstractTest.php');
  23. /**
  24. * Check current PHP Version
  25. * @package AjaXplorer
  26. * @subpackage Tests
  27. */
  28. class PHPCLI extends AbstractTest
  29. {
  30. public function PHPCLI() { parent::AbstractTest("PHP Command Line", "Testing PHP command line (default is php)"); }
  31. public function doTest()
  32. {
  33. if (!is_writable(AJXP_CACHE_DIR)) {
  34. $this->testedParams["Command Line Available"] = "No";
  35. $this->failedLevel = "warning";
  36. $this->failedInfo = "Php command line not detected (cache directory not writeable), this is NOT BLOCKING, but enabling it could allow to send some long tasks in background. If you do not have the ability to tweak your server, you can safely ignore this warning.";
  37. return FALSE;
  38. }
  39. $windows = (PHP_OS == "WIN32" || PHP_OS == "WINNT" || PHP_OS == "Windows");
  40. $sModeExecDir = ini_get("safe_mode_exec_dir") ;
  41. $safeEnabled = ini_get("safe_mode") || !empty($sModeExecDir);
  42. $disabled_functions=explode(',',ini_get('disable_functions'));
  43. $fName = ($windows ? "popen" : "exec");
  44. $notFoundFunction = in_array($fName, $disabled_functions) || !function_exists($fName) || !is_callable($fName);
  45. $comEnabled = class_exists("COM");
  46. $useCOM = false;
  47. if ( ( $safeEnabled || $notFoundFunction )) {
  48. if ($comEnabled) {
  49. $useCOM = true;
  50. } else {
  51. $this->testedParams["Command Line Available"] = "No";
  52. $this->failedLevel = "warning";
  53. $this->failedInfo = "Php command line not detected (there seem to be some safe_mode or a-like restriction), this is NOT BLOCKING, but enabling it could allow to send some long tasks in background. If you do not have the ability to tweak your server, you can safely ignore this warning.";
  54. return FALSE;
  55. }
  56. }
  57. $defaultCli = ConfService::getCoreConf("CLI_PHP");
  58. if($defaultCli == null) $defaultCli = "php";
  59. $token = md5(time());
  60. $robustCacheDir = str_replace("/", DIRECTORY_SEPARATOR, AJXP_CACHE_DIR);
  61. $logDir = $robustCacheDir.DIRECTORY_SEPARATOR."cmd_outputs";
  62. if(!is_dir($logDir)) mkdir($logDir, 0755);
  63. $logFile = $logDir."/".$token.".out";
  64. $testScript = AJXP_CACHE_DIR."/cli_test.php";
  65. file_put_contents($testScript, "<?php file_put_contents('".$robustCacheDir.DIRECTORY_SEPARATOR."cli_result.php', 'cli'); ?>");
  66. $cmd = $defaultCli." ". $robustCacheDir .DIRECTORY_SEPARATOR."cli_test.php";
  67. if ($windows) {
  68. /* Next 2 lines modified by rmeske: Need to wrap the folder and file paths in double quotes. */
  69. $cmd = $defaultCli." ". chr(34).$robustCacheDir .DIRECTORY_SEPARATOR."cli_test.php".chr(34);
  70. $cmd .= " > ".chr(34).$logFile.chr(34);
  71. $comCommand = $cmd;
  72. if ($useCOM) {
  73. $WshShell = new COM("WScript.Shell");
  74. $res = $WshShell->Run("cmd /C $comCommand", 0, false);
  75. } else {
  76. $tmpBat = implode(DIRECTORY_SEPARATOR, array(str_replace("/", DIRECTORY_SEPARATOR, AJXP_INSTALL_PATH), "data","tmp", md5(time()).".bat"));
  77. $cmd .= "\n DEL ".chr(34).$tmpBat.chr(34);
  78. file_put_contents($tmpBat, $cmd);
  79. /* Following 1 line modified by rmeske: The windows Start command identifies the first parameter in quotes as a title for the window. Therefore, when enclosing a command with double quotes you must include a window title first
  80. START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME] [/WAIT] [/B] [command / program] [parameters]
  81. */
  82. @pclose(@popen('start /b "CLI" "'.$tmpBat.'"', 'r'));
  83. sleep(1);
  84. // Failed, but we can try with COM
  85. if ( ! is_file(AJXP_CACHE_DIR."/cli_result.php") && $comEnabled ) {
  86. $useCOM = true;
  87. $WshShell = new COM("WScript.Shell");
  88. $res = $WshShell->Run("cmd /C $comCommand", 0, false);
  89. }
  90. }
  91. } else {
  92. new UnixProcess($cmd, $logFile);
  93. }
  94. sleep(1);
  95. $availability = true;
  96. if (is_file(AJXP_CACHE_DIR."/cli_result.php")) {
  97. $this->testedParams["Command Line Available"] = "Yes";
  98. unlink(AJXP_CACHE_DIR."/cli_result.php");
  99. if ($useCOM) {
  100. $this->failedLevel = "warning";
  101. $availability = true;
  102. $this->failedInfo = "Php command line detected, but using the windows COM extension. Just make sure to <b>enable COM</b> in the Pydio Core Options";
  103. } else {
  104. $this->failedInfo = "Php command line detected, this will allow to send some tasks in background. Enable it in the Pydio Core Options";
  105. }
  106. } else {
  107. if (is_file($logFile)) {
  108. $log = file_get_contents($logFile);
  109. unlink($logFile);
  110. }
  111. $this->testedParams["Command Line Available"] = "No : $log";
  112. $this->failedLevel = "warning";
  113. $this->failedInfo = "Php command line not detected, this is NOT BLOCKING, but enabling it could allow to send some long tasks in background. If you do not have the ability to tweak your server, you can safely ignore this warning.";
  114. if ($windows) {
  115. $this->failedInfo .= "<br> On Windows, try to activate the php COM extension, and set correct rights to the cmd exectuble to make it runnable by the web server, this should solve the problem.";
  116. }
  117. $availability = false;
  118. }
  119. unlink(AJXP_CACHE_DIR."/cli_test.php");
  120. return $availability;
  121. }
  122. };