5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2024-11-09 10:20:25 +00:00

update php server library and example

This commit is contained in:
Jesse Young 2011-09-27 11:30:49 -07:00
parent bf14f23fe5
commit 03d5f556d7
2 changed files with 48 additions and 41 deletions

View File

@ -11,6 +11,7 @@ class EnvayaSMS
const ACTION_INCOMING = 'incoming'; const ACTION_INCOMING = 'incoming';
const ACTION_OUTGOING = 'outgoing'; const ACTION_OUTGOING = 'outgoing';
const ACTION_SEND_STATUS = 'send_status'; const ACTION_SEND_STATUS = 'send_status';
const ACTION_TEST = 'test';
const STATUS_QUEUED = 'queued'; const STATUS_QUEUED = 'queued';
const STATUS_FAILED = 'failed'; const STATUS_FAILED = 'failed';
@ -46,6 +47,15 @@ class EnvayaSMS_Request
{ {
private $request_action; private $request_action;
public $version;
public $phone_number;
function __construct()
{
$this->version = $_POST['version'];
$this->phone_number = $_POST['phone_number'];
}
function get_action() function get_action()
{ {
if (!$this->request_action) if (!$this->request_action)
@ -65,16 +75,13 @@ class EnvayaSMS_Request
return new EnvayaSMS_Action_Outgoing($this); return new EnvayaSMS_Action_Outgoing($this);
case EnvayaSMS::ACTION_SEND_STATUS: case EnvayaSMS::ACTION_SEND_STATUS:
return new EnvayaSMS_Action_SendStatus($this); return new EnvayaSMS_Action_SendStatus($this);
case EnvayaSMS::ACTION_TEST:
return new EnvayaSMS_Action_Test($this);
default: default:
return new EnvayaSMS_Action($this); return new EnvayaSMS_Action($this);
} }
} }
function get_phone_number()
{
return @$_POST['phone_number'];
}
function is_validated($correct_password) function is_validated($correct_password)
{ {
$signature = @$_SERVER['HTTP_X_REQUEST_SIGNATURE']; $signature = @$_SERVER['HTTP_X_REQUEST_SIGNATURE'];
@ -108,11 +115,26 @@ class EnvayaSMS_Request
return base64_encode(sha1($input, true)); return base64_encode(sha1($input, true));
} }
static function get_messages_xml($messages)
{
ob_start();
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<messages>";
foreach ($messages as $message)
{
$id = isset($message->id) ? " id='".EnvayaSMS::escape($message->id)."'" : "";
$to = isset($message->to) ? " to='".EnvayaSMS::escape($message->to)."'" : "";
echo "<sms$id$to>".EnvayaSMS::escape($message->message)."</sms>";
}
echo "</messages>";
return ob_get_clean();
}
} }
class EnvayaSMS_OutgoingMessage class EnvayaSMS_OutgoingMessage
{ {
public $id = ''; // ID generated by server public $id; // ID generated by server
public $to; // destination phone number public $to; // destination phone number
public $message; // content of SMS message public $message; // content of SMS message
} }
@ -128,15 +150,6 @@ class EnvayaSMS_Action
} }
} }
class EnvayaSMS_Action_Test extends EnvayaSMS_Action
{
function __construct($request)
{
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_TEST;
}
}
class EnvayaSMS_MMS_Part class EnvayaSMS_MMS_Part
{ {
public $form_name; // name of form field with MMS part content public $form_name; // name of form field with MMS part content
@ -189,15 +202,7 @@ class EnvayaSMS_Action_Incoming extends EnvayaSMS_Action
function get_response_xml($messages) function get_response_xml($messages)
{ {
ob_start(); return $this->request->get_messages_xml($messages);
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<messages>";
foreach ($messages as $message)
{
echo "<sms id='".EnvayaSMS::escape($message->id)."'>".EnvayaSMS::escape($message->message)."</sms>";
}
echo "</messages>";
return ob_get_clean();
} }
} }
@ -211,16 +216,16 @@ class EnvayaSMS_Action_Outgoing extends EnvayaSMS_Action
function get_response_xml($messages) function get_response_xml($messages)
{ {
ob_start(); return $this->request->get_messages_xml($messages);
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<messages>";
foreach ($messages as $message)
{
echo "<sms id='".EnvayaSMS::escape($message->id)."' to='".EnvayaSMS::escape($message->to)."'>".
EnvayaSMS::escape($message->message)."</sms>";
} }
echo "</messages>"; }
return ob_get_clean();
class EnvayaSMS_Action_Test extends EnvayaSMS_Action
{
function __construct($request)
{
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_TEST;
} }
} }

View File

@ -10,7 +10,7 @@ ini_set('display_errors','0');
$request = EnvayaSMS::get_request(); $request = EnvayaSMS::get_request();
$phone_number = $request->get_phone_number(); $phone_number = $request->phone_number;
$password = @$PASSWORDS[$phone_number]; $password = @$PASSWORDS[$phone_number];
@ -79,7 +79,9 @@ switch ($action->type)
echo "invalid id"; echo "invalid id";
} }
return; return;
case EnvayaSMS::ACTION_TEST:
echo "OK";
return;
default: default:
header("HTTP/1.1 404 Not Found"); header("HTTP/1.1 404 Not Found");
echo "Invalid action"; echo "Invalid action";