-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
92 lines (84 loc) · 2.8 KB
/
server.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
<!DOCTYPE html>
<?php
$creds = json_decode(file_get_contents("creds.json"));
/**
* @return string
*/
function verify(): string {
global $creds;
if (!(isset($_GET["state"]) && isset($_GET["code"]))) return "Invalid request 😢";
try {
preg_match("/\w{16}/", $_GET["state"], $state);
preg_match("/[a-zA-Z0-9._-]+/", $_GET["code"], $code);
if (!$state || !$code) {
http_response_code(400);
return "Invalid request<br>Pls no hak me 😢";
}
$state = $state[0];
$code = $code[0];
try {
$db = $creds->db;
@$connection = new mysqli($db->host, $db->user, $db->password, $db->db);
if ($connection->connect_errno) throw new Exception("Error: $connection->connect_errno: $connection->connect_error");
} catch (Throwable $e) {
http_response_code(500);
return "Server Error!<br>I lost my database ¯\_(ツ)_/¯";
}
$statement = $connection->prepare("update oauth set authorization_code = ? where state = ?;");
$statement->bind_param("ss", $code, $state);
$statement->execute();
if ($statement->affected_rows < 1) {
http_response_code(400);
return "Invalid or expired code 🙁";
}
try {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "localhost", 8888);
socket_write($socket, $state, strlen($state));
// socket_set_timeout($socket, 1);
// socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ["sec" => 1, "usec" => 0]);
echo "<" . socket_read($socket, 10) . ">";
if (!$response = socket_read($socket, 1)) throw new Exception("Error: Could not read from socket");
socket_close($socket);
if ($response === "x") throw new Exception("Error: Did not verify");
} catch (Throwable $e) {
http_response_code(500);
return "Could not process request 😬";
}
http_response_code(200);
return "Verified 👍";
} catch (Exception $e) {
http_response_code(400);
return "Invalid request 😢";
}
}
?>
<html lang="en">
<head>
<title>DSU Verification</title>
<style>
body {
background: #004165;
}
.center {
position: absolute;
left: 50%;
top: 30%;
transform: translate(-50%, -50%);
text-align: center;
color: #ffffff;
}
.center > * {
margin: 0;
}
#main {
background: #ADAFAF;
border-radius: 10px;
padding: 15px;
}
</style>
</head>
<body>
<div class="center" id="main"><h1><?php print(verify()); ?></h1></div>
</body>
</html>