-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
97 lines (79 loc) · 2.47 KB
/
Copy pathindex.php
File metadata and controls
97 lines (79 loc) · 2.47 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
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/src/db.php';
require_once __DIR__ . '/src/auth.php';
$auth = new SessionAuth();
$flash = null;
if (isset($_SESSION['flash'])) {
$flash = $_SESSION['flash'];
unset($_SESSION['flash']);
}
function flash(string $type, string $message): void {
$_SESSION['flash'] = ['type' => $type, 'message' => $message];
}
function redirect(string $page): void {
header('Location: ?page=' . $page);
exit;
}
function csrfToken(): string {
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
function csrfField(): string {
return '<input type="hidden" name="csrf_token" value="' . htmlspecialchars(csrfToken()) . '">';
}
function verifyCsrf(): void {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return;
}
$token = $_POST['csrf_token'] ?? '';
if (!hash_equals(csrfToken(), $token)) {
http_response_code(403);
exit('Invalid request. Please go back and try again.');
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verifyCsrf();
}
$currentPage = $_GET['page'] ?? 'dashboard';
if ($currentPage === 'logout') {
$auth->logout();
redirect('login');
}
$db = getDb();
$userCount = $db->query('SELECT COUNT(*) FROM users')->fetchColumn();
if ($userCount == 0 && $currentPage !== 'install') {
$currentPage = 'install';
}
$publicPages = ['login', 'install', 'unsubscribe'];
if (!in_array($currentPage, $publicPages)) {
$auth->requireAuth();
}
$pages = [
'login' => 'templates/pages/login.php',
'install' => 'templates/pages/install.php',
'dashboard' => 'templates/pages/dashboard.php',
'families' => 'templates/pages/families.php',
'family-detail' => 'templates/pages/family-detail.php',
'settings' => 'templates/pages/settings.php',
'unsubscribe' => 'templates/pages/unsubscribe.php',
'reset' => 'templates/pages/reset.php',
];
if (!isset($pages[$currentPage])) {
$currentPage = 'dashboard';
}
$pageTemplate = $pages[$currentPage];
$pageTitles = [
'login' => 'Log In',
'install' => 'Setup',
'dashboard' => 'Dashboard',
'families' => 'Families',
'family-detail' => 'Family',
'settings' => 'Settings',
'unsubscribe' => 'Unsubscribe',
'reset' => 'Reset',
];
$pageTitle = $pageTitles[$currentPage] ?? 'Campfire';
include __DIR__ . '/templates/layout.php';