-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathindex.php
205 lines (167 loc) · 6.24 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* Habari Index
*
* This is where all the magic happens:
* 1. Validate the installation
* 2. Set the locale
* 3. Load the active plugins
* 4. Parse and handle the incoming request
* 5. Run the cron jobs
* 6. Dispatch the request to the found handler
*
* @package Habari
*/
namespace Habari;
// Fail out if not included from root
if ( !defined( 'HABARI_PATH' ) ) {
header( 'HTTP/1.1 403 Forbidden', true, 403 );
die('If you are seeing this message, and <a href="http://wiki.habariproject.org/en/FAQ#I_get_a_403_error_.28Forbidden.29_when_I_try_to_run_Habari_for_the_first_time.2C_why.3F">you were expecting to see your Habari install</a>, you probably cloned the wrong repo.');
}
// Compares PHP version against our requirement.
if ( !version_compare( PHP_VERSION, '5.4', '>=' ) ) {
die( 'Habari needs PHP 5.4 or higher to run. You are currently running PHP ' . PHP_VERSION . '.' );
}
// Increase the error reporting level: E_ALL, E_NOTICE, and E_STRICT
error_reporting( E_ALL | E_NOTICE | E_STRICT );
// set a default timezone for PHP. Habari will base off of this later on
date_default_timezone_set( 'UTC' );
/**
* Start the profile time
*/
$profile_start = microtime( true );
/**
* Make GLOB_BRACE available on platforms that don't have it. Use Utils::glob().
*/
if ( !defined( 'GLOB_BRACE' ) ) {
define( 'GLOB_NOBRACE', true );
define( 'GLOB_BRACE', 128 );
}
// We start up output buffering in order to take advantage of output compression,
// as well as the ability to dynamically change HTTP headers after output has started.
ob_start();
require( dirname( __FILE__ ) . '/autoload.php' );
spl_autoload_register( array('\Habari\Autoload', 'habari_autoload') );
// Replace all of the $_GET, $_POST and $_SERVER superglobals with object
// representations of each. Unset $_REQUEST, which is evil.
// $_COOKIE must be set after sessions start
SuperGlobal::process_gps();
// Use our own error reporting class.
if ( !defined( 'SUPPRESS_ERROR_HANDLER' ) ) {
Error::handle_errors();
}
/**
* Initiate install verifications
*/
// Retrieve the configuration file's path.
$config = Site::get_dir( 'config_file' );
/**
* We make sure the configuration file exist.
* If it does, we load it and check its validity.
*
* @todo Call the installer from the database classes.
*/
if ( file_exists( $config ) ) {
require_once( $config );
}
// Make sure we have a DSN string and database credentials.
// db_connection is an array with necessary informations to connect to the database.
if ( Config::exists('db_connection') ) {
// Set the default locale.
Locale::set( Config::get('locale', 'en-us' ) );
if ( !defined( 'DEBUG' ) ) {
define( 'DEBUG', false );
}
// The connection details are registered. Try to connect to the database.
try {
DB::connect();
// Make sure Habari is installed properly.
// If the 'installed' option is missing, we assume the database tables are missing or corrupted.
// @todo Find a decent solution, we have to compare tables and restore or upgrade them.
if ( !@ Options::get( 'installed' ) ) {
$installer = new InstallHandler();
$installer->begin_install();
}
}
catch( \PDOException $e ) {
// Error template.
$error_template = '<html><head><title>%s</title><link rel="stylesheet" type="text/css" href="' . Site::get_url( 'system' ) . '/admin/css/admin.css" media="screen"></head><body><div id="page"><div class="container"><h2>%s</h2><p>%s</p></div></div></body></html>';
// Format page with localized messages.
$error_page = sprintf( $error_template,
_t( "Habari General Error" ), // page title
_t( "An error occurred" ), // H1 tag
( defined( 'DEBUG' ) && DEBUG == true ) ?
_t( "Unable to connect to database. Error message: @error", array( '@error' => $e->getMessage() ) ) :
_t( "Unable to connect to database." )
// Error message, more detail if DEBUG is defined.
);
// Set correct HTTP header and die.
header( 'HTTP/1.1 500 Internal Server Error', true, 500 );
die( $error_page );
}
}
else {
if ( !defined( 'DEBUG' ) ) {
define( 'DEBUG', false );
}
// The configuration file does not exist.
// Therefore we load the installer to create the configuration file and install a base database.
$installer = new InstallHandler();
$installer->begin_install();
}
/* Habari is installed and we established a connection with the database */
// Verify if the database has to be upgraded.
if ( Version::requires_upgrade() ) {
$installer = new InstallHandler();
$installer->upgrade_db();
}
// If we're doing unit testing, stop here
if ( defined( 'UNIT_TEST' ) ) {
return;
}
// if this is an asyncronous call, ignore abort.
if ( isset( $_GET['asyncronous'] ) && Utils::crypt( Options::get( 'GUID' ), $_GET['asyncronous'] ) ) {
ignore_user_abort( true );
}
// Send the Content-Type HTTP header.
// @todo Find a better place to put this.
header( 'Content-Type: text/html;charset=utf-8' );
// Load and upgrade all the active plugins.
spl_autoload_register( Method::create( '\Habari\Plugins' , '_autoload' ) );
Plugins::load_active();
// All plugins loaded, tell the plugins.
Plugins::act( 'plugins_loaded' );
// Start the session.
Session::init();
// Set the locale from the user info else config, database, then default english locale
if ( User::identify()->loggedin && User::identify()->info->locale_lang ) {
Locale::set( User::identify()->info->locale_lang );
}
else {
Locale::set( Config::get('locale', Options::get( 'locale', 'en-us' )) );
}
if ( Options::get( 'system_locale' ) ) {
Locale::set_system_locale( Options::get( 'system_locale' ) );
}
// Replace the $_COOKIE superglobal with an object representation
SuperGlobal::process_c();
// Initiating request handling, tell the plugins.
Plugins::act( 'init' );
// Load Stack Items
Stack::load_stackitems();
if ( defined( 'SUPPRESS_REQUEST' ) ) {
return;
}
// Parse and handle the request.
Controller::parse_request();
// Run the cron jobs asyncronously.
CronHandler::run_cron( Config::get('cron_async', true) );
// Dispatch the request (action) to the matched handler.
Controller::dispatch_request();
// Shut down sessions so they can write and send cookies and headers before output, but after everything should be done
Session::shutdown();
// Flush (send) the output buffer.
$buffer = ob_get_clean();
$buffer = Plugins::filter( 'final_output', $buffer );
echo $buffer;
?>