mirror of
https://github.com/cwinfo/envayasms.git
synced 2025-07-02 21:36:17 +00:00
PHP server library with example implementation; add 'check now' button on menu
This commit is contained in:
177
server/php/KalSMS.php
Executable file
177
server/php/KalSMS.php
Executable file
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP server library for KalSMS
|
||||
*
|
||||
* For example usage see example/www/index.php
|
||||
*/
|
||||
|
||||
class KalSMS
|
||||
{
|
||||
const ACTION_INCOMING = 'incoming';
|
||||
const ACTION_OUTGOING = 'outgoing';
|
||||
const ACTION_SEND_STATUS = 'send_status';
|
||||
const ACTION_TEST = 'test';
|
||||
|
||||
const STATUS_QUEUED = 1;
|
||||
const STATUS_FAILED = 2;
|
||||
const STATUS_SENT = 3;
|
||||
|
||||
static function new_from_request()
|
||||
{
|
||||
$version = @$_SERVER['HTTP_X_KALSMS_VERSION'];
|
||||
|
||||
return new KalSMS();
|
||||
}
|
||||
|
||||
static function escape($val)
|
||||
{
|
||||
return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function get_request_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 static::ACTION_TEST:
|
||||
return new KalSMS_Action_Test($this);
|
||||
default:
|
||||
return new KalSMS_Action($this);
|
||||
}
|
||||
}
|
||||
|
||||
function get_request_phone_number()
|
||||
{
|
||||
return @$_SERVER['HTTP_X_KALSMS_PHONENUMBER'];
|
||||
}
|
||||
|
||||
function is_validated_request($correct_password)
|
||||
{
|
||||
$signature = @$_SERVER['HTTP_X_KALSMS_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));
|
||||
}
|
||||
}
|
||||
|
||||
class KalSMS_OutgoingMessage
|
||||
{
|
||||
public $id = '';
|
||||
public $to;
|
||||
public $message;
|
||||
}
|
||||
|
||||
class KalSMS_Action
|
||||
{
|
||||
public $type;
|
||||
public $kalsms;
|
||||
|
||||
function __construct($kalsms)
|
||||
{
|
||||
$this->kalsms = $kalsms;
|
||||
}
|
||||
}
|
||||
|
||||
class KalSMS_Action_Test extends KalSMS_Action
|
||||
{
|
||||
function __construct($kalsms)
|
||||
{
|
||||
parent::__construct($kalsms);
|
||||
$this->type = KalSMS::ACTION_TEST;
|
||||
}
|
||||
}
|
||||
|
||||
class KalSMS_Action_Incoming extends KalSMS_Action
|
||||
{
|
||||
public $from;
|
||||
public $message;
|
||||
|
||||
function __construct($kalsms)
|
||||
{
|
||||
parent::__construct($kalsms);
|
||||
$this->type = KalSMS::ACTION_INCOMING;
|
||||
$this->from = $_POST['from'];
|
||||
$this->message = $_POST['message'];
|
||||
}
|
||||
|
||||
function get_response_xml($messages)
|
||||
{
|
||||
ob_start();
|
||||
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
echo "<Response>";
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
echo "<Sms id='".KalSMS::escape($message->id)."'>".KalSMS::escape($message->message)."</Sms>";
|
||||
}
|
||||
echo "</Response>";
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
|
||||
class KalSMS_Action_Outgoing extends KalSMS_Action
|
||||
{
|
||||
function __construct($kalsms)
|
||||
{
|
||||
parent::__construct($kalsms);
|
||||
$this->type = KalSMS::ACTION_OUTGOING;
|
||||
}
|
||||
|
||||
function get_response_xml($messages)
|
||||
{
|
||||
ob_start();
|
||||
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
echo "<Messages>";
|
||||
foreach ($messages as $message)
|
||||
{
|
||||
echo "<Sms id='".KalSMS::escape($message->id)."' to='".KalSMS::escape($message->to)."'>".
|
||||
KalSMS::escape($message->message)."</Sms>";
|
||||
}
|
||||
echo "</Messages>";
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
|
||||
class KalSMS_Action_SendStatus extends KalSMS_Action
|
||||
{
|
||||
public $status;
|
||||
public $id;
|
||||
|
||||
function __construct($type)
|
||||
{
|
||||
$this->type = KalSMS::ACTION_SEND_STATUS;
|
||||
$this->status = (int)$_POST['status'];
|
||||
$this->id = $_POST['id'];
|
||||
}
|
||||
}
|
25
server/php/README.txt
Executable file
25
server/php/README.txt
Executable file
@ -0,0 +1,25 @@
|
||||
|
||||
PHP server library for KalSMS, with example implementation
|
||||
|
||||
The KalSMS.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.
|
||||
|
||||
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
|
||||
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.
|
||||
|
||||
On a phone running KalSMS, 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
|
||||
* Password: The corresponding password in example/config.php
|
||||
|
||||
To send an outgoing SMS, use
|
||||
php example/send_sms.php
|
||||
|
||||
See KalSMS.php and example/www/index.php
|
9
server/php/example/config.php
Executable file
9
server/php/example/config.php
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
$PASSWORDS = array(
|
||||
'16505551212' => 'rosebud',
|
||||
'16505551213' => 's3krit',
|
||||
);
|
||||
$PHONE_NUMBERS = array_keys($PASSWORDS);
|
||||
|
||||
$OUTGOING_DIR_NAME = __DIR__."/outgoing_sms";
|
1
server/php/example/httpserver
Submodule
1
server/php/example/httpserver
Submodule
Submodule server/php/example/httpserver added at 95a38ab08f
41
server/php/example/send_sms.php
Executable file
41
server/php/example/send_sms.php
Executable file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
// invoke from command line
|
||||
|
||||
require_once __DIR__."/config.php";
|
||||
|
||||
$arg_len = sizeof($argv);
|
||||
|
||||
if ($arg_len == 4)
|
||||
{
|
||||
$from = $argv[1];
|
||||
$to = $argv[2];
|
||||
$message = $argv[3];
|
||||
}
|
||||
else if ($arg_len == 3)
|
||||
{
|
||||
$from = $PHONE_NUMBERS[0];
|
||||
$to = $argv[1];
|
||||
$message = $argv[2];
|
||||
}
|
||||
else
|
||||
{
|
||||
error_log("Usage: php send_sms.php [<from>] <to> \"<message>\"");
|
||||
error_log("Examples: ");
|
||||
error_log(" php send_sms.php 16505551212 16504449876 \"hello world\"");
|
||||
error_log(" php send_sms.php 16504449876 \"hello world\"");
|
||||
die;
|
||||
}
|
||||
|
||||
$id = uniqid("");
|
||||
|
||||
$filename = "$OUTGOING_DIR_NAME/$id.json";
|
||||
|
||||
file_put_contents($filename, json_encode(array(
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
'message' => $message,
|
||||
'id' => $id
|
||||
)));
|
||||
|
||||
echo "Message $id added to outgoing queue\n";
|
46
server/php/example/server.php
Executable file
46
server/php/example/server.php
Executable file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Example standalone HTTP server that routes all .php URIs to PHP files under ./example_www,
|
||||
* and routes all other URIs to static files under ./example_www.
|
||||
*
|
||||
* index.php is used as the directory index.
|
||||
*
|
||||
* Just run it on the command line like "php example_server.php".
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/httpserver/httpserver.php';
|
||||
|
||||
class ExampleServer extends HTTPServer
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct(array(
|
||||
'port' => 8002,
|
||||
));
|
||||
}
|
||||
|
||||
function route_request($request)
|
||||
{
|
||||
$uri = $request->uri;
|
||||
|
||||
$doc_root = __DIR__ . '/www';
|
||||
|
||||
if (preg_match('#/$#', $uri))
|
||||
{
|
||||
$uri .= "index.php";
|
||||
}
|
||||
|
||||
if (preg_match('#\.php$#', $uri))
|
||||
{
|
||||
return $this->get_php_response($request, "$doc_root$uri");
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->get_static_response($request, "$doc_root$uri");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$server = new ExampleServer();
|
||||
$server->run_forever();
|
6
server/php/example/test_sms.php
Executable file
6
server/php/example/test_sms.php
Executable file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/config.php";
|
||||
require_once __DIR__."/lib.php";
|
||||
|
||||
var_dump(get_outgoing_sms("16505551212"));
|
91
server/php/example/www/index.php
Executable file
91
server/php/example/www/index.php
Executable file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
require_once dirname(__DIR__)."/config.php";
|
||||
require_once dirname(dirname(__DIR__))."/KalSMS.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();
|
||||
|
||||
$phone_number = $kalsms->get_request_phone_number();
|
||||
|
||||
$password = @$PASSWORDS[$phone_number];
|
||||
|
||||
if (!isset($password) || !$kalsms->is_validated_request($password))
|
||||
{
|
||||
header("HTTP/1.1 403 Forbidden");
|
||||
error_log("Invalid request signature");
|
||||
echo "Invalid request signature";
|
||||
return;
|
||||
}
|
||||
|
||||
$action = $kalsms->get_request_action();
|
||||
|
||||
switch ($action->type)
|
||||
{
|
||||
case KalSMS::ACTION_INCOMING:
|
||||
error_log("Received SMS from {$action->from}");
|
||||
|
||||
$reply = new KalSMS_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;
|
||||
|
||||
case KalSMS::ACTION_OUTGOING:
|
||||
$messages = array();
|
||||
|
||||
$dir = opendir($OUTGOING_DIR_NAME);
|
||||
while ($file = readdir($dir))
|
||||
{
|
||||
if (preg_match('#\.json$#', $file))
|
||||
{
|
||||
$data = json_decode(file_get_contents("$OUTGOING_DIR_NAME/$file"), true);
|
||||
if ($data && @$data['from'] == $phone_number)
|
||||
{
|
||||
$sms = new KalSMS_OutgoingMessage();
|
||||
$sms->id = $data['id'];
|
||||
$sms->to = $data['to'];
|
||||
$sms->from = $data['from'];
|
||||
$sms->message = $data['message'];
|
||||
$messages[] = $sms;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($dir);
|
||||
|
||||
header("Content-Type: text/xml");
|
||||
echo $action->get_response_xml($messages);
|
||||
return;
|
||||
|
||||
case KalSMS::ACTION_SEND_STATUS:
|
||||
|
||||
$id = $action->id;
|
||||
|
||||
// delete file with matching id
|
||||
if (preg_match('#^\w+$#', $id) && unlink("$OUTGOING_DIR_NAME/$id.json"))
|
||||
{
|
||||
echo "OK";
|
||||
}
|
||||
else
|
||||
{
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
echo "invalid id";
|
||||
}
|
||||
return;
|
||||
|
||||
case KalSMS::ACTION_TEST:
|
||||
echo "OK";
|
||||
return;
|
||||
|
||||
default:
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
echo "Invalid action";
|
||||
return;
|
||||
}
|
Reference in New Issue
Block a user