This repository was archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost.php
62 lines (49 loc) · 1.52 KB
/
post.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
<?php
header("X-Frame-Options: DENY");
header("Content-Security-Policy: frame-ancestors 'none'", false);
header("Content-Type: application/json; charset=UTF-8");
session_start();
function scrub($input) {
$splitted = explode('<', $input);
$splitted = implode('<', $splitted);
$splitted = explode('>', $splitted);
$splitted = implode('>', $splitted);
return $splitted;
}
// Process the data as JSON if using POST -- otherwise it should do something else, I haven't decided yet
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['type'] == "nojs") {
$data['name'] = $_POST['name'];
$data['message'] = $_POST['message'];
header('Location: nojs.php#message');
} else {
$data = json_decode(file_get_contents("php://input"), false);
print_r($data);
}
}
// Cast the object to an array
$data = (array) $data;
date_default_timezone_set("UTC");
$chat = fopen('chat.txt', 'a') or die('Error upon opening file');
$name = 'anonymous';
if ($_POST['name'] !== '') {
$name = $data['name'];
}
// Scrub user input
$message = scrub($data['message']);
$name = scrub($name);
$name = explode(' ', $name);
$name = implode('_', $name);
$_SESSION['name'] = $name;
// Add metadata to message
$text = '<name>' . $name . '</name>' . '<message>' . $message . '</message> <date>' . date("Y-m-d") . 'T' . date("H:i:s") . "</date>\n";
if ($message !== '') {
$status = 'Success!';
fwrite($chat, $text);
}
else {
$status = 'Error. No message';
}
echo $status;
fclose($chat);
?>