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