]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17776 added new define CLI_SCRIPT that identifies all CLI scripts + cron (cli...
authorskodak <skodak>
Sat, 3 Jan 2009 14:28:02 +0000 (14:28 +0000)
committerskodak <skodak>
Sat, 3 Jan 2009 14:28:02 +0000 (14:28 +0000)
admin/cron.php
admin/handlevirus.php
admin/process_email.php
lib/accesslib.php
lib/moodlelib.php
lib/portfolio/exceptions.php
lib/sessionlib.php
lib/setup.php
lib/weblib.php

index 62bae7105ca0dff6fe76314e1db35e2728c82cf4..9615c6caeb9cdbe695003198656712e09064c146 100644 (file)
     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');
index 6311887fa2d2cd5186a253ee023520f6ee744a54..83b8def7c27995654e6b4700010b3f953082f471 100644 (file)
@@ -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();
index 1b311776565d855d2d480a1fc387b015ea64c03f..2b3713b34022d386956f79446f12de1ffdb12b74 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/php -f
 <?php // $Id$
-define('FULLME','cron'); // prevent warnings
+
 //error_reporting(0);
 //ini_set('display_errors',0);
 require_once(dirname(dirname(__FILE__)).'/config.php');
index 84d2b9e756f999c366cd8106a6f16b18208f1503..c1879117371df4c7553f9a7d5c13f43a44482844 100755 (executable)
@@ -241,8 +241,8 @@ function get_role_access($roleid, $accessdata=NULL) {
           ORDER BY ctx.depth, ctx.path";
     $params = array($roleid);
 
-    // we need extra caching in cron only
-    if (defined('FULLME') and FULLME === 'cron') {
+    // we need extra caching in CLI scripts and cron
+    if (CLI_SCRIPT) {
         static $cron_cache = array();
 
         if (!isset($cron_cache[$roleid])) {
@@ -385,7 +385,7 @@ function has_capability($capability, $context, $userid=NULL, $doanything=true) {
         array_shift($contexts);
     }
 
-    if (defined('FULLME') && FULLME === 'cron' && !isset($USER->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();
index 2cef0895cad930fd34d64a5b137a12ece5e6bdf1..72cf6c0bf8bb41e5173a3d70003212c6a78dbb6d 100644 (file)
@@ -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;
 }
index 52476b25d7b57fe0071e8dc3c5814454e606be60..23e86a0d54b16c66a1521807a81f517a701d42fc 100644 (file)
@@ -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 {
index 0bbf7333867c15305bbfd22712d35d254c550001..c6cf04421d241c0994f3c9276ccfa6082b6584ab 100644 (file)
@@ -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;
index 3b17dace89dcae2ef6a96288942351cbb5714abe..62698887dd1b7622ae52d0a9e1b8b55f99178b37 100644 (file)
@@ -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;
 
index 12b970a482a7440381c37e688f585e5fbc6e7fda..6536b26be3db30c5167786912a0de34ebf7ff0fa 100644 (file)
@@ -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;