From 895800711908458a7803acda387248a04df2851c Mon Sep 17 00:00:00 2001 From: synox Date: Mon, 11 Mar 2019 19:49:45 +0700 Subject: [PATCH] format code --- src/autolink.php | 15 +++++------ src/config.sample.php | 1 - src/config_helper.php | 18 ++++++------- src/controller.php | 56 ++++++++++++++++----------------------- src/frontend.template.php | 54 ++++++++++++++++++------------------- src/imap_client.php | 9 +++---- src/user.php | 3 +-- 7 files changed, 70 insertions(+), 86 deletions(-) diff --git a/src/autolink.php b/src/autolink.php index 07d84db..0a270f0 100644 --- a/src/autolink.php +++ b/src/autolink.php @@ -6,12 +6,10 @@ * Author: Denis de Bernardy & Mike Koepke * Author URI: https://www.semiologic.com */ -class AutoLinkExtension -{ - static public function auto_link_text(string $string) - { - - $string = preg_replace_callback("/ +class AutoLinkExtension { + public static function auto_link_text(string $string) { + $string = preg_replace_callback( + "/ ((?' . $url . ''; - }, $string); + }, + $string + ); return $string; } - } diff --git a/src/config.sample.php b/src/config.sample.php index cdab19d..ec939c3 100644 --- a/src/config.sample.php +++ b/src/config.sample.php @@ -37,4 +37,3 @@ $config['blocked_usernames'] = array('root', 'admin', 'administrator', 'hostmast // Mails are usually show as Text and only if not available as HTML. You can turn this around to prefer HTML over text. $config['prefer_plaintext'] = true; - diff --git a/src/config_helper.php b/src/config_helper.php index 6461406..c6302d8 100644 --- a/src/config_helper.php +++ b/src/config_helper.php @@ -4,35 +4,35 @@ * searches for a config-file in the current and parent directories until found. * @return path to found config file, or FALSE otherwise. */ -function find_config($filename='config.php'){ +function find_config($filename='config.php') { // Count the deph of the current directory, so we know how far we can go up. - $path_length = substr_count(getcwd(),DIRECTORY_SEPARATOR) + $path_length = substr_count(getcwd(), DIRECTORY_SEPARATOR) + 1; // also search the current directory $dir = '.'; // updated in each loop - for($i=0; $i<$path_length;$i++){ + for ($i=0; $i<$path_length;$i++) { $config_filename = $dir . DIRECTORY_SEPARATOR . $filename; - if(file_exists($config_filename)){ + if (file_exists($config_filename)) { return $config_filename; } else { $dir = '../' . $dir; } } - return FALSE; + return false; } /** * searches and loads the config file. Prints an error if not found. */ -function load_config(){ +function load_config() { global $config; $file = find_config(); - if ( $file !== FALSE) { + if ($file !== false) { require_once($file); - if(!isset($config) || !is_array($config)){ + if (!isset($config) || !is_array($config)) { die('ERROR: Config file is invalid. Please see the installation instructions in the README.md'); } } else { die('ERROR: Config file not found. Please see the installation instructions in the README.md'); } -} \ No newline at end of file +} diff --git a/src/controller.php b/src/controller.php index 5683e30..fa24d86 100644 --- a/src/controller.php +++ b/src/controller.php @@ -8,52 +8,50 @@ function render_error($status, $msg) { } class DisplayEmailsController { - - static function matches() { + public static function matches() { return !isset($_GET['action']) && !empty($_SERVER['QUERY_STRING'] ?? ''); } - static function invoke(ImapClient $imapClient, array $config) { + public static function invoke(ImapClient $imapClient, array $config) { $address = $_SERVER['QUERY_STRING'] ?? ''; // print emails with html template $user = User::parseDomain($address, $config['blocked_usernames']); - $user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);; + $user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config); $emails = $imapClient->get_emails($user); DisplayEmailsController::render($emails, $config, $user); } - static function render($emails, $config, $user) { + public static function render($emails, $config, $user) { // variables that have to be defined here for frontend template: $emails, $config require "frontend.template.php"; } } class RedirectToAddressController { - static function matches() { - return ($_GET['action'] ?? NULL) === "redirect" + public static function matches() { + return ($_GET['action'] ?? null) === "redirect" && isset($_POST['username']) && isset($_POST['domain']); } - static function invoke(ImapClient $imapClient, array $config) { + public static function invoke(ImapClient $imapClient, array $config) { $user = User::parseUsernameAndDomain($_POST['username'], $_POST['domain'], $config['blocked_usernames']); RedirectToAddressController::render($user->username . "@" . $user->domain); } - static function render($address) { + public static function render($address) { header("location: ?$address"); } - } class RedirectToRandomAddressController { - static function matches() { - return ($_GET['action'] ?? NULL) === 'random'; + public static function matches() { + return ($_GET['action'] ?? null) === 'random'; } - static function invoke(ImapClient $imapClient, array $config) { + public static function invoke(ImapClient $imapClient, array $config) { $address = User::get_random_address($config{'domains'}); RedirectToAddressController::render($address); @@ -61,25 +59,22 @@ class RedirectToRandomAddressController { // finish rendering, this might be called from another controller as a fallback exit(); } - } class HasNewMessagesController { - - static function matches() { - return ($_GET['action'] ?? NULL) === "has_new_messages" + public static function matches() { + return ($_GET['action'] ?? null) === "has_new_messages" && isset($_GET['email_ids']) && isset($_GET['address']); - } - static function invoke(ImapClient $imapClient, array $config) { + public static function invoke(ImapClient $imapClient, array $config) { $email_ids = $_GET['email_ids']; $address = $_GET['address']; $user = User::parseDomain($address, $config['blocked_usernames']); - $user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config);; + $user->isInvalid($config['domains']) && RedirectToRandomAddressController::invoke($imapClient, $config); $emails = $imapClient->get_emails($user); $knownMailIds = explode('|', $email_ids); @@ -93,22 +88,21 @@ class HasNewMessagesController { HasNewMessagesController::render(count($onlyNewMailIds)); } - static function render($counter) { + public static function render($counter) { header('Content-Type: application/json'); print json_encode($counter); } - } class DownloadEmailController { - static function matches() { - return ($_GET['action'] ?? NULL) === "download_email" + public static function matches() { + return ($_GET['action'] ?? null) === "download_email" && isset($_GET['email_id']) && isset($_GET['address']); } - static function invoke(ImapClient $imapClient, array $config) { + public static function invoke(ImapClient $imapClient, array $config) { $email_id = $_GET['email_id']; $address = $_GET['address']; @@ -125,22 +119,21 @@ class DownloadEmailController { } } - static function renderDownloadEmailAsRfc822($full_email, $filename) { + public static function renderDownloadEmailAsRfc822($full_email, $filename) { header("Content-Type: message/rfc822; charset=utf-8"); header("Content-Disposition: attachment; filename=\"$filename\""); print $full_email; } - } class DeleteEmailController { - static function matches() { - return ($_GET['action'] ?? NULL) === "delete_email" + public static function matches() { + return ($_GET['action'] ?? null) === "delete_email" && isset($_GET['email_id']) && isset($_GET['address']); } - static function invoke(ImapClient $imapClient, array $config) { + public static function invoke(ImapClient $imapClient, array $config) { $email_id = $_GET['email_id']; $address = $_GET['address']; @@ -155,6 +148,3 @@ class DeleteEmailController { } } } - - - diff --git a/src/frontend.template.php b/src/frontend.template.php index 1646fa0..464dd7e 100644 --- a/src/frontend.template.php +++ b/src/frontend.template.php @@ -170,8 +170,7 @@ function niceDate($date) { foreach ($emails as $email) { - $safe_email_id = filter_var($email->id, FILTER_VALIDATE_INT); - ?> + $safe_email_id = filter_var($email->id, FILTER_VALIDATE_INT); ?> + purify($email->textHtml); - purify($email->textHtml); + $safeText = htmlspecialchars($email->textPlain); + $safeText = nl2br($safeText); + $safeText = \AutoLinkExtension::auto_link_text($safeText); - $safeText = htmlspecialchars($email->textPlain); - $safeText = nl2br($safeText); - $safeText = \AutoLinkExtension::auto_link_text($safeText); + $hasHtml = strlen(trim($safeHtml)) > 0; + $hasText = strlen(trim($safeText)) > 0; - $hasHtml = strlen(trim($safeHtml)) > 0; - $hasText = strlen(trim($safeText)) > 0; - - if ($config['prefer_plaintext']) { - if ($hasText) { - echo $safeText; - } else { - echo $safeHtml; - } - } else { - if ($hasHtml) { - echo $safeHtml; - } else { - echo $safeText; - } - } - ?> + if ($config['prefer_plaintext']) { + if ($hasText) { + echo $safeText; + } else { + echo $safeHtml; + } + } else { + if ($hasHtml) { + echo $safeHtml; + } else { + echo $safeText; + } + } ?> - + + if (empty($emails)) { + ?>

The mailbox is empty. Checking for new emails automatically.

@@ -260,7 +259,8 @@ function niceDate($date) {
- + diff --git a/src/imap_client.php b/src/imap_client.php index 07362be..b11a434 100644 --- a/src/imap_client.php +++ b/src/imap_client.php @@ -62,7 +62,6 @@ class ImapClient { } else { return null; } - } /** @@ -71,8 +70,7 @@ class ImapClient { * @param $user User * @return array of emails */ - private - function _load_emails(array $mail_ids, User $user) { + private function _load_emails(array $mail_ids, User $user) { $emails = array(); foreach ($mail_ids as $id) { $mail = $this->mailbox->getMail($id); @@ -87,12 +85,11 @@ class ImapClient { /** * deletes messages older than X days. */ - public - function delete_old_messages(string $delete_messages_older_than) { + public function delete_old_messages(string $delete_messages_older_than) { $ids = $this->mailbox->searchMailbox('BEFORE ' . date('d-M-Y', strtotime($delete_messages_older_than))); foreach ($ids as $id) { $this->mailbox->deleteMail($id); } $this->mailbox->expungeDeletedMails(); } -} \ No newline at end of file +} diff --git a/src/user.php b/src/user.php index a595a38..82612a6 100644 --- a/src/user.php +++ b/src/user.php @@ -20,7 +20,7 @@ class User { public function isInvalid(array $config_domains): bool { if (empty($this->username) || empty($this->domain)) { return true; - } else if (!in_array($this->domain, $config_domains)) { + } elseif (!in_array($this->domain, $config_domains)) { return true; } else { return false; @@ -77,5 +77,4 @@ class User { $username = preg_replace('/^.*@/', "", $username); // remove part before @ return preg_replace('/[^A-Za-z0-9_.+-]/', "", $username); // remove special characters } - }