public static function view($removefieldset = false) { if (!self::$_started) { return; } self::$timeEnd = self::getmicrotime(); $time = sprintf('%.5f', self::$timeEnd - self::$timeStart); $files = sprintf('%.5f', self::$filesTime); $rapportSQL = sprintf('%.2f', 100 * self::$totalTime / $time); $rapportPHP = 100 - $rapportSQL; $memoryPeak = round(memory_get_peak_usage() / 1048576, 3); $content = 'File ' . $_SERVER['SCRIPT_NAME'] . "\n" . 'Loaded in ' . $time . ' seconds' . "\n" . 'Loaded PHP files : ' . self::$filesLoaded . "\n" . 'SQL requests : ' . sprintf('%.5f', self::$totalTime) . ' seconds (' . self::$sqlNbRequests . ' requests)' . "\n" . '% SQL/PHP : ' . $rapportSQL . ' / ' . $rapportPHP . ' %' . "\n" . 'Memory Peak : ' . $memoryPeak . 'Mo' . "\n"; if (function_exists('xdebug_get_profiler_filename') && xdebug_get_profiler_filename()) { $content .= 'XDebug Profile : ' . xdebug_get_profiler_filename() . "\n"; } if (function_exists('xdebug_get_profiler_filename') && xdebug_get_tracefile_name()) { $content .= 'XDebug Trace : ' . xdebug_get_tracefile_name() . "\n"; } $content .= 'User : '******' (' . CMS_session::getUserId() . ')' : 'none') . "\n"; $content .= 'Session Id ' . Zend_Session::getId() . "\n"; //$content .= 'Current page '.CMS_session::getPageID()."\n"; if (VIEW_SQL && $_SERVER["SCRIPT_NAME"] != PATH_ADMIN_WR . '/stat.php') { $stat = array('stat_time_start' => self::$timeStart, 'stat_time_end' => self::$timeEnd, 'stat_total_time' => self::$totalTime, 'stat_sql_nb_requests' => self::$sqlNbRequests, 'stat_sql_table' => self::$sqlTable, 'stat_content_name' => basename($_SERVER["SCRIPT_NAME"]), 'stat_files_table' => self::$filesTable, 'stat_memory_table' => self::$memoryTable, 'stat_memory_peak' => $memoryPeak, 'stat_files_loaded' => self::$filesLoaded); $statName = 'stat_' . md5(rand()); //save stats to cache (for 10 min) $cache = new CMS_cache($statName, 'atm-stats', 600, false); if ($cache) { $cache->save($stat); } } $content = !$removefieldset ? '<fieldset style="width:200px;" class="atm-debug"><legend>Debug Statistics</legend><pre>' . $content . '</pre>' : 'Debug Statistics :' . "\n" . $content; if (isset($statName)) { $content .= '<a href="' . PATH_ADMIN_WR . '/stat.php?stat=' . $statName . '" target="_blank">View statistics detail</a>'; } //end xhprof profiling if (defined('APPLICATION_ENABLE_PROFILING') && APPLICATION_ENABLE_PROFILING && function_exists('xhprof_disable')) { $xhprof_data = xhprof_disable(); include_once APPLICATION_XHPROF_ROOT_FS . "/xhprof_lib/utils/xhprof_lib.php"; include_once APPLICATION_XHPROF_ROOT_FS . "/xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $profileName = md5($_SERVER['REQUEST_URI']); $run_id = $xhprof_runs->save_run($xhprof_data, md5($_SERVER['REQUEST_URI'])); $content .= '<br /><a href="' . APPLICATION_XHPROF_URI . 'xhprof_html/index.php?run=' . $run_id . '&source=' . $profileName . '" target="_blank">View profiling detail</a>'; } $content .= !$removefieldset ? '</fieldset>' : ''; return $content; }
/** * 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; } } } } }
/** * Constructor. * Initializes the connection and launches the query. * * @param string $sql The sql statement * @param string $dsn The database dsn * @param string $user The database user * @param string $pass The database password * @return void * @access public */ public function __construct($sql = '', $dsn = APPLICATION_DB_DSN, $user = APPLICATION_DB_USER, $pass = APPLICATION_DB_PASSWORD) { $this->_sql = trim($sql); $this->_connect($dsn, $user, $pass); if ($this->_sql && $this->_db) { /*only for stats*/ if (STATS_DEBUG) { $time_start = CMS_stats::getmicrotime(); } if (preg_match("#^(insert|update)#i", $this->_sql)) { $this->_numRows = $this->_db->exec($this->_sql); if (preg_match("#^insert#i", $this->_sql)) { $this->_lastInsertedID = $this->_db->lastInsertId(); } $errorInfos = $this->_db->errorInfo(); if (isset($errorInfos[2])) { $clean_sql = str_replace("\n", "", $this->_sql); $clean_sql = preg_replace("#\t+#", " ", $clean_sql); $errorInfo = isset($errorInfos[2]) ? $errorInfos[2] : 'No error returned'; $this->raiseError('Database querying failed : ' . $errorInfo . "\nQuery : " . $clean_sql); } } else { $this->_result = $this->_db->query($this->_sql); if ($this->_result) { $this->_numRows = $this->_result->rowCount(); } else { $clean_sql = str_replace("\n", "", $this->_sql); $clean_sql = preg_replace("#\t+#", " ", $clean_sql); $errorInfos = $this->_db->errorInfo(); $errorInfo = isset($errorInfos[2]) ? $errorInfos[2] : 'No error returned'; $this->raiseError('Database querying failed : ' . $errorInfo . "\nQuery : " . $clean_sql . "\nFrom : " . io::getCallInfos(3)); } } /*only for stats*/ if (STATS_DEBUG) { $currenttime = CMS_stats::getmicrotime(); $time = $currenttime - $time_start; if (VIEW_SQL) { CMS_stats::$sqlTable[] = array('sql' => $this->_sql, 'time' => $time, 'current' => $currenttime - CMS_stats::$timeStart, 'from' => io::getCallInfos(3), 'memory' => memory_get_usage(), 'peak' => memory_get_peak_usage()); } CMS_stats::$sqlNbRequests++; CMS_stats::$totalTime += $time; } } }
/** * Returns the number of seconds since the start of the script * * @return integer * @access public */ function getExecutionTime() { return CMS_stats::getmicrotime() - $this->_startDate; }
/** * Function to get current time in microsecond */ function getmicrotime() { return CMS_stats::getmicrotime(); }
/** * Displays Admin page * * @param mixed $mode : the display mode to use in : * - boolean false : default mode, used by main frame page * - string 'menu' : used by menu frame page * - string 'modules' : used by modules frame page * - string 'arbo' : used by tree arbo frame page * - string 'frames' : used by general frames page * - string 'out' : used by out of frames page * - string 'frameChecker' : used by frameChecker page * - string 'loading' : used by loading frame page (send content when received without closing page content) * @return void * @access public */ function show($mode = false) { $this->_displayMode = $mode; switch ($this->_displayMode) { case 'menu': case 'frameChecker': case 'modules': case 'arbo': case 'frames': $this->_showHead(); $this->_showBody(); break; default: $this->_showHead(); $this->_showBody(); if ($this->_displayMode != "loading") { /*only for stats*/ if (STATS_DEBUG && $_SERVER["SCRIPT_NAME"] != PATH_ADMIN_WR . '/stat.php') { echo CMS_stats::view(); } } break; } if ($this->_displayMode != "loading") { echo "\n</html>"; } }
/** * Writes html header * * @return void * @access private */ private function _showHead($returnValue = false) { switch ($this->_displayMode) { case self::SHOW_JSON: case self::SHOW_RAW: case self::SHOW_XML: $return = ''; if ($this->hasErrors()) { $return .= ' <error>1</error>' . "\n" . ' <errormessage><![CDATA[' . $this->_espaceCdata($this->getErrors(true)) . ']]></errormessage>' . "\n"; } else { $return .= ' <error>0</error>' . "\n"; } if ($this->_secure && CMS_session::tokenIsExpired('admin')) { $token = CMS_session::getToken('admin'); //pr('new token : '.$token); $return .= ' <token><![CDATA[' . $token . ']]></token>' . "\n"; } if ($this->hasRawDatas()) { $return .= ' <rawdatas><![CDATA[' . $this->_espaceCdata($this->getRawDatas(true)) . ']]></rawdatas>' . "\n"; } if ($this->_actionmessage) { $return .= ' <message><![CDATA[' . $this->_espaceCdata($this->_actionmessage) . ']]></message>' . "\n"; } if ($this->_title) { $return .= ' <title><![CDATA[' . $this->_espaceCdata($this->_title) . ']]></title>' . "\n"; } if ($this->_disconnected) { $return .= ' <disconnected>1</disconnected>' . "\n"; } $scripts = CMS_scriptsManager::getScriptsNumberLeft(); if ($scripts) { $return .= ' <scripts>' . $scripts . '</scripts>' . "\n"; } if (SYSTEM_DEBUG && STATS_DEBUG) { $return .= ' <stats><![CDATA[' . $this->_espaceCdata(CMS_stats::view(true)) . ']]></stats>' . "\n"; } $jsfiles = CMS_view::getJavascript(array(), 'screen', true); if ($jsfiles) { $files = array('files' => $jsfiles, 'manager' => CMS_view::getJSManagerURL()); $return .= ' <jsfiles><![CDATA[' . $this->_espaceCdata(sensitiveIO::jsonEncode($files)) . ']]></jsfiles>' . "\n"; } $cssfiles = CMS_view::getCSS(array(), 'screen', true); if ($cssfiles) { $files = array('files' => $cssfiles, 'manager' => CMS_view::getCSSManagerURL()); $return .= ' <cssfiles><![CDATA[' . $this->_espaceCdata(sensitiveIO::jsonEncode($files)) . ']]></cssfiles>' . "\n"; } if (!$returnValue) { echo $return; } else { return $return; } break; case self::SHOW_HTML: default: $title = $this->_title ? '<title>' . APPLICATION_LABEL . ' :: ' . $this->_title . '</title>' : ''; echo '<head> <meta http-equiv="Content-Type" content="text/html; charset=' . APPLICATION_DEFAULT_ENCODING . '" /> ' . $title . ' ' . $this->_copyright() . ' <meta name="generator" content="' . CMS_grandFather::SYSTEM_LABEL . '" /> ' . CMS_view::getCSS() . ' ' . CMS_view::getJavascript(); if (APPLICATION_GCF_SUPPORT) { echo '<meta http-equiv="X-UA-Compatible" content="chrome=1">'; } echo '</head>'; break; } }