-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
144 lines (115 loc) · 3.63 KB
/
index.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
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
142
143
144
<?php
if (!file_exists('vendor/autoload.php')) {
echo 'You need to run <code>composer install</code> before you can use this software.';
exit;
}
require 'vendor/autoload.php';
use Ubersite\Config;
use Ubersite\DatabaseManager;
use Ubersite\MessageBank;
use Ubersite\NullUser;
use Ubersite\Software;
use Ubersite\User;
use Ubersite\Utils;
$config = new Config();
if (!$config->isLoaded()) {
header('Location: /setup');
exit;
}
ini_set('display_errors', 'On');
$dbh = DatabaseManager::get();
// URL rewriter
// Courtesy of http://stackoverflow.com/questions/893218/rewrite-for-all-urls
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$SEGMENTS = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
$SEGMENTS = array_map("strtolower", $SEGMENTS);
for ($i = 0; $i <= 9; $i++) {
if (!isset($SEGMENTS[$i])) {
$SEGMENTS[$i] = null;
}
}
$PAGE = $SEGMENTS[0];
// End URL rewriter
if (strlen($PAGE) == 0) {
$PAGE = 'questionnaire';
}
header("Content-Type: text/html; charset=utf-8");
// Register the Twig autoloader so we can use Twig templates
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader);
// Process user session and details
session_start();
$messages = new MessageBank();
/** @var User $user */
$user = null;
$script = explode("/", $_SERVER['SCRIPT_NAME']);
$pageName = $PAGE;
// Special handling for logout page
if ($pageName == 'logout') {
session_destroy();
header('Location: /');
exit;
}
// Populate array of users (Username => User object)
$stmt = $dbh->query('SELECT * FROM users');
/** @var User[] */
$people = [];
while ($row = $stmt->fetch()) {
$people[$row['Username']] = User::createFromRow($row);
}
if (count($people) === 0) {
if ($PAGE != 'account-import') {
header('Location: /account-import');
exit;
}
} else {
if (isset($_SESSION['username'])) {
// If the logged in user no longer exists, something bad happened.
if (!isset($people[$_SESSION['username']])) {
header('Location: /logout');
exit;
}
$user = $people[$_SESSION['username']];
} else {
// Redirect to login page if not logged in
if ($pageName != 'login') {
header("Location: /login/$pageName");
exit;
}
}
}
// Disable error reporting for campers
if ($user && !$user->isLeader()) {
error_reporting(0);
}
// Standalone mode includes all relevant resources directly onto the page so that only the HTML
// file needs to be saved to get a complete copy.
if (isset($_GET['standalone'])) {
$layoutCSS = file_get_contents("resources/css/layout.css");
$colourCSS = file_get_contents("resources/css/winter.css");
$standalone = [
'logo' => Utils::dataURI("resources/img/logo.png", "image/png"),
'icon' => Utils::dataURI("resources/img/icon.png", "image/png"),
'css' => $layoutCSS . "\n\n" . $colourCSS
];
$twig->addGlobal('standalone', $standalone);
}
// TODO: we probably shouldn't be using $twig->addGlobal so much
$twig->addGlobal('config', $config);
$twig->addGlobal("software", new Software());
$twig->addGlobal("user", $user);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$twig->addGlobal("form", $_POST);
}
// Include the specified page
if (file_exists("controllers/$PAGE.php")) {
require_once("controllers/$PAGE.php");
// Special handling for the ajax page, as it has no normal view
if ($PAGE !== 'ajax') {
$twig->addGlobal('messagebank', $messages);
echo $twig->render("$PAGE.twig");
}
} else {
header('HTTP/1.0 404 Not Found');
echo $twig->render('404.twig');
}