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