Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 factor_sms\local\smsgateway;
18
 
19
use core\aws\admin_settings_aws_region;
20
use core\aws\aws_helper;
21
use factor_sms\event\sms_sent;
22
 
23
/**
24
 * AWS SNS SMS Gateway class
25
 *
26
 * @package     factor_sms
27
 * @author      Peter Burnett <peterburnett@catalyst-au.net>
28
 * @copyright   Catalyst IT
29
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class aws_sns implements gateway_interface {
32
 
33
    /**
34
     * Create an instance of this class.
35
     */
36
    public function __construct() {
37
        global $CFG;
38
        require_once($CFG->libdir . '/aws-sdk/src/functions.php');
39
        require_once($CFG->libdir . '/guzzlehttp/guzzle/src/functions_include.php');
40
        require_once($CFG->libdir . '/guzzlehttp/promises/src/functions_include.php');
41
    }
42
 
43
    /**
44
     * Sends a message using the AWS SNS API
45
     *
46
     * @param string $messagecontent the content to send in the SMS message.
47
     * @param string $phonenumber the destination for the message.
48
     * @return bool true on message send success
49
     */
50
    public function send_sms_message(string $messagecontent, string $phonenumber): bool {
51
        global $SITE, $USER;
52
 
53
        $config = get_config('factor_sms');
54
 
55
        // Setup client params and instantiate client.
56
        $params = [
57
            'version' => 'latest',
58
            'region' => $config->api_region,
59
            'http' => ['proxy' => aws_helper::get_proxy_string()],
60
        ];
61
        if (!$config->usecredchain) {
62
            $params['credentials'] = [
63
                'key' => $config->api_key,
64
                'secret' => $config->api_secret,
65
            ];
66
        }
67
        $client = new \Aws\Sns\SnsClient($params);
68
 
69
        // Transform the phone number to international standard.
70
        $phonenumber = \factor_sms\helper::format_number($phonenumber);
71
 
72
        // Setup the sender information.
73
        $senderid = $SITE->shortname;
74
        // Remove spaces and non-alphanumeric characters from ID.
75
        $senderid = preg_replace("/[^A-Za-z0-9]/", '', trim($senderid));
76
        // We have to truncate the senderID to 11 chars.
77
        $senderid = substr($senderid, 0, 11);
78
 
79
        if (defined('BEHAT_SITE_RUNNING')) {
80
            // Fake SMS sending in behat.
81
            return true;
82
        }
83
 
84
        try {
85
            // These messages need to be transactional.
86
            $client->SetSMSAttributes([
87
                'attributes' => [
88
                    'DefaultSMSType' => 'Transactional',
89
                    'DefaultSenderID' => $senderid,
90
                ],
91
            ]);
92
 
93
            // Actually send the message.
94
            $result = $client->publish([
95
                'Message' => $messagecontent,
96
                'PhoneNumber' => $phonenumber,
97
            ]);
98
 
99
            $data = [
100
                'relateduserid' => null,
101
                'context' => \context_user::instance($USER->id),
102
                'other' => [
103
                    'userid' => $USER->id,
104
                    'debug' => [
105
                        'messageid' => $result->get('MessageId'),
106
                    ],
107
                ],
108
            ];
109
            $event = sms_sent::create($data);
110
            $event->trigger();
111
 
112
            return true;
113
        } catch (\Aws\Exception\AwsException $e) {
114
            throw new \moodle_exception('errorawsconection', 'factor_sms', '', $e->getAwsErrorMessage());
115
        }
116
    }
117
 
118
    /**
119
     * Add gateway specific settings to the SMS factor settings page.
120
     *
121
     * @param \admin_settingpage $settings
122
     * @return void
123
     */
124
    public static function add_settings(\admin_settingpage $settings): void {
125
        $settings->add(new \admin_setting_configcheckbox('factor_sms/usecredchain',
126
            get_string('settings:aws:usecredchain', 'factor_sms'), '', 0));
127
 
128
        if (!get_config('factor_sms', 'usecredchain')) {
129
            // AWS Settings.
130
            $settings->add(new \admin_setting_configtext('factor_sms/api_key',
131
                get_string('settings:aws:key', 'factor_sms'),
132
                get_string('settings:aws:key_help', 'factor_sms'), ''));
133
 
134
            $settings->add(new \admin_setting_configpasswordunmask('factor_sms/api_secret',
135
                get_string('settings:aws:secret', 'factor_sms'),
136
                get_string('settings:aws:secret_help', 'factor_sms'), ''));
137
        }
138
 
139
        $settings->add(new admin_settings_aws_region('factor_sms/api_region',
140
            get_string('settings:aws:region', 'factor_sms'),
141
            get_string('settings:aws:region_help', 'factor_sms'),
142
            'ap-southeast-2'));
143
    }
144
 
145
    /**
146
     * Returns whether or not the gateway is enabled
147
     *
148
     * @return  bool
149
     */
150
    public static function is_gateway_enabled(): bool {
151
        return true;
152
    }
153
}