diff --git a/src/pages.php b/src/pages.php index 5da1ddf..edeaa4a 100644 --- a/src/pages.php +++ b/src/pages.php @@ -6,7 +6,7 @@ abstract class Page { } function if_invalid_redirect_to_random(User $user, array $config_domains) { - if ($user->isInvalid()) { + if ($user->isInvalid($config_domains)) { $this->redirect_to_random($config_domains); exit(); } @@ -40,14 +40,16 @@ abstract class Page { class RedirectToAddressPage extends Page { private $username; private $domain; + private $config_blocked_usernames; - public function __construct(string $username, string $domain) { + public function __construct(string $username, string $domain, array $config_blocked_usernames) { $this->username = $username; $this->domain = $domain; + $this->config_blocked_usernames = $config_blocked_usernames; } function invoke(ImapClient $imapClient) { - $user = User::parseUsernameAndDomain($this->username, $this->domain); + $user = User::parseUsernameAndDomain($this->username, $this->domain, $this->config_blocked_usernames); header("location: ?" . $user->username . "@" . $user->domain); } } @@ -57,16 +59,18 @@ class DownloadEmailPage extends Page { private $email_id; private $address; private $config_domains; + private $config_blocked_usernames; - public function __construct(string $email_id, string $address, array $config_domains) { + 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); + $user = User::parseDomain($this->address, $this->config_blocked_usernames); $this->if_invalid_redirect_to_random($user, $this->config_domains); $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); @@ -79,15 +83,17 @@ class DeleteEmailPage extends Page { private $email_id; private $address; private $config_domains; + private $config_blocked_usernames; - public function __construct($email_id, $address, $config_domains) { + 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); + $user = User::parseDomain($this->address, $this->config_blocked_usernames); $this->if_invalid_redirect_to_random($user, $this->config_domains); $delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); @@ -121,7 +127,7 @@ class DisplayEmailsPage extends Page { function invoke(ImapClient $imapClient) { // print emails with html template - $user = User::parseDomain($this->address); + $user = User::parseDomain($this->address, $this->config['blocked_usernames']); $this->if_invalid_redirect_to_random($user, $this->config['domains']); global $emails; diff --git a/src/router.php b/src/router.php index 39db024..ef97b66 100644 --- a/src/router.php +++ b/src/router.php @@ -25,17 +25,17 @@ class Router { 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']); + return new RedirectToAddressPage($this->post_vars['username'], $this->post_vars['domain'], $this->config['blocked_usernames']); } 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']); + return new DownloadEmailPage($this->get_vars['download_email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']); } 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']); + return new DeleteEmailPage($this->get_vars['delete_email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']); } elseif ($this->action === 'random') { return new RedirectToRandomAddressPage($this->config['domains']); diff --git a/src/user.php b/src/user.php index 6e02745..2f3305a 100644 --- a/src/user.php +++ b/src/user.php @@ -5,29 +5,28 @@ class User { public $username; public $domain; - public function isInvalid(): bool { - global $config; + public function isInvalid(array $config_domains): bool { if (empty($this->username) || empty($this->domain)) { return true; - } else if (!in_array($this->domain, $config['domains'])) { + } else if (!in_array($this->domain, $config_domains)) { return true; } else { return false; } } - public static function parseDomain(string $address): User { + public static function parseDomain(string $address, array $config_blocked_usernames): User { $clean_address = User::_clean_address($address); $user = new User(); - $user->username = User::_clean_username($clean_address); + $user->username = User::_clean_username($clean_address, $config_blocked_usernames); $user->domain = User::_clean_domain($clean_address); $user->address = $user->username . '@' . $user->domain; return $user; } - public static function parseUsernameAndDomain(string $username, string $domain): User { + public static function parseUsernameAndDomain(string $username, string $domain, $config_blocked_usernames): User { $user = new User(); - $user->username = User::_clean_username($username); + $user->username = User::_clean_username($username, $config_blocked_usernames); $user->domain = User::_clean_domain($domain); $user->address = $user->username . '@' . $user->domain; return $user; @@ -35,26 +34,24 @@ class User { /** * Remove illegal characters from address. - * @param $address * @return string clean address */ - private static function _clean_address($address) { + private static function _clean_address(string $address) { return strtolower(filter_var($address, FILTER_SANITIZE_EMAIL)); } /** * Remove illegal characters from username and remove everything after the @-sign. You may extend it if your server supports them. - * @param $address + * * @return string clean username */ - private static function _clean_username($address) { - global $config; + private static function _clean_username(string $address, array $config_blocked_usernames) { $username = strtolower($address); $username = preg_replace('/@.*$/', "", $username); // remove part after @ $username = preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters - if (in_array($username, $config['blocked_usernames'])) { + if (in_array($username, $config_blocked_usernames)) { // Forbidden name! return ''; } @@ -63,7 +60,7 @@ class User { } - private static function _clean_domain($address) { + private static function _clean_domain(string $address) { $username = strtolower($address); $username = preg_replace('/^.*@/', "", $username); // remove part before @ return preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters