1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class AbstractRequest |
|
|
|
|
4
|
|
|
{ |
|
|
|
|
5
|
|
|
const CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26'; |
6
|
|
|
const APIURL = 'https://api.go-tellm.com/api'; |
|
|
|
|
7
|
|
|
|
8
|
|
|
const SECRET = 'hyTBJcvtpDLSgGUWjybbYUNKSSoVvMcfdjtjiQvf'; |
|
|
|
|
9
|
|
|
const USERAGENT = 'Jodel/4.47.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )'; |
|
|
|
|
10
|
|
|
const CLIENT_TYPE = 'android_4.47.0'; |
11
|
|
|
|
12
|
|
|
private $accessToken = null; |
13
|
|
|
private $payLoad; |
14
|
|
|
public $expects = ''; |
|
|
|
|
15
|
|
|
public $version = 'v2'; |
|
|
|
|
16
|
|
|
public $hasPayload = FALSE; |
17
|
|
|
|
18
|
|
|
public function execute() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$result = new \stdClass(); |
21
|
|
|
|
22
|
|
|
$this->payLoad = $this->getPayload(); |
23
|
|
|
$device_uid = ''; |
|
|
|
|
24
|
|
|
if(isset($this->payLoad['device_uid'])) { |
25
|
|
|
$device_uid = $this->payLoad['device_uid']; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->payLoad = json_encode($this->payLoad); |
30
|
|
|
$header = $this->getSignHeaders(); |
|
|
|
|
31
|
|
|
$url = $this->getFullUrl(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
if ($this->getAccessToken()) { |
34
|
|
|
$header['Authorization'] = "Bearer " . $this->getAccessToken(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
//Comment out to debug the Request: |
37
|
|
|
|
38
|
|
|
/* |
|
|
|
|
39
|
|
|
printf("URL: "); |
40
|
|
|
var_dump($url); |
41
|
|
|
echo "<br />"; |
42
|
|
|
printf("Header: "); |
43
|
|
|
var_dump($header); |
44
|
|
|
echo "<br />"; |
45
|
|
|
printf("Payload: "); |
46
|
|
|
var_dump($this->payLoad); |
47
|
|
|
echo "<br />"; |
48
|
|
|
*/ |
49
|
|
|
/* |
|
|
|
|
50
|
|
|
$options = array( |
51
|
|
|
'timeout' => 100, |
52
|
|
|
'connect_timeout' => 100, |
53
|
|
|
'proxy' => '186.103.169.165:8080', |
54
|
|
|
);*/ |
55
|
|
|
|
56
|
|
|
switch ($this->getMethod()) { |
57
|
|
|
case 'POST': |
58
|
|
|
$result = Requests::post($url, $header, $this->payLoad); |
|
|
|
|
59
|
|
|
break; |
60
|
|
|
case 'GET': |
61
|
|
|
if($this->hasPayload) |
62
|
|
|
{ |
63
|
|
|
$result = Requests::get($url, $header, $this->payLoad); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
else |
66
|
|
|
{ |
67
|
|
|
$result = Requests::get($url, $header); |
68
|
|
|
} |
69
|
|
|
break; |
70
|
|
|
case 'PUT': |
71
|
|
|
$result = Requests::put($url, $header, $this->payLoad); |
|
|
|
|
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
http_response_code($result->status_code); |
76
|
|
|
|
77
|
|
|
switch ($result->status_code) { |
78
|
|
|
case 200: |
79
|
|
|
$result = json_decode($result->body, true); |
80
|
|
|
break; |
81
|
|
|
case 204: |
82
|
|
|
$result = 'Success'; |
83
|
|
|
http_response_code(200); |
84
|
|
|
break; |
85
|
|
View Code Duplication |
case 400: |
|
|
|
|
86
|
|
|
$result = json_decode($result->body, true); |
87
|
|
|
error_log('Error 400 - ' . print_r($result, true)); |
88
|
|
|
break; |
89
|
|
View Code Duplication |
case 401: |
|
|
|
|
90
|
|
|
|
91
|
|
|
if($result == "Unauthorized") |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
error_log("Error 401: Unauthorized"); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
else |
96
|
|
|
{ |
97
|
|
|
$result = json_decode($result->body, true); |
98
|
|
|
|
99
|
|
|
if(is_array($result) && $result['error'] == 'length') |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
else |
104
|
|
|
{ |
105
|
|
|
error_log('Error 401 - ' . print_r($result, true)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
break; |
111
|
|
View Code Duplication |
case 404: |
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
error_log('Error 404 - ' . print_r($result, true)); |
115
|
|
|
$result = json_decode($result->body, true); |
116
|
|
|
|
117
|
|
|
if(array_key_exists('error', $result) && $result['error'] == 'post_blocked') |
118
|
|
|
{ |
119
|
|
|
header('HTTP/1.0 404 Not Found'); |
120
|
|
|
include('error-pages/404.php'); |
121
|
|
|
exit(); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
break; |
125
|
|
View Code Duplication |
case 477: |
|
|
|
|
126
|
|
|
$result = json_decode($result->body, true); |
127
|
|
|
error_log('Error 477 - ' . print_r($result, true)); |
128
|
|
|
break; |
129
|
|
View Code Duplication |
case 429: |
|
|
|
|
130
|
|
|
error_log('Error 429 - Too Many Requests' . print_r(json_decode($result->body, true), true)); |
131
|
|
|
exit("Error 429: Too Many Requests"); |
|
|
|
|
132
|
|
|
break; |
|
|
|
|
133
|
|
|
case 403: |
134
|
|
|
error_log('Error 403 - Access denied:' . print_r(json_decode($result->body, true), true)); |
135
|
|
|
$result = json_decode($result->body, true); |
136
|
|
|
break; |
137
|
|
|
case 502: |
138
|
|
|
error_log('Error 502 - ' . print_r($result, true)); |
139
|
|
|
$result = json_decode($result->body, true); |
140
|
|
|
header('location:'.$_SERVER['PHP_SELF']); |
141
|
|
|
break; |
142
|
|
View Code Duplication |
case 503: |
|
|
|
|
143
|
|
|
error_log('Error 503 - ' . print_r($result, true)); |
144
|
|
|
$result = json_decode($result->body, true); |
145
|
|
|
|
146
|
|
|
if(array_key_exists('error', $result) && $result['error'] == 'Service Unavailable') |
147
|
|
|
{ |
148
|
|
|
header('location:'.$_SERVER['PHP_SELF']); |
149
|
|
|
} |
150
|
|
|
break; |
151
|
|
|
default: |
152
|
|
|
error_log('Error '.$result->status_code.' - unknown error'); |
153
|
|
|
$result = json_decode($result->body, true); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
//important for account refresh |
157
|
|
|
if($device_uid != '') |
158
|
|
|
{ |
159
|
|
|
$result[0] = $result; |
160
|
|
|
$result[1] = $device_uid; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/* |
|
|
|
|
165
|
|
|
var_dump($result); |
166
|
|
|
*/ |
167
|
|
|
|
168
|
|
|
return $result; |
169
|
|
|
} |
170
|
|
|
abstract function getPayload(); |
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Gets Sign headers |
173
|
|
|
* @return array headers |
|
|
|
|
174
|
|
|
*/ |
175
|
|
|
private function getSignHeaders() |
176
|
|
|
{ |
177
|
|
|
if($this->getAccessToken() == null) { |
178
|
|
|
$payload_accessToken = ""; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
else { |
181
|
|
|
$payload_accessToken = $this->getAccessToken(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
$headers = array( |
|
|
|
|
186
|
|
|
"Connection" => "keep-alive", |
|
|
|
|
187
|
|
|
"Accept-Encoding" => "gzip", |
|
|
|
|
188
|
|
|
"Content-Type" => "application/json; charset=UTF-8", |
|
|
|
|
189
|
|
|
"User-Agent" => self::USERAGENT |
|
|
|
|
190
|
|
|
); |
191
|
|
|
$timestamp = new DateTime(); |
|
|
|
|
192
|
|
|
$timestamp = $timestamp->format(DateTime::ATOM); |
|
|
|
|
193
|
|
|
$timestamp = substr($timestamp, 0, -6); |
|
|
|
|
194
|
|
|
$timestamp .= "Z"; |
|
|
|
|
195
|
|
|
$urlParts = parse_url($this->getFullUrl()); |
|
|
|
|
196
|
|
|
$url2 = ""; |
|
|
|
|
197
|
|
|
$req = [$this->getMethod(), |
|
|
|
|
198
|
|
|
$urlParts['host'], |
199
|
|
|
"443", |
|
|
|
|
200
|
|
|
$urlParts['path'], |
201
|
|
|
$payload_accessToken, |
202
|
|
|
$timestamp, |
203
|
|
|
$url2, |
204
|
|
|
$this->payLoad]; |
205
|
|
|
$reqString = implode("%", $req); |
|
|
|
|
206
|
|
|
$secret = self::SECRET; |
|
|
|
|
207
|
|
|
$signature = hash_hmac('sha1', $reqString, $secret); |
|
|
|
|
208
|
|
|
$signature = strtoupper($signature); |
|
|
|
|
209
|
|
|
$headers['X-Authorization'] = 'HMAC ' . $signature; |
210
|
|
|
$headers['X-Client-Type'] = self::CLIENT_TYPE; |
|
|
|
|
211
|
|
|
$headers['X-Timestamp'] = $timestamp; |
|
|
|
|
212
|
|
|
$headers['X-Api-Version'] = '0.2'; |
|
|
|
|
213
|
|
|
return $headers; |
214
|
|
|
} |
215
|
|
|
private function getFullUrl() |
216
|
|
|
{ |
217
|
|
|
return self::APIURL . $this->getApiEndPoint(); |
218
|
|
|
} |
219
|
|
|
abstract function getApiEndPoint(); |
|
|
|
|
220
|
|
|
abstract function getMethod(); |
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
private function getAccessToken() |
225
|
|
|
{ |
226
|
|
|
return $this->accessToken; |
227
|
|
|
} |
228
|
|
|
/** |
229
|
|
|
* @param string $accessToken |
230
|
|
|
*/ |
231
|
|
|
public function setAccessToken($accessToken) |
232
|
|
|
{ |
233
|
|
|
$this->accessToken = $accessToken; |
234
|
|
|
} |
235
|
|
|
} |
|
|
|
|
236
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.