-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.php
180 lines (170 loc) · 6.04 KB
/
API.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
<?php
session_start();
$pdo = new PDO('mysql:host=localhost;dbname=chatDB;charset=utf8', 'root', '');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function isLoggedIn()
{
if (isset($_SESSION['username']) && isset($_SESSION['lastSeen']) && isset($_SESSION['uid'])) {
return true;
} else {
return false;
}
}
function UpdateLastSeen()
{
if (isLoggedIn()) {
$dateTime = date('Y-m-d H:i:s');
$uid = $_SESSION['uid'];
$sql = "UPDATE users SET lastSeen = :lastSeen WHERE id = :uid";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->bindParam(':lastSeen', $dateTime);
$stmt->bindParam(':uid', $uid);
$stmt->execute();
return true;
}
return false;
}
function GetOnlineUsers()
{
if (isLoggedIn()) {
// get users who lastSeen > 5 minutes ago
$dateTime = date('Y-m-d H:i:s', strtotime('-5 minutes'));
$sql = "SELECT username,lastSeen FROM users WHERE lastSeen > :lastSeen";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->bindParam(':lastSeen', $dateTime);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $users;
}
return "You are not logged in";
}
function Login($user, $pass)
{
if (preg_match('/^[a-zA-Z0-9]{1,8}$/', $user)) {
if (strlen($pass) > 6 && strlen($pass) < 25) {
$pass = strval(md5($pass));
// check if username exists
$sql = "SELECT * FROM users WHERE username = :user";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->execute(array(':user' => $user));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$dateTime = date('Y-m-d H:i:s');
if ($result) {
if ($result['password'] == $pass) {
$sql = "UPDATE users SET lastSeen = :lastSeen WHERE username = :user";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->bindParam(':lastSeen', $dateTime);
$stmt->bindParam(':user', $user);
$stmt->execute();
} else {
return "Wrong password";
}
} else {
// register new user
$sql = "INSERT INTO users (username, password, lastSeen) VALUES (:user, :pass, :lastSeen)";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->bindParam(':user', $user);
$stmt->bindParam(':pass', $pass);
$stmt->bindParam(':lastSeen', $dateTime);
$stmt->execute();
}
// save usr info in session
$stmt = $GLOBALS['pdo']->prepare("SELECT * FROM users WHERE username = :user");
$stmt->execute(array(':user' => $user));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
$_SESSION['username'] = $user;
$_SESSION['lastSeen'] = $dateTime;
$_SESSION['uid'] = $result['id'];
}
return "success";
} else {
return "Password must be between 6 and 25 characters";
}
}
return "Invalid Username";
}
function SendMessage($msg)
{
// check if user is logged in
if (isLoggedIn()) {
UpdateLastSeen();
$dateTime = date('Y-m-d H:i:s');
$uid = $_SESSION['uid'];
$sql = "INSERT INTO messages (message, userId, time) VALUES (:msg, :uid, :time)";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->bindParam(':msg', $msg);
$stmt->bindParam(':uid', $uid);
$stmt->bindParam(':time', $dateTime);
$stmt->execute();
} else {
return "You are not logged in";
}
}
function GetMessages()
{
if (isLoggedIn()) {
UpdateLastSeen();
$sql = "SELECT * FROM messages ORDER BY time DESC";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result) >= 600) {
// delete all messages except last *SomeNumber
$sql = "DELETE FROM messages WHERE id NOT IN (SELECT id FROM (SELECT id FROM messages ORDER BY time DESC LIMIT 150) AS t)";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->execute();
}
$sql = "SELECT username,id FROM users WHERE id IN (SELECT userId FROM messages)";
$stmt = $GLOBALS['pdo']->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
$users = array_column($users, 'username', 'id');
$messages = [];
foreach ($result as $message) {
$messages[] = [
'message' => $message['message'],
'username' => @$users[@$message['userId']],
'time' => $message['time']
];
}
return $messages;
} else {
return "You are not logged in";
}
}
function CustomizeDate($date, $lsf = false)
{
$date_ = date_create($date);
$now = date_create();
$diff = date_diff($date_, $now);
if ($diff->y > 0) {
return $diff->y . " Y ago";
} else if ($diff->m > 0) {
return $diff->m . " Mo ago";
} else if ($diff->d > 0) {
return $diff->d . " D ago";
} else if ($diff->h > 0) {
return $diff->h . " Hr ago";
} else if ($diff->i > 0) {
return $diff->i . " Min ago";
} else {
if ($lsf == true) {
return "";
}
return "Just Now";
}
}
function HandleText($txt)
{
$images = [];
$vid = [];
// handle XSS attacks
$txt = htmlspecialchars($txt);
// check if there is a link in the text make it clickable
$txt = preg_replace('#(http|https|ftp|ftps)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i', '<a href="$1://$2$3" title="$1://$2$3" target="_blank">Link</a>', $txt);
// enable emoji support
$txt = preg_replace('/:([a-zA-Z0-9_]+):/s', '<img src="emoji/$1.png" alt="$1" />', $txt);
return ["txt" => $txt, "img" => $images, "vid" => $vid];
}