5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2025-01-10 04:05:38 +00:00
envayasms/server/php/example/www/index.php

120 lines
3.7 KiB
PHP
Raw Normal View History

<?php
require_once dirname(__DIR__)."/config.php";
2011-09-22 23:16:46 +00:00
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
2011-09-22 23:16:46 +00:00
$request = EnvayaSMS::get_request();
2011-09-27 18:30:49 +00:00
$phone_number = $request->phone_number;
$password = @$PASSWORDS[$phone_number];
header("Content-Type: text/xml");
2011-09-22 23:16:46 +00:00
if (!isset($password) || !$request->is_validated($password))
{
header("HTTP/1.1 403 Forbidden");
error_log("Invalid request signature");
echo EnvayaSMS::get_error_xml("Invalid request signature");
return;
}
// append to EnvayaSMS app log
$app_log = $request->log;
if ($app_log)
{
$log_file = dirname(__DIR__)."/log/sms_".preg_replace('#[^\w]#', '', $request->phone_number).".log";
$f = fopen($log_file, "a");
fwrite($f, $app_log);
fclose($f);
}
2011-09-22 23:16:46 +00:00
$action = $request->get_action();
switch ($action->type)
{
2011-09-22 23:16:46 +00:00
case EnvayaSMS::ACTION_INCOMING:
$type = strtoupper($action->message_type);
error_log("Received $type from {$action->from}");
error_log(" message: {$action->message}");
if ($action->message_type == EnvayaSMS::MESSAGE_TYPE_MMS)
{
foreach ($action->mms_parts as $mms_part)
{
$ext_map = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'text/plain' => 'txt', 'application/smil' => 'smil');
$ext = @$ext_map[$mms_part->type] ?: "unk";
$filename = "mms_parts/" . uniqid('mms') . ".$ext";
copy($mms_part->tmp_name, dirname(__DIR__)."/$filename");
error_log(" mms part type {$mms_part->type} saved to {$filename}");
}
}
2011-09-22 23:16:46 +00:00
$reply = new EnvayaSMS_OutgoingMessage();
$reply->message = "You said: {$action->message}";
error_log("Sending reply: {$reply->message}");
echo $action->get_response_xml(array($reply));
return;
2011-09-22 23:16:46 +00:00
case EnvayaSMS::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)
{
2011-09-22 23:16:46 +00:00
$sms = new EnvayaSMS_OutgoingMessage();
$sms->id = $data['id'];
$sms->to = $data['to'];
$sms->from = $data['from'];
$sms->message = $data['message'];
$messages[] = $sms;
}
}
}
closedir($dir);
echo $action->get_response_xml($messages);
return;
2011-09-22 23:16:46 +00:00
case EnvayaSMS::ACTION_SEND_STATUS:
$id = $action->id;
// delete file with matching id
if (preg_match('#^\w+$#', $id) && unlink("$OUTGOING_DIR_NAME/$id.json"))
{
echo EnvayaSMS::get_success_xml();
}
else
{
header("HTTP/1.1 404 Not Found");
echo EnvayaSMS::get_error_xml("Invalid id");
}
return;
case EnvayaSMS::ACTION_DEVICE_STATUS:
error_log("device_status = {$action->status}");
echo EnvayaSMS::get_success_xml();
return;
2011-09-27 18:30:49 +00:00
case EnvayaSMS::ACTION_TEST:
echo EnvayaSMS::get_success_xml();
2011-09-27 18:30:49 +00:00
return;
default:
header("HTTP/1.1 404 Not Found");
echo EnvayaSMS::get_error_xml("Invalid action");
return;
}