-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·326 lines (304 loc) · 9.23 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
$dir = sys_get_temp_dir();
session_save_path($dir);
require_once('config/settings.php');
session_start(
[
'cookie_httponly'=>true,
'cookie_domain'=>DOMAIN,
]
);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once('config/db.php');
require_once('users/users.php');
require_once('messages/messages.php');
require_once('page.php');
use \DB\Conn as D;
use \User\User as User;
use \Message\Message as Message;
/**
* Constants
**/
define("ACTION", "action");
$db = D::get();
$data = new stdClass();
$error = false;
if ( $db == null){
$data->title = 'Oooops! Something went wrong';
$error = 'It was not possible to connect to the database';
}
if ($_SERVER['REQUEST_URI'] != '/admin/install'){
$user = new User();
}else{
$user = new stdClass();
}
$F_front_page = function($params, &$data, &$template){
global $user;
global $db;
if (empty($user->authenticated)){
$data->title = 'Welcome';
$template->content = 'templates/front.php';
}else{
$data->user = $user;
$data->title = 'Overview';
$template->content = 'templates/panel.php';
$messages = $db->retrieve_user_messages($user->id);
$m = [];
foreach ($messages as $me){
$m[] = new Message($encrypted=True, $me);
}
$data->messages = $m;
if($_POST[ACTION]??False){
$mid = intval($_POST['msg']??0);
if ($mid){
$m= $db->retrieve_message($mid);
if (!empty($m)){
$tobe_deleted = new Message($encrypted=True, $me);
$attempt = $tobe_deleted->remove_from_database();
$data->warning = $attempt!=1 ?
"Couldn't remove message." :
"Message $tobe_deleted->title was deleted.<br>It won't be shown in new requests.";
}
}
}
}
};
$F_install = function($params, &$data, &$template){
global $db;
$data->title = "Installation and upgrade";
$template->content = 'templates/install.php';
$results = $db->install();
if (!empty($results)){
$warning = new stdClass();
$warning->title = 'Errors occured during installation';
}
$data->content = "The installation process was completed. A backup of the old database was mada. Any older backup was destroyed. Your new database backup is within the same folder, with a .bup extension.";
};
$F_not_found = function($params, &$data, &$template){
$template->content = 'templates/content.php';
$data->title = 'Title not set';
$data->content = 'nothing to see here!';
};
$F_login = function($params, &$data, &$template){
$template->content = 'templates/login.php';
global $user;
if (empty($_POST)){
$template->content = 'templates/login.php';
if ($user->authenticated){
$data->title = "Welcome $user->name";
}else{
$data->title = 'Please, provide your credentials!';
}
}else{
$ok = $user->authenticate();
$data->user = $user;
$data->title = "My profile";
if (!$ok){
$err = "<ul>\n";
foreach ($user->err as $e){
$err.= "<li>$e</li>";
}
$err.= "</ul>";
$data->warning = $err;
$data->title = "";
$data->title = 'Something went wrong. Please, review your credentials!';
}else{
$data->warning = new stdClass();
$data->warning->title = "Login sucessful";
$data->warning->content = "<p>Welcome $user->name.</p>";
}
}
};
$F_register = function($params, &$data, &$template){
$template->content = 'templates/register.php';
$data->title = 'Registration';
if (!empty($_POST[ACTION]) and $_POST[ACTION] == 'cancel'){
$_SESSION = ['message'=>'Registration was canceled '];
header("Location: /panel");
die();
}
$action = 'register';
if (!empty($_SESSION[ACTION]) and $_SESSION[ACTION] == 'password_change'){
$action = 'recover';
$data->title = "Password recovery";
if (!empty($_SESSION['uname'])){
$data->recovering_user = trim($_SESSION['uname']);
}
}
if (!empty($_POST)){
global $user;
$ok = $user->$action();
if ($ok){
$data->title = "Thank you for registering, $user->name";
$data->content = "Please, log in to exchange messages with your friends.";
$template->content = 'templates/welcome.php';
}else{
$err = "<ul>\n";
foreach ($user->err as $e){
$err.= "<li>$e</li>";
}
$err.= "</ul>";
$data->warning = $err;
}
}
};
$F_dbdump = function($params, &$data, &$template){
exec(dirname(__FILE__).'/content/dump.sh');
$template->content = 'templates/dbdump.php';
};
$F_compose = function ($params, &$data, &$template){
global $user;
global $db;
$usernames = $db->all_usernames();
$data->ulist = array_column($usernames, 'name');
$data->from = $user->name;
$template->content = 'templates/compose.php';
$action = $_POST[ACTION]??'';
$post2data = [
'to' => 'to',
'title'=>'title',
'body'=>'body'
];
$_POST['from'] = $user->name;
foreach ($post2data as $k=>$v){
if (!empty($_POST[$k]) and empty($data->$v)){
$data->$v = trim($_POST[$k]);
}
}
if ($action == 'send'){
if (!in_array($_POST['to'], $data->ulist)){
$data->warning = "The user to whom you are trying to send a message is not registered.";
}else{
$envelope = Message::envelope_from_post(time());
$m = new Message($encrypted=False, $envelope=$envelope);
$ok = $m->store_message();
if ($ok){
$_SESSION['message'] = "Your message has been sent.";
$template->content = 'templates/message.php';
header("Location: /panel");
die();
}else{
$data->warning = "Something has gone wrong";
}
}
}
};
$F_logout = function ($params, &$data, &$template){
global $user;
$user->unauthenticate();
$template->content = "templates/logout.php";
$data->warning = "You were successfully logged out";
};
$F_about = function ($params, &$data, &$template){
$template->content = "templates/about.php";
};
$F_password_reset = function ($params, &$data, &$template){
if (!empty($_POST[ACTION]) and $_POST[ACTION] == 'cancel'){
$_SESSION = ['message'=>'Password recovery was canceled.'];
header("Location: /panel");
die();
}
global $db;
if (!empty($_POST['username'])){
$alleged = trim($_POST['username']);
$data->account = True;
$mail = $db->mail_by_name($alleged)['mail'];
if (empty($_POST['secret'])){
if (!empty($mail)){
$secret = trim(base64_encode(random_bytes(16)));
$db->insert_recovery($alleged, $secret);
$to = $mail;
$subject = 'Account recovery';
$message = "Hi
Here is your secret key to recover your account at ".DOMAIN."
Did you ask for a pasword recovery?
Secret: $secret
Messaging Privately security team.";
$headers = 'From: ndvo@security.ndvo.geekgalaxy.com' . "\r\n" .
'Reply-To: ndvo@security.ndvo.geekgalaxy.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$ok =mail($to, $subject, $message, $headers);
$data->warning = "A recovering key was sent to your email.";
}
}else{
$secret = trim($_POST['secret']);
$ok = $db->use_secret($alleged, $secret);
if ($ok){
$_SESSION[ACTION] = 'password_change';
$_SESSION['uname'] = $alleged;
$_SESSION['message'] = 'Secret key successfully verified';
header("Location: /register");
die();
}else{
$data->warning = "Sorry, you were not able to provide the secret.";
}
}
}else{
$data->warning = "Upon password change we will not be able to recover your messages.";
}
$template->content = "templates/passwordreset.php";
};
if (!$error){
$params = [];
$routes = [
'/^\/logout\/?$/'=> $F_logout,
'/^\/about\/?$/'=> $F_about,
'/^\/dbdump\/?$/'=> $F_dbdump,
//'/^\/admin\/install\/?$/'=> $F_install,
'/^\/compose\/?$/'=>$F_compose,
'/^\/(login|profile)\/?$/'=> $F_login,
'/^\/register\/?$/'=> $F_register,
'/^(\/panel|\/index\.html)?\/?$/'=> $F_front_page,
'/^\/password-reset\/?$/'=> $F_password_reset,
'/.*/' => $F_not_found,
];
$data->user = $user;
foreach ($routes as $pattern => $function){
$found = preg_match($pattern, $_SERVER['REQUEST_URI'], $params);
if ($found == 1){
$function($params, $data, $template);
if (!empty($_SESSION['message'])){
$data->warning = $_SESSION['message'];
unset($_SESSION['message']);
}
break;
}
}
if (!$found){
$not_found([], $data, $template);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="/css/main.css">
</head>
<body>
<header>
<?php echo \Page\template_render($template->header, $data); ?>
</header>
<nav>
<?php echo \Page\template_render($template->navigation, $data); ?>
</nav>
<?php if (!empty($data->warning)): ?>
<dialog class="warning" open >
<?php echo \Page\template_render($template->warning, $data);; ?>
<span class="close-button" onclick="this.parentNode.removeAttribute('open');this.parentNode.addAttribute('close');">
×
</span>
</dialog>
<?php endif; ?>
<main>
<?php echo \Page\template_render($template->content, $data); ?>
</main>
<footer>
<?php echo \Page\template_render($template->footer, $data); ?>
</footer>
<script src="js/main.js">
</script>
</body>
</html>