2012-09-25 16:56:03 +00:00
|
|
|
<?php
|
|
|
|
namespace SmsGateway;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
use SmsGateway\AuthInterface;
|
2012-09-25 16:56:03 +00:00
|
|
|
use SmsGateway\LoggerInterface;
|
|
|
|
use SmsGateway\SenderInterface;
|
|
|
|
|
|
|
|
class RpcServer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The user backend
|
|
|
|
*
|
2012-10-06 21:17:20 +00:00
|
|
|
* @var SmsGateway\AuthInterface $backend
|
2012-09-25 16:56:03 +00:00
|
|
|
*/
|
2012-10-06 21:17:20 +00:00
|
|
|
private $auth;
|
2012-09-25 16:56:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The logger
|
|
|
|
*
|
|
|
|
* @var SmsGateway\LoggerInterface $logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The sender
|
|
|
|
*
|
|
|
|
* @var SmsGateway\SenderInterface $sender
|
|
|
|
*/
|
|
|
|
private $sender;
|
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
public function __construct(AuthInterface $auth, LoggerInterface $logger, SenderInterface $sender)
|
2012-09-25 16:56:03 +00:00
|
|
|
{
|
2012-10-06 21:17:20 +00:00
|
|
|
$this->auth = $auth;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->sender = $sender;
|
2012-09-25 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
protected function login($username, $password)
|
2012-09-25 16:56:03 +00:00
|
|
|
{
|
2012-10-06 21:17:20 +00:00
|
|
|
$token = $this->auth->authenticate($username, $password, $_SERVER['REMOTE_ADDR'], null);
|
|
|
|
|
|
|
|
if ($token === false) {
|
|
|
|
throw new \Exception('Could not create token.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
2012-09-25 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
protected function send($token, $recipient, $message, array $passwordLocations)
|
2012-09-25 16:56:03 +00:00
|
|
|
{
|
2012-10-06 21:17:20 +00:00
|
|
|
if (!$this->auth->isTokenValid($token, $_SERVER['REMOTE_ADDR'], null)) {
|
|
|
|
throw new \Exception('Invalid token!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sender->send($this->auth->getTokenUsername($token, $_SERVER['REMOTE_ADDR'], null), $recipient, $message, $passwordLocations);
|
|
|
|
|
|
|
|
// TODO: Send the message!
|
|
|
|
|
|
|
|
return 'success';
|
2012-09-25 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 21:17:20 +00:00
|
|
|
protected function logout($token)
|
2012-09-25 16:56:03 +00:00
|
|
|
{
|
2012-10-06 21:17:20 +00:00
|
|
|
if (!$this->auth->isTokenValid($token, $_SERVER['REMOTE_ADDR'], null)) {
|
|
|
|
throw new \Exception('Invalid token!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->auth->removeToken($token, $_SERVER['REMOTE_ADDR'], null);
|
|
|
|
|
|
|
|
return 'success';
|
2012-09-25 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(Request $request, array $jsonData)
|
|
|
|
{
|
|
|
|
$params = $jsonData['params'];
|
|
|
|
switch ($jsonData['method']) {
|
|
|
|
case 'login':
|
2012-10-06 21:17:20 +00:00
|
|
|
if (count($params) != 2) {
|
|
|
|
throw new \InvalidArgumentException('Bad parameter count!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->login($params[0], $params[1]);
|
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
break;
|
|
|
|
case 'send':
|
2012-10-06 21:17:20 +00:00
|
|
|
if (count($params) != 4) {
|
|
|
|
throw new \InvalidArgumentException('Bad parameter count!');
|
|
|
|
}
|
|
|
|
if (!is_array($params[3])) {
|
|
|
|
throw new \InvalidArgumentException('Invalid 4th parameter!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->send($params[0], $params[1], $params[2], $params[3]);
|
|
|
|
|
2012-09-25 16:56:03 +00:00
|
|
|
break;
|
|
|
|
case 'logout':
|
2012-10-06 21:17:20 +00:00
|
|
|
if (count($params) != 1) {
|
|
|
|
throw new \InvalidArgumentException('Bad parameter count!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->logout($params[0]);
|
2012-09-25 16:56:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-10-06 21:17:20 +00:00
|
|
|
throw new \BadMethodCallException('Unknown method ' . $jsonData['method']);
|
|
|
|
break;
|
2012-09-25 16:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|