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/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/frontend.template.php b/src/frontend.template.php index 16c40e9..4a1e56a 100644 --- a/src/frontend.template.php +++ b/src/frontend.template.php @@ -88,7 +88,7 @@ $purifier = new HTMLPurifier($purifier_config); mailbox: -
+
@@ -117,7 +117,7 @@ $purifier = new HTMLPurifier($purifier_config); ?>
@@ -177,12 +177,12 @@ $purifier = new HTMLPurifier($purifier_config);
diff --git a/src/index.php b/src/index.php index a1d280c..6373137 100644 --- a/src/index.php +++ b/src/index.php @@ -9,48 +9,17 @@ $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'; +require_once './router.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"; - - // delete after each request - delete_old_messages(); -} +$router = new Router($_SERVER['REQUEST_METHOD'], $_GET['action'] ?? NULL, $_GET, $_POST, $_SERVER['QUERY_STRING'], $config); +$page = $router->route(); +$page->invoke(); +// delete after each request +delete_old_messages(); /** * print error and stop program. @@ -184,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); @@ -225,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(); @@ -251,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 new file mode 100644 index 0000000..aa5925b --- /dev/null +++ b/src/pages.php @@ -0,0 +1,114 @@ +isInvalid()) { + redirect_to_random($config_domains); + exit(); + } + } +} + +class RedirectToAddressPage extends Page { + private $username; + private $domain; + + public function __construct(string $username, string $domain) { + $this->username = $username; + $this->domain = $domain; + } + + function invoke() { + $user = User::parseUsernameAndDomain($this->username, $this->domain); + header("location: ?" . $user->username . "@" . $user->domain); + } +} + +class DownloadEmailPage extends Page { + + private $email_id; + private $address; + private $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; + } + + + 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); + download_email($download_email_id, $user); + } +} + + +class DeleteEmailPage 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); + $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); + header("location: ?" . $user->address); + } +} + +class RedirectToRandomAddressPage extends Page { + private $config_domains; + + public function __construct($config_domains) { + $this->config_domains = $config_domains; + } + + function invoke() { + redirect_to_random($this->config_domains); + } + +} + +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); + $this->if_invalid_redirect_to_random($user, $this->config['domains']); + + global $emails; + global $config; + $emails = get_emails($user); + require "frontend.template.php"; + } +} + +class InvalidRequestPage extends Page { + function invoke() { + error(400, "Bad Request"); + } +} diff --git a/src/router.php b/src/router.php new file mode 100644 index 0000000..39db024 --- /dev/null +++ b/src/router.php @@ -0,0 +1,54 @@ +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(): Page { + 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'])) { + 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'])) { + return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains']); + + } elseif ($this->action === '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..2791c55 --- /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(string $address): User { + $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(string $username, string $domain): User { + $user = new User(); + $user->username = _clean_username($username); + $user->domain = _clean_domain($domain); + $user->address = $user->username . '@' . $user->domain; + return $user; + } +}