Debian Codegraph @

lib/plugins/sms/SmsService.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 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 * SMS service.
30 *
31 * @author Roland Gruber
32 */
33
34 /**
35 * Provides SMS services.
36 *
37 * @package LAM\PLUGINS\SMS
38 */
39 class SmsService {
40
41 /**
42 * Sender name for SMS display.
43 */
44 public const SENDER_NAME = 'LAM Pro';
45
46 /**
47 * Includes all plugin files.
48 */
49 private function includeFiles(): void {
50 $pluginDir = dir(__DIR__);
51 while ($entry = $pluginDir->read()) {
52 if ((str_starts_with($entry, '.')) || ($entry === basename(__FILE__))) {
53 continue;
54 }
55 include_once(__DIR__ . '/' . $entry);
56 }
57 }
58
59 /**
60 * Returns a list of SmsProvider objects.
61 *
62 * @return SmsProvider[] providers (id => provider object)
63 */
64 public function findProviders(): array {
65 $this->includeFiles();
66 $providers = [];
67 foreach (get_declared_classes() as $declaredClass) {
68 if (in_array('LAM\PLUGINS\SMS\SmsProvider', class_implements($declaredClass))) {
69 $provider = new $declaredClass();
70 $providers[$provider->getId()] = $provider;
71 }
72 }
73 return $providers;
74 }
75
76 /**
77 * Sends out an SMS.
78 *
79 * @param string $message message text
80 * @param string $number mobile phone number
81 * @param string|null $providerId provider ID
82 * @param string|null $apiKey API key
83 * @param string|null $apiToken API token
84 * @param string|null $accountId account ID
85 * @param string|null $region region
86 * @param string|null $from from
87 * @param string|null $subject subject text
88 * @throws LAMException error sending SMS
89 */
90 public function sendSms(string $message, string $number, ?string $providerId = null, ?string $apiKey = null,
91 ?string $apiToken = null, ?string $accountId = null, ?string $region = null,
92 ?string $from = null, ?string $subject = null): void {
93 if ($providerId === null) {
94 $providerId = $_SESSION['cfgMain']->smsProvider;
95 $apiKey = $_SESSION['cfgMain']->smsApiKey;
96 $apiToken = $_SESSION['cfgMain']->smsToken;
97 $accountId = $_SESSION['cfgMain']->smsAccountId;
98 $region = $_SESSION['cfgMain']->smsRegion;
99 $from = $_SESSION['cfgMain']->smsFrom;
100 $subject = $_SESSION['cfgMain']->smsSubject;
101 }
102 $providers = $this->findProviders();
103 if (!isset($providers[$providerId])) {
104 logNewMessage(LOG_ERR, 'SMS provider not found: ' . $providerId);
105 throw new LAMException('Provider not found');
106 }
107 $provider = $providers[$providerId];
108 $number = $this->cleanNumber($number);
109 $number = $this->addCountryPrefixIfMissing($number);
110 logNewMessage(LOG_DEBUG, 'SMS message for: ' . $number);
111 $provider->sendSms($message, $number, $apiKey, $apiToken, $accountId, $region, $from, $subject);
112 }
113
114 /**
115 * Cleans any extra characters from the telephone number.
116 *
117 * @param string $number number
118 * @return string cleaned value
119 */
120 private function cleanNumber(string $number): string {
121 return preg_replace('/[^0-9+]+/', '', $number);
122 }
123
124 /**
125 * Adds the default country prefix to the telephone number if needed.
126 *
127 * @param string $number number
128 * @return string prefixed value
129 */
130 private function addCountryPrefixIfMissing(string $number) {
131 $prefix = $_SESSION['cfgMain']->smsDefaultCountryPrefix;
132 if (empty($prefix) || str_starts_with($number, '+') || str_starts_with($number, '00')) {
133 return $number;
134 }
135 if (str_starts_with($number, '0')) {
136 $number = substr($number, 1);
137 }
138 return $prefix . $number;
139 }
140
141 }
142
143 /**
144 * Interface for providers of SMS services.
145 *
146 * @package LAM\PLUGINS\SMS
147 */
148 interface SmsProvider {
149
150 /**
151 * Returns the label of the service
152 *
153 * @return string label
154 */
155 public function getLabel(): string;
156
157 /**
158 * Returns the id of the service
159 *
160 * @return string id
161 */
162 public function getId(): string;
163
164 /**
165 * Returns if the provider requires an account ID.
166 *
167 * @return bool account id required
168 */
169 public function usesAccountId(): bool;
170
171 /**
172 * Returns if the provider requires a token.
173 *
174 * @return bool token required
175 */
176 public function usesApiToken(): bool;
177
178 /**
179 * Returns if the provider requires an API key.
180 *
181 * @return bool key required
182 */
183 public function usesApiKey(): bool;
184
185 /**
186 * Returns if the provider requires a FROM field.
187 *
188 * @return bool key required
189 */
190 public function usesFrom(): bool;
191
192 /**
193 * Returns if the provider requires a region field.
194 *
195 * @return bool region required
196 */
197 public function usesRegion(): bool;
198
199 /**
200 * Service uses a subject field.
201 *
202 * @return bool use subject
203 */
204 public function usesSubject(): bool;
205
206 /**
207 * Sends out an SMS.
208 *
209 * @param string $message message text
210 * @param string $number mobile phone number
211 * @param string|null $apiKey API key
212 * @param string|null $apiToken API token
213 * @param string|null $accountId account ID
214 * @param string|null $from from
215 * @param string|null $subject subject text
216 * @throws LAMException error sending SMS
217 */
218 public function sendSms(string $message, string $number, ?string $apiKey = '',
219 ?string $apiToken = '', ?string $accountId = '',
220 ?string $region = '', ?string $from = '', ?string $subject = ''): void;
221
222 }
223