1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class GatewayEndpoint |
4
|
|
|
* |
5
|
|
|
* @filesource GatewayEndpoint.php |
6
|
|
|
* @created 02.04.2016 |
7
|
|
|
* @package chillerlan\Threema |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2016 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Threema; |
14
|
|
|
|
15
|
|
|
use chillerlan\Threema\Crypto\CryptoInterface; |
16
|
|
|
use chillerlan\TinyCurl\{ |
17
|
|
|
Request, |
18
|
|
|
RequestOptions, |
19
|
|
|
Response\Response, |
20
|
|
|
URL |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
class GatewayEndpoint implements GatewayInterface{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \chillerlan\TinyCurl\Request |
30
|
|
|
*/ |
31
|
|
|
protected $request; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \chillerlan\Threema\GatewayOptions |
35
|
|
|
*/ |
36
|
|
|
protected $gatewayOptions; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \chillerlan\Threema\Crypto\CryptoInterface |
40
|
|
|
*/ |
41
|
|
|
protected $cryptoInterface; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* GatewayEndpoint constructor. |
45
|
|
|
* |
46
|
|
|
* @param \chillerlan\Threema\Crypto\CryptoInterface $cryptoInterface |
47
|
|
|
* @param \chillerlan\Threema\GatewayOptions $gatewayOptions |
48
|
|
|
*/ |
49
|
|
|
public function __construct(CryptoInterface $cryptoInterface, GatewayOptions $gatewayOptions){ |
50
|
|
|
$this->cryptoInterface = $cryptoInterface; |
51
|
|
|
$this->gatewayOptions = $gatewayOptions; |
52
|
|
|
|
53
|
|
|
$requestOptions = new RequestOptions; |
54
|
|
|
$requestOptions->ca_info = $this->gatewayOptions->cacert; |
55
|
|
|
$this->request = new Request($requestOptions); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $endpoint |
60
|
|
|
* @param array $params |
61
|
|
|
* @param array $body |
62
|
|
|
* |
63
|
|
|
* @return \chillerlan\TinyCurl\Response\Response |
64
|
|
|
* @throws \chillerlan\Threema\GatewayException |
65
|
|
|
* @throws \chillerlan\TinyCurl\RequestException |
66
|
|
|
*/ |
67
|
|
|
protected function getResponse(string $endpoint, array $params = [], array $body = []):Response{ |
68
|
|
|
$endpoint = self::API_BASE.$endpoint; |
69
|
|
|
$params = array_merge($params, [ |
70
|
|
|
'from' => getenv('THREEMA_GATEWAY_ID'), |
71
|
|
|
'secret' => getenv('THREEMA_GATEWAY_SECRET') |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$url = !empty($body) |
75
|
|
|
? new URL($endpoint, $params, 'POST', $body) |
76
|
|
|
: new URL($endpoint, $params); |
77
|
|
|
|
78
|
|
|
$response = $this->request->fetch($url); |
79
|
|
|
|
80
|
|
|
if($response->info->http_code === 200){ |
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
elseif(array_key_exists($response->info->http_code, self::API_ERRORS)){ |
84
|
|
|
throw new GatewayException('gateway error: '.self::API_ERRORS[$response->info->http_code]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new GatewayException('unknown error: "compiles on my machine."'); // @codeCoverageIgnore |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function checkCredits():int{ |
94
|
|
|
return intval($this->getResponse('/credits')->body->content); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function checkCapabilities(string $threemaID):array{ |
101
|
|
|
$response = $this->getResponse('/capabilities/'.$this->checkThreemaID($threemaID))->body->content; |
102
|
|
|
$response = !empty($response) ? explode(',', $response) : []; |
103
|
|
|
|
104
|
|
|
sort($response); |
105
|
|
|
|
106
|
|
|
return $response; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
|
|
public function getIdByPhone(string $phoneno):string{ |
113
|
|
|
return $this->getResponse('/lookup/phone/'.$this->checkPhoneNo($phoneno))->body->content; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
|
|
public function getIdByPhoneHash(string $phonenoHash):string{ |
120
|
|
|
return $this->getResponse('/lookup/phone_hash/'.$this->checkHash($phonenoHash))->body->content; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public function getIdByEmail(string $email):string{ |
127
|
|
|
return $this->getResponse('/lookup/email/'.$this->checkEmail($email))->body->content; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
|
|
public function getIdByEmailHash(string $emailHash):string{ |
134
|
|
|
return $this->getResponse('/lookup/email_hash/'.$this->checkHash($emailHash))->body->content; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function getPublicKey(string $threemaID):string{ |
141
|
|
|
return $this->checkHash($this->getResponse('/pubkeys/'.$this->checkThreemaID($threemaID))->body->content); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
|
|
public function sendSimple(string $to, string $message):string{ |
148
|
|
|
// TODO: Implement sendSimple() method. |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritdoc |
153
|
|
|
*/ |
154
|
|
|
public function sendE2E(string $threemaID, string $box, string $nonce):string{ |
155
|
|
|
// TODO: Implement sendE2E() method. |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritdoc |
160
|
|
|
*/ |
161
|
|
|
public function upload(string $blob):string{ |
162
|
|
|
// TODO: Implement upload() method. |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
168
|
|
|
public function download(string $blobID){ |
169
|
|
|
// TODO: Implement download() method. |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $threemaID |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
* @throws \chillerlan\Threema\GatewayException |
177
|
|
|
*/ |
178
|
|
|
protected function checkThreemaID(string $threemaID):string{ |
179
|
|
|
|
180
|
|
|
if(preg_match('/^[a-z\d\*]{8}$/i', $threemaID)){ |
181
|
|
|
return strtoupper($threemaID); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
throw new GatewayException('invalid threema id'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $phoneNo |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
* @throws \chillerlan\Threema\GatewayException |
192
|
|
|
*/ |
193
|
|
|
protected function checkPhoneNo(string $phoneNo):string{ |
194
|
|
|
$phoneNo = preg_replace('/[^\d]/', '', $phoneNo); |
195
|
|
|
|
196
|
|
|
if(empty($phoneNo)){ |
197
|
|
|
throw new GatewayException('invalid phone number'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return (string)$phoneNo; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $email |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
* @throws \chillerlan\Threema\GatewayException |
208
|
|
|
*/ |
209
|
|
|
protected function checkEmail($email):string{ |
210
|
|
|
$email = filter_var(trim($email), FILTER_VALIDATE_EMAIL); |
211
|
|
|
|
212
|
|
|
if(empty($email)){ |
213
|
|
|
throw new GatewayException('invalid email'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return strtolower($email); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $hash |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
* @throws \chillerlan\Threema\GatewayException |
224
|
|
|
*/ |
225
|
|
|
protected function checkHash(string $hash):string{ |
226
|
|
|
|
227
|
|
|
if(preg_match('/^[a-f\d]{64}$/i', $hash)){ |
228
|
|
|
return $hash; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
throw new GatewayException('invalid hash'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|