a7b9a1e4186154a50473728679484b9872092944
[moodle.git] / lib / setup.php
1 <?PHP // $Id$
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 //
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     require("$CFG->libdir/adodb/adodb.inc.php"); // Database access functions
19     ADOLoadCode($CFG->dbtype);          
20     $db = &ADONewConnection();         
21     if (! $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname)) {
22         echo "<P><FONT COLOR=RED>The database details specified in config.php are not correct, or the database is down.</P>";
23         die;
24     }
26     if (!isset($CFG->prefix)) {   // Just in case it isn't defined in config.php
27         $CFG->prefix = "";
28     }
29     $CFG->prefix = "$CFG->dbname.$CFG->prefix";
33 /// Load up standard libraries 
35     require("$CFG->libdir/weblib.php");          // Functions for producing HTML
36     require("$CFG->libdir/datalib.php");         // Functions for accessing databases
37     require("$CFG->libdir/moodlelib.php");       // Other general-purpose functions
40 /// Load up any configuration from the config table
41     
42     if ($configs = get_records("config")) {
43         $CFG = (array)$CFG;
44         foreach ($configs as $config) {
45             $CFG[$config->name] = $config->value;
46         }
47         $CFG = (object)$CFG;
48         unset($configs);
49         unset($config);
50     }
53 /// Set error reporting back to normal
54     if (empty($CFG->debug)) {
55         $CFG->debug = 7;
56     }
57     error_reporting($CFG->debug);   
60 /// Location of standard files
62     $CFG->wordlist    = "$CFG->libdir/wordlist.txt";
63     $CFG->javascript  = "$CFG->libdir/javascript.php";
64     $CFG->moddata     = "moddata";
67 /// Load up theme variables (colours etc)
69     if (!isset($CFG->theme)) {
70         $CFG->theme = "standard";
71     }
72     include("$CFG->dirroot/theme/$CFG->theme/config.php");
74     $CFG->stylesheet  = "$CFG->wwwroot/theme/$CFG->theme/styles.php";
75     $CFG->header      = "$CFG->dirroot/theme/$CFG->theme/header.html";
76     $CFG->footer      = "$CFG->dirroot/theme/$CFG->theme/footer.html";
80 /// Reference code to remove magic quotes from everything ... just in case.
81 /// If you have problems with slashes everywhere then you might want to 
82 /// uncomment this code.  It will not be necessary on 99.9% of PHP servers.
83 /// Got this from http://www.php.net/manual/en/configuration.php
84 //    if (ini_get("magic_quotes_gpc") ) {
85 //        foreach ($GLOBALS["HTTP_".$GLOBALS["REQUEST_METHOD"]."_VARS"] as $key => $value) {
86 //            if (!is_array($value)) { // Simple value
87 //                $newval = stripslashes($value);
88 //                $GLOBALS["HTTP_".$GLOBALS["REQUEST_METHOD"]."_VARS"][$key] = $newval;
89 //                if (ini_get("register_globals")) {
90 //                    $GLOBALS[$key] = $newval;
91 //                }
92 //            } else {  // Array
93 //                foreach ($value as $k => $v) {
94 //                    $newval = stripslashes($v);
95 //                    $GLOBALS["HTTP_".$GLOBALS["REQUEST_METHOD"]."_VARS"][$key][$k] = $newval;
96 //                    if (ini_get("register_globals")) {
97 //                        $GLOBALS[$key][$k] = $newval;
98 //                    }
99 //                }
100 //            }
101 //        }
102 //    }
104 /// The following is a hack to get around the problem of PHP installations
105 /// that have "register_globals" turned off (default since PHP 4.1.0).
106 /// Eventually I'll go through and upgrade all the code to make this unnecessary
108     if (isset($_REQUEST)) {
109         extract($_REQUEST);
110     }
111     if (isset($_SERVER)) { 
112         extract($_SERVER);
113     }
115     
116 /// Load up global environment variables
118     class object {};
119     
120     session_start();
121     if (! isset($_SESSION["SESSION"])) { $_SESSION["SESSION"] = new object; }
122     if (! isset($_SESSION["USER"]))    { $_SESSION["USER"]    = new object; }
123     extract($_SESSION);  // Makes $SESSION and $USER available for read-only access
125     if (isset($FULLME)) {
126         $ME = $FULLME;
127     } else {
128         $FULLME = qualified_me();
129         $ME = strip_querystring($FULLME);
130     }
133 /// Set language/locale of printed times.  If user has chosen a language that 
134 /// that is different from the site language, then use the locale specified 
135 /// in the language file.  Otherwise, if the admin hasn't specified a locale
136 /// then use the one from the default language.  Otherwise (and this is the 
137 /// majority of cases), use the stored locale specified by admin.
139     if (!empty($USER->lang) and ($USER->lang != $CFG->lang) ) {
140         $CFG->locale = get_string("locale");
141     } else if (empty($CFG->locale)) {
142         $CFG->locale = get_string("locale");
143         set_config("locale", $CFG->locale);   // cache it to save lookups in future
144     }
145     setlocale (LC_TIME, $CFG->locale);
146     setlocale (LC_CTYPE, $CFG->locale);
147     setlocale (LC_COLLATE, $CFG->locale);
149 ?>