-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsms_mediaburst.module
433 lines (404 loc) · 11.4 KB
/
sms_mediaburst.module
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
// Developed by Mike Carter / Budda / Ixis
/**
* @file
* Adds support for sending SMS messages using the Mediaburst gateway.
*/
/**
* Implementation of hook_gateway_info().
*/
function sms_mediaburst_gateway_info() {
return array(
'mediaburst' => array(
'name' => 'MediaBurst',
'configure form' => 'sms_mediaburst_admin_form',
'send' => 'sms_mediaburst_send',
'send form' => 'sms_mediaburst_send_form',
),
);
}
/**
* Gateway configuration form
*
* @access public
* @param mixed $configuration
* @return void
*/
function sms_mediaburst_admin_form($configuration) {
$form['sms_mediaburst_balance'] = array(
'#type' => 'item',
'#title' => t('Current balance'),
'#value' => sms_mediaburst_balance(),
);
$form['sms_mediaburst_ssl'] = array(
'#type' => 'checkbox',
'#title' => t('Use SSL Encyption'),
'#description' => t('Drupal\'s built-in HTTP client only supports SSL on PHP 4.3 compiled with OpenSSL.'),
'#default_value' => $configuration['sms_mediaburst_ssl'],
);
$form['sms_mediaburst_user'] = array(
'#type' => 'textfield',
'#title' => t('User'),
'#description' => t('The username of your MediaBurst account.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => $configuration['sms_mediaburst_user'],
);
$form['sms_mediaburst_password'] = array(
'#type' => 'textfield',
'#title' => t('Password'),
'#description' => t('The current password on your MediaBurst account.'),
'#size' => 30,
'#maxlength' => 64,
'#default_value' => $configuration['sms_mediaburst_password'],
);
return $form;
}
/**
* Validates the submission of the configuration form.
*/
function sms_mediaburst_admin_form_validate($form, &$form_state) {
$result = sms_mediaburst_command('credit', array(), $form_state['values']);
if (!$result['status']) {
form_set_error('', t('A MediaBurst gateway error occured: @error.', array('@error' => $result['message'])));
}
variable_set('sms_mediaburst_session_id_timestamp', 0);
}
/**
* Returns custom additions to be added to the send forms
*/
function sms_mediaburst_send_form() {
$form['country'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#multiple' => FALSE,
'#options' => sms_mediaburst_country_codes(),
'#default_value' => -1,
);
return $form;
}
/**
* Callback for sending messages.
*/
function sms_mediaburst_send($number, $message, $options) {
global $user;
// Strip out any bad characters - probably better as a regular expression for numerics only!
$number = str_replace(array(' ', '+'), '', $number);
// Prefix the number with the country code if provided and not already at the start of the number
if (isset($options['country'])) {
// Remove any prefix numbers
$number = $options['country'] . ltrim($number, '0');
}
return sms_mediaburst_command('sendmsg', array('number' => $number, 'message' => $message));
}
/**
* Callback for a balance enquiry
*
* @access public
* @return void
*/
function sms_mediaburst_balance() {
$result = sms_mediaburst_command('getbalance');
return str_replace('Current Credit: ', '', $result['data']);
}
/**
* Executes a command using the MediaBurst API
*/
function sms_mediaburst_command($command, $data = array(), $config = NULL) {
$gateway = sms_gateways('gateway', 'mediaburst');
if ($config == NULL) {
$config = $gateway['configuration'];
}
if ($config['sms_mediaburst_ssl']) {
$scheme = 'https';
}
else {
$scheme = 'http';
}
switch ($command) {
case 'sendmsg':
// Check if the message requires unicode handling
if ($unicode_message = sms_mediaburst_unicode($data['message'])) {
$message = $unicode_message;
}
else {
$message = drupal_urlencode($data['message']);
}
$query = '&to='. $data['number'] .'&Content='. $message;
$command = 'send';
break;
case 'getbalance':
$command = 'credit';
break;
}
// Add authentication to all queries
$auth = '?user='. $config['sms_mediaburst_user'] .'&password='. $config['sms_mediaburst_password'];
// Run the command
$url = $scheme .'://sms.message-platform.com/http/'. $command .'.aspx'. $auth . $query;
watchdog('sms-mb', 'Sending ' . $url, NULL, WATCHDOG_DEBUG); //!@debug
$http_result = drupal_http_request($url);
// Check for HTTP errors
if ($http_result->error) {
return array('status' => FALSE, 'message' => t('An error occured during the HTTP request: @error', array('@error' => $http_result->error)));
}
if ($http_result->data) {
// Check for Mediaburst errors
if (strpos($http_result->data, 'Error') !== FALSE) {
$result = array('status' => FALSE, 'message' => $http_result->data);
}
else {
$result = array('status' => TRUE, 'data' => $http_result->data);
}
}
watchdog('sms-mb', 'sent: ' . print_r($data, true) . ' received: ' . print_r($result, true), NULL, WATCHDOG_DEBUG); //!@debug
return $result;
}
/**
* Returns an array of error codes and messages that are generated by the Mediaburst gateway
*/
function sms_mediaburst_error_codes() {
return array(
/*
001 => 'Authentication failed', 002 => 'Unknown username or password', 003 => 'Session ID expired',
004 => 'Account frozen', 005 => 'Missing session ID', 007 => 'IP Lockdown violation',
101 => 'Invalid or missing parameters', 102 => 'Invalid user data header',
103 => 'Unknown API message ID', 104 => 'Unknown client message ID',
105 => 'Invalid destination address', 106 => 'Invalid source address', 107 => 'Empty message',
108 => 'Invalid or missing API ID', 109 => 'Missing message ID',
110 => 'Error with email message',
111 => 'Invalid protocol', 112 => 'Invalid message type',
113 => 'Maximum message parts exceeded',
114 => 'Cannot route message',
115 => 'Message expired',
116 => 'Invalid Unicode data', 120 => 'Invalid delivery time',
201 => 'Invalid batch ID', 202 => 'No batch template',
301 => 'No credit left', 302 => 'Max allowed credit',
*/
);
}
function sms_mediaburst_country_codes() {
return array(
93 => "Afghanistan",
355 => "Albania",
213 => "Algeria",
376 => "Andorra",
244 => "Angola",
1264 => "Anguilla",
1268 => "Antigua & Barbuda",
54 => "Argentina",
374 => "Armenia",
297 => "Aruba",
61 => "Australia",
43 => "Austria",
994 => "Azerbaijan",
1242 => "Bahamas",
973 => "Bahrain",
880 => "Bangladesh",
1246 => "Barbados",
375 => "Belarus",
32 => "Belgium",
501 => "Belize",
229 => "Benin",
1441 => "Bermuda",
975 => "Bhutan",
591 => "Bolivia",
387 => "Bosnia-Herzegovina",
267 => "Botswana",
55 => "Brazil",
1284 => "British Virgin Islands",
673 => "Brunei",
359 => "Bulgaria",
226 => "Burkina Faso",
257 => "Burundi",
855 => "Cambodia",
237 => "Cameroon",
34 => "Canary Islands",
238 => "Cape Verde",
1345 => "Cayman Islands",
236 => "Central African Republic",
235 => "Chad",
56 => "Chile",
86 => "China",
57 => "Colombia",
269 => "Comoros",
242 => "Congo",
243 => "Democratic Republic Congo",
682 => "Cook Islands",
385 => "Croatia",
53 => "Cuba",
357 => "Cyprus",
420 => "Czech Republic",
45 => "Denmark",
253 => "Djibouti",
1767 => "Dominica",
670 => "East Timor",
593 => "Ecuador",
20 => "Egypt",
503 => "El Salvador",
240 => "Equatorial Guinea",
372 => "Estonia",
251 => "Ethiopia",
500 => "Falkland Islands",
298 => "Faroe Islands",
679 => "Fiji",
358 => "Finland",
33 => "France",
594 => "French Guiana",
689 => "French Polynesia",
241 => "Gabon",
220 => "Gambia",
995 => "Georgia",
49 => "Germany",
233 => "Ghana",
350 => "Gibraltar",
881 => "Global Mobile Satellite",
30 => "Greece",
299 => "Greenland",
1473 => "Grenada",
590 => "Guadeloupe",
1671 => "Guam",
502 => "Guatemala",
224 => "Guinea",
592 => "Guyana",
509 => "Haiti",
504 => "Honduras",
852 => "HongKong",
36 => "Hungary",
354 => "Iceland",
91 => "India",
62 => "Indonesia",
98 => "Iran",
964 => "Iraq",
353 => "Ireland",
972 => "Israel",
39 => "Italy / Vatican City State",
225 => "Ivory Coast",
1876 => "Jamaica",
81 => "Japan",
962 => "Jordan",
254 => "Kenya",
82 => "Korea (South)",
965 => "Kuwait",
996 => "Kyrgyzstan",
856 => "Lao",
371 => "Latvia",
961 => "Lebanon",
266 => "Lesotho",
231 => "Liberia",
218 => "Libya",
423 => "Liechtenstein",
370 => "Lithuania",
352 => "Luxembourg",
853 => "Macau",
389 => "Macedonia",
261 => "Madagascar",
265 => "Malawi",
60 => "Malaysia",
960 => "Maldives",
223 => "Mali",
356 => "Malta",
596 => "Martinique",
222 => "Mauritania",
230 => "Mauritius",
269 => "Mayotte Island (Comoros)",
52 => "Mexico",
373 => "Moldova",
377 => "Monaco (Kosovo)",
976 => "Mongolia",
382 => "Montenegro",
1664 => "Montserrat",
212 => "Morocco",
258 => "Mozambique",
95 => "Myanmar",
264 => "Namibia",
977 => "Nepal",
31 => "Netherlands",
599 => "Netherlands Antilles",
687 => "New Caledonia",
64 => "New Zealand",
505 => "Nicaragua",
227 => "Niger",
234 => "Nigeria",
47 => "Norway",
968 => "Oman",
92 => "Pakistan",
970 => "Palestine (+970)",
9725 => "Palestine (+9725)",
507 => "Panama",
675 => "Papua New Guinea",
595 => "Paraguay",
51 => "Peru",
63 => "Philippines",
48 => "Poland",
351 => "Portugal",
974 => "Qatar",
262 => "Reunion",
40 => "Romania",
7 => "Russia / Kazakhstan",
250 => "Rwanda",
1670 => "Saipan",
1684 => "Samoa (American)",
685 => "Samoa (Western)",
378 => "San Marino",
882 => "Satellite-Thuraya",
966 => "Saudi Arabia",
221 => "Senegal",
381 => "Serbia",
248 => "Seychelles",
232 => "Sierra Leone",
65 => "Singapore",
421 => "Slovakia",
386 => "Slovenia",
252 => "Somalia",
27 => "South Africa",
34 => "Spain",
94 => "Sri Lanka",
1869 => "St. Kitts And Nevis",
1758 => "St. Lucia",
1784 => "St. Vincent",
249 => "Sudan",
597 => "Suriname",
268 => "Swaziland",
46 => "Sweden",
41 => "Switzerland",
963 => "Syria",
886 => "Taiwan",
992 => "Tajikistan",
255 => "Tanzania",
66 => "Thailand",
228 => "Togo",
676 => "Tonga Islands",
1868 => "Trinidad and Tobago",
216 => "Tunisia",
90 => "Turkey",
993 => "Turkmenistan",
1649 => "Turks and Caicos Islands",
256 => "Uganda",
44 => "UK / Isle of Man / Jersey / Guernsey",
380 => "Ukraine",
971 => "United Arab Emirates",
598 => "Uruguay",
1 => "USA / Canada / Dominican Rep. / Puerto Rico",
998 => "Uzbekistan",
678 => "Vanuatu",
58 => "Venezuela",
84 => "Vietnam",
967 => "Yemen",
260 => "Zambia",
255 => "Zanzibar",
263 => "Zimbabwe",
);
}
/**
* Converts a string to USC-2 encoding if neccessary.
*/
function sms_mediaburst_unicode($message) {
if (function_exists('iconv')) {
$latin = @iconv('UTF-8', 'ISO-8859-1', $message);
if (strcmp($latin, $message)) {
$arr = unpack('H*hex', @iconv('UTF-8', 'UCS-2BE', $message));
return strtoupper($arr['hex']) .'&unicode=1';
}
}
return FALSE;
}