forked from budda/SMS_Mediaburst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsms_mediaburst.module
More file actions
434 lines (402 loc) · 12.4 KB
/
sms_mediaburst.module
File metadata and controls
434 lines (402 loc) · 12.4 KB
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
434
<?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'],
);
$form['sms_mediaburst_from'] = array(
'#type' => 'textfield',
'#title' => t('From'),
'#description' => t('Number (max 12 digits) or Name (max 11 characters) SMS comes from.'),
'#size' => 12,
'#maxlength' => 12,
'#default_value' => $configuration['sms_mediaburst_from'],
);
$form['sms_mediaburst_concat'] = array(
'#type' => 'checkbox',
'#title' => t('Support long messages'),
'#description' => t('Send messages longer than 160 characters. May cost up to 3 SMS credits per recipient'),
'#default_value' => $configuration['sms_mediaburst_concat'],
);
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);
$smsfrom = $form_state['values']['sms_mediaburst_from'];
if (ctype_digit($smsfrom) && strlen($smsfrom) > 12) {
form_set_error('sms_mediaburst_from', t('Numeric From value can be max 12 digits.'));
} elseif (strlen($smsfrom) > 11) {
form_set_error('sms_mediaburst_from', t('From value can be max 11 characters.'));
}
}
/**
* 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
$number = preg_replace("/\D/","",$number);
// Remove leading international prefix zeros
$number = preg_replace("/^00/","",$number);
// Prefix the number with the country code if provided and not already at the start of the number
if (isset($options['country']) && preg_match("/^0/", $number)) {
// 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 only contains GSM characters
if (sms_mediaburst_is_gsm($data['message'])) {
$message = rawurlencode($data['message']);
} else {
$message = rawurlencode($data['message'])."&msgtype=UCS2";
}
$query = '&to='. rawurlencode($data['number']) .'&content='. $message;
if ($config['sms_mediaburst_concat']) {
$query.="&concat=3";
}
if ($config['sms_mediaburst_from']) {
$query.="&from=".rawurlencode($config['sms_mediaburst_from']);
}
$command = 'send';
break;
case 'getbalance':
$command = 'credit';
break;
}
// Add authentication to all queries
$auth = '?username='. rawurlencode($config['sms_mediaburst_user']) .'&password='. rawurlencode($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;
}
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",
);
}
/*
* Check whether a message only contains characters
* from the GSM03.38 character set
*/
function sms_mediaburst_is_gsm($message) {
return preg_match("/^[\x{0040}\x{00A3}\x{0024}\x{00A5}\x{00E8}\x{00E9}\x{00F9}\x{00EC}\x{00F2}\x{00C7}\x{000A}\x{00D8}\x{00F8}\x{000D}\x{00C5}\x{00E5}\x{0394}\x{005F}\x{03A6}\x{0393}\x{039B}\x{03A9}\x{03A0}\x{03A8}\x{03A3}\x{0398}\x{039E}\x{00C6}\x{00E6}\x{00DF}\x{00C9}\x{0020}\x{0021}\x{0022}\x{0023}\x{00A4}\x{0025}\x{0026}\x{0027}\x{0028}\x{0029}\x{002A}\x{002B}\x{002C}\x{002D}\x{002E}\x{002F}\x{0030}\x{0031}\x{0032}\x{0033}\x{0034}\x{0035}\x{0036}\x{0037}\x{0038}\x{0039}\x{003A}\x{003B}\x{003C}\x{003D}\x{003E}\x{003F}\x{00A1}\x{0041}\x{0042}\x{0043}\x{0044}\x{0045}\x{0046}\x{0047}\x{0048}\x{0049}\x{004A}\x{004B}\x{004C}\x{004D}\x{004E}\x{004F}\x{0050}\x{0051}\x{0052}\x{0053}\x{0054}\x{0055}\x{0056}\x{0057}\x{0058}\x{0059}\x{005A}\x{00C4}\x{00D6}\x{00D1}\x{00DC}\x{00A7}\x{00BF}\x{0061}\x{0062}\x{0063}\x{0064}\x{0065}\x{0066}\x{0067}\x{0068}\x{0069}\x{006A}\x{006B}\x{006C}\x{006D}\x{006E}\x{006F}\x{0070}\x{0071}\x{0072}\x{0073}\x{0074}\x{0075}\x{0076}\x{0077}\x{0078}\x{0079}\x{007A}\x{00E4}\x{00F6}\x{00F1}\x{00FC}\x{00E0}\x{20AC}\x{005B}\x{005C}\x{005D}\x{005E}\x{007B}\x{007C}\x{007D}\x{007E}]+$/u",$message);
}