| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\PLUGINS\SMS; |
| 4 |
|
| 5 |
use Aws\Exception\AwsException; |
| 6 |
use Aws\Sns\SnsClient; |
| 7 |
use LAMException; |
| 8 |
|
| 9 |
/* |
| 10 |
|
| 11 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 12 |
Copyright (C) 2025 - 2026 Roland Gruber |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
/** |
| 31 |
* AWS SNS SMS provider. |
| 32 |
* |
| 33 |
* @author Roland Gruber |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* AWS SNS SMS provider. |
| 38 |
* |
| 39 |
* @package LAM\PLUGINS\SMS |
| 40 |
*/ |
| 41 |
class AwsSnsSms implements SmsProvider { |
| 42 |
|
| 43 |
/** |
| 44 |
* @inheritDoc |
| 45 |
*/ |
| 46 |
public function getLabel(): string { |
| 47 |
return 'AWS SNS'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @inheritDoc |
| 52 |
*/ |
| 53 |
public function getId(): string { |
| 54 |
return 'aws-sns'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @inheritDoc |
| 59 |
*/ |
| 60 |
public function sendSms(string $message, string $number, ?string $apiKey = '', ?string $apiToken = '', |
| 61 |
?string $accountId = '', ?string $region = '', ?string $from = '', ?string $subject = ''): void { |
| 62 |
$options = [ |
| 63 |
'Message' => $message, |
| 64 |
'PhoneNumber' => $number, |
| 65 |
]; |
| 66 |
include_once __DIR__ . '/../../3rdParty/composer/autoload.php'; |
| 67 |
$clientOptions = [ |
| 68 |
'version' => '2010-03-31', |
| 69 |
'region' => $region, |
| 70 |
'credentials' => [ |
| 71 |
'key' => $accountId, |
| 72 |
'secret' => $apiToken, |
| 73 |
], |
| 74 |
]; |
| 75 |
$snsClient = new SnsClient($clientOptions); |
| 76 |
try { |
| 77 |
$result = $snsClient->publish($options); |
| 78 |
logNewMessage(LOG_DEBUG, 'SMS sending response: ' . print_r($result, true)); |
| 79 |
} |
| 80 |
catch (AwsException $e) { |
| 81 |
logNewMessage(LOG_ERR, 'Unable to send SMS to ' . $number . ': ' . $e->getMessage()); |
| 82 |
throw new LAMException(null, $e->getMessage(), $e); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @inheritDoc |
| 88 |
*/ |
| 89 |
public function usesApiToken(): bool { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @inheritDoc |
| 95 |
*/ |
| 96 |
public function usesApiKey(): bool { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @inheritDoc |
| 102 |
*/ |
| 103 |
public function usesAccountId(): bool { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @inheritDoc |
| 109 |
*/ |
| 110 |
public function usesFrom(): bool { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @inheritDoc |
| 116 |
*/ |
| 117 |
public function usesRegion(): bool { |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @inheritDoc |
| 123 |
*/ |
| 124 |
public function usesSubject(): bool { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
} |