2011-09-29 16:09:59 +00:00
|
|
|
<?php
|
2012-11-04 13:59:32 +00:00
|
|
|
require __DIR__.'/../vendor/autoload.php';
|
2011-09-29 16:09:59 +00:00
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Routing\RequestContext;
|
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
2011-09-29 16:09:59 +00:00
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
use SmsGateway\Sender\FileSender;
|
|
|
|
use SmsGateway\Logger\FileLogger;
|
|
|
|
use SmsGateway\Auth\FileAuth;
|
2012-09-25 16:56:03 +00:00
|
|
|
use SmsGateway\RpcServer;
|
|
|
|
|
|
|
|
$request = Request::createFromGlobals();
|
|
|
|
|
|
|
|
$routes = new RouteCollection();
|
|
|
|
$routes->add('jsonHandle', new Route('/', array('controller' => 'handleJson'), array('_method' => 'POST')));
|
|
|
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
$context->fromRequest($request);
|
|
|
|
|
|
|
|
$matcher = new UrlMatcher($routes, $context);
|
|
|
|
try {
|
|
|
|
$parameters = $matcher->match($request->getPathInfo());
|
|
|
|
} catch (ResourceNotFoundException $e) {
|
|
|
|
$response = new Response('Bad Request', 400);
|
|
|
|
$response->send();
|
|
|
|
exit;
|
|
|
|
} catch (MethodNotAllowedException $e) {
|
|
|
|
$response = new Response('Bad Request', 400);
|
|
|
|
$response->send();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->getContentType() != 'json') {
|
|
|
|
$response = new Response('Bad Request', 400);
|
|
|
|
$response->send();
|
|
|
|
exit;
|
2011-09-29 16:47:53 +00:00
|
|
|
}
|
2012-09-25 16:56:03 +00:00
|
|
|
|
|
|
|
$jsonData = json_decode($request->getContent(), true);
|
|
|
|
if ($jsonData === null) {
|
|
|
|
$response = new Response('Bad Request', 400);
|
|
|
|
$response->send();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if (!array_key_exists('method', $jsonData) || !array_key_exists('params', $jsonData) || !is_array($jsonData['params'])) {
|
|
|
|
$response = new Response('Bad Request', 400);
|
|
|
|
$response->send();
|
2011-09-29 16:47:53 +00:00
|
|
|
exit;
|
|
|
|
}
|
2012-09-25 16:56:03 +00:00
|
|
|
$wantResponse = (!empty($jsonData['id']));
|
2011-09-29 16:47:53 +00:00
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
try {
|
2012-10-06 21:17:20 +00:00
|
|
|
$sender = new FileSender('/var/www/html/smsgateway/app/cache/sms_spool');
|
2012-09-25 16:56:03 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$response = new Response('Internal Server Error: Sender cannot be instantiated.', 500);
|
|
|
|
$response->send();
|
|
|
|
exit;
|
2011-09-29 16:47:53 +00:00
|
|
|
}
|
2012-09-25 16:56:03 +00:00
|
|
|
|
|
|
|
try {
|
2012-10-06 21:17:20 +00:00
|
|
|
$logger = new FileLogger('/var/www/html/smsgateway/app/logs/sms-message.log', '/var/www/html/smsgateway/app/logs/sms-audit.log');
|
|
|
|
} catch (LogicException $e) {
|
2012-09-25 16:56:03 +00:00
|
|
|
$response = new Response('Internal Server Error: Logger cannot be instantiated.', 500);
|
|
|
|
$response->send();
|
2011-09-30 11:42:45 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
try {
|
2012-10-06 21:17:20 +00:00
|
|
|
$auth = new FileAuth('/var/www/html/smsgateway/senders', '/var/www/html/smsgateway/app/cache/tokens');
|
2012-09-25 16:56:03 +00:00
|
|
|
} catch (Exception $e) {
|
2012-10-06 21:17:20 +00:00
|
|
|
$response = new Response('Internal Server Error: Authenticator cannot be instantiated.', 500);
|
2012-09-25 16:56:03 +00:00
|
|
|
$response->send();
|
|
|
|
exit;
|
2011-09-30 11:42:45 +00:00
|
|
|
}
|
2012-09-25 16:56:03 +00:00
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
$auth->setLogger($logger);
|
|
|
|
$sender->setLogger($logger);
|
|
|
|
|
|
|
|
$handler = new RpcServer($auth, $logger, $sender);
|
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
try {
|
|
|
|
$result = $handler->handle($request, $jsonData);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
if ($wantResponse) {
|
|
|
|
$jsonResponse = array(
|
|
|
|
'id' => $jsonData['id'],
|
|
|
|
'result' => null,
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
);
|
|
|
|
$response = new Response(json_encode($jsonResponse));
|
|
|
|
$response->send();
|
|
|
|
}
|
2011-09-29 16:55:48 +00:00
|
|
|
exit;
|
2011-09-29 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
if ($wantResponse) {
|
|
|
|
$jsonResponse = array(
|
|
|
|
'id' => $jsonData['id'],
|
|
|
|
'result' => $result,
|
|
|
|
'error' => null,
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = new Response(json_encode($jsonResponse), 200, array('Content-Type' => 'application/json'));
|
|
|
|
$response->send();
|
|
|
|
}
|