1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Christian Schrötter <[email protected]> |
7
|
|
|
* @author Christoph Wurst <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Nextcloud - Two-factor Gateway |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; |
26
|
|
|
|
27
|
|
|
use Exception; |
28
|
|
|
use OCA\TwoFactorGateway\Exception\SmsTransmissionException; |
29
|
|
|
use OCP\Http\Client\IClient; |
30
|
|
|
use OCP\Http\Client\IClientService; |
31
|
|
|
|
32
|
|
|
class ClickatellPortal implements IProvider { |
33
|
|
|
public const PROVIDER_ID = 'clickatellportal'; |
34
|
|
|
|
35
|
|
|
/** @var IClient */ |
36
|
|
|
private $client; |
37
|
|
|
|
38
|
|
|
/** @var ClickatellPortalConfig */ |
39
|
|
|
private $config; |
40
|
|
|
|
41
|
|
|
public function __construct(IClientService $clientService, |
42
|
|
|
ClickatellPortalConfig $config) { |
43
|
|
|
$this->client = $clientService->newClient(); |
44
|
|
|
$this->config = $config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $identifier |
49
|
|
|
* @param string $message |
50
|
|
|
* |
51
|
|
|
* @throws SmsTransmissionException |
52
|
|
|
*/ |
53
|
|
|
public function send(string $identifier, string $message) { |
54
|
|
|
$config = $this->getConfig(); |
55
|
|
|
try { |
56
|
|
|
$from = $config->getFromNumber(); |
57
|
|
|
$from = !is_null($from) ? sprintf('&from=%s', urlencode($from)) : ''; |
|
|
|
|
58
|
|
|
$response = $this->client->get(vsprintf('https://platform.clickatell.com/messages/http/send?apiKey=%s&to=%s&content=%s%s', [ |
59
|
|
|
urlencode($config->getApiKey()), |
60
|
|
|
urlencode($identifier), |
61
|
|
|
urlencode($message), |
62
|
|
|
$from, |
63
|
|
|
])); |
64
|
|
|
} catch (Exception $ex) { |
65
|
|
|
throw new SmsTransmissionException(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($response->getStatusCode() !== 202) { |
69
|
|
|
throw new SmsTransmissionException($response->getBody()); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ClickatellPortalConfig |
75
|
|
|
*/ |
76
|
|
|
public function getConfig(): IProviderConfig { |
77
|
|
|
return $this->config; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|