forked from gta5-map/Social-Club-example-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
417 lines (376 loc) · 15.5 KB
/
index.php
File metadata and controls
417 lines (376 loc) · 15.5 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/* Functions */
/**
* Function to print out possible debug
* information.
* @param {String} $msg input message
*/
function debug($msg){
global $config;
if ($config->debug) {
echo('[DEBUG] ' . $msg . PHP_EOL);
}
}
/**
* Function to print out possible trace
* information and prepend a [TRACE]
* prefix.
* @param {String} $msg input message
*/
function trace($msg){
global $config;
if ($config->trace) {
$arr = explode("\n", $msg);
foreach ($arr as $key => $value) {
$arr[$key] = '[TRACE] ' . $arr[$key];
}
$msg = implode(PHP_EOL, $arr);
echo($msg);
}
}
/**
* Function to die in case of possible errors.
* @param {String} $msg input message
*/
function error($msg){
die('[ERROR] ' . $msg . PHP_EOL);
}
/**
* Check for existing cookie jar, will return
* 'true' if the file is found.
* @return {BOOL}
*/
function checkExistingCookieJar(){
if (file_exists('cookie_jar.txt')) {
debug('[checkExistingCookieJar] Found existing cookie file.');
return true;
} else {
debug('[checkExistingCookieJar] Couldn\'t find existing cookie file.');
return false;
}
}
/**
* Check input string for possible captcha, will
* return 'false' if there was no captacha request
* detected, otherwise it'll die with an error msg.
* @param {String} $input
* @return {BOOL}
*/
function checkForCaptchaRequest($input){
if (strpos($input, "showRecaptcha: true")) {
debug('[checkForCaptchaRequest] Captcha request in $input variable detected.');
trace('[checkForCaptchaRequest] <!-- START -->');
trace('[checkForCaptchaRequest] '.$input);
trace('[checkForCaptchaRequest] <!-- STOP -->');
error('[checkForCaptchaRequest] Captcha request detected. Sign into SocialClub using a desktop browser from this machine to please ReCaptcha. Then retry using this parser.');
return true;
} else {
debug('[checkForCaptchaRequest] No captcha request detected.');
// Check for possible login errors
if (strpos($input, "gError = 'There has been an error, please retry!'")) {
debug('[checkForCaptchaRequest] Found an unexpected error in $input variable.');
trace('[checkForCaptchaRequest] <!-- START -->');
trace('[checkForCaptchaRequest] '.$input);
trace('[checkForCaptchaRequest] <!-- STOP -->');
error('[checkForCaptchaRequest] Unexpected error. Wrong credentials?');
return true;
} else {
return false;
}
}
}
/**
* Check for empty data, will return 'true'
* if empty/fake data was detected
* @param {String} $input
* @return {BOOL}
*/
function checkForEmptyData($input){
if (strpos($input, "Play Time: 0h 0m 0s")) {
debug('[checkForEmptyData] Empty/fake data detected.');
return true;
} else {
debug('[checkForEmptyData] No empty/fake data detected.');
return false;
}
}
/**
* Function to sign into SocialClub and store the authorized
* cookie in our cookie jar. Will return 'true' if the
* authentication was successful.
* @return {Bool} State of authentication
*/
function renewAuthentication() {
global $defaultHeaders, $username, $password;
/* Request to parse __RequestVerificationToken */
// Initiate curl request
debug('[renewAuthentication] Sending request to parse the login form.');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://socialclub.rockstargames.com/");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie_jar.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_HTTPHEADER,
array_merge(
$defaultHeaders
)
);
// Store buffer and unset curl variables
$buffer = curl_exec($ch);
curl_close ($ch);
unset($ch);
// Store __RequestVerificationToken
$parsed_rvt = str_get_html($buffer)->find('input[name=__RequestVerificationToken]', 0)->value;
debug('[renewAuthentication] Received RequestVerificationToken: \''.$parsed_rvt.'\'.');
/* Request to sign in and store authorization cookie */
// Initiate curl request
debug('[renewAuthentication] Sending authentication request.');
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie_jar.txt");
curl_setopt($ch, CURLOPT_URL,"https://socialclub.rockstargames.com/profile/signin");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie_jar.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie_jar.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=".$username."&password=".$password."&__RequestVerificationToken=".$parsed_rvt);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array_merge(
$defaultHeaders,
array(
'Content-Type: application/x-www-form-urlencoded'
)
)
);
// Store buffer and unset curl variables
$buffer = curl_exec ($ch);
curl_close ($ch);
unset($ch);
// Check if there is no captcha request by R*
if (!checkForCaptchaRequest($buffer)) {
debug('[renewAuthentication] Successfuly authenticated.');
return true;
}
}
/**
* Function to request to get actual informations using
* authorized session in the cookie file
* @return {String} SocialClub response
*/
function parseActualInformation(){
global $target, $defaultHeaders;
// Initiate curl request
debug('[parseActualInformation] Sending request to parse actual data.');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie_jar.txt");
curl_setopt($ch, CURLOPT_URL,"http://socialclub.rockstargames.com/games/gtav/career/overviewAjax?character=Freemode&nickname=".$target."&slot=Freemode&gamerHandle=&gamerTag=&_=".time()."000");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array_merge(
$defaultHeaders,
array(
'Accept-Encoding: gzip, deflate',
)
)
);
$buffer = curl_exec ($ch);
curl_close ($ch);
unset($ch);
return $buffer;
}
/**
* Calculate the actual percentage of a progress bar stat
* @param {String} $domData The input DOM
* @param {String} $statsType The type of stat to look for
* @return {Integer} The average value in percent
*/
function calculateProgressbarStats($domData, $statsType){
// Find <li> that contains the actual percentages of our progressbar stat
$statsDom = str_get_html($domData)->find('h5[plaintext^='.$statsType.']', 0)->next_sibling();
$percentages = 0;
// For each progress bar part (5 parts)
for ($i=0; $i < 5; $i++) {
// Parse and substitute actual value
$value = $statsDom->find('span',$i)->plaintext;
$value = str_replace('% ', '', $value);
$value = ($value) ? $value : '0';
// Add to $percentages variable
$percentages += $value;
}
// Get average by dividing through 5
return $percentages / 5;
}
/**
* Constructs the array for all recent activities
* @param {String} $domData The input DOM
* @return {Array} Constructed array including activities
*/
function getRecentActivity($domData){
// Store partial DOM
$activities = str_get_html($domData)->find('#recentActivity ul li[data-type=award]');
$array = [];
foreach($activities as $activity) {
// Parse information
$subArray = array(
'name' => (string)$activity->{'data-name'},
'type' => (string)$activity->{'data-award'},
'image' => (string)$activity->find('img', 0)->src
);
// And store in main $array
array_push($array, $subArray);
}
// Return array
return $array;
}
/* Core */
// Require SimpleHTMLDOM library
require('lib/simplehtmldom.php');
// Set default headers
$defaultHeaders = array(
'Pragma: no-cache',
'Accept-Encoding: gzip, deflate, sdch',
'Accept-Language: en-US,en;q=0.8,en-US;q=0.6,en;q=0.4',
'Upgrade-Insecure-Requests: 1',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2460.0 Safari/537.36',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control: no-cache',
'Connection: keep-alive'
);
// Check for config file
if (!file_exists('config.json')) {
error('Error: No configuration file found. Make sure to copy the default one to \'config.json\'');
} else {
$config = json_decode(file_get_contents('config.json'));
debug('Configuration file loaded.');
}
// Load credentials from config
$username = $config->username;
$password = $config->password;
// In case there is no default TZ, set one
if (!@date_default_timezone_get()) {
date_default_timezone_set("Europe/Berlin");
}
// Check for possible "target" argument
$target = (isset($argv[1])) ? $argv[1] : "";
// Authenticate if there is no cookie jar file
if (!checkExistingCookieJar()){
renewAuthentication();
}
// Try to parse data using existing cookie file
$data = parseActualInformation();
// If the string contains empty/fake data ...
if (checkForEmptyData($data)) {
// ... try to renew the authentication
if (renewAuthentication()) {
// ... and actually parse the informations again
$data = parseActualInformation();
}
}
// Check for occurance of sectionBlockedStats container
if (!empty(str_get_html($data)->find('#sectionBlockedStats'))) {
error('Error: Player/target is not sharing game stats.');
}
// Parse data and store in temporary object
$parsed = array(
'general' => array(
'rank' => (int)str_get_html($data)->find('.rankHex h3', 0)->innertext,
'xp' => (string)str_replace(' RP ', '', str_get_html($data)->find('.rankXP .clearfix .left', 0)->plaintext),
'play-time' => (string)str_replace('Play Time: ', '', str_get_html($data)->find('.rankBar h4', 0)->innertext),
'money' => array(
'cash' => (string)str_get_html($data)->find('#cash-value', 0)->innertext,
'bank' => (string)str_get_html($data)->find('#bank-value', 0)->innertext
)
),
'crew' => array(
'name' => (string)str_get_html($data)->find('.crewCard .clearfix .left h3 a', 0)->plaintext,
'tag' => (string)trim(str_get_html($data)->find('.crewCard .clearfix .left .crewTag span', 0)->plaintext),
'emblem' => (string)str_get_html($data)->find('.crewCard .clearfix .avatar', 0)->src
),
'freemode' => array(
'races' => array(
'wins' => (int)str_get_html($data)->find('p[data-name=Races]', 0)->{'data-win'},
'losses' => (int)str_get_html($data)->find('p[data-name=Races]', 0)->{'data-loss'},
'time' => (string)str_get_html($data)->find('p[data-name=Races]', 0)->{'data-extra'}
),
'deathmatches' => array(
'wins' => (int)str_get_html($data)->find('p[data-name=Deathmatches]', 0)->{'data-win'},
'losses' => (int)str_get_html($data)->find('p[data-name=Deathmatches]', 0)->{'data-loss'},
'time' => (string)str_get_html($data)->find('p[data-name=Deathmatches]', 0)->{'data-extra'}
),
'parachuting' => array(
'wins' => (int)str_get_html($data)->find('p[data-name=Parachuting]', 0)->{'data-win'},
'losses' => (int)str_get_html($data)->find('p[data-name=Parachuting]', 0)->{'data-loss'},
'perfect-landing' => (int)str_get_html($data)->find('p[data-name=Parachuting]', 0)->{'data-extra'}
),
'darts' => array(
'wins' => (int)str_get_html($data)->find('p[data-name=Darts]', 0)->{'data-win'},
'losses' => (int)str_get_html($data)->find('p[data-name=Darts]', 0)->{'data-loss'},
'six-darter' => (int)str_get_html($data)->find('p[data-name=Darts]', 0)->{'data-extra'}
),
'tennis' => array(
'wins' => (int)str_get_html($data)->find('p[data-name=Tennis]', 0)->{'data-win'},
'losses' => (int)str_get_html($data)->find('p[data-name=Tennis]', 0)->{'data-loss'},
'aces' => (int)str_get_html($data)->find('p[data-name=Tennis]', 0)->{'data-extra'}
),
'golf' => array(
'wins' => (int)str_get_html($data)->find('p[data-name=Golf]', 0)->{'data-win'},
'losses' => (int)str_get_html($data)->find('p[data-name=Golf]', 0)->{'data-loss'},
'hole-in-one' => (bool)(str_get_html($data)->find('p[data-name=Golf]', 0)->{'data-extra'} === 'No') ? false : true,
),
),
'money' => array(
'total' => array(
'spent' => (string)str_get_html($data)->find('#cashSpent p', 0)->plaintext,
'earned' => (string)str_get_html($data)->find('#cashEarned p', 0)->plaintext
),
'earnedby' => array(
'jobs' => (string)str_get_html($data)->find('.cash-val[data-name=Jobs]', 0)->{'data-cash'},
'shared' => (string)str_get_html($data)->find('.cash-val[data-name=Shared]', 0)->{'data-cash'},
'betting' => (string)str_get_html($data)->find('.cash-val[data-name=Betting]', 0)->{'data-cash'},
'car-sales' => (string)str_get_html($data)->find('.cash-val[data-name=Car Sales]', 0)->{'data-cash'},
'picked-up' => (string)str_get_html($data)->find('.cash-val[data-name=Picked Up]', 0)->{'data-cash'},
'other' => (string)str_get_html($data)->find('.cash-val[data-name=Other]', 0)->{'data-cash'}
)
),
'stats' => array(
'stamina' => (int)calculateProgressbarStats($data, 'Stamina'),
'stealth' => (int)calculateProgressbarStats($data, 'Stealth'),
'lung-capacity' => (int)calculateProgressbarStats($data, 'Lung Capacity'),
'flying' => (int)calculateProgressbarStats($data, 'Flying'),
'shooting' => (int)calculateProgressbarStats($data, 'Shooting'),
'strength' => (int)calculateProgressbarStats($data, 'Strength'),
'driving' => (int)calculateProgressbarStats($data, 'Driving'),
'mental-state' => (int)calculateProgressbarStats($data, 'Mental State')
),
'criminalrecord' => array(
'cops-killed' => (string)str_get_html($data)->find('h5[plaintext^=Cops killed]', 0)->next_sibling()->plaintext,
'wanted-stars' => (string)str_get_html($data)->find('h5[plaintext^=Wanted stars attained]', 0)->next_sibling()->plaintext,
'time-wanted' => (string)str_get_html($data)->find('h5[plaintext^=Time Wanted]', 0)->next_sibling()->plaintext,
'stolen-vehicles' => (string)str_get_html($data)->find('h5[plaintext^=Vehicles Stolen]', 0)->next_sibling()->plaintext,
'cars-exported' => (string)str_get_html($data)->find('h5[plaintext^=Cars Exported]', 0)->next_sibling()->plaintext,
'store-holdups' => (string)str_get_html($data)->find('h5[plaintext^=Store Hold Ups]', 0)->next_sibling()->plaintext
),
'favourite-weapon' => array(
'name' => (string)str_get_html($data)->find('#faveWeaponWrapper .imageHolder h4', 0)->plaintext,
'image' => (string)str_get_html($data)->find('#faveWeaponWrapper .imageHolder img', 0)->src,
'stats' => array(
'damage' => (int)str_replace('% ', '', str_get_html($data)->find('.weaponStats tr span', 0)->plaintext),
'fire-rate' => (int)str_replace('% ', '', str_get_html($data)->find('.weaponStats tr span', 1)->plaintext),
'accuracy' => (int)str_replace('% ', '', str_get_html($data)->find('.weaponStats tr span', 2)->plaintext),
'range' => (int)str_replace('% ', '', str_get_html($data)->find('.weaponStats tr span', 3)->plaintext),
'clip-size' => (int)str_replace('% ', '', str_get_html($data)->find('.weaponStats tr span', 4)->plaintext)
),
'kills' => (int)str_get_html($data)->find('h5[plaintext^=Kills]', 0)->next_sibling()->plaintext,
'headshots' => (int)str_get_html($data)->find('h5[plaintext^=Headshots]', 0)->next_sibling()->plaintext,
'accuracy' => (float)str_replace('%', '', str_get_html($data)->find('h5[plaintext^=Accuracy]', 0)->next_sibling()->plaintext),
'time-held' => (string)str_get_html($data)->find('h5[plaintext^=Time held]', 0)->next_sibling()->plaintext
),
'recent-activity' => getRecentActivity($data)
);
// Encode data object as JSON and print out
echo json_encode($parsed, JSON_PRETTY_PRINT);
// Exit without errors
exit(0);