-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
72 lines (59 loc) · 2.09 KB
/
user.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
<?php
// Load environment variables from config.php
include 'config.php';
// Initialize username and name variables
$username = '';
$name = '';
$id = '';
// Check if token exists in cookie
if (isset($_COOKIE['token'])) {
$mkact = $_COOKIE['token'];
// POST request to retrieve username and name
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://vocaloid.social/api/i');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array("i" => $mkact)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$res = curl_exec($curl);
// Check for errors and save to error.txt if any
if ($res === false) {
$error = curl_error($curl);
file_put_contents('error.txt', $error);
$username = 'Error retrieving username.';
} else {
// Decode response
$arr = json_decode($res, true);
// Check if username and name are retrieved successfully
if (isset($arr['username'])) {
$username = $arr['username'];
if (isset($arr['name'])) {
$name = $arr['name'];
}
if (isset($arr['id'])) {
$id = $arr['id'];
}
// Check if user is moderator or admin
if (isset($arr['isModerator']) && $arr['isModerator'] === true) {
$freelynetwork = true;
} elseif (isset($arr['isAdmin']) && $arr['isAdmin'] === true) {
$freelynetwork = true;
} else {
$freelynetwork = false;
}
} else {
$username = 'Error retrieving username.'; // Default error message
}
}
curl_close($curl);
} else {
// Token does not exist in cookie
$username = 'not login'; // Set username to 'not login'
}
// Save username and name for later use
$_SESSION['username'] = $username;
$_SESSION['name'] = $name;
$_SESSION['id'] = $id;
// Save $freelynetwork for later use
$_SESSION['freelynetwork'] = $freelynetwork ?? false;
?>