PageRenderTime 265ms CodeModel.GetById 101ms app.highlight 8ms RepoModel.GetById 153ms 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
  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 */
 21defined('AJXP_EXEC') or die( 'Access not allowed');
 22require_once('../classes/class.AbstractTest.php');
 23
 24/**
 25 * Check current PHP Version
 26 * @package AjaXplorer
 27 * @subpackage Tests
 28 */
 29class PHPCLI extends AbstractTest
 30{
 31    public function PHPCLI() { parent::AbstractTest("PHP Command Line", "Testing PHP command line (default is php)"); }
 32    public function doTest()
 33    {
 34        if (!is_writable(AJXP_CACHE_DIR)) {
 35            $this->testedParams["Command Line Available"] = "No";
 36            $this->failedLevel = "warning";
 37            $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.";
 38            return FALSE;
 39        }
 40        $windows = (PHP_OS == "WIN32" || PHP_OS == "WINNT" || PHP_OS == "Windows");
 41
 42        $sModeExecDir = ini_get("safe_mode_exec_dir") ;
 43        $safeEnabled = ini_get("safe_mode") || !empty($sModeExecDir);
 44
 45        $disabled_functions=explode(',',ini_get('disable_functions'));
 46        $fName = ($windows ? "popen" : "exec");
 47        $notFoundFunction = in_array($fName, $disabled_functions) || !function_exists($fName) || !is_callable($fName);
 48
 49        $comEnabled = class_exists("COM");
 50        $useCOM = false;
 51
 52        if ( ( $safeEnabled ||  $notFoundFunction )) {
 53            if ($comEnabled) {
 54                $useCOM = true;
 55            } else {
 56                $this->testedParams["Command Line Available"] = "No";
 57                $this->failedLevel = "warning";
 58                $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.";
 59                return FALSE;
 60            }
 61        }
 62
 63        $defaultCli = ConfService::getCoreConf("CLI_PHP");
 64        if($defaultCli == null) $defaultCli = "php";
 65
 66        $token = md5(time());
 67        $robustCacheDir = str_replace("/", DIRECTORY_SEPARATOR, AJXP_CACHE_DIR);
 68        $logDir = $robustCacheDir.DIRECTORY_SEPARATOR."cmd_outputs";
 69        if(!is_dir($logDir)) mkdir($logDir, 0755);
 70        $logFile = $logDir."/".$token.".out";
 71
 72        $testScript = AJXP_CACHE_DIR."/cli_test.php";
 73        file_put_contents($testScript, "<?php file_put_contents('".$robustCacheDir.DIRECTORY_SEPARATOR."cli_result.php', 'cli'); ?>");
 74
 75        $cmd = $defaultCli." ". $robustCacheDir .DIRECTORY_SEPARATOR."cli_test.php";
 76
 77        if ($windows) {
 78            /* Next 2 lines modified by rmeske: Need to wrap the folder and file paths in double quotes.  */
 79            $cmd = $defaultCli." ". chr(34).$robustCacheDir .DIRECTORY_SEPARATOR."cli_test.php".chr(34);
 80            $cmd .= " > ".chr(34).$logFile.chr(34);
 81
 82            $comCommand = $cmd;
 83            if ($useCOM) {
 84                $WshShell   = new COM("WScript.Shell");
 85                $res = $WshShell->Run("cmd /C $comCommand", 0, false);
 86            } else {
 87                $tmpBat = implode(DIRECTORY_SEPARATOR, array(str_replace("/", DIRECTORY_SEPARATOR, AJXP_INSTALL_PATH), "data","tmp", md5(time()).".bat"));
 88                $cmd .= "\n DEL ".chr(34).$tmpBat.chr(34);
 89                file_put_contents($tmpBat, $cmd);
 90                /* 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
 91                START	["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME] [/WAIT] [/B] [command / program] [parameters]
 92                */
 93                @pclose(@popen('start /b "CLI" "'.$tmpBat.'"', 'r'));
 94                sleep(1);
 95                // Failed, but we can try with COM
 96                if ( ! is_file(AJXP_CACHE_DIR."/cli_result.php") && $comEnabled ) {
 97                    $useCOM = true;
 98                    $WshShell   = new COM("WScript.Shell");
 99                    $res = $WshShell->Run("cmd /C $comCommand", 0, false);
100                }
101            }
102        } else {
103            new UnixProcess($cmd, $logFile);
104        }
105
106        sleep(1);
107        $availability = true;
108        if (is_file(AJXP_CACHE_DIR."/cli_result.php")) {
109            $this->testedParams["Command Line Available"] = "Yes";
110            unlink(AJXP_CACHE_DIR."/cli_result.php");
111            if ($useCOM) {
112                $this->failedLevel = "warning";
113                $availability = true;
114                $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";
115            } else {
116                $this->failedInfo = "Php command line detected, this will allow to send some tasks in background. Enable it in the Pydio Core Options";
117            }
118        } else {
119            if (is_file($logFile)) {
120                $log = file_get_contents($logFile);
121                unlink($logFile);
122            }
123            $this->testedParams["Command Line Available"] = "No : $log";
124            $this->failedLevel = "warning";
125            $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.";
126            if ($windows) {
127                $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.";
128            }
129            $availability = false;
130        }
131        unlink(AJXP_CACHE_DIR."/cli_test.php");
132
133        return $availability;
134    }
135};