1 |
efrain |
1 |
<?php
|
|
|
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 |
|
|
|
18 |
/**
|
|
|
19 |
* setup.php - Sets up sessions, connects to databases and so on
|
|
|
20 |
*
|
|
|
21 |
* Normally this is only called by the main config.php file
|
|
|
22 |
* Normally this file does not need to be edited.
|
|
|
23 |
*
|
|
|
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
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
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:
|
|
|
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.
|
|
|
39 |
* - $CFG->backuptempdir - Path to moodle's backup temp file directory on server's filesystem.
|
|
|
40 |
* - $CFG->tempdir - Path to moodle's temp file directory on server's filesystem.
|
|
|
41 |
* - $CFG->cachedir - Path to moodle's cache directory on server's filesystem (shared by cluster nodes).
|
|
|
42 |
* - $CFG->localcachedir - Path to moodle's local cache directory (not shared by cluster nodes).
|
|
|
43 |
* - $CFG->localrequestdir - Path to moodle's local temp request directory (not shared by cluster nodes).
|
|
|
44 |
*
|
|
|
45 |
* @global object $CFG
|
|
|
46 |
* @name $CFG
|
|
|
47 |
*/
|
|
|
48 |
global $CFG; // this should be done much earlier in config.php before creating new $CFG instance
|
|
|
49 |
|
|
|
50 |
if (!isset($CFG)) {
|
|
|
51 |
if (defined('PHPUNIT_TEST') and PHPUNIT_TEST) {
|
|
|
52 |
echo('There is a missing "global $CFG;" at the beginning of the config.php file.'."\n");
|
|
|
53 |
exit(1);
|
|
|
54 |
} else {
|
|
|
55 |
// this should never happen, maybe somebody is accessing this file directly...
|
|
|
56 |
exit(1);
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
// We can detect real dirroot path reliably since PHP 4.0.2,
|
|
|
61 |
// it can not be anything else, there is no point in having this in config.php
|
|
|
62 |
$CFG->dirroot = dirname(__DIR__);
|
|
|
63 |
|
|
|
64 |
// File permissions on created directories in the $CFG->dataroot
|
|
|
65 |
if (!isset($CFG->directorypermissions)) {
|
|
|
66 |
$CFG->directorypermissions = 02777; // Must be octal (that's why it's here)
|
|
|
67 |
}
|
|
|
68 |
if (!isset($CFG->filepermissions)) {
|
|
|
69 |
$CFG->filepermissions = ($CFG->directorypermissions & 0666); // strip execute flags
|
|
|
70 |
}
|
|
|
71 |
// Better also set default umask because developers often forget to include directory
|
|
|
72 |
// permissions in mkdir() and chmod() after creating new files.
|
|
|
73 |
if (!isset($CFG->umaskpermissions)) {
|
|
|
74 |
$CFG->umaskpermissions = (($CFG->directorypermissions & 0777) ^ 0777);
|
|
|
75 |
}
|
|
|
76 |
umask($CFG->umaskpermissions);
|
|
|
77 |
|
|
|
78 |
if (defined('BEHAT_SITE_RUNNING')) {
|
|
|
79 |
// We already switched to behat test site previously.
|
|
|
80 |
|
|
|
81 |
} else if (!empty($CFG->behat_wwwroot) or !empty($CFG->behat_dataroot) or !empty($CFG->behat_prefix)) {
|
|
|
82 |
// The behat is configured on this server, we need to find out if this is the behat test
|
|
|
83 |
// site based on the URL used for access.
|
|
|
84 |
require_once(__DIR__ . '/../lib/behat/lib.php');
|
|
|
85 |
|
|
|
86 |
// Update config variables for parallel behat runs.
|
|
|
87 |
behat_update_vars_for_process();
|
|
|
88 |
|
|
|
89 |
// If behat is being installed for parallel run, then we modify params for parallel run only.
|
|
|
90 |
if (behat_is_test_site() && !(defined('BEHAT_PARALLEL_UTIL') && empty($CFG->behatrunprocess))) {
|
|
|
91 |
clearstatcache();
|
|
|
92 |
|
|
|
93 |
// Checking the integrity of the provided $CFG->behat_* vars and the
|
|
|
94 |
// selected wwwroot to prevent conflicts with production and phpunit environments.
|
|
|
95 |
behat_check_config_vars();
|
|
|
96 |
|
|
|
97 |
// Check that the directory does not contains other things.
|
|
|
98 |
if (!file_exists("$CFG->behat_dataroot/behattestdir.txt")) {
|
|
|
99 |
if ($dh = opendir($CFG->behat_dataroot)) {
|
|
|
100 |
while (($file = readdir($dh)) !== false) {
|
|
|
101 |
if ($file === 'behat' or $file === '.' or $file === '..' or $file === '.DS_Store' or is_numeric($file)) {
|
|
|
102 |
continue;
|
|
|
103 |
}
|
|
|
104 |
behat_error(BEHAT_EXITCODE_CONFIG, "$CFG->behat_dataroot directory is not empty, ensure this is the " .
|
|
|
105 |
"directory where you want to install behat test dataroot");
|
|
|
106 |
}
|
|
|
107 |
closedir($dh);
|
|
|
108 |
unset($dh);
|
|
|
109 |
unset($file);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if (defined('BEHAT_UTIL')) {
|
|
|
113 |
// Now we create dataroot directory structure for behat tests.
|
|
|
114 |
testing_initdataroot($CFG->behat_dataroot, 'behat');
|
|
|
115 |
} else {
|
|
|
116 |
behat_error(BEHAT_EXITCODE_INSTALL);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
if (!defined('BEHAT_UTIL') and !defined('BEHAT_TEST')) {
|
|
|
121 |
// Somebody tries to access test site directly, tell them if not enabled.
|
|
|
122 |
$behatdir = preg_replace("#[/|\\\]" . BEHAT_PARALLEL_SITE_NAME . "\d{0,}$#", '', $CFG->behat_dataroot);
|
|
|
123 |
if (!file_exists($behatdir . '/test_environment_enabled.txt')) {
|
|
|
124 |
behat_error(BEHAT_EXITCODE_CONFIG, 'Behat is configured but not enabled on this test site.');
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// Constant used to inform that the behat test site is being used,
|
|
|
129 |
// this includes all the processes executed by the behat CLI command like
|
|
|
130 |
// the site reset, the steps executed by the browser drivers when simulating
|
|
|
131 |
// a user session and a real session when browsing manually to $CFG->behat_wwwroot
|
|
|
132 |
// like the browser driver does automatically.
|
|
|
133 |
// Different from BEHAT_TEST as only this last one can perform CLI
|
|
|
134 |
// actions like reset the site or use data generators.
|
|
|
135 |
define('BEHAT_SITE_RUNNING', true);
|
|
|
136 |
|
|
|
137 |
// Clean extra config.php settings.
|
|
|
138 |
behat_clean_init_config();
|
|
|
139 |
|
|
|
140 |
// Now we can begin switching $CFG->X for $CFG->behat_X.
|
|
|
141 |
$CFG->wwwroot = $CFG->behat_wwwroot;
|
|
|
142 |
$CFG->prefix = $CFG->behat_prefix;
|
|
|
143 |
$CFG->dataroot = $CFG->behat_dataroot;
|
|
|
144 |
|
|
|
145 |
// And we do the same with the optional ones.
|
|
|
146 |
$allowedconfigoverride = ['dbname', 'dbuser', 'dbpass', 'dbhost'];
|
|
|
147 |
foreach ($allowedconfigoverride as $config) {
|
|
|
148 |
$behatconfig = 'behat_' . $config;
|
|
|
149 |
if (!isset($CFG->$behatconfig)) {
|
|
|
150 |
continue;
|
|
|
151 |
}
|
|
|
152 |
$CFG->$config = $CFG->$behatconfig;
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Set default warn runtime.
|
|
|
158 |
if (!isset($CFG->taskruntimewarn)) {
|
|
|
159 |
$CFG->taskruntimewarn = 12 * 60 * 60;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
// Set default error runtime.
|
|
|
163 |
if (!isset($CFG->taskruntimeerror)) {
|
|
|
164 |
$CFG->taskruntimeerror = 24 * 60 * 60;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
// Normalise dataroot - we do not want any symbolic links, trailing / or any other weirdness there
|
|
|
168 |
if (!isset($CFG->dataroot)) {
|
|
|
169 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
170 |
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error ');
|
|
|
171 |
}
|
|
|
172 |
echo('Fatal error: $CFG->dataroot is not specified in config.php! Exiting.'."\n");
|
|
|
173 |
exit(1);
|
|
|
174 |
}
|
|
|
175 |
$CFG->dataroot = realpath($CFG->dataroot);
|
|
|
176 |
if ($CFG->dataroot === false) {
|
|
|
177 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
178 |
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error ');
|
|
|
179 |
}
|
|
|
180 |
echo('Fatal error: $CFG->dataroot is not configured properly, directory does not exist or is not accessible! Exiting.'."\n");
|
|
|
181 |
exit(1);
|
|
|
182 |
} else if (!is_writable($CFG->dataroot)) {
|
|
|
183 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
184 |
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error ');
|
|
|
185 |
}
|
|
|
186 |
echo('Fatal error: $CFG->dataroot is not writable, admin has to fix directory permissions! Exiting.'."\n");
|
|
|
187 |
exit(1);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
// wwwroot is mandatory
|
|
|
191 |
if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') {
|
|
|
192 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
193 |
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error ');
|
|
|
194 |
}
|
|
|
195 |
echo('Fatal error: $CFG->wwwroot is not configured! Exiting.'."\n");
|
|
|
196 |
exit(1);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
// Make sure there is some database table prefix.
|
|
|
200 |
if (!isset($CFG->prefix)) {
|
|
|
201 |
$CFG->prefix = '';
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
// Define admin directory
|
|
|
205 |
if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
|
|
|
206 |
$CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
// Set up some paths.
|
|
|
210 |
$CFG->libdir = $CFG->dirroot .'/lib';
|
|
|
211 |
|
|
|
212 |
// Allow overriding of tempdir but be backwards compatible
|
|
|
213 |
if (!isset($CFG->tempdir)) {
|
|
|
214 |
$CFG->tempdir = $CFG->dataroot . DIRECTORY_SEPARATOR . "temp";
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
// Allow overriding of backuptempdir but be backwards compatible
|
|
|
218 |
if (!isset($CFG->backuptempdir)) {
|
|
|
219 |
$CFG->backuptempdir = "$CFG->tempdir/backup";
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
// Allow overriding of cachedir but be backwards compatible
|
|
|
223 |
if (!isset($CFG->cachedir)) {
|
|
|
224 |
$CFG->cachedir = "$CFG->dataroot/cache";
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
// Allow overriding of localcachedir.
|
|
|
228 |
if (!isset($CFG->localcachedir)) {
|
|
|
229 |
$CFG->localcachedir = "$CFG->dataroot/localcache";
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
// Allow overriding of localrequestdir.
|
|
|
233 |
if (!isset($CFG->localrequestdir)) {
|
|
|
234 |
$CFG->localrequestdir = sys_get_temp_dir() . '/requestdir';
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
// Location of all languages except core English pack.
|
|
|
238 |
if (!isset($CFG->langotherroot)) {
|
|
|
239 |
$CFG->langotherroot = $CFG->dataroot.'/lang';
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
// Location of local lang pack customisations (dirs with _local suffix).
|
|
|
243 |
if (!isset($CFG->langlocalroot)) {
|
|
|
244 |
$CFG->langlocalroot = $CFG->dataroot.'/lang';
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// The current directory in PHP version 4.3.0 and above isn't necessarily the
|
|
|
248 |
// directory of the script when run from the command line. The require_once()
|
|
|
249 |
// would fail, so we'll have to chdir()
|
|
|
250 |
if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
|
|
|
251 |
// do it only once - skip the second time when continuing after prevous abort
|
|
|
252 |
if (!defined('ABORT_AFTER_CONFIG') and !defined('ABORT_AFTER_CONFIG_CANCEL')) {
|
|
|
253 |
chdir(dirname($_SERVER['argv'][0]));
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
// sometimes default PHP settings are borked on shared hosting servers, I wonder why they have to do that??
|
|
|
258 |
ini_set('precision', 14); // needed for upgrades and gradebook
|
11 |
efrain |
259 |
ini_set('serialize_precision', -1); // Make float serialization consistent on all systems.
|
1 |
efrain |
260 |
|
|
|
261 |
// Scripts may request no debug and error messages in output
|
|
|
262 |
// please note it must be defined before including the config.php script
|
|
|
263 |
// and in some cases you also need to set custom default exception handler
|
|
|
264 |
if (!defined('NO_DEBUG_DISPLAY')) {
|
|
|
265 |
if (defined('AJAX_SCRIPT') and AJAX_SCRIPT) {
|
|
|
266 |
// Moodle AJAX scripts are expected to return json data, any PHP notices or errors break it badly,
|
|
|
267 |
// developers simply must learn to watch error log.
|
|
|
268 |
define('NO_DEBUG_DISPLAY', true);
|
|
|
269 |
} else {
|
|
|
270 |
define('NO_DEBUG_DISPLAY', false);
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
// Some scripts such as upgrade may want to prevent output buffering
|
|
|
275 |
if (!defined('NO_OUTPUT_BUFFERING')) {
|
|
|
276 |
define('NO_OUTPUT_BUFFERING', false);
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
// PHPUnit tests need custom init
|
|
|
280 |
if (!defined('PHPUNIT_TEST')) {
|
|
|
281 |
define('PHPUNIT_TEST', false);
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
// Performance tests needs to always display performance info, even in redirections;
|
|
|
285 |
// MDL_PERF_TEST is used in https://github.com/moodlehq/moodle-performance-comparison scripts.
|
|
|
286 |
if (!defined('MDL_PERF_TEST')) {
|
|
|
287 |
define('MDL_PERF_TEST', false);
|
|
|
288 |
}
|
|
|
289 |
// Make sure all MDL_PERF* constants are always defined.
|
|
|
290 |
if (!defined('MDL_PERF')) {
|
|
|
291 |
define('MDL_PERF', MDL_PERF_TEST);
|
|
|
292 |
}
|
|
|
293 |
if (!defined('MDL_PERFTOFOOT')) {
|
|
|
294 |
define('MDL_PERFTOFOOT', MDL_PERF_TEST);
|
|
|
295 |
}
|
|
|
296 |
if (!defined('MDL_PERFTOLOG')) {
|
|
|
297 |
define('MDL_PERFTOLOG', false);
|
|
|
298 |
}
|
|
|
299 |
if (!defined('MDL_PERFINC')) {
|
|
|
300 |
define('MDL_PERFINC', false);
|
|
|
301 |
}
|
|
|
302 |
// Note that PHPUnit and Behat tests should pass with both MDL_PERF true and false.
|
|
|
303 |
|
|
|
304 |
// When set to true MUC (Moodle caching) will be disabled as much as possible.
|
|
|
305 |
// A special cache factory will be used to handle this situation and will use special "disabled" equivalents objects.
|
|
|
306 |
// This ensure we don't attempt to read or create the config file, don't use stores, don't provide persistence or
|
|
|
307 |
// storage of any kind.
|
|
|
308 |
if (!defined('CACHE_DISABLE_ALL')) {
|
|
|
309 |
define('CACHE_DISABLE_ALL', false);
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
// When set to true MUC (Moodle caching) will not use any of the defined or default stores.
|
|
|
313 |
// The Cache API will continue to function however this will force the use of the cachestore_dummy so all requests
|
|
|
314 |
// will be interacting with a static property and will never go to the proper cache stores.
|
|
|
315 |
// Useful if you need to avoid the stores for one reason or another.
|
|
|
316 |
if (!defined('CACHE_DISABLE_STORES')) {
|
|
|
317 |
define('CACHE_DISABLE_STORES', false);
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
// Servers should define a default timezone in php.ini, but if they don't then make sure no errors are shown.
|
|
|
321 |
date_default_timezone_set(@date_default_timezone_get());
|
|
|
322 |
|
|
|
323 |
// Detect CLI scripts - CLI scripts are executed from command line, do not have session and we do not want HTML in output
|
|
|
324 |
// In your new CLI scripts just add "define('CLI_SCRIPT', true);" before requiring config.php.
|
|
|
325 |
// Please note that one script can not be accessed from both CLI and web interface.
|
|
|
326 |
if (!defined('CLI_SCRIPT')) {
|
|
|
327 |
define('CLI_SCRIPT', false);
|
|
|
328 |
}
|
|
|
329 |
if (defined('WEB_CRON_EMULATED_CLI')) {
|
|
|
330 |
if (!isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
331 |
echo('Web cron can not be executed as CLI script any more, please use admin/cli/cron.php instead'."\n");
|
|
|
332 |
exit(1);
|
|
|
333 |
}
|
|
|
334 |
} else if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
335 |
if (CLI_SCRIPT) {
|
|
|
336 |
echo('Command line scripts can not be executed from the web interface');
|
|
|
337 |
exit(1);
|
|
|
338 |
}
|
|
|
339 |
} else {
|
|
|
340 |
if (!CLI_SCRIPT) {
|
|
|
341 |
echo('Command line scripts must define CLI_SCRIPT before requiring config.php'."\n");
|
|
|
342 |
exit(1);
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
// All web service requests have WS_SERVER == true.
|
|
|
347 |
if (!defined('WS_SERVER')) {
|
|
|
348 |
define('WS_SERVER', false);
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
// Detect CLI maintenance mode - this is useful when you need to mess with database, such as during upgrades
|
|
|
352 |
if (file_exists("$CFG->dataroot/climaintenance.html")) {
|
|
|
353 |
if (!CLI_SCRIPT) {
|
|
|
354 |
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
|
|
355 |
header('Status: 503 Moodle under maintenance');
|
|
|
356 |
header('Retry-After: 300');
|
|
|
357 |
header('Content-type: text/html; charset=utf-8');
|
|
|
358 |
header('X-UA-Compatible: IE=edge');
|
|
|
359 |
/// Headers to make it not cacheable and json
|
|
|
360 |
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
|
361 |
header('Cache-Control: post-check=0, pre-check=0', false);
|
|
|
362 |
header('Pragma: no-cache');
|
|
|
363 |
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
|
|
364 |
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
|
|
365 |
header('Accept-Ranges: none');
|
|
|
366 |
readfile("$CFG->dataroot/climaintenance.html");
|
|
|
367 |
die;
|
|
|
368 |
} else {
|
|
|
369 |
if (!defined('CLI_MAINTENANCE')) {
|
|
|
370 |
define('CLI_MAINTENANCE', true);
|
|
|
371 |
}
|
|
|
372 |
}
|
|
|
373 |
} else {
|
|
|
374 |
if (!defined('CLI_MAINTENANCE')) {
|
|
|
375 |
define('CLI_MAINTENANCE', false);
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
// Some core parts of Moodle may make use of language features not available in older PHP versions.s
|
|
|
380 |
// When this happens as part of our core bootstrap, we can end up having confusing and spurious error
|
|
|
381 |
// messages which are hard to diagnose.
|
|
|
382 |
// This check allows us to insert a very basic check for the absolute minimum version of PHP for the
|
|
|
383 |
// Moodle core to be able to load the environment and error pages.
|
|
|
384 |
// It should only be updated in these circumstances, not with every PHP version.
|
|
|
385 |
if (version_compare(PHP_VERSION, '8.1.0') < 0) {
|
|
|
386 |
$phpversion = PHP_VERSION;
|
|
|
387 |
// Do NOT localise - lang strings would not work here and we CAN NOT move it to later place.
|
|
|
388 |
echo "Moodle 4.4 or later requires at least PHP 8.1 (currently using version $phpversion).\n";
|
|
|
389 |
echo "Some servers may have multiple PHP versions installed, are you using the correct executable?\n";
|
|
|
390 |
exit(1);
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
// Detect ajax scripts - they are similar to CLI because we can not redirect, output html, etc.
|
|
|
394 |
if (!defined('AJAX_SCRIPT')) {
|
|
|
395 |
define('AJAX_SCRIPT', false);
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
// Exact version of currently used yui2 and 3 library.
|
|
|
399 |
$CFG->yui2version = '2.9.0';
|
|
|
400 |
$CFG->yui3version = '3.18.1';
|
|
|
401 |
|
|
|
402 |
// Patching the upstream YUI release.
|
|
|
403 |
// If we need to patch a YUI modules between official YUI releases, the yuipatchlevel will need to be manually
|
|
|
404 |
// incremented here. The module will also need to be listed in the yuipatchedmodules.
|
|
|
405 |
// When upgrading to a subsequent version of YUI, these should be reset back to 0 and an empty array.
|
|
|
406 |
$CFG->yuipatchlevel = 0;
|
|
|
407 |
$CFG->yuipatchedmodules = array(
|
|
|
408 |
);
|
|
|
409 |
|
|
|
410 |
if (!empty($CFG->disableonclickaddoninstall)) {
|
|
|
411 |
// This config.php flag has been merged into another one.
|
|
|
412 |
$CFG->disableupdateautodeploy = true;
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
// Store settings from config.php in array in $CFG - we can use it later to detect problems and overrides.
|
|
|
416 |
if (!isset($CFG->config_php_settings)) {
|
|
|
417 |
$CFG->config_php_settings = (array)$CFG;
|
|
|
418 |
// Forced plugin settings override values from config_plugins table.
|
|
|
419 |
unset($CFG->config_php_settings['forced_plugin_settings']);
|
|
|
420 |
if (!isset($CFG->forced_plugin_settings)) {
|
|
|
421 |
$CFG->forced_plugin_settings = array();
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
if (isset($CFG->debug)) {
|
|
|
426 |
$CFG->debug = (int)$CFG->debug;
|
|
|
427 |
} else {
|
|
|
428 |
$CFG->debug = 0;
|
|
|
429 |
}
|
|
|
430 |
$CFG->debugdeveloper = (($CFG->debug & (E_ALL | E_STRICT)) === (E_ALL | E_STRICT)); // DEBUG_DEVELOPER is not available yet.
|
|
|
431 |
|
|
|
432 |
if (!defined('MOODLE_INTERNAL')) { // Necessary because cli installer has to define it earlier.
|
|
|
433 |
/** Used by library scripts to check they are being called by Moodle. */
|
|
|
434 |
define('MOODLE_INTERNAL', true);
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
// core_component can be used in any scripts, it does not need anything else.
|
|
|
438 |
require_once($CFG->libdir .'/classes/component.php');
|
|
|
439 |
|
|
|
440 |
// special support for highly optimised scripts that do not need libraries and DB connection
|
|
|
441 |
if (defined('ABORT_AFTER_CONFIG')) {
|
|
|
442 |
if (!defined('ABORT_AFTER_CONFIG_CANCEL')) {
|
|
|
443 |
// hide debugging if not enabled in config.php - we do not want to disclose sensitive info
|
|
|
444 |
error_reporting($CFG->debug);
|
|
|
445 |
if (NO_DEBUG_DISPLAY) {
|
|
|
446 |
// Some parts of Moodle cannot display errors and debug at all.
|
|
|
447 |
ini_set('display_errors', '0');
|
|
|
448 |
ini_set('log_errors', '1');
|
|
|
449 |
} else if (empty($CFG->debugdisplay)) {
|
|
|
450 |
ini_set('display_errors', '0');
|
|
|
451 |
ini_set('log_errors', '1');
|
|
|
452 |
} else {
|
|
|
453 |
ini_set('display_errors', '1');
|
|
|
454 |
}
|
|
|
455 |
require_once("$CFG->dirroot/lib/configonlylib.php");
|
|
|
456 |
return;
|
|
|
457 |
}
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
// Early profiling start, based exclusively on config.php $CFG settings
|
|
|
461 |
if (!empty($CFG->earlyprofilingenabled)) {
|
|
|
462 |
require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
|
|
|
463 |
profiling_start();
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
/**
|
|
|
467 |
* Database connection. Used for all access to the database.
|
|
|
468 |
* @global moodle_database $DB
|
|
|
469 |
* @name $DB
|
|
|
470 |
*/
|
|
|
471 |
global $DB;
|
|
|
472 |
|
|
|
473 |
/**
|
|
|
474 |
* Moodle's wrapper round PHP's $_SESSION.
|
|
|
475 |
*
|
|
|
476 |
* @global object $SESSION
|
|
|
477 |
* @name $SESSION
|
|
|
478 |
*/
|
|
|
479 |
global $SESSION;
|
|
|
480 |
|
|
|
481 |
/**
|
|
|
482 |
* Holds the user table record for the current user. Will be the 'guest'
|
|
|
483 |
* user record for people who are not logged in.
|
|
|
484 |
*
|
|
|
485 |
* $USER is stored in the session.
|
|
|
486 |
*
|
|
|
487 |
* Items found in the user record:
|
|
|
488 |
* - $USER->email - The user's email address.
|
|
|
489 |
* - $USER->id - The unique integer identified of this user in the 'user' table.
|
|
|
490 |
* - $USER->email - The user's email address.
|
|
|
491 |
* - $USER->firstname - The user's first name.
|
|
|
492 |
* - $USER->lastname - The user's last name.
|
|
|
493 |
* - $USER->username - The user's login username.
|
|
|
494 |
* - $USER->secret - The user's ?.
|
|
|
495 |
* - $USER->lang - The user's language choice.
|
|
|
496 |
*
|
|
|
497 |
* @global object $USER
|
|
|
498 |
* @name $USER
|
|
|
499 |
*/
|
|
|
500 |
global $USER;
|
|
|
501 |
|
|
|
502 |
/**
|
|
|
503 |
* Frontpage course record
|
|
|
504 |
*/
|
|
|
505 |
global $SITE;
|
|
|
506 |
|
|
|
507 |
/**
|
|
|
508 |
* A central store of information about the current page we are
|
|
|
509 |
* generating in response to the user's request.
|
|
|
510 |
*
|
|
|
511 |
* @global moodle_page $PAGE
|
|
|
512 |
* @name $PAGE
|
|
|
513 |
*/
|
|
|
514 |
global $PAGE;
|
|
|
515 |
|
|
|
516 |
/**
|
|
|
517 |
* The current course. An alias for $PAGE->course.
|
|
|
518 |
* @global object $COURSE
|
|
|
519 |
* @name $COURSE
|
|
|
520 |
*/
|
|
|
521 |
global $COURSE;
|
|
|
522 |
|
|
|
523 |
/**
|
|
|
524 |
* $OUTPUT is an instance of core_renderer or one of its subclasses. Use
|
|
|
525 |
* it to generate HTML for output.
|
|
|
526 |
*
|
|
|
527 |
* $OUTPUT is initialised the first time it is used. See {@link bootstrap_renderer}
|
|
|
528 |
* for the magic that does that. After $OUTPUT has been initialised, any attempt
|
|
|
529 |
* to change something that affects the current theme ($PAGE->course, logged in use,
|
|
|
530 |
* httpsrequried ... will result in an exception.)
|
|
|
531 |
*
|
|
|
532 |
* Please note the $OUTPUT is replacing the old global $THEME object.
|
|
|
533 |
*
|
|
|
534 |
* @global object $OUTPUT
|
|
|
535 |
* @name $OUTPUT
|
|
|
536 |
*/
|
|
|
537 |
global $OUTPUT;
|
|
|
538 |
|
|
|
539 |
/**
|
|
|
540 |
* Full script path including all params, slash arguments, scheme and host.
|
|
|
541 |
*
|
|
|
542 |
* Note: Do NOT use for getting of current page URL or detection of https,
|
|
|
543 |
* instead use $PAGE->url or is_https().
|
|
|
544 |
*
|
|
|
545 |
* @global string $FULLME
|
|
|
546 |
* @name $FULLME
|
|
|
547 |
*/
|
|
|
548 |
global $FULLME;
|
|
|
549 |
|
|
|
550 |
/**
|
|
|
551 |
* Script path including query string and slash arguments without host.
|
|
|
552 |
* @global string $ME
|
|
|
553 |
* @name $ME
|
|
|
554 |
*/
|
|
|
555 |
global $ME;
|
|
|
556 |
|
|
|
557 |
/**
|
|
|
558 |
* $FULLME without slasharguments and query string.
|
|
|
559 |
* @global string $FULLSCRIPT
|
|
|
560 |
* @name $FULLSCRIPT
|
|
|
561 |
*/
|
|
|
562 |
global $FULLSCRIPT;
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* Relative moodle script path '/course/view.php'
|
|
|
566 |
* @global string $SCRIPT
|
|
|
567 |
* @name $SCRIPT
|
|
|
568 |
*/
|
|
|
569 |
global $SCRIPT;
|
|
|
570 |
|
|
|
571 |
// The httpswwwroot has been deprecated, we keep it as an alias for backwards compatibility with plugins only.
|
|
|
572 |
$CFG->httpswwwroot = $CFG->wwwroot;
|
|
|
573 |
|
|
|
574 |
require_once($CFG->libdir .'/setuplib.php'); // Functions that MUST be loaded first
|
|
|
575 |
|
|
|
576 |
if (NO_OUTPUT_BUFFERING) {
|
|
|
577 |
// we have to call this always before starting session because it discards headers!
|
|
|
578 |
disable_output_buffering();
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
// Increase memory limits if possible
|
|
|
582 |
raise_memory_limit(MEMORY_STANDARD);
|
|
|
583 |
|
|
|
584 |
// Time to start counting
|
|
|
585 |
init_performance_info();
|
|
|
586 |
|
|
|
587 |
// Put $OUTPUT in place, so errors can be displayed.
|
|
|
588 |
$OUTPUT = new bootstrap_renderer();
|
|
|
589 |
|
|
|
590 |
// Set handler for uncaught exceptions - equivalent to throw new \moodle_exception() call.
|
|
|
591 |
if (!PHPUNIT_TEST or PHPUNIT_UTIL) {
|
|
|
592 |
set_exception_handler('default_exception_handler');
|
|
|
593 |
set_error_handler('default_error_handler', E_ALL | E_STRICT);
|
|
|
594 |
}
|
|
|
595 |
|
|
|
596 |
// Acceptance tests needs special output to capture the errors,
|
|
|
597 |
// but not necessary for behat CLI command and init script.
|
|
|
598 |
if (defined('BEHAT_SITE_RUNNING') && !defined('BEHAT_TEST') && !defined('BEHAT_UTIL')) {
|
|
|
599 |
require_once(__DIR__ . '/behat/lib.php');
|
|
|
600 |
set_error_handler('behat_error_handler', E_ALL | E_STRICT);
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
if (defined('WS_SERVER') && WS_SERVER) {
|
|
|
604 |
require_once($CFG->dirroot . '/webservice/lib.php');
|
|
|
605 |
set_exception_handler('early_ws_exception_handler');
|
|
|
606 |
}
|
|
|
607 |
|
|
|
608 |
// If there are any errors in the standard libraries we want to know!
|
|
|
609 |
error_reporting(E_ALL | E_STRICT);
|
|
|
610 |
|
|
|
611 |
// Just say no to link prefetching (Moz prefetching, Google Web Accelerator, others)
|
|
|
612 |
// http://www.google.com/webmasters/faq.html#prefetchblock
|
|
|
613 |
if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){
|
|
|
614 |
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Prefetch Forbidden');
|
|
|
615 |
echo('Prefetch request forbidden.');
|
|
|
616 |
exit(1);
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
//point pear include path to moodles lib/pear so that includes and requires will search there for files before anywhere else
|
|
|
620 |
//the problem is that we need specific version of quickforms and hacked excel files :-(
|
|
|
621 |
ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
|
|
|
622 |
|
|
|
623 |
// Register our classloader, in theory somebody might want to replace it to load other hacked core classes.
|
|
|
624 |
if (defined('COMPONENT_CLASSLOADER')) {
|
|
|
625 |
spl_autoload_register(COMPONENT_CLASSLOADER);
|
|
|
626 |
} else {
|
|
|
627 |
spl_autoload_register('core_component::classloader');
|
|
|
628 |
}
|
|
|
629 |
|
|
|
630 |
// Remember the default PHP timezone, we will need it later.
|
|
|
631 |
core_date::store_default_php_timezone();
|
|
|
632 |
|
|
|
633 |
// Load up standard libraries
|
|
|
634 |
require_once($CFG->libdir .'/filterlib.php'); // Functions for filtering test as it is output
|
|
|
635 |
require_once($CFG->libdir .'/ajax/ajaxlib.php'); // Functions for managing our use of JavaScript and YUI
|
|
|
636 |
require_once($CFG->libdir .'/weblib.php'); // Functions relating to HTTP and content
|
|
|
637 |
require_once($CFG->libdir .'/outputlib.php'); // Functions for generating output
|
|
|
638 |
require_once($CFG->libdir .'/navigationlib.php'); // Class for generating Navigation structure
|
|
|
639 |
require_once($CFG->libdir .'/dmllib.php'); // Database access
|
|
|
640 |
require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions.
|
|
|
641 |
require_once($CFG->libdir .'/accesslib.php'); // Access control functions
|
|
|
642 |
require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility
|
|
|
643 |
require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions
|
|
|
644 |
require_once($CFG->libdir .'/enrollib.php'); // Enrolment related functions
|
|
|
645 |
require_once($CFG->libdir .'/pagelib.php'); // Library that defines the moodle_page class, used for $PAGE
|
|
|
646 |
require_once($CFG->libdir .'/blocklib.php'); // Library for controlling blocks
|
|
|
647 |
require_once($CFG->libdir .'/grouplib.php'); // Groups functions
|
|
|
648 |
require_once($CFG->libdir .'/sessionlib.php'); // All session and cookie related stuff
|
|
|
649 |
require_once($CFG->libdir .'/editorlib.php'); // All text editor related functions and classes
|
|
|
650 |
require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions
|
|
|
651 |
require_once($CFG->libdir .'/modinfolib.php'); // Cached information on course-module instances
|
|
|
652 |
require_once($CFG->dirroot.'/cache/lib.php'); // Cache API
|
|
|
653 |
|
|
|
654 |
// make sure PHP is not severly misconfigured
|
|
|
655 |
setup_validate_php_configuration();
|
|
|
656 |
|
|
|
657 |
// Connect to the database
|
|
|
658 |
setup_DB();
|
|
|
659 |
|
|
|
660 |
if (PHPUNIT_TEST and !PHPUNIT_UTIL) {
|
|
|
661 |
// Make sure tests do not run in parallel.
|
|
|
662 |
$suffix = '';
|
|
|
663 |
if (phpunit_util::is_in_isolated_process()) {
|
|
|
664 |
$suffix = '.isolated';
|
|
|
665 |
}
|
|
|
666 |
test_lock::acquire('phpunit', $suffix);
|
|
|
667 |
$dbhash = null;
|
|
|
668 |
try {
|
|
|
669 |
if ($dbhash = $DB->get_field('config', 'value', array('name'=>'phpunittest'))) {
|
|
|
670 |
// reset DB tables
|
|
|
671 |
phpunit_util::reset_database();
|
|
|
672 |
}
|
|
|
673 |
} catch (Exception $e) {
|
|
|
674 |
if ($dbhash) {
|
|
|
675 |
// we ned to reinit if reset fails
|
|
|
676 |
$DB->set_field('config', 'value', 'na', array('name'=>'phpunittest'));
|
|
|
677 |
}
|
|
|
678 |
}
|
|
|
679 |
unset($dbhash);
|
|
|
680 |
}
|
|
|
681 |
|
|
|
682 |
// Load any immutable bootstrap config from local cache.
|
11 |
efrain |
683 |
$bootstraplocalfile = $CFG->localcachedir . '/bootstrap.php';
|
|
|
684 |
$bootstrapsharedfile = $CFG->cachedir . '/bootstrap.php';
|
|
|
685 |
|
|
|
686 |
if (!is_readable($bootstraplocalfile) && is_readable($bootstrapsharedfile)) {
|
|
|
687 |
// If we don't have a local cache but do have a shared cache then clone it,
|
|
|
688 |
// for example when scaling up new front ends.
|
|
|
689 |
make_localcache_directory('', true);
|
|
|
690 |
copy($bootstrapsharedfile, $bootstraplocalfile);
|
|
|
691 |
}
|
|
|
692 |
if (is_readable($bootstraplocalfile)) {
|
1 |
efrain |
693 |
try {
|
11 |
efrain |
694 |
require_once($bootstraplocalfile);
|
1 |
efrain |
695 |
// Verify the file is not stale.
|
|
|
696 |
if (!isset($CFG->bootstraphash) || $CFG->bootstraphash !== hash_local_config_cache()) {
|
|
|
697 |
// Something has changed, the bootstrap.php file is stale.
|
|
|
698 |
unset($CFG->siteidentifier);
|
11 |
efrain |
699 |
@unlink($bootstraplocalfile);
|
|
|
700 |
@unlink($bootstrapsharedfile);
|
1 |
efrain |
701 |
}
|
|
|
702 |
} catch (Throwable $e) {
|
|
|
703 |
// If it is corrupted then attempt to delete it and it will be rebuilt.
|
11 |
efrain |
704 |
@unlink($bootstraplocalfile);
|
|
|
705 |
@unlink($bootstrapsharedfile);
|
1 |
efrain |
706 |
}
|
|
|
707 |
}
|
|
|
708 |
|
|
|
709 |
// Load up any configuration from the config table or MUC cache.
|
|
|
710 |
if (PHPUNIT_TEST) {
|
|
|
711 |
phpunit_util::initialise_cfg();
|
|
|
712 |
} else {
|
|
|
713 |
initialise_cfg();
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
if (isset($CFG->debug)) {
|
|
|
717 |
$CFG->debug = (int)$CFG->debug;
|
|
|
718 |
error_reporting($CFG->debug);
|
|
|
719 |
} else {
|
|
|
720 |
$CFG->debug = 0;
|
|
|
721 |
}
|
|
|
722 |
$CFG->debugdeveloper = (($CFG->debug & DEBUG_DEVELOPER) === DEBUG_DEVELOPER);
|
|
|
723 |
|
|
|
724 |
// Set a default value for whether to show exceptions in a pretty format.
|
|
|
725 |
if (!property_exists($CFG, 'debug_developer_use_pretty_exceptions')) {
|
|
|
726 |
$CFG->debug_developer_use_pretty_exceptions = true;
|
|
|
727 |
|
|
|
728 |
}
|
|
|
729 |
|
|
|
730 |
// Find out if PHP configured to display warnings,
|
|
|
731 |
// this is a security problem because some moodle scripts may
|
|
|
732 |
// disclose sensitive information.
|
|
|
733 |
if (ini_get_bool('display_errors')) {
|
|
|
734 |
define('WARN_DISPLAY_ERRORS_ENABLED', true);
|
|
|
735 |
}
|
|
|
736 |
// If we want to display Moodle errors, then try and set PHP errors to match.
|
|
|
737 |
if (!isset($CFG->debugdisplay)) {
|
|
|
738 |
// Keep it "as is" during installation.
|
|
|
739 |
} else if (NO_DEBUG_DISPLAY) {
|
|
|
740 |
// Some parts of Moodle cannot display errors and debug at all.
|
|
|
741 |
ini_set('display_errors', '0');
|
|
|
742 |
ini_set('log_errors', '1');
|
|
|
743 |
} else if (empty($CFG->debugdisplay)) {
|
|
|
744 |
ini_set('display_errors', '0');
|
|
|
745 |
ini_set('log_errors', '1');
|
|
|
746 |
} else {
|
|
|
747 |
// This is very problematic in XHTML strict mode!
|
|
|
748 |
ini_set('display_errors', '1');
|
|
|
749 |
}
|
|
|
750 |
|
|
|
751 |
// Register our shutdown manager, do NOT use register_shutdown_function().
|
|
|
752 |
core_shutdown_manager::initialize();
|
|
|
753 |
|
|
|
754 |
// Verify upgrade is not running unless we are in a script that needs to execute in any case
|
|
|
755 |
if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
|
|
|
756 |
if ($CFG->upgraderunning < time()) {
|
|
|
757 |
unset_config('upgraderunning');
|
|
|
758 |
} else {
|
|
|
759 |
throw new \moodle_exception('upgraderunning');
|
|
|
760 |
}
|
|
|
761 |
}
|
|
|
762 |
|
|
|
763 |
// enable circular reference collector in PHP 5.3,
|
|
|
764 |
// it helps a lot when using large complex OOP structures such as in amos or gradebook
|
|
|
765 |
if (function_exists('gc_enable')) {
|
|
|
766 |
gc_enable();
|
|
|
767 |
}
|
|
|
768 |
|
|
|
769 |
// detect unsupported upgrade jump as soon as possible - do not change anything, do not use system functions
|
|
|
770 |
if (!empty($CFG->version) and $CFG->version < 2007101509) {
|
|
|
771 |
throw new \moodle_exception('upgraderequires19', 'error');
|
|
|
772 |
die;
|
|
|
773 |
}
|
|
|
774 |
|
|
|
775 |
// Calculate and set $CFG->ostype to be used everywhere. Possible values are:
|
|
|
776 |
// - WINDOWS: for any Windows flavour.
|
|
|
777 |
// - UNIX: for the rest
|
|
|
778 |
// Also, $CFG->os can continue being used if more specialization is required
|
|
|
779 |
if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
|
|
|
780 |
$CFG->ostype = 'WINDOWS';
|
|
|
781 |
} else {
|
|
|
782 |
$CFG->ostype = 'UNIX';
|
|
|
783 |
}
|
|
|
784 |
$CFG->os = PHP_OS;
|
|
|
785 |
|
|
|
786 |
// Configure ampersands in URLs
|
|
|
787 |
ini_set('arg_separator.output', '&');
|
|
|
788 |
|
|
|
789 |
// Work around for a PHP bug see MDL-11237
|
|
|
790 |
ini_set('pcre.backtrack_limit', 20971520); // 20 MB
|
|
|
791 |
|
|
|
792 |
// Set PHP default timezone to server timezone.
|
|
|
793 |
core_date::set_default_server_timezone();
|
|
|
794 |
|
|
|
795 |
// Location of standard files
|
|
|
796 |
$CFG->wordlist = $CFG->libdir .'/wordlist.txt';
|
|
|
797 |
$CFG->moddata = 'moddata';
|
|
|
798 |
|
|
|
799 |
// neutralise nasty chars in PHP_SELF
|
|
|
800 |
if (isset($_SERVER['PHP_SELF'])) {
|
|
|
801 |
$phppos = strpos($_SERVER['PHP_SELF'], '.php');
|
|
|
802 |
if ($phppos !== false) {
|
|
|
803 |
$_SERVER['PHP_SELF'] = substr($_SERVER['PHP_SELF'], 0, $phppos+4);
|
|
|
804 |
}
|
|
|
805 |
unset($phppos);
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
// initialise ME's - this must be done BEFORE starting of session!
|
|
|
809 |
initialise_fullme();
|
|
|
810 |
|
|
|
811 |
// SYSCONTEXTID is cached in local cache to eliminate 1 query per page.
|
|
|
812 |
if (!defined('SYSCONTEXTID')) {
|
|
|
813 |
context_system::instance();
|
|
|
814 |
}
|
|
|
815 |
|
|
|
816 |
// Defining the site - aka frontpage course
|
|
|
817 |
try {
|
|
|
818 |
$SITE = get_site();
|
|
|
819 |
} catch (moodle_exception $e) {
|
|
|
820 |
$SITE = null;
|
|
|
821 |
if (empty($CFG->version)) {
|
|
|
822 |
$SITE = new stdClass();
|
|
|
823 |
$SITE->id = 1;
|
|
|
824 |
$SITE->shortname = null;
|
|
|
825 |
} else {
|
|
|
826 |
throw $e;
|
|
|
827 |
}
|
|
|
828 |
}
|
|
|
829 |
// And the 'default' course - this will usually get reset later in require_login() etc.
|
|
|
830 |
$COURSE = clone($SITE);
|
|
|
831 |
// Id of the frontpage course.
|
|
|
832 |
define('SITEID', $SITE->id);
|
|
|
833 |
|
|
|
834 |
// init session prevention flag - this is defined on pages that do not want session
|
|
|
835 |
if (CLI_SCRIPT) {
|
|
|
836 |
// no sessions in CLI scripts possible
|
|
|
837 |
define('NO_MOODLE_COOKIES', true);
|
|
|
838 |
|
|
|
839 |
} else if (WS_SERVER) {
|
|
|
840 |
// No sessions possible in web services.
|
|
|
841 |
define('NO_MOODLE_COOKIES', true);
|
|
|
842 |
|
|
|
843 |
} else if (!defined('NO_MOODLE_COOKIES')) {
|
|
|
844 |
if (empty($CFG->version) or $CFG->version < 2009011900) {
|
|
|
845 |
// no session before sessions table gets created
|
|
|
846 |
define('NO_MOODLE_COOKIES', true);
|
|
|
847 |
} else if (CLI_SCRIPT) {
|
|
|
848 |
// CLI scripts can not have session
|
|
|
849 |
define('NO_MOODLE_COOKIES', true);
|
|
|
850 |
} else {
|
|
|
851 |
define('NO_MOODLE_COOKIES', false);
|
|
|
852 |
}
|
|
|
853 |
}
|
|
|
854 |
|
|
|
855 |
// Start session and prepare global $SESSION, $USER.
|
|
|
856 |
if (empty($CFG->sessiontimeout)) {
|
|
|
857 |
$CFG->sessiontimeout = 8 * 60 * 60;
|
|
|
858 |
}
|
|
|
859 |
// Set sessiontimeoutwarning 20 minutes.
|
|
|
860 |
if (empty($CFG->sessiontimeoutwarning)) {
|
|
|
861 |
$CFG->sessiontimeoutwarning = 20 * 60;
|
|
|
862 |
}
|
|
|
863 |
|
|
|
864 |
// Allow plugins to callback just before the session is started.
|
|
|
865 |
$pluginswithfunction = get_plugins_with_function('before_session_start', 'lib.php');
|
|
|
866 |
foreach ($pluginswithfunction as $plugins) {
|
|
|
867 |
foreach ($plugins as $function) {
|
|
|
868 |
try {
|
|
|
869 |
$function();
|
|
|
870 |
} catch (Throwable $e) {
|
|
|
871 |
debugging("Exception calling '$function'", DEBUG_DEVELOPER, $e->getTrace());
|
|
|
872 |
}
|
|
|
873 |
}
|
|
|
874 |
}
|
|
|
875 |
|
|
|
876 |
\core\session\manager::start();
|
|
|
877 |
// Prevent ignoresesskey hack from getting carried over to a next page.
|
|
|
878 |
unset($USER->ignoresesskey);
|
|
|
879 |
|
|
|
880 |
if (!empty($CFG->proxylogunsafe) || !empty($CFG->proxyfixunsafe)) {
|
|
|
881 |
if (!empty($CFG->proxyfixunsafe)) {
|
|
|
882 |
require_once($CFG->libdir.'/filelib.php');
|
|
|
883 |
|
|
|
884 |
$proxyurl = get_moodle_proxy_url();
|
|
|
885 |
// This fixes stream handlers inside php.
|
|
|
886 |
$defaults = stream_context_set_default([
|
|
|
887 |
'http' => [
|
|
|
888 |
'user_agent' => \core_useragent::get_moodlebot_useragent(),
|
|
|
889 |
'proxy' => $proxyurl
|
|
|
890 |
],
|
|
|
891 |
]);
|
|
|
892 |
|
|
|
893 |
// Attempt to tell other web clients to use the proxy too. This only
|
|
|
894 |
// works for clients written in php in the same process, it will not
|
|
|
895 |
// work for with requests done in another process from an exec call.
|
|
|
896 |
putenv('http_proxy=' . $proxyurl);
|
|
|
897 |
putenv('https_proxy=' . $proxyurl);
|
|
|
898 |
putenv('HTTPS_PROXY=' . $proxyurl);
|
|
|
899 |
} else {
|
|
|
900 |
$defaults = stream_context_get_default();
|
|
|
901 |
}
|
|
|
902 |
|
|
|
903 |
if (!empty($CFG->proxylogunsafe)) {
|
|
|
904 |
stream_context_set_params($defaults, ['notification' => 'proxy_log_callback']);
|
|
|
905 |
}
|
|
|
906 |
|
|
|
907 |
}
|
|
|
908 |
|
|
|
909 |
// Set default content type and encoding, developers are still required to use
|
|
|
910 |
// echo $OUTPUT->header() everywhere, anything that gets set later should override these headers.
|
|
|
911 |
// This is intended to mitigate some security problems.
|
|
|
912 |
if (AJAX_SCRIPT) {
|
|
|
913 |
if (!core_useragent::supports_json_contenttype()) {
|
|
|
914 |
// Some bloody old IE.
|
|
|
915 |
@header('Content-type: text/plain; charset=utf-8');
|
|
|
916 |
@header('X-Content-Type-Options: nosniff');
|
|
|
917 |
} else if (!empty($_FILES)) {
|
|
|
918 |
// Some ajax code may have problems with json and file uploads.
|
|
|
919 |
@header('Content-type: text/plain; charset=utf-8');
|
|
|
920 |
} else {
|
|
|
921 |
@header('Content-type: application/json; charset=utf-8');
|
|
|
922 |
}
|
|
|
923 |
} else if (!CLI_SCRIPT) {
|
|
|
924 |
@header('Content-type: text/html; charset=utf-8');
|
|
|
925 |
}
|
|
|
926 |
|
|
|
927 |
// Initialise some variables that are supposed to be set in config.php only.
|
|
|
928 |
if (!isset($CFG->filelifetime)) {
|
|
|
929 |
$CFG->filelifetime = 60*60*6;
|
|
|
930 |
}
|
|
|
931 |
|
|
|
932 |
// Late profiling, only happening if early one wasn't started
|
|
|
933 |
if (!empty($CFG->profilingenabled)) {
|
|
|
934 |
require_once($CFG->libdir . '/xhprof/xhprof_moodle.php');
|
|
|
935 |
profiling_start();
|
|
|
936 |
}
|
|
|
937 |
|
|
|
938 |
// Hack to get around max_input_vars restrictions,
|
|
|
939 |
// we need to do this after session init to have some basic DDoS protection.
|
|
|
940 |
workaround_max_input_vars();
|
|
|
941 |
|
|
|
942 |
// Process theme change in the URL.
|
|
|
943 |
if (!empty($CFG->allowthemechangeonurl) and !empty($_GET['theme'])) {
|
|
|
944 |
// we have to use _GET directly because we do not want this to interfere with _POST
|
|
|
945 |
$urlthemename = optional_param('theme', '', PARAM_PLUGIN);
|
|
|
946 |
try {
|
|
|
947 |
$themeconfig = theme_config::load($urlthemename);
|
|
|
948 |
// Makes sure the theme can be loaded without errors.
|
|
|
949 |
if ($themeconfig->name === $urlthemename) {
|
|
|
950 |
$SESSION->theme = $urlthemename;
|
|
|
951 |
} else {
|
|
|
952 |
unset($SESSION->theme);
|
|
|
953 |
}
|
|
|
954 |
unset($themeconfig);
|
|
|
955 |
unset($urlthemename);
|
|
|
956 |
} catch (Exception $e) {
|
|
|
957 |
debugging('Failed to set the theme from the URL.', DEBUG_DEVELOPER, $e->getTrace());
|
|
|
958 |
}
|
|
|
959 |
}
|
|
|
960 |
unset($urlthemename);
|
|
|
961 |
|
|
|
962 |
// Ensure a valid theme is set.
|
|
|
963 |
if (!isset($CFG->theme)) {
|
|
|
964 |
$CFG->theme = 'boost';
|
|
|
965 |
}
|
|
|
966 |
|
|
|
967 |
// Set language/locale of printed times. If user has chosen a language that
|
|
|
968 |
// that is different from the site language, then use the locale specified
|
|
|
969 |
// in the language file. Otherwise, if the admin hasn't specified a locale
|
|
|
970 |
// then use the one from the default language. Otherwise (and this is the
|
|
|
971 |
// majority of cases), use the stored locale specified by admin.
|
|
|
972 |
// note: do not accept lang parameter from POST
|
|
|
973 |
if (isset($_GET['lang']) and ($lang = optional_param('lang', '', PARAM_SAFEDIR))) {
|
|
|
974 |
if (get_string_manager()->translation_exists($lang, false)) {
|
|
|
975 |
$SESSION->lang = $lang;
|
|
|
976 |
\core_courseformat\base::session_cache_reset_all();
|
|
|
977 |
}
|
|
|
978 |
}
|
|
|
979 |
unset($lang);
|
|
|
980 |
|
|
|
981 |
// PARAM_SAFEDIR used instead of PARAM_LANG because using PARAM_LANG results
|
|
|
982 |
// in an empty string being returned when a non-existant language is specified,
|
|
|
983 |
// which would make it necessary to log out to undo the forcelang setting.
|
|
|
984 |
// With PARAM_SAFEDIR, it's possible to specify ?forcelang=none to drop the forcelang effect.
|
|
|
985 |
if ($forcelang = optional_param('forcelang', '', PARAM_SAFEDIR)) {
|
|
|
986 |
if (isloggedin()
|
|
|
987 |
&& get_string_manager()->translation_exists($forcelang, false)
|
|
|
988 |
&& has_capability('moodle/site:forcelanguage', context_system::instance())) {
|
|
|
989 |
$SESSION->forcelang = $forcelang;
|
|
|
990 |
} else if (isset($SESSION->forcelang)) {
|
|
|
991 |
unset($SESSION->forcelang);
|
|
|
992 |
}
|
|
|
993 |
}
|
|
|
994 |
unset($forcelang);
|
|
|
995 |
|
|
|
996 |
setup_lang_from_browser();
|
|
|
997 |
|
|
|
998 |
if (empty($CFG->lang)) {
|
|
|
999 |
if (empty($SESSION->lang)) {
|
|
|
1000 |
$CFG->lang = 'en';
|
|
|
1001 |
} else {
|
|
|
1002 |
$CFG->lang = $SESSION->lang;
|
|
|
1003 |
}
|
|
|
1004 |
}
|
|
|
1005 |
|
|
|
1006 |
// Set the default site locale, a lot of the stuff may depend on this
|
|
|
1007 |
// it is definitely too late to call this first in require_login()!
|
|
|
1008 |
moodle_setlocale();
|
|
|
1009 |
|
|
|
1010 |
// Create the $PAGE global - this marks the PAGE and OUTPUT fully initialised, this MUST be done at the end of setup!
|
|
|
1011 |
if (!empty($CFG->moodlepageclass)) {
|
|
|
1012 |
if (!empty($CFG->moodlepageclassfile)) {
|
|
|
1013 |
require_once($CFG->moodlepageclassfile);
|
|
|
1014 |
}
|
|
|
1015 |
$classname = $CFG->moodlepageclass;
|
|
|
1016 |
} else {
|
|
|
1017 |
$classname = 'moodle_page';
|
|
|
1018 |
}
|
|
|
1019 |
$PAGE = new $classname();
|
|
|
1020 |
unset($classname);
|
|
|
1021 |
|
|
|
1022 |
|
|
|
1023 |
if (!empty($CFG->debugvalidators) and !empty($CFG->guestloginbutton)) {
|
|
|
1024 |
if ($CFG->theme == 'standard') { // Temporary measure to help with XHTML validation
|
|
|
1025 |
if (isset($_SERVER['HTTP_USER_AGENT']) and empty($USER->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest)
|
|
|
1026 |
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
|
|
|
1027 |
(strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
|
|
|
1028 |
if ($user = get_complete_user_data("username", "w3cvalidator")) {
|
|
|
1029 |
$user->ignoresesskey = true;
|
|
|
1030 |
} else {
|
|
|
1031 |
$user = guest_user();
|
|
|
1032 |
}
|
|
|
1033 |
\core\session\manager::set_user($user);
|
|
|
1034 |
}
|
|
|
1035 |
}
|
|
|
1036 |
}
|
|
|
1037 |
}
|
|
|
1038 |
|
|
|
1039 |
// Apache log integration. In apache conf file one can use ${MOODULEUSER}n in
|
|
|
1040 |
// LogFormat to get the current logged in username in moodle.
|
|
|
1041 |
// Alternatvely for other web servers a header X-MOODLEUSER can be set which
|
|
|
1042 |
// can be using in the logfile and stripped out if needed.
|
|
|
1043 |
set_access_log_user();
|
|
|
1044 |
|
|
|
1045 |
if (CLI_SCRIPT && !empty($CFG->version)) {
|
|
|
1046 |
// Allow auth plugins to optionally authenticate users on the CLI.
|
|
|
1047 |
require_once($CFG->libdir. '/authlib.php');
|
|
|
1048 |
auth_plugin_base::login_cli_admin_user();
|
|
|
1049 |
}
|
|
|
1050 |
|
|
|
1051 |
// Ensure the urlrewriteclass is setup correctly (to avoid crippling site).
|
|
|
1052 |
if (isset($CFG->urlrewriteclass)) {
|
|
|
1053 |
if (!class_exists($CFG->urlrewriteclass)) {
|
|
|
1054 |
debugging("urlrewriteclass {$CFG->urlrewriteclass} was not found, disabling.");
|
|
|
1055 |
unset($CFG->urlrewriteclass);
|
|
|
1056 |
} else if (!in_array('core\output\url_rewriter', class_implements($CFG->urlrewriteclass))) {
|
|
|
1057 |
debugging("{$CFG->urlrewriteclass} does not implement core\output\url_rewriter, disabling.", DEBUG_DEVELOPER);
|
|
|
1058 |
unset($CFG->urlrewriteclass);
|
|
|
1059 |
}
|
|
|
1060 |
}
|
|
|
1061 |
|
|
|
1062 |
// Use a custom script replacement if one exists
|
|
|
1063 |
if (!empty($CFG->customscripts)) {
|
|
|
1064 |
if (($customscript = custom_script_path()) !== false) {
|
|
|
1065 |
require ($customscript);
|
|
|
1066 |
}
|
|
|
1067 |
}
|
|
|
1068 |
|
|
|
1069 |
if (PHPUNIT_TEST) {
|
|
|
1070 |
// no ip blocking, these are CLI only
|
|
|
1071 |
} else if (CLI_SCRIPT and !defined('WEB_CRON_EMULATED_CLI')) {
|
|
|
1072 |
// no ip blocking
|
|
|
1073 |
} else if (!empty($CFG->allowbeforeblock)) { // allowed list processed before blocked list?
|
|
|
1074 |
// in this case, ip in allowed list will be performed first
|
|
|
1075 |
// for example, client IP is 192.168.1.1
|
|
|
1076 |
// 192.168 subnet is an entry in allowed list
|
|
|
1077 |
// 192.168.1.1 is banned in blocked list
|
|
|
1078 |
// This ip will be banned finally
|
|
|
1079 |
if (!empty($CFG->allowedip)) {
|
|
|
1080 |
if (!remoteip_in_list($CFG->allowedip)) {
|
|
|
1081 |
die(get_string('ipblocked', 'admin'));
|
|
|
1082 |
}
|
|
|
1083 |
}
|
|
|
1084 |
// need further check, client ip may a part of
|
|
|
1085 |
// allowed subnet, but a IP address are listed
|
|
|
1086 |
// in blocked list.
|
|
|
1087 |
if (!empty($CFG->blockedip)) {
|
|
|
1088 |
if (remoteip_in_list($CFG->blockedip)) {
|
|
|
1089 |
die(get_string('ipblocked', 'admin'));
|
|
|
1090 |
}
|
|
|
1091 |
}
|
|
|
1092 |
|
|
|
1093 |
} else {
|
|
|
1094 |
// in this case, IPs in blocked list will be performed first
|
|
|
1095 |
// for example, client IP is 192.168.1.1
|
|
|
1096 |
// 192.168 subnet is an entry in blocked list
|
|
|
1097 |
// 192.168.1.1 is allowed in allowed list
|
|
|
1098 |
// This ip will be allowed finally
|
|
|
1099 |
if (!empty($CFG->blockedip)) {
|
|
|
1100 |
if (remoteip_in_list($CFG->blockedip)) {
|
|
|
1101 |
// if the allowed ip list is not empty
|
|
|
1102 |
// IPs are not included in the allowed list will be
|
|
|
1103 |
// blocked too
|
|
|
1104 |
if (!empty($CFG->allowedip)) {
|
|
|
1105 |
if (!remoteip_in_list($CFG->allowedip)) {
|
|
|
1106 |
die(get_string('ipblocked', 'admin'));
|
|
|
1107 |
}
|
|
|
1108 |
} else {
|
|
|
1109 |
die(get_string('ipblocked', 'admin'));
|
|
|
1110 |
}
|
|
|
1111 |
}
|
|
|
1112 |
}
|
|
|
1113 |
// if blocked list is null
|
|
|
1114 |
// allowed list should be tested
|
|
|
1115 |
if(!empty($CFG->allowedip)) {
|
|
|
1116 |
if (!remoteip_in_list($CFG->allowedip)) {
|
|
|
1117 |
die(get_string('ipblocked', 'admin'));
|
|
|
1118 |
}
|
|
|
1119 |
}
|
|
|
1120 |
|
|
|
1121 |
}
|
|
|
1122 |
|
|
|
1123 |
// // try to detect IE6 and prevent gzip because it is extremely buggy browser
|
|
|
1124 |
if (!empty($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
|
|
|
1125 |
ini_set('zlib.output_compression', 'Off');
|
|
|
1126 |
if (function_exists('apache_setenv')) {
|
|
|
1127 |
apache_setenv('no-gzip', 1);
|
|
|
1128 |
}
|
|
|
1129 |
}
|
|
|
1130 |
|
|
|
1131 |
// Switch to CLI maintenance mode if required, we need to do it here after all the settings are initialised.
|
|
|
1132 |
if (isset($CFG->maintenance_later) and $CFG->maintenance_later <= time()) {
|
|
|
1133 |
if (!file_exists("$CFG->dataroot/climaintenance.html")) {
|
|
|
1134 |
require_once("$CFG->libdir/adminlib.php");
|
|
|
1135 |
enable_cli_maintenance_mode();
|
|
|
1136 |
}
|
|
|
1137 |
unset_config('maintenance_later');
|
|
|
1138 |
if (AJAX_SCRIPT) {
|
|
|
1139 |
die;
|
|
|
1140 |
} else if (!CLI_SCRIPT) {
|
|
|
1141 |
redirect(new moodle_url('/'));
|
|
|
1142 |
}
|
|
|
1143 |
}
|
|
|
1144 |
|
|
|
1145 |
// Add behat_shutdown_function to shutdown manager, so we can capture php errors,
|
|
|
1146 |
// but not necessary for behat CLI command as it's being captured by behat process.
|
|
|
1147 |
if (defined('BEHAT_SITE_RUNNING') && !defined('BEHAT_TEST')) {
|
|
|
1148 |
core_shutdown_manager::register_function('behat_shutdown_function');
|
|
|
1149 |
}
|
|
|
1150 |
|
|
|
1151 |
// note: we can not block non utf-8 installations here, because empty mysql database
|
|
|
1152 |
// might be converted to utf-8 in admin/index.php during installation
|
|
|
1153 |
|
|
|
1154 |
|
|
|
1155 |
|
|
|
1156 |
// this is a funny trick to make Eclipse believe that $OUTPUT and other globals
|
|
|
1157 |
// contains an instance of core_renderer, etc. which in turn fixes autocompletion ;-)
|
|
|
1158 |
if (false) {
|
|
|
1159 |
$DB = new moodle_database();
|
|
|
1160 |
$OUTPUT = new core_renderer(null, null);
|
|
|
1161 |
$PAGE = new moodle_page();
|
|
|
1162 |
}
|
|
|
1163 |
|
|
|
1164 |
// Cache any immutable config locally to avoid constant DB lookups.
|
|
|
1165 |
initialise_local_config_cache();
|
|
|
1166 |
|
|
|
1167 |
// Allow plugins to callback as soon possible after setup.php is loaded.
|
|
|
1168 |
$pluginswithfunction = get_plugins_with_function('after_config', 'lib.php');
|
|
|
1169 |
foreach ($pluginswithfunction as $plugins) {
|
|
|
1170 |
foreach ($plugins as $function) {
|
|
|
1171 |
try {
|
|
|
1172 |
$function();
|
|
|
1173 |
} catch (Throwable $e) {
|
|
|
1174 |
debugging("Exception calling '$function'", DEBUG_DEVELOPER, $e->getTrace());
|
|
|
1175 |
}
|
|
|
1176 |
}
|
|
|
1177 |
}
|