-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
98 lines (83 loc) · 3 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
<?php
declare(strict_types = 1);
namespace ObieSampleApp;
use Obie\Encoding\Uuid;
use Obie\Http\AcceptLanguageHeader;
use Obie\Http\Request;
use Obie\Http\Response;
use Obie\Http\Router;
use Obie\Log;
use Obie\View;
define('OBIE_APP_DIR', dirname(__FILE__));
$autoload_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (!is_file($autoload_path)) {
error_log('vendor/autoload.php not found. Install composer dependencies: https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies', E_USER_ERROR);
return false;
}
require $autoload_path;
unset($autoload_path);
class App extends \Obie\App {
public static function initRouter(): bool {
if (!parent::initRouter()) return false;
// Set security headers
$csp = [
'frame-ancestors' => "'none'",
'script-src' => "'nonce-" . Router::vars()->get('nonce') . "'",
];
$i = 0;
$cspstr = '';
foreach ($csp as $k => $v) {
if ($i > 0) $cspstr .= '; ';
$cspstr .= $k . ' ' . $v;
$i++;
}
Response::current()?->setHeader('Content-Security-Policy', $cspstr);
Response::current()?->setHeader('Referrer-Policy', 'same-origin');
// Set request ID
$request_id = Uuid::generate();
Response::current()?->setHeader('X-Request-Id', $request_id);
Log::setDefaultContext(['request_id' => $request_id]);
Log::setFormat("[%datetime%][%context.request_id%] %channel%.%level_name%: %context.prefix%%message%%context.suffix%\n");
// Set request timestamp
$now = new \DateTime('now', new \DateTimeZone('UTC'));
Router::vars()->set('now', $now);
Response::current()?->setHeader('Date', $now->format('D, d M Y H:i:s T'));
return true;
}
public static function initViews(): bool {
if (!parent::initViews()) return false;
View::$default_vars = array_merge_recursive(View::$default_vars, [
'nonce' => Router::vars()->get('nonce'),
'language' => static::getI18n()->getLanguage(),
]);
return true;
}
public static function initLocale(): bool {
parent::initLocale();
$accept_language = AcceptLanguageHeader::decode(Request::current()?->getHeader('Accept-Language') ?? '');
$preferred_language = $accept_language->getPreferredLanguage(App::getI18n()->getLanguages(), App::getI18n()->getLanguage());
if ($preferred_language !== null) {
static::$i18n->setLanguage($preferred_language->locale);
}
return true;
}
public static function respond(mixed $data = null, array $meta = [], int $status_code = Response::HTTP_OK, ?string $status_description = null) {
if (!Response::current()) return;
$status_description ??= Router::HTTP_STATUSTEXT[$status_code];
Response::current()->setCode($status_code);
Router::sendJSON([
'meta' => array_merge([
'statusDescription' => $status_description,
'requestId' => Router::vars()->get('nonce'),
'requestTime' => Router::vars()->get('now')?->format(\DateTime::ATOM),
], $meta),
'data' => $data,
]);
}
}
App::register();
if (!App::init()) {
App::respond(status_code: Response::HTTP_INTERNAL_SERVER_ERROR);
return;
}
return App::$app;