forked from movim/movim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
341 lines (279 loc) · 10.5 KB
/
bootstrap.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
if (!defined('DOCUMENT_ROOT')) die('Access denied');
require 'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\SyslogHandler;
/**
* Error Handler...
*/
function systemErrorHandler($errno, $errstr, $errfile, $errline, $errcontext = null)
{
$log = new Logger('movim');
$log->pushHandler(new SyslogHandler('movim'));
$log->addError($errstr);
return false;
}
function fatalErrorShutdownHandler()
{
$last_error = error_get_last();
if($last_error['type'] === E_ERROR) {
systemErrorHandler(
E_ERROR,
$last_error['message'],
$last_error['file'],
$last_error['line']);
if (ob_get_contents()) ob_clean();
?>
<div style="font-family: Arial; text-align: center;">
<h2>Oops... something went wrong.</h2>
<p>But don't panic. The NSA is on the case.</p>
</div>
<?php
if (ob_get_contents()) ob_end_clean();
}
}
/**
* Manage boot order
*/
class Bootstrap {
function boot($light = false) {
//define all needed constants
$this->setConstants();
mb_internal_encoding("UTF-8");
//First thing to do, define error management (in case of error forward)
$this->setLogs();
//Check if vital system need is OK
$this->checkSystem();
$this->loadSystem();
$this->loadCommonLibraries();
$this->loadDispatcher();
$this->loadHelpers();
$loadmodlsuccess = $this->loadModl();
$this->setTimezone();
$this->setLogLevel();
if($loadmodlsuccess) {
$this->startingSession();
$this->loadLanguage();
} else {
throw new Exception('Error loading Modl');
}
}
private function checkSystem() {
$listWritableFile = array(
DOCUMENT_ROOT.'/log/logger.log',
DOCUMENT_ROOT.'/log/php.log',
DOCUMENT_ROOT.'/cache/test.tmp',
);
$errors = array();
if(!is_writable(DOCUMENT_ROOT))
$errors[] = 'We\'re unable to write to folder '.DOCUMENT_ROOT.': check rights';
else {
if(!file_exists(DOCUMENT_ROOT.'/cache') && !@mkdir(DOCUMENT_ROOT.'/cache')) {
$errors[] = 'Couldn\'t create directory cache';
}
if(!file_exists(DOCUMENT_ROOT.'/log') && !@mkdir(DOCUMENT_ROOT.'/log')) {
$errors[] = 'Couldn\'t create directory log';
}
if(!file_exists(DOCUMENT_ROOT.'/config') && !@mkdir(DOCUMENT_ROOT.'/config')) {
$errors[] = 'Couldn\'t create directory config';
}
if(!file_exists(DOCUMENT_ROOT.'/users') && !@mkdir(DOCUMENT_ROOT.'/users')) {
$errors[] = 'Couldn\'t create directory users';
} else {
touch(DOCUMENT_ROOT.'/users/index.html');
}
}
foreach($listWritableFile as $fileName) {
if (!file_exists($fileName)) {
if (touch($fileName) !== true) {
$errors[] = 'We\'re unable to write to '.$fileName.': check rights';
}
}else if (is_writable($fileName) !== true) {
$errors[] = 'We\'re unable to write to file '.$fileName.': check rights';
}
}
if (!function_exists('json_decode')) {
$errors[] = 'You need to install php5-json that\'s not seems to be installed';
}
if (count($errors)) {
throw new Exception(implode("\n<br />",$errors));
}
}
private function setConstants() {
define('APP_TITLE', 'Movim');
define('APP_NAME', 'movim');
define('APP_VERSION', $this->getVersion());
define('APP_SECURED', $this->isServerSecured());
if(isset($_SERVER['HTTP_HOST'])) {
define('BASE_HOST', $_SERVER['HTTP_HOST']);
}
if(isset($_SERVER['SERVER_NAME'])) {
define('BASE_DOMAIN', $_SERVER["SERVER_NAME"]);
}
define('BASE_URI', $this->getBaseUri());
define('CACHE_URI', $this->getBaseUri() . 'cache/');
define('SESSION_ID', getenv('sid'));
define('THEMES_PATH', DOCUMENT_ROOT . '/themes/');
define('USERS_PATH', DOCUMENT_ROOT . '/users/');
define('APP_PATH', DOCUMENT_ROOT . '/app/');
define('SYSTEM_PATH', DOCUMENT_ROOT . '/system/');
define('LIB_PATH', DOCUMENT_ROOT . '/lib/');
define('LOCALES_PATH', DOCUMENT_ROOT . '/locales/');
define('CACHE_PATH', DOCUMENT_ROOT . '/cache/');
define('LOG_PATH', DOCUMENT_ROOT . '/log/');
define('VIEWS_PATH', DOCUMENT_ROOT . '/app/views/');
define('HELPERS_PATH', DOCUMENT_ROOT . '/app/helpers/');
define('WIDGETS_PATH', DOCUMENT_ROOT . '/app/widgets/');
define('MOVIM_API', 'https://api.movim.eu/');
if (!defined('DOCTYPE')) {
define('DOCTYPE','text/html');
}
}
private function isServerSecured() {
if((
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "")
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https")) {
return true;
}
return false;
}
private function getVersion() {
$file = "VERSION";
if($f = fopen(DOCUMENT_ROOT.'/'.$file, 'r')) {
return trim(fgets($f));
}
}
private function getBaseUri() {
$dirname = dirname($_SERVER['PHP_SELF']);
if(strstr($dirname, 'index.php')) {
$dirname = substr($dirname, 0, strrpos($dirname, 'index.php'));
}
$path = (($dirname == DIRECTORY_SEPARATOR) ? '' : $dirname).'/';
// Determining the protocol to use.
$uri = "http://";
if($this->isServerSecured()) {
$uri = 'https://';
}
if($path == "") {
$uri .= $_SERVER['HTTP_HOST'] ;
} elseif(isset($_SERVER['HTTP_HOST'])) {
$uri .= str_replace('//', '/', $_SERVER['HTTP_HOST'] . $path);
}
if(getenv('baseuri') != null
&& filter_var(getenv('baseuri'), FILTER_VALIDATE_URL)
&& sizeof(getenv('baseuri')) < 32) {
return getenv('baseuri');
} else {
return $uri;
}
}
private function loadSystem() {
require_once(SYSTEM_PATH . "Session.php");
require_once(SYSTEM_PATH . "Sessionx.php");
require_once(SYSTEM_PATH . "Utils.php");
require_once(SYSTEM_PATH . "UtilsPicture.php");
require_once(SYSTEM_PATH . "Cache.php");
require_once(SYSTEM_PATH . "Event.php");
require_once(SYSTEM_PATH . "RPC.php");
require_once(SYSTEM_PATH . "User.php");
require_once(SYSTEM_PATH . "Picture.php");
}
private function loadCommonLibraries() {
// XMPPtoForm lib
require_once(LIB_PATH . "XMPPtoForm.php");
// SDPtoJingle and JingletoSDP lib :)
//require_once(LIB_PATH . "SDPtoJingle.php");
//require_once(LIB_PATH . "JingletoSDP.php");
}
private function loadHelpers() {
foreach(glob(HELPERS_PATH."*Helper.php") as $file) {
require $file;
}
}
private function loadDispatcher() {
require_once(SYSTEM_PATH . "template/TplPageBuilder.php");
require_once(SYSTEM_PATH . "controllers/BaseController.php");
require_once(SYSTEM_PATH . "controllers/AjaxController.php");
require_once(SYSTEM_PATH . "Route.php");
require_once(SYSTEM_PATH . "controllers/FrontController.php");
require_once(SYSTEM_PATH . "widget/WidgetBase.php");
require_once(SYSTEM_PATH . "widget/WidgetWrapper.php");
//require_once(APP_PATH . "widgets/WidgetCommon/WidgetCommon.php");
require_once(APP_PATH . "widgets/Notification/Notification.php");
}
/**
* Loads up the language, either from the User or default.
*/
function loadLanguage() {
$user = new User();
$user->reload();
$cd = new \Modl\ConfigDAO();
$config = $cd->get();
$l = \Movim\i18n\Locale::start();
if($user->isLogged()) {
$lang = $user->getConfig('language');
}
if(isset($lang)) {
$l->load($lang);
} elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$l->detect();
$l->loadPo();
} else {
$l->load($config->locale);
}
}
private function setLogs() {
ini_set('display_errors', 0);
ini_set('error_log', DOCUMENT_ROOT.'/log/php.log');
set_error_handler('systemErrorHandler', E_ALL);
register_shutdown_function('fatalErrorShutdownHandler');
}
private function setTimezone() {
// We set the default timezone to the server timezone
$cd = new \Modl\ConfigDAO();
$config = $cd->get();
// And we set a global offset
define('TIMEZONE_OFFSET', getTimezoneOffset($config->timezone));
date_default_timezone_set($config->timezone);
}
private function setLogLevel() {
// We set the default timezone to the server timezone
$cd = new \Modl\ConfigDAO();
$config = $cd->get();
define('LOG_LEVEL', (int)$config->loglevel);
}
private function loadModl() {
// We load Movim Data Layer
$db = Modl\Modl::getInstance();
$db->setModelsPath(APP_PATH.'models');
Modl\Utils::loadModel('Config');
Modl\Utils::loadModel('Presence');
Modl\Utils::loadModel('Contact');
Modl\Utils::loadModel('Privacy');
Modl\Utils::loadModel('RosterLink');
Modl\Utils::loadModel('Cache');
Modl\Utils::loadModel('Postn');
Modl\Utils::loadModel('Subscription');
Modl\Utils::loadModel('Caps');
Modl\Utils::loadModel('Item');
Modl\Utils::loadModel('Message');
Modl\Utils::loadModel('Sessionx');
Modl\Utils::loadModel('Conference');
Modl\Utils::loadModel('Tag');
if(file_exists(DOCUMENT_ROOT.'/config/db.inc.php')) {
require DOCUMENT_ROOT.'/config/db.inc.php';
} else {
throw new Exception('Cannot find config/db.inc.php file');
}
$db->setConnectionArray($conf);
$db->connect();
return true;
}
private function startingSession() {
$s = \Sessionx::start();
$s->load();
$user = new User;
$db = Modl\Modl::getInstance();
$db->setUser($user->getLogin());
}
}