init.php 5.06 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1 2
<?php
/**
3
 * This program is part of Mahara
Penny Leach's avatar
Penny Leach committed
4
 *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *  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 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 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('.' . PATH_SEPARATOR . $CFG->libroot);
Penny Leach's avatar
Penny Leach committed
40

41 42 43
// Set up error handling
require 'errors.php';

44
if (!is_readable($CFG->docroot . 'config.php')) {
45 46 47 48
    // @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
49 50
}

51
require('config.php');
Nigel McNie's avatar
Nigel McNie committed
52
$CFG = (object)array_merge((array)$cfg, (array)$CFG);
Penny Leach's avatar
Penny Leach committed
53

54 55 56 57
// Fix up paths in $CFG
foreach (array('docroot', 'dataroot') as $path) {
    $CFG->{$path} = (substr($CFG->{$path}, -1) != DIRECTORY_SEPARATOR) ? $CFG->{$path} . DIRECTORY_SEPARATOR : $CFG->{$path};
}
58
if (!isset($CFG->wwwroot) && isset($_SERVER['HTTP_HOST'])) {
59
    $proto = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';
Nigel McNie's avatar
Nigel McNie committed
60 61
    $host  =  (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
    $path  = substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']));
62 63 64 65
    if ($path) {
        $path .= '/';
    }
    $CFG->wwwroot = $proto . $host . '/' . $path;
66
}
67 68 69 70 71 72
if (!isset($CFG->noreplyaddress) && isset($_SERVER['HTTP_HOST'])) {
    $CFG->noreplyaddress = 'noreply@' .
        ((isset($_SERVER['HTTP_X_FORWARDED_HOST'])) 
         ? $_SERVER['HTTP_X_FORWARDED_HOST'] 
         : $_SERVER['HTTP_HOST']);
}
73

74 75 76 77
// xmldb stuff
$CFG->xmldbdisablenextprevchecking = true;
$CFG->xmldbdisablecommentchecking = true;

78
// core libraries
79
require('mahara.php');
80
ensure_sanity();
81
require('dml.php');
82
require('ddl.php');
83 84
require('constants.php');
require('web.php');
85
require('activity.php');
86

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

Nigel McNie's avatar
Nigel McNie committed
91 92 93 94 95 96 97 98 99 100 101 102
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);
    if (!empty($CFG->dbport)) {
        $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 107
        $dbconnected = $db->Connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
    }
    
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 116
    $errormessage = ob_get_contents();
    ob_end_clean();
Nigel McNie's avatar
Nigel McNie committed
117 118 119 120
    // @todo <nigel|penny>: At this point the raw error message can be munged from
    // $errormessage, while the $e object holds some other information (like backtrace,
    // which can be parsed with adodb_backtrace($e->gettrace());). At this point a
    // nice message should be displayed explaining the problem etc. etc.
121
    throw new Exception($errormessage);
122
}
123 124 125
try {
    load_config();
} 
126
catch (SQLException $e) {
127 128 129 130
    if (!defined('INSTALLER')) {
        throw $e;
    }
}
131

Penny Leach's avatar
Penny Leach committed
132 133 134 135 136 137
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'; 
}

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

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

142 143
// Only do authentication once we know the page theme, so that the login form
// can have the correct theming.
144
require('auth/lib.php');
145
$USER = auth_setup();
146

147 148 149 150 151 152 153 154
// 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')) {
    redirect(get_config('wwwroot') . 'admin/index.php');
}

Nigel McNie's avatar
Nigel McNie committed
155
?>