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

PHP server library with example implementation; add 'check now' button on menu

This commit is contained in:
Jesse Young
2011-09-12 16:53:38 -07:00
parent d5b973411b
commit cb8d95ebd5
16 changed files with 442 additions and 29 deletions

9
server/php/example/config.php Executable file
View File

@ -0,0 +1,9 @@
<?php
$PASSWORDS = array(
'16505551212' => 'rosebud',
'16505551213' => 's3krit',
);
$PHONE_NUMBERS = array_keys($PASSWORDS);
$OUTGOING_DIR_NAME = __DIR__."/outgoing_sms";

41
server/php/example/send_sms.php Executable file
View 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
View 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();

View File

@ -0,0 +1,6 @@
<?php
require_once __DIR__."/config.php";
require_once __DIR__."/lib.php";
var_dump(get_outgoing_sms("16505551212"));

View 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;
}