-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.php
116 lines (100 loc) · 3.41 KB
/
util.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
<?php
function hashify($val) {
return hash('sha256', PEPPER.":$val");
}
function authenticate($val, $expectedHash, $description) {
$hash = hashify($val);
if (!isset($_REQUEST['auth'])) {
die('Error: Missing parameter in URL!');
}
if ($hash !== $expectedHash) {
die("Error: $description validation failed! Did you tamper with the URL?");
}
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "PHPMailer/src/Exception.php";
require_once "PHPMailer/src/PHPMailer.php";
require_once "PHPMailer/src/SMTP.php";
function sendEmail($recipient, $subject, $content) {
$mail = new PHPMailer(true);
try {
// Setup Mailer
$mail->isSMTP();
$mail->Host = 'outgoing.mit.edu';
$mail->SMTPAuth = false;
$mail->Port = 25;
// Add Details
$mail->setFrom('[email protected]', 'Wide Tim');
$mail->addAddress($recipient);
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $content;
// $mail->AltBody = $content;
// Send It
$mail->send();
return true;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
return false;
}
}
function sendVerificationEmail($email) {
$email_content = "Hello!\n\nTo verify your email address, please click on the following link:\n\n$baseurl$_SERVER[REQUEST_URI]&emailauth=" . hashify($email) . "\n\nBest,\nWide Tim";
return sendEmail($email, 'Verify your email for 2027 Discord', $email_content);
}
function getRecordByEmail($connection, $email) {
$stmt = mysqli_prepare($connection, "SELECT * FROM users2027 where email=?");
$stmt->bind_param("s", $email);
$stmt->execute();
return $stmt->get_result();
}
function getPropertyByEmail($connection, $email, $property) {
$result = getRecordByEmail($connection, $email);
if (!$result) {
die("email $email not found in the database!");
}
$result = $result->fetch_array();
return $result[$property];
}
function isAdmit($connection, $email) {
return getRecordByEmail($connection, $email)->num_rows > 0;
}
function getName($connection, $email) {
return getPropertyByEmail($connection, $email, 'name');
}
function hasDiscordAccount($connection, $email) {
return getPropertyByEmail($connection, $email, 'discord');
}
function updateRecord($connection, $email, $name, $discord) {
$now = time();
$stmt = mysqli_prepare($connection, "UPDATE users2027 SET discord=?, name=?, timestamp=? WHERE email=?");
$stmt->bind_param("ssis", $discord, $name, $now, $email);
if (!$stmt->execute()) {
die("query failed! please report to [email protected] or DM TO CONTACT STAFF");
}
}
function redirect($url) {
header("Location: $url");
die();
}
/// Code to make POST requests, used for OpenID/OAuth
/// Reference: https://www.php.net/manual/en/context.http.php
function post($url, $args) {
$postdata = http_build_query($args);
$opts = array('http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
));
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
/// Polyfill
/// https://www.php.net/manual/en/function.str-contains.php
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
?>