-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
60 lines (50 loc) · 1.73 KB
/
api.php
File metadata and controls
60 lines (50 loc) · 1.73 KB
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
<?php
require("smf_2_api.php");
if ($_REQUEST['login'] == 1) {
$token = $_REQUEST['token'];
$signature = $_REQUEST['signature'];
if ($signature != base64url_encode(hash_hmac("sha512", $token, base64_decode(file_get_contents("forum.key"))))) {
echo "Bad token\n"; exit;
}
$data = json_decode(base64url_decode($token), true);
if (!$data['nlt'] || $data['nlt'] < time()) {
echo "Expired token\n"; exit;
}
smfapi_login($data['cid']);
header("Location: " . $data['return']);
} elseif ($_REQUEST['logout'] == 1) {
smfapi_logout();
header("Location: " . $_REQUEST['return']);
} elseif ($_REQUEST['register'] == 1) {
$data = $_REQUEST['data'];
$signature = $_REQUEST['signature'];
if ($signature != base64url_encode(hash_hmac("sha512", $data, base64_decode(file_get_contents("forum.key"))))) {
echo "Bad data\n"; exit;
}
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = [];
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < 32; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
$pass = implode($pass);
$data = json_decode(base64url_decode($data), true);
$data['password'] = $pass;
$data['password_check'] = $pass;
$r = smfapi_registerMember($data);
if (is_array($r) || $r == false) {
echo "Failed to create new user: " . base64_encode(serialize($r)) . ", " . base64_encode(serialize($data));
exit;
}
echo "OK"; exit;
} else {
echo "Unknown request.\n";
}
function base64url_decode($data) {
return base64_decode(strtr($data, '-_', '+/'));
}
function base64url_encode($data, $use_padding = false) {
$encoded = strtr(base64_encode($data), '+/', '-_');
return true === $use_padding ? $encoded : rtrim($encoded, '=');
}