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