1
|
|
|
<?php |
2
|
|
|
date_default_timezone_set('UTC'); |
3
|
|
|
if (isset($_POST['submit'])) { |
4
|
|
|
if (isset($_POST['message']) && (!empty($_POST['message']) || !empty($_FILES['file']['name']))) { |
5
|
|
|
|
6
|
|
|
//file upload (if there is a file) |
7
|
|
|
$file_name_for_db = ''; |
8
|
|
|
if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != "") { |
9
|
|
|
$file_name_for_db = uploadFile($max_file_size, $last_msg); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
if(is_null($file_name_for_db)){ |
13
|
|
|
$jsondata = messageToJSON($file_name_for_db, $messages, $last_msg); |
14
|
|
|
file_put_contents($file, $jsondata); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if(!isset($_SESSION['response']) || (!$_SESSION['response'] && is_bool($_SESSION['response']))){ |
18
|
|
|
$_SESSION['response'] = true; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
} else { |
22
|
|
|
$_SESSION['response'] = "The message field is <b>required</b>."; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function greenText($msg){ |
27
|
|
|
preg_match_all('/(?<!>)>(?!>).*/', $msg, $matches, PREG_SET_ORDER, 0); |
28
|
|
|
|
29
|
|
|
foreach ($matches as $m) { |
30
|
|
|
$m[0] = str_replace('\n', '', str_replace("\r", "", $m[0])); |
31
|
|
|
$msg = str_replace($m[0], '<span style="color:#0E0; display:inline;">' . $m[0] . '</span>', $msg); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $msg; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function postLink($msg) { |
38
|
|
|
preg_match_all('/>>\d.\d*/', $msg, $matches, PREG_SET_ORDER, 0); |
39
|
|
|
|
40
|
|
|
foreach ($matches as $m) { |
41
|
|
|
$m[0] = str_replace('\n', '', str_replace("\r", "", $m[0])); |
42
|
|
|
$msg = str_replace($m[0], '<a href="#' . str_replace(">>", "", $m[0]) . '" class="postLink" style="color:#55F;">' . $m[0] . '</a>', $msg); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $msg; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function is_file_okay($max_file_size) { |
49
|
|
|
$accepted_mimes = array("video/webm", "video/mp4", "image/png", "image/jpeg", "image/gif"); |
50
|
|
|
|
51
|
|
|
$file = $_FILES["file"]; |
52
|
|
|
$file_mime = mime_content_type($file); |
53
|
|
|
return in_array($file_mime, $accepted_mimes) && $file["size"] <= $max_file_size; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function get_ext() { |
57
|
|
|
$ext = ""; |
58
|
|
|
|
59
|
|
|
$file_mime = mime_content_type($_FILES["file"]); |
60
|
|
|
switch($file_mime) { |
61
|
|
|
case "video/webm": |
62
|
|
|
$ext = "webm"; |
63
|
|
|
break; |
64
|
|
|
case "video/mp4": |
65
|
|
|
$ext = "mp4"; |
66
|
|
|
break; |
67
|
|
|
case "image/png": |
68
|
|
|
$ext = "png"; |
69
|
|
|
break; |
70
|
|
|
case "image/jpeg": |
71
|
|
|
$ext = "jpg"; |
72
|
|
|
break; |
73
|
|
|
case "image/gif": |
74
|
|
|
$ext = "gif"; |
75
|
|
|
break; |
76
|
|
|
default: |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $ext; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function uploadFile($max_file_size, $last_msg){ |
84
|
|
|
$target_dir = "images/"; |
85
|
|
|
$target_file_for_base_name = explode(".", $_FILES["file"]["name"]); //sorry, this is horrid, I know. |
86
|
|
|
$target_file_for_base_name = $last_msg + 1 . "." . $target_file_for_base_name[get_ext()]; |
87
|
|
|
$target_file = $target_dir . basename($target_file_for_base_name); |
88
|
|
|
|
89
|
|
|
if(is_file_okay($max_file_size)) { |
90
|
|
|
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file); |
91
|
|
|
return $target_file_for_base_name; |
92
|
|
|
} else { |
93
|
|
|
$_SESSION['response'] = "what are you doing anon? that file was not an image or a webm..."; |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
function messageToJSON($file_name_for_db, $messages, $last_msg){ |
99
|
|
|
$new_message = array( |
100
|
|
|
'id' => $last_msg + 1, |
101
|
|
|
'message' => nl2br(greenText(postLink(str_replace(">", ">", str_replace("<", "<", $_POST['message'])))), false), |
102
|
|
|
'file' => $file_name_for_db, |
103
|
|
|
'date' => date("Y/m/d") . " at " . date("h:i:s A") . " UTC", |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
array_push($messages["messages"], $new_message); |
107
|
|
|
$jsondata = json_encode($messages, JSON_PRETTY_PRINT); |
108
|
|
|
return $jsondata; |
109
|
|
|
} |
110
|
|
|
|