Feature/removed router (#83)

* CHANGE: refactoring to simplify application, removed router layer
REMOVED: JSON api
* add licence info
This commit is contained in:
Aravindo Wingeier 2019-03-11 01:09:24 +07:00 committed by GitHub
parent 38e1d4fd42
commit 32edf54eda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 217 additions and 418 deletions

View file

@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file. The format
- find config.php automatically in current and parent directories. Show error message if not found.
- use local CSS/JS instead of CDN
- detect missing imap extension and config error
- refactoring to simplify routing
### Removed
- JSON API (json-api.php), this feature would better fit in a separate project.
## [0.2.1] - 2018-07-01

View file

@ -1,7 +1,15 @@
<?php
class AutoLinkExtension {
static public function auto_link_text(string $string) {
/**
* Adapted from https://plugins.trac.wordpress.org/browser/sem-external-links/trunk/sem-autolink-uri.php
* which is MIT/GPL licenced.
* Author: Denis de Bernardy & Mike Koepke
* Author URI: https://www.semiologic.com
*/
class AutoLinkExtension
{
static public function auto_link_text(string $string)
{
$string = preg_replace_callback("/
((?<![\"']) # don't look inside quotes

View file

@ -1,48 +0,0 @@
<?php
if (version_compare(phpversion(), '7.2', '<')) {
die("ERROR! The php version isn't high enough, you need at least 7.2 to run this application! But you have: " . phpversion());
}
extension_loaded("imap") || die('ERROR: IMAP extension not loaded. Please see the installation instructions in the README.md');
/**
* searches for a config-file in the current and parent directories until found.
* @return path to found config file, or FALSE otherwise.
*/
function find_config($filename='config.php'){
// Count the deph of the current directory, so we know how far we can go up.
$path_length = substr_count(getcwd(),DIRECTORY_SEPARATOR)
+ 1; // also search the current directory
$dir = '.'; // updated in each loop
for($i=0; $i<$path_length;$i++){
$config_filename = $dir . DIRECTORY_SEPARATOR . $filename;
if(file_exists($config_filename)){
return $config_filename;
} else {
$dir = '../' . $dir;
}
}
return FALSE;
}
/**
* searches and loads the config file. Prints an error if not found.
*/
function load_config(){
global $config;
$file = find_config();
if ( $file !== FALSE) {
require_once($file);
if(!isset($config) || !is_array($config)){
die('ERROR: Config file is invalid. Please see the installation instructions in the README.md');
}
} else {
die('ERROR: Config file not found. Please see the installation instructions in the README.md');
}
}
load_config();
# load php dependencies:
require_once './backend-libs/autoload.php';

38
src/config_helper.php Normal file
View file

@ -0,0 +1,38 @@
<?php
/**
* searches for a config-file in the current and parent directories until found.
* @return path to found config file, or FALSE otherwise.
*/
function find_config($filename='config.php'){
// Count the deph of the current directory, so we know how far we can go up.
$path_length = substr_count(getcwd(),DIRECTORY_SEPARATOR)
+ 1; // also search the current directory
$dir = '.'; // updated in each loop
for($i=0; $i<$path_length;$i++){
$config_filename = $dir . DIRECTORY_SEPARATOR . $filename;
if(file_exists($config_filename)){
return $config_filename;
} else {
$dir = '../' . $dir;
}
}
return FALSE;
}
/**
* searches and loads the config file. Prints an error if not found.
*/
function load_config(){
global $config;
$file = find_config();
if ( $file !== FALSE) {
require_once($file);
if(!isset($config) || !is_array($config)){
die('ERROR: Config file is invalid. Please see the installation instructions in the README.md');
}
} else {
die('ERROR: Config file not found. Please see the installation instructions in the README.md');
}
}

View file

@ -1,169 +1,160 @@
<?php
require_once './imap_client.php';
require_once './view.php';
abstract class Controller {
/**
* @var ViewHandler
*/
protected $viewHandler;
public function setViewHandler(ViewHandler $outputHandler) {
$this->viewHandler = $outputHandler;
}
function invoke(ImapClient $imapClient) {
}
function validate_user(User $user, array $config_domains) {
if ($user->isInvalid($config_domains)) {
$this->viewHandler->invalid_input($config_domains);
exit();
}
}
function render_error($status, $msg) {
@http_response_code($status);
die("{'result': 'error', 'error': '$msg'}");
}
class RedirectToAddressController extends Controller {
private $username;
private $domain;
private $config_blocked_usernames;
class DisplayEmailsController {
public function __construct(string $username, string $domain, array $config_blocked_usernames) {
$this->username = $username;
$this->domain = $domain;
$this->config_blocked_usernames = $config_blocked_usernames;
static function matches() {
return !isset($_GET['action']) && !empty($_SERVER['QUERY_STRING'] ?? '');
}
function invoke(ImapClient $imapClient) {
$user = User::parseUsernameAndDomain($this->username, $this->domain, $this->config_blocked_usernames);
$this->viewHandler->newAddress($user->username . "@" . $user->domain);
}
}
static function invoke(ImapClient $imapClient, array $config) {
$address = $_SERVER['QUERY_STRING'] ?? '';
class DownloadEmailController extends Controller {
private $email_id;
private $address;
private $config_domains;
private $config_blocked_usernames;
public function __construct(string $email_id, string $address, array $config_domains, array $config_blocked_usernames) {
$this->email_id = $email_id;
$this->address = $address;
$this->config_domains = $config_domains;
$this->config_blocked_usernames = $config_blocked_usernames;
}
function invoke(ImapClient $imapClient) {
$user = User::parseDomain($this->address, $this->config_blocked_usernames);
$this->validate_user($user, $this->config_domains);
$download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT);
$full_email = $imapClient->load_one_email_fully($download_email_id, $user);
if ($full_email !== null) {
$filename = $user->address . "-" . $download_email_id . ".eml";
$this->viewHandler->downloadEmailAsRfc822($full_email, $filename);
} else {
$this->viewHandler->error(404, 'download error: invalid username/mailid combination');
}
}
}
class DeleteEmailController extends Controller {
private $email_id;
private $address;
private $config_domains;
private $config_blocked_usernames;
public function __construct($email_id, $address, $config_domains, array $config_blocked_usernames) {
$this->email_id = $email_id;
$this->address = $address;
$this->config_domains = $config_domains;
$this->config_blocked_usernames = $config_blocked_usernames;
}
function invoke(ImapClient $imapClient) {
$user = User::parseDomain($this->address, $this->config_blocked_usernames);
$this->validate_user($user, $this->config_domains);
$delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT);
if ($imapClient->delete_email($delete_email_id, $user)) {
$this->viewHandler->done($this->address);
} else {
$this->viewHandler->error(404, 'delete error: invalid username/mailid combination');
}
}
}
class HasNewMessagesController extends Controller {
private $email_ids;
private $address;
private $config_domains;
private $config_blocked_usernames;
public function __construct($email_ids, $address, $config_domains, array $config_blocked_usernames) {
$this->email_ids = $email_ids;
$this->address = $address;
$this->config_domains = $config_domains;
$this->config_blocked_usernames = $config_blocked_usernames;
}
function invoke(ImapClient $imapClient) {
$user = User::parseDomain($this->address, $this->config_blocked_usernames);
$this->validate_user($user, $this->config_domains);
// print emails with html template
$user = User::parseDomain($address, $config['blocked_usernames']);
$user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);;
$emails = $imapClient->get_emails($user);
$knownMailIds = explode('|', $this->email_ids);
DisplayEmailsController::render($emails, $config, $user);
}
static function render($emails, $config, $user) {
// variables that have to be defined here for frontend template: $emails, $config
require "frontend.template.php";
}
}
class RedirectToAddressController {
static function matches() {
return ($_GET['action'] ?? NULL) === "redirect"
&& isset($_POST['username'])
&& isset($_POST['domain']);
}
static function invoke(ImapClient $imapClient, array $config) {
$user = User::parseUsernameAndDomain($_POST['username'], $_POST['domain'], $config['blocked_usernames']);
RedirectToAddressController::render($user->username . "@" . $user->domain);
}
static function render($address) {
header("location: ?$address");
}
}
class RedirectToRandomAddressController {
static function matches() {
return ($_GET['action'] ?? NULL) === 'random';
}
static function invoke(ImapClient $imapClient, array $config) {
$address = User::get_random_address($config{'domains'});
RedirectToAddressController::render($address);
// finish rendering, this might be called from another controller as a fallback
exit();
}
}
class HasNewMessagesController {
static function matches() {
return ($_GET['action'] ?? NULL) === "has_new_messages"
&& isset($_GET['email_ids'])
&& isset($_GET['address']);
}
static function invoke(ImapClient $imapClient, array $config) {
$email_ids = $_GET['email_ids'];
$address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']);
$user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);;
$emails = $imapClient->get_emails($user);
$knownMailIds = explode('|', $email_ids);
$newMailIds = array_map(function ($mail) {
return $mail->id;
}, $emails);
$onlyNewMailIds = array_diff($newMailIds, $knownMailIds);
$this->viewHandler->new_mail_counter_json(count($onlyNewMailIds));
}
}
class RedirectToRandomAddressController extends Controller {
private $config_domains;
public function __construct($config_domains) {
$this->config_domains = $config_domains;
HasNewMessagesController::render(count($onlyNewMailIds));
}
function invoke(ImapClient $imapClient) {
$address = User::get_random_address($this->config_domains);
$this->viewHandler->newAddress($address);
static function render($counter) {
header('Content-Type: application/json');
print json_encode($counter);
}
}
class DisplayEmailsController extends Controller {
private $address;
private $config;
public function __construct($address, $config) {
$this->address = $address;
$this->config = $config;
class DownloadEmailController {
static function matches() {
return ($_GET['action'] ?? NULL) === "download_email"
&& isset($_GET['email_id'])
&& isset($_GET['address']);
}
function invoke(ImapClient $imapClient) {
// print emails with html template
$user = User::parseDomain($this->address, $this->config['blocked_usernames']);
$this->validate_user($user, $this->config['domains']);
$emails = $imapClient->get_emails($user);
$this->viewHandler->displayEmails($emails, $this->config, $user);
static function invoke(ImapClient $imapClient, array $config) {
$email_id = $_GET['email_id'];
$address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']);
$user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);
$download_email_id = filter_var($email_id, FILTER_SANITIZE_NUMBER_INT);
$full_email = $imapClient->load_one_email_fully($download_email_id, $user);
if ($full_email !== null) {
$filename = $user->address . "-" . $download_email_id . ".eml";
DownloadEmailController::renderDownloadEmailAsRfc822($full_email, $filename);
} else {
render_error(404, 'download error: invalid username/mailid combination');
}
}
static function renderDownloadEmailAsRfc822($full_email, $filename) {
header("Content-Type: message/rfc822; charset=utf-8");
header("Content-Disposition: attachment; filename=\"$filename\"");
print $full_email;
}
}
class DeleteEmailController {
static function matches() {
return ($_GET['action'] ?? NULL) === "delete_email"
&& isset($_GET['email_id'])
&& isset($_GET['address']);
}
static function invoke(ImapClient $imapClient, array $config) {
$email_id = $_GET['email_id'];
$address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']);
$user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);
$delete_email_id = filter_var($email_id, FILTER_SANITIZE_NUMBER_INT);
if ($imapClient->delete_email($delete_email_id, $user)) {
RedirectToAddressController::render($address);
} else {
render_error(404, 'delete error: invalid username/mailid combination');
}
}
}
class InvalidRequestController extends Controller {
function invoke(ImapClient $imapClient) {
$this->viewHandler->error(400, "Bad Request");
}
}

View file

@ -45,14 +45,14 @@ function niceDate($date) {
<title><?php
echo $emails ? "(" . count($emails) . ") " : "";
echo $user->address ?></title>
<link rel="stylesheet" href="spinner.css">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="assets/spinner.css">
<link rel="stylesheet" href="assets/custom.css">
<script>
var mailCount = <?php echo count($emails)?>;
setInterval(function () {
var r = new XMLHttpRequest();
r.open("GET", "./json-api.php?action=has_new_messages&address=$<?php echo $user->address?>&email_ids=<?php echo $mailIdsJoinedString?>", true);
r.open("GET", "?action=has_new_messages&address=<?php echo $user->address?>&email_ids=<?php echo $mailIdsJoinedString?>", true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
if (r.responseText > 0) {
@ -251,7 +251,7 @@ function niceDate($date) {
<?php
if (empty($emails)) { ?>
<div id="empty-mailbox">
<p>Emails will appear here automatically. </p>
<p>The mailbox is empty. Checking for new emails automatically. </p>
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
@ -269,13 +269,13 @@ function niceDate($date) {
<div class="container">
<!-- <select id="language-selection" class="custom-select" title="Language">-->
<!-- <option selected>English</option>-->
<!-- <option value="1">Deutsch</option>-->
<!-- <option value="2">Two</option>-->
<!-- <option value="3">Three</option>-->
<!-- </select>-->
<!-- <br>-->
<!-- <select id="language-selection" class="custom-select" title="Language">-->
<!-- <option selected>English</option>-->
<!-- <option value="1">Deutsch</option>-->
<!-- <option value="2">Two</option>-->
<!-- <option value="3">Three</option>-->
<!-- </select>-->
<!-- <br>-->
<small class="text-justify quick-summary">
This is a disposable mailbox service. Whoever knows your username, can read your emails.

View file

@ -1,24 +1,40 @@
<?php
// check for common errors
if (version_compare(phpversion(), '7.2', '<')) {
die("ERROR! The php version isn't high enough, you need at least 7.2 to run this application! But you have: " . phpversion());
}
extension_loaded("imap") || die('ERROR: IMAP extension not loaded. Please see the installation instructions in the README.md');
# load config file and dependencies
require_once './boot.php';
# load php dependencies:
require_once './backend-libs/autoload.php';
require_once './config_helper.php';
require_once './user.php';
require_once './imap_client.php';
require_once './controller.php';
require_once './router.php';
load_config();
$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);
$controller = $router->route();
$controller->setViewHandler(new ServerRenderViewHandler());
$controller->invoke($imapClient);
if (DisplayEmailsController::matches()) {
DisplayEmailsController::invoke($imapClient, $config);
} elseif (RedirectToAddressController::matches()) {
RedirectToAddressController::invoke($imapClient, $config);
} elseif (RedirectToRandomAddressController::matches()) {
RedirectToRandomAddressController::invoke($imapClient, $config);
} elseif (DownloadEmailController::matches()) {
DownloadEmailController::invoke($imapClient, $config);
} elseif (DeleteEmailController::matches()) {
DeleteEmailController::invoke($imapClient, $config);
} elseif (HasNewMessagesController::matches()) {
HasNewMessagesController::invoke($imapClient, $config);
} else {
// If requesting the main site, just redirect to a new random mailbox.
RedirectToRandomAddressController::invoke($imapClient, $config);
}
// delete after each request
$imapClient->delete_old_messages($config['delete_messages_older_than']);
?>

View file

@ -1,102 +0,0 @@
<?php
# load config file and dependencies
require_once './boot.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->method === "GET"
&& $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->method === "DELETE"
&& 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->method === "GET"
&& $this->action === 'random_username') {
return new RedirectToRandomAddressController($this->config['domains']);
} elseif ($this->action === "has_new_messages"
&& isset($this->get_vars['email_ids'])
&& isset($this->get_vars['address'])) {
return new HasNewMessagesController($this->get_vars['email_ids'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']);
} elseif ($this->method === "GET"
&& $this->action === '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));
}
function invalid_input($config_domains) {
$this->error(400, 'Bad Request');
}
function new_mail_counter_json($counter) {
header('Content-Type: application/json');
print json_encode($counter);
}
}
$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']);
?>

View file

@ -1,50 +0,0 @@
<?php
require_once './controller.php';
class Router {
protected $method;
protected $action;
protected $get_vars;
protected $post_vars;
protected $query_string;
protected $config;
public function __construct(string $method, string $action = NULL, array $get_vars, array $post_vars, string $query_string, array $config) {
$this->method = $method;
$this->action = $action;
$this->get_vars = $get_vars;
$this->post_vars = $post_vars;
$this->query_string = $query_string;
$this->config = $config;
}
function route(): Controller {
if ($this->action === "redirect"
&& isset($this->post_vars['username'])
&& isset($this->post_vars['domain'])) {
return new RedirectToAddressController($this->post_vars['username'], $this->post_vars['domain'], $this->config['blocked_usernames']);
} elseif ($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 === 'random') {
return new RedirectToRandomAddressController($this->config['domains']);
} elseif (!empty($this->query_string)) {
return new DisplayEmailsController($this->query_string, $this->config);
} else {
return new RedirectToRandomAddressController($this->config['domains']);
}
}
}

View file

@ -1,58 +0,0 @@
<?php
interface ViewHandler {
function done($address);
/**
* print error and stop program.
* @param $status integer http status
* @param $text string error text
*/
function error($status, $text);
function displayEmails($emails, $config, $user);
function newAddress($string);
function downloadEmailAsRfc822($full_email, $filename);
function invalid_input($config_domains);
function new_mail_counter_json($counter);
}
class ServerRenderViewHandler implements ViewHandler {
function done($address) {
header("location: ?" . $address);
}
function error($status, $msg) {
@http_response_code($status);
die("{'result': 'error', 'error': '$msg'}");
}
function displayEmails($emails, $config, $user) {
// Set variables for frontend template: $emails, $config
require "frontend.template.php";
}
function newAddress($address) {
header("location: ?$address");
}
function downloadEmailAsRfc822($full_email, $filename) {
header("Content-Type: message/rfc822; charset=utf-8");
header("Content-Disposition: attachment; filename=\"$filename\"");
print $full_email;
}
function invalid_input($config_domains) {
$address = User::get_random_address($config_domains);
$this->newAddress($address);
}
function new_mail_counter_json($counter) {
$this->error("not implemented for ServerRenderViewHandler, see json-api");
}
}