Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/setup.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00045 global $CFG; // this should be done much earlier in config.php before creating new $CFG instance
00046 
00047 if (!isset($CFG)) {
00048     if (defined('PHPUNIT_SCRIPT') and PHPUNIT_SCRIPT) {
00049         echo('There is a missing "global $CFG;" at the beginning of the config.php file.'."\n");
00050         exit(1);
00051     } else {
00052         // this should never happen, maybe somebody is accessing this file directly...
00053         exit(1);
00054     }
00055 }
00056 
00057 // We can detect real dirroot path reliably since PHP 4.0.2,
00058 // it can not be anything else, there is no point in having this in config.php
00059 $CFG->dirroot = dirname(dirname(__FILE__));
00060 
00061 // Normalise dataroot - we do not want any symbolic links, trailing / or any other weirdness there
00062 if (!isset($CFG->dataroot)) {
00063     if (isset($_SERVER['REMOTE_ADDR'])) {
00064         header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable');
00065     }
00066     echo('Fatal error: $CFG->dataroot is not specified in config.php! Exiting.'."\n");
00067     exit(1);
00068 }
00069 $CFG->dataroot = realpath($CFG->dataroot);
00070 if ($CFG->dataroot === false) {
00071     if (isset($_SERVER['REMOTE_ADDR'])) {
00072         header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable');
00073     }
00074     echo('Fatal error: $CFG->dataroot is not configured properly, directory does not exist or is not accessible! Exiting.'."\n");
00075     exit(1);
00076 } else if (!is_writable($CFG->dataroot)) {
00077     if (isset($_SERVER['REMOTE_ADDR'])) {
00078         header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable');
00079     }
00080     echo('Fatal error: $CFG->dataroot is not writable, admin has to fix directory permissions! Exiting.'."\n");
00081     exit(1);
00082 }
00083 
00084 // wwwroot is mandatory
00085 if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') {
00086     if (isset($_SERVER['REMOTE_ADDR'])) {
00087         header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable');
00088     }
00089     echo('Fatal error: $CFG->wwwroot is not configured! Exiting.'."\n");
00090     exit(1);
00091 }
00092 
00093 // Define admin directory
00094 if (!isset($CFG->admin)) {   // Just in case it isn't defined in config.php
00095     $CFG->admin = 'admin';   // This is relative to the wwwroot and dirroot
00096 }
00097 
00098 // Set up some paths.
00099 $CFG->libdir = $CFG->dirroot .'/lib';
00100 
00101 // Allow overriding of tempdir but be backwards compatible
00102 if (!isset($CFG->tempdir)) {
00103     $CFG->tempdir = "$CFG->dataroot/temp";
00104 }
00105 
00106 // Allow overriding of cachedir but be backwards compatible
00107 if (!isset($CFG->cachedir)) {
00108     $CFG->cachedir = "$CFG->dataroot/cache";
00109 }
00110 
00111 // The current directory in PHP version 4.3.0 and above isn't necessarily the
00112 // directory of the script when run from the command line. The require_once()
00113 // would fail, so we'll have to chdir()
00114 if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
00115     chdir(dirname($_SERVER['argv'][0]));
00116 }
00117 
00118 // sometimes default PHP settings are borked on shared hosting servers, I wonder why they have to do that??
00119 ini_set('precision', 14); // needed for upgrades and gradebook
00120 
00121 // Scripts may request no debug and error messages in output
00122 // please note it must be defined before including the config.php script
00123 // and in some cases you also need to set custom default exception handler
00124 if (!defined('NO_DEBUG_DISPLAY')) {
00125     define('NO_DEBUG_DISPLAY', false);
00126 }
00127 
00128 // Some scripts such as upgrade may want to prevent output buffering
00129 if (!defined('NO_OUTPUT_BUFFERING')) {
00130     define('NO_OUTPUT_BUFFERING', false);
00131 }
00132 
00133 // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined.
00134 // This is a quick hack.  Ideally we should ask the admin for a value.  See MDL-22625 for more on this.
00135 if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) {
00136     $olddebug = error_reporting(0);
00137     date_default_timezone_set(date_default_timezone_get());
00138     error_reporting($olddebug);
00139     unset($olddebug);
00140 }
00141 
00142 // PHPUnit scripts are a special case, for now we treat them as normal CLI scripts,
00143 // please note you must install PHPUnit library separately via PEAR
00144 if (!defined('PHPUNIT_SCRIPT')) {
00145     define('PHPUNIT_SCRIPT', false);
00146 }
00147 if (PHPUNIT_SCRIPT) {
00148     define('CLI_SCRIPT', true);
00149 }
00150 
00151 // Detect CLI scripts - CLI scripts are executed from command line, do not have session and we do not want HTML in output
00152 // In your new CLI scripts just add "define('CLI_SCRIPT', true);" before requiring config.php.
00153 // Please note that one script can not be accessed from both CLI and web interface.
00154 if (!defined('CLI_SCRIPT')) {
00155     define('CLI_SCRIPT', false);
00156 }
00157 if (defined('WEB_CRON_EMULATED_CLI')) {
00158     if (!isset($_SERVER['REMOTE_ADDR'])) {
00159         echo('Web cron can not be executed as CLI script any more, please use admin/cli/cron.php instead'."\n");
00160         exit(1);
00161     }
00162 } else if (isset($_SERVER['REMOTE_ADDR'])) {
00163     if (CLI_SCRIPT) {
00164         echo('Command line scripts can not be executed from the web interface');
00165         exit(1);
00166     }
00167 } else {
00168     if (!CLI_SCRIPT) {
00169         echo('Command line scripts must define CLI_SCRIPT before requiring config.php'."\n");
00170         exit(1);
00171     }
00172 }
00173 
00174 // Detect CLI maintenance mode - this is useful when you need to mess with database, such as during upgrades
00175 if (file_exists("$CFG->dataroot/climaintenance.html")) {
00176     if (!CLI_SCRIPT) {
00177         header('Content-type: text/html; charset=utf-8');
00179         header('Cache-Control: no-store, no-cache, must-revalidate');
00180         header('Cache-Control: post-check=0, pre-check=0', false);
00181         header('Pragma: no-cache');
00182         header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
00183         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
00184         header('Accept-Ranges: none');
00185         readfile("$CFG->dataroot/climaintenance.html");
00186         die;
00187     } else {
00188         if (!defined('CLI_MAINTENANCE')) {
00189             define('CLI_MAINTENANCE', true);
00190         }
00191     }
00192 } else {
00193     if (!defined('CLI_MAINTENANCE')) {
00194         define('CLI_MAINTENANCE', false);
00195     }
00196 }
00197 
00198 if (CLI_SCRIPT) {
00199     // sometimes people use different PHP binary for web and CLI, make 100% sure they have the supported PHP version
00200     if (version_compare(phpversion(), '5.3.2') < 0) {
00201         $phpversion = phpversion();
00202         // do NOT localise - lang strings would not work here and we CAN NOT move it to later place
00203         echo "Moodle 2.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).\n";
00204         echo "Some servers may have multiple PHP versions installed, are you using the correct executable?\n";
00205         exit(1);
00206     }
00207 }
00208 
00209 // Detect ajax scripts - they are similar to CLI because we can not redirect, output html, etc.
00210 if (!defined('AJAX_SCRIPT')) {
00211     define('AJAX_SCRIPT', false);
00212 }
00213 
00214 // File permissions on created directories in the $CFG->dataroot
00215 if (empty($CFG->directorypermissions)) {
00216     $CFG->directorypermissions = 02777;      // Must be octal (that's why it's here)
00217 }
00218 if (empty($CFG->filepermissions)) {
00219     $CFG->filepermissions = ($CFG->directorypermissions & 0666); // strip execute flags
00220 }
00221 // better also set default umask because recursive mkdir() does not apply permissions recursively otherwise
00222 umask(0000);
00223 
00224 // exact version of currently used yui2 and 3 library
00225 $CFG->yui2version = '2.9.0';
00226 $CFG->yui3version = '3.4.1';
00227 
00228 
00229 // special support for highly optimised scripts that do not need libraries and DB connection
00230 if (defined('ABORT_AFTER_CONFIG')) {
00231     if (!defined('ABORT_AFTER_CONFIG_CANCEL')) {
00232         // hide debugging if not enabled in config.php - we do not want to disclose sensitive info
00233         if (isset($CFG->debug)) {
00234             error_reporting($CFG->debug);
00235         } else {
00236             error_reporting(0);
00237         }
00238         if (empty($CFG->debugdisplay)) {
00239             ini_set('display_errors', '0');
00240             ini_set('log_errors', '1');
00241         } else {
00242             ini_set('display_errors', '1');
00243         }
00244         require_once("$CFG->dirroot/lib/configonlylib.php");
00245         return;
00246     }
00247 }
00248 
00250 if (!defined('MOODLE_INTERNAL')) { // necessary because cli installer has to define it earlier
00251     define('MOODLE_INTERNAL', true);
00252 }
00253 
00254 // Early profiling start, based exclusively on config.php $CFG settings
00255 if (!empty($CFG->earlyprofilingenabled)) {
00256     require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
00257     if (profiling_start()) {
00258         register_shutdown_function('profiling_stop');
00259     }
00260 }
00261 
00267 global $DB;
00268 
00275 global $SESSION;
00276 
00296 global $USER;
00297 
00301 global $SITE;
00302 
00310 global $PAGE;
00311 
00317 global $COURSE;
00318 
00333 global $OUTPUT;
00334 
00340 global $MCACHE;
00341 
00351 global $FULLME;
00352 
00358 global $ME;
00359 
00365 global $FULLSCRIPT;
00366 
00372 global $SCRIPT;
00373 
00374 // Store settings from config.php in array in $CFG - we can use it later to detect problems and overrides
00375 $CFG->config_php_settings = (array)$CFG;
00376 // Forced plugin settings override values from config_plugins table
00377 unset($CFG->config_php_settings['forced_plugin_settings']);
00378 if (!isset($CFG->forced_plugin_settings)) {
00379     $CFG->forced_plugin_settings = array();
00380 }
00381 // Set httpswwwroot default value (this variable will replace $CFG->wwwroot
00382 // inside some URLs used in HTTPSPAGEREQUIRED pages.
00383 $CFG->httpswwwroot = $CFG->wwwroot;
00384 
00385 require_once($CFG->libdir .'/setuplib.php');        // Functions that MUST be loaded first
00386 
00387 if (NO_OUTPUT_BUFFERING) {
00388     // we have to call this always before starting session because it discards headers!
00389     disable_output_buffering();
00390 }
00391 
00392 // Increase memory limits if possible
00393 raise_memory_limit(MEMORY_STANDARD);
00394 
00395 // Time to start counting
00396 init_performance_info();
00397 
00398 // Put $OUTPUT in place, so errors can be displayed.
00399 $OUTPUT = new bootstrap_renderer();
00400 
00401 // set handler for uncaught exceptions - equivalent to print_error() call
00402 set_exception_handler('default_exception_handler');
00403 set_error_handler('default_error_handler', E_ALL | E_STRICT);
00404 
00405 // If there are any errors in the standard libraries we want to know!
00406 error_reporting(E_ALL);
00407 
00408 // Just say no to link prefetching (Moz prefetching, Google Web Accelerator, others)
00409 // http://www.google.com/webmasters/faq.html#prefetchblock
00410 if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){
00411     header($_SERVER['SERVER_PROTOCOL'] . ' 404 Prefetch Forbidden');
00412     echo('Prefetch request forbidden.');
00413     exit(1);
00414 }
00415 
00416 if (!isset($CFG->prefix)) {   // Just in case it isn't defined in config.php
00417     $CFG->prefix = '';
00418 }
00419 
00420 // location of all languages except core English pack
00421 if (!isset($CFG->langotherroot)) {
00422     $CFG->langotherroot = $CFG->dataroot.'/lang';
00423 }
00424 
00425 // location of local lang pack customisations (dirs with _local suffix)
00426 if (!isset($CFG->langlocalroot)) {
00427     $CFG->langlocalroot = $CFG->dataroot.'/lang';
00428 }
00429 
00430 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
00431 //the problem is that we need specific version of quickforms and hacked excel files :-(
00432 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
00433 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
00434 //please note zend library is supposed to be used only from web service protocol classes, it may be removed in future
00435 ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path'));
00436 
00437 // Load up standard libraries
00438 require_once($CFG->libdir .'/textlib.class.php');   // Functions to handle multibyte strings
00439 require_once($CFG->libdir .'/filterlib.php');       // Functions for filtering test as it is output
00440 require_once($CFG->libdir .'/ajax/ajaxlib.php');    // Functions for managing our use of JavaScript and YUI
00441 require_once($CFG->libdir .'/weblib.php');          // Functions relating to HTTP and content
00442 require_once($CFG->libdir .'/outputlib.php');       // Functions for generating output
00443 require_once($CFG->libdir .'/navigationlib.php');   // Class for generating Navigation structure
00444 require_once($CFG->libdir .'/dmllib.php');          // Database access
00445 require_once($CFG->libdir .'/datalib.php');         // Legacy lib with a big-mix of functions.
00446 require_once($CFG->libdir .'/accesslib.php');       // Access control functions
00447 require_once($CFG->libdir .'/deprecatedlib.php');   // Deprecated functions included for backward compatibility
00448 require_once($CFG->libdir .'/moodlelib.php');       // Other general-purpose functions
00449 require_once($CFG->libdir .'/enrollib.php');        // Enrolment related functions
00450 require_once($CFG->libdir .'/pagelib.php');         // Library that defines the moodle_page class, used for $PAGE
00451 require_once($CFG->libdir .'/blocklib.php');        // Library for controlling blocks
00452 require_once($CFG->libdir .'/eventslib.php');       // Events functions
00453 require_once($CFG->libdir .'/grouplib.php');        // Groups functions
00454 require_once($CFG->libdir .'/sessionlib.php');      // All session and cookie related stuff
00455 require_once($CFG->libdir .'/editorlib.php');       // All text editor related functions and classes
00456 require_once($CFG->libdir .'/messagelib.php');      // Messagelib functions
00457 require_once($CFG->libdir .'/modinfolib.php');      // Cached information on course-module instances
00458 
00459 // make sure PHP is not severly misconfigured
00460 setup_validate_php_configuration();
00461 
00462 // Connect to the database
00463 setup_DB();
00464 
00465 // Disable errors for now - needed for installation when debug enabled in config.php
00466 if (isset($CFG->debug)) {
00467     $originalconfigdebug = $CFG->debug;
00468     unset($CFG->debug);
00469 } else {
00470     $originalconfigdebug = -1;
00471 }
00472 
00473 // Load up any configuration from the config table
00474 initialise_cfg();
00475 
00476 // Verify upgrade is not running unless we are in a script that needs to execute in any case
00477 if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
00478     if ($CFG->upgraderunning < time()) {
00479         unset_config('upgraderunning');
00480     } else {
00481         print_error('upgraderunning');
00482     }
00483 }
00484 
00485 // Turn on SQL logging if required
00486 if (!empty($CFG->logsql)) {
00487     $DB->set_logging(true);
00488 }
00489 
00490 // Prevent warnings from roles when upgrading with debug on
00491 if (isset($CFG->debug)) {
00492     $originaldatabasedebug = $CFG->debug;
00493     unset($CFG->debug);
00494 } else {
00495     $originaldatabasedebug = -1;
00496 }
00497 
00498 // enable circular reference collector in PHP 5.3,
00499 // it helps a lot when using large complex OOP structures such as in amos or gradebook
00500 if (function_exists('gc_enable')) {
00501     gc_enable();
00502 }
00503 
00504 // Register default shutdown tasks - such as Apache memory release helper, perf logging, etc.
00505 if (function_exists('register_shutdown_function')) {
00506     register_shutdown_function('moodle_request_shutdown');
00507 }
00508 
00509 // Set error reporting back to normal
00510 if ($originaldatabasedebug == -1) {
00511     $CFG->debug = DEBUG_MINIMAL;
00512 } else {
00513     $CFG->debug = $originaldatabasedebug;
00514 }
00515 if ($originalconfigdebug !== -1) {
00516     $CFG->debug = $originalconfigdebug;
00517 }
00518 unset($originalconfigdebug);
00519 unset($originaldatabasedebug);
00520 error_reporting($CFG->debug);
00521 
00522 // find out if PHP configured to display warnings,
00523 // this is a security problem because some moodle scripts may
00524 // disclose sensitive information
00525 if (ini_get_bool('display_errors')) {
00526     define('WARN_DISPLAY_ERRORS_ENABLED', true);
00527 }
00528 // If we want to display Moodle errors, then try and set PHP errors to match
00529 if (!isset($CFG->debugdisplay)) {
00530     // keep it "as is" during installation
00531 } else if (NO_DEBUG_DISPLAY) {
00532     // some parts of Moodle cannot display errors and debug at all.
00533     ini_set('display_errors', '0');
00534     ini_set('log_errors', '1');
00535 } else if (empty($CFG->debugdisplay)) {
00536     ini_set('display_errors', '0');
00537     ini_set('log_errors', '1');
00538 } else {
00539     // This is very problematic in XHTML strict mode!
00540     ini_set('display_errors', '1');
00541 }
00542 
00543 // detect unsupported upgrade jump as soon as possible - do not change anything, do not use system functions
00544 if (!empty($CFG->version) and $CFG->version < 2007101509) {
00545     print_error('upgraderequires19', 'error');
00546     die;
00547 }
00548 
00549 // Shared-Memory cache init -- will set $MCACHE
00550 // $MCACHE is a global object that offers at least add(), set() and delete()
00551 // with similar semantics to the memcached PHP API http://php.net/memcache
00552 // Ensure we define rcache - so we can later check for it
00553 // with a really fast and unambiguous $CFG->rcache === false
00554 if (!empty($CFG->cachetype)) {
00555     if (empty($CFG->rcache)) {
00556         $CFG->rcache = false;
00557     } else {
00558         $CFG->rcache = true;
00559     }
00560 
00561     // do not try to initialize if cache disabled
00562     if (!$CFG->rcache) {
00563         $CFG->cachetype = '';
00564     }
00565 
00566     if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) {
00567         if (!init_memcached()) {
00568             debugging("Error initialising memcached");
00569             $CFG->cachetype = '';
00570             $CFG->rcache = false;
00571         }
00572     } else if ($CFG->cachetype === 'eaccelerator') {
00573         if (!init_eaccelerator()) {
00574             debugging("Error initialising eaccelerator cache");
00575             $CFG->cachetype = '';
00576             $CFG->rcache = false;
00577         }
00578     }
00579 
00580 } else { // just make sure it is defined
00581     $CFG->cachetype = '';
00582     $CFG->rcache    = false;
00583 }
00584 
00585 // Calculate and set $CFG->ostype to be used everywhere. Possible values are:
00586 // - WINDOWS: for any Windows flavour.
00587 // - UNIX: for the rest
00588 // Also, $CFG->os can continue being used if more specialization is required
00589 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
00590     $CFG->ostype = 'WINDOWS';
00591 } else {
00592     $CFG->ostype = 'UNIX';
00593 }
00594 $CFG->os = PHP_OS;
00595 
00596 // Configure ampersands in URLs
00597 ini_set('arg_separator.output', '&amp;');
00598 
00599 // Work around for a PHP bug   see MDL-11237
00600 ini_set('pcre.backtrack_limit', 20971520);  // 20 MB
00601 
00602 // Location of standard files
00603 $CFG->wordlist = $CFG->libdir .'/wordlist.txt';
00604 $CFG->moddata  = 'moddata';
00605 
00606 // A hack to get around magic_quotes_gpc being turned on
00607 // It is strongly recommended to disable "magic_quotes_gpc"!
00608 if (ini_get_bool('magic_quotes_gpc')) {
00609     function stripslashes_deep($value) {
00610         $value = is_array($value) ?
00611                 array_map('stripslashes_deep', $value) :
00612                 stripslashes($value);
00613         return $value;
00614     }
00615     $_POST = array_map('stripslashes_deep', $_POST);
00616     $_GET = array_map('stripslashes_deep', $_GET);
00617     $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
00618     $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
00619     if (!empty($_SERVER['REQUEST_URI'])) {
00620         $_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']);
00621     }
00622     if (!empty($_SERVER['QUERY_STRING'])) {
00623         $_SERVER['QUERY_STRING'] = stripslashes($_SERVER['QUERY_STRING']);
00624     }
00625     if (!empty($_SERVER['HTTP_REFERER'])) {
00626         $_SERVER['HTTP_REFERER'] = stripslashes($_SERVER['HTTP_REFERER']);
00627     }
00628    if (!empty($_SERVER['PATH_INFO'])) {
00629         $_SERVER['PATH_INFO'] = stripslashes($_SERVER['PATH_INFO']);
00630     }
00631     if (!empty($_SERVER['PHP_SELF'])) {
00632         $_SERVER['PHP_SELF'] = stripslashes($_SERVER['PHP_SELF']);
00633     }
00634     if (!empty($_SERVER['PATH_TRANSLATED'])) {
00635         $_SERVER['PATH_TRANSLATED'] = stripslashes($_SERVER['PATH_TRANSLATED']);
00636     }
00637 }
00638 
00639 // neutralise nasty chars in PHP_SELF
00640 if (isset($_SERVER['PHP_SELF'])) {
00641     $phppos = strpos($_SERVER['PHP_SELF'], '.php');
00642     if ($phppos !== false) {
00643         $_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, $phppos+4);
00644     }
00645     unset($phppos);
00646 }
00647 
00648 // initialise ME's - this must be done BEFORE starting of session!
00649 initialise_fullme();
00650 
00651 // define SYSCONTEXTID in config.php if you want to save some queries,
00652 // after install it must match the system context record id.
00653 if (!defined('SYSCONTEXTID')) {
00654     get_system_context();
00655 }
00656 
00657 // Defining the site - aka frontpage course
00658 try {
00659     $SITE = get_site();
00660 } catch (dml_exception $e) {
00661     $SITE = null;
00662     if (empty($CFG->version)) {
00663         $SITE = new stdClass();
00664         $SITE->id = 1;
00665     } else {
00666         throw $e;
00667     }
00668 }
00669 // And the 'default' course - this will usually get reset later in require_login() etc.
00670 $COURSE = clone($SITE);
00672 define('SITEID', $SITE->id);
00673 
00674 // init session prevention flag - this is defined on pages that do not want session
00675 if (CLI_SCRIPT) {
00676     // no sessions in CLI scripts possible
00677     define('NO_MOODLE_COOKIES', true);
00678 
00679 } else if (!defined('NO_MOODLE_COOKIES')) {
00680     if (empty($CFG->version) or $CFG->version < 2009011900) {
00681         // no session before sessions table gets created
00682         define('NO_MOODLE_COOKIES', true);
00683     } else if (CLI_SCRIPT) {
00684         // CLI scripts can not have session
00685         define('NO_MOODLE_COOKIES', true);
00686     } else {
00687         define('NO_MOODLE_COOKIES', false);
00688     }
00689 }
00690 
00691 // start session and prepare global $SESSION, $USER
00692 session_get_instance();
00693 $SESSION = &$_SESSION['SESSION'];
00694 $USER    = &$_SESSION['USER'];
00695 
00696 // Late profiling, only happening if early one wasn't started
00697 if (!empty($CFG->profilingenabled)) {
00698     require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
00699     if (profiling_start()) {
00700         register_shutdown_function('profiling_stop');
00701     }
00702 }
00703 
00704 // Process theme change in the URL.
00705 if (!empty($CFG->allowthemechangeonurl) and !empty($_GET['theme'])) {
00706     // we have to use _GET directly because we do not want this to interfere with _POST
00707     $urlthemename = optional_param('theme', '', PARAM_PLUGIN);
00708     try {
00709         $themeconfig = theme_config::load($urlthemename);
00710         // Makes sure the theme can be loaded without errors.
00711         if ($themeconfig->name === $urlthemename) {
00712             $SESSION->theme = $urlthemename;
00713         } else {
00714             unset($SESSION->theme);
00715         }
00716         unset($themeconfig);
00717         unset($urlthemename);
00718     } catch (Exception $e) {
00719         debugging('Failed to set the theme from the URL.', DEBUG_DEVELOPER, $e->getTrace());
00720     }
00721 }
00722 unset($urlthemename);
00723 
00724 // Ensure a valid theme is set.
00725 if (!isset($CFG->theme)) {
00726     $CFG->theme = 'standardwhite';
00727 }
00728 
00729 // Set language/locale of printed times.  If user has chosen a language that
00730 // that is different from the site language, then use the locale specified
00731 // in the language file.  Otherwise, if the admin hasn't specified a locale
00732 // then use the one from the default language.  Otherwise (and this is the
00733 // majority of cases), use the stored locale specified by admin.
00734 // note: do not accept lang parameter from POST
00735 if (isset($_GET['lang']) and ($lang = optional_param('lang', '', PARAM_SAFEDIR))) {
00736     if (get_string_manager()->translation_exists($lang, false)) {
00737         $SESSION->lang = $lang;
00738     }
00739 }
00740 unset($lang);
00741 
00742 setup_lang_from_browser();
00743 
00744 if (empty($CFG->lang)) {
00745     if (empty($SESSION->lang)) {
00746         $CFG->lang = 'en';
00747     } else {
00748         $CFG->lang = $SESSION->lang;
00749     }
00750 }
00751 
00752 // Set the default site locale, a lot of the stuff may depend on this
00753 // it is definitely too late to call this first in require_login()!
00754 moodle_setlocale();
00755 
00756 // Create the $PAGE global - this marks the PAGE and OUTPUT fully initialised, this MUST be done at the end of setup!
00757 if (!empty($CFG->moodlepageclass)) {
00758     $classname = $CFG->moodlepageclass;
00759 } else {
00760     $classname = 'moodle_page';
00761 }
00762 $PAGE = new $classname();
00763 unset($classname);
00764 
00765 
00766 if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) {
00767     if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') {    // Temporary measure to help with XHTML validation
00768         if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) {      // Allow W3CValidator in as user called w3cvalidator (or guest)
00769             if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
00770                 (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
00771                 if ($user = get_complete_user_data("username", "w3cvalidator")) {
00772                     $user->ignoresesskey = true;
00773                 } else {
00774                     $user = guest_user();
00775                 }
00776                 session_set_user($user);
00777             }
00778         }
00779     }
00780 }
00781 
00782 // Apache log integration. In apache conf file one can use ${MOODULEUSER}n in
00783 // LogFormat to get the current logged in username in moodle.
00784 if ($USER && function_exists('apache_note')
00785     && !empty($CFG->apacheloguser) && isset($USER->username)) {
00786     $apachelog_userid = $USER->id;
00787     $apachelog_username = clean_filename($USER->username);
00788     $apachelog_name = '';
00789     if (isset($USER->firstname)) {
00790         // We can assume both will be set
00791         // - even if to empty.
00792         $apachelog_name = clean_filename($USER->firstname . " " .
00793                                          $USER->lastname);
00794     }
00795     if (session_is_loggedinas()) {
00796         $realuser = session_get_realuser();
00797         $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username);
00798         $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name);
00799         $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid);
00800     }
00801     switch ($CFG->apacheloguser) {
00802         case 3:
00803             $logname = $apachelog_username;
00804             break;
00805         case 2:
00806             $logname = $apachelog_name;
00807             break;
00808         case 1:
00809         default:
00810             $logname = $apachelog_userid;
00811             break;
00812     }
00813     apache_note('MOODLEUSER', $logname);
00814 }
00815 
00816 // Use a custom script replacement if one exists
00817 if (!empty($CFG->customscripts)) {
00818     if (($customscript = custom_script_path()) !== false) {
00819         require ($customscript);
00820     }
00821 }
00822 
00823 if (CLI_SCRIPT and !defined('WEB_CRON_EMULATED_CLI') and !PHPUNIT_SCRIPT) {
00824     // no ip blocking
00825 } else if (!empty($CFG->allowbeforeblock)) { // allowed list processed before blocked list?
00826     // in this case, ip in allowed list will be performed first
00827     // for example, client IP is 192.168.1.1
00828     // 192.168 subnet is an entry in allowed list
00829     // 192.168.1.1 is banned in blocked list
00830     // This ip will be banned finally
00831     if (!empty($CFG->allowedip)) {
00832         if (!remoteip_in_list($CFG->allowedip)) {
00833             die(get_string('ipblocked', 'admin'));
00834         }
00835     }
00836     // need further check, client ip may a part of
00837     // allowed subnet, but a IP address are listed
00838     // in blocked list.
00839     if (!empty($CFG->blockedip)) {
00840         if (remoteip_in_list($CFG->blockedip)) {
00841             die(get_string('ipblocked', 'admin'));
00842         }
00843     }
00844 
00845 } else {
00846     // in this case, IPs in blocked list will be performed first
00847     // for example, client IP is 192.168.1.1
00848     // 192.168 subnet is an entry in blocked list
00849     // 192.168.1.1 is allowed in allowed list
00850     // This ip will be allowed finally
00851     if (!empty($CFG->blockedip)) {
00852         if (remoteip_in_list($CFG->blockedip)) {
00853             // if the allowed ip list is not empty
00854             // IPs are not included in the allowed list will be
00855             // blocked too
00856             if (!empty($CFG->allowedip)) {
00857                 if (!remoteip_in_list($CFG->allowedip)) {
00858                     die(get_string('ipblocked', 'admin'));
00859                 }
00860             } else {
00861                 die(get_string('ipblocked', 'admin'));
00862             }
00863         }
00864     }
00865     // if blocked list is null
00866     // allowed list should be tested
00867     if(!empty($CFG->allowedip)) {
00868         if (!remoteip_in_list($CFG->allowedip)) {
00869             die(get_string('ipblocked', 'admin'));
00870         }
00871     }
00872 
00873 }
00874 
00875 // note: we can not block non utf-8 installations here, because empty mysql database
00876 // might be converted to utf-8 in admin/index.php during installation
00877 
00878 
00879 
00880 // this is a funny trick to make Eclipse believe that $OUTPUT and other globals
00881 // contains an instance of core_renderer, etc. which in turn fixes autocompletion ;-)
00882 if (false) {
00883     $DB = new moodle_database();
00884     $OUTPUT = new core_renderer(null, null);
00885     $PAGE = new moodle_page();
00886 }
 All Data Structures Namespaces Files Functions Variables Enumerations