sms_clickatell.module in SMS Framework 5
Same filename and directory in other branches
Adds support for sending SMS messages using the Clickatell gateway.
File
modules/sms_clickatell/sms_clickatell.moduleView source
<?php
/**
* @file
* Adds support for sending SMS messages using the Clickatell gateway.
*/
/**
* Implementation of hook_gateway_info().
*/
function sms_clickatell_gateway_info() {
return array(
'clickatell' => array(
'name' => 'Clickatell',
'configure form' => 'sms_clickatell_admin_form',
'send' => 'sms_clickatell_send',
'send form' => 'sms_clickatell_send_form',
),
);
}
function sms_clickatell_admin_form($configuration) {
$form['sms_clickatell_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_clickatell_ssl'],
);
$form['sms_clickatell_api_id'] = array(
'#type' => 'textfield',
'#title' => t('API ID'),
'#description' => t('Clickatell issues this number upon addition of an HTTP sub-product to your account.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => $configuration['sms_clickatell_api_id'],
);
$form['sms_clickatell_user'] = array(
'#type' => 'textfield',
'#title' => t('User'),
'#description' => t('The username of your Clickatell account.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => $configuration['sms_clickatell_user'],
);
$form['sms_clickatell_password'] = array(
'#type' => 'textfield',
'#title' => t('Password'),
'#description' => t('The current password on your Clickatell account.'),
'#size' => 30,
'#maxlength' => 64,
'#default_value' => $configuration['sms_clickatell_password'],
);
return $form;
}
/**
* Validates the submission of the configuration form.
*/
function sms_clickatell_admin_form_validate($form_id, $form_values) {
$result = sms_clickatell_command('auth', array(), $form_values);
if (!$result['status']) {
form_set_error('', t('A Clickatell gateway error occured: @error.', array(
'@error' => $result['message'],
)));
}
variable_set('sms_clickatell_session_id_timestamp', 0);
}
/**
* Returns custom additions to be added to the send forms
*/
function sms_clickatell_send_form() {
$form['country'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#multiple' => FALSE,
'#options' => sms_clickatell_country_codes(),
'#default_value' => -1,
);
return $form;
}
/**
* Callback for sending messages.
*/
function sms_clickatell_send($number, $message, $options) {
$number = $options['country'] . $number;
return sms_clickatell_command('sendmsg', array(
'number' => $number,
'message' => $message,
));
}
function sms_clickatell_balance() {
$result = sms_clickatell_command('getbalance');
return $result['data'];
}
/**
* Executes a command using the Clickatell API
*/
function sms_clickatell_command($command = 'auth', $data = array(), $config = NULL) {
$gateway = sms_gateways('gateway', 'clickatell');
if ($config == NULL) {
$config = $gateway['configuration'];
}
if ($config['sms_clickatell_ssl']) {
$scheme = 'https';
}
else {
$scheme = 'http';
}
switch ($command) {
case 'auth':
$query = 'api_id=' . $config['sms_clickatell_api_id'] . '&user=' . $config['sms_clickatell_user'] . '&password=' . $config['sms_clickatell_password'];
break;
case 'sendmsg':
// Check if the message requires unicode handling
if ($unicode_message = sms_clickatell_unicode($data['message'])) {
$message = $unicode_message;
}
else {
$message = drupal_urlencode($data['message']);
}
$query = 'session_id=' . sms_clickatell_get_session_id() . '&to=' . $data['number'] . '&text=' . $message;
break;
case 'getbalance':
$query = 'session_id=' . sms_clickatell_get_session_id();
break;
}
// Run the command
$http_result = drupal_http_request($scheme . '://api.clickatell.com/http/' . $command . '?' . $query);
// 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 Clickatell errors
if (strpos($http_result->data, 'ERR') !== FALSE) {
$result = array(
'status' => FALSE,
'message' => $http_result->data,
);
}
elseif ($command == 'auth') {
// Add Clickatell session ID to result array.
list($status, $sid) = explode(': ', $http_result->data);
$result = array(
'status' => TRUE,
'sid' => $sid,
);
}
else {
$result = array(
'status' => TRUE,
);
}
}
return $result;
}
function sms_clickatell_get_session_id() {
if (variable_get('sms_clickatell_session_id_timestamp', 0) < strtotime('-10 mins')) {
if ($result = sms_clickatell_command()) {
if ($result['status']) {
variable_set('sms_clickatell_session_id', $result['sid']);
variable_set('sms_clickatell_session_id_timestamp', time());
watchdog('sms', t('Clickatell session ID refreshed: %sid', array(
'%sid' => $result['sid'],
)));
}
}
}
return variable_get('sms_clickatell_session_id', 0);
}
/**
* Returns an array of error codes and messages that are generated by the Clickatell gateway
*/
function sms_clickatell_error_codes() {
return array(
01 => 'Authentication failed',
02 => 'Unknown username or password',
03 => 'Session ID expired',
04 => 'Account frozen',
05 => 'Missing session ID',
07 => '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_clickatell_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_clickatell_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;
}
Functions
Name | Description |
---|---|
sms_clickatell_admin_form | |
sms_clickatell_admin_form_validate | Validates the submission of the configuration form. |
sms_clickatell_balance | |
sms_clickatell_command | Executes a command using the Clickatell API |
sms_clickatell_country_codes | |
sms_clickatell_error_codes | Returns an array of error codes and messages that are generated by the Clickatell gateway |
sms_clickatell_gateway_info | Implementation of hook_gateway_info(). |
sms_clickatell_get_session_id | |
sms_clickatell_send | Callback for sending messages. |
sms_clickatell_send_form | Returns custom additions to be added to the send forms |
sms_clickatell_unicode | Converts a string to USC-2 encoding if neccessary. |