cleanup, add type information

This commit is contained in:
Synox 2018-02-23 21:44:57 +01:00
parent 2e2fe4b85f
commit 2a35a1737e
4 changed files with 38 additions and 29 deletions

View File

@ -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();

View File

@ -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";
}
}

View File

@ -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();
}
}
}

View File

@ -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);