-
Notifications
You must be signed in to change notification settings - Fork 2
/
rockstarsocialclubapi.php
282 lines (233 loc) · 10.8 KB
/
rockstarsocialclubapi.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
<?php
/*
This class allows:
- to check if the user [SocialClubID] owns a copy of GTA:V on any platform
- send messages to users
Script authors:
- Gamer_Z / grasmanek94 (https://github.com/grasmanek94/)
- eider (https://github.com/eider-i128/)
Requires: http://sourceforge.net/projects/simplehtmldom/
*/
require('simplehtmldom.php');
class RockStarSocialClubAPI
{
private $_username;
private $_password;
private $_connection;
//make sure no-one can access this file(!) [eg with .htaccess]
//http://stackoverflow.com/questions/11728976/how-to-deny-access-to-a-file-in-htaccess
private $_cookiefile;
private $_static_curl_options;
private $_ssl_curl_problem_solving;
public function __construct()
{
$this->_cookiefile = realpath(dirname(__FILE__)) . '/cookies.txt';
$this->_static_curl_options = array
(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'gzip',
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 5,
CURLOPT_MAXREDIRS => 3,
CURLOPT_COOKIEJAR => $this->_cookiefile,
CURLOPT_COOKIEFILE => $this->_cookiefile,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
);
$this->_ssl_curl_problem_solving = array
(
//keep this enabled, for more information check: https://www.openssl.org/~bodo/ssl-poodle.pdf
CURLOPT_SSLVERSION => 1, //CURL_SSLVERSION_TLSv1
CURLOPT_SSL_CIPHER_LIST => 'TLSv1'
);
$this->_connection = curl_init();
}
public function __destruct()
{
curl_close($this->_connection);
}
//uuse this function and download this file http://curl.haxx.se/ca/cacert.pem (its bundle of CA Root Certificates) if you have problems with verifying ssl cert
public function EnableCAINFO()
{
$this->_ssl_curl_problem_solving[CURLOPT_CAINFO] = realpath(dirname(__FILE__)) . '/cacert.pem';
}
//if you have problems even after using EnableCAINFO(), use this function (will make your script vulnerable to MITM attacks):
public function DisableVerification()
{
$this->_ssl_curl_problem_solving = $this->_ssl_curl_problem_solving + array
(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0
);
}
private function GetRequestData($index, $url)
{
curl_reset($this->_connection);
curl_setopt_array($this->_connection, (array(
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: gzip, deflate',
'Connection: keep-alive',
'Cache-Control: max-age=0')
) + $this->_static_curl_options + (($url[4] == 's' || $url[4] == 'S') ? $this->_ssl_curl_problem_solving : array())));
$homepage = curl_exec($this->_connection);
$html = str_get_html($homepage);
if(strlen($homepage) > 0 && $html !== false)
{
$req = $html->find('input[name=__RequestVerificationToken]', $index);
if($req !== null)
{
return array(0 => $req->value, 1 => $homepage);
}
}
return array(0 => "", 1 => $homepage);
}
public function SetCredentials($username, $password)
{
$this->_username = $username;
$this->_password = $password;
}
//see function for return values, 0 means success
public function Login()
{
@unlink($this->_cookiefile);
$RequestVerificationToken = $this->GetRequestData(0, 'https://socialclub.rockstargames.com/profile/signin/')[0];
if($RequestVerificationToken == "")
{
return 3;//login failed because cannot download the Rockstar Signin page (SSL error? R* down?)
}
$data = json_encode(array("login" => $this->_username, "password" => $this->_password, "rememberme" => TRUE));
curl_reset($this->_connection);
curl_setopt_array($this->_connection, (array(
CURLOPT_URL => 'https://socialclub.rockstargames.com/profile/signincompact',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array('Accept-Encoding: gzip, deflate', 'Content-Type: application/json; charset=UTF-8','Content-Length:' . strlen($data), 'RequestVerificationToken: ' . $RequestVerificationToken)
) + $this->_static_curl_options + $this->_ssl_curl_problem_solving));
curl_exec($this->_connection);
$retData = curl_exec($this->_connection);
$http_code = curl_getinfo($this->_connection, CURLINFO_HTTP_CODE);
$retJSON = json_decode($retData);
if($http_code == 200)
{
if(isset($retJSON->result) && $retJSON->result == true)
{
return 0;//login OK
}
return 1;//login failed because credentials do not work
}
return 2;//login failed because, well.. because. (probably invalid cookie or invalid requestverificationtoken)
}
//returns true if logged in, false if not or some error occured
public function AlreadyLoggedIn()
{
$modtime = @filemtime($this->_cookiefile);
$timenow = time();
$diff = $timenow - $modtime;
if ($diff > 5*60)
{
@unlink($this->_cookiefile);
}
curl_reset($this->_connection);
curl_setopt_array($this->_connection, (array(
CURLOPT_URL => 'http://socialclub.rockstargames.com',
CURLOPT_HTTPHEADER => array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: gzip, deflate',
'Connection: keep-alive',
'Cache-Control: max-age=0')
) + $this->_static_curl_options));
curl_exec($this->_connection);
$http_code = curl_getinfo($this->_connection, CURLINFO_HTTP_CODE);
return $http_code == 302;
}
public function UserGTAStatus($SocialClubID, $maxretries, $retries = 0)
{
curl_reset($this->_connection);
curl_setopt_array($this->_connection, (array(
CURLOPT_URL => 'http://socialclub.rockstargames.com/games/gtav/api/mp/gun/0/minigun?nickname=' . $SocialClubID,
CURLOPT_HTTPHEADER => array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: gzip, deflate',
'Connection: keep-alive',
'Cache-Control: max-age=0')
) + $this->_static_curl_options));
$page = curl_exec ($this->_connection);
$http_code = curl_getinfo($this->_connection, CURLINFO_HTTP_CODE);
if ($http_code == 200)
{
if(strlen($page) > 0)
{
return 0;//user has GTA:V on any platform
}
else
{
return 1;//user has no GTA or privacy settings do not allow viewing this information
}
}
else if($http_code == 302)
{
return 2;//user does not exist
}
else if($retries < $maxretries)
{
return $this->UserGTAStatus($SocialClubID, $maxretries, $retries+1);
}
else
{
return 3;//max retries reached
}
}
function SendMessage($SocialClubID, $message, $maxretries, $retries = 0)
{
$reqData = $this->GetRequestData(0, 'http://socialclub.rockstargames.com/member/'. $SocialClubID .'/');
$RequestVerificationToken = $reqData[0];
if($RequestVerificationToken == "")
{
return 4;//sendmessage failed because cannot download the Rockstar Signin page (SSL error? R* down?)
}
if((strpos($reqData[1], $SocialClubID) !== false))
{
curl_reset($this->_connection);
$data = json_encode(array("nickname" => $SocialClubID, "__RequestVerificationToken" => $RequestVerificationToken, "message" => $message));
curl_setopt_array($this->_connection, (array(
CURLOPT_URL => 'http://socialclub.rockstargames.com/Message/AddMessage',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array('Accept-Encoding: gzip, deflate', 'Content-Type: application/json; charset=UTF-8','Content-Length:' . strlen($data), 'RequestVerificationToken: ' . $RequestVerificationToken)
) + $this->_static_curl_options));
$retData = curl_exec($this->_connection);
$http_code = curl_getinfo($this->_connection, CURLINFO_HTTP_CODE);
$retJSON = json_decode($retData);
if($http_code == 200)
{
if(isset($retJSON->Status) && $retJSON->Status == true)
{
return 0;//message send
}
else
{
return 1;//message send failed, unkown error
}
}
else if($retries < $maxretries)
{
return $this->SendMessage($SocialClubID, $message, $maxretries, $retries+1);
}
else
{
return 2;//message send failed, too many retries
}
}
else
{
return 3;//error user not found
}
}
}
?>