Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
141 lines (123 loc) · 5.25 KB
/
Copy pathindex.php
File metadata and controls
141 lines (123 loc) · 5.25 KB
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
<?php
/**
* OpenParliamentTV Platform — Main Entry Point
*
* 1. Session, config, security, i18n bootstrap
* 2. Plates template engine
* 3. FastRoute dispatcher
* 4. Centralized auth check at the route level
* 5. Handler dispatch
*
* Clean URLs are routed here by .htaccess (single catch-all). The old
* switch($_REQUEST["a"]) router and per-template auth boilerplate are gone;
* routes live in routes/web.php and handlers in modules/routing/handlers.php.
*/
require_once(__DIR__ . "/bootstrap.php"); // session, config, headers, i18n, view globals
// Load search functions at global scope so the OpenSearch client global
// ($ESClient, set at the top of search/opensearch.php) is available to the
// search/media handlers — which now run in function scope rather than the old
// global switch.
require_once(__DIR__ . "/modules/search/opensearch.php");
// Build the clean query-string ($paramStr) and result flag ($isResult) used by
// header.php and RSS autodiscovery — reuses the same allow-list as before.
$paramStr = "";
$allowedParams = filterAllowedSearchParams($_REQUEST, 'media');
$paramCount = 1;
foreach ($allowedParams as $k => $v) {
$paramPrefix = ($paramCount == 1) ? "?" : "&";
if (is_array($v)) {
foreach ($v as $i) {
$paramStr .= $paramPrefix . $k . "[]=" . urlencode($i);
}
} else {
$paramStr .= $paramPrefix . $k . "=" . urlencode($_REQUEST[$k]);
}
$paramCount++;
}
$isResult = (strlen($paramStr) > 2);
// Plates engine (reads the globals set above)
require_once(__DIR__ . "/modules/templating/engine.php");
$plates = createPlatesEngine();
// Routing
require_once(__DIR__ . "/modules/routing/auth.php");
require_once(__DIR__ . "/modules/routing/handlers.php");
// --- Per-client page rate limit (coarse anti-scrape guard) ---
// Only true router-driven navigations reach here; static files, AJAX fragments
// and the image proxy are served directly by Apache and are not counted.
// Logged-in users and exemptIPs are exempted inside the limiter.
require_once(__DIR__ . "/modules/utilities/ratelimit.php");
$pageRateLimit = $config["rateLimit"]["pages"] ?? null;
if ($pageRateLimit) {
$pageRetryAfter = optvRateLimitExceeded($pageRateLimit, 'page');
if ($pageRetryAfter !== null) {
pageRender429($plates, $pageRetryAfter);
exit;
}
}
// --- Build the request path (strip query string and any base path) ---
$uri = $_SERVER['REQUEST_URI'];
if (false !== ($pos = strpos($uri, '?'))) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
$basePath = rtrim(parse_url($config['dir']['root'], PHP_URL_PATH) ?? '', '/');
if ($basePath !== '' && strpos($uri, $basePath) === 0) {
$uri = substr($uri, strlen($basePath));
}
if ($uri === '' || $uri === false) {
$uri = '/';
}
// --- Dispatch ---
$dispatcher = FastRoute\simpleDispatcher(require __DIR__ . '/routes/web.php');
$routeInfo = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $uri);
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
pageRender404($plates);
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
http_response_code(405);
pageRender404($plates);
break;
case FastRoute\Dispatcher::FOUND:
$routeData = $routeInfo[1];
$params = $routeInfo[2];
// Legacy compatibility shim: some templates / client JS still read
// $_REQUEST["a"] (legacy page key) and $_REQUEST["id"]. The embed route's
// {type} path segment is intentionally NOT mirrored, since the real
// entity type arrives as ?type=... in the query string.
$_REQUEST['a'] = $routeData['page'] ?? '';
if (isset($params['id'])) {
$_REQUEST['id'] = $params['id'];
}
// --- Centralized auth check (skipped for public embeds & feeds) ---
// Uses the route's `access` level ('public'|'admin'), which is the AUTH
// dimension only — separate from the presentation `pageType` the handlers
// put on the render data.
if (empty($routeData['skipAuth'])) {
$access = $routeData['access'] ?? 'public';
if (isset($routeData['accessResolver']) && is_callable($routeData['accessResolver'])) {
$access = $routeData['accessResolver']($params);
}
$authResult = authCheckPage($access);
// Some entity-typed pages (e.g. notifications) are public by page type
// but still require an authenticated session — reproduces the old
// inline `empty($_SESSION["login"])` guard.
if ($authResult === true && !empty($routeData['requireLogin']) && empty($_SESSION['login'])) {
$authResult = ['alertText' => ''];
}
if ($authResult !== true) {
optvRenderPage($plates, 'pages/login/page', [
'page' => 'login',
'pageType' => 'default',
'pageTitle' => L::login(),
'pageBreadcrumbs' => [['label' => L::login()]],
'alertText' => $authResult['alertText'],
]);
break;
}
}
// --- Dispatch to the handler ---
$handlerName = $routeData['handler'];
$handlerName($routeData, $params, $plates);
break;
}