1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SPDX-FileCopyrightText: 2024 Marcin Kot <[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 getToken() |
20
|
|
|
* @method static setToken(string $token) |
21
|
|
|
* @method string getSender() |
22
|
|
|
* @method static setSender(string $sender) |
23
|
|
|
*/ |
24
|
|
|
class SMSApi extends AProvider { |
25
|
|
|
public const SCHEMA = [ |
26
|
|
|
'name' => 'SMSAPI', |
27
|
|
|
'fields' => [ |
28
|
|
|
['field' => 'token', 'prompt' => 'Please enter your SMSApi.com API token:'], |
29
|
|
|
['field' => 'sender','prompt' => 'Please enter your SMSApi.com sender name:'], |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
private IClient $client; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
IClientService $clientService, |
36
|
|
|
) { |
37
|
|
|
$this->client = $clientService->newClient(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
#[\Override] |
41
|
|
|
public function send(string $identifier, string $message) { |
42
|
|
|
$sender = $this->getSender(); |
43
|
|
|
$token = $this->getToken(); |
44
|
|
|
$url = 'https://api.smsapi.com/sms.do'; |
45
|
|
|
|
46
|
|
|
$params = [ |
47
|
|
|
'to' => $identifier, //destination number |
48
|
|
|
'from' => $sender, //sendername made in https://ssl.smsapi.com/sms_settings/sendernames |
49
|
|
|
'message' => $message, //message content |
50
|
|
|
'format' => 'json', //get response in json format |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
static $content; |
55
|
|
|
|
56
|
|
|
$c = curl_init(); |
57
|
|
|
curl_setopt($c, CURLOPT_URL, $url); |
58
|
|
|
curl_setopt($c, CURLOPT_POST, true); |
59
|
|
|
curl_setopt($c, CURLOPT_POSTFIELDS, $params); |
60
|
|
|
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
61
|
|
|
curl_setopt($c, CURLOPT_HTTPHEADER, [ |
62
|
|
|
"Authorization: Bearer $token" |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$content = curl_exec($c); |
66
|
|
|
if ($content === false) { |
67
|
|
|
throw new MessageTransmissionException(); |
68
|
|
|
} |
69
|
|
|
$http_status = curl_getinfo($c, CURLINFO_HTTP_CODE); |
|
|
|
|
70
|
|
|
|
71
|
|
|
curl_close($c); |
72
|
|
|
$responseData = json_decode($content, true); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if ($responseData['count'] !== 1) { |
75
|
|
|
throw new MessageTransmissionException(); |
76
|
|
|
} |
77
|
|
|
} catch (Exception $ex) { |
78
|
|
|
throw new MessageTransmissionException(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|