-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
303 lines (231 loc) · 8.51 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
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
<?php
$start_time = microtime(true);
//define root path
define('ROOT_PATH', dirname(__FILE__) . "/");
error_reporting(E_ALL);
require("system/core/init.php");
//reset OpCache in debug mode
if (CLEAR_OP_CACHE) {
//http://php.net/manual/en/function.opcache-reset.php
//http://php.net/manual/en/function.opcache-invalidate.php
opcache_reset();
}
//throw event
Events::throwEvent("start_session");
//get domain
$domain = Domain::getCurrent();
//check, if redirect is enabled
if ($domain->isRedirectUrl()) {
if ($domain->getRedirectCode() == 301) {
header("HTTP/1.1 301 Moved Permanently");
} else if ($domain->getRedirectCode() == 302) {
header("HTTP/1.1 302 Found");
}
header("Location: " + $domain->getRedirectUrl());
header("Connection: close");
exit;
}
//check, if gzip compression is enabled
if (Settings::get("gzip_compression", false)) {
//use gzip compression
ob_start();
}
//TODO: remove this code in production
if (isset($_REQUEST['clear_cache'])) {
//clear cache
Cache::clear();
//clear gettext cache
PHPUtils::clearGetTextCache();
echo "Clear cache!<br />";
}
//TODO: remove this code later
if (isset($_REQUEST['generate_csrf_token'])) {
Security::generateNewCSRFToken();
}
//create new instance of registry
$registry = Registry::singleton();
$registry->setSetting("clear_cache", false);
//get domain
$domain = new Domain();
$domain->load();
$registry->storeObject("domain", $domain);
$registry->setSetting("domain_name", DomainUtils::getCurrentDomain());
$mobile_detection_start_time = microtime(true);
//mobile detection
$registry->setSetting("isMobile", Browser::isMobile());
$registry->setSetting("isDesktop", !Browser::isMobile());
$mobile_detection_end_time = microtime(true);
$mobile_detection_exec_time = $mobile_detection_end_time - $mobile_detection_start_time;
//get prefered language
$registry->setSetting("pref_lang", Lang::getPrefLangToken());
$registry->setSetting("lang_token", Lang::getLangToken(Lang::listSupportedLangTokens()));
//set default language domain
Translator::getBackend()->setDefaultDomain("core");
//get user
$user = User::current();
$registry->storeObject("user", $user);
//get my groups
$groups = new Groups();
$groups->loadMyGroups($user->getID());
$registry->storeObject("groups", $groups);
//get permission checker
$registry->storeObject("permission_checker", PermissionChecker::current());
$page = new Page();
$page->load();
//load folder
$folder = new Folder($page->getFolder());
$folder->load($page->getFolder());
//check, if user has folder permissions
if (!$folder->checkPermissions(PermissionChecker::current())) {
//user dont has permissions to access folder
$page->load("error403");
}
//set folder
$registry->setSetting("folder", $page->getFolder());
//create page type
$page_type = PageLoader::loadInstance($page->getPageType());
$page_type->setPage($page);
//check, if user has page permissions
if (!$page_type->checkPermissions(PermissionChecker::current())) {
//user dont has custom permissions to access page
$page->load("error403");
//create page type
$page_type = PageLoader::loadInstance($page->getPageType());
$page_type->setPage($page);
}
//check page rights
$page_rights = new PageRights($page);
$page_rights->load();
//permission to see a published page
$page_permission = "see";
//check, if page is not published
if (!$page->isPublished()) {
//another permission is required, because page is not published yet
if (isset($_REQUEST['preview']) && $_REQUEST['preview'] == "true") {
//show preview
//check permissions
if (PermissionChecker::current()->hasRight("can_edit_all_pages") || (PermissionChecker::current()->hasRight("can_edit_own_pages") && $page->getAuthorID() == User::current()->getID())) {
//editor's can see previews of this page, also if they aren't published yet
$page_permission = "see";
} else {
$page_permission = "see_draft";
}
} else {
$page_permission = "see_draft";
}
}
if (!$page_rights->checkRights($user->getID(), $groups->listGroupIDs(), $page_permission)) {
//user dont has custom permissions to access page
$page->load("error403");
//create page type
$page_type = PageLoader::loadInstance($page->getPageType());
$page_type->setPage($page);
}
$registry->storeObject("page", $page);
$registry->storeObject("folder", $folder);
$registry->storeObject("page_type", $page_type);
//set content type
header("Content-Type: " . $page_type->getContentType());
$page_type->setCustomHeader();
Events::throwEvent("http_header");
//get current style
$registry->setSetting("current_style_name", StyleController::getCurrentStyle($registry, $page, $page_type));
//set login & logout url
$redirect_url = urlencode(DomainUtils::getURL());
if (isset($_REQUEST['redirect_url']) && !empty($_REQUEST['redirect_url'])) {
$redirect_url = $_REQUEST['redirect_url'];
}
$registry->setSetting("login_url", DomainUtils::getBaseURL() . "/" . Settings::get("login_page", "login") . "?action=login&redirect_url=" . $redirect_url);
$registry->setSetting("logout_url", DomainUtils::getBaseURL() . "/" . Settings::get("logout_page", "logout") . "?csrf_token=" . urlencode(Security::getCSRFToken()));
$registry->setSetting("thirdparty_url", DomainUtils::getBaseURL() . "/system/thirdparty/");
//get (global) main menu
$menuID = (int) ($page->getGlobalMenuID() != -1) ? $page->getGlobalMenuID() : ($folder->hasCustomMainMenu() ? $folder->getMainMenu() : Settings::get("main_menuID"));
$menu = new Menu($menuID, "menu");
$menu->loadMenu($menuID, $folder);
$registry->storeObject("main_menu", $menu);
//get (local) main menu
$localMenuID = (int) ($page->getLocalMenuID() != -1) ? $page->getLocalMenuID() : ($folder->hasCustomLocalMenu() ? $folder->getLocalMenu() : Settings::get("local_menuID"));
$localMenu = new Menu($menuID, "localmenu");
$localMenu->loadMenu($localMenuID, $folder);
$registry->storeObject("local_menu", $localMenu);
//load sidebar
$sidebar_controller = new SidebarController($page);
$left_sidebar = $sidebar_controller->getLeftSidebar();
$right_sidebar = $sidebar_controller->getRightSidebar();
$registry->storeObject("left_sidebar", $left_sidebar);
$registry->storeObject("right_sidebar", $right_sidebar);
$registry->setSetting("header", "");
$registry->setSetting("footer", "");
Events::throwEvent("Show page", array(
'registry' => &$registry
));
//show page here
if ($page_type->showDesign()) {
//show page with design
StyleController::showPage($registry, $page, $page_type);
} else {
//only show content
echo $page_type->getContent();
if ($page_type->exitAfterOutput()) {
//flush gzip cache
ob_end_flush();
exit;
}
}
$end_time = microtime(true);
$exec_time = $end_time - $start_time;
if ($page_type->showHTMLComments()) {
//benchmark code
if (ACTIVATE_BENCHMARK) {
echo "<!-- page was generated in " . $exec_time . " seconds -->\n";
echo "<!-- mobile detection executed in " . $mobile_detection_exec_time . " seconds, isMobile: " . ($registry->getSetting("isMobile") ? "true" : "false") . " -->\n";
//benchmark dwoo template engine
foreach (DwooTemplate::listFileBenchmark() as $file=>$exec_time) {
echo "<!-- Dwoo benchmark file '" . $file . "': " . $exec_time . " seconds -->\n";
}
foreach (CSSBuilder::listBenchmarks() as $key=>$exec_time) {
echo "<!-- css generation of file '" . $key . "': " . $exec_time . " seconds -->\n";
}
foreach (JSBuilder::listBenchmarks() as $key=>$exec_time) {
echo "<!-- js generation of file '" . $key . "': " . $exec_time . " seconds -->\n";
}
}
if (DEBUG_MODE) {
echo "<!-- userID: " . User::current()->getID() . ", username: " . User::current()->getUsername() . " -->\n";
echo "<!-- " . Database::getInstance()->countQueries() . " sql queries executed -->\n";
echo "<!-- pref_lang: " . $registry->getSetting("pref_lang") . " -->\n";
echo "<!-- lang_token: " . $registry->getSetting("lang_token") . " -->\n";
if (DEBUG_SQL_QUERIES) {
foreach (Database::getInstance()->listQueryHistory() as $query_array) {
echo "<!-- query: " . $query_array['query'] . " -->\n";
}
}
}
}
//flush gzip cache
ob_flush();
//ob_end_flush();
//ob_end_flush();
flush();
//clear cache
if ($registry->getSetting("clear_cache") == true) {
//clear cache
Cache::clear();
}
//update online list
User::current()->updateOnlineList();
//https://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection
ignore_user_abort(true);
//execute tasks
if (!Settings::get("cronjon_enabled", true)) {
Tasks::schedule(Settings::get("max_tasks_on_site", 3));
}
//save changed settings into database
Settings::saveAsync();
//send logs to server
if (LOGGING_ENABLED) {
Logger::send();
}
ob_end_flush();
ob_end_flush();
?>