-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletedRush.php
More file actions
90 lines (78 loc) · 2.99 KB
/
completedRush.php
File metadata and controls
90 lines (78 loc) · 2.99 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
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
<?php
function getid($token){
$url = "https://discord.com/api/users/@me";
$headers = array('Content-Type: application/x-www-form-urlencoded', 'Authorization: Bearer ' . $token);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);
$id = $results['id'];
return $id;
}
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the raw JSON data from the request body
$jsonData = file_get_contents('php://input');
// Check if the JSON data is not empty
if (!empty($jsonData)) {
// Decode the JSON data into a PHP associative array
$data2 = json_decode($jsonData, true);
if ($data2 === null) {
// JSON parsing failed
http_response_code(400); // Bad Request
echo json_encode(["error" => "Invalid JSON data"]);
} else {
$token = $data2['access_token'];
$id = getid($token);
if (isset($id)) {
// Read the JSON data from the file
$jsonString = file_get_contents('data.json');
// Decode the JSON data into a PHP associative array
$data = json_decode($jsonString, true);
// Get the 'userid' from the URL
$userId = $id;
// Check if the 'userid' exists in the JSON data
if (isset($data[$userId])) {
// Output the JSON data for the specified 'userid'
// 'userid' not found in the JSON data, create a new entry
if($data2['score']>$data[$userId]['rush_score']){
$score = $data2['score'];
$time = time();
}else{
$score = $data[$userId]['rush_score'];
$time = $data[$userId]['rush_time'];
}
$newEntry = [
"userid" => $data[$userId]['userid'],
"history" => $data[$userId]['history'],
"points" => $data[$userId]['points'],
"username" => $data[$userId]['username'],
"avatar" => $data[$userId]['avatar'],
"time" => $data[$userId]['time'], // Current time in milliseconds
"supporter" => $data[$userId]['supporter'],
"rush_score" => $score,
"rush_time" => $time,
"rush_access" => $data[$userId]['rush_access']
];
// Add the new entry to the data array
$data[$userId] = $newEntry;
// Save the updated JSON data back to the file
file_put_contents('data.json', json_encode($data));
echo "saved";
}
}
}
} else {
// JSON data is empty
http_response_code(400); // Bad Request
echo json_encode(["error" => "Empty JSON data"]);
}
} else {
// Request method is not POST
http_response_code(405); // Method Not Allowed
echo json_encode(["error" => "Method not allowed"]);
}
?>