This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRobloxAPI.php
356 lines (356 loc) · 16.2 KB
/
RobloxAPI.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* RobloxSDK
* @author Glebux
* @license MIT License
* @copyright Glebux©2022
*/
function headersToArray( $str ): array {
$headers = array();
$headersTmpArray = explode( "\r\n" , $str );
for ( $i = 0 ; $i < count( $headersTmpArray ) ; ++$i )
{
// we dont care about the two \r\n lines at the end of the headers
if ( strlen( $headersTmpArray[$i] ) > 0 )
{
// the headers start with HTTP status codes, which do not contain a colon so we can filter them out too
if ( strpos( $headersTmpArray[$i] , ":" ) )
{
$headerName = substr( $headersTmpArray[$i] , 0 , strpos( $headersTmpArray[$i] , ":" ) );
$headerValue = substr( $headersTmpArray[$i] , strpos( $headersTmpArray[$i] , ":" )+1 );
$headers[$headerName] = $headerValue;
}
}
}
return $headers;
}
function makerobloxrequest(string $url, bool|string $request = false, ?array $postfields = null, ?string $contenttype = null, ?string $cookie = null, ?string $token = null, ?array $headers = null, ?bool $returnheaders = false, ?bool $returncode = false){
switch ($contenttype){
case "json":
$contenttype = "application/json";
case "text":
$contenttype = "text/plain";
case "xml":
$contenttype = "text/xml";
default:
$contenttype = "application/json";
}
$post = false;
$ch = curl_init($url);
if (isset($cookie)){
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if (gettype($request) == "bool"){
$post = $request;
}
else{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request);
}
curl_setopt($ch, CURLOPT_POST, $post);
if (isset($postfields)){
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields));
}
$temphd = array(
"Content-Type: ".$contenttype,
"Referer: www.roblox.com",
"Accept: application/json"
);
if (isset($token)){
array_push($temphd, "X-CSRF-Token: ".$token);
}
if (isset($headers)){
foreach ($headers as $v){
array_push($temphd, $v);
}
}
if ($returnheaders == true) {
curl_setopt($ch, CURLOPT_HEADER, 1);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $temphd);
$resp = curl_exec($ch);
if ($returnheaders == true) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
return headersToArray($header);
}
if ($returncode == true){
return curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
}
return $resp;
}
class RobloxClient{
private string $cookie;
private string $cookieheader;
public string $userid;
public string $username;
public string $displayname;
private string $accesspassword = "";
public function __construct(string $cookie, $accesspassword = "")
{
$this->cookie = $cookie;
$this->cookieheader = ".ROBLOSECURITY=" . $this->cookie . "; Path=/; Domain=.www.roblox.com; HttpOnly; Expires=Mon, 1 Jan " . strval(intval(date("Y") + 50)) . " 00:00:00 GMT";
$data = json_decode(makerobloxrequest("https://users.roblox.com/v1/users/authenticated", false, null, null, $this->cookieheader, null, null, false), true);
if (!isset($data["id"])){
throw new ErrorException("Identification failed or invalid cookie");
}
if (isset($accesspassword)){
$this->accesspassword = $accesspassword;
}
$this->userid = $data["id"];
$this->username = $data["name"];
$this->displayname = $data["displayName"];
}
public function getcookie(string $password = ""){
if ($password == $this->accesspassword){
return $this->cookie;
}
return "Incorrect password";
}
public function getcookieheader(string $password = ""){
if ($password == $this->accesspassword){
return $this->cookieheader;
}
return "Incorrect password";
}
public function setpassword(string $oldpassword, string $newpassword){
if ($this->accesspassword == $oldpassword){
$this->accesspassword = $newpassword;
return true;
}
return "Old password is incorrect";
}
public function gettokenbypass(string $password = ""){
if ($this->accesspassword == $password){
return $this->gettoken();
}
return "Incorrect password";
}
private function gettoken(): string{
return makerobloxrequest("https://auth.roblox.com/v2/logout", true, null, null, $this->cookieheader, null, null, true)["x-csrf-token"];
}
public function getauthticketbypass(string $password = ""){
if ($this->accesspassword == $password){
return $this->getauthticket();
}
return "Incorrect password";
}
private function getauthticket(): string{
return substr(makerobloxrequest("https://auth.roblox.com/v1/authentication-ticket/", true, null, null, $this->cookieheader, $this->gettoken(), null, true)["rbx-authentication-ticket"], 1);
}
public function joinplaceid($placeid){
header('Location: roblox-player:1+launchmode:play+gameinfo:'.$this->getauthticket().'+launchtime:1605197413770+placelauncherurl:https%3A%2F%2Fassetgame.roblox.com/game/PlaceLauncher.ashx?request=RequestGame&placeId='.$placeid.'&isPlayTogetherGame=false+robloxLocale:en_us+gameLocale:en_us+channel:');
}
public function joinjobid($placeid, $jobid){
header('Location: roblox-player:1+launchmode:play+gameinfo:'.$this->getauthticket().'+launchtime:1605197413770+placelauncherurl:https%3A%2F%2Fassetgame.roblox.com/game/PlaceLauncher.ashx?request=RequestGame&placeId='.$placeid.'&gameId='.$jobid.'&isPlayTogetherGame=false+robloxLocale:en_us+gameLocale:en_us+channel:');
}
public function getfriendcount(){
return json_decode(makerobloxrequest("https://friends.roblox.com/v1/my/friends/count", false, null, null, $this->cookieheader, $this->gettoken(), null, false), true)["count"];
}
public function getfriends(){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/users/".$this->userid."/friends?userSort=Alphabetical", false, null, null, $this->cookie, $this->gettoken(), null, false), true);
$users = array();
if (!isset($data["data"])){
return null;
}
foreach ($data["data"] as $k => $v){
$users[] = new RobloxPlayer($v["id"]);
}
return $users;
}
public function getfriendrequestcount(){
return json_decode(makerobloxrequest("https://friends.roblox.com/v1/user/friend-requests/count", false, null, null, $this->cookieheader, $this->gettoken(), null, false), true)["count"];
}
public function getfriendrequests(){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/my/friends/requests?sortOrder=Desc&limit=100", false, null, null, $this->cookie, $this->gettoken(), null, false), true);
$users = array();
if (!isset($data["data"])){
return null;
}
foreach ($data["data"] as $k => $v){
$users[] = new RobloxPlayer($v["id"]);
}
return $users;
}
public function addtofriends(int $userid){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/users/".$userid."/request-friendship", true, array("friendshipRequestModel" => array("friendshipOriginSourceType" => "Unknown")), null, $this->cookieheader, $this->gettoken(), null, false), true);
if (isset($data["success"])){
return true;
}
return false;
}
public function unfried(int $userid){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/users/".$userid."/unfriend", true, null, null, $this->cookieheader, $this->gettoken(), null, false), true);
if (isset($data["success"])){
return true;
}
return false;
}
public function acceptfriendrequest(int $userid){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/users/".$userid."/accept-friend-request", true, null, null, $this->cookieheader, $this->gettoken(), null, false), true);
if (isset($data["success"])){
return true;
}
return false;
}
public function declinefriendrequest(int $userid){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/users/".$userid."/decline-friend-request", true, null, null, $this->cookieheader, $this->gettoken(), null, false), true);
if (isset($data["success"])){
return true;
}
return false;
}
public function isfriendswith(int $userid){
$data = $this->getfriends();
foreach ($data as $v){
if ($v->userid == $userid){
return true;
}
}
return false;
}
}
class RobloxPlayer{
public int $userid;
public string $username;
public string $displayname;
public ?string $description;
public string $created;
public bool $isBanned;
public function __construct(int $userid)
{
$this->userid = $userid;
$data = json_decode(makerobloxrequest("https://users.roblox.com/v1/users/".strval($userid), false, null, null, null, null, null, false), true);
$this->description = $data["description"];
$this->created = $data["created"];
$this->isBanned = $data["isBanned"];
$this->username = $data["name"];
$this->displayname = $data["displayName"];
}
public function getfriends(){
$data = json_decode(makerobloxrequest("https://friends.roblox.com/v1/users/".$this->userid."/friends?userSort=Alphabetical", false, null, null, null, null, null, false), true);
$users = array();
if (!isset($data["data"])){
return null;
}
foreach ($data["data"] as $k => $v){
$users[] = new RobloxPlayer($v["id"]);
}
return $users;
}
public function addtofriends(RobloxClient $client){
$client->addtofriends($this->userid);
}
public function unfriend(RobloxClient $client){
$client->unfried($this->userid);
}
public function acceptfriendrequest(RobloxClient $client){
$client->acceptfriendrequest($this->userid);
}
public function declinefriendrequest(RobloxClient $client){
$client->declinefriendrequest($this->userid);
}
public function isfriendswith(int $userid){
$data = $this->getfriends();
foreach ($data as $v){
if ($v->userid == $userid){
return true;
}
}
return false;
}
}
class RobloxGame{
public $gameid;
public $placeid;
public $universeid;
public $placename;
public $description;
public $creator;
public $price;
public $created;
public $updated;
public $playing;
public $visits;
public $maxplayers;
public function getuniversalid(): int{
return json_decode(makerobloxrequest("https://api.roblox.com/universes/get-universe-containing-place?placeid=".$this->gameid, false, null, null, null, null, null, false), true)["UniverseId"];
}
public function __construct(int $placeid){
$this->gameid = $placeid;
$this->universeid = $this->getuniversalid();
if (!isset($this->universeid)){
throw new ErrorException("Invalid game or cannot fetch information");
}
$data = json_decode(makerobloxrequest("https://games.roblox.com/v1/games?universeIds=".$this->getuniversalid(), false, null, null, null, null, null, null), true)["data"][0];
$this->placeid = $data["rootPlaceId"];
$this->placename = $data["name"];
$this->description = $data["description"];
$this->creator = new RobloxPlayer($data["creator"]["id"]);
switch ($data["price"]){
case null:
$this->price = 0;
default:
$this->price = $data["price"];
}
$this->created = $data["created"];
$this->updated = $data["updated"];
$this->playing = $data["playing"];
$this->visits = $data["visits"];
$this->maxplayers = $data["maxPlayers"];
}
public function getservers(?RobloxClient $client = null, ?string $password = ""){
$data = null;
if (isset($client) and isset($password)){
$data = json_decode(makerobloxrequest("https://games.roblox.com/v1/games/".$this->gameid."/servers/Public?sortOrder=Asc&limit=100", false, null, null, $client->getcookieheader($password), $client->gettokenbypass($password), null, false), true);
}
else{
$data = json_decode(makerobloxrequest("https://games.roblox.com/v1/games/".$this->gameid."/servers/Public?sortOrder=Asc&limit=100", false, null, null, null, null, null, false), true);
}
$servers = array();
foreach ($data["data"] as $v) {
$playertokens = array();
foreach ($v["playerTokens"] as $p) {
$playertokens[] = $p;
}
//print_r($players);
$ping = 0;
if (isset($v["ping"])){
$ping = $v["ping"];
}
$serverdata = array(
"jobId" => $v["id"],
"playing" => $v["playing"],
"playerTokens" => $playertokens,
"ping" => $ping,
"fps" => $v["fps"]
);
if (isset($client) and isset($password)){
foreach ($v["players"] as $p){
$serverdata["players"] = new RobloxPlayer($p["id"]);
}
}
$servers[] = $serverdata;
}
return $servers;
}
public function getplaces(){
$data = json_decode(makerobloxrequest("https://develop.roblox.com/v1/universes/".$this->universeid."/places?sortOrder=Asc&limit=100"), true);
$places = array();
foreach ($data["data"] as $v){
$places[] = new RobloxGame($v["id"]);
}
return $places;
}
public function setname(RobloxClient $client, string $password, string $name){
$request = makerobloxrequest("https://develop.roblox.com/v1/places/".$this->gameid, true, array("name" => $name, "description" => $this->description), null, $client->getcookieheader($password), $client->gettokenbypass($password), null, false, true);
if ($request == 200){return true;}
return false;
}
public function setdescription(RobloxClient $client, string $password, string $description){
$request = makerobloxrequest("https://develop.roblox.com/v1/places/".$this->gameid, true, array("name" => $this->placename, "description" => $description), null, $client->getcookieheader($password), $client->gettokenbypass($password), null, false, true);
if ($request == 200){return true;}
return false;
}
}
?>