init.php 9.34 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 5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Penny Leach's avatar
Penny Leach committed
6
 *
Francois Marier's avatar
Francois Marier committed
7 8 9 10
 * 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.
11
 *
Francois Marier's avatar
Francois Marier committed
12 13 14 15
 * 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17 18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
 *
Penny Leach's avatar
Penny Leach committed
26 27
 */

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

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

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

42 43 44 45 46
// 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');

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

50
if (!is_readable($CFG->docroot . 'config.php')) {
51 52 53 54
    // @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
55 56
}

57 58
init_performance_info();

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

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

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

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

78
// core libraries
79
require('mahara.php');
80
ensure_sanity();
81
require('dml.php');
82
require('web.php');
Penny Leach's avatar
Penny Leach committed
83
require('user.php');
84
require(get_config('docroot') . 'local/lib.php');
85

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

Nigel McNie's avatar
Nigel McNie committed
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();
95 96 97 98 99 100 101

    if (is_postgres()) {
        $CFG->dbtype = 'postgres7';
    }
    else if (is_mysql()) {
        $CFG->dbtype = 'mysql';
    }
Nigel McNie's avatar
Nigel McNie committed
102 103
    
    $db = &ADONewConnection($CFG->dbtype);
104 105 106 107
    if (empty($CFG->dbhost)) {
        $CFG->dbhost = '';
    }
    else if (!empty($CFG->dbport)) {
Nigel McNie's avatar
Nigel McNie committed
108 109 110 111
        $CFG->dbhost .= ':'.$CFG->dbport;
    }
    if (!empty($CFG->dbpersist)) {    // Use persistent connection (default)
        $dbconnected = $db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
112 113
    } 
    else {                                                     // Use single connection
Nigel McNie's avatar
Nigel McNie committed
114 115
        $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
    }
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130
    // Now we have a connection, verify the server is a new enough version
    $dbversion = $db->ServerInfo();
    if (is_postgres()) {
        $okversion = '8.1';
        $dbfriendlyname = 'PostgreSQL';
    }
    else if (is_mysql()) {
        $okversion = '5.0.25';
        $dbfriendlyname = 'MySQL';
    }
    if ($dbversion['version'] < $okversion) {
        throw new ConfigSanityException(get_string('dbversioncheckfailed', 'error', $dbfriendlyname, $dbversion['version'], $okversion));
    }

131 132
    $db->SetFetchMode(ADODB_FETCH_ASSOC);
    configure_dbconnection();
133
    ensure_internal_plugins_exist();
134

Nigel McNie's avatar
Nigel McNie committed
135
    ob_end_clean();
136
}
Nigel McNie's avatar
Nigel McNie committed
137
catch (Exception $e) {
138 139 140
    if ($e instanceof ConfigSanityException) {
        throw $e;
    }
141
    $errormessage = ob_get_contents();
142 143 144
    if (!$errormessage) {
        $errormessage = $e->getMessage();
    }
145
    ob_end_clean();
146 147
    $errormessage = get_string('dbconnfailed', 'error') . $errormessage;
    throw new ConfigSanityException($errormessage);
148
}
149
try {
150
    db_ignore_sql_exceptions(true);
151
    load_config();
152
    db_ignore_sql_exceptions(false);
153
} 
154
catch (SQLException $e) {
155
    db_ignore_sql_exceptions(false);
156
}
157

Nigel McNie's avatar
Nigel McNie committed
158
// Make sure wwwroot is set and available, either in the database or in the
159
// config file. Cron requires it for some purposes.
160 161 162
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'];
163 164 165 166
    if (false !== strpos($host, ',')) {
        list($host) = explode(',', $host);
        $host = trim($host);
    }
167 168
    $path  = substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']));
    if ($path) {
169
        $path = str_replace('\\', '/', $path);  // windows
170 171 172
        if (substr($path, 0, 1) != '/') {
            $path = '/' . $path;
        }
173
        $path .= '/';
174 175
    } else {
        $path = '/';
176
    }
177
    $wwwroot = $proto . $host . $path;
178 179 180 181
    try {
        set_config('wwwroot', $wwwroot);
    }
    catch (Exception $e) {
182
        // Just set it directly. The system will most likely not be installed, so we don't care
183 184 185
        $CFG->wwwroot = $wwwroot;
    }
}
186 187 188 189 190
if (isset($CFG->httpswwwroot)) {
    if (substr($CFG->httpswwwroot, -1, 1) != '/') {
        $CFG->httpswwwroot .= '/';
    }
}
191
if (!isset($CFG->noreplyaddress) && isset($_SERVER['HTTP_HOST'])) {
192 193 194 195 196 197 198
    $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;
199 200 201 202 203 204 205 206 207
    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
208 209 210 211 212 213
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'; 
}

214 215 216 217 218
if (defined('INSTALLER')) { 
    // Custom themes sometimes cause upgrades to fail.
    $CFG->theme = 'default';
}

219 220 221 222 223 224 225 226 227
// Make sure the search plugin is configured
if (!get_config('searchplugin')) {
    try {
        set_config('searchplugin', 'internal');
    }
    catch (Exception $e) {
        $CFG->searchplugin = 'internal';
    }
}
228 229
header('Content-type: text/html; charset=UTF-8');

230 231
// Only do authentication once we know the page theme, so that the login form
// can have the correct theming.
232
require_once('auth/lib.php');
233 234
$SESSION = Session::singleton();
$USER    = new LiveUser();
235
$THEME   = new Theme($USER);
236 237 238 239 240
// 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();
}
241

242 243 244 245
$siteclosedforupgrade = get_config('siteclosed');
if ($siteclosedforupgrade && $USER->admin) {
    if (get_config('disablelogin')) {
        $USER->logout();
246
    }
247 248 249 250 251 252 253 254 255 256 257 258
    else if (!defined('INSTALLER')) {
        redirect('/admin/upgrade.php');
    }
}

$siteclosed = $siteclosedforupgrade || get_config('siteclosedbyadmin');
if ($siteclosed && !$USER->admin) {
    if ($USER->is_logged_in()) {
        $USER->logout();
    }
    if (!defined('HOME') && !defined('INSTALLER')) {
        redirect();
259 260 261
    }
}

262
// check to see if we're installed...
263 264 265
if (!get_config('installed')) {
    ensure_install_sanity();

266
    $scriptfilename = str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']);
267
    if (false === strpos($scriptfilename, 'admin/index.php')
268 269
    && false === strpos($scriptfilename, 'admin/upgrade.php')
    && false === strpos($scriptfilename, 'admin/upgrade.json.php')) {
270 271
        redirect('/admin/');
    }
272 273
}

Clare Lenihan's avatar
Clare Lenihan committed
274
if (defined('JSON') && !defined('NOSESSKEY')) {
275
    $sesskey = param_variable('sesskey', null);
276 277
    global $USER;
    if ($sesskey === null || $USER->get('sesskey') != $sesskey) {
278
        $USER->logout();
279
        json_reply('global', get_string('invalidsesskey'), 1);
280
    }
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296
/*
 * 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;
297
    $PERF->dbreads = $PERF->dbwrites = 0;
298 299 300 301 302 303 304 305 306 307 308 309
    $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
310
?>