-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.php
187 lines (155 loc) · 5.28 KB
/
init.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
<?php
if (!defined('PROJECT_PATH')) {
exit("Define a PROJECT_PATH global variable");
}
if (!defined('SITE_PATH')) {
exit("Define a SITE_PATH global variable");
}
if (!is_file(GLOBAL_PATH.PROJECT_PATH."settings/".SITE_PATH.".php")) {
exit('No configuration file found for site "'.PROJECT_PATH.SITE_PATH.'"');
}
//old php version fix
if (!class_exists('Error')) {
class Error extends Exception {}
}
//old php version fix
if (!class_exists('Success')) {
class Success extends Exception {}
}
//initialize request methods
//in addition to work they are called in the rewrite class
//they are also added to $_REQUEST
//but won't work inside a function must use global
$_DELETE = array();
$_PUT = array();
$_PATCH = array();
if (isset($_SERVER['REQUEST_METHOD'])) {
$contents = file_get_contents('php://input');
if (!empty($contents)) {
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
if ($_SERVER['CONTENT_TYPE'] === 'application/json') {
$_DELETE = json_decode($contents, true);
if (empty($_DELETE)) {
exit("Invalid JSON: ".$contents);
}
} else {
mb_parse_str($contents, $_DELETE);
}
$_REQUEST = array_merge($_REQUEST, $_DELETE);
} elseif ($_SERVER['REQUEST_METHOD'] == 'PUT') {
if ($_SERVER['CONTENT_TYPE'] === 'application/json') {
$_PUT = json_decode($contents, true);
if (empty($_PUT)) {
exit("Invalid JSON: ".$contents);
}
} else {
mb_parse_str($contents, $_PUT);
}
$_REQUEST = array_merge($_REQUEST, $_PUT);
} elseif ($_SERVER['REQUEST_METHOD'] == 'PATCH') {
if ($_SERVER['CONTENT_TYPE'] === 'application/json') {
$_PATCH = json_decode($contents, true);
if (empty($_PATCH)) {
exit("Invalid JSON: ".$contents);
}
} else {
mb_parse_str($contents, $_PATCH);
}
$_REQUEST = array_merge($_REQUEST, $_PATCH);
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_SERVER['CONTENT_TYPE'] === 'application/json') {
$_POST = json_decode($contents, true);
if (empty($_POST)) {
exit("Invalid JSON: ".$contents);
}
} else {
mb_parse_str($contents, $_POST);
}
$_REQUEST = array_merge($_REQUEST, $_POST);
}
}
}
try {
//require current site settings
require_once (GLOBAL_PATH.PROJECT_PATH."settings/".SITE_PATH.".php");
if (!isset($info) || empty($info) || !is_array($info)) {
exit('Variable $info must be present in the site config file and must be a non-empty array!');
}
$platformFiles = array(
'class.mc',
'class.db',
'globalTemplates',
'core');
foreach ($platformFiles as $file) {
if (!is_file(GLOBAL_PATH.'platform/core/'.$file.".php")) {
exit("File $file.php is missing!");
}
require_once (GLOBAL_PATH.'platform/core/'.$file.'.php');
}
$Core = new Core($info);
//init Language class to listen for language change if multilanguage is allowed
if ($Core->allowMultilanguage) {
$Core->Language;
}
//init images class if present
if ($Core->{$Core->imagesModel}) {
$Core->{$Core->imagesModel};
}
//init users class if present
if ($Core->{$Core->userModel}) {
$Core->{$Core->userModel}->init();
}
if ($Core->multiLanguageLinks === true) {
$Core->Links->setLanguageChangeLinks(
$Core->Links->getLanguageChangeLinksOfController()
);
}
//get required files
$Core->Rewrite->getFiles();
unset($info, $platformFiles, $file);
} catch (Success $e) {
ob_end_clean();
if (isset($Core) && is_object($Core->exceptionhandler)) {
$Core->exceptionhandler->Success($e);
} else {
header('HTTP/1.1 500 Internal Server Error', true, 500);
echo "Internal Server Error";
}
die;
} catch (Error $e) {
ob_end_clean();
if (isset($Core) && is_object($Core->exceptionhandler)) {
$Core->exceptionhandler->Error($e);
} else {
header('HTTP/1.1 500 Internal Server Error', true, 500);
echo "Internal Server Error";
}
die;
} catch (UnauthorizedException $e) {
ob_end_clean();
if (isset($Core) && is_object($Core->exceptionhandler)) {
$Core->exceptionhandler->UnauthorizedException($e);
} else {
header('HTTP/1.1 500 Internal Server Error', true, 500);
echo "Internal Server Error";
}
die;
} catch (BaseException $e) {
ob_end_clean();
if (isset($Core) && is_object($Core->exceptionhandler)) {
$Core->exceptionhandler->BaseException($e);
} else {
header('HTTP/1.1 500 Internal Server Error', true, 500);
echo "Internal Server Error";
}
die;
} catch (Exception $e) {
ob_end_clean();
if (isset($Core) && is_object($Core->exceptionhandler)) {
$Core->exceptionhandler->Exception($e);
} else {
header('HTTP/1.1 500 Internal Server Error', true, 500);
echo "Internal Server Error";
}
die;
}