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 | * |
78bfb562 PS |
24 | * @package core |
25 | * @subpackage lib | |
26 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
27 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
da8759cb | 28 | */ |
29 | ||
da8759cb | 30 | /** |
e1d1b796 | 31 | * Holds the core settings that affect how Moodle works. Some of its fields |
32 | * are set in config.php, and the rest are loaded from the config table. | |
33 | * | |
34 | * Some typical settings in the $CFG global: | |
3b5ff37f PS |
35 | * - $CFG->wwwroot - Path to moodle index directory in url format. |
36 | * - $CFG->dataroot - Path to moodle data files directory on server's filesystem. | |
37 | * - $CFG->dirroot - Path to moodle's library folder on server's filesystem. | |
38 | * - $CFG->libdir - Path to moodle's library folder on server's filesystem. | |
3e939871 | 39 | * - $CFG->tempdir - Path to moodle's temp file directory on server's filesystem. |
383d3884 | 40 | * - $CFG->cachedir - Path to moodle's cache directory on server's filesystem. |
e1d1b796 | 41 | * |
42 | * @global object $CFG | |
b37eac91 | 43 | * @name $CFG |
e1d1b796 | 44 | */ |
b30fa335 | 45 | global $CFG; // this should be done much earlier in config.php before creating new $CFG instance |
e1d1b796 | 46 | |
fe79c400 PS |
47 | if (!isset($CFG)) { |
48 | if (defined('PHPUNIT_SCRIPT') and PHPUNIT_SCRIPT) { | |
49 | echo('There is a missing "global $CFG;" at the beginning of the config.php file.'."\n"); | |
50 | exit(1); | |
51 | } else { | |
52 | // this should never happen, maybe somebody is accessing this file directly... | |
53 | exit(1); | |
54 | } | |
55 | } | |
56 | ||
3b5ff37f PS |
57 | // We can detect real dirroot path reliably since PHP 4.0.2, |
58 | // it can not be anything else, there is no point in having this in config.php | |
59 | $CFG->dirroot = dirname(dirname(__FILE__)); | |
60 | ||
61 | // Normalise dataroot - we do not want any symbolic links, trailing / or any other weirdness there | |
62 | if (!isset($CFG->dataroot)) { | |
56953adc PS |
63 | if (isset($_SERVER['REMOTE_ADDR'])) { |
64 | header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); | |
65 | } | |
fe79c400 | 66 | echo('Fatal error: $CFG->dataroot is not specified in config.php! Exiting.'."\n"); |
3b5ff37f PS |
67 | exit(1); |
68 | } | |
69 | $CFG->dataroot = realpath($CFG->dataroot); | |
70 | if ($CFG->dataroot === false) { | |
56953adc PS |
71 | if (isset($_SERVER['REMOTE_ADDR'])) { |
72 | header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); | |
73 | } | |
fe79c400 | 74 | echo('Fatal error: $CFG->dataroot is not configured properly, directory does not exist or is not accessible! Exiting.'."\n"); |
3b5ff37f | 75 | exit(1); |
8b2ebe7f PS |
76 | } else if (!is_writable($CFG->dataroot)) { |
77 | if (isset($_SERVER['REMOTE_ADDR'])) { | |
78 | header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); | |
79 | } | |
80 | echo('Fatal error: $CFG->dataroot is not writable, admin has to fix directory permissions! Exiting.'."\n"); | |
81 | exit(1); | |
3b5ff37f PS |
82 | } |
83 | ||
84 | // wwwroot is mandatory | |
85 | if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') { | |
56953adc PS |
86 | if (isset($_SERVER['REMOTE_ADDR'])) { |
87 | header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); | |
88 | } | |
fe79c400 | 89 | echo('Fatal error: $CFG->wwwroot is not configured! Exiting.'."\n"); |
3b5ff37f PS |
90 | exit(1); |
91 | } | |
92 | ||
93 | // Define admin directory | |
94 | if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php | |
95 | $CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot | |
96 | } | |
97 | ||
60f2c866 PS |
98 | // Set up some paths. |
99 | $CFG->libdir = $CFG->dirroot .'/lib'; | |
100 | ||
383d3884 | 101 | // Allow overriding of tempdir but be backwards compatible |
3e939871 | 102 | if (!isset($CFG->tempdir)) { |
383d3884 TL |
103 | $CFG->tempdir = "$CFG->dataroot/temp"; |
104 | } | |
105 | ||
106 | // Allow overriding of cachedir but be backwards compatible | |
107 | if (!isset($CFG->cachedir)) { | |
108 | $CFG->cachedir = "$CFG->dataroot/cache"; | |
3e939871 TL |
109 | } |
110 | ||
3b5ff37f PS |
111 | // The current directory in PHP version 4.3.0 and above isn't necessarily the |
112 | // directory of the script when run from the command line. The require_once() | |
113 | // would fail, so we'll have to chdir() | |
114 | if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) { | |
115 | chdir(dirname($_SERVER['argv'][0])); | |
116 | } | |
117 | ||
118 | // sometimes default PHP settings are borked on shared hosting servers, I wonder why they have to do that?? | |
27466303 | 119 | ini_set('precision', 14); // needed for upgrades and gradebook |
3b5ff37f PS |
120 | |
121 | // Scripts may request no debug and error messages in output | |
122 | // please note it must be defined before including the config.php script | |
123 | // and in some cases you also need to set custom default exception handler | |
124 | if (!defined('NO_DEBUG_DISPLAY')) { | |
125 | define('NO_DEBUG_DISPLAY', false); | |
126 | } | |
127 | ||
cbad562e PS |
128 | // Some scripts such as upgrade may want to prevent output buffering |
129 | if (!defined('NO_OUTPUT_BUFFERING')) { | |
130 | define('NO_OUTPUT_BUFFERING', false); | |
131 | } | |
132 | ||
62c5e654 MD |
133 | // Servers should define a default timezone in php.ini, but if they don't then make sure something is defined. |
134 | // This is a quick hack. Ideally we should ask the admin for a value. See MDL-22625 for more on this. | |
177b5480 | 135 | if (function_exists('date_default_timezone_set') and function_exists('date_default_timezone_get')) { |
27466303 PS |
136 | $olddebug = error_reporting(0); |
137 | date_default_timezone_set(date_default_timezone_get()); | |
138 | error_reporting($olddebug); | |
139 | unset($olddebug); | |
62c5e654 MD |
140 | } |
141 | ||
1e36599b PS |
142 | // PHPUnit scripts are a special case, for now we treat them as normal CLI scripts, |
143 | // please note you must install PHPUnit library separately via PEAR | |
144 | if (!defined('PHPUNIT_SCRIPT')) { | |
145 | define('PHPUNIT_SCRIPT', false); | |
146 | } | |
147 | if (PHPUNIT_SCRIPT) { | |
148 | define('CLI_SCRIPT', true); | |
149 | } | |
150 | ||
3b5ff37f | 151 | // Detect CLI scripts - CLI scripts are executed from command line, do not have session and we do not want HTML in output |
28bd3d9a PS |
152 | // In your new CLI scripts just add "define('CLI_SCRIPT', true);" before requiring config.php. |
153 | // Please note that one script can not be accessed from both CLI and web interface. | |
3b5ff37f | 154 | if (!defined('CLI_SCRIPT')) { |
28bd3d9a PS |
155 | define('CLI_SCRIPT', false); |
156 | } | |
157 | if (defined('WEB_CRON_EMULATED_CLI')) { | |
158 | if (!isset($_SERVER['REMOTE_ADDR'])) { | |
159 | echo('Web cron can not be executed as CLI script any more, please use admin/cli/cron.php instead'."\n"); | |
160 | exit(1); | |
161 | } | |
162 | } else if (isset($_SERVER['REMOTE_ADDR'])) { | |
163 | if (CLI_SCRIPT) { | |
164 | echo('Command line scripts can not be executed from the web interface'); | |
165 | exit(1); | |
166 | } | |
167 | } else { | |
168 | if (!CLI_SCRIPT) { | |
169 | echo('Command line scripts must define CLI_SCRIPT before requiring config.php'."\n"); | |
170 | exit(1); | |
3b5ff37f PS |
171 | } |
172 | } | |
173 | ||
8820f4e7 PS |
174 | // Detect CLI maintenance mode - this is useful when you need to mess with database, such as during upgrades |
175 | if (file_exists("$CFG->dataroot/climaintenance.html")) { | |
176 | if (!CLI_SCRIPT) { | |
8a7703ce | 177 | header('Content-type: text/html; charset=utf-8'); |
8820f4e7 | 178 | /// Headers to make it not cacheable and json |
27466303 PS |
179 | header('Cache-Control: no-store, no-cache, must-revalidate'); |
180 | header('Cache-Control: post-check=0, pre-check=0', false); | |
181 | header('Pragma: no-cache'); | |
182 | header('Expires: Mon, 20 Aug 1969 09:23:00 GMT'); | |
183 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | |
184 | header('Accept-Ranges: none'); | |
8820f4e7 PS |
185 | readfile("$CFG->dataroot/climaintenance.html"); |
186 | die; | |
187 | } else { | |
e4c05ec7 PS |
188 | if (!defined('CLI_MAINTENANCE')) { |
189 | define('CLI_MAINTENANCE', true); | |
190 | } | |
8820f4e7 PS |
191 | } |
192 | } else { | |
e4c05ec7 PS |
193 | if (!defined('CLI_MAINTENANCE')) { |
194 | define('CLI_MAINTENANCE', false); | |
195 | } | |
8820f4e7 PS |
196 | } |
197 | ||
8de5d7b9 PS |
198 | if (CLI_SCRIPT) { |
199 | // sometimes people use different PHP binary for web and CLI, make 100% sure they have the supported PHP version | |
200 | if (version_compare(phpversion(), '5.3.2') < 0) { | |
201 | $phpversion = phpversion(); | |
202 | // do NOT localise - lang strings would not work here and we CAN NOT move it to later place | |
203 | echo "Moodle 2.1 or later requires at least PHP 5.3.2 (currently using version $phpversion).\n"; | |
204 | echo "Some servers may have multiple PHP versions installed, are you using the correct executable?\n"; | |
205 | exit(1); | |
206 | } | |
207 | } | |
208 | ||
3b5ff37f PS |
209 | // Detect ajax scripts - they are similar to CLI because we can not redirect, output html, etc. |
210 | if (!defined('AJAX_SCRIPT')) { | |
211 | define('AJAX_SCRIPT', false); | |
212 | } | |
213 | ||
4cee2155 PS |
214 | // File permissions on created directories in the $CFG->dataroot |
215 | if (empty($CFG->directorypermissions)) { | |
216 | $CFG->directorypermissions = 02777; // Must be octal (that's why it's here) | |
217 | } | |
218 | if (empty($CFG->filepermissions)) { | |
219 | $CFG->filepermissions = ($CFG->directorypermissions & 0666); // strip execute flags | |
220 | } | |
221 | // better also set default umask because recursive mkdir() does not apply permissions recursively otherwise | |
222 | umask(0000); | |
223 | ||
60f2c866 | 224 | // exact version of currently used yui2 and 3 library |
6fc17ca9 | 225 | $CFG->yui2version = '2.9.0'; |
9e44f79f | 226 | $CFG->yui3version = '3.4.1'; |
60f2c866 | 227 | |
3b5ff37f | 228 | |
60f2c866 PS |
229 | // special support for highly optimised scripts that do not need libraries and DB connection |
230 | if (defined('ABORT_AFTER_CONFIG')) { | |
231 | if (!defined('ABORT_AFTER_CONFIG_CANCEL')) { | |
0037dcc9 PS |
232 | // hide debugging if not enabled in config.php - we do not want to disclose sensitive info |
233 | if (isset($CFG->debug)) { | |
234 | error_reporting($CFG->debug); | |
235 | } else { | |
236 | error_reporting(0); | |
237 | } | |
238 | if (empty($CFG->debugdisplay)) { | |
27466303 PS |
239 | ini_set('display_errors', '0'); |
240 | ini_set('log_errors', '1'); | |
0037dcc9 | 241 | } else { |
27466303 | 242 | ini_set('display_errors', '1'); |
0037dcc9 | 243 | } |
60f2c866 PS |
244 | require_once("$CFG->dirroot/lib/configonlylib.php"); |
245 | return; | |
246 | } | |
247 | } | |
248 | ||
460ebd6c | 249 | /** Used by library scripts to check they are being called by Moodle */ |
26f073ff PS |
250 | if (!defined('MOODLE_INTERNAL')) { // necessary because cli installer has to define it earlier |
251 | define('MOODLE_INTERNAL', true); | |
252 | } | |
1a3993c1 | 253 | |
c646dd26 EL |
254 | // Early profiling start, based exclusively on config.php $CFG settings |
255 | if (!empty($CFG->earlyprofilingenabled)) { | |
256 | require_once($CFG->libdir . '/xhprof/xhprof_moodle.php'); | |
257 | if (profiling_start()) { | |
258 | register_shutdown_function('profiling_stop'); | |
259 | } | |
260 | } | |
261 | ||
e1d1b796 | 262 | /** |
263 | * Database connection. Used for all access to the database. | |
264 | * @global moodle_database $DB | |
b37eac91 | 265 | * @name $DB |
e1d1b796 | 266 | */ |
267 | global $DB; | |
268 | ||
269 | /** | |
270 | * Moodle's wrapper round PHP's $_SESSION. | |
271 | * | |
272 | * @global object $SESSION | |
b37eac91 | 273 | * @name $SESSION |
e1d1b796 | 274 | */ |
275 | global $SESSION; | |
276 | ||
277 | /** | |
278 | * Holds the user table record for the current user. Will be the 'guest' | |
279 | * user record for people who are not logged in. | |
280 | * | |
281 | * $USER is stored in the session. | |
da8759cb | 282 | * |
735b8567 | 283 | * Items found in the user record: |
da8759cb | 284 | * - $USER->email - The user's email address. |
285 | * - $USER->id - The unique integer identified of this user in the 'user' table. | |
286 | * - $USER->email - The user's email address. | |
287 | * - $USER->firstname - The user's first name. | |
288 | * - $USER->lastname - The user's last name. | |
289 | * - $USER->username - The user's login username. | |
290 | * - $USER->secret - The user's ?. | |
291 | * - $USER->lang - The user's language choice. | |
292 | * | |
e1d1b796 | 293 | * @global object $USER |
b37eac91 | 294 | * @name $USER |
da8759cb | 295 | */ |
674fb525 | 296 | global $USER; |
e1d1b796 | 297 | |
c13a5e71 | 298 | /** |
299 | * A central store of information about the current page we are | |
300 | * generating in response to the user's request. | |
301 | * | |
302 | * @global moodle_page $PAGE | |
b37eac91 | 303 | * @name $PAGE |
c13a5e71 | 304 | */ |
305 | global $PAGE; | |
306 | ||
da8759cb | 307 | /** |
e1d1b796 | 308 | * The current course. An alias for $PAGE->course. |
309 | * @global object $COURSE | |
b37eac91 | 310 | * @name $COURSE |
da8759cb | 311 | */ |
312 | global $COURSE; | |
e1d1b796 | 313 | |
da8759cb | 314 | /** |
78946b9b | 315 | * $OUTPUT is an instance of core_renderer or one of its subclasses. Use |
34a2777c | 316 | * it to generate HTML for output. |
da8759cb | 317 | * |
c84a2dbe | 318 | * $OUTPUT is initialised the first time it is used. See {@link bootstrap_renderer} |
319 | * for the magic that does that. After $OUTPUT has been initialised, any attempt | |
320 | * to change something that affects the current theme ($PAGE->course, logged in use, | |
321 | * httpsrequried ... will result in an exception.) | |
34a2777c | 322 | * |
cbcc9852 PS |
323 | * Please note the $OUTPUT is replacing the old global $THEME object. |
324 | * | |
34a2777c | 325 | * @global object $OUTPUT |
326 | * @name $OUTPUT | |
327 | */ | |
328 | global $OUTPUT; | |
329 | ||
9d0dd812 | 330 | /** |
e1d1b796 | 331 | * Shared memory cache. |
332 | * @global object $MCACHE | |
b37eac91 | 333 | * @name $MCACHE |
e1d1b796 | 334 | */ |
335 | global $MCACHE; | |
336 | ||
b37eac91 | 337 | /** |
338 | * Full script path including all params, slash arguments, scheme and host. | |
339 | * @global string $FULLME | |
340 | * @name $FULLME | |
341 | */ | |
11e7b506 | 342 | global $FULLME; |
e1d1b796 | 343 | |
b37eac91 | 344 | /** |
345 | * Script path including query string and slash arguments without host. | |
346 | * @global string $ME | |
347 | * @name $ME | |
348 | */ | |
11e7b506 | 349 | global $ME; |
e1d1b796 | 350 | |
b37eac91 | 351 | /** |
352 | * $FULLME without slasharguments and query string. | |
353 | * @global string $FULLSCRIPT | |
354 | * @name $FULLSCRIPT | |
355 | */ | |
11e7b506 | 356 | global $FULLSCRIPT; |
e1d1b796 | 357 | |
b37eac91 | 358 | /** |
359 | * Relative moodle script path '/course/view.php' | |
360 | * @global string $SCRIPT | |
361 | * @name $SCRIPT | |
362 | */ | |
11e7b506 | 363 | global $SCRIPT; |
9d0dd812 | 364 | |
4dffc775 PS |
365 | // Store settings from config.php in array in $CFG - we can use it later to detect problems and overrides |
366 | $CFG->config_php_settings = (array)$CFG; | |
12bb0c3e PS |
367 | // Forced plugin settings override values from config_plugins table |
368 | unset($CFG->config_php_settings['forced_plugin_settings']); | |
369 | if (!isset($CFG->forced_plugin_settings)) { | |
370 | $CFG->forced_plugin_settings = array(); | |
371 | } | |
4dffc775 PS |
372 | // Set httpswwwroot default value (this variable will replace $CFG->wwwroot |
373 | // inside some URLs used in HTTPSPAGEREQUIRED pages. | |
374 | $CFG->httpswwwroot = $CFG->wwwroot; | |
d3f9f1f8 | 375 | |
4dffc775 | 376 | require_once($CFG->libdir .'/setuplib.php'); // Functions that MUST be loaded first |
17da2e6f | 377 | |
cbad562e | 378 | if (NO_OUTPUT_BUFFERING) { |
871ed458 | 379 | // we have to call this always before starting session because it discards headers! |
cbad562e PS |
380 | disable_output_buffering(); |
381 | } | |
382 | ||
0f0f0768 PS |
383 | // Increase memory limits if possible |
384 | raise_memory_limit(MEMORY_STANDARD); | |
385 | ||
4dffc775 PS |
386 | // Time to start counting |
387 | init_performance_info(); | |
b7009474 | 388 | |
4dffc775 PS |
389 | // Put $OUTPUT in place, so errors can be displayed. |
390 | $OUTPUT = new bootstrap_renderer(); | |
9d0dd812 | 391 | |
4c1c9175 | 392 | // set handler for uncaught exceptions - equivalent to print_error() call |
4dffc775 | 393 | set_exception_handler('default_exception_handler'); |
85ba1e78 | 394 | set_error_handler('default_error_handler', E_ALL | E_STRICT); |
6800d78e | 395 | |
4dffc775 PS |
396 | // If there are any errors in the standard libraries we want to know! |
397 | error_reporting(E_ALL); | |
c84a2dbe | 398 | |
4dffc775 PS |
399 | // Just say no to link prefetching (Moz prefetching, Google Web Accelerator, others) |
400 | // http://www.google.com/webmasters/faq.html#prefetchblock | |
401 | if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){ | |
402 | header($_SERVER['SERVER_PROTOCOL'] . ' 404 Prefetch Forbidden'); | |
403 | echo('Prefetch request forbidden.'); | |
404 | exit(1); | |
405 | } | |
b8cea9b2 | 406 | |
4dffc775 PS |
407 | if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php |
408 | $CFG->prefix = ''; | |
409 | } | |
d7196099 | 410 | |
38c253f9 PS |
411 | // location of all languages except core English pack |
412 | if (!isset($CFG->langotherroot)) { | |
413 | $CFG->langotherroot = $CFG->dataroot.'/lang'; | |
414 | } | |
415 | ||
416 | // location of local lang pack customisations (dirs with _local suffix) | |
417 | if (!isset($CFG->langlocalroot)) { | |
418 | $CFG->langlocalroot = $CFG->dataroot.'/lang'; | |
419 | } | |
420 | ||
3d732d84 EL |
421 | //point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else |
422 | //the problem is that we need specific version of quickforms and hacked excel files :-( | |
423 | ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path')); | |
424 | //point zend include path to moodles lib/zend so that includes and requires will search there for files before anywhere else | |
425 | //please note zend library is supposed to be used only from web service protocol classes, it may be removed in future | |
426 | ini_set('include_path', $CFG->libdir.'/zend' . PATH_SEPARATOR . ini_get('include_path')); | |
427 | ||
4dffc775 PS |
428 | // Load up standard libraries |
429 | require_once($CFG->libdir .'/textlib.class.php'); // Functions to handle multibyte strings | |
430 | require_once($CFG->libdir .'/filterlib.php'); // Functions for filtering test as it is output | |
431 | require_once($CFG->libdir .'/ajax/ajaxlib.php'); // Functions for managing our use of JavaScript and YUI | |
432 | require_once($CFG->libdir .'/weblib.php'); // Functions relating to HTTP and content | |
433 | require_once($CFG->libdir .'/outputlib.php'); // Functions for generating output | |
434 | require_once($CFG->libdir .'/navigationlib.php'); // Class for generating Navigation structure | |
435 | require_once($CFG->libdir .'/dmllib.php'); // Database access | |
436 | require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions. | |
437 | require_once($CFG->libdir .'/accesslib.php'); // Access control functions | |
438 | require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility | |
439 | require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions | |
df997f84 | 440 | require_once($CFG->libdir .'/enrollib.php'); // Enrolment related functions |
4dffc775 PS |
441 | require_once($CFG->libdir .'/pagelib.php'); // Library that defines the moodle_page class, used for $PAGE |
442 | require_once($CFG->libdir .'/blocklib.php'); // Library for controlling blocks | |
443 | require_once($CFG->libdir .'/eventslib.php'); // Events functions | |
444 | require_once($CFG->libdir .'/grouplib.php'); // Groups functions | |
445 | require_once($CFG->libdir .'/sessionlib.php'); // All session and cookie related stuff | |
446 | require_once($CFG->libdir .'/editorlib.php'); // All text editor related functions and classes | |
7c7d3afa | 447 | require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions |
0d8b6a69 | 448 | require_once($CFG->libdir .'/modinfolib.php'); // Cached information on course-module instances |
4dffc775 | 449 | |
4dffc775 PS |
450 | // make sure PHP is not severly misconfigured |
451 | setup_validate_php_configuration(); | |
452 | ||
4dffc775 PS |
453 | // Connect to the database |
454 | setup_DB(); | |
455 | ||
456 | // Disable errors for now - needed for installation when debug enabled in config.php | |
457 | if (isset($CFG->debug)) { | |
458 | $originalconfigdebug = $CFG->debug; | |
459 | unset($CFG->debug); | |
460 | } else { | |
461 | $originalconfigdebug = -1; | |
462 | } | |
dae73c05 | 463 | |
4dffc775 | 464 | // Load up any configuration from the config table |
12bb0c3e | 465 | initialise_cfg(); |
a8a71844 | 466 | |
4dffc775 PS |
467 | // Verify upgrade is not running unless we are in a script that needs to execute in any case |
468 | if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) { | |
469 | if ($CFG->upgraderunning < time()) { | |
470 | unset_config('upgraderunning'); | |
aa893d6b | 471 | } else { |
4dffc775 | 472 | print_error('upgraderunning'); |
aa893d6b | 473 | } |
4dffc775 | 474 | } |
aa893d6b | 475 | |
4dffc775 PS |
476 | // Turn on SQL logging if required |
477 | if (!empty($CFG->logsql)) { | |
478 | $DB->set_logging(true); | |
479 | } | |
f9903ed0 | 480 | |
4dffc775 PS |
481 | // Prevent warnings from roles when upgrading with debug on |
482 | if (isset($CFG->debug)) { | |
483 | $originaldatabasedebug = $CFG->debug; | |
484 | unset($CFG->debug); | |
485 | } else { | |
486 | $originaldatabasedebug = -1; | |
487 | } | |
775f811a | 488 | |
c7b0e5e1 PS |
489 | // enable circular reference collector in PHP 5.3, |
490 | // it helps a lot when using large complex OOP structures such as in amos or gradebook | |
491 | if (function_exists('gc_enable')) { | |
492 | gc_enable(); | |
493 | } | |
1e3e716f | 494 | |
c22473a2 | 495 | // Register default shutdown tasks - such as Apache memory release helper, perf logging, etc. |
4dffc775 PS |
496 | if (function_exists('register_shutdown_function')) { |
497 | register_shutdown_function('moodle_request_shutdown'); | |
498 | } | |
4fd7ccc0 | 499 | |
4dffc775 PS |
500 | // Defining the site |
501 | try { | |
502 | $SITE = get_site(); | |
4dffc775 PS |
503 | /** |
504 | * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1. | |
505 | */ | |
506 | define('SITEID', $SITE->id); | |
7b51fb61 PS |
507 | // And the 'default' course - this will usually get reset later in require_login() etc. |
508 | $COURSE = clone($SITE); | |
08d3898c | 509 | } catch (dml_exception $e) { |
7b51fb61 PS |
510 | $SITE = null; |
511 | if (empty($CFG->version)) { | |
512 | // we are just installing | |
513 | /** | |
514 | * @ignore | |
515 | */ | |
516 | define('SITEID', 1); | |
517 | // And the 'default' course | |
365a5941 | 518 | $COURSE = new stdClass(); // no site created yet |
7b51fb61 PS |
519 | $COURSE->id = 1; |
520 | } else { | |
521 | throw $e; | |
522 | } | |
4dffc775 | 523 | } |
cf8133c4 | 524 | |
4dffc775 PS |
525 | // define SYSCONTEXTID in config.php if you want to save some queries (after install or upgrade!) |
526 | if (!defined('SYSCONTEXTID')) { | |
527 | get_system_context(); | |
528 | } | |
c23b0ea1 | 529 | |
4dffc775 PS |
530 | // Set error reporting back to normal |
531 | if ($originaldatabasedebug == -1) { | |
532 | $CFG->debug = DEBUG_MINIMAL; | |
533 | } else { | |
534 | $CFG->debug = $originaldatabasedebug; | |
535 | } | |
536 | if ($originalconfigdebug !== -1) { | |
537 | $CFG->debug = $originalconfigdebug; | |
538 | } | |
539 | unset($originalconfigdebug); | |
540 | unset($originaldatabasedebug); | |
541 | error_reporting($CFG->debug); | |
542 | ||
12bb0c3e | 543 | // find out if PHP configured to display warnings, |
4dffc775 PS |
544 | // this is a security problem because some moodle scripts may |
545 | // disclose sensitive information | |
546 | if (ini_get_bool('display_errors')) { | |
547 | define('WARN_DISPLAY_ERRORS_ENABLED', true); | |
548 | } | |
549 | // If we want to display Moodle errors, then try and set PHP errors to match | |
550 | if (!isset($CFG->debugdisplay)) { | |
551 | // keep it "as is" during installation | |
552 | } else if (NO_DEBUG_DISPLAY) { | |
553 | // some parts of Moodle cannot display errors and debug at all. | |
27466303 PS |
554 | ini_set('display_errors', '0'); |
555 | ini_set('log_errors', '1'); | |
4dffc775 | 556 | } else if (empty($CFG->debugdisplay)) { |
27466303 PS |
557 | ini_set('display_errors', '0'); |
558 | ini_set('log_errors', '1'); | |
4dffc775 PS |
559 | } else { |
560 | // This is very problematic in XHTML strict mode! | |
27466303 | 561 | ini_set('display_errors', '1'); |
4dffc775 | 562 | } |
475e9de8 | 563 | |
4dffc775 PS |
564 | // detect unsupported upgrade jump as soon as possible - do not change anything, do not use system functions |
565 | if (!empty($CFG->version) and $CFG->version < 2007101509) { | |
566 | print_error('upgraderequires19', 'error'); | |
567 | die; | |
568 | } | |
475e9de8 | 569 | |
4dffc775 PS |
570 | // Shared-Memory cache init -- will set $MCACHE |
571 | // $MCACHE is a global object that offers at least add(), set() and delete() | |
572 | // with similar semantics to the memcached PHP API http://php.net/memcache | |
573 | // Ensure we define rcache - so we can later check for it | |
574 | // with a really fast and unambiguous $CFG->rcache === false | |
575 | if (!empty($CFG->cachetype)) { | |
576 | if (empty($CFG->rcache)) { | |
577 | $CFG->rcache = false; | |
25338300 | 578 | } else { |
4dffc775 | 579 | $CFG->rcache = true; |
25338300 | 580 | } |
581 | ||
4dffc775 PS |
582 | // do not try to initialize if cache disabled |
583 | if (!$CFG->rcache) { | |
584 | $CFG->cachetype = ''; | |
f9955801 | 585 | } |
586 | ||
4dffc775 PS |
587 | if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) { |
588 | if (!init_memcached()) { | |
589 | debugging("Error initialising memcached"); | |
590 | $CFG->cachetype = ''; | |
392e7363 | 591 | $CFG->rcache = false; |
392e7363 | 592 | } |
4dffc775 PS |
593 | } else if ($CFG->cachetype === 'eaccelerator') { |
594 | if (!init_eaccelerator()) { | |
595 | debugging("Error initialising eaccelerator cache"); | |
392e7363 | 596 | $CFG->cachetype = ''; |
4dffc775 | 597 | $CFG->rcache = false; |
392e7363 | 598 | } |
2142d492 | 599 | } |
aa893d6b | 600 | |
4dffc775 PS |
601 | } else { // just make sure it is defined |
602 | $CFG->cachetype = ''; | |
603 | $CFG->rcache = false; | |
604 | } | |
0182c65c | 605 | |
4dffc775 PS |
606 | // Calculate and set $CFG->ostype to be used everywhere. Possible values are: |
607 | // - WINDOWS: for any Windows flavour. | |
608 | // - UNIX: for the rest | |
609 | // Also, $CFG->os can continue being used if more specialization is required | |
610 | if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) { | |
611 | $CFG->ostype = 'WINDOWS'; | |
612 | } else { | |
613 | $CFG->ostype = 'UNIX'; | |
614 | } | |
615 | $CFG->os = PHP_OS; | |
820743c5 | 616 | |
4dffc775 | 617 | // Configure ampersands in URLs |
27466303 | 618 | ini_set('arg_separator.output', '&'); |
de7e4ac9 | 619 | |
4dffc775 | 620 | // Work around for a PHP bug see MDL-11237 |
27466303 | 621 | ini_set('pcre.backtrack_limit', 20971520); // 20 MB |
e7aa5a88 | 622 | |
4dffc775 | 623 | // Location of standard files |
4c1c9175 PS |
624 | $CFG->wordlist = $CFG->libdir .'/wordlist.txt'; |
625 | $CFG->moddata = 'moddata'; | |
f9903ed0 | 626 | |
4dffc775 PS |
627 | // Create the $PAGE global. |
628 | if (!empty($CFG->moodlepageclass)) { | |
629 | $classname = $CFG->moodlepageclass; | |
630 | } else { | |
631 | $classname = 'moodle_page'; | |
632 | } | |
633 | $PAGE = new $classname(); | |
634 | unset($classname); | |
635 | ||
636 | // A hack to get around magic_quotes_gpc being turned on | |
637 | // It is strongly recommended to disable "magic_quotes_gpc"! | |
638 | if (ini_get_bool('magic_quotes_gpc')) { | |
639 | function stripslashes_deep($value) { | |
640 | $value = is_array($value) ? | |
641 | array_map('stripslashes_deep', $value) : | |
642 | stripslashes($value); | |
643 | return $value; | |
c84a2dbe | 644 | } |
4dffc775 PS |
645 | $_POST = array_map('stripslashes_deep', $_POST); |
646 | $_GET = array_map('stripslashes_deep', $_GET); | |
647 | $_COOKIE = array_map('stripslashes_deep', $_COOKIE); | |
648 | $_REQUEST = array_map('stripslashes_deep', $_REQUEST); | |
649 | if (!empty($_SERVER['REQUEST_URI'])) { | |
650 | $_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']); | |
f98cfb53 | 651 | } |
4dffc775 PS |
652 | if (!empty($_SERVER['QUERY_STRING'])) { |
653 | $_SERVER['QUERY_STRING'] = stripslashes($_SERVER['QUERY_STRING']); | |
654 | } | |
655 | if (!empty($_SERVER['HTTP_REFERER'])) { | |
656 | $_SERVER['HTTP_REFERER'] = stripslashes($_SERVER['HTTP_REFERER']); | |
aa6af0f8 | 657 | } |
4dffc775 PS |
658 | if (!empty($_SERVER['PATH_INFO'])) { |
659 | $_SERVER['PATH_INFO'] = stripslashes($_SERVER['PATH_INFO']); | |
660 | } | |
661 | if (!empty($_SERVER['PHP_SELF'])) { | |
662 | $_SERVER['PHP_SELF'] = stripslashes($_SERVER['PHP_SELF']); | |
663 | } | |
664 | if (!empty($_SERVER['PATH_TRANSLATED'])) { | |
665 | $_SERVER['PATH_TRANSLATED'] = stripslashes($_SERVER['PATH_TRANSLATED']); | |
666 | } | |
667 | } | |
7a302afc | 668 | |
4dffc775 PS |
669 | // neutralise nasty chars in PHP_SELF |
670 | if (isset($_SERVER['PHP_SELF'])) { | |
671 | $phppos = strpos($_SERVER['PHP_SELF'], '.php'); | |
672 | if ($phppos !== false) { | |
673 | $_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, $phppos+4); | |
18b9d664 | 674 | } |
4dffc775 PS |
675 | unset($phppos); |
676 | } | |
677 | ||
d2114969 PS |
678 | // initialise ME's - this must be done BEFORE starting of session! |
679 | initialise_fullme(); | |
680 | ||
df92ba9a | 681 | // init session prevention flag - this is defined on pages that do not want session |
28bd3d9a PS |
682 | if (CLI_SCRIPT) { |
683 | // no sessions in CLI scripts possible | |
684 | define('NO_MOODLE_COOKIES', true); | |
685 | ||
686 | } else if (!defined('NO_MOODLE_COOKIES')) { | |
df92ba9a PS |
687 | if (empty($CFG->version) or $CFG->version < 2009011900) { |
688 | // no session before sessions table gets created | |
689 | define('NO_MOODLE_COOKIES', true); | |
690 | } else if (CLI_SCRIPT) { | |
691 | // CLI scripts can not have session | |
692 | define('NO_MOODLE_COOKIES', true); | |
693 | } else { | |
694 | define('NO_MOODLE_COOKIES', false); | |
695 | } | |
696 | } | |
697 | ||
4dffc775 PS |
698 | // start session and prepare global $SESSION, $USER |
699 | session_get_instance(); | |
700 | $SESSION = &$_SESSION['SESSION']; | |
701 | $USER = &$_SESSION['USER']; | |
18b9d664 | 702 | |
c646dd26 | 703 | // Late profiling, only happening if early one wasn't started |
6af80cae EL |
704 | if (!empty($CFG->profilingenabled)) { |
705 | require_once($CFG->libdir . '/xhprof/xhprof_moodle.php'); | |
c646dd26 EL |
706 | if (profiling_start()) { |
707 | register_shutdown_function('profiling_stop'); | |
708 | } | |
6af80cae EL |
709 | } |
710 | ||
4dffc775 | 711 | // Process theme change in the URL. |
998999e7 PS |
712 | if (!empty($CFG->allowthemechangeonurl) and !empty($_GET['theme'])) { |
713 | // we have to use _GET directly because we do not want this to interfere with _POST | |
aff24313 | 714 | $urlthemename = optional_param('theme', '', PARAM_PLUGIN); |
4dffc775 | 715 | try { |
998999e7 PS |
716 | $themeconfig = theme_config::load($urlthemename); |
717 | // Makes sure the theme can be loaded without errors. | |
718 | if ($themeconfig->name === $urlthemename) { | |
719 | $SESSION->theme = $urlthemename; | |
720 | } else { | |
721 | unset($SESSION->theme); | |
722 | } | |
723 | unset($themeconfig); | |
724 | unset($urlthemename); | |
4dffc775 PS |
725 | } catch (Exception $e) { |
726 | debugging('Failed to set the theme from the URL.', DEBUG_DEVELOPER, $e->getTrace()); | |
18b9d664 | 727 | } |
4dffc775 PS |
728 | } |
729 | unset($urlthemename); | |
18b9d664 | 730 | |
4dffc775 PS |
731 | // Ensure a valid theme is set. |
732 | if (!isset($CFG->theme)) { | |
733 | $CFG->theme = 'standardwhite'; | |
734 | } | |
735 | ||
736 | // Set language/locale of printed times. If user has chosen a language that | |
737 | // that is different from the site language, then use the locale specified | |
738 | // in the language file. Otherwise, if the admin hasn't specified a locale | |
739 | // then use the one from the default language. Otherwise (and this is the | |
740 | // majority of cases), use the stored locale specified by admin. | |
4000129f PS |
741 | // note: do not accept lang parameter from POST |
742 | if (isset($_GET['lang']) and ($lang = optional_param('lang', '', PARAM_SAFEDIR))) { | |
ecd7978c | 743 | if (get_string_manager()->translation_exists($lang, false)) { |
4dffc775 | 744 | $SESSION->lang = $lang; |
3e9b5d5a | 745 | } |
4dffc775 PS |
746 | } |
747 | unset($lang); | |
ab036ed9 | 748 | |
4dffc775 | 749 | setup_lang_from_browser(); |
ab036ed9 | 750 | |
4dffc775 PS |
751 | if (empty($CFG->lang)) { |
752 | if (empty($SESSION->lang)) { | |
3a915b06 | 753 | $CFG->lang = 'en'; |
4dffc775 PS |
754 | } else { |
755 | $CFG->lang = $SESSION->lang; | |
16ba7351 | 756 | } |
4dffc775 | 757 | } |
6800d78e | 758 | |
00dadbe1 PS |
759 | // Set the default site locale, a lot of the stuff may depend on this |
760 | // it is definitely too late to call this first in require_login()! | |
761 | moodle_setlocale(); | |
4dffc775 PS |
762 | |
763 | if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) { | |
764 | if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation | |
765 | if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest) | |
766 | if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or | |
767 | (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) { | |
768 | if ($user = get_complete_user_data("username", "w3cvalidator")) { | |
769 | $user->ignoresesskey = true; | |
770 | } else { | |
771 | $user = guest_user(); | |
9610a66e | 772 | } |
4dffc775 | 773 | session_set_user($user); |
9610a66e | 774 | } |
775 | } | |
776 | } | |
4dffc775 | 777 | } |
9610a66e | 778 | |
4c1c9175 | 779 | // Apache log integration. In apache conf file one can use ${MOODULEUSER}n in |
4dffc775 PS |
780 | // LogFormat to get the current logged in username in moodle. |
781 | if ($USER && function_exists('apache_note') | |
782 | && !empty($CFG->apacheloguser) && isset($USER->username)) { | |
783 | $apachelog_userid = $USER->id; | |
784 | $apachelog_username = clean_filename($USER->username); | |
785 | $apachelog_name = ''; | |
786 | if (isset($USER->firstname)) { | |
787 | // We can assume both will be set | |
788 | // - even if to empty. | |
789 | $apachelog_name = clean_filename($USER->firstname . " " . | |
790 | $USER->lastname); | |
791 | } | |
792 | if (session_is_loggedinas()) { | |
793 | $realuser = session_get_realuser(); | |
794 | $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username); | |
795 | $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name); | |
796 | $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid); | |
797 | } | |
798 | switch ($CFG->apacheloguser) { | |
799 | case 3: | |
800 | $logname = $apachelog_username; | |
801 | break; | |
802 | case 2: | |
803 | $logname = $apachelog_name; | |
804 | break; | |
805 | case 1: | |
806 | default: | |
807 | $logname = $apachelog_userid; | |
808 | break; | |
809 | } | |
810 | apache_note('MOODLEUSER', $logname); | |
811 | } | |
a559eee6 | 812 | |
4dffc775 PS |
813 | // Adjust ALLOWED_TAGS |
814 | adjust_allowed_tags(); | |
5982740d | 815 | |
4dffc775 PS |
816 | // Use a custom script replacement if one exists |
817 | if (!empty($CFG->customscripts)) { | |
818 | if (($customscript = custom_script_path()) !== false) { | |
819 | require ($customscript); | |
18259d4f | 820 | } |
4dffc775 | 821 | } |
18259d4f | 822 | |
4dffc775 PS |
823 | // in the first case, ip in allowed list will be performed first |
824 | // for example, client IP is 192.168.1.1 | |
825 | // 192.168 subnet is an entry in allowed list | |
826 | // 192.168.1.1 is banned in blocked list | |
827 | // This ip will be banned finally | |
828 | if (!empty($CFG->allowbeforeblock)) { // allowed list processed before blocked list? | |
829 | if (!empty($CFG->allowedip)) { | |
830 | if (!remoteip_in_list($CFG->allowedip)) { | |
831 | die(get_string('ipblocked', 'admin')); | |
ab99c8f0 | 832 | } |
4dffc775 PS |
833 | } |
834 | // need further check, client ip may a part of | |
835 | // allowed subnet, but a IP address are listed | |
836 | // in blocked list. | |
837 | if (!empty($CFG->blockedip)) { | |
838 | if (remoteip_in_list($CFG->blockedip)) { | |
839 | die(get_string('ipblocked', 'admin')); | |
5035228f | 840 | } |
4dffc775 | 841 | } |
d255c6e9 | 842 | |
4dffc775 PS |
843 | } else { |
844 | // in this case, IPs in blocked list will be performed first | |
845 | // for example, client IP is 192.168.1.1 | |
846 | // 192.168 subnet is an entry in blocked list | |
847 | // 192.168.1.1 is allowed in allowed list | |
848 | // This ip will be allowed finally | |
849 | if (!empty($CFG->blockedip)) { | |
850 | if (remoteip_in_list($CFG->blockedip)) { | |
851 | // if the allowed ip list is not empty | |
852 | // IPs are not included in the allowed list will be | |
853 | // blocked too | |
854 | if (!empty($CFG->allowedip)) { | |
855 | if (!remoteip_in_list($CFG->allowedip)) { | |
d255c6e9 | 856 | die(get_string('ipblocked', 'admin')); |
857 | } | |
4dffc775 | 858 | } else { |
d255c6e9 | 859 | die(get_string('ipblocked', 'admin')); |
860 | } | |
5035228f | 861 | } |
4dffc775 PS |
862 | } |
863 | // if blocked list is null | |
864 | // allowed list should be tested | |
865 | if(!empty($CFG->allowedip)) { | |
866 | if (!remoteip_in_list($CFG->allowedip)) { | |
867 | die(get_string('ipblocked', 'admin')); | |
868 | } | |
ab99c8f0 | 869 | } |
870 | ||
4dffc775 | 871 | } |
092bfaf1 | 872 | |
4dffc775 PS |
873 | // note: we can not block non utf-8 installations here, because empty mysql database |
874 | // might be converted to utf-8 in admin/index.php during installation | |
d5fac1cf PS |
875 | |
876 | ||
877 | ||
878 | // this is a funny trick to make Eclipse believe that $OUTPUT and other globals | |
879 | // contains an instance of core_renderer, etc. which in turn fixes autocompletion ;-) | |
880 | if (false) { | |
881 | $DB = new moodle_database(); | |
882 | $OUTPUT = new core_renderer(null, null); | |
883 | $PAGE = new moodle_page(); | |
884 | } |