-
Notifications
You must be signed in to change notification settings - Fork 151
/
index.php
executable file
·232 lines (208 loc) · 6.79 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Copyright © 2024, Art of WiFi
* www.artofwifi.net
*
* @license This file is subject to the MIT license bundled with this package in the file LICENSE.md
*/
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
/**
* If we are using HTTPS, we need to set secure cookies.
*/
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
session_set_cookie_params(0, '/', '', true, true);
}
session_start();
/**
* We check whether the user has requested to clear (force expiry) the PHP session, if so, we
* clear the session and reload the page without the query string.
* This feature can be useful when login errors occur, mostly after upgrades or credential changes.
*/
if (isset($_GET['reset_session']) && $_GET['reset_session']) {
$_SESSION = [];
session_unset();
session_destroy();
session_start();
$current_url = $_SERVER['REQUEST_URI'];
$current_url = strtok($current_url, '?');
header("refresh: 0; url = $current_url");
}
/**
* load required packages using the composer autoloader
*/
require_once 'vendor/autoload.php';
/**
* include the common functions file
*
* @var string $curl_info
* @var string $unknown_string
* @var array $about_modal_params
*/
require_once 'common.php';
/**
* load the file containing the collections for the menu options
*
* @var array $collections
*/
require_once 'collections.php';
/**
* initialize the Twig loader early on in case we need to render the error page
*/
$loader = new FilesystemLoader('templates');
$twig = new Environment($loader);
/**
* load the configuration file, if readable, if not, stop and display an error message
*
* @var array $controllers
* @var bool $debug
* @var string $theme
* @var string $navbar_class
* @var string $navbar_bg_class
* @var bool $about_modal_params
*/
if (is_file('config/config.php') && is_readable('config/config.php')) {
require_once 'config/config.php';
} else {
/**
* render the config error page
*/
try {
echo $twig->render('config_error.html.twig', [
'error_message' => 'The file <b>config/config.php</b> does not exist! Please create one based on the <b>config/config-template.php</b> file!<br>',
]);
} catch (LoaderError $e) {
error_log('Twig LoaderError: ' . $e->getMessage());
} catch (RuntimeError $e) {
error_log('Twig RuntimeError: ' . $e->getMessage());
} catch (SyntaxError $e) {
error_log('Twig SyntaxError: ' . $e->getMessage());
}
exit;
}
/**
* inject Twig global variables for use across the templates
*/
$twig->addGlobal('tool_version', TOOL_VERSION);
$twig->addGlobal('debug', $debug);
$twig->addGlobal('session', $_SESSION);
$twig->addGlobal('navbar_class', $navbar_class);
$twig->addGlobal('navbar_bg_class', $navbar_bg_class);
$twig->addGlobal('about_modal_params', $about_modal_params);
/**
* check whether the required PHP curl module is available, if not, stop and display an error message
*/
if (!function_exists('curl_version')) {
/**
* render the config error page
*/
try {
echo $twig->render('config_error.html.twig', [
'error_message' => 'The <b>PHP curl</b> module is not installed! Please correct this before proceeding!<br>',
]);
} catch (LoaderError $e) {
error_log('Twig LoaderError: ' . $e->getMessage());
} catch (RuntimeError $e) {
error_log('Twig RuntimeError: ' . $e->getMessage());
} catch (SyntaxError $e) {
error_log('Twig SyntaxError: ' . $e->getMessage());
}
exit;
}
/**
* check whether the minimum required PHP version (5.6.0) is met
* - if not, stop and display an error message
*/
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
/**
* render the config error page
*/
try {
echo $twig->render('config_error.html.twig', [
'error_message' => 'The current PHP version (' . PHP_VERSION . ') does not meet the minimum required version which is 5.6.0. Please upgrade before proceeding!<br>',
]);
} catch (LoaderError $e) {
error_log('Twig LoaderError: ' . $e->getMessage());
} catch (RuntimeError $e) {
error_log('Twig RuntimeError: ' . $e->getMessage());
} catch (SyntaxError $e) {
error_log('Twig SyntaxError: ' . $e->getMessage());
}
exit;
}
/**
* load the file containing user accounts, if readable
*/
if (is_file('config/users.php') && is_readable('config/users.php')) {
require_once 'config/users.php';
$user_authentication = true;
if (!isset($users) || !is_array($users) || count($users) === 0) {
$user_authentication = false;
error_log('The $users array in the config/users.php file does not exist or is empty, proceeding without user authentication.');
}
} else {
$user_authentication = false;
error_log('The file config/users.php does not exist, proceeding without user authentication.');
}
/**
* if needed, we request the user to log in
*/
if ($user_authentication && (empty($_SESSION['logged_in']))) {
$login_error = '';
if (!empty($_SESSION['login_error_message'])) {
$login_error = $_SESSION['login_error_message'];
$_SESSION['login_error_message'] = '';
}
/**
* render the login page
*/
try {
echo $twig->render('login.html.twig', [
'login_error' => $login_error,
]);
} catch (LoaderError $e) {
error_log('Twig LoaderError: ' . $e->getMessage());
} catch (RuntimeError $e) {
error_log('Twig RuntimeError: ' . $e->getMessage());
} catch (SyntaxError $e) {
error_log('Twig SyntaxError: ' . $e->getMessage());
}
exit;
}
if (empty($_SESSION['controller'])) {
/**
* the user needs to select a controller first, so we render the appropriate template
*/
try {
echo $twig->render('controller_select.html.twig', [
'controllers' => $controllers,
]);
} catch (LoaderError $e) {
error_log('Twig LoaderError: ' . $e->getMessage());
} catch (RuntimeError $e) {
error_log('Twig RuntimeError: ' . $e->getMessage());
} catch (SyntaxError $e) {
error_log('Twig SyntaxError: ' . $e->getMessage());
}
} else {
/**
* proceed and render the collections_view template
*/
try {
echo $twig->render('collections_view.html.twig', [
'controllers' => $controllers,
'controller' => $_SESSION['controller'],
'collections' => $collections,
]);
} catch (LoaderError $e) {
error_log('Twig LoaderError: ' . $e->getMessage());
} catch (RuntimeError $e) {
error_log('Twig RuntimeError: ' . $e->getMessage());
} catch (SyntaxError $e) {
error_log('Twig SyntaxError: ' . $e->getMessage());
}
}
exit;