sms_simplegateway.module in SMS Framework 6
Simple gateway module for Drupal SMS Framework. Outbound+Inbound
For inbound messaging you must configure your gateway to send messages to:
- http(s)://yourhost.example.com/sms/simplegateway/receiver
@package sms @subpackage sms_simplegateway
File
modules/sms_simplegateway/sms_simplegateway.moduleView source
<?php
/**
* @file
* Simple gateway module for Drupal SMS Framework. Outbound+Inbound
*
* For inbound messaging you must configure your gateway to send messages to:
* - http(s)://yourhost.example.com/sms/simplegateway/receiver
*
* @package sms
* @subpackage sms_simplegateway
*/
/**
* Implement hook_gateway_info()
*
* @ingroup hooks
*/
function sms_simplegateway_gateway_info() {
return array(
'simplegateway' => array(
'name' => 'Simple gateway',
'send' => 'sms_simplegateway_send',
'configure form' => 'sms_simplegateway_admin_form',
),
);
}
/**
* Implement hook_menu()
*
* @ingroup hooks
*/
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
* 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_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Base URL for sending messages'),
'#description' => t('Eg: http://simplegateway.example.com:13031/sendsms'),
'#size' => 60,
'#maxlength' => 255,
'#default_value' => $configuration['sms_simplegateway_base_url'],
);
$form['sms_simplegateway_send']['sms_simplegateway_method'] = array(
'#type' => 'radios',
'#title' => t('HTTP method'),
'#default_value' => $configuration['sms_simplegateway_method'],
'#options' => array(
'GET' => 'GET',
'POST' => 'POST',
),
);
$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' => $configuration['sms_simplegateway_user_field'],
);
$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' => $configuration['sms_simplegateway_user_value'],
);
$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' => $configuration['sms_simplegateway_pass_field'],
);
$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' => $configuration['sms_simplegateway_pass_value'],
);
$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' => $configuration['sms_simplegateway_sender_field'],
);
$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' => $configuration['sms_simplegateway_number_field'],
);
$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' => $configuration['sms_simplegateway_message_field'],
);
$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',
'#value' => url('sms/simplegateway/receiver', array(
'absolute' => TRUE,
)),
);
$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' => $configuration['sms_simplegateway_recv_number_field'],
);
$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' => $configuration['sms_simplegateway_recv_gwnumber_field'],
);
$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' => $configuration['sms_simplegateway_recv_message_field'],
);
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
* Response array.
*/
function sms_simplegateway_send($number, $message, $options) {
// Get config
$gateway = sms_gateways('gateway', 'simplegateway');
$config = $gateway['configuration'];
// Prepare the URL and get the method
$url_base = $config['sms_simplegateway_base_url'];
$method = $config['sms_simplegateway_method'];
// Lets specify a gw number
$sender = '';
if (array_key_exists('sender', $options)) {
$sender = $options['sender'];
}
// Prepare required arguments
$params = array();
$user_field = $config['sms_simplegateway_user_field'];
$user = $config['sms_simplegateway_user_value'];
if (!empty($user_field)) {
$params[$user_field] = $user;
}
$pass_field = $config['sms_simplegateway_pass_field'];
$pass = $config['sms_simplegateway_pass_value'];
if (!empty($pass_field)) {
$params[$pass_field] = $pass;
}
$sender_field = $config['sms_simplegateway_sender_field'];
if (!empty($sender_field) && !empty($sender)) {
$params[$sender_field] = $sender;
}
$number_field = $config['sms_simplegateway_number_field'];
$params[$number_field] = $number;
$message_field = $config['sms_simplegateway_message_field'];
$params[$message_field] = $message;
// Prepare the query string
// Note that we are forcing http_build_query to use '&' as a separator instead of the usual '&'
$query_string = http_build_query($params, NULL, '&');
if ($method == 'GET') {
$url = $url_base . '?' . $query_string;
$http_result = drupal_http_request($url, array(), 'GET');
}
elseif ($method == 'POST') {
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$http_result = drupal_http_request($url_base, $headers, 'POST', $query_string);
}
// 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) {
// Test the HTTP return code
if ($http_result->code >= 200 && $http_result->code <= 299) {
// Prepare a good response array
$result = 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
$result = array(
'status' => FALSE,
'status_code' => SMS_GW_ERR_OTHER,
'gateway_status_code' => $http_result->code,
'gateway_status_text' => $http_result->data,
);
}
}
return $result;
}
/**
* Receive an SMS message and pass it into the SMS Framework
*/
function sms_simplegateway_receive_message() {
// Get config
$gateway = sms_gateways('gateway', 'simplegateway');
$config = $gateway['configuration'];
$number_field = $config['sms_simplegateway_recv_number_field'];
$gwnumber_field = $config['sms_simplegateway_recv_gwnumber_field'];
$message_field = $config['sms_simplegateway_recv_message_field'];
$number = $_REQUEST[$number_field];
$message = $_REQUEST[$message_field];
$options = array();
// Define raw gateway response parameters
$options['gateway_params'] = array();
// Define message receiver if possible
if (array_key_exists($gwnumber_field, $_REQUEST) && !empty($_REQUEST[$gwnumber_field])) {
$options['receiver'] = $_REQUEST[$gwnumber_field];
}
sms_incoming($number, $message, $options);
}
Functions
Name | Description |
---|---|
sms_simplegateway_admin_form | Configuration form for gateway module |
sms_simplegateway_gateway_info | Implement hook_gateway_info() |
sms_simplegateway_menu | Implement hook_menu() |
sms_simplegateway_receive_message | Receive an SMS message and pass it into the SMS Framework |
sms_simplegateway_send | Send a message |