Debian Codegraph @

lib/plugins/sms/TwilioSmsProvider.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 * Twilio SMS provider.
30 *
31 * @author Roland Gruber
32 */
33
34 /**
35 * Twilio SMS provider.
36 *
37 * @package LAM\PLUGINS\SMS
38 */
39 class TwilioSms implements SmsProvider {
40
41 /**
42 * @inheritDoc
43 */
44 public function getLabel(): string {
45 return 'Twilio';
46 }
47
48 /**
49 * @inheritDoc
50 */
51 public function getId(): string {
52 return 'twilio';
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 "Body" => $message,
64 ];
65 if (preg_match('/^[0-9+]+/', $from)) {
66 $postData['From'] = $from;
67 }
68 else {
69 $postData['MessagingServiceSid'] = $from;
70 }
71 curl_setopt_array($curl, [
72 CURLOPT_URL => 'https://api.twilio.com/2010-04-01/Accounts/' . urlencode($accountId) . '/Messages.json',
73 CURLOPT_RETURNTRANSFER => true,
74 CURLOPT_POST => true,
75 CURLOPT_POSTFIELDS => $postData,
76 CURLOPT_USERNAME => $accountId,
77 CURLOPT_PASSWORD => $apiToken,
78 ]);
79 $response = curl_exec($curl);
80 $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
81 logNewMessage(LOG_DEBUG, 'SMS gateway response: ' . $response);
82 $responseJson = json_decode($response, true);
83 if ($httpCode !== 201) {
84 logNewMessage(LOG_ERR, 'Unable to send SMS: ' . $response);
85 $message = $responseJson['message'] ?? $responseJson['error_message'] ?? '';
86 throw new LAMException(null, $message);
87 }
88 }
89
90 /**
91 * @inheritDoc
92 */
93 public function usesApiToken(): bool {
94 return true;
95 }
96
97 /**
98 * @inheritDoc
99 */
100 public function usesApiKey(): bool {
101 return false;
102 }
103
104 /**
105 * @inheritDoc
106 */
107 public function usesAccountId(): bool {
108 return true;
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public function usesFrom(): bool {
115 return true;
116 }
117
118 /**
119 * @inheritDoc
120 */
121 public function usesRegion(): bool {
122 return false;
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function usesSubject(): bool {
129 return false;
130 }
131
132 }