Commit | Line | Data |
---|---|---|
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 | */ |
41 | global $CFG; | |
42 | ||
60f2c866 PS |
43 | // Set up some paths. |
44 | $CFG->libdir = $CFG->dirroot .'/lib'; | |
45 | ||
46 | // exact version of currently used yui2 and 3 library | |
c595847d PS |
47 | $CFG->yui2version = '2.8.1'; |
48 | $CFG->yui3version = '3.1.1'; | |
60f2c866 PS |
49 | |
50 | // special support for highly optimised scripts that do not need libraries and DB connection | |
51 | if (defined('ABORT_AFTER_CONFIG')) { | |
52 | if (!defined('ABORT_AFTER_CONFIG_CANCEL')) { | |
0037dcc9 PS |
53 | // hide debugging if not enabled in config.php - we do not want to disclose sensitive info |
54 | if (isset($CFG->debug)) { | |
55 | error_reporting($CFG->debug); | |
56 | } else { | |
57 | error_reporting(0); | |
58 | } | |
59 | if (empty($CFG->debugdisplay)) { | |
60 | @ini_set('display_errors', '0'); | |
61 | @ini_set('log_errors', '1'); | |
62 | } else { | |
63 | @ini_set('display_errors', '1'); | |
64 | } | |
60f2c866 PS |
65 | require_once("$CFG->dirroot/lib/configonlylib.php"); |
66 | return; | |
67 | } | |
68 | } | |
69 | ||
e1d1b796 | 70 | /** |
71 | * Database connection. Used for all access to the database. | |
72 | * @global moodle_database $DB | |
b37eac91 | 73 | * @name $DB |
e1d1b796 | 74 | */ |
75 | global $DB; | |
76 | ||
77 | /** | |
78 | * Moodle's wrapper round PHP's $_SESSION. | |
79 | * | |
80 | * @global object $SESSION | |
b37eac91 | 81 | * @name $SESSION |
e1d1b796 | 82 | */ |
83 | global $SESSION; | |
84 | ||
85 | /** | |
86 | * Holds the user table record for the current user. Will be the 'guest' | |
87 | * user record for people who are not logged in. | |
88 | * | |
89 | * $USER is stored in the session. | |
da8759cb | 90 | * |
735b8567 | 91 | * Items found in the user record: |
da8759cb | 92 | * - $USER->emailstop - Does the user want email sent to them? |
93 | * - $USER->email - The user's email address. | |
94 | * - $USER->id - The unique integer identified of this user in the 'user' table. | |
95 | * - $USER->email - The user's email address. | |
96 | * - $USER->firstname - The user's first name. | |
97 | * - $USER->lastname - The user's last name. | |
98 | * - $USER->username - The user's login username. | |
99 | * - $USER->secret - The user's ?. | |
100 | * - $USER->lang - The user's language choice. | |
101 | * | |
e1d1b796 | 102 | * @global object $USER |
b37eac91 | 103 | * @name $USER |
da8759cb | 104 | */ |
674fb525 | 105 | global $USER; |
e1d1b796 | 106 | |
c13a5e71 | 107 | /** |
108 | * A central store of information about the current page we are | |
109 | * generating in response to the user's request. | |
110 | * | |
111 | * @global moodle_page $PAGE | |
b37eac91 | 112 | * @name $PAGE |
c13a5e71 | 113 | */ |
114 | global $PAGE; | |
115 | ||
da8759cb | 116 | /** |
e1d1b796 | 117 | * The current course. An alias for $PAGE->course. |
118 | * @global object $COURSE | |
b37eac91 | 119 | * @name $COURSE |
da8759cb | 120 | */ |
121 | global $COURSE; | |
e1d1b796 | 122 | |
da8759cb | 123 | /** |
78946b9b | 124 | * $OUTPUT is an instance of core_renderer or one of its subclasses. Use |
34a2777c | 125 | * it to generate HTML for output. |
da8759cb | 126 | * |
c84a2dbe | 127 | * $OUTPUT is initialised the first time it is used. See {@link bootstrap_renderer} |
128 | * for the magic that does that. After $OUTPUT has been initialised, any attempt | |
129 | * to change something that affects the current theme ($PAGE->course, logged in use, | |
130 | * httpsrequried ... will result in an exception.) | |
34a2777c | 131 | * |
cbcc9852 PS |
132 | * Please note the $OUTPUT is replacing the old global $THEME object. |
133 | * | |
34a2777c | 134 | * @global object $OUTPUT |
135 | * @name $OUTPUT | |
136 | */ | |
137 | global $OUTPUT; | |
138 | ||
9d0dd812 | 139 | /** |
e1d1b796 | 140 | * Shared memory cache. |
141 | * @global object $MCACHE | |
b37eac91 | 142 | * @name $MCACHE |
e1d1b796 | 143 | */ |
144 | global $MCACHE; | |
145 | ||
146 | /** | |
147 | * A global to define if the page being displayed must run under HTTPS. | |
6800d78e | 148 | * |
e1d1b796 | 149 | * Its primary goal is to allow 100% HTTPS pages when $CFG->loginhttps is enabled. Default to false. |
b7009474 | 150 | * Its enabled only by the $PAGE->https_required() function and used in some pages to update some URLs |
b37eac91 | 151 | * |
152 | * @global bool $HTTPSPAGEREQUIRED | |
153 | * @name $HTTPSPAGEREQUIRED | |
154 | */ | |
9d0dd812 | 155 | global $HTTPSPAGEREQUIRED; |
156 | ||
b37eac91 | 157 | /** |
158 | * Full script path including all params, slash arguments, scheme and host. | |
159 | * @global string $FULLME | |
160 | * @name $FULLME | |
161 | */ | |
11e7b506 | 162 | global $FULLME; |
e1d1b796 | 163 | |
b37eac91 | 164 | /** |
165 | * Script path including query string and slash arguments without host. | |
166 | * @global string $ME | |
167 | * @name $ME | |
168 | */ | |
11e7b506 | 169 | global $ME; |
e1d1b796 | 170 | |
b37eac91 | 171 | /** |
172 | * $FULLME without slasharguments and query string. | |
173 | * @global string $FULLSCRIPT | |
174 | * @name $FULLSCRIPT | |
175 | */ | |
11e7b506 | 176 | global $FULLSCRIPT; |
e1d1b796 | 177 | |
b37eac91 | 178 | /** |
179 | * Relative moodle script path '/course/view.php' | |
180 | * @global string $SCRIPT | |
181 | * @name $SCRIPT | |
182 | */ | |
11e7b506 | 183 | global $SCRIPT; |
9d0dd812 | 184 | |
2e9b772f PS |
185 | // Scripts may request no debug and error messages in output |
186 | // please note it must be defined before including the config.php script | |
187 | // and in some cases you also need to set custom default exception handler | |
188 | if (!defined('NO_DEBUG_DISPLAY')) { | |
189 | define('NO_DEBUG_DISPLAY', false); | |
190 | } | |
191 | ||
ccda6d68 PS |
192 | // wwwroot is mandatory |
193 | if (!isset($CFG->wwwroot)) { | |
194 | // trigger_error() is not correct here, no need to log this | |
47c2811b PS |
195 | header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); |
196 | echo('Fatal: $CFG->wwwroot is not configured! Exiting.'); | |
197 | exit(1); | |
ccda6d68 | 198 | } |
75249234 | 199 | |
4dffc775 PS |
200 | // Detect CLI scripts - CLI scripts are executed from command line, do not have session and we do not want HTML in output |
201 | if (!defined('CLI_SCRIPT')) { // CLI_SCRIPT might be defined in 'fake' CLI scripts like admin/cron.php | |
202 | if (isset($_SERVER['REMOTE_ADDR'])) { | |
203 | define('CLI_SCRIPT', false); | |
204 | } else { | |
205 | /** @ignore */ | |
206 | define('CLI_SCRIPT', true); | |
a91b910e | 207 | } |
4dffc775 | 208 | } |
a91b910e | 209 | |
1adaa404 PS |
210 | // Detect ajax scripts - they are similar to CLI because we can not redirect, output html, etc. |
211 | if (!defined('AJAX_SCRIPT')) { | |
212 | define('AJAX_SCRIPT', false); | |
213 | } | |
214 | ||
4dffc775 PS |
215 | // sometimes default PHP settings are borked on shared hosting servers, I wonder why they have to do that?? |
216 | @ini_set('precision', 14); // needed for upgrades and gradebook | |
133b5929 | 217 | |
4dffc775 PS |
218 | // The current directory in PHP version 4.3.0 and above isn't necessarily the |
219 | // directory of the script when run from the command line. The require_once() | |
220 | // would fail, so we'll have to chdir() | |
221 | if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) { | |
222 | chdir(dirname($_SERVER['argv'][0])); | |
223 | } | |
133b5929 | 224 | |
4dffc775 PS |
225 | // Store settings from config.php in array in $CFG - we can use it later to detect problems and overrides |
226 | $CFG->config_php_settings = (array)$CFG; | |
12bb0c3e PS |
227 | // Forced plugin settings override values from config_plugins table |
228 | unset($CFG->config_php_settings['forced_plugin_settings']); | |
229 | if (!isset($CFG->forced_plugin_settings)) { | |
230 | $CFG->forced_plugin_settings = array(); | |
231 | } | |
4dffc775 PS |
232 | // Set httpswwwroot default value (this variable will replace $CFG->wwwroot |
233 | // inside some URLs used in HTTPSPAGEREQUIRED pages. | |
234 | $CFG->httpswwwroot = $CFG->wwwroot; | |
d3f9f1f8 | 235 | |
4dffc775 | 236 | require_once($CFG->libdir .'/setuplib.php'); // Functions that MUST be loaded first |
17da2e6f | 237 | |
4dffc775 PS |
238 | // Time to start counting |
239 | init_performance_info(); | |
b7009474 | 240 | |
4dffc775 PS |
241 | // Put $OUTPUT in place, so errors can be displayed. |
242 | $OUTPUT = new bootstrap_renderer(); | |
9d0dd812 | 243 | |
4c1c9175 | 244 | // set handler for uncaught exceptions - equivalent to print_error() call |
4dffc775 | 245 | set_exception_handler('default_exception_handler'); |
6800d78e | 246 | |
4dffc775 PS |
247 | // If there are any errors in the standard libraries we want to know! |
248 | error_reporting(E_ALL); | |
c84a2dbe | 249 | |
4dffc775 PS |
250 | // Just say no to link prefetching (Moz prefetching, Google Web Accelerator, others) |
251 | // http://www.google.com/webmasters/faq.html#prefetchblock | |
252 | if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){ | |
253 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Prefetch Forbidden'); | |
254 | echo('Prefetch request forbidden.'); | |
255 | exit(1); | |
256 | } | |
b8cea9b2 | 257 | |
4dffc775 PS |
258 | // Define admin directory |
259 | if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php | |
260 | $CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot | |
261 | } | |
f9903ed0 | 262 | |
4dffc775 PS |
263 | if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php |
264 | $CFG->prefix = ''; | |
265 | } | |
d7196099 | 266 | |
38c253f9 PS |
267 | // location of all languages except core English pack |
268 | if (!isset($CFG->langotherroot)) { | |
269 | $CFG->langotherroot = $CFG->dataroot.'/lang'; | |
270 | } | |
271 | ||
272 | // location of local lang pack customisations (dirs with _local suffix) | |
273 | if (!isset($CFG->langlocalroot)) { | |
274 | $CFG->langlocalroot = $CFG->dataroot.'/lang'; | |
275 | } | |
276 | ||
3d732d84 EL |
277 | //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else |
278 | //the problem is that we need specific version of quickforms and hacked excel files :-( | |
279 | ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); | |
280 | //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else | |
281 | //please note zend library is supposed to be used only from web service protocol classes, it may be removed in future | |
282 | ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path')); | |
283 | ||
4dffc775 PS |
284 | // Load up standard libraries |
285 | require_once($CFG->libdir .'/textlib.class.php'); // Functions to handle multibyte strings | |
286 | require_once($CFG->libdir .'/filterlib.php'); // Functions for filtering test as it is output | |
287 | require_once($CFG->libdir .'/ajax/ajaxlib.php'); // Functions for managing our use of JavaScript and YUI | |
288 | require_once($CFG->libdir .'/weblib.php'); // Functions relating to HTTP and content | |
289 | require_once($CFG->libdir .'/outputlib.php'); // Functions for generating output | |
290 | require_once($CFG->libdir .'/navigationlib.php'); // Class for generating Navigation structure | |
291 | require_once($CFG->libdir .'/dmllib.php'); // Database access | |
292 | require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions. | |
293 | require_once($CFG->libdir .'/accesslib.php'); // Access control functions | |
294 | require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility | |
295 | require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions | |
296 | require_once($CFG->libdir .'/pagelib.php'); // Library that defines the moodle_page class, used for $PAGE | |
297 | require_once($CFG->libdir .'/blocklib.php'); // Library for controlling blocks | |
298 | require_once($CFG->libdir .'/eventslib.php'); // Events functions | |
299 | require_once($CFG->libdir .'/grouplib.php'); // Groups functions | |
300 | require_once($CFG->libdir .'/sessionlib.php'); // All session and cookie related stuff | |
301 | require_once($CFG->libdir .'/editorlib.php'); // All text editor related functions and classes | |
7c7d3afa | 302 | require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions |
4dffc775 | 303 | |
4dffc775 PS |
304 | // make sure PHP is not severly misconfigured |
305 | setup_validate_php_configuration(); | |
306 | ||
307 | // Increase memory limits if possible | |
308 | raise_memory_limit('96M'); // We should never NEED this much but just in case... | |
309 | ||
310 | // Connect to the database | |
311 | setup_DB(); | |
312 | ||
313 | // Disable errors for now - needed for installation when debug enabled in config.php | |
314 | if (isset($CFG->debug)) { | |
315 | $originalconfigdebug = $CFG->debug; | |
316 | unset($CFG->debug); | |
317 | } else { | |
318 | $originalconfigdebug = -1; | |
319 | } | |
dae73c05 | 320 | |
4dffc775 | 321 | // Load up any configuration from the config table |
12bb0c3e | 322 | initialise_cfg(); |
a8a71844 | 323 | |
4dffc775 PS |
324 | // Verify upgrade is not running unless we are in a script that needs to execute in any case |
325 | if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) { | |
326 | if ($CFG->upgraderunning < time()) { | |
327 | unset_config('upgraderunning'); | |
aa893d6b | 328 | } else { |
4dffc775 | 329 | print_error('upgraderunning'); |
aa893d6b | 330 | } |
4dffc775 | 331 | } |
aa893d6b | 332 | |
4dffc775 PS |
333 | // Turn on SQL logging if required |
334 | if (!empty($CFG->logsql)) { | |
335 | $DB->set_logging(true); | |
336 | } | |
f9903ed0 | 337 | |
4dffc775 PS |
338 | // Prevent warnings from roles when upgrading with debug on |
339 | if (isset($CFG->debug)) { | |
340 | $originaldatabasedebug = $CFG->debug; | |
341 | unset($CFG->debug); | |
342 | } else { | |
343 | $originaldatabasedebug = -1; | |
344 | } | |
775f811a | 345 | |
1e3e716f | 346 | |
4dffc775 PS |
347 | // For now, only needed under apache (and probably unstable in other contexts) |
348 | if (function_exists('register_shutdown_function')) { | |
349 | register_shutdown_function('moodle_request_shutdown'); | |
350 | } | |
4fd7ccc0 | 351 | |
4dffc775 PS |
352 | // Defining the site |
353 | try { | |
354 | $SITE = get_site(); | |
4dffc775 PS |
355 | /** |
356 | * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1. | |
357 | */ | |
358 | define('SITEID', $SITE->id); | |
7b51fb61 PS |
359 | // And the 'default' course - this will usually get reset later in require_login() etc. |
360 | $COURSE = clone($SITE); | |
361 | } catch (dml_read_exception $e) { | |
362 | $SITE = null; | |
363 | if (empty($CFG->version)) { | |
364 | // we are just installing | |
365 | /** | |
366 | * @ignore | |
367 | */ | |
368 | define('SITEID', 1); | |
369 | // And the 'default' course | |
370 | $COURSE = new object(); // no site created yet | |
371 | $COURSE->id = 1; | |
372 | } else { | |
373 | throw $e; | |
374 | } | |
4dffc775 | 375 | } |
cf8133c4 | 376 | |
4dffc775 PS |
377 | // define SYSCONTEXTID in config.php if you want to save some queries (after install or upgrade!) |
378 | if (!defined('SYSCONTEXTID')) { | |
379 | get_system_context(); | |
380 | } | |
c23b0ea1 | 381 | |
4dffc775 PS |
382 | // Set error reporting back to normal |
383 | if ($originaldatabasedebug == -1) { | |
384 | $CFG->debug = DEBUG_MINIMAL; | |
385 | } else { | |
386 | $CFG->debug = $originaldatabasedebug; | |
387 | } | |
388 | if ($originalconfigdebug !== -1) { | |
389 | $CFG->debug = $originalconfigdebug; | |
390 | } | |
391 | unset($originalconfigdebug); | |
392 | unset($originaldatabasedebug); | |
393 | error_reporting($CFG->debug); | |
394 | ||
12bb0c3e | 395 | // find out if PHP configured to display warnings, |
4dffc775 PS |
396 | // this is a security problem because some moodle scripts may |
397 | // disclose sensitive information | |
398 | if (ini_get_bool('display_errors')) { | |
399 | define('WARN_DISPLAY_ERRORS_ENABLED', true); | |
400 | } | |
401 | // If we want to display Moodle errors, then try and set PHP errors to match | |
402 | if (!isset($CFG->debugdisplay)) { | |
403 | // keep it "as is" during installation | |
404 | } else if (NO_DEBUG_DISPLAY) { | |
405 | // some parts of Moodle cannot display errors and debug at all. | |
406 | @ini_set('display_errors', '0'); | |
407 | @ini_set('log_errors', '1'); | |
408 | } else if (empty($CFG->debugdisplay)) { | |
409 | @ini_set('display_errors', '0'); | |
410 | @ini_set('log_errors', '1'); | |
411 | } else { | |
412 | // This is very problematic in XHTML strict mode! | |
413 | @ini_set('display_errors', '1'); | |
414 | } | |
475e9de8 | 415 | |
4dffc775 PS |
416 | // detect unsupported upgrade jump as soon as possible - do not change anything, do not use system functions |
417 | if (!empty($CFG->version) and $CFG->version < 2007101509) { | |
418 | print_error('upgraderequires19', 'error'); | |
419 | die; | |
420 | } | |
475e9de8 | 421 | |
4dffc775 PS |
422 | // Shared-Memory cache init -- will set $MCACHE |
423 | // $MCACHE is a global object that offers at least add(), set() and delete() | |
424 | // with similar semantics to the memcached PHP API http://php.net/memcache | |
425 | // Ensure we define rcache - so we can later check for it | |
426 | // with a really fast and unambiguous $CFG->rcache === false | |
427 | if (!empty($CFG->cachetype)) { | |
428 | if (empty($CFG->rcache)) { | |
429 | $CFG->rcache = false; | |
25338300 | 430 | } else { |
4dffc775 | 431 | $CFG->rcache = true; |
25338300 | 432 | } |
433 | ||
4dffc775 PS |
434 | // do not try to initialize if cache disabled |
435 | if (!$CFG->rcache) { | |
436 | $CFG->cachetype = ''; | |
f9955801 | 437 | } |
438 | ||
4dffc775 PS |
439 | if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) { |
440 | if (!init_memcached()) { | |
441 | debugging("Error initialising memcached"); | |
442 | $CFG->cachetype = ''; | |
392e7363 | 443 | $CFG->rcache = false; |
392e7363 | 444 | } |
4dffc775 PS |
445 | } else if ($CFG->cachetype === 'eaccelerator') { |
446 | if (!init_eaccelerator()) { | |
447 | debugging("Error initialising eaccelerator cache"); | |
392e7363 | 448 | $CFG->cachetype = ''; |
4dffc775 | 449 | $CFG->rcache = false; |
392e7363 | 450 | } |
2142d492 | 451 | } |
aa893d6b | 452 | |
4dffc775 PS |
453 | } else { // just make sure it is defined |
454 | $CFG->cachetype = ''; | |
455 | $CFG->rcache = false; | |
456 | } | |
0182c65c | 457 | |
4dffc775 PS |
458 | // File permissions on created directories in the $CFG->dataroot |
459 | if (empty($CFG->directorypermissions)) { | |
460 | $CFG->directorypermissions = 0777; // Must be octal (that's why it's here) | |
461 | } | |
462 | if (empty($CFG->filepermissions)) { | |
463 | $CFG->filepermissions = ($CFG->directorypermissions & 0666); // strip execute flags | |
464 | } | |
465 | // better also set default umask because recursive mkdir() does not apply permissions recursively otherwise | |
466 | umask(0000); | |
467 | ||
468 | // Calculate and set $CFG->ostype to be used everywhere. Possible values are: | |
469 | // - WINDOWS: for any Windows flavour. | |
470 | // - UNIX: for the rest | |
471 | // Also, $CFG->os can continue being used if more specialization is required | |
472 | if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) { | |
473 | $CFG->ostype = 'WINDOWS'; | |
474 | } else { | |
475 | $CFG->ostype = 'UNIX'; | |
476 | } | |
477 | $CFG->os = PHP_OS; | |
820743c5 | 478 | |
4dffc775 PS |
479 | // Setup cache dir for Smarty and others |
480 | if (!file_exists($CFG->dataroot .'/cache')) { | |
481 | make_upload_directory('cache'); | |
482 | } | |
d6ead3a2 | 483 | |
4dffc775 PS |
484 | // Configure ampersands in URLs |
485 | @ini_set('arg_separator.output', '&'); | |
de7e4ac9 | 486 | |
4dffc775 PS |
487 | // Work around for a PHP bug see MDL-11237 |
488 | @ini_set('pcre.backtrack_limit', 20971520); // 20 MB | |
e7aa5a88 | 489 | |
4dffc775 | 490 | // Location of standard files |
4c1c9175 PS |
491 | $CFG->wordlist = $CFG->libdir .'/wordlist.txt'; |
492 | $CFG->moddata = 'moddata'; | |
f9903ed0 | 493 | |
4dffc775 PS |
494 | // Create the $PAGE global. |
495 | if (!empty($CFG->moodlepageclass)) { | |
496 | $classname = $CFG->moodlepageclass; | |
497 | } else { | |
498 | $classname = 'moodle_page'; | |
499 | } | |
500 | $PAGE = new $classname(); | |
501 | unset($classname); | |
502 | ||
503 | // A hack to get around magic_quotes_gpc being turned on | |
504 | // It is strongly recommended to disable "magic_quotes_gpc"! | |
505 | if (ini_get_bool('magic_quotes_gpc')) { | |
506 | function stripslashes_deep($value) { | |
507 | $value = is_array($value) ? | |
508 | array_map('stripslashes_deep', $value) : | |
509 | stripslashes($value); | |
510 | return $value; | |
c84a2dbe | 511 | } |
4dffc775 PS |
512 | $_POST = array_map('stripslashes_deep', $_POST); |
513 | $_GET = array_map('stripslashes_deep', $_GET); | |
514 | $_COOKIE = array_map('stripslashes_deep', $_COOKIE); | |
515 | $_REQUEST = array_map('stripslashes_deep', $_REQUEST); | |
516 | if (!empty($_SERVER['REQUEST_URI'])) { | |
517 | $_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']); | |
f98cfb53 | 518 | } |
4dffc775 PS |
519 | if (!empty($_SERVER['QUERY_STRING'])) { |
520 | $_SERVER['QUERY_STRING'] = stripslashes($_SERVER['QUERY_STRING']); | |
521 | } | |
522 | if (!empty($_SERVER['HTTP_REFERER'])) { | |
523 | $_SERVER['HTTP_REFERER'] = stripslashes($_SERVER['HTTP_REFERER']); | |
aa6af0f8 | 524 | } |
4dffc775 PS |
525 | if (!empty($_SERVER['PATH_INFO'])) { |
526 | $_SERVER['PATH_INFO'] = stripslashes($_SERVER['PATH_INFO']); | |
527 | } | |
528 | if (!empty($_SERVER['PHP_SELF'])) { | |
529 | $_SERVER['PHP_SELF'] = stripslashes($_SERVER['PHP_SELF']); | |
530 | } | |
531 | if (!empty($_SERVER['PATH_TRANSLATED'])) { | |
532 | $_SERVER['PATH_TRANSLATED'] = stripslashes($_SERVER['PATH_TRANSLATED']); | |
533 | } | |
534 | } | |
7a302afc | 535 | |
4dffc775 PS |
536 | // neutralise nasty chars in PHP_SELF |
537 | if (isset($_SERVER['PHP_SELF'])) { | |
538 | $phppos = strpos($_SERVER['PHP_SELF'], '.php'); | |
539 | if ($phppos !== false) { | |
540 | $_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, $phppos+4); | |
18b9d664 | 541 | } |
4dffc775 PS |
542 | unset($phppos); |
543 | } | |
544 | ||
545 | // initialise ME's | |
546 | initialise_fullme(); | |
547 | ||
548 | // start session and prepare global $SESSION, $USER | |
549 | session_get_instance(); | |
550 | $SESSION = &$_SESSION['SESSION']; | |
551 | $USER = &$_SESSION['USER']; | |
18b9d664 | 552 | |
4dffc775 | 553 | // Process theme change in the URL. |
998999e7 PS |
554 | if (!empty($CFG->allowthemechangeonurl) and !empty($_GET['theme'])) { |
555 | // we have to use _GET directly because we do not want this to interfere with _POST | |
556 | $urlthemename = optional_param('theme', '', PARAM_SAFEDIR); | |
4dffc775 | 557 | try { |
998999e7 PS |
558 | $themeconfig = theme_config::load($urlthemename); |
559 | // Makes sure the theme can be loaded without errors. | |
560 | if ($themeconfig->name === $urlthemename) { | |
561 | $SESSION->theme = $urlthemename; | |
562 | } else { | |
563 | unset($SESSION->theme); | |
564 | } | |
565 | unset($themeconfig); | |
566 | unset($urlthemename); | |
4dffc775 PS |
567 | } catch (Exception $e) { |
568 | debugging('Failed to set the theme from the URL.', DEBUG_DEVELOPER, $e->getTrace()); | |
18b9d664 | 569 | } |
4dffc775 PS |
570 | } |
571 | unset($urlthemename); | |
18b9d664 | 572 | |
4dffc775 PS |
573 | // Ensure a valid theme is set. |
574 | if (!isset($CFG->theme)) { | |
575 | $CFG->theme = 'standardwhite'; | |
576 | } | |
577 | ||
578 | // Set language/locale of printed times. If user has chosen a language that | |
579 | // that is different from the site language, then use the locale specified | |
580 | // in the language file. Otherwise, if the admin hasn't specified a locale | |
581 | // then use the one from the default language. Otherwise (and this is the | |
582 | // majority of cases), use the stored locale specified by admin. | |
4000129f PS |
583 | // note: do not accept lang parameter from POST |
584 | if (isset($_GET['lang']) and ($lang = optional_param('lang', '', PARAM_SAFEDIR))) { | |
ecd7978c | 585 | if (get_string_manager()->translation_exists($lang, false)) { |
4dffc775 | 586 | $SESSION->lang = $lang; |
3e9b5d5a | 587 | } |
4dffc775 PS |
588 | } |
589 | unset($lang); | |
ab036ed9 | 590 | |
4dffc775 | 591 | setup_lang_from_browser(); |
ab036ed9 | 592 | |
4dffc775 PS |
593 | if (empty($CFG->lang)) { |
594 | if (empty($SESSION->lang)) { | |
3a915b06 | 595 | $CFG->lang = 'en'; |
4dffc775 PS |
596 | } else { |
597 | $CFG->lang = $SESSION->lang; | |
16ba7351 | 598 | } |
4dffc775 | 599 | } |
6800d78e | 600 | |
00dadbe1 PS |
601 | // Set the default site locale, a lot of the stuff may depend on this |
602 | // it is definitely too late to call this first in require_login()! | |
603 | moodle_setlocale(); | |
4dffc775 PS |
604 | |
605 | if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) { | |
606 | if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation | |
607 | if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest) | |
608 | if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or | |
609 | (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) { | |
610 | if ($user = get_complete_user_data("username", "w3cvalidator")) { | |
611 | $user->ignoresesskey = true; | |
612 | } else { | |
613 | $user = guest_user(); | |
9610a66e | 614 | } |
4dffc775 | 615 | session_set_user($user); |
9610a66e | 616 | } |
617 | } | |
618 | } | |
4dffc775 | 619 | } |
9610a66e | 620 | |
4c1c9175 | 621 | // Apache log integration. In apache conf file one can use ${MOODULEUSER}n in |
4dffc775 PS |
622 | // LogFormat to get the current logged in username in moodle. |
623 | if ($USER && function_exists('apache_note') | |
624 | && !empty($CFG->apacheloguser) && isset($USER->username)) { | |
625 | $apachelog_userid = $USER->id; | |
626 | $apachelog_username = clean_filename($USER->username); | |
627 | $apachelog_name = ''; | |
628 | if (isset($USER->firstname)) { | |
629 | // We can assume both will be set | |
630 | // - even if to empty. | |
631 | $apachelog_name = clean_filename($USER->firstname . " " . | |
632 | $USER->lastname); | |
633 | } | |
634 | if (session_is_loggedinas()) { | |
635 | $realuser = session_get_realuser(); | |
636 | $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username); | |
637 | $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name); | |
638 | $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid); | |
639 | } | |
640 | switch ($CFG->apacheloguser) { | |
641 | case 3: | |
642 | $logname = $apachelog_username; | |
643 | break; | |
644 | case 2: | |
645 | $logname = $apachelog_name; | |
646 | break; | |
647 | case 1: | |
648 | default: | |
649 | $logname = $apachelog_userid; | |
650 | break; | |
651 | } | |
652 | apache_note('MOODLEUSER', $logname); | |
653 | } | |
a559eee6 | 654 | |
4dffc775 PS |
655 | // Adjust ALLOWED_TAGS |
656 | adjust_allowed_tags(); | |
5982740d | 657 | |
4dffc775 PS |
658 | // Use a custom script replacement if one exists |
659 | if (!empty($CFG->customscripts)) { | |
660 | if (($customscript = custom_script_path()) !== false) { | |
661 | require ($customscript); | |
18259d4f | 662 | } |
4dffc775 | 663 | } |
18259d4f | 664 | |
4dffc775 PS |
665 | // in the first case, ip in allowed list will be performed first |
666 | // for example, client IP is 192.168.1.1 | |
667 | // 192.168 subnet is an entry in allowed list | |
668 | // 192.168.1.1 is banned in blocked list | |
669 | // This ip will be banned finally | |
670 | if (!empty($CFG->allowbeforeblock)) { // allowed list processed before blocked list? | |
671 | if (!empty($CFG->allowedip)) { | |
672 | if (!remoteip_in_list($CFG->allowedip)) { | |
673 | die(get_string('ipblocked', 'admin')); | |
ab99c8f0 | 674 | } |
4dffc775 PS |
675 | } |
676 | // need further check, client ip may a part of | |
677 | // allowed subnet, but a IP address are listed | |
678 | // in blocked list. | |
679 | if (!empty($CFG->blockedip)) { | |
680 | if (remoteip_in_list($CFG->blockedip)) { | |
681 | die(get_string('ipblocked', 'admin')); | |
5035228f | 682 | } |
4dffc775 | 683 | } |
d255c6e9 | 684 | |
4dffc775 PS |
685 | } else { |
686 | // in this case, IPs in blocked list will be performed first | |
687 | // for example, client IP is 192.168.1.1 | |
688 | // 192.168 subnet is an entry in blocked list | |
689 | // 192.168.1.1 is allowed in allowed list | |
690 | // This ip will be allowed finally | |
691 | if (!empty($CFG->blockedip)) { | |
692 | if (remoteip_in_list($CFG->blockedip)) { | |
693 | // if the allowed ip list is not empty | |
694 | // IPs are not included in the allowed list will be | |
695 | // blocked too | |
696 | if (!empty($CFG->allowedip)) { | |
697 | if (!remoteip_in_list($CFG->allowedip)) { | |
d255c6e9 | 698 | die(get_string('ipblocked', 'admin')); |
699 | } | |
4dffc775 | 700 | } else { |
d255c6e9 | 701 | die(get_string('ipblocked', 'admin')); |
702 | } | |
5035228f | 703 | } |
4dffc775 PS |
704 | } |
705 | // if blocked list is null | |
706 | // allowed list should be tested | |
707 | if(!empty($CFG->allowedip)) { | |
708 | if (!remoteip_in_list($CFG->allowedip)) { | |
709 | die(get_string('ipblocked', 'admin')); | |
710 | } | |
ab99c8f0 | 711 | } |
712 | ||
4dffc775 | 713 | } |
092bfaf1 | 714 | |
4dffc775 PS |
715 | // note: we can not block non utf-8 installations here, because empty mysql database |
716 | // might be converted to utf-8 in admin/index.php during installation | |
d5fac1cf PS |
717 | |
718 | ||
719 | ||
720 | // this is a funny trick to make Eclipse believe that $OUTPUT and other globals | |
721 | // contains an instance of core_renderer, etc. which in turn fixes autocompletion ;-) | |
722 | if (false) { | |
723 | $DB = new moodle_database(); | |
724 | $OUTPUT = new core_renderer(null, null); | |
725 | $PAGE = new moodle_page(); | |
726 | } |