sms_simplegateway.module in SMS simple gateway 7
Same filename and directory in other branches
Simple gateway module for Drupal SMS Framework. Outbound + Inbound
@package sms @subpackage sms_simplegateway
File
sms_simplegateway.moduleView source
<?php
/**
* @file
* Simple gateway module for Drupal SMS Framework. Outbound + Inbound
*
* @package sms
* @subpackage sms_simplegateway
*/
/**
* Implements hook_gateway_info().
*/
function sms_simplegateway_gateway_info() {
return array(
'simplegateway' => array(
'name' => 'Simple gateway',
'send' => 'sms_simplegateway_send',
'configure form' => 'sms_simplegateway_admin_form',
),
);
}
/**
* Implements hook_menu().
*/
function sms_simplegateway_menu() {
$items = array();
$items['sms/simplegateway/receiver'] = array(
'title' => 'Simple gateway SMS message receiver',
'page callback' => 'sms_simplegateway_receive_message',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Configuration form for gateway module
*
* @param $configuration
*
* @return array
* Drupal form array
*/
function sms_simplegateway_admin_form($configuration) {
$form['sms_simplegateway_send'] = array(
'#type' => 'fieldset',
'#title' => 'Sender (outgoing messages)',
'#collapsible' => TRUE,
);
$form['sms_simplegateway_send']['sms_simplegateway_method'] = array(
'#type' => 'radios',
'#title' => t('HTTP method'),
'#default_value' => isset($configuration['sms_simplegateway_method']) ? $configuration['sms_simplegateway_method'] : NULL,
'#options' => array(
'GET' => 'GET',
'POST' => 'POST',
),
);
$form['sms_simplegateway_send']['sms_simplegateway_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Base URL for sending messages'),
'#description' => t('Eg: http://simplegateway.example.com:13031/sendsms'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_base_url']) ? $configuration['sms_simplegateway_base_url'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_user_field'] = array(
'#type' => 'textfield',
'#title' => t('Username field name'),
'#description' => t('Optional. The argument/field name for the field that holds the username. Eg: user, username, authid.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_user_field']) ? $configuration['sms_simplegateway_user_field'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_user_value'] = array(
'#type' => 'textfield',
'#title' => t('Username field value'),
'#description' => t('Optional. Your username for this gateway account.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_user_value']) ? $configuration['sms_simplegateway_user_value'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_pass_field'] = array(
'#type' => 'textfield',
'#title' => t('Password field name'),
'#description' => t('Optional. The argument/field name for the field that holds the password. Eg: pass, password, passwd.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_pass_field']) ? $configuration['sms_simplegateway_pass_field'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_pass_value'] = array(
'#type' => 'textfield',
'#title' => t('Password field value'),
'#description' => t('Optional. Your password for this gateway account.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_pass_value']) ? $configuration['sms_simplegateway_pass_value'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_sender_field'] = array(
'#type' => 'textfield',
'#title' => t('Sender (from) field name'),
'#description' => t('The argument/field name for the field that holds the sender number data. Eg: from, sender'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_sender_field']) ? $configuration['sms_simplegateway_sender_field'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_sender_value'] = array(
'#type' => 'textfield',
'#title' => t('Sender (from) field value'),
'#description' => t('The default value for the sender.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_sender_value']) ? $configuration['sms_simplegateway_sender_value'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_number_field'] = array(
'#type' => 'textfield',
'#title' => t('Number (to) field name'),
'#description' => t('The argument/field name for the field that holds the number data. Eg: number, to, no'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_number_field']) ? $configuration['sms_simplegateway_number_field'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_number_prefix_value'] = array(
'#type' => 'textfield',
'#title' => t('Number (to) prefix value'),
'#description' => t('The value to be prefixed to the sender. Eg: country code'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_number_prefix_value']) ? $configuration['sms_simplegateway_number_prefix_value'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_message_field'] = array(
'#type' => 'textfield',
'#title' => t('Message field name'),
'#description' => t('The argument/field name for the field that holds the message text. Eg: message, text, content'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_message_field']) ? $configuration['sms_simplegateway_message_field'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_extra_params'] = array(
'#type' => 'textfield',
'#title' => t('Extra Parameters'),
'#description' => t('Any additional parameters the gateway may need. Eg: route=1'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_extra_params']) ? $configuration['sms_simplegateway_extra_params'] : NULL,
);
$form['sms_simplegateway_send']['sms_simplegateway_authorization'] = array(
'#type' => 'radios',
'#title' => t('Authorization'),
'#description' => t('For POST Requests Only. use "Username field value" & "Password field value" to specify credentials'),
'#default_value' => isset($configuration['sms_simplegateway_authorization']) ? $configuration['sms_simplegateway_authorization'] : NULL,
'#options' => array(
'none' => 'NONE',
'basic' => 'BASIC',
'digest' => 'DIGEST',
'ntlm' => 'NTLM',
),
);
$form['sms_simplegateway_send']['sms_simplegateway_content_type'] = array(
'#type' => 'radios',
'#title' => t('Content Encoding'),
'#description' => t('For POST Requests Only.'),
'#default_value' => isset($configuration['sms_simplegateway_content_type']) ? $configuration['sms_simplegateway_content_type'] : NULL,
'#options' => array(
'plain' => 'Plain',
'json' => 'Json',
),
);
$form['sms_simplegateway_receive'] = array(
'#type' => 'fieldset',
'#title' => 'Receiver (incoming messages)',
'#collapsible' => TRUE,
);
$form['sms_simplegateway_receive']['sms_simplegateway_recv_url'] = array(
'#type' => 'item',
'#title' => 'Target URL for the message receiver',
'#markup' => url('sms/simplegateway/receiver', array(
'absolute' => TRUE,
)),
);
$form['sms_simplegateway_receive']['sms_simplegateway_recv_content_type'] = array(
'#type' => 'radios',
'#title' => t('Content Encoding'),
'#description' => t('For POST Requests Only.'),
'#default_value' => isset($configuration['sms_simplegateway_recv_content_type']) ? $configuration['sms_simplegateway_recv_content_type'] : NULL,
'#options' => array(
'plain' => 'Plain',
'json' => 'Json',
),
);
$form['sms_simplegateway_receive']['sms_simplegateway_recv_number_field'] = array(
'#type' => 'textfield',
'#title' => t('Sender (from) field name'),
'#description' => t('The argument/field name for the field that holds the sender number. Eg: sender, from.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_recv_number_field']) ? $configuration['sms_simplegateway_recv_number_field'] : NULL,
);
$form['sms_simplegateway_receive']['sms_simplegateway_recv_gwnumber_field'] = array(
'#type' => 'textfield',
'#title' => t('Receiver (to) field name'),
'#description' => t('Optional. The argument/field name for the field that holds the gateway receiver number. Eg: to, inNumber, receiver.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_recv_gwnumber_field']) ? $configuration['sms_simplegateway_recv_gwnumber_field'] : NULL,
);
$form['sms_simplegateway_receive']['sms_simplegateway_recv_message_field'] = array(
'#type' => 'textfield',
'#title' => t('Message field name'),
'#description' => t('The argument/field name for the field that holds the message. Eg: message, text, content.'),
'#size' => 40,
'#maxlength' => 255,
'#default_value' => isset($configuration['sms_simplegateway_recv_message_field']) ? $configuration['sms_simplegateway_recv_message_field'] : NULL,
);
return $form;
}
/**
* Send a message
*
* @param $number
* MSISDN of message recipient. Expected to include the country code prefix.
* @param $message
* Message body text.
* @param $options
* Options array from SMS Framework.
*
* @return \stdClass Response array.
* Response array.
*/
function sms_simplegateway_send($number, $message, $options) {
$gateway = sms_gateways('gateway', 'simplegateway');
$config = $gateway['configuration'];
if (is_array($options) and array_key_exists('sender', $options)) {
$config['sms_simplegateway_sender_value'] = $options['sender'];
}
$body = array_filter(array(
$config['sms_simplegateway_user_field'] => $config['sms_simplegateway_user_value'],
$config['sms_simplegateway_pass_field'] => $config['sms_simplegateway_pass_value'],
$config['sms_simplegateway_sender_field'] => $config['sms_simplegateway_sender_value'],
$config['sms_simplegateway_number_field'] => $config['sms_simplegateway_number_prefix_value'] . $number,
$config['sms_simplegateway_message_field'] => $message,
));
switch ($config['sms_simplegateway_method']) {
case 'GET':
$body = http_build_query($body) . '&' . $config['sms_simplegateway_extra_params'];
$http_result = drupal_http_request($config['sms_simplegateway_base_url'] . '?' . $body);
break;
case 'POST':
$headers = array();
switch ($config['sms_simplegateway_authorization']) {
case 'basic':
$headers += array(
'Authorization' => 'Basic ' . base64_encode($config['sms_simplegateway_user_value'] . ':' . $config['sms_simplegateway_pass_value']),
);
break;
case 'digest':
$headers += array(
'Authorization' => 'Digest ' . base64_encode($config['sms_simplegateway_user_value'] . ':' . $config['sms_simplegateway_pass_value']),
);
break;
case 'ntlm':
$headers += array(
'Authorization' => 'Ntlm ' . base64_encode($config['sms_simplegateway_user_value'] . ':' . $config['sms_simplegateway_pass_value']),
);
break;
}
switch ($config['sms_simplegateway_content_type']) {
case 'plain':
$headers += array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$body = http_build_query($body) . '&' . $config['sms_simplegateway_extra_params'];
break;
case 'json':
$headers += array(
'Content-Type' => 'application/json',
);
$body = json_encode($body);
break;
}
$http_result = drupal_http_request($config['sms_simplegateway_base_url'], array(
'method' => 'POST',
'headers' => $headers,
'data' => $body,
));
break;
}
// Check for HTTP errors
if (property_exists($http_result, 'error') && ($http_result->code <= 200 || $http_result->code >= 299)) {
return array(
'status' => FALSE,
'message' => t('An error occurred during the HTTP request: @error', array(
'@error' => $http_result->error,
)),
);
}
if (property_exists($http_result, 'data')) {
// Test the HTTP return code
if ($http_result->code >= 200 && $http_result->code <= 299) {
// Prepare a good response array
return array(
'status' => TRUE,
'status_code' => SMS_GW_OK,
'gateway_status_code' => $http_result->code,
'gateway_status_text' => $http_result->data,
);
}
else {
// We got a (possibly) bad response code
return array(
'status' => FALSE,
'status_code' => SMS_GW_ERR_OTHER,
'gateway_status_code' => $http_result->code,
'gateway_status_text' => $http_result->data,
);
}
}
}
/**
* Receive an SMS message and pass it into the SMS Framework
*/
function sms_simplegateway_receive_message() {
$gateway = sms_gateways('gateway', 'simplegateway');
$config = $gateway['configuration'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($config['sms_simplegateway_recv_content_type'] == 'json') {
$data = (array) json_decode(file_get_contents('php://input'));
}
else {
$data = $_POST;
}
}
else {
$data = $_GET;
}
sms_incoming($data[$config['sms_simplegateway_recv_number_field']], $data[$config['sms_simplegateway_recv_message_field']], array(
'gateway_params' => array(),
'receiver' => $data[$config['sms_simplegateway_recv_gwnumber_field']],
));
drupal_exit();
}
Functions
Name | Description |
---|---|
sms_simplegateway_admin_form | Configuration form for gateway module |
sms_simplegateway_gateway_info | Implements hook_gateway_info(). |
sms_simplegateway_menu | Implements hook_menu(). |
sms_simplegateway_receive_message | Receive an SMS message and pass it into the SMS Framework |
sms_simplegateway_send | Send a message |