Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks for API compatibility and adding From address #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sms_mediaburst.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; $Id$
name = "MediaBurst"
description = "Allows the SMS Framework to use MediaBurst as a gateway."
name = "Mediaburst"
description = "Allows the SMS Framework to use Mediaburst as a gateway."
package = SMS Framework
dependencies[] = sms
core = 6.x
97 changes: 49 additions & 48 deletions sms_mediaburst.module
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
function sms_mediaburst_gateway_info() {
return array(
'mediaburst' => array(
'name' => 'MediaBurst',
'name' => 'Mediaburst',
'configure form' => 'sms_mediaburst_admin_form',
'send' => 'sms_mediaburst_send',
'send form' => 'sms_mediaburst_send_form',
Expand Down Expand Up @@ -56,6 +56,20 @@ function sms_mediaburst_admin_form($configuration) {
'#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;
}
Expand All @@ -69,6 +83,13 @@ function sms_mediaburst_admin_form_validate($form, &$form_state) {
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.'));
}
}

/**
Expand All @@ -92,11 +113,14 @@ function sms_mediaburst_send_form() {
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);
// 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'])) {
if (isset($options['country']) && preg_match("/^0/", $number)) {
// Remove any prefix numbers
$number = $options['country'] . ltrim($number, '0');
}
Expand All @@ -117,7 +141,7 @@ function sms_mediaburst_balance() {


/**
* Executes a command using the MediaBurst API
* Executes a command using the Mediaburst API
*/
function sms_mediaburst_command($command, $data = array(), $config = NULL) {
$gateway = sms_gateways('gateway', 'mediaburst');
Expand All @@ -135,14 +159,21 @@ function sms_mediaburst_command($command, $data = array(), $config = NULL) {

switch ($command) {
case 'sendmsg':
// Check if the message requires unicode handling
if ($unicode_message = sms_mediaburst_unicode($data['message'])) {
$message = $unicode_message;
// 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";
}
else {
$message = drupal_urlencode($data['message']);
$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']);
}
$query = '&to='. $data['number'] .'&Content='. $message;

$command = 'send';
break;

Expand All @@ -152,7 +183,7 @@ function sms_mediaburst_command($command, $data = array(), $config = NULL) {
}

// Add authentication to all queries
$auth = '?user='. $config['sms_mediaburst_user'] .'&password='. $config['sms_mediaburst_password'];
$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;
Expand Down Expand Up @@ -180,30 +211,6 @@ function sms_mediaburst_command($command, $data = array(), $config = NULL) {
}


/**
* 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",
Expand Down Expand Up @@ -418,16 +425,10 @@ function sms_mediaburst_country_codes() {
);
}

/**
* Converts a string to USC-2 encoding if neccessary.
/*
* Check whether a message only contains characters
* from the GSM03.38 character set
*/
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;
}
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);
}