Slightly more efficient way to open the database connections
[moodle.git] / lib / setup.php
CommitLineData
f9903ed0 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//
f9903ed0 11//////////////////////////////////////////////////////////////
12
74944b73 13/// If there are any errors in the standard libraries we want to know!
346b1a24 14 error_reporting(E_ALL);
f9903ed0 15
a8a71844 16/// Connect to the database using adodb
17
18 require("$CFG->libdir/adodb/adodb.inc.php"); // Database access functions
fa3ba0f6 19
20 $db = &ADONewConnection($CFG->dbtype);
21
a8a71844 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 }
26
b6bae251 27 if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
28 $CFG->prefix = "";
29 }
7adcd58f 30 $CFG->prefix = "$CFG->dbname.$CFG->prefix";
b6bae251 31
32
a8a71844 33
74944b73 34/// Load up standard libraries
1e3e716f 35
9fa49e22 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
1e3e716f 39
1e3e716f 40
74944b73 41/// Load up any configuration from the config table
42
9fa49e22 43 if ($configs = get_records("config")) {
74944b73 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 }
f9903ed0 52
1e3e716f 53
4fd7ccc0 54/// Set error reporting back to normal
6fbf8d8f 55 if (empty($CFG->debug)) {
56 $CFG->debug = 7;
57 }
4fd7ccc0 58 error_reporting($CFG->debug);
59
60
74944b73 61/// Location of standard files
f9903ed0 62
f9903ed0 63 $CFG->wordlist = "$CFG->libdir/wordlist.txt";
64 $CFG->javascript = "$CFG->libdir/javascript.php";
ca4f8eb8 65 $CFG->moddata = "moddata";
f9903ed0 66
ae7aafeb 67
74944b73 68/// Load up theme variables (colours etc)
1e3e716f 69
74944b73 70 if (!isset($CFG->theme)) {
71 $CFG->theme = "standard";
72 }
45f21f94 73 include("$CFG->dirroot/theme/$CFG->theme/config.php");
1e3e716f 74
e6f10ec5 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";
78
ae7aafeb 79
7a302afc 80
0ee8d920 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// }
ae7aafeb 104
74944b73 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
b0cb5e22 108
74944b73 109 if (isset($_REQUEST)) {
110 extract($_REQUEST);
111 }
112 if (isset($_SERVER)) {
113 extract($_SERVER);
b0cb5e22 114 }
ae7aafeb 115
f9903ed0 116
74944b73 117/// Load up global environment variables
f9903ed0 118
119 class object {};
120
121 session_start();
8223d271 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
f9903ed0 125
9d378732 126 if (isset($FULLME)) {
127 $ME = $FULLME;
128 } else {
9fa49e22 129 $FULLME = qualified_me();
9d378732 130 $ME = strip_querystring($FULLME);
9fa49e22 131 }
f9903ed0 132
f9903ed0 133
339bb559 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.
139
9c9f7d77 140 if (!empty($USER->lang) and ($USER->lang != $CFG->lang) ) {
339bb559 141 $CFG->locale = get_string("locale");
9c9f7d77 142 } else if (empty($CFG->locale)) {
339bb559 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);
149
f9903ed0 150?>