/** * activates the script function. * * @return void * @access public */ function activate() { parent::activate(); if ($_SERVER['argv']['1'] == '-s' && SensitiveIO::isPositiveInteger($_SERVER['argv']['2'])) { // SUB-SCRIPT : Processes one script task @ini_set('max_execution_time', SUB_SCRIPT_TIME_OUT); //set max execution time for sub script @set_time_limit(SUB_SCRIPT_TIME_OUT); //set the PHP timeout for sub script $sql = "\n\t\t\t\tselect\n\t\t\t\t\t*\n\t\t\t\tfrom\n\t\t\t\t\tregenerator\n\t\t\t\twhere\n\t\t\t\t\tid_reg = '" . $_SERVER['argv']['2'] . "'\n\t\t\t"; $q = new CMS_query($sql); if ($q->getNumRows()) { $data = $q->getArray(); //send script informations to process manager $this->_processManager->setParameters($data['module_reg'], $data['parameters_reg']); //instanciate script module $module = CMS_modulesCatalog::getByCodename($data['module_reg']); //then send script task to module (return task title by reference) $task = $module->scriptTask(unserialize($data['parameters_reg'])); //delete the current script task $sql_delete = "\n\t\t\t\t\tdelete\n\t\t\t\t\tfrom\n\t\t\t\t\t\tregenerator\n\t\t\t\t\twhere\n\t\t\t\t\t\tid_reg='" . $data['id_reg'] . "'"; $q = new CMS_query($sql_delete); if ($this->_debug) { $this->raiseError($this->_processManager->getPIDFilePath() . " : task " . $_SERVER['argv']['2'] . " seems " . (!$task ? 'NOT ' : '') . "done !"); $this->raiseError($this->_processManager->getPIDFilePath() . " : PID file exists ? " . @file_exists($this->_processManager->getPIDFilePath())); } $fpath = $this->_processManager->getPIDFilePath() . '.ok'; if (@touch($fpath) && @chmod($fpath, octdec(FILES_CHMOD))) { $f = @fopen($fpath, 'a'); if (!@fwrite($f, 'Script OK')) { $this->raiseError($this->_processManager->getPIDFilePath() . " : Can't write into file: " . $fpath); } @fclose($f); } else { $this->raiseError($this->_processManager->getPIDFilePath() . " : Can't create file: " . $fpath); } } } else { // MASTER SCRIPT : Processes all sub-scripts @ini_set('max_execution_time', MASTER_SCRIPT_TIME_OUT); //set max execution time for master script @set_time_limit(MASTER_SCRIPT_TIME_OUT); //set the PHP timeout for master script //max simultaneous scripts $maxScripts = $_SERVER['argv']['2']; $scriptsArray = array(); //send script informations to process manager $this->_processManager->setParameters(processManager::MASTER_SCRIPT_NAME, ''); //the sql script which selects one script task at a time $sql_select = "\n\t\t\t\tselect\n\t\t\t\t\t*\n\t\t\t\tfrom\n\t\t\t\t\tregenerator\n\t\t\t\tlimit\n\t\t\t\t\t" . $maxScripts . "\n\t\t\t"; //and now, launch all sub-scripts until table is empty. while (true) { //get scripts $q = new CMS_query($sql_select); if ($q->getNumRows()) { while (count($scriptsArray) < $maxScripts && ($data = $q->getArray())) { // Launch sub-process if (!APPLICATION_IS_WINDOWS) { // On unix system $sub_system = PATH_PACKAGES_FS . "/scripts/script.php -s " . $data["id_reg"] . " > /dev/null 2>&1 &"; if (!defined('PATH_PHP_CLI_UNIX') || !PATH_PHP_CLI_UNIX) { CMS_patch::executeCommand("cd " . PATH_REALROOT_FS . "; php " . $sub_system, $error); if ($error) { CMS_grandFather::raiseError('Error during execution of sub script command (cd ' . PATH_REALROOT_FS . '; php ' . $sub_system . '), please check your configuration : ' . $error); return false; } } else { CMS_patch::executeCommand("cd " . PATH_REALROOT_FS . "; " . PATH_PHP_CLI_UNIX . " " . $sub_system, $error); if ($error) { CMS_grandFather::raiseError('Error during execution of sub script command (cd ' . PATH_REALROOT_FS . '; ' . PATH_PHP_CLI_UNIX . ' ' . $sub_system . '), please check your configuration : ' . $error); return false; } } $PIDfile = $this->_processManager->getTempPath() . "/" . SCRIPT_CODENAME . "_" . $data["id_reg"]; if ($this->_debug) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " : Executes system(" . $sub_system . ")"); } //sleep a little @sleep(SLEEP_TIME); } else { // On windows system //Create the BAT file $command = '@echo off' . "\r\n" . '@start /B /BELOWNORMAL ' . realpath(PATH_PHP_CLI_WINDOWS) . ' ' . realpath(PATH_PACKAGES_FS . '\\scripts\\script.php') . ' -s ' . $data["id_reg"]; if (!@touch(realpath(PATH_WINDOWS_BIN_FS) . DIRECTORY_SEPARATOR . "sub_script.bat")) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " : Create file error : sub_script.bat"); } $replace = array('program files (x86)' => 'progra~2', 'program files' => 'progra~1', 'documents and settings' => 'docume~1'); $command = str_ireplace(array_keys($replace), $replace, $command); $fh = fopen(realpath(PATH_WINDOWS_BIN_FS . DIRECTORY_SEPARATOR . "sub_script.bat"), "wb"); if (is_resource($fh)) { if (!fwrite($fh, $command, io::strlen($command))) { CMS_grandFather::raiseError(processManager::MASTER_SCRIPT_NAME . " : Save file error : sub_script.bat"); } fclose($fh); } $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run(str_ireplace(array_keys($replace), $replace, realpath(PATH_WINDOWS_BIN_FS . '\\sub_script.bat')), 0, false); $PIDfile = $this->_processManager->getTempPath() . DIRECTORY_SEPARATOR . SCRIPT_CODENAME . "_" . $data["id_reg"]; //sleep a little @sleep(SLEEP_TIME); } if ($this->_debug) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " : script : " . $data["id_reg"] . " - sub_system : " . $sub_system); } $scriptsArray[] = array("PID" => $PIDfile, "startTime" => CMS_stats::getmicrotime(), "scriptID" => $data["id_reg"], "scriptDatas" => $data); } } else { // no more scripts to process // > delete all temporary files // > end script if (APPLICATION_IS_WINDOWS) { $files = glob(realpath($this->_processManager->getTempPath()) . DIRECTORY_SEPARATOR . SCRIPT_CODENAME . '*.ok', GLOB_NOSORT); if (is_array($files)) { foreach ($files as $file) { if (!CMS_file::deleteFile($file)) { $this->raiseError("Can't delete file " . $file); return false; } } } } else { $tmpDir = dir($this->_processManager->getTempPath()); while (false !== ($file = $tmpDir->read())) { if (io::strpos($file, SCRIPT_CODENAME) !== false) { @unlink($this->_processManager->getTempPath() . '/' . $file); } } } break; } while (true) { @sleep(SLEEP_TIME); //wait a little to check sub_scripts $break = false; $timeStop = CMS_stats::getmicrotime(); if ($this->_debug) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " Scripts in progress : " . sizeof($scriptsArray)); } foreach ($scriptsArray as $nb => $aScript) { if ($this->_debug) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " PID : " . $aScript["PID"] . " - time : " . ($timeStop - $aScript["startTime"])); } $ok = ''; $ok = is_file($aScript["PID"] . '.ok'); if ($ok) { //$break = true; if ($this->_debug) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " Script : " . $aScript["PID"] . " OK !"); } unset($scriptsArray[$nb]); } elseif ($timeStop - $aScript["startTime"] >= SUB_SCRIPT_TIME_OUT) { if ($this->_debug) { $this->raiseError(processManager::MASTER_SCRIPT_NAME . " : Script : " . $aScript["PID"] . " NOT OK !"); } $this->raiseError(processManager::MASTER_SCRIPT_NAME . ' : Error on task : ' . $aScript["scriptID"] . ' ... skip it. Task parameters : ' . print_r($aScript['scriptDatas'], true)); //$break = true; unset($scriptsArray[$nb]); //delete the script in error from task list $q_del = "\n\t\t\t\t\t\t\t\tdelete\n\t\t\t\t\t\t\t\tfrom\n\t\t\t\t\t\t\t\t\tregenerator\n\t\t\t\t\t\t\t\twhere\n\t\t\t\t\t\t\t\t\tid_reg='" . $aScript["scriptID"] . "'"; $q_del = new CMS_query($q_del); } } if (!$scriptsArray) { break; } } } } }
} else { $content .= '<li class="atm-pic-ok">GD extension OK</li>'; } //XAPIAN if (class_exists('CMS_module_ase')) { $return = CMS_patch::executeCommand('"' . $cliPath . '" -r "echo (function_exists(\'xapian_version_string\') ? xapian_version_string() : (function_exists(\'flint_open\') ? version_string() : \'none\'));"', $error); if ($error || $return == 'none') { $content .= '<li class="atm-pic-cancel">Error, Xapian extension not installed ' . $error . ' ' . $return . '</li>'; } else { $content .= '<li class="atm-pic-ok">Xapian extension OK (' . $return . ')</li>'; } } $content .= ' </ul>'; //Ini file infos $return = CMS_patch::executeCommand('"' . $cliPath . '" --ini', $error); if (!$error && $return) { $content .= '<code>' . str_replace("\n", '<br />', $return) . '</code>'; } $content .= ' </fieldset>'; } //Daily Routine if ($mysqlOk) { $modules = CMS_modulesCatalog::getAll(); $drContent = ''; foreach ($modules as $aModule) { //see if the action was done today $sql = "\n\t\t\tselect\n\t\t\t\t*\n\t\t\tfrom\n\t\t\t\tactionsTimestamps\n\t\t\twhere\n\t\t\t\ttype_at='DAILY_ROUTINE'\n\t\t\t\tand module_at='" . io::sanitizeSQLString($aModule->getCodename()) . "'\n\t\t"; $q = new CMS_query($sql); if ($q->getNumRows()) {
/** * Start the scripts process queue. * Remove the lock file then relaunch the script if force is true * * @param boolean $force Set to true if you wish to remove the lock file before launch * @return void * @access public * @static */ static function startScript($force = false) { if (USE_BACKGROUND_REGENERATOR) { $forceRestart = ''; if ($force) { $forceRestart = ' -F'; } elseif (processManager::hasRunningScript()) { return false; } //test if we're on windows or linux, for the output redirection if (APPLICATION_IS_WINDOWS) { if (realpath(PATH_PHP_CLI_WINDOWS) === false) { CMS_grandFather::raiseError("Unknown CLI location : " . PATH_PHP_CLI_WINDOWS . ", please check your configuration."); return false; } // Create the BAT file $command = '@echo off' . "\r\n" . 'start /B /LOW ' . realpath(PATH_PHP_CLI_WINDOWS) . ' ' . realpath(PATH_PACKAGES_FS . '\\scripts\\script.php') . ' -m ' . REGENERATION_THREADS . $forceRestart; $replace = array('program files (x86)' => 'progra~2', 'program files' => 'progra~1', 'documents and settings' => 'docume~1'); $command = str_ireplace(array_keys($replace), $replace, $command); if (!@touch(PATH_WINDOWS_BIN_FS . "/script.bat")) { CMS_grandFather::_raiseError("CMS_scriptsManager : startScript : Create file error : " . PATH_WINDOWS_BIN_FS . "/script.bat"); return false; } $fh = @fopen(PATH_WINDOWS_BIN_FS . "/script.bat", "wb"); if (is_resource($fh)) { if (!@fwrite($fh, $command, io::strlen($command))) { CMS_grandFather::raiseError("Save file error : script.bat"); } @fclose($fh); } $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run(str_ireplace(array_keys($replace), $replace, realpath(PATH_WINDOWS_BIN_FS . '\\script.bat')), 0, false); } else { $error = ''; if (!defined('PATH_PHP_CLI_UNIX') || !PATH_PHP_CLI_UNIX) { $return = CMS_patch::executeCommand('which php 2>&1', $error); if ($error) { CMS_grandFather::raiseError('Error when finding php CLI with command "which php", please check your configuration : ' . $error); return false; } if (io::substr($return, 0, 1) != '/') { CMS_grandFather::raiseError('Can\'t find php CLI with command "which php", please check your configuration.'); return false; } $return = CMS_patch::executeCommand("cd " . PATH_REALROOT_FS . "; php " . PATH_PACKAGES_FS . "/scripts/script.php -m " . REGENERATION_THREADS . $forceRestart . " > /dev/null 2>&1 &", $error); if ($error) { CMS_grandFather::raiseError('Error during execution of script command (cd ' . PATH_REALROOT_FS . '; php ' . PATH_PACKAGES_FS . '/scripts/script.php -m ' . REGENERATION_THREADS . $forceRestart . '), please check your configuration : ' . $error); return false; } } else { $return = CMS_patch::executeCommand(PATH_PHP_CLI_UNIX . ' -v 2>&1', $error); if ($error) { CMS_grandFather::raiseError('Error when testing php CLI with command "' . PATH_PHP_CLI_UNIX . ' -v", please check your configuration : ' . $error); return false; } if (io::strpos(io::strtolower($return), '(cli)') === false) { CMS_grandFather::raiseError(PATH_PHP_CLI_UNIX . ' is not the CLI version'); return false; } $return = CMS_patch::executeCommand("cd " . PATH_REALROOT_FS . "; " . PATH_PHP_CLI_UNIX . " " . PATH_PACKAGES_FS . "/scripts/script.php -m " . REGENERATION_THREADS . $forceRestart . " > /dev/null 2>&1 &", $error); if ($error) { CMS_grandFather::raiseError('Error during execution of script command (cd ' . PATH_REALROOT_FS . '; ' . PATH_PHP_CLI_UNIX . ' ' . PATH_PACKAGES_FS . '/scripts/script.php -m ' . REGENERATION_THREADS . $forceRestart . '), please check your configuration : ' . $error); return false; } } //CMS_grandFather::log($return); //CMS_grandFather::log("cd ".PATH_REALROOT_FS."; php ".PATH_PACKAGES_FS."/scripts/script.php -m ".REGENERATION_THREADS.$forceRestart." > /dev/null 2>&1 &"); //@system("cd ".PATH_REALROOT_FS."; php ".PATH_PACKAGES_FS."/scripts/script.php -m ".REGENERATION_THREADS.$forceRestart." > /dev/null 2>&1 &"); } } else { CMS_session::setSessionVar('start_script', true); } }
$error .= $error_step7_valid_tmp_path . '<br />'; } } } if ($error || $cms_action != "regeneratorParams") { //get values in standard_rc.xml file $module = CMS_modulesCatalog::getByCodename('standard'); $moduleParameters = $module->getParameters(false, true, true); //found CLI path $needCliPath = false; if ($moduleParameters['USE_BACKGROUND_REGENERATOR'][0] == 1) { if (APPLICATION_IS_WINDOWS) { $needCliPath = true; $cliPath = isset($_POST["cliPath"]) ? $_POST["cliPath"] : PATH_PHP_CLI_WINDOWS; } elseif (substr(CMS_patch::executeCommand('which php 2>&1', $error), 0, 1) !== '/') { $return = CMS_patch::executeCommand('php -v', $error); if (strpos(strtolower($return), '(cli)') === false) { $needCliPath = true; $cliPath = isset($_POST["cliPath"]) ? $_POST["cliPath"] : PATH_PHP_CLI_UNIX; } } } //CHMOD scripts with good values $scriptsFiles = CMS_file::getFileList(PATH_PACKAGES_FS . '/scripts/*.php'); foreach ($scriptsFiles as $aScriptFile) { //then set it executable CMS_file::makeExecutable($aScriptFile["name"]); } //test temporary directory and create it if none found $tmpPath = ''; if (@is_dir(ini_get("session.save_path")) && is_writable(PATH_TMP_FS)) {