Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace smsgateway_aws\local\service;
18
 
19
use core_sms\message_status;
20
use smsgateway_aws\helper;
21
use smsgateway_aws\local\aws_sms_service_provider;
22
use stdClass;
23
 
24
/**
25
 * AWS SNS service provider.
26
 *
27
 * @package    smsgateway_aws
28
 * @copyright  2024 Safat Shahin <safat.shahin@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class aws_sns implements aws_sms_service_provider {
32
 
33
    /**
34
     * Include the required calls.
35
     */
36
    private static function require(): void {
37
        global $CFG;
38
        require_once($CFG->libdir . '/aws-sdk/src/functions.php');
39
    }
40
 
41
    #[\Override]
42
    public static function send_sms_message(
43
        string $messagecontent,
44
        string $phonenumber,
45
        stdclass $config,
46
    ): message_status {
47
        global $SITE;
48
        self::require();
49
 
50
        // Setup client params and instantiate client.
51
        $params = [
52
            'version' => 'latest',
53
            'region' => $config->api_region,
54
            'http' => ['proxy' => helper::get_proxy_string()],
55
        ];
56
        if (!property_exists($config, 'usecredchain') || !$config->usecredchain) {
57
            $params['credentials'] = [
58
                'key' => $config->api_key,
59
                'secret' => $config->api_secret,
60
            ];
61
        }
62
        $client = new \Aws\Sns\SnsClient($params);
63
 
64
        // Set up the sender information.
65
        $senderid = $SITE->shortname;
66
        // Remove spaces and non-alphanumeric characters from ID.
67
        $senderid = preg_replace("/[^A-Za-z0-9]/", '', trim($senderid));
68
        // We have to truncate the senderID to 11 chars.
69
        $senderid = substr($senderid, 0, 11);
70
 
71
        try {
72
            // These messages need to be transactional.
73
            $client->SetSMSAttributes([
74
                'attributes' => [
75
                    'DefaultSMSType' => 'Transactional',
76
                    'DefaultSenderID' => $senderid,
77
                ],
78
            ]);
79
 
80
            // Actually send the message.
81
            $client->publish([
82
                'Message' => $messagecontent,
83
                'PhoneNumber' => $phonenumber,
84
            ]);
85
            return \core_sms\message_status::GATEWAY_SENT;
86
        } catch (\Aws\Exception\AwsException $e) {
87
            return \core_sms\message_status::GATEWAY_NOT_AVAILABLE;
88
        }
89
    }
90
}