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