forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.php
370 lines (303 loc) · 12 KB
/
setup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* setup.php - Sets up sessions, connects to databases and so on
*
* Normally this is only called by the main config.php file
* Normally this file does not need to be edited.
* @author Martin Dougiamas
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
////// DOCUMENTATION IN PHPDOC FORMAT FOR MOODLE GLOBALS AND COMMON OBJECT TYPES /////////////
/**
* This global variable is read in from the 'config' table.
*
* Some typical settings in the $CFG global:
* - $USER->emailstop - Does the user want email sent to them?
* - $USER->email - The user's email address.
* - $USER->id - The unique integer identified of this user in the 'user' table.
* - $USER->email - The user's email address.
* - $USER->firstname - The user's first name.
* - $USER->lastname - The user's last name.
* - $USER->username - The user's login username.
* - $USER->secret - The user's ?.
* - $USER->lang - The user's language choice.
*
* @global object(user) $USER
*/
global $USER;
/**
* $USER is a global instance of a typical $user record.
*
* Items found in the user record:
* - $CFG->wwwroot - Path to moodle index directory in url format.
* - $CFG->dataroot - Path to moodle index directory on server's filesystem.
* - $CFG->libroot - Path to moodle's library folder on server's filesystem.
*
* @global object(cfg) $CFG
*/
global $CFG;
/**
* Definition of session type
* @global object(session) $SESSION
*/
global $SESSION;
/**
* Definition of course type
* @global object(course) $COURSE
*/
global $COURSE;
/**
* Definition of db type
* @global object(db) $db
*/
global $db;
/**
* $THEME is a global that defines the site theme.
*
* Items found in the theme record:
* - $THEME->cellheading - Cell colors.
* - $THEME->cellheading2 - Alternate cell colors.
*
* @global object(theme) $THEME
*/
global $THEME;
if (!isset($CFG->wwwroot)) {
die;
}
if (!isset($CFG->enrol)) { // This is a hack to fix bug 1598
$CFG->enrol = 'internal';
}
/// If there are any errors in the standard libraries we want to know!
error_reporting(E_ALL);
/// Connect to the database using adodb
$CFG->libdir = $CFG->dirroot .'/lib';
require_once($CFG->libdir .'/adodb/adodb.inc.php'); // Database access functions
$db = &ADONewConnection($CFG->dbtype);
error_reporting(0); // Hide errors
if (!isset($CFG->dbpersist) or !empty($CFG->dbpersist)) { // Use persistent connection (default)
$dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
} else { // Use single connection
$dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
}
if (! $dbconnected) {
echo '<table align="center"><tr>';
echo '<td style="color:#990000; text-align:center; font-size:large; border-width:1px; '.
' border-color:#000000; border-style:solid; border-radius: 20px; border-collapse: collapse; '.
' -moz-border-radius: 20px; padding: 15px">';
echo '<p>Error: Database connection failed.</p>';
echo '<p>It is possible that the database is overloaded or otherwise not running properly.</p>';
echo '<p>The site administrator should also check that the database details have been correctly specified in config.php</p>';
echo '</td></tr></table>';
die;
}
error_reporting(E_ALL); // Show errors from now on.
if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
$CFG->prefix = '';
}
/// Define admin directory
if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
$CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
}
/// Load up standard libraries
require_once($CFG->libdir .'/weblib.php'); // Functions for producing HTML
require_once($CFG->libdir .'/datalib.php'); // Functions for accessing databases
require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions
/// Increase memory limits if possible
raise_memory_limit('64M'); // We should never NEED this much but just in case...
/// Load up any configuration from the config table
if ($configs = get_records('config')) {
$CFG = (array)$CFG;
foreach ($configs as $config) {
$CFG[$config->name] = $config->value;
}
$CFG = (object)$CFG;
unset($configs);
unset($config);
}
/// Set error reporting back to normal
if (empty($CFG->debug)) {
$CFG->debug = 7;
}
error_reporting($CFG->debug);
/// File permissions on created directories in the $CFG->dataroot
if (empty($CFG->directorypermissions)) {
$CFG->directorypermissions = 0777; // Must be octal (that's why it's here)
}
/// Set up smarty template system
require_once($CFG->libdir .'/smarty/Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = $CFG->dirroot .'/templates/'. $CFG->template;
if (!file_exists($CFG->dataroot .'/cache')) {
make_upload_directory('cache');
}
$smarty->compile_dir = $CFG->dataroot .'/cache';
/// Set up session handling
if (empty($CFG->dbsessions)) { /// File-based sessions
if (!empty($CFG->sessiontimeout)) {
ini_set('session.gc_maxlifetime', $CFG->sessiontimeout);
}
if (!file_exists($CFG->dataroot .'/sessions')) {
make_upload_directory('sessions');
}
ini_set('session.save_path', $CFG->dataroot .'/sessions');
} else { /// Database sessions
ini_set('session.save_handler', 'user');
$ADODB_SESSION_DRIVER = $CFG->dbtype;
$ADODB_SESSION_CONNECT = $CFG->dbhost;
$ADODB_SESSION_USER = $CFG->dbuser;
$ADODB_SESSION_PWD = $CFG->dbpass;
$ADODB_SESSION_DB = $CFG->dbname;
$ADODB_SESSION_TBL = $CFG->prefix.'sessions';
require_once($CFG->libdir. '/adodb/session/adodb-session.php');
}
/// Set sessioncookie variable if it isn't already
if (!isset($CFG->sessioncookie)) {
$CFG->sessioncookie = '';
}
/// Configure ampersands in URLs
@ini_set('arg_separator.output', '&');
/// Location of standard files
$CFG->wordlist = $CFG->libdir .'/wordlist.txt';
$CFG->javascript = $CFG->libdir .'/javascript.php';
$CFG->moddata = 'moddata';
/// Load up theme variables (colours etc)
if (!isset($CFG->theme)) {
$CFG->theme = 'standard';
}
include($CFG->dirroot .'/theme/'. $CFG->theme .'/config.php');
$CFG->stylesheet = $CFG->wwwroot .'/theme/'. $CFG->theme .'/styles.php';
$CFG->header = $CFG->dirroot .'/theme/'. $CFG->theme .'/header.html';
$CFG->footer = $CFG->dirroot .'/theme/'. $CFG->theme .'/footer.html';
if (empty($THEME->custompix)) {
$CFG->pixpath = $CFG->wwwroot .'/pix';
$CFG->modpixpath = $CFG->wwwroot .'/mod';
} else {
$CFG->pixpath = $CFG->wwwroot .'/theme/'. $CFG->theme .'/pix';
$CFG->modpixpath = $CFG->wwwroot .'/theme/'. $CFG->theme .'/pix/mod';
}
/// A hack to get around magic_quotes_gpc being turned off
if (!ini_get_bool('magic_quotes_gpc') ) {
foreach ($_GET as $key => $var) {
if (!is_array($var)) {
$_GET[$key] = addslashes($var);
} else {
foreach ($var as $arrkey => $arrvar) {
$var[$arrkey] = addslashes($arrvar);
}
$_GET[$key] = $var;
}
}
foreach ($_POST as $key => $var) {
if (!is_array($var)) {
$_POST[$key] = addslashes($var);
} else {
foreach ($var as $arrkey => $arrvar) {
$var[$arrkey] = addslashes($arrvar);
}
$_POST[$key] = $var;
}
}
foreach ($_COOKIE as $key => $var) {
if (!is_array($var)) {
$_COOKIE[$key] = addslashes($var);
} else {
foreach ($var as $arrkey => $arrvar) {
$var[$arrkey] = addslashes($arrvar);
}
$_COOKIE[$key] = $var;
}
}
}
/// The following is a hack to get around the problem of PHP installations
/// that have "register_globals" turned off (default since PHP 4.1.0).
/// Eventually I'll go through and upgrade all the code to make this unnecessary
if (isset($_GET)) {
extract($_GET, EXTR_SKIP); // Skip existing variables, ie CFG
}
if (isset($_POST)) {
extract($_POST, EXTR_SKIP); // Skip existing variables, ie CFG
}
if (isset($_SERVER)) {
extract($_SERVER);
}
/// Load up global environment variables
class object {};
unset(${'MoodleSession'.$CFG->sessioncookie});
unset($_GET['MoodleSession'.$CFG->sessioncookie]);
unset($_POST['MoodleSession'.$CFG->sessioncookie]);
if (!isset($nomoodlecookie)) {
session_name('MoodleSession'.$CFG->sessioncookie);
@session_start();
if (! isset($_SESSION['SESSION'])) {
$_SESSION['SESSION'] = new object;
}
if (! isset($_SESSION['USER'])) {
$_SESSION['USER'] = new object;
}
$SESSION = &$_SESSION['SESSION']; // Makes them easier to reference
$USER = &$_SESSION['USER'];
}
else {
$SESSION = NULL;
$USER = NULL;
}
if (defined('FULLME')) { // Usually in command-line scripts like admin/cron.php
$FULLME = FULLME;
$ME = FULLME;
} else {
$FULLME = qualified_me();
$ME = strip_querystring($FULLME);
}
/// In VERY rare cases old PHP server bugs (it has been found on PHP 4.1.2 running
/// as a CGI under IIS on Windows) may require that you uncomment the following:
// session_register("USER");
// session_register("SESSION");
/// Set language/locale of printed times. If user has chosen a language that
/// that is different from the site language, then use the locale specified
/// in the language file. Otherwise, if the admin hasn't specified a locale
/// then use the one from the default language. Otherwise (and this is the
/// majority of cases), use the stored locale specified by admin.
if (isset($_GET['lang'])) {
if (!detect_munged_arguments($lang, 0) and file_exists($CFG->dirroot .'/lang/'. $lang)) {
$SESSION->lang = $lang;
$SESSION->encoding = get_string('thischarset');
}
}
if (empty($CFG->lang)) {
$CFG->lang = "en";
}
moodle_setlocale();
if (!empty($CFG->opentogoogle)) {
if (empty($_SESSION['USER'])) {
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false ) {
$USER = guest_user();
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'google.com') !== false ) {
$USER = guest_user();
}
}
if (empty($_SESSION['USER']) and !empty($_SERVER['HTTP_REFERER'])) {
if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
$USER = guest_user();
}
}
}
}
if ($CFG->theme == 'standardxhtml') { // Temporary measure to help with XHTML validation
if (empty($_SESSION['USER'])) { // Allow W3CValidator in as user called w3cvalidator (or guest)
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
(strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
if ($USER = get_user_info_from_db("username", "w3cvalidator")) {
$USER->loggedin = true;
$USER->site = $CFG->wwwroot;
$USER->ignoresesskey = true;
} else {
$USER = guest_user();
}
}
}
}
?>