From 4775a190b0c960a7c8deaa476fecded092872ae1 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 21:07:15 +0100 Subject: [PATCH] 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; + } +}