aboutsummaryrefslogtreecommitdiff
path: root/webroot/index.php
blob: dd8a6d9418f4ee3f87e3580d2e03581c971ea453 (plain)
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
<?php

spl_autoload_register(function ($class) {
    $sep = DIRECTORY_SEPARATOR;
    $cname = str_replace('\\', $sep, $class);
    include __DIR__ . "$sep..$sep$cname.php";
});

$path = $_SERVER['PATH_INFO'] ?? $_SERVER['REQUEST_URI'] ?? null;

if (!isset($path)) {
    http_response_code(500);
    echo "<p>Server misconfiguration! couldn't figure out the request path</p>";
    exit;
}

$path = explode('?', $path, 2)[0];

$config = parse_ini_file(__DIR__ . '/../config.ini', true, INI_SCANNER_TYPED);

require_once __DIR__ . '/../localisation.php';

Psso\XMLResponse::addStylesheet('../templates/index.xsl');
Psso\XMLResponse::addPreamble(function ($doc) use ($config) {
    $doc->addAttribute('site-name', $config['site']['name']);
    $doc->addAttribute('source', $config['site']['source-location']);
});

$router = new Psso\Router('../routes', $config);

$router->registerStatusHandler(404, function ($path) {
    $resp = new Psso\XMLResponse;
    $resp->doc->addAttribute('title', L('error.not-found'));
    $resp->doc->addChild('error', L('error.not-found.long', $path));
    $resp->send();
});

$router->registerStatusHandler(405, function ($path) {
    $method = $_SERVER['REQUEST_METHOD'];
    $resp = new Psso\XMLResponse;
    $resp->doc->addAttribute('title', L('error.wrong-method'));
    $resp->doc->addChild('error', L('error.wrong-method.long', $method));
    $resp->send();
});

$router->setExceptionHandler(function (Throwable $e) {
    $resp = new Psso\XMLResponse;
    $resp->doc->addAttribute('title', L('error.uncaught'));
    $resp->doc->addChild('error', $e::class . ': ' . $e->getMessage());
    $resp->send();
    error_log($e);
});

$router->dispatch($path);