4
0
mirror of https://github.com/cwinfo/envayasms.git synced 2025-07-02 21:36:17 +00:00

rename to EnvayaSMS; add icon

This commit is contained in:
Jesse Young
2011-09-22 16:16:46 -07:00
parent 73bc3c9fc6
commit 7df89cd5d0
44 changed files with 201 additions and 189 deletions

View File

@ -1,12 +1,12 @@
<?php
/*
* PHP server library for KalSMS
* PHP server library for EnvayaSMS
*
* For example usage see example/www/index.php
*/
class KalSMS
class EnvayaSMS
{
const ACTION_INCOMING = 'incoming';
const ACTION_OUTGOING = 'outgoing';
@ -17,57 +17,67 @@ class KalSMS
const STATUS_SENT = 'sent';
const MESSAGE_TYPE_SMS = 'sms';
const MESSAGE_TYPE_MMS = 'mms';
static function new_from_request()
{
$version = @$_POST['version'];
// If API version changes, could return different KalSMS instance
// to support multiple phone versions
return new KalSMS();
}
const MESSAGE_TYPE_MMS = 'mms';
static function escape($val)
{
return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
}
}
private static $request;
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;
}
}
class EnvayaSMS_Request
{
private $request_action;
function get_request_action()
function get_action()
{
if (!$this->request_action)
{
$this->request_action = $this->_get_request_action();
$this->request_action = $this->_get_action();
}
return $this->request_action;
}
private function _get_request_action()
private function _get_action()
{
switch (@$_POST['action'])
{
case static::ACTION_INCOMING:
return new KalSMS_Action_Incoming($this);
case static::ACTION_OUTGOING:
return new KalSMS_Action_Outgoing($this);
case static::ACTION_SEND_STATUS:
return new KalSMS_Action_SendStatus($this);
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);
default:
return new KalSMS_Action($this);
return new EnvayaSMS_Action($this);
}
}
function get_request_phone_number()
function get_phone_number()
{
return @$_POST['phone_number'];
}
function is_validated_request($correct_password)
function is_validated($correct_password)
{
$signature = @$_SERVER['HTTP_X_KALSMS_SIGNATURE'];
$signature = @$_SERVER['HTTP_X_REQUEST_SIGNATURE'];
if (!$signature)
{
return false;
@ -100,34 +110,34 @@ class KalSMS
}
}
class KalSMS_OutgoingMessage
class EnvayaSMS_OutgoingMessage
{
public $id = ''; // ID generated by server
public $to; // destination phone number
public $message; // content of SMS message
}
class KalSMS_Action
class EnvayaSMS_Action
{
public $type;
public $kalsms;
public $request;
function __construct($kalsms)
function __construct($request)
{
$this->kalsms = $kalsms;
$this->request = $request;
}
}
class KalSMS_Action_Test extends KalSMS_Action
class EnvayaSMS_Action_Test extends EnvayaSMS_Action
{
function __construct($kalsms)
function __construct($request)
{
parent::__construct($kalsms);
$this->type = KalSMS::ACTION_TEST;
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_TEST;
}
}
class KalSMS_MMS_Part
class EnvayaSMS_MMS_Part
{
public $form_name; // name of form field with MMS part content
public $cid; // MMS Content-ID
@ -152,27 +162,27 @@ class KalSMS_MMS_Part
}
}
class KalSMS_Action_Incoming extends KalSMS_Action
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.
public $message_type; // KalSMS::MESSAGE_TYPE_MMS or KalSMS::MESSAGE_TYPE_SMS
public $mms_parts; // array of KalSMS_MMS_Part instances
public $message_type; // EnvayaSMS::MESSAGE_TYPE_MMS or EnvayaSMS::MESSAGE_TYPE_SMS
public $mms_parts; // array of EnvayaSMS_MMS_Part instances
function __construct($kalsms)
function __construct($request)
{
parent::__construct($kalsms);
$this->type = KalSMS::ACTION_INCOMING;
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_INCOMING;
$this->from = $_POST['from'];
$this->message = $_POST['message'];
$this->message_type = $_POST['message_type'];
if ($this->message_type == KalSMS::MESSAGE_TYPE_MMS)
if ($this->message_type == EnvayaSMS::MESSAGE_TYPE_MMS)
{
$this->mms_parts = array();
foreach (json_decode($_POST['mms_parts'], true) as $mms_part)
{
$this->mms_parts[] = new KalSMS_MMS_Part($mms_part);
$this->mms_parts[] = new EnvayaSMS_MMS_Part($mms_part);
}
}
}
@ -184,19 +194,19 @@ class KalSMS_Action_Incoming extends KalSMS_Action
echo "<messages>";
foreach ($messages as $message)
{
echo "<sms id='".KalSMS::escape($message->id)."'>".KalSMS::escape($message->message)."</sms>";
echo "<sms id='".EnvayaSMS::escape($message->id)."'>".EnvayaSMS::escape($message->message)."</sms>";
}
echo "</messages>";
return ob_get_clean();
}
}
class KalSMS_Action_Outgoing extends KalSMS_Action
class EnvayaSMS_Action_Outgoing extends EnvayaSMS_Action
{
function __construct($kalsms)
function __construct($request)
{
parent::__construct($kalsms);
$this->type = KalSMS::ACTION_OUTGOING;
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_OUTGOING;
}
function get_response_xml($messages)
@ -206,22 +216,23 @@ class KalSMS_Action_Outgoing extends KalSMS_Action
echo "<messages>";
foreach ($messages as $message)
{
echo "<sms id='".KalSMS::escape($message->id)."' to='".KalSMS::escape($message->to)."'>".
KalSMS::escape($message->message)."</sms>";
echo "<sms id='".EnvayaSMS::escape($message->id)."' to='".EnvayaSMS::escape($message->to)."'>".
EnvayaSMS::escape($message->message)."</sms>";
}
echo "</messages>";
return ob_get_clean();
}
}
class KalSMS_Action_SendStatus extends KalSMS_Action
class EnvayaSMS_Action_SendStatus extends EnvayaSMS_Action
{
public $status; // KalSMS::STATUS_* values
public $id; // server ID previously used in KalSMS_OutgoingMessage
public $status; // EnvayaSMS::STATUS_* values
public $id; // server ID previously used in EnvayaSMS_OutgoingMessage
function __construct($type)
function __construct($request)
{
$this->type = KalSMS::ACTION_SEND_STATUS;
parent::__construct($request);
$this->type = EnvayaSMS::ACTION_SEND_STATUS;
$this->status = $_POST['status'];
$this->id = $_POST['id'];
}

View File

@ -1,9 +1,8 @@
PHP server library for EnvayaSMS, with example implementation
PHP server library for KalSMS, with example implementation
The KalSMS.php library is intended to be used as part of a PHP application
The EnvayaSMS.php library is intended to be used as part of a PHP application
running on an HTTP server that receives incoming SMS messages from, and sends
outgoing SMS messages to, an Android phone running KalSMS.
outgoing SMS messages to, an Android phone running EnvayaSMS.
To run the example implementation, the example/www/ directory should be made available
via a web server running PHP (e.g. Apache). You can also use the included standalone
@ -11,9 +10,9 @@ PHP web server, by running the following commands:
git submodule init
php server.php
example/config.php contains the list of phone numbers and passwords for phones running KalSMS.
example/config.php contains the list of phone numbers and passwords for phones running EnvayaSMS.
On a phone running KalSMS, go to Menu -> Settings and enter:
On a phone running EnvayaSMS, go to Menu -> Settings and enter:
* Server URL: The URL to example/www/index.php.
If you're using server.php, this will be http://<your_ip_address>:8002/
* Your phone number: One of the phone numbers listed in example/config.php
@ -22,4 +21,4 @@ On a phone running KalSMS, go to Menu -> Settings and enter:
To send an outgoing SMS, use
php example/send_sms.php
See KalSMS.php and example/www/index.php
See EnvayaSMS.php and example/www/index.php

View File

@ -3,7 +3,8 @@
/*
* Command line script to simulate sending an outgoing SMS from the server.
*
* The message will be queued on the server until the next time KalSMS checks for outgoing messages.
* The message will be queued on the server until the next time
* EnvayaSMS checks for outgoing messages.
*/
require_once __DIR__."/config.php";

View File

@ -1,20 +1,20 @@
<?php
require_once dirname(__DIR__)."/config.php";
require_once dirname(dirname(__DIR__))."/KalSMS.php";
require_once dirname(dirname(__DIR__))."/EnvayaSMS.php";
ini_set('display_errors','0');
// this example implementation uses the filesystem to store outgoing SMS messages,
// but presumably a production implementation would use another storage method
$kalsms = KalSMS::new_from_request();
$request = EnvayaSMS::get_request();
$phone_number = $kalsms->get_request_phone_number();
$phone_number = $request->get_phone_number();
$password = @$PASSWORDS[$phone_number];
if (!isset($password) || !$kalsms->is_validated_request($password))
if (!isset($password) || !$request->is_validated($password))
{
header("HTTP/1.1 403 Forbidden");
error_log("Invalid request signature");
@ -22,14 +22,14 @@ if (!isset($password) || !$kalsms->is_validated_request($password))
return;
}
$action = $kalsms->get_request_action();
$action = $request->get_action();
switch ($action->type)
{
case KalSMS::ACTION_INCOMING:
case EnvayaSMS::ACTION_INCOMING:
error_log("Received SMS from {$action->from}");
$reply = new KalSMS_OutgoingMessage();
$reply = new EnvayaSMS_OutgoingMessage();
$reply->message = "You said: {$action->message}";
error_log("Sending reply: {$reply->message}");
@ -38,7 +38,7 @@ switch ($action->type)
echo $action->get_response_xml(array($reply));
return;
case KalSMS::ACTION_OUTGOING:
case EnvayaSMS::ACTION_OUTGOING:
$messages = array();
$dir = opendir($OUTGOING_DIR_NAME);
@ -49,7 +49,7 @@ switch ($action->type)
$data = json_decode(file_get_contents("$OUTGOING_DIR_NAME/$file"), true);
if ($data && @$data['from'] == $phone_number)
{
$sms = new KalSMS_OutgoingMessage();
$sms = new EnvayaSMS_OutgoingMessage();
$sms->id = $data['id'];
$sms->to = $data['to'];
$sms->from = $data['from'];
@ -64,7 +64,7 @@ switch ($action->type)
echo $action->get_response_xml($messages);
return;
case KalSMS::ACTION_SEND_STATUS:
case EnvayaSMS::ACTION_SEND_STATUS:
$id = $action->id;