-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
395 lines (284 loc) · 9.91 KB
/
functions.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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/SMTP.php';
require "config.php";
$conn = new mysqli(Config::SERVER_NAME, Config::USER_NAME, Config::PASSWORD, Config::DB_NAME);
function exchangeCode($length) {
$bytes = random_bytes(($length)/2);
$exchange_code = bin2hex($bytes);
return $exchange_code;
}
//send email on account creation with option to deactivate. Use deactivation code to authenticate deactivation.
function create_opt_out($email) {
global $conn;
$dcode = exchangeCode(32);
$stmt = $conn->prepare("INSERT INTO deactivate(uemail, dcode) VALUES(?,?)");
$stmt->bind_param("ss", $email, $dcode);
$stmt->execute();
$stmt->close();
send_opt_out_email($email, $dcode);
}
function get_opt_code($email) {
global $conn;
$stmt = $conn->prepare("SELECT dcode FROM deactivate WHERE uemail = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return $result;
}
function send_opt_out_email($email, $dcode) {
$url = "https://otanga.co.ke/Projects/Chat-App-PHP/";
$link = $url."opt-out.php?user=$email&code=$dcode";
$body = "<div style='background-color: #77d7c8;' > <div style='background-color: black; color: #77d7c8; text-align: center;'><h2>OurChat</h2></div> <div style='text-align: center; color: black; background-color: #77d7c8; padding: 5%;'><p>Thank you for choosing OurChat. </p><p>Visit <a href='$url'>OurChat</a> to talk to friends in real time. </p><br><br><span style='font-size: 0.85em;'>Didn't sign up? <a href='$link'>opt out</a> of OurChat.</span> </div> </div>";
$altbody = "Thank you for choosing OurChat. Visit https://otanga.co.ke/Projects/Chat-App-PHP/ to talk to friends in real time. \n\nDidn't sign up? Use the link below to opt out of OurChat. \n$link";
$subject = "WELCOME";
send_email($email, $subject, $body, $altbody);
}
function verify_opt_out($user, $code) {
global $conn;
$stmt = $conn->prepare("SELECT uemail FROM deactivate WHERE dcode = ?");
$stmt->bind_param("s", $code);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
if($result == $user) {
return true;
} else {
return false;
}
}
function create_user($email, $username, $passwordHash, $active = 1) {
global $conn;
$stmt = $conn->prepare("INSERT INTO users(uemail, username, passwordHash, uactive) VALUES(?,?,?,?)");
$stmt->bind_param("sssi", $email, $username, $passwordHash, $active);
$stmt->execute();
$stmt->close();
create_opt_out($email);
}
function create_google_user($email, $username, $active = 1) {
global $conn;
$stmt = $conn->prepare("INSERT INTO users_google(g_email, username, uactive) VALUES(?,?,?)");
$stmt->bind_param("ssi", $email, $username, $active);
$stmt->execute();
$stmt->close();
}
function check_user($user) {
global $conn;
$stmt = $conn->prepare("SELECT id FROM users WHERE uemail = ?");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return isset($result) ? true : false;
}
function check_username($username) {
global $conn;
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
if(!isset($result)) {
$stmt = $conn->prepare("SELECT id FROM users_google WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
}
return isset($result) ? true : false;
}
function check_google_user($user) {
global $conn;
$stmt = $conn->prepare("SELECT id FROM users_google WHERE g_email = ?");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return isset($result) ? true : false;
}
function check_active($user) {
global $conn;
$stmt = $conn->prepare("SELECT uactive FROM users WHERE uemail = ?");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return $result == 1 ? true : false;
}
function activate($user, $active = 1) {
global $conn;
$stmt = $conn->prepare("UPDATE users SET uactive = ? WHERE uemail = ?");
$stmt->bind_param("is", $active, $user);
$stmt->execute();
$stmt->close();
}
function check_active_user($user) {
return check_user($user) && check_active($user) ? true : false;
}
function isDeactivated($user) {
if(check_user($user)) {
if(!check_active($user)) {
return true;
}
}
return false;
}
function retrievePassword($user) {
global $conn;
$stmt = $conn->prepare("SELECT passwordHash FROM users WHERE uemail = ?");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return $result;
}
function retrieveEmail($username) {
global $conn;
$stmt = $conn->prepare("SELECT uemail FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
$len = strlen($result);
if($len < 1) {
$stmt2 = $conn->prepare("SELECT g_email FROM users_google WHERE username = ?");
$stmt2->bind_param("s", $username);
$stmt2->execute();
$stmt2->bind_result($result2);
$stmt2->fetch();
$stmt2->close();
return $result2;
}
return $result;
}
function send_email($user, $subject, $body, $altbody) {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = Config::SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = Config::SMTP_USER;
$mail->Password = Config::SMTP_PASSWORD;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->setFrom('no-reply@otanga.co.ke', 'OurChat');
$mail->addAddress($user);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->Altbody = $altbody;
$mail->send();
}
function login($user) {
$_SESSION['user'] = $user;
}
function logout() {
unset($_SESSION['user']);
}
function must_login($redirect) {
if(!(isset($_SESSION['user']))) {
header("Location: ".$redirect);
}
}
function all_users($user) {
global $conn;
$all = array();
$stmt = $conn->prepare("SELECT username FROM users WHERE NOT(uemail = ?)");
$stmt->bind_param("s", $user);
$stmt->execute();
$stmt->bind_result($result);
while($stmt->fetch()) {
array_push($all, $result);
}
$stmt2 = $conn->prepare("SELECT username FROM users_google WHERE NOT(g_email = ?)");
$stmt2->bind_param("s", $user);
$stmt2->execute();
$stmt2->bind_result($result2);
while($stmt2->fetch()) {
array_push($all, $result2);
}
return $all;
}
function search_users($str) {
global $conn;
$all = array();
$regex = "%".$str."%";
$stmt = $conn->prepare("SELECT username FROM users WHERE username LIKE ?");
$stmt->bind_param("s", $regex);
$stmt->execute();
$stmt->bind_result($result);
while($stmt->fetch()) {
array_push($all, $result);
}
$stmt2 = $conn->prepare("SELECT username FROM users_google WHERE username LIKE ?");
$stmt2->bind_param("s", $regex);
$stmt2->execute();
$stmt2->bind_result($result2);
while($stmt2->fetch()) {
array_push($all, $result2);
}
return $all;
}
function insert_chat($sender, $receiver, $encryptedMessage, $key, $iv, $tag) {
global $conn;
$stmt = $conn->prepare("INSERT INTO chat(sender, receiver, encrypted_message, passphrase, iv, tag) VALUES(?,?,?,?,?,?)");
$stmt->bind_param("ssssss", $sender, $receiver, $encryptedMessage, $key, $iv, $tag);
$stmt->execute();
$stmt->close();
}
function get_chats($user1, $user2) {
global $conn;
$cipher = "aes-128-gcm";
$stmt = $conn->prepare("SELECT sender, receiver, encrypted_message, passphrase, iv, tag, timest FROM chat WHERE sender = ? AND receiver = ? OR sender = ? AND receiver = ?");
$stmt->bind_param("ssss", $user1, $user2, $user2, $user1);
$stmt->execute();
$result = $stmt->get_result();
$array = $result->fetch_all();
foreach($array as $row) {
$sender = $row[0];
$receiver = $row[1];
$tag = hex2bin($row[5]);
$message = openssl_decrypt($row[2], $cipher, $row[3], $options=0, $row[4], $tag);
if($sender == $user1) {
echo "
<span class='rounded p-1' style='display: block; width: fit-content; margin-left: auto; margin-right: 0; background-color: #dcf8c6;'>".$message."</span><br>
";
} else {
echo "
<span class='rounded p-1' style='display: block; width: fit-content; margin-right: auto; margin-left: 0; background-color: #fff5c4;'>".$message."</span><br>
";
}
}
}
function get_latest_chat_id($sender, $receiver) {
global $conn;
$stmt = $conn->prepare("SELECT id FROM chat WHERE sender = ? AND receiver = ? ORDER BY id DESC LIMIT 1");
$stmt->bind_param("ss", $sender, $receiver);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
return $result;
}
function get_chat($id) {
global $conn;
$cipher = "aes-128-gcm";
$stmt = $conn->prepare("SELECT encrypted_message, passphrase, iv, tag FROM chat WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($enc, $key, $iv, $tag);
$stmt->fetch();
$tag = hex2bin($tag);
$message = openssl_decrypt($enc, $cipher, $key, $options=0, $iv, $tag);
return $message;
}