New translationd added.
[moodle.git] / lib / setup.php
blob128c591a37a33b0540caec2071db26c8fd2ee339
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.
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 $CFG->libdir = "$CFG->dirroot/lib";
20 require_once("$CFG->libdir/adodb/adodb.inc.php"); // Database access functions
22 $db = &ADONewConnection($CFG->dbtype);
24 error_reporting(0); // Hide errors
26 if (!isset($CFG->dbpersist) or !empty($CFG->dbpersist)) { // Use persistent connection (default)
27 $dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
28 } else { // Use single connection
29 $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
31 if (! $dbconnected) {
32 echo "<font color=\"#990000\">";
33 echo "<p>Error: Moodle could not connect to the database.</p>";
34 echo "<p>It's possible the database itself is just not working at the moment.</p>";
35 echo "<p>The admin should
36 also check that the database details have been correctly specified in config.php</p>";
37 echo "<p>Database host: $CFG->dbhost<br />";
38 echo "Database name: $CFG->dbname<br />";
39 echo "Database user: $CFG->dbuser<br />";
40 if (!isset($CFG->dbpersist)) {
41 echo "<p>The admin should also try setting this in config.php: $"."CFG->dbpersist = false; </p>";
43 echo "</font>";
44 die;
47 error_reporting(E_ALL); // Show errors from now on.
49 if (!isset($CFG->prefix)) { // Just in case it isn't defined in config.php
50 $CFG->prefix = "";
54 /// Define admin directory
56 if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
57 $CFG->admin = "admin"; // This is relative to the wwwroot and dirroot
61 /// Load up standard libraries
63 require_once("$CFG->libdir/weblib.php"); // Functions for producing HTML
64 require_once("$CFG->libdir/datalib.php"); // Functions for accessing databases
65 require_once("$CFG->libdir/moodlelib.php"); // Other general-purpose functions
68 /// Load up any configuration from the config table
70 if ($configs = get_records("config")) {
71 $CFG = (array)$CFG;
72 foreach ($configs as $config) {
73 $CFG[$config->name] = $config->value;
75 $CFG = (object)$CFG;
76 unset($configs);
77 unset($config);
81 /// Set error reporting back to normal
82 if (empty($CFG->debug)) {
83 $CFG->debug = 7;
85 error_reporting($CFG->debug);
88 /// File permissions on created directories in the $CFG->dataroot
90 if (empty($CFG->directorypermissions)) {
91 $CFG->directorypermissions = 0777; // Must be octal (that's why it's here)
95 /// Set session timeouts
96 if (!empty($CFG->sessiontimeout)) {
97 ini_set("session.gc_maxlifetime", $CFG->sessiontimeout);
101 /// Location of standard files
103 $CFG->wordlist = "$CFG->libdir/wordlist.txt";
104 $CFG->javascript = "$CFG->libdir/javascript.php";
105 $CFG->moddata = "moddata";
108 /// Load up theme variables (colours etc)
110 if (!isset($CFG->theme)) {
111 $CFG->theme = "standard";
113 include("$CFG->dirroot/theme/$CFG->theme/config.php");
115 $CFG->stylesheet = "$CFG->wwwroot/theme/$CFG->theme/styles.php";
116 $CFG->header = "$CFG->dirroot/theme/$CFG->theme/header.html";
117 $CFG->footer = "$CFG->dirroot/theme/$CFG->theme/footer.html";
121 /// Reference code to remove magic quotes from everything ... just in case.
122 /// If you have problems with slashes everywhere then you might want to
123 /// uncomment this code. It will not be necessary on 99.9% of PHP servers.
124 /// Got this from http://www.php.net/manual/en/configuration.php
125 // if (ini_get_bool("magic_quotes_gpc") ) {
126 // foreach ($GLOBALS["HTTP_".$GLOBALS["REQUEST_METHOD"]."_VARS"] as $key => $value) {
127 // if (!is_array($value)) { // Simple value
128 // $newval = stripslashes($value);
129 // $GLOBALS["HTTP_".$GLOBALS["REQUEST_METHOD"]."_VARS"][$key] = $newval;
130 // if (ini_get_bool("register_globals")) {
131 // $GLOBALS[$key] = $newval;
132 // }
133 // } else { // Array
134 // foreach ($value as $k => $v) {
135 // $newval = stripslashes($v);
136 // $GLOBALS["HTTP_".$GLOBALS["REQUEST_METHOD"]."_VARS"][$key][$k] = $newval;
137 // if (ini_get_bool("register_globals")) {
138 // $GLOBALS[$key][$k] = $newval;
139 // }
140 // }
141 // }
142 // }
143 // }
145 /// The following is a hack to get around the problem of PHP installations
146 /// that have "register_globals" turned off (default since PHP 4.1.0).
147 /// Eventually I'll go through and upgrade all the code to make this unnecessary
149 if (isset($_GET)) {
150 extract($_GET, EXTR_SKIP); // Skip existing variables, ie CFG
152 if (isset($_POST)) {
153 extract($_POST, EXTR_SKIP); // Skip existing variables, ie CFG
155 if (isset($_SERVER)) {
156 extract($_SERVER);
160 /// Load up global environment variables
162 class object {};
164 session_name('MoodleSession');
165 @session_start();
166 if (! isset($_SESSION['SESSION'])) {
167 $_SESSION['SESSION'] = new object;
169 if (! isset($_SESSION['USER'])) {
170 $_SESSION['USER'] = new object;
173 $SESSION = &$_SESSION['SESSION']; // Makes them easier to reference
174 $USER = &$_SESSION['USER'];
176 if (isset($FULLME)) {
177 $ME = $FULLME;
178 } else {
179 $FULLME = qualified_me();
180 $ME = strip_querystring($FULLME);
183 /// In VERY rare cases old PHP server bugs (it has been found on PHP 4.1.2 running
184 /// as a CGI under IIS on Windows) may require that you uncomment the following:
185 // session_register("USER");
186 // session_register("SESSION");
189 /// Set language/locale of printed times. If user has chosen a language that
190 /// that is different from the site language, then use the locale specified
191 /// in the language file. Otherwise, if the admin hasn't specified a locale
192 /// then use the one from the default language. Otherwise (and this is the
193 /// majority of cases), use the stored locale specified by admin.
195 if (isset($_GET['lang'])) {
196 $SESSION->lang = $lang;
198 if (empty($CFG->lang)) {
199 $CFG->lang = "en";
201 if (!empty($SESSION->lang) and ($SESSION->lang != $CFG->lang) ) {
202 $CFG->locale = get_string("locale");
203 } else if (!empty($USER->lang) and ($USER->lang != $CFG->lang) ) {
204 $CFG->locale = get_string("locale");
205 } else if (empty($CFG->locale)) {
206 $CFG->locale = get_string("locale");
207 set_config("locale", $CFG->locale); // cache it to save lookups in future
209 setlocale (LC_TIME, $CFG->locale);
210 setlocale (LC_CTYPE, $CFG->locale);
211 setlocale (LC_COLLATE, $CFG->locale);