5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2024-11-13 20:00:26 +00:00
envayasms/server/php/EnvayaSMS.php

273 lines
8.1 KiB
PHP
Raw Normal View History

<?php
/*
2011-09-22 23:16:46 +00:00
* PHP server library for EnvayaSMS
*
* For example usage see example/www/index.php
*/
2011-09-22 23:16:46 +00:00
class EnvayaSMS
{
const ACTION_INCOMING = 'incoming';
const ACTION_OUTGOING = 'outgoing';
const ACTION_SEND_STATUS = 'send_status';
const ACTION_DEVICE_STATUS = 'device_status';
2011-09-27 18:30:49 +00:00
const ACTION_TEST = 'test';
const STATUS_QUEUED = 'queued';
const STATUS_FAILED = 'failed';
const STATUS_SENT = 'sent';
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 MESSAGE_TYPE_SMS = 'sms';
2011-09-22 23:16:46 +00:00
const MESSAGE_TYPE_MMS = 'mms';
static function escape($val)
{
return htmlspecialchars($val, ENT_COMPAT, 'UTF-8');
2011-09-22 23:16:46 +00:00
}
private static $request;
2011-09-22 23:16:46 +00:00
static function get_request()
{
if (!isset(static::$request))
{
$version = @$_POST['version'];
// If API version changes, could return
// different EnvayaSMS_Request instance
// to support multiple phone versions
static::$request = new EnvayaSMS_Request();
}
return static::$request;
2011-09-27 18:30:49 +00:00
}
2011-09-22 23:16:46 +00:00
}
class EnvayaSMS_Request
{
private $request_action;
2011-09-27 18:30:49 +00:00
public $version;
public $phone_number;
public $log;
2011-09-27 18:30:49 +00:00
function __construct()
{
$this->version = $_POST['version'];
$this->phone_number = $_POST['phone_number'];
$this->log = @$_POST['log'];
2011-09-27 18:30:49 +00:00
}
2011-09-22 23:16:46 +00:00
function get_action()
{
if (!$this->request_action)
{
2011-09-22 23:16:46 +00:00
$this->request_action = $this->_get_action();
}
return $this->request_action;
}
2011-09-22 23:16:46 +00:00
private function _get_action()
{
switch (@$_POST['action'])
{
2011-09-22 23:16:46 +00:00
case EnvayaSMS::ACTION_INCOMING:
return new EnvayaSMS_Action_Incoming($this);
case EnvayaSMS::ACTION_OUTGOING:
return new EnvayaSMS_Action_Outgoing($this);
case EnvayaSMS::ACTION_SEND_STATUS:
return new EnvayaSMS_Action_SendStatus($this);
2011-09-27 18:30:49 +00:00
case EnvayaSMS::ACTION_TEST:
return new EnvayaSMS_Action_Test($this);
case EnvayaSMS::ACTION_DEVICE_STATUS:
return new EnvayaSMS_Action_DeviceStatus($this);
default:
2011-09-22 23:16:46 +00:00
return new EnvayaSMS_Action($this);
}
2011-09-27 18:30:49 +00:00
}
2011-09-22 23:16:46 +00:00
function is_validated($correct_password)
{
2011-09-22 23:16:46 +00:00
$signature = @$_SERVER['HTTP_X_REQUEST_SIGNATURE'];
if (!$signature)
{
return false;
}
$is_secure = (!empty($_SERVER['HTTPS']) AND filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN));
$protocol = $is_secure ? 'https' : 'http';
$full_url = $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$correct_signature = $this->compute_signature($full_url, $_POST, $correct_password);
//error_log("Correct signature: '$correct_signature'");
return $signature === $correct_signature;
}
function compute_signature($url, $data, $password)
{
ksort($data);
$input = $url;
foreach($data as $key => $value)
$input .= ",$key=$value";
$input .= ",$password";
//error_log("Signed data: '$input'");
return base64_encode(sha1($input, true));
}
2011-09-27 18:30:49 +00:00
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)."\"" : "";
$priority = isset($message->priority) ? " priority=\"".$message->priority."\"" : "";
echo "<sms$id$to$priority>".EnvayaSMS::escape($message->message)."</sms>";
2011-09-27 18:30:49 +00:00
}
echo "</messages>";
return ob_get_clean();
}
}
2011-09-22 23:16:46 +00:00
class EnvayaSMS_OutgoingMessage
{
2011-09-27 18:30:49 +00:00
public $id; // ID generated by server
public $to; // destination phone number
public $message; // content of SMS message
public $priority; // integer priority, higher numbers will be sent first
}
2011-09-22 23:16:46 +00:00
class EnvayaSMS_Action
{
public $type;
2011-09-22 23:16:46 +00:00
public $request;
2011-09-22 23:16:46 +00:00
function __construct($request)
{
2011-09-22 23:16:46 +00:00
$this->request = $request;
}
}
2011-09-22 23:16:46 +00:00
class EnvayaSMS_MMS_Part
{
public $form_name; // name of form field with MMS part content
public $cid; // MMS Content-ID
public $type; // Content type
public $filename; // Original filename of MMS part on sender phone
public $tmp_name; // Temporary file where MMS part content is stored
public $size; // Content length
public $error; // see http://www.php.net/manual/en/features.file-upload.errors.php
function __construct($args)
{
$this->form_name = $args['name'];
$this->cid = $args['cid'];
$this->type = $args['type'];
$this->filename = $args['filename'];
$file = $_FILES[$this->form_name];
$this->tmp_name = $file['tmp_name'];
$this->size = $file['size'];
$this->error = $file['error'];
}
}
2011-09-22 23:16:46 +00:00
class EnvayaSMS_Action_Incoming extends EnvayaSMS_Action
{
public $from; // Sender phone number
public $message; // The message body of the SMS, or the content of the text/plain part of the MMS.
2011-09-22 23:16:46 +00:00
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)
2011-09-22 23:16:46 +00:00
function __construct($request)
{
2011-09-22 23:16:46 +00:00
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_INCOMING;
$this->from = $_POST['from'];
$this->message = $_POST['message'];
$this->message_type = $_POST['message_type'];
$this->timestamp = @$_POST['timestamp'];
2011-09-22 23:16:46 +00:00
if ($this->message_type == EnvayaSMS::MESSAGE_TYPE_MMS)
{
$this->mms_parts = array();
foreach (json_decode($_POST['mms_parts'], true) as $mms_part)
{
2011-09-22 23:16:46 +00:00
$this->mms_parts[] = new EnvayaSMS_MMS_Part($mms_part);
}
}
2011-09-27 18:30:49 +00:00
}
function get_response_xml($messages)
{
2011-09-27 18:30:49 +00:00
return $this->request->get_messages_xml($messages);
}
}
2011-09-22 23:16:46 +00:00
class EnvayaSMS_Action_Outgoing extends EnvayaSMS_Action
{
2011-09-22 23:16:46 +00:00
function __construct($request)
{
2011-09-22 23:16:46 +00:00
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_OUTGOING;
2011-09-27 18:30:49 +00:00
}
function get_response_xml($messages)
{
2011-09-27 18:30:49 +00:00
return $this->request->get_messages_xml($messages);
}
}
class EnvayaSMS_Action_Test extends EnvayaSMS_Action
{
function __construct($request)
{
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_TEST;
}
}
2011-09-22 23:16:46 +00:00
class EnvayaSMS_Action_SendStatus extends EnvayaSMS_Action
{
2011-09-22 23:16:46 +00:00
public $status; // EnvayaSMS::STATUS_* values
public $id; // server ID previously used in EnvayaSMS_OutgoingMessage
public $error; // textual descrption of error (if applicable)
2011-09-22 23:16:46 +00:00
function __construct($request)
{
2011-09-22 23:16:46 +00:00
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_SEND_STATUS;
$this->status = $_POST['status'];
$this->id = $_POST['id'];
$this->error = $_POST['error'];
}
}
class EnvayaSMS_Action_DeviceStatus extends EnvayaSMS_Action
{
public $status; // EnvayaSMS::DEVICE_STATUS_* values
function __construct($request)
{
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_DEVICE_STATUS;
$this->status = $_POST['status'];
}
}