5 // Sets up sessions, connects to databases and so on
7 // Normally this is only called by the main config.php file
9 // Normally this file does not need to be edited.
11 //////////////////////////////////////////////////////////////
13 /// If there are any errors in the standard libraries we want to know!
14 error_reporting(E_ALL
);
16 /// Connect to the database using adodb
18 $CFG->libdir
= "$CFG->dirroot/lib";
20 require_once("$CFG->libdir/adodb/adodb.inc.php"); // Database access functions
22 $db = &ADONewConnection($CFG->dbtype
);
24 error_reporting(0); // Hide errors
26 if (!isset($CFG->dbpersist
) or !empty($CFG->dbpersist
)) { // Use persistent connection (default)
27 $dbconnected = $db->PConnect($CFG->dbhost
,$CFG->dbuser
,$CFG->dbpass
,$CFG->dbname
);
28 } else { // Use single connection
29 $dbconnected = $db->Connect($CFG->dbhost
,$CFG->dbuser
,$CFG->dbpass
,$CFG->dbname
);
32 echo "<font color=\"#990000\">";
33 echo "<p>Error: Moodle could not connect to the database.</p>";
34 echo "<p>It's possible the database itself is just not working at the moment.</p>";
35 echo "<p>The admin should
36 also check that the database details have been correctly specified in config.php</p>";
37 echo "<p>Database host: $CFG->dbhost<br />";
38 echo "Database name: $CFG->dbname<br />";
39 echo "Database user: $CFG->dbuser<br />";
40 if (!isset($CFG->dbpersist
)) {
41 echo "<p>The admin should also try setting this in config.php: $"."CFG->dbpersist = false; </p>";
47 error_reporting(E_ALL
); // Show errors from now on.
49 if (!isset($CFG->prefix
)) { // Just in case it isn't defined in config.php
54 /// Define admin directory
56 if (!isset($CFG->admin
)) { // Just in case it isn't defined in config.php
57 $CFG->admin
= 'admin'; // This is relative to the wwwroot and dirroot
61 /// Load up standard libraries
63 require_once("$CFG->libdir/weblib.php"); // Functions for producing HTML
64 require_once("$CFG->libdir/datalib.php"); // Functions for accessing databases
65 require_once("$CFG->libdir/moodlelib.php"); // Other general-purpose functions
68 /// Load up any configuration from the config table
70 if ($configs = get_records('config')) {
72 foreach ($configs as $config) {
73 $CFG[$config->name
] = $config->value
;
81 /// Set error reporting back to normal
82 if (empty($CFG->debug
)) {
85 error_reporting($CFG->debug
);
88 /// File permissions on created directories in the $CFG->dataroot
90 if (empty($CFG->directorypermissions
)) {
91 $CFG->directorypermissions
= 0777; // Must be octal (that's why it's here)
95 /// Set session timeouts
96 if (!empty($CFG->sessiontimeout
)) {
97 ini_set('session.gc_maxlifetime', $CFG->sessiontimeout
);
100 /// Set sessioncookie variable if it isn't already
101 if (!isset($CFG->sessioncookie
)) {
102 $CFG->sessioncookie
= '';
105 /// Location of standard files
107 $CFG->wordlist
= "$CFG->libdir/wordlist.txt";
108 $CFG->javascript
= "$CFG->libdir/javascript.php";
109 $CFG->moddata
= 'moddata';
112 /// Load up theme variables (colours etc)
114 if (!isset($CFG->theme
)) {
115 $CFG->theme
= 'standard';
117 include("$CFG->dirroot/theme/$CFG->theme/config.php");
119 $CFG->stylesheet
= "$CFG->wwwroot/theme/$CFG->theme/styles.php";
120 $CFG->header
= "$CFG->dirroot/theme/$CFG->theme/header.html";
121 $CFG->footer
= "$CFG->dirroot/theme/$CFG->theme/footer.html";
123 if (empty($THEME->custompix
)) {
124 $CFG->pixpath
= "$CFG->wwwroot/pix";
125 $CFG->modpixpath
= "$CFG->wwwroot/mod";
127 $CFG->pixpath
= "$CFG->wwwroot/theme/$CFG->theme/pix";
128 $CFG->modpixpath
= "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
132 /// A hack to get around magic_quotes_gpc being turned off
134 if (!ini_get_bool('magic_quotes_gpc') ) {
135 foreach ($_GET as $key => $var) {
136 if (!is_array($var)) {
137 $_GET[$key] = addslashes($var);
139 foreach ($var as $arrkey => $arrvar) {
140 $var[$arrkey] = addslashes($arrvar);
145 foreach ($_POST as $key => $var) {
146 if (!is_array($var)) {
147 $_POST[$key] = addslashes($var);
149 foreach ($var as $arrkey => $arrvar) {
150 $var[$arrkey] = addslashes($arrvar);
158 /// The following is a hack to get around the problem of PHP installations
159 /// that have "register_globals" turned off (default since PHP 4.1.0).
160 /// Eventually I'll go through and upgrade all the code to make this unnecessary
163 extract($_GET, EXTR_SKIP
); // Skip existing variables, ie CFG
166 extract($_POST, EXTR_SKIP
); // Skip existing variables, ie CFG
168 if (isset($_SERVER)) {
173 /// Load up global environment variables
177 if (!isset($nomoodlecookie)) {
178 session_name('MoodleSession'.$CFG->sessioncookie
);
180 if (! isset($_SESSION['SESSION'])) {
181 $_SESSION['SESSION'] = new object;
183 if (! isset($_SESSION['USER'])) {
184 $_SESSION['USER'] = new object;
187 $SESSION = &$_SESSION['SESSION']; // Makes them easier to reference
188 $USER = &$_SESSION['USER'];
191 if (isset($FULLME)) {
194 $FULLME = qualified_me();
195 $ME = strip_querystring($FULLME);
198 /// In VERY rare cases old PHP server bugs (it has been found on PHP 4.1.2 running
199 /// as a CGI under IIS on Windows) may require that you uncomment the following:
200 // session_register("USER");
201 // session_register("SESSION");
204 /// Set language/locale of printed times. If user has chosen a language that
205 /// that is different from the site language, then use the locale specified
206 /// in the language file. Otherwise, if the admin hasn't specified a locale
207 /// then use the one from the default language. Otherwise (and this is the
208 /// majority of cases), use the stored locale specified by admin.
210 if (isset($_GET['lang'])) {
211 $SESSION->lang
= $lang;
213 if (empty($CFG->lang
)) {
216 if (!empty($SESSION->lang
) and ($SESSION->lang
!= $CFG->lang
) ) {
217 $CFG->locale
= get_string('locale');
218 } else if (!empty($USER->lang
) and ($USER->lang
!= $CFG->lang
) ) {
219 $CFG->locale
= get_string('locale');
220 } else if (empty($CFG->locale
)) {
221 $CFG->locale
= get_string('locale');
222 set_config('locale', $CFG->locale
); // cache it to save lookups in future
224 setlocale (LC_TIME
, $CFG->locale
);
225 setlocale (LC_COLLATE
, $CFG->locale
);
227 if ($CFG->locale
!= 'tr_TR') { // To workaround a well-known PHP bug with Turkish
228 setlocale (LC_CTYPE
, $CFG->locale
);
231 if (!empty($CFG->opentogoogle
)) {
232 if (empty($_SESSION['USER'])) {
233 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
234 if (strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false ) {
235 $USER = guest_user();
238 if (empty($_SESSION['USER']) and !empty($_SERVER['HTTP_REFERER'])) {
239 if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
240 $USER = guest_user();