removed globals
This commit is contained in:
parent
f91c88cad3
commit
76789e8dad
|
@ -6,7 +6,7 @@ abstract class Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
function if_invalid_redirect_to_random(User $user, array $config_domains) {
|
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);
|
$this->redirect_to_random($config_domains);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
@ -40,14 +40,16 @@ abstract class Page {
|
||||||
class RedirectToAddressPage extends Page {
|
class RedirectToAddressPage extends Page {
|
||||||
private $username;
|
private $username;
|
||||||
private $domain;
|
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->username = $username;
|
||||||
$this->domain = $domain;
|
$this->domain = $domain;
|
||||||
|
$this->config_blocked_usernames = $config_blocked_usernames;
|
||||||
}
|
}
|
||||||
|
|
||||||
function invoke(ImapClient $imapClient) {
|
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);
|
header("location: ?" . $user->username . "@" . $user->domain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,16 +59,18 @@ class DownloadEmailPage extends Page {
|
||||||
private $email_id;
|
private $email_id;
|
||||||
private $address;
|
private $address;
|
||||||
private $config_domains;
|
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->email_id = $email_id;
|
||||||
$this->address = $address;
|
$this->address = $address;
|
||||||
$this->config_domains = $config_domains;
|
$this->config_domains = $config_domains;
|
||||||
|
$this->config_blocked_usernames = $config_blocked_usernames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function invoke(ImapClient $imapClient) {
|
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);
|
$this->if_invalid_redirect_to_random($user, $this->config_domains);
|
||||||
|
|
||||||
$download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT);
|
$download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT);
|
||||||
|
@ -79,15 +83,17 @@ class DeleteEmailPage extends Page {
|
||||||
private $email_id;
|
private $email_id;
|
||||||
private $address;
|
private $address;
|
||||||
private $config_domains;
|
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->email_id = $email_id;
|
||||||
$this->address = $address;
|
$this->address = $address;
|
||||||
$this->config_domains = $config_domains;
|
$this->config_domains = $config_domains;
|
||||||
|
$this->config_blocked_usernames = $config_blocked_usernames;
|
||||||
}
|
}
|
||||||
|
|
||||||
function invoke(ImapClient $imapClient) {
|
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);
|
$this->if_invalid_redirect_to_random($user, $this->config_domains);
|
||||||
|
|
||||||
$delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT);
|
$delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT);
|
||||||
|
@ -121,7 +127,7 @@ class DisplayEmailsPage extends Page {
|
||||||
|
|
||||||
function invoke(ImapClient $imapClient) {
|
function invoke(ImapClient $imapClient) {
|
||||||
// print emails with html template
|
// 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']);
|
$this->if_invalid_redirect_to_random($user, $this->config['domains']);
|
||||||
|
|
||||||
global $emails;
|
global $emails;
|
||||||
|
|
|
@ -25,17 +25,17 @@ class Router {
|
||||||
if ($this->action === "redirect"
|
if ($this->action === "redirect"
|
||||||
&& isset($this->post_vars['username'])
|
&& isset($this->post_vars['username'])
|
||||||
&& isset($this->post_vars['domain'])) {
|
&& 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"
|
} elseif ($this->action === "download_email"
|
||||||
&& isset($this->get_vars['download_email_id'])
|
&& isset($this->get_vars['download_email_id'])
|
||||||
&& isset($this->get_vars['address'])) {
|
&& 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"
|
} elseif ($this->action === "delete_email"
|
||||||
&& isset($this->get_vars['delete_email_id'])
|
&& isset($this->get_vars['delete_email_id'])
|
||||||
&& isset($this->get_vars['address'])) {
|
&& 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') {
|
} elseif ($this->action === 'random') {
|
||||||
return new RedirectToRandomAddressPage($this->config['domains']);
|
return new RedirectToRandomAddressPage($this->config['domains']);
|
||||||
|
|
25
src/user.php
25
src/user.php
|
@ -5,29 +5,28 @@ class User {
|
||||||
public $username;
|
public $username;
|
||||||
public $domain;
|
public $domain;
|
||||||
|
|
||||||
public function isInvalid(): bool {
|
public function isInvalid(array $config_domains): bool {
|
||||||
global $config;
|
|
||||||
if (empty($this->username) || empty($this->domain)) {
|
if (empty($this->username) || empty($this->domain)) {
|
||||||
return true;
|
return true;
|
||||||
} else if (!in_array($this->domain, $config['domains'])) {
|
} else if (!in_array($this->domain, $config_domains)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
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);
|
$clean_address = User::_clean_address($address);
|
||||||
$user = new User();
|
$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->domain = User::_clean_domain($clean_address);
|
||||||
$user->address = $user->username . '@' . $user->domain;
|
$user->address = $user->username . '@' . $user->domain;
|
||||||
return $user;
|
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 = new User();
|
||||||
$user->username = User::_clean_username($username);
|
$user->username = User::_clean_username($username, $config_blocked_usernames);
|
||||||
$user->domain = User::_clean_domain($domain);
|
$user->domain = User::_clean_domain($domain);
|
||||||
$user->address = $user->username . '@' . $user->domain;
|
$user->address = $user->username . '@' . $user->domain;
|
||||||
return $user;
|
return $user;
|
||||||
|
@ -35,26 +34,24 @@ class User {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove illegal characters from address.
|
* Remove illegal characters from address.
|
||||||
* @param $address
|
|
||||||
* @return string clean 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));
|
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.
|
* 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
|
* @return string clean username
|
||||||
*/
|
*/
|
||||||
private static function _clean_username($address) {
|
private static function _clean_username(string $address, array $config_blocked_usernames) {
|
||||||
global $config;
|
|
||||||
$username = strtolower($address);
|
$username = strtolower($address);
|
||||||
$username = preg_replace('/@.*$/', "", $username); // remove part after @
|
$username = preg_replace('/@.*$/', "", $username); // remove part after @
|
||||||
$username = preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters
|
$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!
|
// Forbidden name!
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
@ -63,7 +60,7 @@ class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static function _clean_domain($address) {
|
private static function _clean_domain(string $address) {
|
||||||
$username = strtolower($address);
|
$username = strtolower($address);
|
||||||
$username = preg_replace('/^.*@/', "", $username); // remove part before @
|
$username = preg_replace('/^.*@/', "", $username); // remove part before @
|
||||||
return preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters
|
return preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters
|
||||||
|
|
Loading…
Reference in New Issue
Block a user