-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuk_co_compucorp_amazonsns.php
executable file
·210 lines (183 loc) · 5.88 KB
/
uk_co_compucorp_amazonsns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
use \Aws\Sns\SnsClient as SNSClient;
/**
* Class uk_co_compucorp_amazonsns implements SMS Provider methods required to
* validate and send SMS messages.
*/
class uk_co_compucorp_amazonsns extends CRM_SMS_Provider {
/**
* Unique Instance of uk_co_compucorp_amazonsns to be used on SMS sending
* operations.
*
* @var object
*/
static private $_singleton;
/**
* Default API URL, moot in the case of this extension, as actual URL is
* determined by AWS SDK, but required by CiviCRM.
*
* @var string
*/
public $_apiURL = 'https://aws.amazon.com';
/**
* AWS SDK Client to send SMS messages via Amazon SNS.
*
* @var \Aws\Sns\SnsClient
*/
private $snsClient;
/**
* Sender ID to be used on SMS messages. Should be an alphanumeric string,
* with at least one letter.
*
* @var string
*/
private $senderID;
/**
* Maximum number of characters in an SMS message.
*/
const MAX_SMS_CHAR = 160;
/**
* uk_co_compucorp_amazonsns constructor.
*
* @param array $providerParameters
*/
public function __construct($providerParameters) {
if ($this->validateProviderParameters($providerParameters)) {
$params = array(
'credentials' => array(
'key' => $providerParameters['username'],
'secret' => $providerParameters['password'],
),
'region' => $providerParameters['api_params']['region'],
'version' => $providerParameters['api_params']['version'],
);
$this->snsClient = new SNSClient($params);
}
if (!empty($providerParameters['api_params']['SenderID'])) {
$this->senderID = $providerParameters['api_params']['SenderID'];
}
}
/**
* Validates if all reuired provider parameters are set.
*
* @param array $parameters
*
* @return bool
* True if all required parameters are given, false otherwise
*/
private function validateProviderParameters($parameters) {
switch (true) {
case empty($parameters['username']):
case empty($parameters['password']):
case empty($parameters['api_params']['region']):
case empty($parameters['api_params']['version']):
$valid = FALSE;
break;
default:
$valid = TRUE;
}
return $valid;
}
/**
* Creates unique instance of the class to be used on all SMS opertions.
*
* @param array $providerParams
* @param bool $force
*
* @return object|\uk_co_compucorp_amazonsns
*/
public static function &singleton($providerParams = array(), $force = FALSE) {
if (!isset(self::$_singleton) || $force) {
$provider = array();
$providerID = CRM_Utils_Array::value('provider_id', $providerParams);
if ($providerID) {
$provider = CRM_SMS_BAO_Provider::getProviderInfo($providerID);
}
self::$_singleton = new uk_co_compucorp_amazonsns($provider);
}
return self::$_singleton;
}
/**
* Sends an SMS message to the given phone number through Amazon SNS.
*
* @param array $recipients
* Phone number to where the SMS messge will be sent
* @param string $header
* Headers for SMS message to be sent
* @param string $message
* Content of SMS message to be sent
* @param null $jobID
* ID of mailing job to which the SMS message is associated, if sent as part
* of a batch SMS job.
* @param null $userID
* ID of user that is sending the SMS message, if doing a single SMS sending
*
* @return PEAR_Error|int
* Pear error object if an error is found, otherwise the message ID
* generated by Amazon SNS
*/
public function send($recipients, $header, $message, $jobID = NULL, $userID = NULL) {
if (!$this->validatePhoneNumber($recipients)) {
return PEAR::raiseError(
'The phone number ' . $recipients . ' does not comply with the E.164 format! SMS sending not possible. <a href="http://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html" target="_blank">More information...</a>',
0,
PEAR_ERROR_RETURN
);
}
$messageParams = $this->buildMessageParameters($jobID);
$messageParams['Message'] = $message;
$messageParams['PhoneNumber'] = $recipients;
try {
$result = $this->snsClient->publish($messageParams);
$messageID = $result->get('MessageId');
$this->createActivity($messageID, $message, $header, $jobID, $userID);
} catch(Aws\Sns\Exception\SnsException $e) {
return PEAR::raiseError(
'Error Sending SMS through Amazon SNS [' . $e->getAwsErrorCode() . ']' . ':' . ' - ' . $e->getAwsErrorMessage(),
$e->getAwsErrorCode(),
PEAR_ERROR_RETURN
);
}
return $messageID;
}
private function buildMessageParameters($jobID) {
$messageParams = array();
if (!empty($this->senderID)) {
$messageParams['SenderID'] = $this->senderID;
}
if (!empty($jobID)) {
$mailingData = civicrm_api3('MailingJob', 'get', array(
'id' => $jobID,
'api.Mailing.getvalue' => array(
'id' => '$value.mailing_id',
'return' => 'template_options',
),
));
$messageParams['SMSType'] = $mailingData['values'][$jobID]['api.Mailing.getvalue']['sms_type'] ?: 'Promotional';
} else {
$messageParams['SMSType'] = CRM_Utils_Request::retrieve('sms_type', 'String') ?: 'Promotional';
}
$messageParams['MessageAttributes'] = array(
'AWS.SNS.SMS.SenderID' => array(
'DataType' => 'String',
'StringValue' => $this->senderID
),
'AWS.SNS.SMS.SMSType' => array(
'DataType' => 'String',
'StringValue' => $messageParams['SMSType']
)
);
return $messageParams;
}
/**
* Validate given pone number's format.
*
* @param $phone
*
* @return bool
* True if valid, false otherwise.
*/
private function validatePhoneNumber($phone) {
return CRM_Amazonsns_SMS_PhoneValidator::validatePhoneNumber($phone);
}
}