2004-09-25 05:30:03 +00:00
|
|
|
<?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.
|
|
|
|
*
|
2004-09-28 02:51:56 +00:00
|
|
|
* @global object(user) $USER
|
2004-09-25 05:30:03 +00:00
|
|
|
*/
|
2004-09-28 02:51:56 +00:00
|
|
|
global $USER;
|
2004-09-25 05:30:03 +00:00
|
|
|
/**
|
|
|
|
* $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.
|
|
|
|
*
|
2004-09-28 02:51:56 +00:00
|
|
|
* @global object(cfg) $CFG
|
2004-09-25 05:30:03 +00:00
|
|
|
*/
|
2004-09-28 02:51:56 +00:00
|
|
|
global $CFG;
|
2004-09-25 05:30:03 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-03-20 04:04:15 +00:00
|
|
|
if (!isset($CFG->wwwroot)) {
|
|
|
|
die;
|
|
|
|
}
|
2004-06-29 21:16:58 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
/// If there are any errors in the standard libraries we want to know!
|
2003-01-01 06:40:31 +00:00
|
|
|
error_reporting(E_ALL);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-12-07 07:27:58 +00:00
|
|
|
/// Connect to the database using adodb
|
|
|
|
|
2004-09-25 05:30:03 +00:00
|
|
|
$CFG->libdir = $CFG->dirroot .'/lib';
|
2003-01-12 06:31:07 +00:00
|
|
|
|
2004-09-25 05:30:03 +00:00
|
|
|
require_once($CFG->libdir .'/adodb/adodb.inc.php'); // Database access functions
|
2003-01-02 11:34:00 +00:00
|
|
|
|
|
|
|
$db = &ADONewConnection($CFG->dbtype);
|
|
|
|
|
2003-05-07 08:07:48 +00:00
|
|
|
error_reporting(0); // Hide errors
|
2003-05-09 02:20:03 +00:00
|
|
|
|
|
|
|
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) {
|
2004-10-15 08:47:12 +00:00
|
|
|
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>';
|
2003-05-09 02:20:03 +00:00
|
|
|
die;
|
2002-12-07 07:27:58 +00:00
|
|
|
}
|
|
|
|
|
2003-05-07 08:07:48 +00:00
|
|
|
error_reporting(E_ALL); // Show errors from now on.
|
|
|
|
|
2003-01-01 09:29:05 +00:00
|
|
|
if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
|
2004-09-25 05:30:03 +00:00
|
|
|
$CFG->prefix = '';
|
2003-01-01 09:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-10 13:46:52 +00:00
|
|
|
/// Define admin directory
|
|
|
|
|
|
|
|
if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
|
2003-11-12 07:39:10 +00:00
|
|
|
$CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
|
2003-04-10 13:46:52 +00:00
|
|
|
}
|
|
|
|
|
2002-12-07 07:27:58 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
/// Load up standard libraries
|
2002-08-17 13:01:06 +00:00
|
|
|
|
2004-09-25 05:30:03 +00:00
|
|
|
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
|
2002-08-17 13:01:06 +00:00
|
|
|
|
|
|
|
|
2004-08-16 07:26:19 +00:00
|
|
|
/// Increase memory limits if possible
|
|
|
|
|
2004-11-18 02:55:06 +00:00
|
|
|
raise_memory_limit('64M'); // We should never NEED this much but just in case...
|
2004-06-24 13:10:47 +00:00
|
|
|
|
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
/// Load up any configuration from the config table
|
|
|
|
|
2003-11-12 07:39:10 +00:00
|
|
|
if ($configs = get_records('config')) {
|
2002-09-19 12:01:55 +00:00
|
|
|
$CFG = (array)$CFG;
|
|
|
|
foreach ($configs as $config) {
|
2005-01-09 21:54:48 +00:00
|
|
|
if (!isset($CFG[$config->name])) {
|
|
|
|
$CFG[$config->name] = $config->value;
|
2005-01-18 22:59:41 +00:00
|
|
|
} else {
|
|
|
|
error_log("\$CFG->$config->name in config.php overrides database setting");
|
2005-01-09 21:54:48 +00:00
|
|
|
}
|
2002-09-19 12:01:55 +00:00
|
|
|
}
|
2004-09-25 05:30:03 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
$CFG = (object)$CFG;
|
|
|
|
unset($configs);
|
|
|
|
unset($config);
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-08-17 13:01:06 +00:00
|
|
|
|
2005-01-18 18:28:25 +00:00
|
|
|
|
2002-12-30 03:24:07 +00:00
|
|
|
/// Set error reporting back to normal
|
2003-01-01 08:48:15 +00:00
|
|
|
if (empty($CFG->debug)) {
|
|
|
|
$CFG->debug = 7;
|
|
|
|
}
|
2003-05-26 15:38:52 +00:00
|
|
|
error_reporting($CFG->debug);
|
2002-12-30 03:24:07 +00:00
|
|
|
|
|
|
|
|
2005-01-18 18:28:25 +00:00
|
|
|
/// Set a default enrolment configuration (see bug 1598)
|
|
|
|
if (!isset($CFG->enrol)) {
|
|
|
|
$CFG->enrol = 'internal';
|
|
|
|
}
|
|
|
|
|
2003-01-20 14:03:11 +00:00
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
|
2004-06-24 13:10:47 +00:00
|
|
|
/// Set up smarty template system
|
2005-01-18 18:28:25 +00:00
|
|
|
//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';
|
2003-01-20 14:03:11 +00:00
|
|
|
|
2004-09-30 06:02:39 +00:00
|
|
|
/// 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');
|
2004-04-15 14:46:54 +00:00
|
|
|
}
|
|
|
|
|
2003-10-17 12:30:17 +00:00
|
|
|
/// Set sessioncookie variable if it isn't already
|
|
|
|
if (!isset($CFG->sessioncookie)) {
|
|
|
|
$CFG->sessioncookie = '';
|
|
|
|
}
|
2003-01-24 04:31:58 +00:00
|
|
|
|
2004-09-07 09:49:41 +00:00
|
|
|
/// Configure ampersands in URLs
|
|
|
|
|
|
|
|
@ini_set('arg_separator.output', '&');
|
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
/// Location of standard files
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-09-25 05:30:03 +00:00
|
|
|
$CFG->wordlist = $CFG->libdir .'/wordlist.txt';
|
|
|
|
$CFG->javascript = $CFG->libdir .'/javascript.php';
|
2003-11-12 07:39:10 +00:00
|
|
|
$CFG->moddata = 'moddata';
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-19 14:07:24 +00:00
|
|
|
|
2003-07-28 16:37:12 +00:00
|
|
|
/// A hack to get around magic_quotes_gpc being turned off
|
|
|
|
|
2003-11-12 07:39:10 +00:00
|
|
|
if (!ini_get_bool('magic_quotes_gpc') ) {
|
2003-07-28 16:37:12 +00:00
|
|
|
foreach ($_GET as $key => $var) {
|
2003-07-29 01:54:16 +00:00
|
|
|
if (!is_array($var)) {
|
|
|
|
$_GET[$key] = addslashes($var);
|
|
|
|
} else {
|
|
|
|
foreach ($var as $arrkey => $arrvar) {
|
|
|
|
$var[$arrkey] = addslashes($arrvar);
|
|
|
|
}
|
|
|
|
$_GET[$key] = $var;
|
|
|
|
}
|
2003-07-28 16:37:12 +00:00
|
|
|
}
|
|
|
|
foreach ($_POST as $key => $var) {
|
2003-07-29 01:54:16 +00:00
|
|
|
if (!is_array($var)) {
|
|
|
|
$_POST[$key] = addslashes($var);
|
|
|
|
} else {
|
|
|
|
foreach ($var as $arrkey => $arrvar) {
|
|
|
|
$var[$arrkey] = addslashes($arrvar);
|
|
|
|
}
|
|
|
|
$_POST[$key] = $var;
|
|
|
|
}
|
2003-07-28 16:37:12 +00:00
|
|
|
}
|
2004-12-17 01:43:06 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2003-07-28 16:37:12 +00:00
|
|
|
}
|
2002-07-02 07:02:28 +00:00
|
|
|
|
2002-09-19 14:07:24 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
/// 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
|
2002-08-23 02:14:19 +00:00
|
|
|
|
2003-05-26 15:38:52 +00:00
|
|
|
if (isset($_GET)) {
|
|
|
|
extract($_GET, EXTR_SKIP); // Skip existing variables, ie CFG
|
|
|
|
}
|
|
|
|
if (isset($_POST)) {
|
|
|
|
extract($_POST, EXTR_SKIP); // Skip existing variables, ie CFG
|
2002-09-19 12:01:55 +00:00
|
|
|
}
|
|
|
|
if (isset($_SERVER)) {
|
|
|
|
extract($_SERVER);
|
2002-08-23 02:14:19 +00:00
|
|
|
}
|
2002-09-19 14:07:24 +00:00
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-19 12:01:55 +00:00
|
|
|
/// Load up global environment variables
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
class object {};
|
2004-12-17 01:43:06 +00:00
|
|
|
|
|
|
|
unset(${'MoodleSession'.$CFG->sessioncookie});
|
|
|
|
unset($_GET['MoodleSession'.$CFG->sessioncookie]);
|
|
|
|
unset($_POST['MoodleSession'.$CFG->sessioncookie]);
|
|
|
|
|
2003-08-12 07:02:34 +00:00
|
|
|
if (!isset($nomoodlecookie)) {
|
2003-10-17 12:30:17 +00:00
|
|
|
session_name('MoodleSession'.$CFG->sessioncookie);
|
2003-08-12 07:02:34 +00:00
|
|
|
@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'];
|
2003-02-27 02:38:55 +00:00
|
|
|
}
|
2004-12-17 01:43:06 +00:00
|
|
|
else {
|
|
|
|
$SESSION = NULL;
|
|
|
|
$USER = NULL;
|
|
|
|
}
|
2003-02-27 02:38:55 +00:00
|
|
|
|
2004-12-12 06:49:26 +00:00
|
|
|
if (defined('FULLME')) { // Usually in command-line scripts like admin/cron.php
|
|
|
|
$FULLME = FULLME;
|
|
|
|
$ME = FULLME;
|
2002-12-29 04:17:32 +00:00
|
|
|
} else {
|
2002-12-20 14:44:14 +00:00
|
|
|
$FULLME = qualified_me();
|
2002-12-29 04:17:32 +00:00
|
|
|
$ME = strip_querystring($FULLME);
|
2002-12-20 14:44:14 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-01-13 12:06:33 +00:00
|
|
|
/// 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");
|
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2005-02-01 08:00:58 +00:00
|
|
|
|
|
|
|
/// Load up theme variables (colours etc)
|
|
|
|
if (isset($_GET['theme'])) {
|
|
|
|
if (confirm_sesskey()) {
|
|
|
|
if (!detect_munged_arguments($_GET['theme'], 0) and file_exists($CFG->dirroot .'/theme/'. $_GET['theme'])) {
|
|
|
|
$SESSION->theme = $_GET['theme'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($CFG->theme)) {
|
|
|
|
$CFG->theme = 'standard';
|
|
|
|
}
|
|
|
|
|
|
|
|
theme_setup(); // Sets up theme global variables
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-12-08 17:26:21 +00:00
|
|
|
/// 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.
|
|
|
|
|
2003-05-26 15:38:52 +00:00
|
|
|
if (isset($_GET['lang'])) {
|
2004-09-25 05:30:03 +00:00
|
|
|
if (!detect_munged_arguments($lang, 0) and file_exists($CFG->dirroot .'/lang/'. $lang)) {
|
2004-07-17 04:31:34 +00:00
|
|
|
$SESSION->lang = $lang;
|
|
|
|
$SESSION->encoding = get_string('thischarset');
|
|
|
|
}
|
2003-01-20 08:09:25 +00:00
|
|
|
}
|
2003-05-26 15:38:52 +00:00
|
|
|
if (empty($CFG->lang)) {
|
|
|
|
$CFG->lang = "en";
|
|
|
|
}
|
2002-12-08 17:26:21 +00:00
|
|
|
|
2004-06-21 13:07:44 +00:00
|
|
|
moodle_setlocale();
|
2003-11-12 07:39:10 +00:00
|
|
|
|
2004-01-25 09:37:28 +00:00
|
|
|
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();
|
|
|
|
}
|
2004-05-16 14:19:35 +00:00
|
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], 'google.com') !== false ) {
|
|
|
|
$USER = guest_user();
|
|
|
|
}
|
2004-01-25 09:37:28 +00:00
|
|
|
}
|
|
|
|
if (empty($_SESSION['USER']) and !empty($_SERVER['HTTP_REFERER'])) {
|
|
|
|
if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
|
|
|
|
$USER = guest_user();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-25 13:22:18 +00:00
|
|
|
if ($CFG->theme == 'standard') { // Temporary measure to help with XHTML validation
|
2004-09-07 07:38:44 +00:00
|
|
|
if (empty($_SESSION['USER'])) { // Allow W3CValidator in as user called w3cvalidator (or guest)
|
|
|
|
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false) or
|
2004-09-07 08:04:02 +00:00
|
|
|
(strpos($_SERVER['HTTP_USER_AGENT'], 'Cynthia') !== false )) {
|
2004-09-07 08:23:11 +00:00
|
|
|
if ($USER = get_user_info_from_db("username", "w3cvalidator")) {
|
|
|
|
$USER->loggedin = true;
|
|
|
|
$USER->site = $CFG->wwwroot;
|
2004-12-22 02:56:16 +00:00
|
|
|
$USER->ignoresesskey = true;
|
2004-09-07 08:23:11 +00:00
|
|
|
} else {
|
2004-09-07 07:38:44 +00:00
|
|
|
$USER = guest_user();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-27 05:15:21 +00:00
|
|
|
?>
|