From 7ece4b393e707c340711e2d2298af4e8464d5a87 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 20:42:13 +0100 Subject: [PATCH 01/11] new router: done with redirect and download --- src/index.php | 142 ++++++++++++++++++++++++++++++++++++-------------- src/pages.php | 18 +++++++ 2 files changed, 122 insertions(+), 38 deletions(-) create mode 100644 src/pages.php diff --git a/src/index.php b/src/index.php index a1d280c..25b5f46 100644 --- a/src/index.php +++ b/src/index.php @@ -5,52 +5,118 @@ require_once '../../config.php'; # load php dependencies: require_once './backend-libs/autoload.php'; + $mailbox = new PhpImap\Mailbox($config['imap']['url'], $config['imap']['username'], $config['imap']['password']); +require_once './pages.php'; -// simple router: -if (isset($_POST['username']) && isset($_POST['domain'])) { - $user = User::parseUsernameAndDomain($_POST['username'], $_POST['domain']); - header("location: ?" . $user->username . "@" . $user->domain); - exit(); -} elseif (isset($_GET['download_email_id']) && isset($_GET['address'])) { - $user = User::parseDomain($_GET['address']); - $download_email_id = filter_input(INPUT_GET, 'download_email_id', FILTER_SANITIZE_NUMBER_INT); - if ($user->isInvalid()) { - redirect_to_random($config['domains']); - exit(); - } - download_email($download_email_id, $user); - exit(); -} elseif (isset($_GET['delete_email_id']) && isset($_GET['address'])) { - $user = User::parseDomain($_GET['address']); - $delete_email_id = filter_input(INPUT_GET, 'delete_email_id', FILTER_SANITIZE_NUMBER_INT); - if ($user->isInvalid()) { - redirect_to_random($config['domains']); - exit(); - } - delete_email($delete_email_id, $user); - header("location: ?" . $user->address); - exit(); -} elseif (isset($_GET['random'])) { - redirect_to_random($config['domains']); - exit(); -} else { - // print emails with html template - $user = User::parseDomain($_SERVER['QUERY_STRING']); - if ($user->isInvalid()) { - redirect_to_random($config['domains']); - exit(); - } - $emails = get_emails($user); - require "frontend.template.php"; +class Router { - // delete after each request - delete_old_messages(); + private $method; + private $action; + private $get_vars; + private $post_vars; + private $query_string; + private $config; + + public function __construct($method, $action, $get_vars, $post_vars, $query_string, $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; + } + + static function init() { + global $config; + return new Router($_SERVER['REQUEST_METHOD'], isset($_GET['action']) ? $_GET['action'] : null, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); + } + + + function route() { + // TODO: use $this->action + if (isset($this->post_vars['username']) && isset($this->post_vars['domain'])) { + return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain']); + } elseif (isset($_GET['download_email_id']) && isset($_GET['address'])) { + return new DownloadEmailPage($_GET['download_email_id'], $_GET['address'], $this->config['domains']); + } else { + return null; + } + } } +abstract class Page { + + function invoke() { + } +} + +class DownloadEmailPage extends Page { + + private $email_id; + private $address; + private $config_domains; + + public function __construct($email_id, $address, $config_domains) { + $this->email_id = $email_id; + $this->address = $address; + $this->config_domains = $config_domains; + } + + + function invoke() { + $user = User::parseDomain($this->address); + $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); + if ($user->isInvalid()) { + redirect_to_random($this->config_domains); + exit(); + } + download_email($download_email_id, $user); + exit(); + } +} + +$router = Router::init(); + +$page = $router->route(); +if ($page != null) { + $page->invoke(); +} else { +// simple router: + if (isset($_GET['delete_email_id']) && isset($_GET['address'])) { + $user = User::parseDomain($_GET['address']); + $delete_email_id = filter_input(INPUT_GET, 'delete_email_id', FILTER_SANITIZE_NUMBER_INT); + if ($user->isInvalid()) { + redirect_to_random($config['domains']); + exit(); + } + delete_email($delete_email_id, $user); + header("location: ?" . $user->address); + exit(); + } elseif (isset($_GET['random'])) { + redirect_to_random($config['domains']); + exit(); + } else { + + + // print emails with html template + $user = User::parseDomain($_SERVER['QUERY_STRING']); + if ($user->isInvalid()) { + redirect_to_random($config['domains']); + exit(); + } + $emails = get_emails($user); + require "frontend.template.php"; + + // delete after each request + delete_old_messages(); + + + } +} /** * print error and stop program. diff --git a/src/pages.php b/src/pages.php new file mode 100644 index 0000000..b9ac576 --- /dev/null +++ b/src/pages.php @@ -0,0 +1,18 @@ +username = $username; + $this->domain = $domain; + } + + function invoke() { + $user = User::parseUsernameAndDomain($this->username, $this->domain); + header("location: ?" . $user->username . "@" . $user->domain); + exit(); + } +} + From 09d76a628b64c5616ad0bc9aeb82b7b14ae37d34 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:01:07 +0100 Subject: [PATCH 02/11] new router: DisplayEmailsPage --- src/index.php | 68 ++++++-------------------------------- src/pages.php | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 58 deletions(-) diff --git a/src/index.php b/src/index.php index 25b5f46..e6a7f00 100644 --- a/src/index.php +++ b/src/index.php @@ -40,8 +40,16 @@ class Router { // TODO: use $this->action if (isset($this->post_vars['username']) && isset($this->post_vars['domain'])) { return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain']); - } elseif (isset($_GET['download_email_id']) && isset($_GET['address'])) { - return new DownloadEmailPage($_GET['download_email_id'], $_GET['address'], $this->config['domains']); + } elseif (isset($this->get_vars['download_email_id']) && isset($this->get_vars['address'])) { + return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains']); + } elseif (isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { + return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); + } elseif (isset($this->get_vars['random'])) { + return new RedirectToRandomAddressPage($this->config['domains']); + } elseif (empty($this->query_string)) { + return new RedirectToRandomAddressPage($this->config['domains']); + } elseif (!empty($this->query_string)) { + return new DisplayEmailsPage($this->query_string, $this->config); } else { return null; } @@ -54,68 +62,12 @@ abstract class Page { } } -class DownloadEmailPage extends Page { - - private $email_id; - private $address; - private $config_domains; - - public function __construct($email_id, $address, $config_domains) { - $this->email_id = $email_id; - $this->address = $address; - $this->config_domains = $config_domains; - } - - - function invoke() { - $user = User::parseDomain($this->address); - $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); - if ($user->isInvalid()) { - redirect_to_random($this->config_domains); - exit(); - } - download_email($download_email_id, $user); - exit(); - } -} $router = Router::init(); $page = $router->route(); if ($page != null) { $page->invoke(); -} else { -// simple router: - if (isset($_GET['delete_email_id']) && isset($_GET['address'])) { - $user = User::parseDomain($_GET['address']); - $delete_email_id = filter_input(INPUT_GET, 'delete_email_id', FILTER_SANITIZE_NUMBER_INT); - if ($user->isInvalid()) { - redirect_to_random($config['domains']); - exit(); - } - delete_email($delete_email_id, $user); - header("location: ?" . $user->address); - exit(); - } elseif (isset($_GET['random'])) { - redirect_to_random($config['domains']); - exit(); - } else { - - - // print emails with html template - $user = User::parseDomain($_SERVER['QUERY_STRING']); - if ($user->isInvalid()) { - redirect_to_random($config['domains']); - exit(); - } - $emails = get_emails($user); - require "frontend.template.php"; - - // delete after each request - delete_old_messages(); - - - } } /** diff --git a/src/pages.php b/src/pages.php index b9ac576..3bd7428 100644 --- a/src/pages.php +++ b/src/pages.php @@ -16,3 +16,93 @@ class RedirectToAddressPage extends Page { } } +class DownloadEmailPage extends Page { + + private $email_id; + private $address; + private $config_domains; + + public function __construct($email_id, $address, $config_domains) { + $this->email_id = $email_id; + $this->address = $address; + $this->config_domains = $config_domains; + } + + + function invoke() { + $user = User::parseDomain($this->address); + $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); + if ($user->isInvalid()) { + redirect_to_random($this->config_domains); + exit(); + } + download_email($download_email_id, $user); + exit(); + } +} + + +class DeleteEmailPage extends Page { + private $email_id; + private $address; + private $all_domains; + + public function __construct($email_id, $address, $all_domains) { + $this->email_id = $email_id; + $this->address = $address; + $this->all_domains = $all_domains; + } + + function invoke() { + $user = User::parseDomain($this->address); + $delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); + if ($user->isInvalid()) { + redirect_to_random($this->all_domains); + exit(); + } + delete_email($delete_email_id, $user); + header("location: ?" . $user->address); + exit(); + } +} + +class RedirectToRandomAddressPage extends Page { + private $all_domains; + + public function __construct($all_domains) { + $this->all_domains = $all_domains; + } + + function invoke() { + redirect_to_random($this->all_domains); + exit(); + } + +} + +class DisplayEmailsPage extends Page { + private $address; + private $config; + + public function __construct($address, $config) { + $this->address = $address; + $this->config = $config; + } + + + function invoke() { + // print emails with html template + $user = User::parseDomain($this->address); + if ($user->isInvalid()) { + redirect_to_random($this->config['domains']); + exit(); + } + global $emails; + global $config; + $emails = get_emails($user); + require "frontend.template.php"; + + // delete after each request + delete_old_messages(); + } +} \ No newline at end of file From 4775a190b0c960a7c8deaa476fecded092872ae1 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:07:15 +0100 Subject: [PATCH 03/11] new router: split into multiple files --- src/autolink.php | 49 ++++++++++++++++ src/index.php | 145 +++-------------------------------------------- src/pages.php | 14 ++++- src/router.php | 47 +++++++++++++++ src/user.php | 35 ++++++++++++ 5 files changed, 150 insertions(+), 140 deletions(-) create mode 100644 src/autolink.php create mode 100644 src/router.php create mode 100644 src/user.php diff --git a/src/autolink.php b/src/autolink.php new file mode 100644 index 0000000..d9ea250 --- /dev/null +++ b/src/autolink.php @@ -0,0 +1,49 @@ +' . $url . ''; + }, $string); + + return $string; + } + +} diff --git a/src/index.php b/src/index.php index e6a7f00..401d7ad 100644 --- a/src/index.php +++ b/src/index.php @@ -5,70 +5,21 @@ require_once '../../config.php'; # load php dependencies: require_once './backend-libs/autoload.php'; - $mailbox = new PhpImap\Mailbox($config['imap']['url'], $config['imap']['username'], $config['imap']['password']); +require_once './user.php'; +require_once './autolink.php'; require_once './pages.php'; - -class Router { - - private $method; - private $action; - private $get_vars; - private $post_vars; - private $query_string; - private $config; - - public function __construct($method, $action, $get_vars, $post_vars, $query_string, $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; - } - - static function init() { - global $config; - return new Router($_SERVER['REQUEST_METHOD'], isset($_GET['action']) ? $_GET['action'] : null, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); - } - - - function route() { - // TODO: use $this->action - if (isset($this->post_vars['username']) && isset($this->post_vars['domain'])) { - return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain']); - } elseif (isset($this->get_vars['download_email_id']) && isset($this->get_vars['address'])) { - return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains']); - } elseif (isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { - return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); - } elseif (isset($this->get_vars['random'])) { - return new RedirectToRandomAddressPage($this->config['domains']); - } elseif (empty($this->query_string)) { - return new RedirectToRandomAddressPage($this->config['domains']); - } elseif (!empty($this->query_string)) { - return new DisplayEmailsPage($this->query_string, $this->config); - } else { - return null; - } - } -} - -abstract class Page { - - function invoke() { - } -} - +require_once './router.php'; $router = Router::init(); - $page = $router->route(); -if ($page != null) { - $page->invoke(); -} +$page->invoke(); + +// delete after each request +delete_old_messages(); /** * print error and stop program. @@ -202,40 +153,6 @@ function _clean_username($address) { return $username; } -class User { - public $address; - public $username; - public $domain; - - public function isInvalid() { - global $config; - if (empty($this->username) || empty($this->domain)) { - return true; - } else if (!in_array($this->domain, $config['domains'])) { - return true; - } else { - return false; - } - } - - public static function parseDomain($address) { - $clean_address = _clean_address($address); - $user = new User(); - $user->username = _clean_username($clean_address); - $user->domain = _clean_domain($clean_address); - $user->address = $user->username . '@' . $user->domain; - return $user; - } - - public static function parseUsernameAndDomain($username, $domain) { - $user = new User(); - $user->username = _clean_username($username); - $user->domain = _clean_domain($domain); - $user->address = $user->username . '@' . $user->domain; - return $user; - } -} - function _clean_domain($address) { $username = strtolower($address); @@ -269,52 +186,4 @@ function delete_old_messages() { } -class AutoLinkExtension { - static public function auto_link_text($string) { - - $string = preg_replace_callback("/ - ((?' . $url . ''; - }, $string); - - return $string; - } - -} - ?> \ No newline at end of file diff --git a/src/pages.php b/src/pages.php index 3bd7428..425228c 100644 --- a/src/pages.php +++ b/src/pages.php @@ -1,5 +1,11 @@ method = $method; + $this->action = $action; + $this->get_vars = $get_vars; + $this->post_vars = $post_vars; + $this->query_string = $query_string; + $this->config = $config; + } + + static function init() { + global $config; + return new Router($_SERVER['REQUEST_METHOD'], isset($_GET['action']) ? $_GET['action'] : null, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); + } + + + function route() { + // TODO: use $this->action + if (isset($this->post_vars['username']) && isset($this->post_vars['domain'])) { + return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain']); + } elseif (isset($this->get_vars['download_email_id']) && isset($this->get_vars['address'])) { + return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains']); + } elseif (isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { + return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); + } elseif (isset($this->get_vars['random'])) { + return new RedirectToRandomAddressPage($this->config['domains']); + } elseif (empty($this->query_string)) { + return new RedirectToRandomAddressPage($this->config['domains']); + } elseif (!empty($this->query_string)) { + return new DisplayEmailsPage($this->query_string, $this->config); + } else { + return new InvalidRequestPage(); + } + } +} \ No newline at end of file diff --git a/src/user.php b/src/user.php new file mode 100644 index 0000000..d8f1881 --- /dev/null +++ b/src/user.php @@ -0,0 +1,35 @@ +username) || empty($this->domain)) { + return true; + } else if (!in_array($this->domain, $config['domains'])) { + return true; + } else { + return false; + } + } + + public static function parseDomain($address) { + $clean_address = _clean_address($address); + $user = new User(); + $user->username = _clean_username($clean_address); + $user->domain = _clean_domain($clean_address); + $user->address = $user->username . '@' . $user->domain; + return $user; + } + + public static function parseUsernameAndDomain($username, $domain) { + $user = new User(); + $user->username = _clean_username($username); + $user->domain = _clean_domain($domain); + $user->address = $user->username . '@' . $user->domain; + return $user; + } +} From ee18dd058177030605af4b5a646eba0ea9233b3a Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:19:35 +0100 Subject: [PATCH 04/11] require php 7 --- README.md | 4 ++-- src/pages.php | 25 ++++++++++--------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f603daf..a0a028d 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ A **self-hosted** disposable mailbox service (aka trash mail) :cloud: :envelop * Automatic refresh. Download and delete your emails. * Display emails as text or html with sanitization filter. * Display emails based on one [catch-all imap mailbox](https://www.google.ch/search?q=how+to+setup+catch-all+imap+mailbox). -* Only requires PHP >=5.3.0 and [imap extension](http://php.net/manual/book.imap.php) +* Only requires PHP >=7.2 and [imap extension](http://php.net/manual/book.imap.php) ## Usage ### Requirements -* webserver with php >=5.3.0 +* webserver with php >=7.2 * php [imap extension](http://php.net/manual/book.imap.php) * IMAP account and a domain with [catch-all configuration](https://www.google.ch/search?q=how+to+setup+catch-all+imap+mailbox). (all emails go to one mailbox). diff --git a/src/pages.php b/src/pages.php index 425228c..842cec2 100644 --- a/src/pages.php +++ b/src/pages.php @@ -18,7 +18,6 @@ class RedirectToAddressPage extends Page { function invoke() { $user = User::parseUsernameAndDomain($this->username, $this->domain); header("location: ?" . $user->username . "@" . $user->domain); - exit(); } } @@ -40,10 +39,9 @@ class DownloadEmailPage extends Page { $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); if ($user->isInvalid()) { redirect_to_random($this->config_domains); - exit(); + } else { + download_email($download_email_id, $user); } - download_email($download_email_id, $user); - exit(); } } @@ -64,11 +62,10 @@ class DeleteEmailPage extends Page { $delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); if ($user->isInvalid()) { redirect_to_random($this->all_domains); - exit(); + } else { + delete_email($delete_email_id, $user); + header("location: ?" . $user->address); } - delete_email($delete_email_id, $user); - header("location: ?" . $user->address); - exit(); } } @@ -81,7 +78,6 @@ class RedirectToRandomAddressPage extends Page { function invoke() { redirect_to_random($this->all_domains); - exit(); } } @@ -101,13 +97,12 @@ class DisplayEmailsPage extends Page { $user = User::parseDomain($this->address); if ($user->isInvalid()) { redirect_to_random($this->config['domains']); - exit(); + } else { + global $emails; + global $config; + $emails = get_emails($user); + require "frontend.template.php"; } - global $emails; - global $config; - $emails = get_emails($user); - require "frontend.template.php"; - } } From 2e2fe4b85f48518e42351eacadb71b299941efdb Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:23:35 +0100 Subject: [PATCH 05/11] cleanup --- src/router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/router.php b/src/router.php index b907852..9ee86d5 100644 --- a/src/router.php +++ b/src/router.php @@ -22,7 +22,7 @@ class Router { static function init() { global $config; - return new Router($_SERVER['REQUEST_METHOD'], isset($_GET['action']) ? $_GET['action'] : null, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); + return new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? array(), $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); } From 2a35a1737e271bc3c187895dcc61ea4f7533aea1 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:44:57 +0100 Subject: [PATCH 06/11] cleanup, add type information --- src/index.php | 2 +- src/pages.php | 45 +++++++++++++++++++++++---------------------- src/router.php | 16 ++++++++++++---- src/user.php | 4 ++-- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/index.php b/src/index.php index 401d7ad..b2ef149 100644 --- a/src/index.php +++ b/src/index.php @@ -160,7 +160,7 @@ function _clean_domain($address) { return preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters } -function redirect_to_random($domains) { +function redirect_to_random(array $domains) { $wordLength = rand(3, 8); $container = new PronounceableWord_DependencyInjectionContainer(); $generator = $container->getGenerator(); diff --git a/src/pages.php b/src/pages.php index 842cec2..5a46941 100644 --- a/src/pages.php +++ b/src/pages.php @@ -4,13 +4,20 @@ abstract class Page { function invoke() { } + + function if_invalid_redirect_to_random(User $user, array $config_domains) { + if ($user->isInvalid()) { + redirect_to_random($config_domains); + exit(); + } + } } class RedirectToAddressPage extends Page { private $username; private $domain; - public function __construct($username, $domain) { + public function __construct(string $username, string $domain) { $this->username = $username; $this->domain = $domain; } @@ -27,7 +34,7 @@ class DownloadEmailPage extends Page { private $address; private $config_domains; - public function __construct($email_id, $address, $config_domains) { + public function __construct(string $email_id, string $address, array $config_domains) { $this->email_id = $email_id; $this->address = $address; $this->config_domains = $config_domains; @@ -36,12 +43,10 @@ class DownloadEmailPage extends Page { function invoke() { $user = User::parseDomain($this->address); + $this->if_invalid_redirect_to_random($user, $this->config_domains); + $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); - if ($user->isInvalid()) { - redirect_to_random($this->config_domains); - } else { - download_email($download_email_id, $user); - } + download_email($download_email_id, $user); } } @@ -59,13 +64,11 @@ class DeleteEmailPage extends Page { function invoke() { $user = User::parseDomain($this->address); + $this->if_invalid_redirect_to_random($user, $this->all_domains); + $delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); - if ($user->isInvalid()) { - redirect_to_random($this->all_domains); - } else { - delete_email($delete_email_id, $user); - header("location: ?" . $user->address); - } + delete_email($delete_email_id, $user); + header("location: ?" . $user->address); } } @@ -95,14 +98,12 @@ class DisplayEmailsPage extends Page { function invoke() { // print emails with html template $user = User::parseDomain($this->address); - if ($user->isInvalid()) { - redirect_to_random($this->config['domains']); - } else { - global $emails; - global $config; - $emails = get_emails($user); - require "frontend.template.php"; - } + $this->if_invalid_redirect_to_random($user, $this->config['domains']); + + global $emails; + global $config; + $emails = get_emails($user); + require "frontend.template.php"; } } @@ -110,4 +111,4 @@ class InvalidRequestPage extends Page { function invoke() { error(400, "Bad Request"); } -} \ No newline at end of file +} diff --git a/src/router.php b/src/router.php index 9ee86d5..8e8ddc0 100644 --- a/src/router.php +++ b/src/router.php @@ -11,7 +11,7 @@ class Router { private $query_string; private $config; - public function __construct($method, $action, $get_vars, $post_vars, $query_string, $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; @@ -20,28 +20,36 @@ class Router { $this->config = $config; } - static function init() { + static function init(): Router { global $config; - return new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? array(), $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); + return new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); + } - function route() { + function route(): Page { // TODO: use $this->action if (isset($this->post_vars['username']) && isset($this->post_vars['domain'])) { return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain']); + } elseif (isset($this->get_vars['download_email_id']) && isset($this->get_vars['address'])) { return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains']); + } elseif (isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); + } elseif (isset($this->get_vars['random'])) { return new RedirectToRandomAddressPage($this->config['domains']); + } elseif (empty($this->query_string)) { return new RedirectToRandomAddressPage($this->config['domains']); + } elseif (!empty($this->query_string)) { return new DisplayEmailsPage($this->query_string, $this->config); + } else { return new InvalidRequestPage(); + } } } \ No newline at end of file diff --git a/src/user.php b/src/user.php index d8f1881..7d17a22 100644 --- a/src/user.php +++ b/src/user.php @@ -16,7 +16,7 @@ class User { } } - public static function parseDomain($address) { + public static function parseDomain(string $address) { $clean_address = _clean_address($address); $user = new User(); $user->username = _clean_username($clean_address); @@ -25,7 +25,7 @@ class User { return $user; } - public static function parseUsernameAndDomain($username, $domain) { + public static function parseUsernameAndDomain(string $username, string $domain) { $user = new User(); $user->username = _clean_username($username); $user->domain = _clean_domain($domain); From 92ee25dbf63923faa972227068b8b12496d0e289 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:46:19 +0100 Subject: [PATCH 07/11] cleanup, add type information --- src/pages.php | 16 ++++++++-------- src/user.php | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/pages.php b/src/pages.php index 5a46941..aa5925b 100644 --- a/src/pages.php +++ b/src/pages.php @@ -54,17 +54,17 @@ class DownloadEmailPage extends Page { class DeleteEmailPage extends Page { private $email_id; private $address; - private $all_domains; + private $config_domains; - public function __construct($email_id, $address, $all_domains) { + public function __construct($email_id, $address, $config_domains) { $this->email_id = $email_id; $this->address = $address; - $this->all_domains = $all_domains; + $this->config_domains = $config_domains; } function invoke() { $user = User::parseDomain($this->address); - $this->if_invalid_redirect_to_random($user, $this->all_domains); + $this->if_invalid_redirect_to_random($user, $this->config_domains); $delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); delete_email($delete_email_id, $user); @@ -73,14 +73,14 @@ class DeleteEmailPage extends Page { } class RedirectToRandomAddressPage extends Page { - private $all_domains; + private $config_domains; - public function __construct($all_domains) { - $this->all_domains = $all_domains; + public function __construct($config_domains) { + $this->config_domains = $config_domains; } function invoke() { - redirect_to_random($this->all_domains); + redirect_to_random($this->config_domains); } } diff --git a/src/user.php b/src/user.php index 7d17a22..2791c55 100644 --- a/src/user.php +++ b/src/user.php @@ -5,7 +5,7 @@ class User { public $username; public $domain; - public function isInvalid() { + public function isInvalid(): bool { global $config; if (empty($this->username) || empty($this->domain)) { return true; @@ -16,7 +16,7 @@ class User { } } - public static function parseDomain(string $address) { + public static function parseDomain(string $address): User { $clean_address = _clean_address($address); $user = new User(); $user->username = _clean_username($clean_address); @@ -25,7 +25,7 @@ class User { return $user; } - public static function parseUsernameAndDomain(string $username, string $domain) { + public static function parseUsernameAndDomain(string $username, string $domain): User { $user = new User(); $user->username = _clean_username($username); $user->domain = _clean_domain($domain); From e66fd86dc715575f1af39e72d5148d6bfaeaa8bd Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:55:08 +0100 Subject: [PATCH 08/11] use actions --- src/frontend.template.php | 4 ++-- src/router.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/frontend.template.php b/src/frontend.template.php index 16c40e9..c145e39 100644 --- a/src/frontend.template.php +++ b/src/frontend.template.php @@ -88,7 +88,7 @@ $purifier = new HTMLPurifier($purifier_config); mailbox: -
+
diff --git a/src/router.php b/src/router.php index 147b5c7..ac53b03 100644 --- a/src/router.php +++ b/src/router.php @@ -35,7 +35,7 @@ class Router { } elseif ($this->action === "download_email" && isset($this->get_vars['download_email_id']) && isset($this->get_vars['address'])) { return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains']); - } elseif (isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { + } elseif ($this->action === "delete_email" && isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); } elseif (isset($this->get_vars['random'])) { From 664edcc1cb08803fe8ff9ec76fd91f11bcd72d8e Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:57:14 +0100 Subject: [PATCH 10/11] use actions --- src/frontend.template.php | 2 +- src/router.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/frontend.template.php b/src/frontend.template.php index b5143a0..4a1e56a 100644 --- a/src/frontend.template.php +++ b/src/frontend.template.php @@ -117,7 +117,7 @@ $purifier = new HTMLPurifier($purifier_config); ?>
diff --git a/src/router.php b/src/router.php index ac53b03..3aef6e5 100644 --- a/src/router.php +++ b/src/router.php @@ -38,7 +38,7 @@ class Router { } elseif ($this->action === "delete_email" && isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); - } elseif (isset($this->get_vars['random'])) { + } elseif ($this->action === 'random') { return new RedirectToRandomAddressPage($this->config['domains']); } elseif (empty($this->query_string)) { From 5149869952461b3c0a6e13b19d50cbf359dff431 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 22:02:22 +0100 Subject: [PATCH 11/11] cleanup --- src/index.php | 2 +- src/router.php | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/index.php b/src/index.php index b2ef149..6373137 100644 --- a/src/index.php +++ b/src/index.php @@ -14,7 +14,7 @@ require_once './autolink.php'; require_once './pages.php'; require_once './router.php'; -$router = Router::init(); +$router = new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); $page = $router->route(); $page->invoke(); diff --git a/src/router.php b/src/router.php index 3aef6e5..39db024 100644 --- a/src/router.php +++ b/src/router.php @@ -20,22 +20,21 @@ class Router { $this->config = $config; } - static function init(): Router { - global $config; - return new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); - - } - function route(): Page { - // TODO: use $this->action - if ($this->action === "redirect" && isset($this->post_vars['username']) && isset($this->post_vars['domain'])) { + if ($this->action === "redirect" + && isset($this->post_vars['username']) + && isset($this->post_vars['domain'])) { return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain']); - } elseif ($this->action === "download_email" && isset($this->get_vars['download_email_id']) && isset($this->get_vars['address'])) { + } elseif ($this->action === "download_email" + && isset($this->get_vars['download_email_id']) + && isset($this->get_vars['address'])) { return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains']); - } elseif ($this->action === "delete_email" && isset($this->get_vars['delete_email_id']) && isset($this->get_vars['address'])) { + } elseif ($this->action === "delete_email" + && isset($this->get_vars['delete_email_id']) + && isset($this->get_vars['address'])) { return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); } elseif ($this->action === 'random') {