refactor json api classes
This commit is contained in:
parent
75ed103d17
commit
5ced6b93df
|
@ -13,9 +13,9 @@ require_once './router.php';
|
||||||
$imapClient = new ImapClient($config['imap']['url'], $config['imap']['username'], $config['imap']['password']);
|
$imapClient = new ImapClient($config['imap']['url'], $config['imap']['username'], $config['imap']['password']);
|
||||||
|
|
||||||
$router = new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config);
|
$router = new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config);
|
||||||
$page = $router->route();
|
$controller = $router->route();
|
||||||
$page->setViewHandler(new ServerRenderViewHandler());
|
$controller->setViewHandler(new ServerRenderViewHandler());
|
||||||
$page->invoke($imapClient);
|
$controller->invoke($imapClient);
|
||||||
|
|
||||||
// delete after each request
|
// delete after each request
|
||||||
$imapClient->delete_old_messages($config['delete_messages_older_than']);
|
$imapClient->delete_old_messages($config['delete_messages_older_than']);
|
||||||
|
|
85
src/json-api.php
Normal file
85
src/json-api.php
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
<?php
|
||||||
|
# set the new path of config.php (must be in a safe location outside the `public_html`)
|
||||||
|
require_once '../../config.php';
|
||||||
|
|
||||||
|
# load php dependencies:
|
||||||
|
require_once 'backend-libs/autoload.php';
|
||||||
|
|
||||||
|
require_once 'user.php';
|
||||||
|
require_once 'imap_client.php';
|
||||||
|
require_once 'controller.php';
|
||||||
|
require_once 'router.php';
|
||||||
|
|
||||||
|
class RestRouter extends Router {
|
||||||
|
|
||||||
|
function route(): Controller {
|
||||||
|
if ($this->action === "download_email"
|
||||||
|
&& isset($this->get_vars['email_id'])
|
||||||
|
&& isset($this->get_vars['address'])) {
|
||||||
|
return new DownloadEmailController($this->get_vars['email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']);
|
||||||
|
|
||||||
|
} elseif ($this->action === "delete_email"
|
||||||
|
&& isset($this->get_vars['email_id'])
|
||||||
|
&& isset($this->get_vars['address'])) {
|
||||||
|
return new DeleteEmailController($this->get_vars['email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']);
|
||||||
|
|
||||||
|
} elseif ($this->action === 'get_random_username') {
|
||||||
|
return new RedirectToRandomAddressController($this->config['domains']);
|
||||||
|
|
||||||
|
} elseif ($this->action === 'get_emails' && isset($this->get_vars['address'])) {
|
||||||
|
return new DisplayEmailsController($this->get_vars['address'], $this->config);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return new InvalidRequestController();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class JsonViewHandler implements ViewHandler {
|
||||||
|
|
||||||
|
private function json($obj) {
|
||||||
|
header('Content-type: application/json');
|
||||||
|
|
||||||
|
// Never cache requests:
|
||||||
|
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
|
||||||
|
header("Cache-Control: post-check=0, pre-check=0", false);
|
||||||
|
header("Pragma: no-cache");
|
||||||
|
print json_encode($obj);
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
|
function done($address) {
|
||||||
|
$this->json(array('status' => "success"));
|
||||||
|
}
|
||||||
|
|
||||||
|
function error($status, $msg) {
|
||||||
|
@http_response_code($status);
|
||||||
|
$this->json(array('status' => "failure", 'error' => $msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayEmails($emails, $config, $user) {
|
||||||
|
$this->json(array('status' => "success", 'emails' => $emails));
|
||||||
|
}
|
||||||
|
|
||||||
|
function newAddress($address) {
|
||||||
|
$this->json(array('status' => "failure", 'address' => $address));
|
||||||
|
}
|
||||||
|
|
||||||
|
function downloadEmailAsRfc822($full_email, $filename) {
|
||||||
|
$this->json(array('status' => "success", 'body' => $full_email));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$imapClient = new ImapClient($config['imap']['url'], $config['imap']['username'], $config['imap']['password']);
|
||||||
|
|
||||||
|
$router = new RestRouter($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config);
|
||||||
|
$controller = $router->route();
|
||||||
|
$controller->setViewHandler(new JsonViewHandler());
|
||||||
|
$controller->invoke($imapClient);
|
||||||
|
|
||||||
|
// delete after each request
|
||||||
|
$imapClient->delete_old_messages($config['delete_messages_older_than']);
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
23
src/rest.php
23
src/rest.php
|
@ -1,23 +0,0 @@
|
||||||
<?php
|
|
||||||
# set the new path of config.php (must be in a safe location outside the `public_html`)
|
|
||||||
require_once '../../config.php';
|
|
||||||
|
|
||||||
# load php dependencies:
|
|
||||||
require_once './backend-libs/autoload.php';
|
|
||||||
|
|
||||||
require_once './user.php';
|
|
||||||
require_once './imap_client.php';
|
|
||||||
require_once './controller.php';
|
|
||||||
require_once './router.rest.php';
|
|
||||||
|
|
||||||
$imapClient = new ImapClient($config['imap']['url'], $config['imap']['username'], $config['imap']['password']);
|
|
||||||
|
|
||||||
$router = new RestRouter($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config);
|
|
||||||
$page = $router->route();
|
|
||||||
$page->setViewHandler(new JsonViewHandler());
|
|
||||||
$page->invoke($imapClient);
|
|
||||||
|
|
||||||
// delete after each request
|
|
||||||
$imapClient->delete_old_messages($config['delete_messages_older_than']);
|
|
||||||
|
|
||||||
?>
|
|
|
@ -1,30 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
require_once './controller.php';
|
|
||||||
require_once './router.php';
|
|
||||||
|
|
||||||
|
|
||||||
class RestRouter extends Router {
|
|
||||||
|
|
||||||
function route(): Controller {
|
|
||||||
if ($this->action === "download_email"
|
|
||||||
&& isset($this->get_vars['email_id'])
|
|
||||||
&& isset($this->get_vars['address'])) {
|
|
||||||
return new DownloadEmailController($this->get_vars['email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']);
|
|
||||||
|
|
||||||
} elseif ($this->action === "delete_email"
|
|
||||||
&& isset($this->get_vars['email_id'])
|
|
||||||
&& isset($this->get_vars['address'])) {
|
|
||||||
return new DeleteEmailController($this->get_vars['email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']);
|
|
||||||
|
|
||||||
} elseif ($this->action === 'get_random_username') {
|
|
||||||
return new RedirectToRandomAddressController($this->config['domains']);
|
|
||||||
|
|
||||||
} elseif ($this->action === 'get_emails' && isset($this->get_vars['address'])) {
|
|
||||||
return new DisplayEmailsController($this->get_vars['address'], $this->config);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return new InvalidRequestController();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
34
src/view.php
34
src/view.php
|
@ -17,40 +17,6 @@ interface ViewHandler {
|
||||||
function downloadEmailAsRfc822($full_email, $filename);
|
function downloadEmailAsRfc822($full_email, $filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
class JsonViewHandler implements ViewHandler {
|
|
||||||
|
|
||||||
private function json($obj) {
|
|
||||||
header('Content-type: application/json');
|
|
||||||
|
|
||||||
// Never cache requests:
|
|
||||||
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
|
|
||||||
header("Cache-Control: post-check=0, pre-check=0", false);
|
|
||||||
header("Pragma: no-cache");
|
|
||||||
print json_encode($obj);
|
|
||||||
die();
|
|
||||||
}
|
|
||||||
|
|
||||||
function done($address) {
|
|
||||||
$this->json(array('status' => "success"));
|
|
||||||
}
|
|
||||||
|
|
||||||
function error($status, $msg) {
|
|
||||||
@http_response_code($status);
|
|
||||||
$this->json(array('status' => "failure", 'error' => $msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function displayEmails($emails, $config, $user) {
|
|
||||||
$this->json(array('status' => "success", 'emails' => $emails));
|
|
||||||
}
|
|
||||||
|
|
||||||
function newAddress($address) {
|
|
||||||
$this->json(array('status' => "failure", 'address' => $address));
|
|
||||||
}
|
|
||||||
|
|
||||||
function downloadEmailAsRfc822($full_email, $filename) {
|
|
||||||
$this->json(array('status' => "success", 'body' => $full_email));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ServerRenderViewHandler implements ViewHandler {
|
class ServerRenderViewHandler implements ViewHandler {
|
||||||
function done($address) {
|
function done($address) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user