MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / lib / setup.php
blob53ce9e4b59f4770eee8ecc6acd80c9b43d9ada27
1 <?php
2 /**
3 * setup.php - Sets up sessions, connects to databases and so on
5 * Normally this is only called by the main config.php file
6 * Normally this file does not need to be edited.
7 * @author Martin Dougiamas
8 * @version $Id$
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package moodlecore
13 ////// DOCUMENTATION IN PHPDOC FORMAT FOR MOODLE GLOBALS AND COMMON OBJECT TYPES /////////////
14 /**
15 * $USER is a global instance of a typical $user record.
17 * Items found in the user record:
18 * - $USER->emailstop - Does the user want email sent to them?
19 * - $USER->email - The user's email address.
20 * - $USER->id - The unique integer identified of this user in the 'user' table.
21 * - $USER->email - The user's email address.
22 * - $USER->firstname - The user's first name.
23 * - $USER->lastname - The user's last name.
24 * - $USER->username - The user's login username.
25 * - $USER->secret - The user's ?.
26 * - $USER->lang - The user's language choice.
28 * @global object(user) $USER
30 global $USER;
31 /**
32 * This global variable is read in from the 'config' table.
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 index directory on server's filesystem.
37 * - $CFG->libdir - Path to moodle's library folder on server's filesystem.
39 * @global object(cfg) $CFG
41 global $CFG;
42 /**
43 * Definition of session type
44 * @global object(session) $SESSION
46 global $SESSION;
47 /**
48 * Definition of shared memory cache
50 global $MCACHE;
51 /**
52 * Definition of course type
53 * @global object(course) $COURSE
55 global $COURSE;
56 /**
57 * Definition of db type
58 * @global object(db) $db
60 global $db;
61 /**
62 * $THEME is a global that defines the site theme.
64 * Items found in the theme record:
65 * - $THEME->cellheading - Cell colors.
66 * - $THEME->cellheading2 - Alternate cell colors.
68 * @global object(theme) $THEME
70 global $THEME;
72 /**
73 * HTTPSPAGEREQUIRED is a global to define if the page being displayed must run under HTTPS.
75 * It's primary goal is to allow 100% HTTPS pages when $CFG->loginhttps is enabled. Default to false.
76 * It's enabled only by the httpsrequired() function and used in some pages to update some URLs
78 global $HTTPSPAGEREQUIRED;
81 /// First try to detect some attacks on older buggy PHP versions
82 if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) {
83 die('Fatal: Illegal GLOBALS overwrite attempt detected!');
87 if (!isset($CFG->wwwroot)) {
88 trigger_error('Fatal: $CFG->wwwroot is not configured! Exiting.');
89 die;
92 /// Set httpswwwroot default value (this variable will replace $CFG->wwwroot
93 /// inside some URLs used in HTTPSPAGEREQUIRED pages.
94 $CFG->httpswwwroot = $CFG->wwwroot;
96 $CFG->libdir = $CFG->dirroot .'/lib';
98 require_once($CFG->libdir .'/setuplib.php'); // Functions that MUST be loaded first
100 /// Time to start counting
101 init_performance_info();
104 /// If there are any errors in the standard libraries we want to know!
105 error_reporting(E_ALL);
107 /// Just say no to link prefetching (Moz prefetching, Google Web Accelerator, others)
108 /// http://www.google.com/webmasters/faq.html#prefetchblock
109 if (!empty($_SERVER['HTTP_X_moz']) && $_SERVER['HTTP_X_moz'] === 'prefetch'){
110 header($_SERVER['SERVER_PROTOCOL'] . ' 404 Prefetch Forbidden');
111 trigger_error('Prefetch request forbidden.');
112 exit;
115 /// Connect to the database using adodb
117 /// Some defines required BEFORE including AdoDB library
118 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC
119 //(only meaningful for oci8po, it's the default
120 //for other DB drivers so this won't affect them)
122 require_once($CFG->libdir .'/adodb/adodb.inc.php'); // Database access functions
124 $db = &ADONewConnection($CFG->dbtype);
126 // See MDL-6760 for why this is necessary. In Moodle 1.8, once we start using NULLs properly,
127 // we probably want to change this value to ''.
128 $db->null2null = 'A long random string that will never, ever match something we want to insert into the database, I hope. \'';
130 error_reporting(0); // Hide errors
132 if (!isset($CFG->dbpersist) or !empty($CFG->dbpersist)) { // Use persistent connection (default)
133 $dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
134 } else { // Use single connection
135 $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
137 if (! $dbconnected) {
138 // In the name of protocol correctness, monitoring and performance
139 // profiling, set the appropriate error headers for machine comsumption
140 if (isset($_SERVER['SERVER_PROTOCOL'])) {
141 // Avoid it with cron.php. Note that we assume it's HTTP/1.x
142 header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable');
144 // and then for human consumption...
145 echo '<html><body>';
146 echo '<table align="center"><tr>';
147 echo '<td style="color:#990000; text-align:center; font-size:large; border-width:1px; '.
148 ' border-color:#000000; border-style:solid; border-radius: 20px; border-collapse: collapse; '.
149 ' -moz-border-radius: 20px; padding: 15px">';
150 echo '<p>Error: Database connection failed.</p>';
151 echo '<p>It is possible that the database is overloaded or otherwise not running properly.</p>';
152 echo '<p>The site administrator should also check that the database details have been correctly specified in config.php</p>';
153 echo '</td></tr></table>';
154 echo '</body></html>';
156 if (!empty($CFG->emailconnectionerrorsto)) {
157 mail($CFG->emailconnectionerrorsto,
158 'WARNING: Database connection error: '.$CFG->wwwroot,
159 'Connection error: '.$CFG->wwwroot);
161 die;
164 /// Forcing ASSOC mode for ADOdb (some DBs default to FETCH_BOTH)
165 $db->SetFetchMode(ADODB_FETCH_ASSOC);
167 /// Starting here we have a correct DB conection but me must avoid
168 /// to execute any DB transaction until "set names" has been executed
169 /// some lines below!
171 error_reporting(E_ALL); // Show errors from now on.
173 if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
174 $CFG->prefix = '';
178 /// Define admin directory
180 if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
181 $CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
184 /// Increase memory limits if possible
185 raise_memory_limit('96M'); // We should never NEED this much but just in case...
187 /// Load up standard libraries
189 require_once($CFG->libdir .'/textlib.class.php'); // Functions to handle multibyte strings
190 require_once($CFG->libdir .'/weblib.php'); // Functions for producing HTML
191 require_once($CFG->libdir .'/dmllib.php'); // Functions to handle DB data (DML)
192 require_once($CFG->libdir .'/datalib.php'); // Legacy lib with a big-mix of functions.
193 require_once($CFG->libdir .'/accesslib.php'); // Access control functions
194 require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility
195 require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions
196 require_once($CFG->libdir .'/eventslib.php'); // Events functions
197 require_once($CFG->libdir .'/grouplib.php'); // Groups functions
199 /// Disable errors for now - needed for installation when debug enabled in config.php
200 if (isset($CFG->debug)) {
201 $originalconfigdebug = $CFG->debug;
202 unset($CFG->debug);
203 } else {
204 $originalconfigdebug = -1;
207 /// Set the client/server and connection to utf8
208 /// and configure some other specific variables for each db
209 configure_dbconnection();
211 /// Load up any configuration from the config table
212 unset($CFG->rcache);
213 $CFG = get_config();
215 /// Turn on SQL logging if required
216 if (!empty($CFG->logsql)) {
217 $db->LogSQL();
220 /// Prevent warnings from roles when upgrading with debug on
221 if (isset($CFG->debug)) {
222 $originaldatabasedebug = $CFG->debug;
223 unset($CFG->debug);
224 } else {
225 $originaldatabasedebug = -1;
229 /// For now, only needed under apache (and probably unstable in other contexts)
230 if (function_exists('register_shutdown_function')) {
231 register_shutdown_function('moodle_request_shutdown');
234 /// Defining the site
235 if ($SITE = get_site()) {
237 * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1.
239 define('SITEID', $SITE->id);
240 /// And the 'default' course
241 $COURSE = clone($SITE); // For now. This will usually get reset later in require_login() etc.
242 } else {
244 * @ignore
246 define('SITEID', 1);
247 /// And the 'default' course
248 $COURSE = new object; // no site created yet
249 $COURSE->id = 1;
252 if ($sysctxid = get_field('context', 'id', 'contextlevel', CONTEXT_SYSTEM)) {
253 define('SYSCONTEXTID', $sysctxid);
254 } else {
255 define('SYSCONTEXTID', 1);
258 /// Set error reporting back to normal
259 if ($originaldatabasedebug == -1) {
260 $CFG->debug = DEBUG_MINIMAL;
261 } else {
262 $CFG->debug = $originaldatabasedebug;
264 if ($originalconfigdebug !== -1) {
265 $CFG->debug = $originalconfigdebug;
267 unset($originalconfigdebug);
268 unset($originaldatabasedebug);
269 error_reporting($CFG->debug);
272 /// If we want to display Moodle errors, then try and set PHP errors to match
273 if (!isset($CFG->debugdisplay)) {
274 //keep it as is during installation
275 } else if (empty($CFG->debugdisplay)) {
276 @ini_set('display_errors', '0');
277 @ini_set('log_errors', '1');
278 } else {
279 @ini_set('display_errors', '1');
282 /// Shared-Memory cache init -- will set $MCACHE
283 /// $MCACHE is a global object that offers at least add(), set() and delete()
284 /// with similar semantics to the memcached PHP API http://php.net/memcache
285 if (!empty($CFG->cachetype)) {
286 if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) {
287 if (!init_memcached()) {
288 debugging("Error initialising memcached");
290 } elseif ($CFG->cachetype === 'eaccelerator') {
291 if (!init_eaccelerator()) {
292 debugging("Error initialising eaccelerator cache");
295 } else { // just make sure it is defined
296 $CFG->cachetype = '';
298 /// Ensure we define rcache - so we can later check for it
299 /// with a really fast and unambiguous $CFG->rcache === false
300 if (empty($CFG->rcache)) {
301 $CFG->rcache = false;
302 } else {
303 $CFG->rcache = true;
306 /// Set a default enrolment configuration (see bug 1598)
307 if (!isset($CFG->enrol)) {
308 $CFG->enrol = 'manual';
311 /// Set default enabled enrolment plugins
312 if (!isset($CFG->enrol_plugins_enabled)) {
313 $CFG->enrol_plugins_enabled = 'manual';
316 /// File permissions on created directories in the $CFG->dataroot
318 if (empty($CFG->directorypermissions)) {
319 $CFG->directorypermissions = 0777; // Must be octal (that's why it's here)
322 /// Calculate and set $CFG->ostype to be used everywhere. Possible values are:
323 /// - WINDOWS: for any Windows flavour.
324 /// - UNIX: for the rest
325 /// Also, $CFG->os can continue being used if more specialization is required
326 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
327 $CFG->ostype = 'WINDOWS';
328 } else {
329 $CFG->ostype = 'UNIX';
331 $CFG->os = PHP_OS;
333 /// Set up default frame target string, based on $CFG->framename
334 $CFG->frametarget = frametarget();
336 /// Setup cache dir for Smarty and others
337 if (!file_exists($CFG->dataroot .'/cache')) {
338 make_upload_directory('cache');
341 /// Set up smarty template system
342 //require_once($CFG->libdir .'/smarty/Smarty.class.php');
343 //$smarty = new Smarty;
344 //$smarty->template_dir = $CFG->dirroot .'/templates/'. $CFG->template;
345 //if (!file_exists($CFG->dataroot .'/cache/smarty')) {
346 // make_upload_directory('cache/smarty');
348 //$smarty->compile_dir = $CFG->dataroot .'/cache/smarty';
350 /// Set up session handling
351 if(empty($CFG->respectsessionsettings)) {
352 if (empty($CFG->dbsessions)) { /// File-based sessions
354 // Some distros disable GC by setting probability to 0
355 // overriding the PHP default of 1
356 // (gc_probability is divided by gc_divisor, which defaults to 1000)
357 if (ini_get('session.gc_probability') == 0) {
358 ini_set('session.gc_probability', 1);
361 if (!empty($CFG->sessiontimeout)) {
362 ini_set('session.gc_maxlifetime', $CFG->sessiontimeout);
365 if (!file_exists($CFG->dataroot .'/sessions')) {
366 make_upload_directory('sessions');
368 ini_set('session.save_path', $CFG->dataroot .'/sessions');
370 } else { /// Database sessions
371 ini_set('session.save_handler', 'user');
373 $ADODB_SESSION_DRIVER = $CFG->dbtype;
374 $ADODB_SESSION_CONNECT = $CFG->dbhost;
375 $ADODB_SESSION_USER = $CFG->dbuser;
376 $ADODB_SESSION_PWD = $CFG->dbpass;
377 $ADODB_SESSION_DB = $CFG->dbname;
378 $ADODB_SESSION_TBL = $CFG->prefix.'sessions2';
379 if (!empty($CFG->sessiontimeout)) {
380 $ADODB_SESS_LIFE = $CFG->sessiontimeout;
383 require_once($CFG->libdir. '/adodb/session/adodb-session2.php');
386 /// Set sessioncookie variable if it isn't already
387 if (!isset($CFG->sessioncookie)) {
388 $CFG->sessioncookie = '';
391 /// Configure ampersands in URLs
393 @ini_set('arg_separator.output', '&amp;');
395 /// Work around for a PHP bug see MDL-11237
397 @ini_set('pcre.backtrack_limit', 20971520); // 20 MB
399 /// Location of standard files
401 $CFG->wordlist = $CFG->libdir .'/wordlist.txt';
402 $CFG->javascript = $CFG->libdir .'/javascript.php';
403 $CFG->moddata = 'moddata';
406 /// A hack to get around magic_quotes_gpc being turned off
407 /// It is strongly recommended to enable "magic_quotes_gpc"!
409 if (!ini_get_bool('magic_quotes_gpc') ) {
410 function addslashes_deep($value) {
411 $value = is_array($value) ?
412 array_map('addslashes_deep', $value) :
413 addslashes($value);
414 return $value;
416 $_POST = array_map('addslashes_deep', $_POST);
417 $_GET = array_map('addslashes_deep', $_GET);
418 $_COOKIE = array_map('addslashes_deep', $_COOKIE);
419 $_REQUEST = array_map('addslashes_deep', $_REQUEST);
420 if (!empty($_SERVER['REQUEST_URI'])) {
421 $_SERVER['REQUEST_URI'] = addslashes($_SERVER['REQUEST_URI']);
423 if (!empty($_SERVER['QUERY_STRING'])) {
424 $_SERVER['QUERY_STRING'] = addslashes($_SERVER['QUERY_STRING']);
426 if (!empty($_SERVER['HTTP_REFERER'])) {
427 $_SERVER['HTTP_REFERER'] = addslashes($_SERVER['HTTP_REFERER']);
429 if (!empty($_SERVER['PATH_INFO'])) {
430 $_SERVER['PATH_INFO'] = addslashes($_SERVER['PATH_INFO']);
432 if (!empty($_SERVER['PHP_SELF'])) {
433 $_SERVER['PHP_SELF'] = addslashes($_SERVER['PHP_SELF']);
435 if (!empty($_SERVER['PATH_TRANSLATED'])) {
436 $_SERVER['PATH_TRANSLATED'] = addslashes($_SERVER['PATH_TRANSLATED']);
441 /// The following code can emulate "register globals" if required.
442 /// This hack is no longer being applied as of Moodle 1.6 unless you really
443 /// really want to use it (by defining $CFG->enableglobalshack = true)
445 if (!empty($CFG->enableglobalshack)) {
446 if (!empty($CFG->detect_unchecked_vars)) {
447 global $UNCHECKED_VARS;
448 $UNCHECKED_VARS->url = $_SERVER['PHP_SELF'];
449 $UNCHECKED_VARS->vars = array();
452 if (isset($_GET)) {
453 extract($_GET, EXTR_SKIP); // Skip existing variables, ie CFG
454 if (!empty($CFG->detect_unchecked_vars)) {
455 foreach ($_GET as $key => $val) {
456 $UNCHECKED_VARS->vars[$key]=$val;
460 if (isset($_POST)) {
461 extract($_POST, EXTR_SKIP); // Skip existing variables, ie CFG
462 if (!empty($CFG->detect_unchecked_vars)) {
463 foreach ($_POST as $key => $val) {
464 $UNCHECKED_VARS->vars[$key]=$val;
468 if (isset($_SERVER)) {
469 extract($_SERVER);
474 /// Load up global environment variables
476 class object {};
478 //discard session ID from POST, GET and globals to tighten security,
479 //this session fixation prevention can not be used in cookieless mode
480 if (empty($CFG->usesid)) {
481 unset(${'MoodleSession'.$CFG->sessioncookie});
482 unset($_GET['MoodleSession'.$CFG->sessioncookie]);
483 unset($_POST['MoodleSession'.$CFG->sessioncookie]);
485 //compatibility hack for Moodle Cron, cookies not deleted, but set to "deleted" - should not be needed with $nomoodlecookie in cron.php now
486 if (!empty($_COOKIE['MoodleSession'.$CFG->sessioncookie]) && $_COOKIE['MoodleSession'.$CFG->sessioncookie] == "deleted") {
487 unset($_COOKIE['MoodleSession'.$CFG->sessioncookie]);
489 if (!empty($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie]) && $_COOKIE['MoodleSessionTest'.$CFG->sessioncookie] == "deleted") {
490 unset($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie]);
492 if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
493 require_once("$CFG->dirroot/lib/cookieless.php");
494 sid_start_ob();
497 if (empty($nomoodlecookie)) {
498 session_name('MoodleSession'.$CFG->sessioncookie);
499 @session_start();
500 if (! isset($_SESSION['SESSION'])) {
501 $_SESSION['SESSION'] = new object;
502 $_SESSION['SESSION']->session_test = random_string(10);
503 if (!empty($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie])) {
504 $_SESSION['SESSION']->has_timed_out = true;
506 setcookie('MoodleSessionTest'.$CFG->sessioncookie, $_SESSION['SESSION']->session_test, 0, '/');
507 $_COOKIE['MoodleSessionTest'.$CFG->sessioncookie] = $_SESSION['SESSION']->session_test;
509 if (! isset($_SESSION['USER'])) {
510 $_SESSION['USER'] = new object;
513 $SESSION = &$_SESSION['SESSION']; // Makes them easier to reference
514 $USER = &$_SESSION['USER'];
515 if (!isset($USER->id)) {
516 $USER->id = 0; // to enable proper function of $CFG->notloggedinroleid hack
519 else {
520 $SESSION = NULL;
521 $USER = new object();
522 $USER->id = 0; // user not logged in when session disabled
525 if (defined('FULLME')) { // Usually in command-line scripts like admin/cron.php
526 $FULLME = FULLME;
527 $ME = FULLME;
528 } else {
529 $FULLME = qualified_me();
530 $ME = strip_querystring($FULLME);
533 /// In VERY rare cases old PHP server bugs (it has been found on PHP 4.1.2 running
534 /// as a CGI under IIS on Windows) may require that you uncomment the following:
535 // session_register("USER");
536 // session_register("SESSION");
540 /// Load up theme variables (colours etc)
542 if (!isset($CFG->themedir)) {
543 $CFG->themedir = $CFG->dirroot.'/theme';
544 $CFG->themewww = $CFG->wwwroot.'/theme';
546 $CFG->httpsthemewww = $CFG->themewww;
548 if (isset($_GET['theme'])) {
549 if ($CFG->allowthemechangeonurl || confirm_sesskey()) {
550 $themename = clean_param($_GET['theme'], PARAM_SAFEDIR);
551 if (($themename != '') and file_exists($CFG->themedir.'/'.$themename)) {
552 $SESSION->theme = $themename;
554 unset($themename);
558 if (!isset($CFG->theme)) {
559 $CFG->theme = 'standardwhite';
562 /// now do a session test to prevent random user switching - observed on some PHP/Apache combinations,
563 /// disable checks when working in cookieless mode
564 if (empty($CFG->usesid) || !empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
565 if ($SESSION != NULL) {
566 if (empty($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie])) {
567 report_session_error();
568 } else if (isset($SESSION->session_test) && $_COOKIE['MoodleSessionTest'.$CFG->sessioncookie] != $SESSION->session_test) {
569 report_session_error();
575 /// Set language/locale of printed times. If user has chosen a language that
576 /// that is different from the site language, then use the locale specified
577 /// in the language file. Otherwise, if the admin hasn't specified a locale
578 /// then use the one from the default language. Otherwise (and this is the
579 /// majority of cases), use the stored locale specified by admin.
580 if ($SESSION !== NULL && isset($_GET['lang']) && ($lang = clean_param($_GET['lang'], PARAM_SAFEDIR))) {
581 if (file_exists($CFG->dataroot .'/lang/'. $lang) or file_exists($CFG->dirroot .'/lang/'. $lang)) {
582 $SESSION->lang = $lang;
583 } else if (file_exists($CFG->dataroot.'/lang/'.$lang.'_utf8') or
584 file_exists($CFG->dirroot .'/lang/'.$lang.'_utf8')) {
585 $SESSION->lang = $lang.'_utf8';
589 setup_lang_from_browser();
591 unset($lang);
593 if (empty($CFG->lang)) {
594 if (empty($SESSION->lang)) {
595 $CFG->lang = 'en_utf8';
596 } else {
597 $CFG->lang = $SESSION->lang;
601 // set default locale and themes - might be changed again later from require_login()
602 course_setup();
604 if (!empty($CFG->opentogoogle)) {
605 if (empty($USER->id)) { // Ignore anyone logged in
606 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
607 if (strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false ) {
608 $USER = guest_user();
609 } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'google.com') !== false ) { // Google
610 $USER = guest_user();
611 } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'Yahoo! Slurp') !== false ) { // Yahoo
612 $USER = guest_user();
613 } else if (strpos($_SERVER['HTTP_USER_AGENT'], '[ZSEBOT]') !== false ) { // Zoomspider
614 $USER = guest_user();
615 } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSNBOT') !== false ) { // MSN Search
616 $USER = guest_user();
619 if (empty($USER) && !empty($_SERVER['HTTP_REFERER'])) {
620 if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
621 $USER = guest_user();
622 } else if (strpos($_SERVER['HTTP_REFERER'], 'altavista') !== false ) {
623 $USER = guest_user();
626 if (!empty($USER)) {
627 load_all_capabilities();
632 if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation
633 if (isset($_SERVER['HTTP_USER_AGENT']) and empty($_SESSION['USER']->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest)
634 if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
635 (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
636 if ($USER = get_complete_user_data("username", "w3cvalidator")) {
637 $USER->ignoresesskey = true;
638 } else {
639 $USER = guest_user();
645 /// Apache log intergration. In apache conf file one can use ${MOODULEUSER}n in
646 /// LogFormat to get the current logged in username in moodle.
647 if ($USER && function_exists('apache_note')
648 && !empty($CFG->apacheloguser) && isset($user->username)) {
649 $apachelog_userid = $USER->id;
650 $apachelog_username = clean_filename($USER->username);
651 $apachelog_name = '';
652 if (isset($USER->firstname)) {
653 // We can assume both will be set
654 // - even if to empty.
655 $apachelog_name = clean_filename($USER->firstname . " " .
656 $USER->lastname);
658 if (isset($USER->realuser)) {
659 if ($realuser = get_record('user', 'id', $USER->realuser)) {
660 $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username);
661 $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name);
662 $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid);
665 switch ($CFG->apacheloguser) {
666 case 3:
667 $logname = $apachelog_username;
668 break;
669 case 2:
670 $logname = $apachelog_name;
671 break;
672 case 1:
673 default:
674 $logname = $apachelog_userid;
675 break;
677 apache_note('MOODLEUSER', $logname);
680 /// Adjust ALLOWED_TAGS
681 adjust_allowed_tags();
684 /// Use a custom script replacement if one exists
685 if (!empty($CFG->customscripts)) {
686 if (($customscript = custom_script_path()) !== false) {
687 require ($customscript);
691 /// note: we can not block non utf-8 installatrions here, because empty mysql database
692 /// might be converted to utf-8 in admin/index.php during installation