diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..abd3a07 --- /dev/null +++ b/.htaccess @@ -0,0 +1,10 @@ + + RewriteEngine On + + # + # RewriteBase / + # + + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^(.*)$ json_sms.php [QSA,L] + diff --git a/json_sms.php b/json_sms.php index bc851f8..6d31acc 100644 --- a/json_sms.php +++ b/json_sms.php @@ -1,39 +1,107 @@ 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; } -try -{ - $smsBackend = new gnokiiSMSBackend(); -} -catch (Exception $e) -{ - header('Status: 500 Internal Server Error (SMS)'); +if ($request->getContentType() != 'json') { + $response = new Response('Bad Request', 400); + $response->send(); exit; } -try -{ - $smsSender = new smsSender($dbBackend, $smsBackend, session_id()); +$jsonData = json_decode($request->getContent(), true); +if ($jsonData === null) { + $response = new Response('Bad Request', 400); + $response->send(); + exit; } -catch (Exception $e) -{ - header('Status: 500 Internal Server Error'); +if (!array_key_exists('method', $jsonData) || !array_key_exists('params', $jsonData) || !is_array($jsonData['params'])) { + $response = new Response('Bad Request', 400); + $response->send(); + exit; +} +$wantResponse = (!empty($jsonData['id'])); + +try { + $sender = new GnokiiSender(); +} catch (Exception $e) { + $response = new Response('Internal Server Error: Sender cannot be instantiated.', 500); + $response->send(); exit; } -jsonRPCServer::handle($smsSender); +try { + $logger = new DatabaseLogger('pgsql:host=127.0.0.1;dbname=smsgateway', 'smsgateway', 'Imuiwai8'); +} catch (PDOException $e) { + $response = new Response('Internal Server Error: Logger cannot be instantiated.', 500); + $response->send(); + exit; +} + +try { + $backend = new DatabaseBackend(); +} catch (Exception $e) { + $response = new Response('Internal Server Error: Backend cannot be instantiated.', 500); + $response->send(); + exit; +} + +$handler = new RpcServer($backend, $logger, $sender); +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(); + } + exit; +} + +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(); +} diff --git a/src/SmsGateway/Backend/DatabaseBackend.php b/src/SmsGateway/Backend/DatabaseBackend.php index e69de29..5e6b1d6 100644 --- a/src/SmsGateway/Backend/DatabaseBackend.php +++ b/src/SmsGateway/Backend/DatabaseBackend.php @@ -0,0 +1,8 @@ +dbh = new PDO($dsn, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); + } + + /** + * Log an audit event + * + * @param integer $type The message type + * @param string $username The user this audit log connects to. Can be + * NULL if the user is unauthenticated + * @param string $message The audit message + * @return boolean TRUE if the message was saved, FALSE otherwise + */ + public function auditLog($type, $username, $message) + { + } + + /** + * Log a sent message + * + * @param string $username The username who sent this message + * @param string $recipient The recipient of the message + * @param string $message The message itself + */ + public function messageLog($username, $recipient, $message) + { + } +} \ No newline at end of file diff --git a/src/SmsGateway/LoggerInterface.php b/src/SmsGateway/LoggerInterface.php new file mode 100644 index 0000000..762cf71 --- /dev/null +++ b/src/SmsGateway/LoggerInterface.php @@ -0,0 +1,27 @@ +