-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
107 lines (87 loc) · 2.46 KB
/
bootstrap.php
File metadata and controls
107 lines (87 loc) · 2.46 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
<?php
declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
date_default_timezone_set('Europe/Berlin');
const APP_ROOT = __DIR__;
function config_path(): string
{
return APP_ROOT . '/config/config.php';
}
function is_setup_script(): bool
{
return basename((string) ($_SERVER['SCRIPT_NAME'] ?? '')) === 'setup.php';
}
$appConfig = [];
if (file_exists(config_path())) {
$appConfig = require config_path();
}
function app_config(string $key, $default = null)
{
global $appConfig;
return $appConfig[$key] ?? $default;
}
function db(): PDO
{
static $pdo = null;
if ($pdo instanceof PDO) {
return $pdo;
}
$dbHost = (string) app_config('db_host', '127.0.0.1');
$dbPort = (int) app_config('db_port', 3306);
$dbName = (string) app_config('db_name', '');
$dbUser = (string) app_config('db_user', '');
$dbPass = (string) app_config('db_pass', '');
if ($dbName === '' || $dbUser === '') {
throw new RuntimeException('Konfiguration unvollstaendig. Bitte setup.php ausfuehren.');
}
$dsn = sprintf('mysql:host=%s;port=%d;dbname=%s;charset=utf8mb4', $dbHost, $dbPort, $dbName);
$pdo = new PDO($dsn, $dbUser, $dbPass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
return $pdo;
}
function app_is_installed(): bool
{
if (!file_exists(config_path())) {
return false;
}
try {
$stmt = db()->query("SHOW TABLES LIKE 'settings'");
return (bool) $stmt->fetchColumn();
} catch (Throwable $e) {
return false;
}
}
function app_settings(): array
{
static $settings = null;
if (is_array($settings)) {
return $settings;
}
if (!app_is_installed()) {
return [];
}
$stmt = db()->query('SELECT * FROM settings WHERE id = 1');
$settings = $stmt->fetch() ?: [];
return $settings;
}
if (!app_is_installed() && !is_setup_script()) {
header('Location: setup.php');
exit;
}
require_once APP_ROOT . '/lib/helpers.php';
require_once APP_ROOT . '/lib/auth.php';
require_once APP_ROOT . '/lib/audit.php';
require_once APP_ROOT . '/lib/login_guard.php';
require_once APP_ROOT . '/lib/layout.php';
if (app_is_installed()) {
ensure_training_groups_whatsapp_join_url_column();
purge_expired_invitations();
if (random_int(1, 20) === 1) {
purge_old_audit_logs();
}
}