This repository was archived by the owner on Apr 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuAPI.php
More file actions
297 lines (270 loc) · 11.1 KB
/
uAPI.php
File metadata and controls
297 lines (270 loc) · 11.1 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
<?php
/**
* @author Kacper Serewis (k4czp3r.dev@gmail.com)
* @copyright 2017
* @version 2.0.1.2
* github.com/K4CZP3R
* Updated at 01-Mar-2017
*/
class ubiapi{
private $b64authcreds;
public $http_useragent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
public $http_encoding="gzip";
public $debug=true;
/**
* @param string $location - From which function is this message executed
* @param string $content - Content of debug message
* @param string $color - Background Color of message
*/
public function debugReport($location,$content,$color){
$color_array = array("red"=>"E57373","green"=>"4DB6AC","blue"=>"4FC3F7","grey"=>"E0E0E0");
if($this->debug){
print '<span style="background:#'.$color_array[$color].';text-align:center;">'.date("h:i:s").' - ['.$location.' ] - '.$content.'</span><br>';
}
}
/**
* @param string $emailandpassword - email:password
* @return string - email:password in b64
*/
public function generateB64Creds($emailandpassword){
$this->debugReport(__FUNCTION__,"B64: <hidden> uncomment line below to see this","grey");
//$this->debugReport(__FUNCTION__,"B64: ".base64_encode($emailandpassword),"grey");
return base64_encode($emailandpassword);
}
/**
* ubiapi constructor.
* You can use email and password OR already encoded b64 string
* If you gonna use encoded string, enter 'null' in other places
* @param string $credsemail Uplay Account Email
* @param string $credspass Uplay Account Password
* @param string $credsb64 B64 Encoded string (email:password)
*/
function __construct($credsemail,$credspass,$credsb64){
if($credspass == null && $credsb64 != null){
$this->debugReport(__FUNCTION__,"Using b64 string to login","green");
$this->b64authcreds=$credsb64;
}
else{
$this->debugReport(__FUNCTION__,"Using creds to login","green");
$this->b64authcreds=$this->generateB64Creds($credsemail.":".$credspass);
}
}
/**
* If you gonna use this API on website, it is smart to use this.
* Ubisoft is going to temp ban your account if you gonna login every time you refresh page.
* And this function logins only when its needed (see examples on my github.com/K4CZP3R
*
* @param string $searchWith How to search (bynick,byid)
* @param string $userToCheck What to search (id,nick)
* @return array error,content
*/
public function refreshTicket($searchWith,$userToCheck){
$this->debugReport("begin ".__FUNCTION__,"Checking if ubi gonna return normal answer","green");
$firstResponse = $this->searchUser($searchWith,$userToCheck);
if($firstResponse["error"]){
$this->debugReport(__FUNCTION__,"strike1, ubi returned null. Trying to refresh ticket","red");
$this->login();
$this->debugReport(__FUNCTION__,"Checking if after logging in, ubi will return normal answer","grey");
$secondResponse=$this->searchUser($searchWith,$userToCheck);
if($secondResponse["error"]){
$this->debugReport(__FUNCTION__,"strike2, ubi returned null","red");
return array("error"=>true,
"content"=>"After logging in still empty response!");
}
else{
$this->debugReport(__FUNCTION__,"Success! Ubi returned normal answer!","green");
return array("error"=>false,
"content"=>"Ticket has been refreshed");
}
}
else{
$this->debugReport(__FUNCTION__,"Ticket is still up-to-date","green");
return array("error"=>false,
"content"=>"No action has been taken, ticket up-to-date");
}
}
/**
* @param string|int $mode 1-bynick, 2-byid
* @param string $content id or nick you want to search
* @return array raw-raw answer from ubi, json-array formated json, nick- nickname, uid- user id
*/
public function searchUser($mode,$content){
$prefixUrl = "https://api-ubiservices.ubi.com/v2/profiles?";
if($mode == 1 || $mode == "bynick"){
$request_url = $prefixUrl."nameOnPlatform=".$content."&platformType=uplay";
}
if($mode == 2 || $mode == "byid"){
$request_url = $prefixUrl."profileId=".$content;
}
$this->debugReport(__FUNCTION__,"Request URL: ".$request_url,"grey");
$request_header_ubiappid = "314d4fef-e568-454a-ae06-43e3bece12a6";
$request_header_ubisessionid = "a651a618-bead-4732-b929-4a9488a21d27";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers =[
"Accept: */*",
"ubi-appid: ".$request_header_ubiappid,
"ubi-sessionid: ".$request_header_ubisessionid,
"authorization: ".$this->uplayticket(false),
"Referer: https://club.ubisoft.com/en-US/friends",
"Accept-Language: en-US",
"Origin: https://club.ubisoft.com",
"Accept-Encoding: ".$this->http_encoding,
"User-Agent: ".$this->http_useragent,
"Host: api-ubiservices.ubi.com",
"Cache-Control: no-cache"];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ubioutput = curl_exec($ch);
curl_close($ch);
$orginaloutput=$ubioutput;
if($this->http_encoding == "gzip"){
$ubioutput = gzdecode($ubioutput);
}
$this->debugReport(__FUNCTION__,"Executed request (to see it, uncomment log line)","blue");
$this->debugReport(__FUNCTION__,$ubioutput,"grey");
//idk why but sometimes gzdecoded returns null
if(empty($ubioutput)){
$this->debugReport(__FUNCTION__,"After making use of ".$this->http_encoding. "decode, string is empty, using orginal one...","red");
$ubioutput=$orginaloutput;
if(empty($ubioutput)){
return array("error"=>true,
"content"=>"Ubi Response is empty!");
}
}
$jsonoutput = json_decode($ubioutput,true);
return array("error"=>false,
"raw"=>$ubioutput,
"json"=>$jsonoutput,
"nick"=>$jsonoutput['profiles'][0]['nameOnPlatform'],
"uid"=>$jsonoutput['profiles'][0]['profileId']);
}
/**
* @return array raw-raw answer from ubi, json-array formated json
*/
public function getFriends(){
$this->debugReport(__FUNCTION__,"Going to return friends array/json","green");
$request_url = "https://api-ubiservices.ubi.com/v2/profiles/me/friends?locale=en-US";
$request_header_ubiappid = "314d4fef-e568-454a-ae06-43e3bece12a6";
$request_header_ubisessionid= "a651a618-bead-4732-b929-4a9488a21d27";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL,$request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers=[
"Accept: */*",
"ubi-appid: ".$request_header_ubiappid,
"ubi-sessionid: ".$request_header_ubisessionid,
"authorization: ".$this->uplayticket(false),
"Referer: https://club.ubisoft.com/en-US/friends",
"Accept-Language: en-US",
"Origin: https://club.ubisoft.com",
"Accept-Encoding: ".$this->http_encoding,
"User-Agent: ".$this->http_useragent,
"Host: api-ubiservices.ubi.com",
"Cache-Control: no-cache"];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ubioutput=curl_exec($ch);
curl_close($ch);
//after testing i dont see reason to include gzdecoding here, if you want to, do it
$this->debugReport(__FUNCTION__,"Request Executed (to see it, uncomment next line)","blue");
//$this->debugReport(__FUNCTION__,$ubioutput,"grey");
if(empty($ubioutput)){
return array("error"=>true,
"content"=>"Ubisoft response is empty!");
}
return array("error"=>false,
"raw"=>$ubioutput,
"json"=>json_decode($ubioutput,true));
}
/**
* @param int $showraw outputs (1) raw response or (2) raw decoded response
* @return array returns true when ticket updated (or not)
* todo: catch errors
*/
public function login($showraw){
$this->debugReport(__FUNCTION__,"Going to login...","green");
$request_url = "https://connect.ubi.com/ubiservices/v2/profiles/sessions";
$request_header_ubiappid="314d4fef-e568-454a-ae06-43e3bece12a6";
$request_header_authbasic=$this->b64authcreds;
$this->debugReport(__FUNCTION__,"<br>url:".$request_url."<br>appid:".$request_header_ubiappid."<br>authbasic:".$request_header_authbasic,"grey");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"rememberMe":true}');
$headers = [
"Content-Type: application/json; charset=utf-8",
"Accept: */*",
"Ubi-AppId: ".$request_header_ubiappid,
"Ubi-RequestedPlatformType: uplay",
"Authorization: Basic ".$request_header_authbasic,
"X-Requested-With: XMLHttpRequest",
"Referer: https://connect.ubi.com/Default/Login?appId=".$request_header_ubiappid."&lang=en-US&nextUrl=https%3A%2F%2Fclub.ubisoft.com%2Flogged-in.html%3Flocale%3Den-US",
"Accept-Language: en-US",
"Accept-Encoding: ".$this->http_encoding,
"User-Agent: ".$this->http_useragent,
"Host: connect.ubi.com",
"Content-Lenght: 19", //change this when you are changing CURLOPT_POSTFIELDS!!!!
"Cache-Control: no-cache",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ubioutput = curl_exec($ch);
$orginaloutput=$ubioutput;
curl_close($ch);
if($this->http_encoding == "gzip"){
$ubioutput = gzdecode($ubioutput);
}
if($showraw == 1){
$this->debugReport(__FUNCTION__,"Raw response (not decoded):<br>".$orginaloutput."<br>","blue");
}
if($showraw == 2){
$this->debugReport(__FUNCTION__,"Raw Response (decoded):<br>".$ubioutput."<br>","blue");
}
$this->debugReport(__FUNCTION__,"Executed request (to see it, uncomment log line)","blue");
//$this->debugReport(__FUNCTION__,__FUNCTION__,$ubioutput,"grey");
//idk why but sometimes gzdecoded returns null todo: check it
if(empty($ubioutput)){
$this->debugReport(__FUNCTION__,"After making use of ".$this->http_encoding. "decode, string is empty, using orginal one...","red");
$ubioutput=$orginaloutput;}
$json = json_decode($ubioutput,true);
$this->debugReport(__FUNCTION__,"Your authstring (to see it, uncomment next line of code)","green");
//$this->debugReport($json['ticket']);
$this->debugReport(__FUNCTION__,"Welcome, ".$json['username'],"green");
$this->debugReport(__FUNCTION__,"Your UserId is ".$json['userId'],"green");
$this->debugReport(__FUNCTION__,"You can ignore last function for saving authstring but you'll need to edit some lines of codes to disable it","grey");
$test_beforeSave=$this->uplayticket(false);
$this->uplayticket(true,$json['ticket']);
$test_afterSave=$this->uplayticket(false);
$test_fileUpdated=false;
if($test_beforeSave != $test_afterSave){
$test_fileUpdated=true;
}
return array("error"=>false,
"content"=>"Ticket Updated? (1==true):".$test_fileUpdated);
}
/**
* @param bool $save when false, returns ticket otherwise saves ticket
* @param string $ticket when $save is true, this will be ticket to save
* @return string Ticket
*/
public function uplayticket($save,$ticket=""){
if($save){
$this->debugReport(__FUNCTION__,"Saving ticket","green");
$file_ticket = fopen("api_ticket","w") or die("Can't open ticket file");
try{
fwrite($file_ticket, $ticket);
return true;
}
catch(Exception $e){
return false;
}
}
else{
$this->debugReport(__FUNCTION__,"Returning Ticket","green");
$prefix = "Ubi_v1 t=";
$ticket_file = fopen("api_ticket","r") or die("Can't open ticket file");
$ticket = fgets($ticket_file);
return $prefix.$ticket;
}
}
}