init.php 8.14 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
Penny Leach's avatar
Penny Leach committed
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage core
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
 *
Penny Leach's avatar
Penny Leach committed
25
26
 */

27
28
defined('INTERNAL') || die();

Penny Leach's avatar
Penny Leach committed
29
$CFG = new StdClass;
30
$CFG->docroot = dirname(__FILE__) . '/';
Penny Leach's avatar
Penny Leach committed
31

Nigel McNie's avatar
Nigel McNie committed
32
33
// Figure out our include path
if (!empty($_SERVER['MAHARA_LIBDIR'])) {
34
    $CFG->libroot = $_SERVER['MAHARA_LIBDIR'];
Penny Leach's avatar
Penny Leach committed
35
}
Nigel McNie's avatar
Nigel McNie committed
36
else {
37
    $CFG->libroot = dirname(__FILE__) . '/lib/';
Nigel McNie's avatar
Nigel McNie committed
38
}
39
set_include_path($CFG->libroot . PATH_SEPARATOR . $CFG->libroot . 'pear/' . PATH_SEPARATOR . get_include_path());
Penny Leach's avatar
Penny Leach committed
40

41
42
43
44
45
// Ensure that, by default, the response is not cached
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 507686400) .' GMT');
header('Pragma: no-cache');

46
// Set up error handling
Nigel McNie's avatar
Nigel McNie committed
47
require('errors.php');
48

49
if (!is_readable($CFG->docroot . 'config.php')) {
50
51
52
53
    // @todo Later, this will redirect to the installer script. For now, we
    // just log and exit.
    log_environ('Not installed! Please create config.php from config-dist.php');
    exit;
Penny Leach's avatar
Penny Leach committed
54
55
}

56
57
init_performance_info();

58
require('config.php');
Nigel McNie's avatar
Nigel McNie committed
59
$CFG = (object)array_merge((array)$cfg, (array)$CFG);
60
61
require_once('config-defaults.php');
$CFG = (object)array_merge((array)$cfg, (array)$CFG);
Penny Leach's avatar
Penny Leach committed
62

63
64
// Fix up paths in $CFG
foreach (array('docroot', 'dataroot') as $path) {
65
    $CFG->{$path} = (substr($CFG->{$path}, -1) != DIRECTORY_SEPARATOR) ? $CFG->{$path} . DIRECTORY_SEPARATOR : $CFG->{$path};
66
67
}

68
69
70
71
// xmldb stuff
$CFG->xmldbdisablenextprevchecking = true;
$CFG->xmldbdisablecommentchecking = true;

72
73
74
75
76
// ensure directorypermissions is set
if (empty($CFG->directorypermissions)) {
    $CFG->directorypermissions = 0700;
}

77
// core libraries
78
require('mahara.php');
79
ensure_sanity();
80
require('dml.php');
81
require('web.php');
Penny Leach's avatar
Penny Leach committed
82
require('user.php');
83

Nigel McNie's avatar
Nigel McNie committed
84
// Database access functions
85
86
require('adodb/adodb-exceptions.inc.php');
require('adodb/adodb.inc.php');
87

Nigel McNie's avatar
Nigel McNie committed
88
89
90
91
92
93
94
try {
    // ADODB does not provide the raw driver error message if the connection
    // fails for some reason, so we use output buffering to catch whatever
    // the error is instead.
    ob_start();
    
    $db = &ADONewConnection($CFG->dbtype);
95
96
97
98
    if (empty($CFG->dbhost)) {
        $CFG->dbhost = '';
    }
    else if (!empty($CFG->dbport)) {
Nigel McNie's avatar
Nigel McNie committed
99
100
101
102
        $CFG->dbhost .= ':'.$CFG->dbport;
    }
    if (!empty($CFG->dbpersist)) {    // Use persistent connection (default)
        $dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
103
104
    } 
    else {                                                     // Use single connection
Nigel McNie's avatar
Nigel McNie committed
105
106
        $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
    }
107

108
109
    $db->SetFetchMode(ADODB_FETCH_ASSOC);
    configure_dbconnection();
110
    ensure_internal_plugins_exist();
111

Nigel McNie's avatar
Nigel McNie committed
112
    ob_end_clean();
113
}
Nigel McNie's avatar
Nigel McNie committed
114
catch (Exception $e) {
115
    $errormessage = ob_get_contents();
116
117
118
    if (!$errormessage) {
        $errormessage = $e->getMessage();
    }
119
    ob_end_clean();
120
121
    $errormessage = get_string('dbconnfailed', 'error') . $errormessage;
    throw new ConfigSanityException($errormessage);
122
}
123
try {
124
    db_ignore_sql_exceptions(true);
125
    load_config();
126
    db_ignore_sql_exceptions(false);
127
} 
128
catch (SQLException $e) {
129
    db_ignore_sql_exceptions(false);
130
}
131

132
133
// Make sure wwwroot is set and available, either in the database or int the
// config file. Cron requires it for some purposes.
134
135
136
if (!isset($CFG->wwwroot) && isset($_SERVER['HTTP_HOST'])) {
    $proto = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';
    $host  =  (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
137
138
139
140
    if (false !== strpos($host, ',')) {
        list($host) = explode(',', $host);
        $host = trim($host);
    }
141
142
    $path  = substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']));
    if ($path) {
143
144
145
        if (substr($path, 0, 1) != '/') {
            $path = '/' . $path;
        }
146
        $path .= '/';
147
148
    } else {
        $path = '/';
149
    }
150
    $wwwroot = $proto . $host . $path;
151
152
153
154
    try {
        set_config('wwwroot', $wwwroot);
    }
    catch (Exception $e) {
155
        // Just set it directly. The system will most likely not be installed, so we don't care
156
157
158
159
        $CFG->wwwroot = $wwwroot;
    }
}
if (!isset($CFG->noreplyaddress) && isset($_SERVER['HTTP_HOST'])) {
160
161
162
163
164
165
166
    $noreplyaddress = 'noreply@';
    $host  =  (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
    if (false !== strpos($host, ',')) {
        list($host) = explode(',', $host);
        $host = trim($host);
    }
    $noreplyaddress .= $host;
167
168
169
170
171
172
173
174
175
    try {
        set_config('noreplyaddress', $noreplyaddress);
    }
    catch (Exception $e) {
        // Do nothing again, same reason as above
        $CFG->noreplyaddress = $noreplyaddress;
    }
}

Penny Leach's avatar
Penny Leach committed
176
177
178
179
180
181
if (!get_config('theme')) { 
    // if it's not set, we're probably not installed, 
    // so set it in $CFG directly rather than the db which doesn't yet exist
    $CFG->theme = 'default'; 
}

182
$CFG->themeurl = get_config('wwwroot') . 'theme/' . get_config('theme') . '/static/';
Penny Leach's avatar
Penny Leach committed
183

184
185
186
187
188
189
190
191
192
193
// Make sure the search plugin is configured
if (!get_config('searchplugin')) {
    try {
        set_config('searchplugin', 'internal');
    }
    catch (Exception $e) {
        $CFG->searchplugin = 'internal';
    }
}

194
195
header('Content-type: text/html; charset=UTF-8');

196
197
// Only do authentication once we know the page theme, so that the login form
// can have the correct theming.
198
require_once('auth/lib.php');
199
200
$SESSION = Session::singleton();
$USER    = new LiveUser();
201
202
203
204
205
// The installer does its own auth_setup checking, because some upgrades may
// break logging in and so need to allow no logins.
if (!defined('INSTALLER')) {
    auth_setup();
}
206

207
if (get_config('siteclosed')) {
208
209
210
211
    if ($USER->admin) {
        if (get_config('disablelogin')) {
            $USER->logout();
        }
212
        else if (!defined('INSTALLER')) {
213
214
            redirect('/admin/upgrade.php');
        }
215
216
217
218
219
    }
    if (!$USER->admin) {
        if ($USER->is_logged_in()) {
            $USER->logout();
        }
220
        if (!defined('HOME') && !defined('INSTALLER')) {
221
222
223
224
225
            redirect();
        }
    }
}

226
227
228
229
230
// check to see if we're installed...
if (!get_config('installed')
    && false === strpos($_SERVER['SCRIPT_FILENAME'], 'admin/index.php')
    && false === strpos($_SERVER['SCRIPT_FILENAME'], 'admin/upgrade.php')
    && false === strpos($_SERVER['SCRIPT_FILENAME'], 'admin/upgrade.json.php')) {
231
    redirect('/admin/');
232
233
}

Clare Lenihan's avatar
Clare Lenihan committed
234
if (defined('JSON') && !defined('NOSESSKEY')) {
235
    $sesskey = param_variable('sesskey', null);
236
237
    global $USER;
    if ($sesskey === null || $USER->get('sesskey') != $sesskey) {
238
        $USER->logout();
239
        json_reply('global', get_string('invalidsesskey'), 1);
240
    }
241
242
}

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Initializes our performance info early.
 *
 * Pairs up with get_performance_info() which is actually
 * in lib/mahara.php. This function is here so that we can 
 * call it before all the libs are pulled in. 
 *
 * @uses $PERF
 */
function init_performance_info() {

    global $PERF;
  
    $PERF = new StdClass;
    $PERF->dbqueries = 0;   
    $PERF->logwrites = 0;
    if (function_exists('microtime')) {
        $PERF->starttime = microtime();
        }
    if (function_exists('memory_get_usage')) {
        $PERF->startmemory = memory_get_usage();
    }
    if (function_exists('posix_times')) {
        $PERF->startposixtimes = posix_times();  
    }
}

Nigel McNie's avatar
Nigel McNie committed
270
?>