More phpdoc work
[moodle.git] / lib / setup.php
CommitLineData
dcb1bd3c 1<?php // $Id$
f9903ed0 2//
3// setup.php
4//
5// Sets up sessions, connects to databases and so on
6//
7// Normally this is only called by the main config.php file
8//
9// Normally this file does not need to be edited.
10//
f9903ed0 11//////////////////////////////////////////////////////////////
12
36ec6afe 13 if (!isset($CFG->wwwroot)) {
14 die;
15 }
be0bdec8 16
17 if (!isset($CFG->enrol)) { // This is a hack to fix bug 1598
18 $CFG->enrol = 'internal';
19 }
36ec6afe 20
74944b73 21/// If there are any errors in the standard libraries we want to know!
346b1a24 22 error_reporting(E_ALL);
f9903ed0 23
a8a71844 24/// Connect to the database using adodb
25
dae73c05 26 $CFG->libdir = "$CFG->dirroot/lib";
1685298e 27
b0e3a925 28 require_once("$CFG->libdir/adodb/adodb.inc.php"); // Database access functions
fa3ba0f6 29
30 $db = &ADONewConnection($CFG->dbtype);
31
0cb29cc4 32 error_reporting(0); // Hide errors
9f71c9e7 33
34 if (!isset($CFG->dbpersist) or !empty($CFG->dbpersist)) { // Use persistent connection (default)
35 $dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
36 } else { // Use single connection
37 $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
38 }
39 if (! $dbconnected) {
40 echo "<font color=\"#990000\">";
41 echo "<p>Error: Moodle could not connect to the database.</p>";
42 echo "<p>It's possible the database itself is just not working at the moment.</p>";
43 echo "<p>The admin should
44 also check that the database details have been correctly specified in config.php</p>";
45 echo "<p>Database host: $CFG->dbhost<br />";
46 echo "Database name: $CFG->dbname<br />";
47 echo "Database user: $CFG->dbuser<br />";
48 if (!isset($CFG->dbpersist)) {
49 echo "<p>The admin should also try setting this in config.php: $"."CFG->dbpersist = false; </p>";
4f5dbd13 50 }
9f71c9e7 51 echo "</font>";
52 die;
a8a71844 53 }
54
0cb29cc4 55 error_reporting(E_ALL); // Show errors from now on.
56
b6bae251 57 if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
58 $CFG->prefix = "";
59 }
60
61
dae73c05 62/// Define admin directory
63
64 if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
1040ea85 65 $CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
dae73c05 66 }
67
a8a71844 68
74944b73 69/// Load up standard libraries
1e3e716f 70
b0e3a925 71 require_once("$CFG->libdir/weblib.php"); // Functions for producing HTML
72 require_once("$CFG->libdir/datalib.php"); // Functions for accessing databases
73 require_once("$CFG->libdir/moodlelib.php"); // Other general-purpose functions
1e3e716f 74
1e3e716f 75
22d42bf0 76/// Increase memory limits if possible
77
91d39e19 78 @ini_set('memory_limit' , '64M'); // We should never NEED this much but just in case...
afb18f5a 79
80
74944b73 81/// Load up any configuration from the config table
82
1040ea85 83 if ($configs = get_records('config')) {
74944b73 84 $CFG = (array)$CFG;
85 foreach ($configs as $config) {
86 $CFG[$config->name] = $config->value;
87 }
88 $CFG = (object)$CFG;
89 unset($configs);
90 unset($config);
91 }
f9903ed0 92
1e3e716f 93
4fd7ccc0 94/// Set error reporting back to normal
6fbf8d8f 95 if (empty($CFG->debug)) {
96 $CFG->debug = 7;
97 }
16ba7351 98 error_reporting($CFG->debug);
4fd7ccc0 99
100
2e6d4273 101/// File permissions on created directories in the $CFG->dataroot
102
103 if (empty($CFG->directorypermissions)) {
104 $CFG->directorypermissions = 0777; // Must be octal (that's why it's here)
105 }
106
afb18f5a 107/// Set up smarty template system
108 require_once("$CFG->libdir/smarty/Smarty.class.php");
109 $smarty = new Smarty;
110 $smarty->template_dir = "$CFG->dirroot/templates/$CFG->template";
111 if (!file_exists("$CFG->dataroot/cache")) {
112 make_upload_directory('cache');
113 }
114 $smarty->compile_dir = "$CFG->dataroot/cache";
2e6d4273 115
eb58acca 116/// Set session timeouts
117 if (!empty($CFG->sessiontimeout)) {
1040ea85 118 ini_set('session.gc_maxlifetime', $CFG->sessiontimeout);
eb58acca 119 }
120
39e1c415 121/// Set custom session path
122 if (!file_exists("$CFG->dataroot/sessions")) {
123 make_upload_directory('sessions');
124 }
125 ini_set('session.save_path', "$CFG->dataroot/sessions");
126
482b6e6e 127/// Set sessioncookie variable if it isn't already
128 if (!isset($CFG->sessioncookie)) {
129 $CFG->sessioncookie = '';
130 }
eb58acca 131
de7e4ac9 132/// Configure ampersands in URLs
133
134 @ini_set('arg_separator.output', '&amp;');
135
74944b73 136/// Location of standard files
f9903ed0 137
f9903ed0 138 $CFG->wordlist = "$CFG->libdir/wordlist.txt";
139 $CFG->javascript = "$CFG->libdir/javascript.php";
1040ea85 140 $CFG->moddata = 'moddata';
f9903ed0 141
ae7aafeb 142
74944b73 143/// Load up theme variables (colours etc)
1e3e716f 144
74944b73 145 if (!isset($CFG->theme)) {
1040ea85 146 $CFG->theme = 'standard';
74944b73 147 }
45f21f94 148 include("$CFG->dirroot/theme/$CFG->theme/config.php");
1e3e716f 149
e6f10ec5 150 $CFG->stylesheet = "$CFG->wwwroot/theme/$CFG->theme/styles.php";
151 $CFG->header = "$CFG->dirroot/theme/$CFG->theme/header.html";
152 $CFG->footer = "$CFG->dirroot/theme/$CFG->theme/footer.html";
153
c9f6251e 154 if (empty($THEME->custompix)) {
155 $CFG->pixpath = "$CFG->wwwroot/pix";
156 $CFG->modpixpath = "$CFG->wwwroot/mod";
157 } else {
158 $CFG->pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
159 $CFG->modpixpath = "$CFG->wwwroot/theme/$CFG->theme/pix/mod";
160 }
161
ae7aafeb 162
aa6af0f8 163/// A hack to get around magic_quotes_gpc being turned off
164
1040ea85 165 if (!ini_get_bool('magic_quotes_gpc') ) {
aa6af0f8 166 foreach ($_GET as $key => $var) {
46213aa8 167 if (!is_array($var)) {
168 $_GET[$key] = addslashes($var);
169 } else {
170 foreach ($var as $arrkey => $arrvar) {
171 $var[$arrkey] = addslashes($arrvar);
172 }
173 $_GET[$key] = $var;
174 }
aa6af0f8 175 }
176 foreach ($_POST as $key => $var) {
46213aa8 177 if (!is_array($var)) {
178 $_POST[$key] = addslashes($var);
179 } else {
180 foreach ($var as $arrkey => $arrvar) {
181 $var[$arrkey] = addslashes($arrvar);
182 }
183 $_POST[$key] = $var;
184 }
aa6af0f8 185 }
186 }
7a302afc 187
ae7aafeb 188
74944b73 189/// The following is a hack to get around the problem of PHP installations
190/// that have "register_globals" turned off (default since PHP 4.1.0).
191/// Eventually I'll go through and upgrade all the code to make this unnecessary
b0cb5e22 192
16ba7351 193 if (isset($_GET)) {
194 extract($_GET, EXTR_SKIP); // Skip existing variables, ie CFG
195 }
196 if (isset($_POST)) {
197 extract($_POST, EXTR_SKIP); // Skip existing variables, ie CFG
74944b73 198 }
199 if (isset($_SERVER)) {
200 extract($_SERVER);
b0cb5e22 201 }
ae7aafeb 202
f9903ed0 203
74944b73 204/// Load up global environment variables
f9903ed0 205
206 class object {};
207
16e4918d 208 if (!isset($nomoodlecookie)) {
482b6e6e 209 session_name('MoodleSession'.$CFG->sessioncookie);
16e4918d 210 @session_start();
211 if (! isset($_SESSION['SESSION'])) {
212 $_SESSION['SESSION'] = new object;
213 }
214 if (! isset($_SESSION['USER'])) {
215 $_SESSION['USER'] = new object;
216 }
217
218 $SESSION = &$_SESSION['SESSION']; // Makes them easier to reference
219 $USER = &$_SESSION['USER'];
08c17336 220 }
221
9d378732 222 if (isset($FULLME)) {
223 $ME = $FULLME;
224 } else {
9fa49e22 225 $FULLME = qualified_me();
9d378732 226 $ME = strip_querystring($FULLME);
9fa49e22 227 }
f9903ed0 228
1063d088 229/// In VERY rare cases old PHP server bugs (it has been found on PHP 4.1.2 running
230/// as a CGI under IIS on Windows) may require that you uncomment the following:
231// session_register("USER");
232// session_register("SESSION");
233
f9903ed0 234
339bb559 235/// Set language/locale of printed times. If user has chosen a language that
236/// that is different from the site language, then use the locale specified
237/// in the language file. Otherwise, if the admin hasn't specified a locale
238/// then use the one from the default language. Otherwise (and this is the
239/// majority of cases), use the stored locale specified by admin.
240
16ba7351 241 if (isset($_GET['lang'])) {
80035a89 242 if (!detect_munged_arguments($lang, 0) and file_exists("$CFG->dirroot/lang/$lang")) {
32c60ce3 243 $SESSION->lang = $lang;
244 $SESSION->encoding = get_string('thischarset');
245 }
3e9b5d5a 246 }
16ba7351 247 if (empty($CFG->lang)) {
248 $CFG->lang = "en";
249 }
339bb559 250
a2fa19d8 251 moodle_setlocale();
1040ea85 252
18845ac3 253 if (!empty($CFG->opentogoogle)) {
254 if (empty($_SESSION['USER'])) {
255 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
256 if (strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false ) {
257 $USER = guest_user();
258 }
2d33fc00 259 if (strpos($_SERVER['HTTP_USER_AGENT'], 'google.com') !== false ) {
260 $USER = guest_user();
261 }
18845ac3 262 }
263 if (empty($_SESSION['USER']) and !empty($_SERVER['HTTP_REFERER'])) {
264 if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
265 $USER = guest_user();
266 }
267 }
268 }
269 }
270
9610a66e 271 if ($CFG->theme == 'standardxhtml') { // Temporary measure to help with XHTML validation
272 if (empty($_SESSION['USER'])) { // Allow W3CValidator in as user called w3cvalidator (or guest)
273 if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
98de6431 274 (strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
003c49cb 275 if ($USER = get_user_info_from_db("username", "w3cvalidator")) {
276 $USER->loggedin = true;
277 $USER->site = $CFG->wwwroot;
278 } else {
9610a66e 279 $USER = guest_user();
280 }
281 }
282 }
283 }
284
f9903ed0 285?>