Changed grade_item so that its grademax is count(scale_items) and grademin is 1,...
[moodle-pu.git] / lib / setup.php
blob6d7b75ea66339dd35fbb83fc16aca2a4e3e83a5e
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
198 /// Disable errors for now - needed for installation when debug enabled in config.php
199 if (isset($CFG->debug)) {
200 $originalconfigdebug = $CFG->debug;
201 unset($CFG->debug);
202 } else {
203 $originalconfigdebug = -1;
206 /// Set the client/server and connection to utf8
207 /// and configure some other specific variables for each db
208 configure_dbconnection();
210 /// Load up any configuration from the config table
211 unset($CFG->rcache);
212 $CFG = get_config();
214 /// Turn on SQL logging if required
215 if (!empty($CFG->logsql)) {
216 $db->LogSQL();
219 /// Prevent warnings from roles when upgrading with debug on
220 if (isset($CFG->debug)) {
221 $originaldatabasedebug = $CFG->debug;
222 unset($CFG->debug);
223 } else {
224 $originaldatabasedebug = -1;
228 /// For now, only needed under apache (and probably unstable in other contexts)
229 if (function_exists('apache_child_terminate')) {
230 register_shutdown_function('moodle_request_shutdown');
233 //// Defining the site
234 if ($SITE = get_site()) {
236 * If $SITE global from {@link get_site()} is set then SITEID to $SITE->id, otherwise set to 1.
238 define('SITEID', $SITE->id);
239 /// And the 'default' course
240 $COURSE = clone($SITE); // For now. This will usually get reset later in require_login() etc.
241 } else {
243 * @ignore
245 define('SITEID', 1);
246 /// And the 'default' course
247 $COURSE = new object; // no site created yet
248 $COURSE->id = 1;
252 /// Set error reporting back to normal
253 if ($originaldatabasedebug == -1) {
254 $CFG->debug = DEBUG_MINIMAL;
255 } else {
256 $CFG->debug = $originaldatabasedebug;
258 if ($originalconfigdebug !== -1) {
259 $CFG->debug = $originalconfigdebug;
261 unset($originalconfigdebug);
262 unset($originaldatabasedebug);
263 error_reporting($CFG->debug);
266 /// If we want to display Moodle errors, then try and set PHP errors to match
267 if (!isset($CFG->debugdisplay)) {
268 //keep it as is during installation
269 } else if (empty($CFG->debugdisplay)) {
270 @ini_set('display_errors', '0');
271 @ini_set('log_errors', '1');
272 } else {
273 @ini_set('display_errors', '1');
276 /// Shared-Memory cache init -- will set $MCACHE
277 /// $MCACHE is a global object that offers at least add(), set() and delete()
278 /// with similar semantics to the memcached PHP API http://php.net/memcache
279 if (!empty($CFG->cachetype)) {
280 if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) {
281 if (!init_memcached()) {
282 debugging("Error initialising memcached");
284 } elseif ($CFG->cachetype === 'eaccelerator') {
285 if (!init_eaccelerator()) {
286 debugging("Error initialising eaccelerator cache");
289 } else { // just make sure it is defined
290 $CFG->cachetype = '';
292 /// Ensure we define rcache - so we can later check for it
293 /// with a really fast and unambiguous $CFG->rcache === false
294 if (empty($CFG->rcache)) {
295 $CFG->rcache = false;
296 } else {
297 $CFG->rcache = true;
300 /// Set a default enrolment configuration (see bug 1598)
301 if (!isset($CFG->enrol)) {
302 $CFG->enrol = 'manual';
305 /// Set default enabled enrolment plugins
306 if (!isset($CFG->enrol_plugins_enabled)) {
307 $CFG->enrol_plugins_enabled = 'manual';
310 /// File permissions on created directories in the $CFG->dataroot
312 if (empty($CFG->directorypermissions)) {
313 $CFG->directorypermissions = 0777; // Must be octal (that's why it's here)
316 /// Calculate and set $CFG->ostype to be used everywhere. Possible values are:
317 /// - WINDOWS: for any Windows flavour.
318 /// - UNIX: for the rest
319 /// Also, $CFG->os can continue being used if more specialization is required
320 if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
321 $CFG->ostype = 'WINDOWS';
322 } else {
323 $CFG->ostype = 'UNIX';
325 $CFG->os = PHP_OS;
327 /// Set up default frame target string, based on $CFG->framename
328 $CFG->frametarget = frametarget();
330 /// Setup cache dir for Smarty and others
331 if (!file_exists($CFG->dataroot .'/cache')) {
332 make_upload_directory('cache');
335 /// Set up smarty template system
336 //require_once($CFG->libdir .'/smarty/Smarty.class.php');
337 //$smarty = new Smarty;
338 //$smarty->template_dir = $CFG->dirroot .'/templates/'. $CFG->template;
339 //if (!file_exists($CFG->dataroot .'/cache/smarty')) {
340 // make_upload_directory('cache/smarty');
342 //$smarty->compile_dir = $CFG->dataroot .'/cache/smarty';
344 /// Set up session handling
345 if(empty($CFG->respectsessionsettings)) {
346 if (empty($CFG->dbsessions)) { /// File-based sessions
348 // Some distros disable GC by setting probability to 0
349 // overriding the PHP default of 1
350 // (gc_probability is divided by gc_divisor, which defaults to 1000)
351 if (ini_get('session.gc_probability') == 0) {
352 ini_set('session.gc_probability', 1);
355 if (!empty($CFG->sessiontimeout)) {
356 ini_set('session.gc_maxlifetime', $CFG->sessiontimeout);
359 if (!file_exists($CFG->dataroot .'/sessions')) {
360 make_upload_directory('sessions');
362 ini_set('session.save_path', $CFG->dataroot .'/sessions');
364 } else { /// Database sessions
365 ini_set('session.save_handler', 'user');
367 $ADODB_SESSION_DRIVER = $CFG->dbtype;
368 $ADODB_SESSION_CONNECT = $CFG->dbhost;
369 $ADODB_SESSION_USER = $CFG->dbuser;
370 $ADODB_SESSION_PWD = $CFG->dbpass;
371 $ADODB_SESSION_DB = $CFG->dbname;
372 $ADODB_SESSION_TBL = $CFG->prefix.'sessions2';
373 if (!empty($CFG->sessiontimeout)) {
374 $ADODB_SESS_LIFE = $CFG->sessiontimeout;
377 require_once($CFG->libdir. '/adodb/session/adodb-session2.php');
380 /// Set sessioncookie variable if it isn't already
381 if (!isset($CFG->sessioncookie)) {
382 $CFG->sessioncookie = '';
385 /// Configure ampersands in URLs
387 @ini_set('arg_separator.output', '&amp;');
389 /// Location of standard files
391 $CFG->wordlist = $CFG->libdir .'/wordlist.txt';
392 $CFG->javascript = $CFG->libdir .'/javascript.php';
393 $CFG->moddata = 'moddata';
396 /// A hack to get around magic_quotes_gpc being turned off
397 /// It is strongly recommended to enable "magic_quotes_gpc"!
399 if (!ini_get_bool('magic_quotes_gpc') ) {
400 function addslashes_deep($value) {
401 $value = is_array($value) ?
402 array_map('addslashes_deep', $value) :
403 addslashes($value);
404 return $value;
406 $_POST = array_map('addslashes_deep', $_POST);
407 $_GET = array_map('addslashes_deep', $_GET);
408 $_COOKIE = array_map('addslashes_deep', $_COOKIE);
409 $_REQUEST = array_map('addslashes_deep', $_REQUEST);
410 if (!empty($_SERVER['REQUEST_URI'])) {
411 $_SERVER['REQUEST_URI'] = addslashes($_SERVER['REQUEST_URI']);
413 if (!empty($_SERVER['QUERY_STRING'])) {
414 $_SERVER['QUERY_STRING'] = addslashes($_SERVER['QUERY_STRING']);
416 if (!empty($_SERVER['HTTP_REFERER'])) {
417 $_SERVER['HTTP_REFERER'] = addslashes($_SERVER['HTTP_REFERER']);
419 if (!empty($_SERVER['PATH_INFO'])) {
420 $_SERVER['PATH_INFO'] = addslashes($_SERVER['PATH_INFO']);
422 if (!empty($_SERVER['PHP_SELF'])) {
423 $_SERVER['PHP_SELF'] = addslashes($_SERVER['PHP_SELF']);
425 if (!empty($_SERVER['PATH_TRANSLATED'])) {
426 $_SERVER['PATH_TRANSLATED'] = addslashes($_SERVER['PATH_TRANSLATED']);
431 /// The following code can emulate "register globals" if required.
432 /// This hack is no longer being applied as of Moodle 1.6 unless you really
433 /// really want to use it (by defining $CFG->enableglobalshack = true)
435 if (!empty($CFG->enableglobalshack)) {
436 if (!empty($CFG->detect_unchecked_vars)) {
437 global $UNCHECKED_VARS;
438 $UNCHECKED_VARS->url = $_SERVER['PHP_SELF'];
439 $UNCHECKED_VARS->vars = array();
442 if (isset($_GET)) {
443 extract($_GET, EXTR_SKIP); // Skip existing variables, ie CFG
444 if (!empty($CFG->detect_unchecked_vars)) {
445 foreach ($_GET as $key => $val) {
446 $UNCHECKED_VARS->vars[$key]=$val;
450 if (isset($_POST)) {
451 extract($_POST, EXTR_SKIP); // Skip existing variables, ie CFG
452 if (!empty($CFG->detect_unchecked_vars)) {
453 foreach ($_POST as $key => $val) {
454 $UNCHECKED_VARS->vars[$key]=$val;
458 if (isset($_SERVER)) {
459 extract($_SERVER);
464 /// Load up global environment variables
466 class object {};
468 //discard session ID from POST, GET and globals to tighten security,
469 //this session fixation prevention can not be used in cookieless mode
470 if (empty($CFG->usesid)) {
471 unset(${'MoodleSession'.$CFG->sessioncookie});
472 unset($_GET['MoodleSession'.$CFG->sessioncookie]);
473 unset($_POST['MoodleSession'.$CFG->sessioncookie]);
475 //compatibility hack for Moodle Cron, cookies not deleted, but set to "deleted" - should not be needed with $nomoodlecookie in cron.php now
476 if (!empty($_COOKIE['MoodleSession'.$CFG->sessioncookie]) && $_COOKIE['MoodleSession'.$CFG->sessioncookie] == "deleted") {
477 unset($_COOKIE['MoodleSession'.$CFG->sessioncookie]);
479 if (!empty($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie]) && $_COOKIE['MoodleSessionTest'.$CFG->sessioncookie] == "deleted") {
480 unset($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie]);
482 if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
483 require_once("$CFG->dirroot/lib/cookieless.php");
484 sid_start_ob();
487 if (empty($nomoodlecookie)) {
488 session_name('MoodleSession'.$CFG->sessioncookie);
489 @session_start();
490 if (! isset($_SESSION['SESSION'])) {
491 $_SESSION['SESSION'] = new object;
492 $_SESSION['SESSION']->session_test = random_string(10);
493 if (!empty($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie])) {
494 $_SESSION['SESSION']->has_timed_out = true;
496 setcookie('MoodleSessionTest'.$CFG->sessioncookie, $_SESSION['SESSION']->session_test, 0, '/');
497 $_COOKIE['MoodleSessionTest'.$CFG->sessioncookie] = $_SESSION['SESSION']->session_test;
499 if (! isset($_SESSION['USER'])) {
500 $_SESSION['USER'] = new object;
503 $SESSION = &$_SESSION['SESSION']; // Makes them easier to reference
504 $USER = &$_SESSION['USER'];
505 if (!isset($USER->id)) {
506 $USER->id = 0; // to enable proper function of $CFG->notloggedinroleid hack
509 else {
510 $SESSION = NULL;
511 $USER = new object();
512 $USER->id = 0; // user not logged in when session disabled
515 if (defined('FULLME')) { // Usually in command-line scripts like admin/cron.php
516 $FULLME = FULLME;
517 $ME = FULLME;
518 } else {
519 $FULLME = qualified_me();
520 $ME = strip_querystring($FULLME);
523 /// In VERY rare cases old PHP server bugs (it has been found on PHP 4.1.2 running
524 /// as a CGI under IIS on Windows) may require that you uncomment the following:
525 // session_register("USER");
526 // session_register("SESSION");
530 /// Load up theme variables (colours etc)
532 if (!isset($CFG->themedir)) {
533 $CFG->themedir = $CFG->dirroot.'/theme';
534 $CFG->themewww = $CFG->wwwroot.'/theme';
536 $CFG->httpsthemewww = $CFG->themewww;
538 if (isset($_GET['theme'])) {
539 if ($CFG->allowthemechangeonurl || confirm_sesskey()) {
540 $themename = clean_param($_GET['theme'], PARAM_SAFEDIR);
541 if (($themename != '') and file_exists($CFG->themedir.'/'.$themename)) {
542 $SESSION->theme = $themename;
544 unset($themename);
548 if (!isset($CFG->theme)) {
549 $CFG->theme = 'standardwhite';
552 /// now do a session test to prevent random user switching - observed on some PHP/Apache combinations,
553 /// disable checks when working in cookieless mode
554 if (empty($CFG->usesid) || !empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) {
555 if ($SESSION != NULL) {
556 if (empty($_COOKIE['MoodleSessionTest'.$CFG->sessioncookie])) {
557 report_session_error();
558 } else if (isset($SESSION->session_test) && $_COOKIE['MoodleSessionTest'.$CFG->sessioncookie] != $SESSION->session_test) {
559 report_session_error();
565 /// Set language/locale of printed times. If user has chosen a language that
566 /// that is different from the site language, then use the locale specified
567 /// in the language file. Otherwise, if the admin hasn't specified a locale
568 /// then use the one from the default language. Otherwise (and this is the
569 /// majority of cases), use the stored locale specified by admin.
570 if ($SESSION !== NULL && isset($_GET['lang']) && ($lang = clean_param($_GET['lang'], PARAM_SAFEDIR))) {
571 if (file_exists($CFG->dataroot .'/lang/'. $lang) or file_exists($CFG->dirroot .'/lang/'. $lang)) {
572 $SESSION->lang = $lang;
573 } else if (file_exists($CFG->dataroot.'/lang/'.$lang.'_utf8') or
574 file_exists($CFG->dirroot .'/lang/'.$lang.'_utf8')) {
575 $SESSION->lang = $lang.'_utf8';
579 setup_lang_from_browser();
581 unset($lang);
583 if (empty($CFG->lang)) {
584 if (empty($SESSION->lang)) {
585 $CFG->lang = 'en_utf8';
586 } else {
587 $CFG->lang = $SESSION->lang;
591 $CFG->langdirection = (get_string('thisdirection') == 'rtl') ? 'rtl' : 'ltr';
593 // set default locale and themes - might be changed again later from require_login()
594 course_setup();
596 if (!empty($CFG->opentogoogle)) {
597 if (empty($USER->id)) { // Ignore anyone logged in
598 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
599 if (strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false ) {
600 $USER = guest_user();
601 } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'google.com') !== false ) {
602 $USER = guest_user();
603 } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'Yahoo! Slurp') !== false ) {
604 $USER = guest_user();
605 } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSNBOT') !== false ) {
606 $USER = guest_user();
609 if (empty($USER) && !empty($_SERVER['HTTP_REFERER'])) {
610 if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
611 $USER = guest_user();
612 } else if (strpos($_SERVER['HTTP_REFERER'], 'altavista') !== false ) {
613 $USER = guest_user();
616 if (!empty($USER)) {
617 load_all_capabilities();
622 if ($CFG->theme == 'standard' or $CFG->theme == 'standardwhite') { // Temporary measure to help with XHTML validation
623 if (isset($_SERVER['HTTP_USER_AGENT']) and empty($_SESSION['USER']->id)) { // Allow W3CValidator in as user called w3cvalidator (or guest)
624 if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
625 (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
626 if ($USER = get_complete_user_data("username", "w3cvalidator")) {
627 $USER->ignoresesskey = true;
628 } else {
629 $USER = guest_user();
635 /// Apache log intergration. In apache conf file one can use ${MOODULEUSER}n in
636 /// LogFormat to get the current logged in username in moodle.
637 if ($USER && function_exists('apache_note') && !empty($CFG->apacheloguser)) {
638 $apachelog_username = clean_filename($USER->username);
639 $apachelog_name = clean_filename($USER->firstname. " ".$USER->lastname);
640 $apachelog_userid = $USER->id;
641 if (isset($USER->realuser)) {
642 if ($realuser = get_record('user', 'id', $USER->realuser)) {
643 $apachelog_username = clean_filename($realuser->username." as ".$apachelog_username);
644 $apachelog_name = clean_filename($realuser->firstname." ".$realuser->lastname ." as ".$apachelog_name);
645 $apachelog_userid = clean_filename($realuser->id." as ".$apachelog_userid);
648 switch ($CFG->apacheloguser) {
649 case 3:
650 $logname = $apachelog_username;
651 break;
652 case 2:
653 $logname = $apachelog_name;
654 break;
655 case 1:
656 default:
657 $logname = $apachelog_userid;
658 break;
660 apache_note('MOODLEUSER', $logname);
663 /// Adjust ALLOWED_TAGS
664 adjust_allowed_tags();
667 /// Use a custom script replacement if one exists
668 if (!empty($CFG->customscripts)) {
669 if (($customscript = custom_script_path()) !== false) {
670 require ($customscript);
674 /// note: we can not block non utf-8 installatrions here, because empty mysql database
675 /// might be converted to utf-8 in admin/index.php during installation