Debian Codegraph @

lib/plugins/sms/SmsApiSmsProvider.inc

raw
1 <?php
2
3 namespace LAM\PLUGINS\SMS;
4
5 /*
6
7 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
8 Copyright (C) 2025 - 2026 Roland Gruber
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
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 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
24 */
25
26 use LAMException;
27
28 /**
29 * SMSAPI SMS provider.
30 *
31 * @author Roland Gruber
32 */
33
34 /**
35 * SMSAPI SMS provider.
36 *
37 * @package LAM\PLUGINS\SMS
38 */
39 class SmsApiSms implements SmsProvider {
40
41 /**
42 * @inheritDoc
43 */
44 public function getLabel(): string {
45 return 'SMSAPI';
46 }
47
48 /**
49 * @inheritDoc
50 */
51 public function getId(): string {
52 return 'smsapi';
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function sendSms(string $message, string $number, ?string $apiKey = '', ?string $apiToken = '',
59 ?string $accountId = '', ?string $region = '', ?string $from = '', ?string $subject = ''): void {
60 $curl = curl_init();
61 $postData = [
62 "to" => $number,
63 "format" => "json",
64 "message" => $message,
65 ];
66 $postJson = json_encode($postData);
67 curl_setopt_array($curl, [
68 CURLOPT_URL => 'https://api.smsapi.com/sms.do',
69 CURLOPT_RETURNTRANSFER => true,
70 CURLOPT_POST => true,
71 CURLOPT_POSTFIELDS => $postJson,
72 CURLOPT_HTTPHEADER => [
73 'Content-Type: application/json',
74 'Accept: application/json',
75 'Authorization: Bearer ' . $apiToken,
76 ],
77 ]);
78 $response = curl_exec($curl);
79 $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
80 logNewMessage(LOG_DEBUG, 'SMS gateway response: ' . $response);
81 $responseJson = json_decode($response, true);
82 if (($httpCode !== 200) || isset($responseJson['error'])) {
83 logNewMessage(LOG_ERR, 'Unable to send SMS: ' . $response);
84 $message = $responseJson['message'] ?? $responseJson['list'][0]['error'] ?? '';
85 throw new LAMException(null, $message);
86 }
87 }
88
89 /**
90 * @inheritDoc
91 */
92 public function usesApiToken(): bool {
93 return true;
94 }
95
96 /**
97 * @inheritDoc
98 */
99 public function usesApiKey(): bool {
100 return false;
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function usesAccountId(): bool {
107 return false;
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function usesFrom(): bool {
114 return false;
115 }
116
117 /**
118 * @inheritDoc
119 */
120 public function usesRegion(): bool {
121 return false;
122 }
123
124 /**
125 * @inheritDoc
126 */
127 public function usesSubject(): bool {
128 return false;
129 }
130
131 }