1
|
|
|
<?PHP |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
5
|
|
|
* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Mailgun; |
11
|
|
|
|
12
|
|
|
use Http\Client\Common\HttpMethodsClient; |
13
|
|
|
use Http\Client\HttpClient; |
14
|
|
|
use Mailgun\Connection\RestClient; |
15
|
|
|
use Mailgun\Constants\ExceptionMessages; |
16
|
|
|
use Mailgun\Lists\OptInHandler; |
17
|
|
|
use Mailgun\Messages\BatchMessage; |
18
|
|
|
use Mailgun\Messages\Exceptions; |
19
|
|
|
use Mailgun\Messages\MessageBuilder; |
20
|
|
|
use Mailgun\Deserializer\ModelDeserializer; |
21
|
|
|
use Mailgun\Deserializer\ResponseDeserializer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class is the base class for the Mailgun SDK. |
25
|
|
|
*/ |
26
|
|
|
class Mailgun |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RestClient |
30
|
|
|
* |
31
|
|
|
* @depracated Will be removed in 3.0 |
32
|
|
|
*/ |
33
|
|
|
protected $restClient; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var null|string |
37
|
|
|
*/ |
38
|
|
|
protected $apiKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var HttpMethodsClient |
42
|
|
|
*/ |
43
|
|
|
private $httpClient; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ResponseDeserializer |
47
|
|
|
*/ |
48
|
|
|
private $deserializer; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var RequestBuilder |
52
|
|
|
*/ |
53
|
|
|
private $requestBuilder; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|null $apiKey |
57
|
|
|
* @param HttpClient|null $httpClient |
58
|
|
|
* @param string $apiEndpoint |
59
|
|
|
* @param ResponseDeserializer|null $deserializer |
60
|
|
|
* @param HttpClientConfigurator|null $clientConfigurator |
61
|
|
|
* @param RequestBuilder|null $requestBuilder |
62
|
|
|
*/ |
63
|
6 |
|
public function __construct( |
64
|
|
|
$apiKey = null, |
65
|
|
|
HttpClient $httpClient = null, /* Deprecated, will be removed in 3.0 */ |
66
|
|
|
$apiEndpoint = 'api.mailgun.net', /* Deprecated, will be removed in 3.0 */ |
67
|
|
|
ResponseDeserializer $deserializer = null, |
68
|
|
|
HttpClientConfigurator $clientConfigurator = null, |
69
|
|
|
RequestBuilder $requestBuilder = null |
70
|
|
|
) { |
71
|
6 |
|
$this->apiKey = $apiKey; |
72
|
6 |
|
$this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient); |
73
|
|
|
|
74
|
6 |
|
if (null === $clientConfigurator) { |
75
|
6 |
|
$clientConfigurator = new HttpClientConfigurator(); |
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* To be backward compatible |
79
|
|
|
*/ |
80
|
6 |
|
if ($apiEndpoint !== 'api.mailgun.net') { |
81
|
|
|
$clientConfigurator->setEndpoint($apiEndpoint); |
82
|
|
|
} |
83
|
6 |
|
if ($httpClient !== null) { |
84
|
|
|
$clientConfigurator->setHttpClient($httpClient); |
85
|
|
|
} |
86
|
6 |
|
} |
87
|
|
|
|
88
|
6 |
|
$clientConfigurator->setApiKey($apiKey); |
89
|
|
|
|
90
|
6 |
|
$this->httpClient = $clientConfigurator->createConfiguredClient(); |
|
|
|
|
91
|
6 |
|
$this->requestBuilder = $requestBuilder ?: new RequestBuilder(); |
92
|
6 |
|
$this->deserializer = $deserializer ?: new ModelDeserializer(); |
93
|
6 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* This function allows the sending of a fully formed message OR a custom |
97
|
|
|
* MIME string. If sending MIME, the string must be passed in to the 3rd |
98
|
|
|
* position of the function call. |
99
|
|
|
* |
100
|
|
|
* @param string $workingDomain |
101
|
|
|
* @param array $postData |
102
|
|
|
* @param array $postFiles |
103
|
|
|
* |
104
|
|
|
* @throws Exceptions\MissingRequiredMIMEParameters |
105
|
|
|
* |
106
|
|
|
* @return \stdClass |
107
|
|
|
*/ |
108
|
6 |
|
public function sendMessage($workingDomain, $postData, $postFiles = []) |
109
|
|
|
{ |
110
|
6 |
|
if (is_array($postFiles)) { |
111
|
4 |
|
return $this->post("$workingDomain/messages", $postData, $postFiles); |
112
|
2 |
|
} elseif (is_string($postFiles)) { |
113
|
1 |
|
$tempFile = tempnam(sys_get_temp_dir(), 'MG_TMP_MIME'); |
114
|
1 |
|
$fileHandle = fopen($tempFile, 'w'); |
115
|
1 |
|
fwrite($fileHandle, $postFiles); |
116
|
|
|
|
117
|
1 |
|
$result = $this->post("$workingDomain/messages.mime", $postData, ['message' => $tempFile]); |
118
|
1 |
|
fclose($fileHandle); |
119
|
1 |
|
unlink($tempFile); |
120
|
|
|
|
121
|
1 |
|
return $result; |
122
|
|
|
} else { |
123
|
1 |
|
throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); |
124
|
|
|
} |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* This function checks the signature in a POST request to see if it is |
129
|
|
|
* authentic. |
130
|
|
|
* |
131
|
|
|
* Pass an array of parameters. If you pass nothing, $_POST will be |
132
|
|
|
* used instead. |
133
|
|
|
* |
134
|
|
|
* If this function returns FALSE, you must not process the request. |
135
|
|
|
* You should reject the request with status code 403 Forbidden. |
136
|
|
|
* |
137
|
|
|
* @param array|null $postData |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
3 |
|
public function verifyWebhookSignature($postData = null) |
|
|
|
|
142
|
|
|
{ |
143
|
3 |
|
if ($postData === null) { |
144
|
|
|
$postData = $_POST; |
145
|
|
|
} |
146
|
3 |
|
if (!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { |
147
|
1 |
|
return false; |
148
|
|
|
} |
149
|
2 |
|
$hmac = hash_hmac('sha256', "{$postData['timestamp']}{$postData['token']}", $this->apiKey); |
150
|
2 |
|
$sig = $postData['signature']; |
151
|
2 |
|
if (function_exists('hash_equals')) { |
152
|
|
|
// hash_equals is constant time, but will not be introduced until PHP 5.6 |
153
|
|
|
return hash_equals($hmac, $sig); |
154
|
|
|
} else { |
155
|
2 |
|
return $hmac === $sig; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $endpointUrl |
161
|
|
|
* @param array $postData |
162
|
|
|
* @param array $files |
163
|
|
|
* |
164
|
|
|
* @return \stdClass |
165
|
|
|
*/ |
166
|
7 |
|
public function post($endpointUrl, $postData = [], $files = []) |
167
|
|
|
{ |
168
|
7 |
|
return $this->restClient->post($endpointUrl, $postData, $files); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $endpointUrl |
173
|
|
|
* @param array $queryString |
174
|
|
|
* |
175
|
|
|
* @return \stdClass |
176
|
|
|
*/ |
177
|
|
|
public function get($endpointUrl, $queryString = []) |
178
|
|
|
{ |
179
|
|
|
return $this->restClient->get($endpointUrl, $queryString); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $url |
184
|
|
|
* |
185
|
|
|
* @return \stdClass |
186
|
|
|
*/ |
187
|
2 |
|
public function getAttachment($url) |
188
|
|
|
{ |
189
|
2 |
|
return $this->restClient->getAttachment($url); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $endpointUrl |
194
|
|
|
* |
195
|
|
|
* @return \stdClass |
196
|
|
|
*/ |
197
|
|
|
public function delete($endpointUrl) |
198
|
|
|
{ |
199
|
|
|
return $this->restClient->delete($endpointUrl); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $endpointUrl |
204
|
|
|
* @param array $putData |
205
|
|
|
* |
206
|
|
|
* @return \stdClass |
207
|
|
|
*/ |
208
|
|
|
public function put($endpointUrl, $putData) |
209
|
|
|
{ |
210
|
|
|
return $this->restClient->put($endpointUrl, $putData); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $apiVersion |
215
|
|
|
* |
216
|
|
|
* @return Mailgun |
217
|
|
|
*/ |
218
|
|
|
public function setApiVersion($apiVersion) |
219
|
|
|
{ |
220
|
|
|
$this->restClient->setApiVersion($apiVersion); |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param bool $sslEnabled |
227
|
|
|
* |
228
|
|
|
* @return Mailgun |
229
|
|
|
* |
230
|
|
|
* @deprecated This will be removed in 3.0. Mailgun does not support non-secure connections to their API. |
231
|
|
|
*/ |
232
|
|
|
public function setSslEnabled($sslEnabled) |
233
|
|
|
{ |
234
|
|
|
$this->restClient->setSslEnabled($sslEnabled); |
|
|
|
|
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return MessageBuilder |
241
|
|
|
*/ |
242
|
28 |
|
public function MessageBuilder() |
243
|
|
|
{ |
244
|
28 |
|
return new MessageBuilder(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return OptInHandler |
249
|
|
|
*/ |
250
|
3 |
|
public function OptInHandler() |
251
|
|
|
{ |
252
|
3 |
|
return new OptInHandler(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $workingDomain |
257
|
|
|
* @param bool $autoSend |
258
|
|
|
* |
259
|
|
|
* @return BatchMessage |
260
|
|
|
*/ |
261
|
15 |
|
public function BatchMessage($workingDomain, $autoSend = true) |
262
|
|
|
{ |
263
|
15 |
|
return new BatchMessage($this->restClient, $workingDomain, $autoSend); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return Api\Stats |
268
|
|
|
*/ |
269
|
|
|
public function stats() |
270
|
|
|
{ |
271
|
|
|
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->deserializer); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return Api\Domain |
276
|
|
|
*/ |
277
|
|
|
public function domains() |
278
|
|
|
{ |
279
|
|
|
return new Api\Domain($this->httpClient, $this->requestBuilder, $this->deserializer); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return Api\Tag |
284
|
|
|
*/ |
285
|
|
|
public function tag() |
286
|
|
|
{ |
287
|
|
|
return new Api\Tag($this->httpClient, $this->requestBuilder, $this->deserializer); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return Api\Event |
292
|
|
|
*/ |
293
|
|
|
public function events() |
294
|
|
|
{ |
295
|
|
|
return new Api\Event($this->httpClient, $this->requestBuilder, $this->deserializer); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return Api\Routes |
300
|
|
|
*/ |
301
|
|
|
public function routes() |
302
|
|
|
{ |
303
|
|
|
return new Api\Routes($this->httpClient, $this->requestBuilder, $this->deserializer); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return Api\Webhook |
308
|
|
|
*/ |
309
|
|
|
public function webhooks() |
310
|
|
|
{ |
311
|
|
|
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->deserializer); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return Api\Message |
316
|
|
|
*/ |
317
|
|
|
public function messages() |
318
|
|
|
{ |
319
|
|
|
return new Api\Message($this->httpClient, $this->requestBuilder, $this->deserializer); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return Api\Suppressions |
324
|
|
|
*/ |
325
|
|
|
public function suppressions() |
326
|
|
|
{ |
327
|
|
|
return new Api\Suppressions($this->httpClient, $this->requestBuilder, $this->deserializer); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..