MDL-16438 standardized component names
[moodle.git] / lib / setup.php
CommitLineData
da8759cb 1<?php
b37eac91 2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
da8759cb 18/**
19 * setup.php - Sets up sessions, connects to databases and so on
20 *
75249234 21 * Normally this is only called by the main config.php file
22 * Normally this file does not need to be edited.
b37eac91 23 *
24 * @package moodlecore
25 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
da8759cb 27 */
28
da8759cb 29/**
e1d1b796 30 * Holds the core settings that affect how Moodle works. Some of its fields
31 * are set in config.php, and the rest are loaded from the config table.
32 *
33 * Some typical settings in the $CFG global:
34 * - $CFG->wwwroot - Path to moodle index directory in url format.
35 * - $CFG->dataroot - Path to moodle index directory on server's filesystem.
36 * - $CFG->libdir - Path to moodle's library folder on server's filesystem.
37 *
38 * @global object $CFG
b37eac91 39 * @name $CFG
e1d1b796 40 */
41global $CFG;
42
43/**
44 * Database connection. Used for all access to the database.
45 * @global moodle_database $DB
b37eac91 46 * @name $DB
e1d1b796 47 */
48global $DB;
49
50/**
51 * Moodle's wrapper round PHP's $_SESSION.
52 *
53 * @global object $SESSION
b37eac91 54 * @name $SESSION
e1d1b796 55 */
56global $SESSION;
57
58/**
59 * Holds the user table record for the current user. Will be the 'guest'
60 * user record for people who are not logged in.
61 *
62 * $USER is stored in the session.
da8759cb 63 *
735b8567 64 * Items found in the user record:
da8759cb 65 * - $USER->emailstop - Does the user want email sent to them?
66 * - $USER->email - The user's email address.
67 * - $USER->id - The unique integer identified of this user in the 'user' table.
68 * - $USER->email - The user's email address.
69 * - $USER->firstname - The user's first name.
70 * - $USER->lastname - The user's last name.
71 * - $USER->username - The user's login username.
72 * - $USER->secret - The user's ?.
73 * - $USER->lang - The user's language choice.
74 *
e1d1b796 75 * @global object $USER
b37eac91 76 * @name $USER
da8759cb 77 */
674fb525 78global $USER;
e1d1b796 79
c13a5e71 80/**
81 * A central store of information about the current page we are
82 * generating in response to the user's request.
83 *
84 * @global moodle_page $PAGE
b37eac91 85 * @name $PAGE
c13a5e71 86 */
87global $PAGE;
88
da8759cb 89/**
e1d1b796 90 * The current course. An alias for $PAGE->course.
91 * @global object $COURSE
b37eac91 92 * @name $COURSE
da8759cb 93 */
94global $COURSE;
e1d1b796 95
da8759cb 96/**
97 * $THEME is a global that defines the site theme.
98 *
99 * Items found in the theme record:
100 * - $THEME->cellheading - Cell colors.
101 * - $THEME->cellheading2 - Alternate cell colors.
102 *
e1d1b796 103 * @global object $THEME
b37eac91 104 * @name THEME
da8759cb 105 */
106global $THEME;
f9903ed0 107
9d0dd812 108/**
e1d1b796 109 * Shared memory cache.
110 * @global object $MCACHE
b37eac91 111 * @name $MCACHE
e1d1b796 112 */
113global $MCACHE;
114
115/**
116 * A global to define if the page being displayed must run under HTTPS.
6800d78e 117 *
e1d1b796 118 * Its primary goal is to allow 100% HTTPS pages when $CFG->loginhttps is enabled. Default to false.
119 * Its enabled only by the httpsrequired() function and used in some pages to update some URLs
b37eac91 120 *
121 * @global bool $HTTPSPAGEREQUIRED
122 * @name $HTTPSPAGEREQUIRED
123 */
9d0dd812 124global $HTTPSPAGEREQUIRED;
125
b37eac91 126/**
127 * Full script path including all params, slash arguments, scheme and host.
128 * @global string $FULLME
129 * @name $FULLME
130 */
11e7b506 131global $FULLME;
e1d1b796 132
b37eac91 133/**
134 * Script path including query string and slash arguments without host.
135 * @global string $ME
136 * @name $ME
137 */
11e7b506 138global $ME;
e1d1b796 139
b37eac91 140/**
141 * $FULLME without slasharguments and query string.
142 * @global string $FULLSCRIPT
143 * @name $FULLSCRIPT
144 */
11e7b506 145global $FULLSCRIPT;
e1d1b796 146
b37eac91 147/**
148 * Relative moodle script path '/course/view.php'
149 * @global string $SCRIPT
150 * @name $SCRIPT
151 */
11e7b506 152global $SCRIPT;
9d0dd812 153
36ec6afe 154 if (!isset($CFG->wwwroot)) {
a670108e 155 trigger_error('Fatal: $CFG->wwwroot is not configured! Exiting.');
36ec6afe 156 die;
157 }
75249234 158
a91b910e 159/// Detect CLI scripts - CLI scripts are executed from command line, do not have session and we do not want HTML in output
160 if (!defined('CLI_SCRIPT')) { // CLI_SCRIPT might be defined in 'fake' CLI scripts like admin/cron.php
161 if (isset($_SERVER['REMOTE_ADDR'])) {
162 define('CLI_SCRIPT', false);
163 } else {
b37eac91 164 /** @ignore */
a91b910e 165 define('CLI_SCRIPT', true);
166 }
167 }
168
133b5929 169/// sometimes default PHP settings are borked on shared hosting servers, I wonder why they have to do that??
170 @ini_set('precision', 14); // needed for upgrades and gradebook
171
172
a91b910e 173/// The current directory in PHP version 4.3.0 and above isn't necessarily the
174/// directory of the script when run from the command line. The require_once()
175/// would fail, so we'll have to chdir()
176 if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
177 chdir(dirname($_SERVER['argv'][0]));
178 }
179
180
220a90c5 181/// store settings from config.php in array in $CFG - we can use it later to detect problems and overrides
182 $CFG->config_php_settings = (array)$CFG;
183
9d0dd812 184/// Set httpswwwroot default value (this variable will replace $CFG->wwwroot
185/// inside some URLs used in HTTPSPAGEREQUIRED pages.
d3f9f1f8 186 $CFG->httpswwwroot = $CFG->wwwroot;
187
188 $CFG->libdir = $CFG->dirroot .'/lib';
189
17da2e6f 190 if (!isset($CFG->themedir)) {
191 $CFG->themedir = $CFG->dirroot.'/theme';
192 $CFG->themewww = $CFG->wwwroot.'/theme';
193 }
194
d3f9f1f8 195 require_once($CFG->libdir .'/setuplib.php'); // Functions that MUST be loaded first
9d0dd812 196
6800d78e
FM
197/// Time to start counting
198 init_performance_info();
199
b8cea9b2 200
74944b73 201/// If there are any errors in the standard libraries we want to know!
346b1a24 202 error_reporting(E_ALL);
f9903ed0 203
d7196099 204/// Just say no to link prefetching (Moz prefetching, Google Web Accelerator, others)
0a194c4c 205/// http://www.google.com/webmasters/faq.html#prefetchblock
d7196099 206 if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){
6800d78e 207 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Prefetch Forbidden');
d7196099 208 trigger_error('Prefetch request forbidden.');
209 exit;
210 }
211
dae73c05 212/// Define admin directory
dae73c05 213 if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
1040ea85 214 $CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
dae73c05 215 }
216
f33e1ed4 217 if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
218 $CFG->prefix = '';
219 }
a8a71844 220
75249234 221/// Load up standard libraries
6aaa17c7 222 require_once($CFG->libdir .'/textlib.class.php'); // Functions to handle multibyte strings
93d4a373 223 require_once($CFG->libdir .'/filterlib.php'); // Functions for filtering test as it is output
224 require_once($CFG->libdir .'/ajax/ajaxlib.php'); // Functions for managing our use of JavaScript and YUI
da8759cb 225 require_once($CFG->libdir .'/weblib.php'); // Functions for producing HTML
8aff8482 226 require_once($CFG->libdir .'/dmllib.php'); // Database access
7e13be08 227 require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions.
c4d0753b 228 require_once($CFG->libdir .'/accesslib.php'); // Access control functions
229 require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility
da8759cb 230 require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions
93d4a373 231 require_once($CFG->libdir .'/blocklib.php'); // Library for controlling blocks
0856223c 232 require_once($CFG->libdir .'/eventslib.php'); // Events functions
13534ef7 233 require_once($CFG->libdir .'/grouplib.php'); // Groups functions
6800d78e 234 require_once($CFG->libdir .'/sessionlib.php'); // All session and cookie related stuff
5ca3c838 235 require_once($CFG->libdir .'/editorlib.php'); // All text editor related functions and classes
a4c371ec 236
7fc1a27d 237 //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
238 //the problem is that we need specific version of quickforms and hacked excel files :-(
239 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
647a1a82 240 //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else
241 ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path'));
a4c371ec 242
251387d0 243/// set handler for uncought exceptions - equivalent to print_error() call
244 set_exception_handler('default_exception_handler');
245
fbf2c91e 246/// make sure PHP is not severly misconfigured
247 setup_validate_php_configuration();
248
f33e1ed4 249/// Connect to the database
250 setup_DB();
251
252/// Increase memory limits if possible
253 raise_memory_limit('96M'); // We should never NEED this much but just in case...
254
aa893d6b 255/// Disable errors for now - needed for installation when debug enabled in config.php
256 if (isset($CFG->debug)) {
257 $originalconfigdebug = $CFG->debug;
258 unset($CFG->debug);
259 } else {
260 $originalconfigdebug = -1;
261 }
262
74944b73 263/// Load up any configuration from the config table
c23b0ea1 264 try {
265 $CFG = get_config();
266 } catch (dml_read_exception $e) {
267 // most probably empty db, going to install soon
268 }
f9903ed0 269
a78bee28 270/// Verify upgrade is not running unless we are in a script that needs to execute in any case
271 if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
775f811a 272 if ($CFG->upgraderunning < time()) {
273 unset_config('upgraderunning');
274 } else {
275 print_error('upgraderunning');
276 }
277 }
278
ceff9307 279/// Turn on SQL logging if required
280 if (!empty($CFG->logsql)) {
f33e1ed4 281 $DB->set_logging(true);
ceff9307 282 }
1e3e716f 283
aa893d6b 284/// Prevent warnings from roles when upgrading with debug on
285 if (isset($CFG->debug)) {
286 $originaldatabasedebug = $CFG->debug;
287 unset($CFG->debug);
288 } else {
289 $originaldatabasedebug = -1;
6fbf8d8f 290 }
4fd7ccc0 291
292
cf8133c4 293/// For now, only needed under apache (and probably unstable in other contexts)
cf1348ca 294 if (function_exists('register_shutdown_function')) {
cf8133c4 295 register_shutdown_function('moodle_request_shutdown');
296 }
297
bac6d28a 298/// Defining the site
c23b0ea1 299 try {
300 $SITE = get_site();
301 } catch (dml_read_exception $e) {
302 $SITE = null;
303 }
304
305 if ($SITE) {
475e9de8 306 /**
307 * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1.
308 */
309 define('SITEID', $SITE->id);
508b76d9 310 /// And the 'default' course
311 $COURSE = clone($SITE); // For now. This will usually get reset later in require_login() etc.
475e9de8 312 } else {
313 /**
314 * @ignore
315 */
316 define('SITEID', 1);
508b76d9 317 /// And the 'default' course
318 $COURSE = new object; // no site created yet
319 $COURSE->id = 1;
475e9de8 320 }
475e9de8 321
7d0c81b3 322 // define SYSCONTEXTID in config.php if you want to save some queries (after install or upgrade!)
323 if (!defined('SYSCONTEXTID')) {
0ecff22d 324 get_system_context();
6dd34e93 325 }
475e9de8 326
aa893d6b 327/// Set error reporting back to normal
328 if ($originaldatabasedebug == -1) {
7eb0b60a 329 $CFG->debug = DEBUG_MINIMAL;
aa893d6b 330 } else {
331 $CFG->debug = $originaldatabasedebug;
332 }
333 if ($originalconfigdebug !== -1) {
6800d78e 334 $CFG->debug = $originalconfigdebug;
aa893d6b 335 }
336 unset($originalconfigdebug);
337 unset($originaldatabasedebug);
338 error_reporting($CFG->debug);
339
25338300 340
b3732604 341/// find out if PHP cofigured to display warnings
342 if (ini_get_bool('display_errors')) {
343 define('WARN_DISPLAY_ERRORS_ENABLED', true);
344 }
25338300 345/// If we want to display Moodle errors, then try and set PHP errors to match
6349a3ba 346 if (!isset($CFG->debugdisplay)) {
347 //keep it as is during installation
348 } else if (empty($CFG->debugdisplay)) {
25338300 349 @ini_set('display_errors', '0');
350 @ini_set('log_errors', '1');
351 } else {
352 @ini_set('display_errors', '1');
353 }
8f64ba04 354// Even when users want to see errors in the output,
355// some parts of Moodle cannot display them at all.
356// (Once we are XHTML strict compliant, debugdisplay
357// _must_ go away).
358 if (defined('MOODLE_SANE_OUTPUT')) {
359 @ini_set('display_errors', '0');
360 @ini_set('log_errors', '1');
361 }
25338300 362
7b9be84b 363/// Create the $PAGE global.
364 if (!empty($CFG->moodlepageclass)) {
365 $classname = $CFG->moodlepageclass;
366 } else {
367 $classname = 'moodle_page';
368 }
369 $PAGE = new $classname();
370 unset($classname);
371
f9955801 372/// detect unsupported upgrade jump as soon as possible - do not change anything, do not use system functions
2728a623 373 if (!empty($CFG->version) and $CFG->version < 2007101509) {
6d5a22b2 374 print_error('upgraderequires19', 'error');
f9955801 375 die;
376 }
377
419e1d93 378/// Shared-Memory cache init -- will set $MCACHE
379/// $MCACHE is a global object that offers at least add(), set() and delete()
380/// with similar semantics to the memcached PHP API http://php.net/memcache
392e7363 381/// Ensure we define rcache - so we can later check for it
382/// with a really fast and unambiguous $CFG->rcache === false
bb931a61 383 if (!empty($CFG->cachetype)) {
b1df0eb2 384 if (empty($CFG->rcache)) {
392e7363 385 $CFG->rcache = false;
386 } else {
387 $CFG->rcache = true;
388 }
389
390 // do not try to initialize if cache disabled
391 if (!$CFG->rcache) {
392 $CFG->cachetype = '';
393 }
394
bb931a61 395 if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) {
396 if (!init_memcached()) {
397 debugging("Error initialising memcached");
0a2925be 398 $CFG->cachetype = '';
399 $CFG->rcache = false;
bb931a61 400 }
392e7363 401 } else if ($CFG->cachetype === 'eaccelerator') {
bb931a61 402 if (!init_eaccelerator()) {
403 debugging("Error initialising eaccelerator cache");
0a2925be 404 $CFG->cachetype = '';
6800d78e 405 $CFG->rcache = false;
bb931a61 406 }
407 }
392e7363 408
bb931a61 409 } else { // just make sure it is defined
410 $CFG->cachetype = '';
392e7363 411 $CFG->rcache = false;
2142d492 412 }
aa893d6b 413
0182c65c 414/// Set a default enrolment configuration (see bug 1598)
415 if (!isset($CFG->enrol)) {
a51e2a7f 416 $CFG->enrol = 'manual';
0182c65c 417 }
418
ed8365d9 419/// Set default enabled enrolment plugins
420 if (!isset($CFG->enrol_plugins_enabled)) {
421 $CFG->enrol_plugins_enabled = 'manual';
422 }
423
2e6d4273 424/// File permissions on created directories in the $CFG->dataroot
425
426 if (empty($CFG->directorypermissions)) {
427 $CFG->directorypermissions = 0777; // Must be octal (that's why it's here)
428 }
1aa01caf 429 if (empty($CFG->filepermissions)) {
430 $CFG->filepermissions = ($CFG->directorypermissions & 0666); // strip execute flags
431 }
432/// better also set default umask because recursive mkdir() does not apply permissions recursively otherwise
433 umask(0000);
2e6d4273 434
820743c5 435/// Calculate and set $CFG->ostype to be used everywhere. Possible values are:
436/// - WINDOWS: for any Windows flavour.
437/// - UNIX: for the rest
438/// Also, $CFG->os can continue being used if more specialization is required
323edd4b 439 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
440 $CFG->ostype = 'WINDOWS';
441 } else {
442 $CFG->ostype = 'UNIX';
443 }
444 $CFG->os = PHP_OS;
820743c5 445
d21a5865 446/// Set up default frame target string, based on $CFG->framename
447 $CFG->frametarget = frametarget();
448
d6ead3a2 449/// Setup cache dir for Smarty and others
450 if (!file_exists($CFG->dataroot .'/cache')) {
451 make_upload_directory('cache');
452 }
453
de7e4ac9 454/// Configure ampersands in URLs
de7e4ac9 455 @ini_set('arg_separator.output', '&amp;');
456
e7aa5a88 457/// Work around for a PHP bug see MDL-11237
6800d78e 458 @ini_set('pcre.backtrack_limit', 20971520); // 20 MB
e7aa5a88 459
74944b73 460/// Location of standard files
da8759cb 461 $CFG->wordlist = $CFG->libdir .'/wordlist.txt';
462 $CFG->javascript = $CFG->libdir .'/javascript.php';
1040ea85 463 $CFG->moddata = 'moddata';
f9903ed0 464
294ce987 465/// A hack to get around magic_quotes_gpc being turned on
466/// It is strongly recommended to disable "magic_quotes_gpc"!
467 if (ini_get_bool('magic_quotes_gpc')) {
468 function stripslashes_deep($value) {
75249234 469 $value = is_array($value) ?
294ce987 470 array_map('stripslashes_deep', $value) :
471 stripslashes($value);
75249234 472 return $value;
24cc8ec9 473 }
294ce987 474 $_POST = array_map('stripslashes_deep', $_POST);
475 $_GET = array_map('stripslashes_deep', $_GET);
476 $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
477 $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
578dcc40 478 if (!empty($_SERVER['REQUEST_URI'])) {
294ce987 479 $_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']);
578dcc40 480 }
481 if (!empty($_SERVER['QUERY_STRING'])) {
294ce987 482 $_SERVER['QUERY_STRING'] = stripslashes($_SERVER['QUERY_STRING']);
578dcc40 483 }
484 if (!empty($_SERVER['HTTP_REFERER'])) {
294ce987 485 $_SERVER['HTTP_REFERER'] = stripslashes($_SERVER['HTTP_REFERER']);
578dcc40 486 }
487 if (!empty($_SERVER['PATH_INFO'])) {
294ce987 488 $_SERVER['PATH_INFO'] = stripslashes($_SERVER['PATH_INFO']);
578dcc40 489 }
490 if (!empty($_SERVER['PHP_SELF'])) {
294ce987 491 $_SERVER['PHP_SELF'] = stripslashes($_SERVER['PHP_SELF']);
578dcc40 492 }
493 if (!empty($_SERVER['PATH_TRANSLATED'])) {
294ce987 494 $_SERVER['PATH_TRANSLATED'] = stripslashes($_SERVER['PATH_TRANSLATED']);
b62dff99 495 }
f98cfb53 496 }
497
498/// neutralise nasty chars in PHP_SELF
499 if (isset($_SERVER['PHP_SELF'])) {
500 $phppos = strpos($_SERVER['PHP_SELF'], '.php');
501 if ($phppos !== false) {
502 $_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, $phppos+4);
578dcc40 503 }
f98cfb53 504 unset($phppos);
aa6af0f8 505 }
7a302afc 506
9bda43e6 507/// initialise ME's
11e7b506 508 initialise_fullme();
f9903ed0 509
9bda43e6 510/// start session and prepare global $SESSION, $USER
511 session_get_instance();
512 $SESSION = &$_SESSION['SESSION'];
513 $USER = &$_SESSION['USER'];
514
18b9d664 515/// Load up theme variables (colours etc)
86cc3e75 516
6800d78e 517 $CFG->httpsthemewww = $CFG->themewww;
86cc3e75 518
18b9d664 519 if (isset($_GET['theme'])) {
32e2b302 520 if ($CFG->allowthemechangeonurl || confirm_sesskey()) {
a44091bf 521 $themename = clean_param($_GET['theme'], PARAM_SAFEDIR);
522 if (($themename != '') and file_exists($CFG->themedir.'/'.$themename)) {
523 $SESSION->theme = $themename;
18b9d664 524 }
a44091bf 525 unset($themename);
18b9d664 526 }
527 }
528
529 if (!isset($CFG->theme)) {
34137668 530 $CFG->theme = 'standardwhite';
18b9d664 531 }
532
75249234 533/// Set language/locale of printed times. If user has chosen a language that
534/// that is different from the site language, then use the locale specified
339bb559 535/// in the language file. Otherwise, if the admin hasn't specified a locale
75249234 536/// then use the one from the default language. Otherwise (and this is the
339bb559 537/// majority of cases), use the stored locale specified by admin.
6800d78e 538 if (isset($_GET['lang']) && ($lang = clean_param($_GET['lang'], PARAM_SAFEDIR))) {
6ec7ca0f 539 if (file_exists($CFG->dataroot .'/lang/'. $lang) or file_exists($CFG->dirroot .'/lang/'. $lang)) {
32c60ce3 540 $SESSION->lang = $lang;
6800d78e 541 } else if (file_exists($CFG->dataroot.'/lang/'.$lang.'_utf8') or
748390cd 542 file_exists($CFG->dirroot .'/lang/'.$lang.'_utf8')) {
543 $SESSION->lang = $lang.'_utf8';
32c60ce3 544 }
3e9b5d5a 545 }
ab036ed9 546
547 setup_lang_from_browser();
548
6ec7ca0f 549 unset($lang);
ab036ed9 550
16ba7351 551 if (empty($CFG->lang)) {
6ec7ca0f 552 if (empty($SESSION->lang)) {
810944af 553 $CFG->lang = 'en_utf8';
6ec7ca0f 554 } else {
555 $CFG->lang = $SESSION->lang;
556 }
16ba7351 557 }
6800d78e 558
c13a5e71 559 // We used to call moodle_setlocale() and theme_setup() here, even though they
560 // would be called again from require_login or $PAGE->set_course. As an experiment
561 // I am going to try removing those calls. With luck it will help us find and
562 // fix a few bugs where scripts do not initialise thigns properly, wihtout causing
563 // too much grief.
1040ea85 564
1b813f5c 565 if (!empty($CFG->guestloginbutton)) {
566 if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation
567 if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest)
568 if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
569 (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
570 if ($user = get_complete_user_data("username", "w3cvalidator")) {
571 $user->ignoresesskey = true;
572 } else {
573 $user = guest_user();
574 }
575 session_set_user($user);
9610a66e 576 }
577 }
578 }
579 }
580
a559eee6 581/// Apache log intergration. In apache conf file one can use ${MOODULEUSER}n in
582/// LogFormat to get the current logged in username in moodle.
ac0b19ff 583 if ($USER && function_exists('apache_note')
20e9d26f 584 && !empty($CFG->apacheloguser) && isset($USER->username)) {
2b287cac 585 $apachelog_userid = $USER->id;
ac0b19ff 586 $apachelog_username = clean_filename($USER->username);
587 $apachelog_name = '';
588 if (isset($USER->firstname)) {
589 // We can assume both will be set
590 // - even if to empty.
591 $apachelog_name = clean_filename($USER->firstname . " " .
592 $USER->lastname);
593 }
b7b64ff2 594 if (session_is_loggedinas()) {
595 $realuser = session_get_realuser();
6132768e 596 $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username);
597 $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name);
598 $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid);
96e19e7b 599 }
5c5c16bb 600 switch ($CFG->apacheloguser) {
601 case 3:
2b287cac 602 $logname = $apachelog_username;
5c5c16bb 603 break;
604 case 2:
2b287cac 605 $logname = $apachelog_name;
5c5c16bb 606 break;
607 case 1:
608 default:
2b287cac 609 $logname = $apachelog_userid;
5c5c16bb 610 break;
611 }
612 apache_note('MOODLEUSER', $logname);
a559eee6 613 }
614
5982740d 615/// Adjust ALLOWED_TAGS
616 adjust_allowed_tags();
617
18259d4f 618/// Use a custom script replacement if one exists
619 if (!empty($CFG->customscripts)) {
620 if (($customscript = custom_script_path()) !== false) {
2b0b32d8 621 require ($customscript);
18259d4f 622 }
623 }
624
5035228f 625 // in the first case, ip in allowed list will be performed first
626 // for example, client IP is 192.168.1.1
627 // 192.168 subnet is an entry in allowed list
d255c6e9 628 // 192.168.1.1 is banned in blocked list
5035228f 629 // This ip will be banned finally
d255c6e9 630 if (!empty($CFG->allowbeforeblock)) { // allowed list processed before blocked list?
631 if (!empty($CFG->allowedip)) {
632 if (!remoteip_in_list($CFG->allowedip)) {
633 die(get_string('ipblocked', 'admin'));
634 }
ab99c8f0 635 }
d255c6e9 636 // need further check, client ip may a part of
637 // allowed subnet, but a IP address are listed
5035228f 638 // in blocked list.
d255c6e9 639 if (!empty($CFG->blockedip)) {
640 if (remoteip_in_list($CFG->blockedip)) {
641 die(get_string('ipblocked', 'admin'));
642 }
5035228f 643 }
d255c6e9 644
5035228f 645 } else {
646 // in this case, IPs in blocked list will be performed first
647 // for example, client IP is 192.168.1.1
648 // 192.168 subnet is an entry in blocked list
d255c6e9 649 // 192.168.1.1 is allowed in allowed list
5035228f 650 // This ip will be allowed finally
d255c6e9 651 if (!empty($CFG->blockedip)) {
652 if (remoteip_in_list($CFG->blockedip)) {
653 // if the allowed ip list is not empty
654 // IPs are not included in the allowed list will be
655 // blocked too
656 if (!empty($CFG->allowedip)) {
657 if (!remoteip_in_list($CFG->allowedip)) {
658 die(get_string('ipblocked', 'admin'));
659 }
660 } else {
661 die(get_string('ipblocked', 'admin'));
662 }
663 }
5035228f 664 }
d255c6e9 665 // if blocked list is null
666 // allowed list should be tested
667 if(!empty($CFG->allowedip)) {
668 if (!remoteip_in_list($CFG->allowedip)) {
669 die(get_string('ipblocked', 'admin'));
670 }
5035228f 671 }
d255c6e9 672
ab99c8f0 673 }
674
810944af 675/// note: we can not block non utf-8 installatrions here, because empty mysql database
676/// might be converted to utf-8 in admin/index.php during installation
d3f9f1f8 677?>