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
|
|
|
const SECRET = 'plerFToqEdWlzShdZlTywaCHRuzlKIMsNmOJVDGE'; |
|
|
|
|
8
|
|
|
const USERAGENT = 'Jodel/4.31.1 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )'; |
|
|
|
|
9
|
|
|
const CLIENT_TYPE = 'android_4.31.1'; |
10
|
|
|
|
11
|
|
|
private $accessToken = null; |
12
|
|
|
private $payLoad; |
13
|
|
|
public $expects = ""; |
|
|
|
|
14
|
|
|
public $version = 'v2'; |
15
|
|
|
|
16
|
|
|
public function execute() |
17
|
|
|
{ |
18
|
|
|
$result = new \stdClass(); |
19
|
|
|
|
20
|
|
|
$this->payLoad = $this->getPayload(); |
21
|
|
|
$device_uid = ""; |
|
|
|
|
22
|
|
|
if(isset($this->payLoad["device_uid"])) { |
|
|
|
|
23
|
|
|
$device_uid = $this->payLoad["device_uid"]; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->payLoad = json_encode($this->payLoad); |
28
|
|
|
$header = $this->getSignHeaders(); |
|
|
|
|
29
|
|
|
$url = $this->getFullUrl(); |
|
|
|
|
30
|
|
|
|
31
|
|
|
if ($this->getAccessToken()) { |
32
|
|
|
$header['Authorization'] = "Bearer " . $this->getAccessToken(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
//Comment out to debug the Request: |
35
|
|
|
|
36
|
|
|
/* |
|
|
|
|
37
|
|
|
var_dump($url); |
38
|
|
|
var_dump($header); |
39
|
|
|
var_dump($this->payLoad); |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
switch ($this->getMethod()) { |
44
|
|
|
case 'POST': |
45
|
|
|
$result = Requests::post($url, $header, $this->payLoad); |
|
|
|
|
46
|
|
|
break; |
47
|
|
|
case 'GET': |
48
|
|
|
if($this->version == 'v3') |
49
|
|
|
{ |
50
|
|
|
$result = Requests::get($url, $header); |
51
|
|
|
} |
52
|
|
|
else |
53
|
|
|
{ |
54
|
|
|
$result = Requests::get($url, $header); |
55
|
|
|
} |
56
|
|
|
break; |
57
|
|
|
case 'PUT': |
58
|
|
|
$result = Requests::put($url, $header, $this->payLoad); |
|
|
|
|
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
switch ($result->status_code) { |
62
|
|
|
case 200: |
63
|
|
|
$result = json_decode($result->body, true); |
64
|
|
|
break; |
65
|
|
|
case 204: |
66
|
|
|
$result = "Success"; |
|
|
|
|
67
|
|
|
break; |
68
|
|
|
case 401: |
69
|
|
|
//throw new \Exception('Unauthorized'); |
|
|
|
|
70
|
|
|
break; |
71
|
|
|
case 404: |
72
|
|
|
//echo "Es wurde bereits gevoted"; |
73
|
|
|
case 477: |
74
|
|
|
//echo "Es wurde bereits gevoted"; |
75
|
|
|
//throw new \Exception('Signing failed!'); |
|
|
|
|
76
|
|
|
break; |
77
|
|
|
case 429: |
78
|
|
|
exit("Error 429: Too Many Requests"); |
|
|
|
|
79
|
|
|
break; |
|
|
|
|
80
|
|
|
case 403: |
81
|
|
|
exit("Error 403: Access denied"); |
|
|
|
|
82
|
|
|
break; |
|
|
|
|
83
|
|
|
default: |
84
|
|
|
throw new \Exception('Unknown Error: '.$result->status_code); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if($device_uid != "") |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$result[0] = $result; |
90
|
|
|
$result[1] = $device_uid; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/*var_dump($result);*/ |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
abstract function getPayload(); |
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets Sign headers |
102
|
|
|
* @return array headers |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
private function getSignHeaders() |
105
|
|
|
{ |
106
|
|
|
if($this->getAccessToken() == null) { |
107
|
|
|
$payload_accessToken = ""; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
else { |
110
|
|
|
$payload_accessToken = $this->getAccessToken(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
$headers = array( |
|
|
|
|
115
|
|
|
"Connection" => "keep-alive", |
|
|
|
|
116
|
|
|
"Accept-Encoding" => "gzip", |
|
|
|
|
117
|
|
|
"Content-Type" => "application/json; charset=UTF-8", |
|
|
|
|
118
|
|
|
"User-Agent" => self::USERAGENT |
|
|
|
|
119
|
|
|
); |
120
|
|
|
$timestamp = new DateTime(); |
|
|
|
|
121
|
|
|
$timestamp = $timestamp->format(DateTime::ATOM); |
|
|
|
|
122
|
|
|
$timestamp = substr($timestamp, 0, -6); |
|
|
|
|
123
|
|
|
$timestamp .= "Z"; |
|
|
|
|
124
|
|
|
$urlParts = parse_url($this->getFullUrl()); |
|
|
|
|
125
|
|
|
$url2 = ""; |
|
|
|
|
126
|
|
|
$req = [$this->getMethod(), |
|
|
|
|
127
|
|
|
$urlParts['host'], |
128
|
|
|
"443", |
|
|
|
|
129
|
|
|
$urlParts['path'], |
130
|
|
|
$payload_accessToken, |
131
|
|
|
$timestamp, |
132
|
|
|
$url2, |
133
|
|
|
$this->payLoad]; |
134
|
|
|
$reqString = implode("%", $req); |
|
|
|
|
135
|
|
|
$secret = self::SECRET; |
|
|
|
|
136
|
|
|
$signature = hash_hmac('sha1', $reqString, $secret); |
|
|
|
|
137
|
|
|
$signature = strtoupper($signature); |
|
|
|
|
138
|
|
|
$headers['X-Authorization'] = 'HMAC ' . $signature; |
139
|
|
|
$headers['X-Client-Type'] = self::CLIENT_TYPE; |
|
|
|
|
140
|
|
|
$headers['X-Timestamp'] = $timestamp; |
|
|
|
|
141
|
|
|
$headers['X-Api-Version'] = '0.2'; |
|
|
|
|
142
|
|
|
return $headers; |
143
|
|
|
} |
144
|
|
|
private function getFullUrl() |
145
|
|
|
{ |
146
|
|
|
return self::APIURL . $this->getApiEndPoint(); |
147
|
|
|
} |
148
|
|
|
abstract function getApiEndPoint(); |
|
|
|
|
149
|
|
|
abstract function getMethod(); |
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
private function getAccessToken() |
154
|
|
|
{ |
155
|
|
|
return $this->accessToken; |
156
|
|
|
} |
157
|
|
|
/** |
158
|
|
|
* @param string $accessToken |
159
|
|
|
*/ |
160
|
|
|
public function setAccessToken($accessToken) |
161
|
|
|
{ |
162
|
|
|
$this->accessToken = $accessToken; |
163
|
|
|
} |
164
|
|
|
} |
|
|
|
|
165
|
|
|
|
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.