From: skodak Date: Sat, 3 Jan 2009 14:28:02 +0000 (+0000) Subject: MDL-17776 added new define CLI_SCRIPT that identifies all CLI scripts + cron (cli... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a91b910e4f09e60866d72a6bac44ce0734094b3b;p=moodle.git MDL-17776 added new define CLI_SCRIPT that identifies all CLI scripts + cron (cli scripts do not have session and do not like HTML in output and are executed from command line, cron is an exception) --- diff --git a/admin/cron.php b/admin/cron.php index 62bae7105c..9615c6caeb 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -13,23 +13,17 @@ set_time_limit(0); $starttime = microtime(); +/// this cron script might be considered to be a CLI script even when accessed over HTTP, +/// we do not want HTML in output and there is no real session ;-) + define('CLI_SCRIPT', true); + /// The following is a hack necessary to allow this script to work well /// from the command line. - - define('FULLME', 'cron'); - + define('FULLME', 'cron'); //TODO: to be removed soon ;-) /// Do not set moodle cookie because we do not need it here, it is better to emulate session define('NO_MOODLE_COOKIES', true); -/// The current directory in PHP version 4.3.0 and above isn't necessarily the -/// directory of the script when run from the command line. The require_once() -/// would fail, so we'll have to chdir() - - if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) { - chdir(dirname($_SERVER['argv'][0])); - } - require_once(dirname(__FILE__) . '/../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/gradelib.php'); diff --git a/admin/handlevirus.php b/admin/handlevirus.php index 6311887fa2..83b8def7c2 100644 --- a/admin/handlevirus.php +++ b/admin/handlevirus.php @@ -6,16 +6,13 @@ * php -d error_log=/path/to/log thisfile.php will override the default error log for php cli, which is stderr, so if you want this script to just print stuff out, use php thisfile.php instead. */ -require_once($CFG->libdir.'/eventslib.php'); - - $fd = fopen('php://stdin','r'); if (!$fd) { exit(); } -$FULLME='cron'; require_once(dirname(dirname(__FILE__)).'/config.php'); +require_once($CFG->libdir.'/eventslib.php'); require_once($CFG->dirroot.'/lib/uploadlib.php'); // contains virus handling stuff. $site = get_site(); diff --git a/admin/process_email.php b/admin/process_email.php index 1b31177656..2b3713b340 100755 --- a/admin/process_email.php +++ b/admin/process_email.php @@ -1,6 +1,6 @@ #!/usr/bin/php -f access)) { + if (CLI_SCRIPT && !isset($USER->access)) { // In cron, some modules setup a 'fake' $USER, // ensure we load the appropriate accessdata. if (isset($ACCESS[$userid])) { @@ -422,7 +422,7 @@ function has_capability($capability, $context, $userid=NULL, $doanything=true) { $ACCESS = array(); $RDEFS = array(); - if (defined('FULLME') && FULLME === 'cron') { + if (CLI_SCRIPT) { load_user_accessdata($userid); $USER->access = $ACCESS[$userid]; $DIRTYCONTEXTS = array(); diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 2cef0895ca..72cf6c0bf8 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -3070,12 +3070,12 @@ function authenticate_user_login($username, $password) { $auth = empty($user->auth) ? 'manual' : $user->auth; // use manual if auth not set if ($auth=='nologin' or !is_enabled_auth($auth)) { add_to_log(0, 'login', 'error', 'index.php', $username); - error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Disabled Login: $username ".$_SERVER['HTTP_USER_AGENT']); + error_log('[client '.getremoteaddr()."] $CFG->wwwroot Disabled Login: $username ".$_SERVER['HTTP_USER_AGENT']); return false; } if (!empty($user->deleted)) { add_to_log(0, 'login', 'error', 'index.php', $username); - error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Deleted Login: $username ".$_SERVER['HTTP_USER_AGENT']); + error_log('[client '.getremoteaddr()."] $CFG->wwwroot Deleted Login: $username ".$_SERVER['HTTP_USER_AGENT']); return false; } $auths = array($auth); @@ -3138,7 +3138,7 @@ function authenticate_user_login($username, $password) { // failed if all the plugins have failed add_to_log(0, 'login', 'error', 'index.php', $username); if (debugging('', DEBUG_ALL)) { - error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Failed Login: $username ".$_SERVER['HTTP_USER_AGENT']); + error_log('[client '.getremoteaddr()."] $CFG->wwwroot Failed Login: $username ".$_SERVER['HTTP_USER_AGENT']); } return false; } diff --git a/lib/portfolio/exceptions.php b/lib/portfolio/exceptions.php index 52476b25d7..23e86a0d54 100644 --- a/lib/portfolio/exceptions.php +++ b/lib/portfolio/exceptions.php @@ -53,7 +53,7 @@ class portfolio_export_exception extends portfolio_exception { $continue = $exporter->get('caller')->get_return_url(); } } - if (!defined('FULLME') || FULLME != 'cron') { + if (!defined('FULLME') || FULLME != 'cron') { // TODO: this is not nice at all $exporter->process_stage_cleanup(); } } else { diff --git a/lib/sessionlib.php b/lib/sessionlib.php index 0bbf733386..c6cf04421d 100644 --- a/lib/sessionlib.php +++ b/lib/sessionlib.php @@ -159,14 +159,17 @@ class moodle_session { global $CFG, $nomoodlecookie; if (!defined('NO_MOODLE_COOKIES')) { - if (isset($nomoodlecookie)) { + if (CLI_SCRIPT) { + // CLI scripts can not have session + define('NO_MOODLE_COOKIES', true); + } else if (isset($nomoodlecookie)) { // backwards compatibility only define('NO_MOODLE_COOKIES', $nomoodlecookie); - unset($nomoodlecookie); } else { define('NO_MOODLE_COOKIES', false); } } + unset($nomoodlecookie); // cleanup if (!isset($CFG->cookiesecure) or strpos($CFG->wwwroot, 'https://') !== 0) { $CFG->cookiesecure = 0; diff --git a/lib/setup.php b/lib/setup.php index 3b17dace89..62698887dd 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -89,6 +89,23 @@ global $HTTPSPAGEREQUIRED; die; } +/// Detect CLI scripts - CLI scripts are executed from command line, do not have session and we do not want HTML in output + if (!defined('CLI_SCRIPT')) { // CLI_SCRIPT might be defined in 'fake' CLI scripts like admin/cron.php + if (isset($_SERVER['REMOTE_ADDR'])) { + define('CLI_SCRIPT', false); + } else { + define('CLI_SCRIPT', true); + } + } + +/// The current directory in PHP version 4.3.0 and above isn't necessarily the +/// directory of the script when run from the command line. The require_once() +/// would fail, so we'll have to chdir() + if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) { + chdir(dirname($_SERVER['argv'][0])); + } + + /// store settings from config.php in array in $CFG - we can use it later to detect problems and overrides $CFG->config_php_settings = (array)$CFG; diff --git a/lib/weblib.php b/lib/weblib.php index 12b970a482..6536b26be3 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1408,7 +1408,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL $time = time() - $CFG->cachetext; $md5key = md5($hashstr); - if (defined('FULLME') and FULLME == 'cron') { + if (CLI_SCRIPT) { if (isset($croncache[$md5key])) { return $croncache[$md5key]; } @@ -1416,7 +1416,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL if ($oldcacheitem = $DB->get_record('cache_text', array('md5key'=>$md5key), '*', true)) { if ($oldcacheitem->timemodified >= $time) { - if (defined('FULLME') and FULLME == 'cron') { + if (CLI_SCRIPT) { if (count($croncache) > 150) { reset($croncache); $key = key($croncache); @@ -1504,7 +1504,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL } if (empty($options->nocache) and !empty($CFG->cachetext) and $CFG->currenttextiscacheable) { - if (defined('FULLME') and FULLME == 'cron') { + if (CLI_SCRIPT) { // special static cron cache - no need to store it in db if its not already there if (count($croncache) > 150) { reset($croncache); @@ -5814,7 +5814,7 @@ function _print_normal_error($errorcode, $module, $a, $link, $backtrace, $debugi $message = get_string($errorcode, 'moodle', $a); } - if (defined('FULLME') && FULLME == 'cron') { + if (CLI_SCRIPT) { // Errors in cron should be mtrace'd. mtrace($message); die; @@ -6160,7 +6160,7 @@ function notice ($message, $link='', $course=NULL) { $message = clean_text($message); // In case nasties are in here - if (defined('FULLME') && FULLME == 'cron') { + if (CLI_SCRIPT) { // notices in cron should be mtrace'd. mtrace($message); die;