#441 SMS sending

This commit is contained in:
Roland Gruber 2025-05-30 20:42:09 +02:00
parent 09dbbcb0fc
commit eae28a2986
8 changed files with 124 additions and 9 deletions

View file

@ -358,10 +358,10 @@ $helpArray = [
"296" => ["Headline" => _("SMS provider"), "296" => ["Headline" => _("SMS provider"),
"Text" => _("Please select the SMS provider that should be used for password and reset link sending.") "Text" => _("Please select the SMS provider that should be used for password and reset link sending.")
], ],
"297" => ["Headline" => _("SMS API key"), "297" => ["Headline" => _("API key"),
"Text" => _("Please enter the API key of your SMS provider.") "Text" => _("Please enter the API key of your SMS provider.")
], ],
"298" => ["Headline" => _("SMS API token"), "298" => ["Headline" => _("Token"),
"Text" => _("Please enter the API token of your SMS provider.") "Text" => _("Please enter the API token of your SMS provider.")
], ],
"299" => ["Headline" => _("Mobile phone attributes"), "299" => ["Headline" => _("Mobile phone attributes"),

View file

@ -2961,7 +2961,7 @@ class LAMCfgMain {
public const MAIL_ATTRIBUTE_DEFAULT = 'mail'; public const MAIL_ATTRIBUTE_DEFAULT = 'mail';
public const MAIL_BACKUP_ATTRIBUTE_DEFAULT = 'passwordselfresetbackupmail'; public const MAIL_BACKUP_ATTRIBUTE_DEFAULT = 'passwordselfresetbackupmail';
public const SMS_ATTRIBUTES_DEFAULT = 'mobileTelephoneNumber;mobile;pager'; public const SMS_ATTRIBUTES_DEFAULT = 'mobileTelephoneNumber;mobile';
/** Default profile */ /** Default profile */
public $default; public $default;

View file

@ -63,7 +63,7 @@ class GatewayApiSmsProvider implements SmsProvider {
], ],
"message" => $message, "message" => $message,
"label" => "LAM Pro", "label" => "LAM Pro",
"sender" => "LAM Pro" "sender" => SmsService::SENDER_NAME
]; ];
$postJson = json_encode($postData); $postJson = json_encode($postData);
curl_setopt_array($curl, [ curl_setopt_array($curl, [
@ -77,7 +77,6 @@ class GatewayApiSmsProvider implements SmsProvider {
'Accept: application/json', 'Accept: application/json',
], ],
]); ]);
logNewMessage(LOG_DEBUG, 'SMS message for: ' . $number);
$response = curl_exec($curl); $response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);

View file

@ -74,7 +74,6 @@ class SmsApiSms implements SmsProvider {
'Authorization: Bearer ' . $apiToken, 'Authorization: Bearer ' . $apiToken,
], ],
]); ]);
logNewMessage(LOG_DEBUG, 'SMS message for: ' . $number);
$response = curl_exec($curl); $response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);

View file

@ -38,6 +38,11 @@ use LAMException;
*/ */
class SmsService { class SmsService {
/**
* Sender name for SMS display.
*/
public const SENDER_NAME = 'LAM Pro';
/** /**
* Includes all plugin files. * Includes all plugin files.
*/ */
@ -90,9 +95,21 @@ class SmsService {
throw new LAMException('Provider not found'); throw new LAMException('Provider not found');
} }
$provider = $providers[$providerId]; $provider = $providers[$providerId];
$number = $this->cleanNumber($number);
logNewMessage(LOG_DEBUG, 'SMS message for: ' . $number);
$provider->sendSms($message, $number, $apiKey, $apiToken); $provider->sendSms($message, $number, $apiKey, $apiToken);
} }
/**
* Cleans any extra characters from the telephone number.
*
* @param string $number number
* @return string cleaned value
*/
private function cleanNumber(string $number): string {
return preg_replace('/[^0-9+]+/', '', $number);
}
} }
/** /**

View file

@ -0,0 +1,101 @@
<?php
namespace LAM\PLUGINS\SMS;
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2025 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
use LAMException;
/**
* SMSBOX SMS provider.
*
* @author Roland Gruber
*/
/**
* SMSBOX SMS provider.
*
* @package LAM\PLUGINS\SMS
*/
class SmsboxSms implements SmsProvider {
/**
* @inheritDoc
*/
public function getLabel(): string {
return 'SMSBOX';
}
/**
* @inheritDoc
*/
public function getId(): string {
return 'smsbox';
}
/**
* @inheritDoc
*/
public function sendSms(string $message, string $number, ?string $apiKey = '', ?string $apiToken = ''): void {
$curl = curl_init();
$postData = [
"dest" => $number,
"msg" => $message,
'charset' => 'utf-8',
'mode' => 'Standard',
'strategy' => '2'
];
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.smsbox.pro/1.1/api.php',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Authorization: App ' . $apiKey,
],
]);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
logNewMessage(LOG_DEBUG, 'SMS gateway response: ' . $response);
if (($httpCode !== 200) || ($response !== 'OK')) {
logNewMessage(LOG_ERR, 'Unable to send SMS: ' . $response);
throw new LAMException(null, '');
}
}
/**
* @inheritDoc
*/
public function usesApiToken(): bool {
return false;
}
/**
* @inheritDoc
*/
public function usesApiKey(): bool {
return true;
}
}

View file

@ -86,7 +86,6 @@ class SweegoSms implements SmsProvider {
'Api-Key: ' . $apiKey, 'Api-Key: ' . $apiKey,
], ],
]); ]);
logNewMessage(LOG_DEBUG, 'SMS message for: ' . $number);
$response = curl_exec($curl); $response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); curl_close($curl);

View file

@ -768,8 +768,8 @@ if (isset($_POST['submitFormData'])) {
$smsProviderSelect->setTableRowsToHide($smsOptionsToHide); $smsProviderSelect->setTableRowsToHide($smsOptionsToHide);
$smsProviderSelect->setTableRowsToShow($smsOptionsToShow); $smsProviderSelect->setTableRowsToShow($smsOptionsToShow);
$row->add($smsProviderSelect); $row->add($smsProviderSelect);
$row->add(new htmlResponsiveInputField(_('SMS API key'), 'smsApiKey', $cfg->smsApiKey, '297')); $row->add(new htmlResponsiveInputField(_('API key'), 'smsApiKey', $cfg->smsApiKey, '297'));
$row->add(new htmlResponsiveInputField(_('SMS API token'), 'smsApiToken', $cfg->smsToken, '298')); $row->add(new htmlResponsiveInputField(_('Token'), 'smsApiToken', $cfg->smsToken, '298'));
$row->add(new htmlResponsiveInputField(_("Mobile phone attributes"), 'smsAttributes', implode(';', $cfg->getSmsAttributes()), '299')); $row->add(new htmlResponsiveInputField(_("Mobile phone attributes"), 'smsAttributes', implode(';', $cfg->getSmsAttributes()), '299'));
$smsTestButtonRow = new htmlResponsiveRow(); $smsTestButtonRow = new htmlResponsiveRow();
$smsTestButton = new htmlButton('testSms', _('Test settings')); $smsTestButton = new htmlButton('testSms', _('Test settings'));