/
server.php
485 lines (380 loc) · 19.3 KB
/
server.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/*
Copyright Xphysics 2012. All Rights Reserved.
WRPI.FM is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
WRPI.FM is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
<http://www.gnu.org/licenses/>
*/
// Initialize the page load timer
$start_time = microtime(true);
// Get the site's domain
$domain = array();
preg_match("/[^.]+\.[^.]+$/", $_SERVER["HTTP_HOST"], $domain);
$_SERVER["site"]["domain"] = $domain[0];
// Get the site's url
$url = $_SERVER["HTTPS"] ? "https://" : "http://";
$url .= "{$_SERVER["HTTP_HOST"]}/";
$_SERVER["site"]["url"] = $url;
// Open a connection to the MySQL database
$_SERVER["mysqli"] = new mysqli($_SERVER["database"]["host"], $_SERVER["database"]["user"], $_SERVER["database"]["pass"], $_SERVER["database"]["name"]);
// Set the default timezone
date_default_timezone_set($_SERVER["site"]["timezone"]);
// Set session expiration
ini_set("session.cookie_lifetime", 60 * 60 * 24);
ini_set("session.gc_maxlifetime", 60 * 60 * 24);
// Start (or reopen) a PHP session
session_start();
// Sanitize GET variables
$_GET = sanitize($_GET);
// Sanitize POST variables
$_POST = sanitize($_POST);
// Process the delete account form
if (isset($_POST["delete_account"]) && isset($_POST["user_id"]) && isset($_SESSION["logged_in"])) {
// Check for valid permissions
if ($_SESSION["admin"] && ($_POST["user_id"] !== $_SESSION["user_id"])) {
// Delete the account
$_SERVER["mysqli"]->query("DELETE FROM users WHERE user_id = '{$_POST["user_id"]}'");
// Delete the account's posts
$_SERVER["mysqli"]->query("DELETE FROM posts WHERE user_id = '{$_POST["user_id"]}'");
exit("1");
} else {
// Delete the account
$_SERVER["mysqli"]->query("DELETE FROM users WHERE user_id = '{$_SESSION["user_id"]}'");
// Delete the account's posts
$_SERVER["mysqli"]->query("DELETE FROM posts WHERE user_id = '{$_SESSION["user_id"]}'");
// Remove session variables (log the user out)
session_unset();
exit("0");
}
}
// Process the delete post form
if (isset($_POST["delete_post"]) && isset($_POST["post_id"]) && isset($_SESSION["logged_in"]) && $_SESSION["edit"]) {
// Check for valid permissions
$result = $_SERVER["mysqli"]->query("SELECT * FROM posts WHERE post_id = '{$_POST["post_id"]}'");
$row = $result->fetch_assoc();
if (!$row || ($row["user_id"] !== $_SESSION["user_id"]))
exit("1");
// Delete the post
$_SERVER["mysqli"]->query("DELETE FROM posts WHERE post_id = '{$_POST["post_id"]}'");
exit("0");
}
// Process finish password reset form
if (isset($_POST["finish_password_reset"]) && isset($_POST["id"]) && isset($_POST["password"]) && isset($_SESSION["id"]) && isset($_SESSION["user_id"]) && isset($_SESSION["admin"])) {
// Check the verification ID
if ($_POST["id"] !== $_SESSION["id"])
exit("1");
// Generate a new password hash
$_POST["password"] = hash("sha256", $_POST["password"] . $_SESSION["user_id"]);
// Update the account
$_SERVER["mysqli"]->query("UPDATE users SET password = '{$_POST["password"]}', login_date = CURRENT_TIMESTAMP, login_ip = '{$_SERVER["REMOTE_ADDR"]}' WHERE user_id = '{$_SESSION["user_id"]}'");
// Remove temporary session variables
unset($_SESSION["id"]);
// Log the user in
$_SESSION["logged_in"] = true;
exit("0");
}
// Process the finish registration form
if (isset($_POST["finish_registration"]) && isset($_POST["id"]) && isset($_SESSION["id"]) && isset($_SESSION["user_id"]) && isset($_SESSION["username"]) && isset($_SESSION["password"]) && isset($_SESSION["email"]) && isset($_SESSION["subscribe"]) && isset($_SESSION["admin"])) {
// Check the verification ID
if ($_POST["id"] !== $_SESSION["id"])
exit("1");
// Check for username in use
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE username = '{$_SESSION["username"]}'");
$row = $result->fetch_assoc();
if ($row)
exit("2");
// Check for email in use
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE email = '{$_SESSION["email"]}'");
$row = $result->fetch_assoc();
if ($row)
exit("3");
// Create the account
$_SERVER["mysqli"]->query("INSERT INTO users SET user_id = '{$_SESSION["user_id"]}', username = '{$_SESSION["username"]}', password = '{$_SESSION["password"]}', email = '{$_SESSION["email"]}', edit = {$_SESSION["edit"]}, admin = {$_SESSION["admin"]}, subscribe = {$_SESSION["subscribe"]}, login_date = CURRENT_TIMESTAMP, login_ip = '{$_SERVER["REMOTE_ADDR"]}'");
// Remove temporary session variables
unset($_SESSION["id"]);
unset($_SESSION["password"]);
// Log the user in
$_SESSION["logged_in"] = true;
exit("0");
}
// Process the login form
if (isset($_POST["login"]) && isset($_POST["email"]) && isset($_POST["password"])) {
// Check for a valid email address
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE email = '{$_POST["email"]}'");
$row = $result->fetch_assoc();
if (!$row)
exit("1");
// Check the user's password
$_POST["password"] = hash("sha256", $_POST["password"] . $row["user_id"]);
if ($_POST["password"] !== $row["password"])
exit("1");
// Log the user in
$_SESSION["logged_in"] = true;
$_SESSION["user_id"] = $row["user_id"];
$_SESSION["username"] = $row["username"];
$_SESSION["email"] = $row["email"];
$_SESSION["edit"] = $row["edit"] ? true : false;
$_SESSION["admin"] = $row["admin"] ? true : false;
$_SESSION["subscribe"] = $row["subscribe"] ? true : false;
// Update the last login time and ip
$_SERVER["mysqli"]->query("UPDATE users SET login_date = CURRENT_TIMESTAMP, login_ip = '{$_SERVER["REMOTE_ADDR"]}' WHERE user_id = '{$_SESSION["user_id"]}'");
exit("0");
}
// Process logout requests
if (isset($_POST["logout"])) {
// Remove session variables (log the user out)
session_unset();
exit("0");
}
// Process the new post form
if (isset($_POST["new_post"]) && isset($_POST["title"]) && isset($_POST["content"]) && isset($_SESSION["logged_in"]) && $_SESSION["edit"]) {
// Create the new post
$post_id = uniqid();
$_SERVER["mysqli"]->query("INSERT INTO posts SET post_id = '{$post_id}', user_id = '{$_SESSION["user_id"]}', date_created = CURRENT_TIMESTAMP, title = '{$_POST["title"]}', content = '{$_POST["content"]}'");
// Get the post
$result = $_SERVER["mysqli"]->query("SELECT * FROM posts INNER JOIN users ON posts.user_id = users.user_id WHERE posts.post_id = '{$post_id}'");
$row = $result->fetch_assoc();
$date_created = strtotime($row["date_created"]);
// Create the new post email
$headers = "From: {$_SERVER["site"]["title"]} <do-not-reply@{$_SERVER["site"]["domain"]}>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$subject = $row["title"];
$date_created = date("F jS Y", $date_created);
// Convert BB formatting to HTML formatting
$row["content"] = bb2html($row["content"]);
$body = "<html>
<head>
<title>{$_SERVER["site"]["title"]} - {$_SERVER["site"]["description"]}</title>
</head>
<body style=\"background-color: {$_SERVER["style"]["background_color"]}; color: {$_SERVER["style"]["font_color"]}; font-family: Tahoma, Sans-serif; font-size: 0.75em; padding-top: 20px; padding-bottom: 20px; text-align: center;\">
<div style=\"background-color: {$_SERVER["style"]["foreground_color"]}; margin: auto; width: 80%; text-align: left; word-wrap: break-word;\">
<div style=\"padding: 10px;\">
<div style=\"font-size: 1.2em; font-weight: bold; margin-bottom: 2px;\">
{$row["title"]}
</div>
<div>
by <a style=\"color: {$_SERVER["style"]["link_color"]};\" href=\"{$_SERVER["site"]["url"]}#about={$_SESSION["user_id"]}\">{$row["username"]}</a> on {$date_created}
</div>
<div style=\"margin-top: 10px;\">
{$row["content"]}
</div>
</div>
</div>
</body>
</html>";
// Send the email to subscribed users
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE subscribe = 1");
while ($row = $result->fetch_assoc())
mail($row["email"], $subject, $body, $headers);
exit("0");
}
// Generate RSS feeds
if (isset($_GET["rss"])) {
// Set the content type to RSS
header("Content-Type: application/rss+xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>\n";
// Get the site publication date
$pub_date = filemtime($_SERVER["DOCUMENT_ROOT"] . $_SERVER["PHP_SELF"]);
// Get the specified feed
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE user_id = '{$_GET["rss"]}'");
$row = $result->fetch_assoc();
// Check if the feed is for a specific user
$user_specific = $row ? true : false;
echo " <title>", $_SERVER["site"]["title"], " - ", $user_specific ? "Posts by {$row["username"]}" : "All Posts", "</title>
<description>Latest Posts @ ", $_SERVER["site"]["description"], "</description>
<link>", $_SERVER["site"]["url"], "</link>
<pubDate>", date("D, d M Y H:i:s T", $pub_date), "</pubDate>\n";
// Load the most recent 5 posts
if ($user_specific)
$result = $_SERVER["mysqli"]->query("SELECT * FROM posts WHERE user_id = '{$_GET["rss"]}' ORDER BY date_created DESC LIMIT 5");
else
$result = $_SERVER["mysqli"]->query("SELECT * FROM posts ORDER BY date_created DESC LIMIT 5");
$row = $result->fetch_assoc();
// Get the last build date from the most recent post
if ($row) {
$last_build_date = strtotime($row["date"]);
if ($last_build_date < $pub_date)
$last_build_date = $pub_date;
} else
$last_build_date = $pub_date;
echo " <lastBuildDate>", date("D, d M Y H:i:s T", $last_build_date), "</lastBuildDate>
<atom:link href=\"", $_SERVER["site"]["url"], "?rss", $user_specific ? "={$_GET["rss"]}" : "", "\" rel=\"self\" type=\"application/rss+xml\"/>";
// Loop through the most recent 5 posts
do {
// Remove BB formatting
$row["content"] = bb2rss($row["content"]);
echo "\n <item>
<title>", $row["title"], "</title>
<description>", $row["content"], "</description>
<link>", $_SERVER["site"]["url"], "#search=", $row["post_id"], "&page=1</link>
<guid>", $_SERVER["site"]["url"], "#search=", $row["post_id"], "&page=1</guid>
<pubDate>", date("D, d M Y H:i:s T", strtotime($row["date_created"])), "</pubDate>
</item>";
} while ($row = $result->fetch_assoc());
echo "\n </channel>
</rss>\n";
exit;
}
// Process the start password reset form
if (isset($_POST["start_password_reset"]) && isset($_POST["email"])) {
// Check if the email address exists
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE email = '{$_POST["email"]}'");
$row = $result->fetch_assoc();
if (!$row)
exit("1");
// Set temporary session variables
$_SESSION["id"] = uniqid();
$_SESSION["user_id"] = $row["user_id"];
$_SESSION["username"] = $row["username"];
$_SESSION["email"] = $row["email"];
$_SESSION["edit"] = $row["edit"] ? true : false;
$_SESSION["admin"] = $row["admin"] ? true : false;
$_SESSION["subscribe"] = $row["subscribe"] ? true : false;
// Create password reset request email
$headers = "From: {$_SERVER["site"]["title"]} <do-not-reply@{$_SERVER["site"]["domain"]}>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$subject = "Password Reset Link";
$url = "{$_SERVER["site"]["url"]}#finish_password_reset={$_SESSION["id"]}";
$body = "<html>
<head>
<title>{$_SERVER["site"]["title"]} - {$_SERVER["site"]["description"]}</title>
</head>
<body style=\"background-color: {$_SERVER["style"]["background_color"]}; color: {$_SERVER["style"]["font_color"]}; font-family: Tahoma, Sans-serif; font-size: 0.75em; padding-top: 20px; padding-bottom: 20px; text-align: center;\">
<div style=\"background-color: {$_SERVER["style"]["foreground_color"]}; margin: auto; width: 80%; text-align: left; word-wrap: break-word;\">
<div style=\"padding: 10px;\">
<div style=\"font-size: 1.2em; font-weight: bold; margin-bottom: 2px;\">
Password Reset Link
</div>
<div>
If you did not request this email, please disregard it.
</div>
<div style=\"margin-top: 10px;\">
Your password reset link: <a style=\"color: {$_SERVER["style"]["link_color"]};\" href=\"{$url}\">{$url}</a>
</div>
</div>
</div>
</body>
</html>";
// Send the email
mail($_POST["email"], $subject, $body, $headers);
exit("0");
}
// Process the start registration form
if (isset($_POST["start_registration"]) && isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["email"]) && isset($_POST["subscribe"])) {
// Check for username in use
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE username = '{$_POST["username"]}'");
$row = $result->fetch_assoc();
if ($row)
exit("1");
// Check for invalid email address
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
exit("2");
// Check for email address in use
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE email = '{$_POST["email"]}'");
$row = $result->fetch_assoc();
if ($row)
exit("3");
// Set temporary session variables
$_SESSION["id"] = uniqid();
$_SESSION["user_id"] = uniqid();
$_SESSION["username"] = $_POST["username"];
$_SESSION["password"] = hash("sha256", $_POST["password"] . $_SESSION["user_id"]);
$_SESSION["email"] = $_POST["email"];
$_SESSION["edit"] = "0";
$_SESSION["admin"] = "0";
$_SESSION["subscribe"] = $_POST["subscribe"];
// Create registration request email
$headers = "From: {$_SERVER["site"]["title"]} <do-not-reply@{$_SERVER["site"]["domain"]}>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$subject = "Registration Link";
$url = "{$_SERVER["site"]["url"]}#finish_registration={$_SESSION["id"]}";
$body = "<html>
<head>
<title>{$_SERVER["site"]["title"]} - {$_SERVER["site"]["description"]}</title>
</head>
<body style=\"background-color: {$_SERVER["style"]["background_color"]}; color: {$_SERVER["style"]["font_color"]}; font-family: Tahoma, Sans-serif; font-size: 0.75em; padding-top: 20px; padding-bottom: 20px; text-align: center;\">
<div style=\"background-color: {$_SERVER["style"]["foreground_color"]}; margin: auto; width: 80%; text-align: left; word-wrap: break-word;\">
<div style=\"padding: 10px;\">
<div style=\"font-size: 1.2em; font-weight: bold; margin-bottom: 2px;\">
Registration Link
</div>
<div>
If you did not request this email, please disregard it.
</div>
<div style=\"margin-top: 10px;\">
Your registration link: <a style=\"color: {$_SERVER["style"]["link_color"]};\" href=\"{$url}\">{$url}</a>
</div>
</div>
</div>
</body>
</html>";
// Send the email
mail($_POST["email"], $subject, $body, $headers);
exit("0");
}
// Process the update account form
if (isset($_POST["update_account"]) && isset($_POST["user_id"]) && isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["email"]) && isset($_POST["about"]) && isset($_POST["edit"]) && isset($_POST["admin"]) && isset($_POST["subscribe"]) && isset($_SESSION["logged_in"])) {
// Check for username in use
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE username = '{$_POST["username"]}'");
$row = $result->fetch_assoc();
if ($row && ($row["user_id"] !== $_POST["user_id"]))
exit("2");
// Check for invalid email address
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
exit("3");
// Check for email address in use
$result = $_SERVER["mysqli"]->query("SELECT * FROM users WHERE email = '{$_POST["email"]}'");
$row = $result->fetch_assoc();
if ($row && ($row["user_id"] !== $_POST["user_id"]))
exit("4");
// Check for valid permissions
if ($_SESSION["admin"]) {
// Update the account
$_SERVER["mysqli"]->query("UPDATE users SET username = '{$_POST["username"]}', email = '{$_POST["email"]}', about = '{$_POST["about"]}', edit = {$_POST["edit"]}, admin = {$_POST["admin"]}, subscribe = {$_POST["subscribe"]} WHERE user_id = '{$_POST["user_id"]}'");
// Update the password if specified
if ($_POST["password"] !== "") {
// Generate a new password hash
$_POST["password"] = hash("sha256", $_POST["password"] . $_POST["user_id"]);
// Update the account
$_SERVER["mysqli"]->query("UPDATE users SET password = '{$_POST["password"]}' WHERE user_id = '{$_POST["user_id"]}'");
}
// Update session variables
$_SESSION["username"] = $_POST["username"];
$_SESSION["email"] = $_POST["email"];
$_SESSION["edit"] = $_POST["edit"] ? true : false;
$_SESSION["admin"] = $_POST["admin"] ? true : false;
$_SESSION["subscribe"] = $_POST["subscribe"] ? true : false;
exit("1");
} else {
// Update the account
$_SERVER["mysqli"]->query("UPDATE users SET username = '{$_POST["username"]}', email = '{$_POST["email"]}', about = '{$_POST["about"]}', subscribe = {$_POST["subscribe"]} WHERE user_id = '{$_SESSION["user_id"]}'");
// Update the password if specified
if ($_POST["password"] !== "") {
// Generate a new password hash
$_POST["password"] = hash("sha256", $_POST["password"] . $_SESSION["user_id"]);
// Update the account
$_SERVER["mysqli"]->query("UPDATE users SET password = '{$_POST["password"]}' WHERE user_id = '{$_SESSION["user_id"]}'");
}
// Update session variables
$_SESSION["username"] = $_POST["username"];
$_SESSION["email"] = $_POST["email"];
$_SESSION["subscribe"] = $_POST["subscribe"] ? true : false;
exit("0");
}
}
// Process the update post form
if (isset($_POST["update_post"]) && isset($_POST["post_id"]) && isset($_POST["title"]) && isset($_POST["content"]) && isset($_SESSION["logged_in"]) && $_SESSION["edit"]) {
// Check for valid permissions
$result = $_SERVER["mysqli"]->query("SELECT * FROM posts WHERE post_id = '{$_POST["post_id"]}'");
$row = $result->fetch_assoc();
if ($row && ($row["user_id"] !== $_SESSION["user_id"]))
exit("1");
// Update the post
$_SERVER["mysqli"]->query("UPDATE posts SET updated = 1, title = '{$_POST["title"]}', content = '{$_POST["content"]}' WHERE post_id = '{$_POST["post_id"]}'");
exit("0");
}
?>