Feature/removed router (#83)
* CHANGE: refactoring to simplify application, removed router layer REMOVED: JSON api * add licence info
This commit is contained in:
parent
38e1d4fd42
commit
32edf54eda
12 changed files with 217 additions and 418 deletions
|
@ -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.
|
- find config.php automatically in current and parent directories. Show error message if not found.
|
||||||
- use local CSS/JS instead of CDN
|
- use local CSS/JS instead of CDN
|
||||||
- detect missing imap extension and config error
|
- 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
|
## [0.2.1] - 2018-07-01
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
<?php
|
<?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("/
|
$string = preg_replace_callback("/
|
||||||
((?<![\"']) # don't look inside quotes
|
((?<![\"']) # don't look inside quotes
|
||||||
|
|
48
src/boot.php
48
src/boot.php
|
@ -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
38
src/config_helper.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,169 +1,160 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once './imap_client.php';
|
require_once './imap_client.php';
|
||||||
require_once './view.php';
|
|
||||||
|
|
||||||
|
function render_error($status, $msg) {
|
||||||
abstract class Controller {
|
@http_response_code($status);
|
||||||
|
die("{'result': 'error', 'error': '$msg'}");
|
||||||
/**
|
|
||||||
* @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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RedirectToAddressController extends Controller {
|
class DisplayEmailsController {
|
||||||
private $username;
|
|
||||||
private $domain;
|
|
||||||
private $config_blocked_usernames;
|
|
||||||
|
|
||||||
public function __construct(string $username, string $domain, array $config_blocked_usernames) {
|
static function matches() {
|
||||||
$this->username = $username;
|
return !isset($_GET['action']) && !empty($_SERVER['QUERY_STRING'] ?? '');
|
||||||
$this->domain = $domain;
|
|
||||||
$this->config_blocked_usernames = $config_blocked_usernames;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function invoke(ImapClient $imapClient) {
|
static function invoke(ImapClient $imapClient, array $config) {
|
||||||
$user = User::parseUsernameAndDomain($this->username, $this->domain, $this->config_blocked_usernames);
|
$address = $_SERVER['QUERY_STRING'] ?? '';
|
||||||
$this->viewHandler->newAddress($user->username . "@" . $user->domain);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DownloadEmailController extends Controller {
|
// print emails with html template
|
||||||
|
$user = User::parseDomain($address, $config['blocked_usernames']);
|
||||||
private $email_id;
|
$user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);;
|
||||||
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);
|
|
||||||
$emails = $imapClient->get_emails($user);
|
$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) {
|
$newMailIds = array_map(function ($mail) {
|
||||||
return $mail->id;
|
return $mail->id;
|
||||||
}, $emails);
|
}, $emails);
|
||||||
|
|
||||||
$onlyNewMailIds = array_diff($newMailIds, $knownMailIds);
|
$onlyNewMailIds = array_diff($newMailIds, $knownMailIds);
|
||||||
$this->viewHandler->new_mail_counter_json(count($onlyNewMailIds));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RedirectToRandomAddressController extends Controller {
|
HasNewMessagesController::render(count($onlyNewMailIds));
|
||||||
private $config_domains;
|
|
||||||
|
|
||||||
public function __construct($config_domains) {
|
|
||||||
$this->config_domains = $config_domains;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function invoke(ImapClient $imapClient) {
|
static function render($counter) {
|
||||||
$address = User::get_random_address($this->config_domains);
|
header('Content-Type: application/json');
|
||||||
$this->viewHandler->newAddress($address);
|
print json_encode($counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DisplayEmailsController extends Controller {
|
class DownloadEmailController {
|
||||||
private $address;
|
static function matches() {
|
||||||
private $config;
|
return ($_GET['action'] ?? NULL) === "download_email"
|
||||||
|
&& isset($_GET['email_id'])
|
||||||
public function __construct($address, $config) {
|
&& isset($_GET['address']);
|
||||||
$this->address = $address;
|
|
||||||
$this->config = $config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -45,14 +45,14 @@ function niceDate($date) {
|
||||||
<title><?php
|
<title><?php
|
||||||
echo $emails ? "(" . count($emails) . ") " : "";
|
echo $emails ? "(" . count($emails) . ") " : "";
|
||||||
echo $user->address ?></title>
|
echo $user->address ?></title>
|
||||||
<link rel="stylesheet" href="spinner.css">
|
<link rel="stylesheet" href="assets/spinner.css">
|
||||||
<link rel="stylesheet" href="style.css">
|
<link rel="stylesheet" href="assets/custom.css">
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
var mailCount = <?php echo count($emails)?>;
|
var mailCount = <?php echo count($emails)?>;
|
||||||
setInterval(function () {
|
setInterval(function () {
|
||||||
var r = new XMLHttpRequest();
|
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 () {
|
r.onreadystatechange = function () {
|
||||||
if (r.readyState != 4 || r.status != 200) return;
|
if (r.readyState != 4 || r.status != 200) return;
|
||||||
if (r.responseText > 0) {
|
if (r.responseText > 0) {
|
||||||
|
@ -251,7 +251,7 @@ function niceDate($date) {
|
||||||
<?php
|
<?php
|
||||||
if (empty($emails)) { ?>
|
if (empty($emails)) { ?>
|
||||||
<div id="empty-mailbox">
|
<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="spinner">
|
||||||
<div class="rect1"></div>
|
<div class="rect1"></div>
|
||||||
<div class="rect2"></div>
|
<div class="rect2"></div>
|
||||||
|
@ -269,13 +269,13 @@ function niceDate($date) {
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
||||||
|
|
||||||
<!-- <select id="language-selection" class="custom-select" title="Language">-->
|
<!-- <select id="language-selection" class="custom-select" title="Language">-->
|
||||||
<!-- <option selected>English</option>-->
|
<!-- <option selected>English</option>-->
|
||||||
<!-- <option value="1">Deutsch</option>-->
|
<!-- <option value="1">Deutsch</option>-->
|
||||||
<!-- <option value="2">Two</option>-->
|
<!-- <option value="2">Two</option>-->
|
||||||
<!-- <option value="3">Three</option>-->
|
<!-- <option value="3">Three</option>-->
|
||||||
<!-- </select>-->
|
<!-- </select>-->
|
||||||
<!-- <br>-->
|
<!-- <br>-->
|
||||||
|
|
||||||
<small class="text-justify quick-summary">
|
<small class="text-justify quick-summary">
|
||||||
This is a disposable mailbox service. Whoever knows your username, can read your emails.
|
This is a disposable mailbox service. Whoever knows your username, can read your emails.
|
||||||
|
|
|
@ -1,24 +1,40 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// check for common errors
|
||||||
if (version_compare(phpversion(), '7.2', '<')) {
|
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());
|
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 './user.php';
|
||||||
require_once './imap_client.php';
|
require_once './imap_client.php';
|
||||||
require_once './controller.php';
|
require_once './controller.php';
|
||||||
require_once './router.php';
|
|
||||||
|
load_config();
|
||||||
|
|
||||||
$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);
|
if (DisplayEmailsController::matches()) {
|
||||||
$controller = $router->route();
|
DisplayEmailsController::invoke($imapClient, $config);
|
||||||
$controller->setViewHandler(new ServerRenderViewHandler());
|
} elseif (RedirectToAddressController::matches()) {
|
||||||
$controller->invoke($imapClient);
|
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
|
// delete after each request
|
||||||
$imapClient->delete_old_messages($config['delete_messages_older_than']);
|
$imapClient->delete_old_messages($config['delete_messages_older_than']);
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
102
src/json-api.php
102
src/json-api.php
|
@ -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']);
|
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
|
@ -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']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
58
src/view.php
58
src/view.php
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue