|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPDX-FileCopyrightText: 2024 Christian Schrötter <[email protected]> |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\Drivers; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use OCA\TwoFactorGateway\Exception\MessageTransmissionException; |
|
14
|
|
|
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\AProvider; |
|
15
|
|
|
use OCP\Http\Client\IClient; |
|
16
|
|
|
use OCP\Http\Client\IClientService; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @method string getApi() |
|
20
|
|
|
* @method static setApi(string $api) |
|
21
|
|
|
* @method string getUser() |
|
22
|
|
|
* @method static setUser(string $user) |
|
23
|
|
|
* @method string getPassword() |
|
24
|
|
|
* @method static setPassword(string $password) |
|
25
|
|
|
*/ |
|
26
|
|
|
class ClickatellCentral extends AProvider { |
|
27
|
|
|
public const SCHEMA = [ |
|
28
|
|
|
'id' => 'clickatell_central', |
|
29
|
|
|
'name' => 'Clickatell Central', |
|
30
|
|
|
'fields' => [ |
|
31
|
|
|
['field' => 'api', 'prompt' => 'Please enter your central.clickatell.com API-ID:'], |
|
32
|
|
|
['field' => 'user', 'prompt' => 'Please enter your central.clickatell.com username:'], |
|
33
|
|
|
['field' => 'password', 'prompt' => 'Please enter your central.clickatell.com password:'], |
|
34
|
|
|
], |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
private IClient $client; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
IClientService $clientService, |
|
41
|
|
|
) { |
|
42
|
|
|
$this->client = $clientService->newClient(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
#[\Override] |
|
46
|
|
|
public function send(string $identifier, string $message) { |
|
47
|
|
|
try { |
|
48
|
|
|
$response = $this->client->get(vsprintf('https://api.clickatell.com/http/sendmsg?user=%s&password=%s&api_id=%u&to=%s&text=%s', [ |
|
49
|
|
|
urlencode($this->getUser()), |
|
50
|
|
|
urlencode($this->getPassword()), |
|
51
|
|
|
$this->getApi(), |
|
52
|
|
|
urlencode($identifier), |
|
53
|
|
|
urlencode($message), |
|
54
|
|
|
])); |
|
55
|
|
|
} catch (Exception $ex) { |
|
56
|
|
|
throw new MessageTransmissionException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$body = (string)$response->getBody(); |
|
60
|
|
|
if ($response->getStatusCode() !== 200 || substr($body, 0, 4) !== 'ID: ') { |
|
61
|
|
|
throw new MessageTransmissionException($body); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|