-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.php
147 lines (123 loc) · 5.06 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
<?php
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use JsonSchema\Validator;
/**
* Booting...
* [================> ]
*
* @copyright (C) 2013 by OKFN Belgium
* @license AGPLv3
* @author Michiel Vancoillie
* @author Jan Vansteenlandt
* @author Pieter Colpaert
*/
// Autoload dependencies with composer (PSR-0)
require_once VENDORPATH . "autoload.php";
// Load the configurator
require_once APPPATH . "core/configurator.php";
// Load the configuration wrapper
require_once APPPATH . "core/Config.php";
// Load the start controllers
require_once APPPATH . "controllers/ErrorController.class.php";
require_once APPPATH . "controllers/DocumentationController.class.php";
require_once APPPATH . "controllers/RedirectController.class.php";
// Load auth classes
require_once APPPATH . "auth/Auth.php";
require_once APPPATH . "auth/BasicAuth.php";
$c = new ErrorController();
// Keep cores as the last item
$config_files = array(
"general",
"routes",
"db",
"cores",
"auth"
);
$config_validator = new Validator();
// Check if all config files are present and validate them
foreach($config_files as $file){
$filename = APPPATH. "config/". $file . ".json";
$schema = APPPATH. "config/schema/". $file . "-schema.json";
if(!file_exists($filename)){
echo "The file $file doesn't exist. Please check whether you have copied ". APPPATH ."config/$file.example.json to ".$filename;
exit();
}elseif(file_exists($schema)){
// Validate config file if schema exists
$config_validator->check(json_decode(Configurator::stripComments(file_get_contents($filename))), json_decode(file_get_contents($schema)));
if (!$config_validator->isValid()) {
echo "JSON ($file.json) does not validate. Violations:\n";
foreach ($config_validator->getErrors() as $error) {
echo sprintf("[%s] %s\n",$error['property'], $error['message']);
}
exit();
}
}
}
// Start loading config files
try{
$config = Configurator::load($config_files);
}catch(Exception $e){
// TODO: show nice error page
echo $e->getMessage();
exit();
}
// Pass on the configuration
app\core\Config::setConfig($config);
// General getallheaders function
if (!function_exists("getallheaders" )){
function getallheaders(){
foreach ($_SERVER as $name => $value){
if (substr($name, 0, 5) == "HTTP_" ) {
$headers[str_replace( " ", "-", ucwords(strtolower(str_replace("_" , " " , substr($name, 5)))))] = $value;
} else if ($name == "CONTENT_TYPE") {
$headers["Content-Type"] = $value;
} else if ($name == "CONTENT_LENGTH") {
$headers["Content-Length"] = $value;
}
}
return $headers;
}
}
// Start the router
require_once APPPATH."core/router.php";
// Hacking the brains of other people using fault injection
// http://jlouisramblings.blogspot.dk/2012/12/hacking-brains-of-other-people-with-api.html
if(app\core\Config::get("general", "faultinjection", "enabled")){
// Return a 503 Service Unavailable ~ each {period} requests and add Retry-After header
if(! rand(0, app\core\Config::get("general", "faultinjection", "period") -1) ){
set_error_header("503","Service Unavailable");
header("Retry-After: 0");
exit();
}
}
// Prepare the error handler defined at the end of this file
set_error_handler("wrapper_handler");
// Initialize the timezone
date_default_timezone_set(app\core\Config::get("general","timezone"));
// TODO: add Tracker
/**
* This function is called when an unexpected error(non-exception) occurs
* @param integer $number Number of the level of the error that's been raised.
* @param string $string Contains errormessage.
* @param string $file Contains the filename in which the error occured.
* @param integer $line Represents the linenumber on which the error occured.
* @param string $context Context is an array that points to the active symbol table at the point the error occurred. In other words, errcontext will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context.
*/
function wrapper_handler($number, $string, $file, $line, $context){
global $log;
$error_message = $string . " on line " . $line . " in file ". $file . ".";
$log = new Logger('bootstrap');
$log->pushHandler(new StreamHandler(app\core\Config::get("general", "logging", "path") . "/log_" . date('Y-m-d') . ".txt", Logger::ERROR));
$log->addError($error_message);
echo "<script>location = \"" . app\core\Config::get("general","hostname") . app\core\Config::get("general","subdir") . "error/critical\";</script>";
set_error_header(500,"Internal Server Error");
//No need to continue
exit(0);
}
function set_error_header($code,$short){
// All header cases for different servers (FAST CGI, Apache...)
header($_SERVER["SERVER_PROTOCOL"]." ". $code ." ". $short);
header("Status: ". $code ." ". $short);
$_SERVER['REDIRECT_STATUS'] = $code;
}