4
0
mirror of https://github.com/cwinfo/envayasms.git synced 2025-07-04 05:57:44 +00:00

add support for incoming call notifications; fix intermittent NullPointerException when receiving MMS; fix MMS parts with missing filename; allow server to send error messages to be shown in app log (requires updating server library and using EnvayaSMS::get_error_xml()); send device manufacturer/model/sdk version in HTTP User-Agent header; add device_status notification for send_limit_exceeded; add send_status constant for cancelled messages; 30 sec timeouts for outgoing messages; send network type (MOBILE or WIFI) to server; send age (i.e. delay) of incoming message to server; fix crashing bug with checking connectivity; save MMS parts in example server

This commit is contained in:
Jesse Young
2012-03-15 17:20:23 -07:00
parent d93ff76231
commit f53ccc3cc9
37 changed files with 1527 additions and 775 deletions

View File

@ -17,14 +17,20 @@ class EnvayaSMS
const STATUS_QUEUED = 'queued';
const STATUS_FAILED = 'failed';
const STATUS_SENT = 'sent';
const STATUS_CANCELLED = 'cancelled';
const DEVICE_STATUS_POWER_CONNECTED = "power_connected";
const DEVICE_STATUS_POWER_DISCONNECTED = "power_disconnected";
const DEVICE_STATUS_BATTERY_LOW = "battery_low";
const DEVICE_STATUS_BATTERY_OKAY = "battery_okay";
const DEVICE_STATUS_SEND_LIMIT_EXCEEDED = "send_limit_exceeded";
const MESSAGE_TYPE_SMS = 'sms';
const MESSAGE_TYPE_MMS = 'mms';
const MESSAGE_TYPE_CALL = 'call';
const NETWORK_MOBILE = "MOBILE";
const NETWORK_WIFI = "WIFI";
static function escape($val)
{
@ -46,7 +52,27 @@ class EnvayaSMS
static::$request = new EnvayaSMS_Request();
}
return static::$request;
}
}
static function get_error_xml($message)
{
ob_start();
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<response>";
echo "<error>";
echo EnvayaSMS::escape($message);
echo "</error>";
echo "</response>";
return ob_get_clean();
}
static function get_success_xml()
{
ob_start();
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<response></response>";
return ob_get_clean();
}
}
class EnvayaSMS_Request
@ -56,12 +82,28 @@ class EnvayaSMS_Request
public $version;
public $phone_number;
public $log;
public $version_name;
public $sdk_int;
public $manufacturer;
public $model;
public $network;
function __construct()
{
$this->version = $_POST['version'];
$this->phone_number = $_POST['phone_number'];
$this->log = @$_POST['log'];
$this->log = $_POST['log'];
$this->network = @$_POST['network'];
if (preg_match('#/(?P<version_name>[\w\.\-]+) \(Android; SDK (?P<sdk_int>\d+); (?P<manufacturer>[^;]*); (?P<model>[^\)]*)\)#',
@$_SERVER['HTTP_USER_AGENT'], $matches))
{
$this->version_name = $matches['version_name'];
$this->sdk_int = $matches['sdk_int'];
$this->manufacturer = $matches['manufacturer'];
$this->model = $matches['model'];
}
}
function get_action()
@ -124,21 +166,24 @@ class EnvayaSMS_Request
//error_log("Signed data: '$input'");
return base64_encode(sha1($input, true));
}
}
static function get_messages_xml($messages)
{
ob_start();
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<response>";
echo "<messages>";
foreach ($messages as $message)
{
{
$type = isset($message->type) ? $message->type : EnvayaSMS::MESSAGE_TYPE_SMS;
$id = isset($message->id) ? " id=\"".EnvayaSMS::escape($message->id)."\"" : "";
$to = isset($message->to) ? " to=\"".EnvayaSMS::escape($message->to)."\"" : "";
$priority = isset($message->priority) ? " priority=\"".$message->priority."\"" : "";
echo "<sms$id$to$priority>".EnvayaSMS::escape($message->message)."</sms>";
echo "<$type$id$to$priority>".EnvayaSMS::escape($message->message)."</$type>";
}
echo "</messages>";
echo "</response>";
return ob_get_clean();
}
}
@ -149,6 +194,7 @@ class EnvayaSMS_OutgoingMessage
public $to; // destination phone number
public $message; // content of SMS message
public $priority; // integer priority, higher numbers will be sent first
public $type; // EnvayaSMS::MESSAGE_TYPE_* value (default sms)
}
class EnvayaSMS_Action
@ -194,15 +240,17 @@ class EnvayaSMS_Action_Incoming extends EnvayaSMS_Action
public $message_type; // EnvayaSMS::MESSAGE_TYPE_MMS or EnvayaSMS::MESSAGE_TYPE_SMS
public $mms_parts; // array of EnvayaSMS_MMS_Part instances
public $timestamp; // timestamp of incoming message (added in version 12)
public $age; // delay in ms between time when message originally received and when forwarded to server (added in version 18)
function __construct($request)
{
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_INCOMING;
$this->from = $_POST['from'];
$this->message = $_POST['message'];
$this->message = @$_POST['message'];
$this->message_type = $_POST['message_type'];
$this->timestamp = @$_POST['timestamp'];
$this->age = @$_POST['age'];
if ($this->message_type == EnvayaSMS::MESSAGE_TYPE_MMS)
{
@ -247,7 +295,7 @@ class EnvayaSMS_Action_SendStatus extends EnvayaSMS_Action
{
public $status; // EnvayaSMS::STATUS_* values
public $id; // server ID previously used in EnvayaSMS_OutgoingMessage
public $error; // textual descrption of error (if applicable)
public $error; // textual description of error (if applicable)
function __construct($request)
{

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -14,11 +14,13 @@ $phone_number = $request->phone_number;
$password = @$PASSWORDS[$phone_number];
header("Content-Type: text/xml");
if (!isset($password) || !$request->is_validated($password))
{
header("HTTP/1.1 403 Forbidden");
error_log("Invalid request signature");
echo "Invalid request signature";
error_log("Invalid request signature");
echo EnvayaSMS::get_error_xml("Invalid request signature");
return;
}
@ -37,14 +39,30 @@ $action = $request->get_action();
switch ($action->type)
{
case EnvayaSMS::ACTION_INCOMING:
error_log("Received SMS from {$action->from}");
$type = strtoupper($action->message_type);
error_log("Received $type from {$action->from}");
error_log(" message: {$action->message}");
if ($action->message_type == EnvayaSMS::MESSAGE_TYPE_MMS)
{
foreach ($action->mms_parts as $mms_part)
{
$ext_map = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'text/plain' => 'txt', 'application/smil' => 'smil');
$ext = @$ext_map[$mms_part->type] ?: "unk";
$filename = "mms_parts/" . uniqid('mms') . ".$ext";
copy($mms_part->tmp_name, dirname(__DIR__)."/$filename");
error_log(" mms part type {$mms_part->type} saved to {$filename}");
}
}
$reply = new EnvayaSMS_OutgoingMessage();
$reply->message = "You said: {$action->message}";
error_log("Sending reply: {$reply->message}");
header("Content-Type: text/xml");
echo $action->get_response_xml(array($reply));
return;
@ -70,7 +88,6 @@ switch ($action->type)
}
closedir($dir);
header("Content-Type: text/xml");
echo $action->get_response_xml($messages);
return;
@ -81,23 +98,23 @@ switch ($action->type)
// delete file with matching id
if (preg_match('#^\w+$#', $id) && unlink("$OUTGOING_DIR_NAME/$id.json"))
{
echo "OK";
echo EnvayaSMS::get_success_xml();
}
else
{
header("HTTP/1.1 404 Not Found");
echo "invalid id";
echo EnvayaSMS::get_error_xml("Invalid id");
}
return;
case EnvayaSMS::ACTION_DEVICE_STATUS:
error_log("device_status = {$action->status}");
echo "OK";
echo EnvayaSMS::get_success_xml();
return;
case EnvayaSMS::ACTION_TEST:
echo "OK";
echo EnvayaSMS::get_success_xml();
return;
default:
header("HTTP/1.1 404 Not Found");
echo "Invalid action";
echo EnvayaSMS::get_error_xml("Invalid action");
return;
}