From 2a35a1737e271bc3c187895dcc61ea4f7533aea1 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:44:57 +0100 Subject: [PATCH] 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);