format code

This commit is contained in:
synox 2019-03-11 19:49:45 +07:00
parent 11b9216a0d
commit 8958007119
7 changed files with 70 additions and 86 deletions

View file

@ -6,12 +6,10 @@
* Author: Denis de Bernardy & Mike Koepke * Author: Denis de Bernardy & Mike Koepke
* Author URI: https://www.semiologic.com * Author URI: https://www.semiologic.com
*/ */
class AutoLinkExtension class AutoLinkExtension {
{ public static function auto_link_text(string $string) {
static public function auto_link_text(string $string) $string = preg_replace_callback(
{ "/
$string = preg_replace_callback("/
((?<![\"']) # don't look inside quotes ((?<![\"']) # don't look inside quotes
(\b (\b
( # protocol or www. ( # protocol or www.
@ -49,9 +47,10 @@ class AutoLinkExtension
$href = 'http://' . $href; $href = 'http://' . $href;
} }
return '<a href="' . $href . '" rel="noreferrer">' . $url . '</a>'; return '<a href="' . $href . '" rel="noreferrer">' . $url . '</a>';
}, $string); },
$string
);
return $string; return $string;
} }
} }

View file

@ -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. // 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; $config['prefer_plaintext'] = true;

View file

@ -4,32 +4,32 @@
* searches for a config-file in the current and parent directories until found. * searches for a config-file in the current and parent directories until found.
* @return path to found config file, or FALSE otherwise. * @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. // 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 + 1; // also search the current directory
$dir = '.'; // updated in each loop $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; $config_filename = $dir . DIRECTORY_SEPARATOR . $filename;
if(file_exists($config_filename)){ if (file_exists($config_filename)) {
return $config_filename; return $config_filename;
} else { } else {
$dir = '../' . $dir; $dir = '../' . $dir;
} }
} }
return FALSE; return false;
} }
/** /**
* searches and loads the config file. Prints an error if not found. * searches and loads the config file. Prints an error if not found.
*/ */
function load_config(){ function load_config() {
global $config; global $config;
$file = find_config(); $file = find_config();
if ( $file !== FALSE) { if ($file !== false) {
require_once($file); 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'); die('ERROR: Config file is invalid. Please see the installation instructions in the README.md');
} }
} else { } else {

View file

@ -8,52 +8,50 @@ function render_error($status, $msg) {
} }
class DisplayEmailsController { class DisplayEmailsController {
public static function matches() {
static function matches() {
return !isset($_GET['action']) && !empty($_SERVER['QUERY_STRING'] ?? ''); 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'] ?? ''; $address = $_SERVER['QUERY_STRING'] ?? '';
// print emails with html template // print emails with html template
$user = User::parseDomain($address, $config['blocked_usernames']); $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); $emails = $imapClient->get_emails($user);
DisplayEmailsController::render($emails, $config, $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 // variables that have to be defined here for frontend template: $emails, $config
require "frontend.template.php"; require "frontend.template.php";
} }
} }
class RedirectToAddressController { class RedirectToAddressController {
static function matches() { public static function matches() {
return ($_GET['action'] ?? NULL) === "redirect" return ($_GET['action'] ?? null) === "redirect"
&& isset($_POST['username']) && isset($_POST['username'])
&& isset($_POST['domain']); && 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']); $user = User::parseUsernameAndDomain($_POST['username'], $_POST['domain'], $config['blocked_usernames']);
RedirectToAddressController::render($user->username . "@" . $user->domain); RedirectToAddressController::render($user->username . "@" . $user->domain);
} }
static function render($address) { public static function render($address) {
header("location: ?$address"); header("location: ?$address");
} }
} }
class RedirectToRandomAddressController { class RedirectToRandomAddressController {
static function matches() { public static function matches() {
return ($_GET['action'] ?? NULL) === 'random'; 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'}); $address = User::get_random_address($config{'domains'});
RedirectToAddressController::render($address); RedirectToAddressController::render($address);
@ -61,25 +59,22 @@ class RedirectToRandomAddressController {
// finish rendering, this might be called from another controller as a fallback // finish rendering, this might be called from another controller as a fallback
exit(); exit();
} }
} }
class HasNewMessagesController { class HasNewMessagesController {
public static function matches() {
static function matches() { return ($_GET['action'] ?? null) === "has_new_messages"
return ($_GET['action'] ?? NULL) === "has_new_messages"
&& isset($_GET['email_ids']) && isset($_GET['email_ids'])
&& isset($_GET['address']); && isset($_GET['address']);
} }
static function invoke(ImapClient $imapClient, array $config) { public static function invoke(ImapClient $imapClient, array $config) {
$email_ids = $_GET['email_ids']; $email_ids = $_GET['email_ids'];
$address = $_GET['address']; $address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']); $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); $emails = $imapClient->get_emails($user);
$knownMailIds = explode('|', $email_ids); $knownMailIds = explode('|', $email_ids);
@ -93,22 +88,21 @@ class HasNewMessagesController {
HasNewMessagesController::render(count($onlyNewMailIds)); HasNewMessagesController::render(count($onlyNewMailIds));
} }
static function render($counter) { public static function render($counter) {
header('Content-Type: application/json'); header('Content-Type: application/json');
print json_encode($counter); print json_encode($counter);
} }
} }
class DownloadEmailController { class DownloadEmailController {
static function matches() { public static function matches() {
return ($_GET['action'] ?? NULL) === "download_email" return ($_GET['action'] ?? null) === "download_email"
&& isset($_GET['email_id']) && isset($_GET['email_id'])
&& isset($_GET['address']); && isset($_GET['address']);
} }
static function invoke(ImapClient $imapClient, array $config) { public static function invoke(ImapClient $imapClient, array $config) {
$email_id = $_GET['email_id']; $email_id = $_GET['email_id'];
$address = $_GET['address']; $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-Type: message/rfc822; charset=utf-8");
header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Disposition: attachment; filename=\"$filename\"");
print $full_email; print $full_email;
} }
} }
class DeleteEmailController { class DeleteEmailController {
static function matches() { public static function matches() {
return ($_GET['action'] ?? NULL) === "delete_email" return ($_GET['action'] ?? null) === "delete_email"
&& isset($_GET['email_id']) && isset($_GET['email_id'])
&& isset($_GET['address']); && isset($_GET['address']);
} }
static function invoke(ImapClient $imapClient, array $config) { public static function invoke(ImapClient $imapClient, array $config) {
$email_id = $_GET['email_id']; $email_id = $_GET['email_id'];
$address = $_GET['address']; $address = $_GET['address'];
@ -155,6 +148,3 @@ class DeleteEmailController {
} }
} }
} }

View file

@ -170,8 +170,7 @@ function niceDate($date) {
foreach ($emails foreach ($emails
as $email) { as $email) {
$safe_email_id = filter_var($email->id, FILTER_VALIDATE_INT); $safe_email_id = filter_var($email->id, FILTER_VALIDATE_INT); ?>
?>
<a class="list-group-item list-group-item-action email-list-item" data-toggle="collapse" <a class="list-group-item list-group-item-action email-list-item" data-toggle="collapse"
href="#mail-box-<?php echo $email->id ?>" href="#mail-box-<?php echo $email->id ?>"
@ -217,7 +216,6 @@ function niceDate($date) {
Delete Delete
</a> </a>
</div> </div>
<?php <?php
$safeHtml = $purifier->purify($email->textHtml); $safeHtml = $purifier->purify($email->textHtml);
@ -240,16 +238,17 @@ function niceDate($date) {
} else { } else {
echo $safeText; echo $safeText;
} }
} } ?>
?>
</div> </div>
</div> </div>
</div> </div>
<?php } ?> <?php
} ?>
<?php <?php
if (empty($emails)) { ?> if (empty($emails)) {
?>
<div id="empty-mailbox"> <div id="empty-mailbox">
<p>The mailbox is empty. Checking for new emails automatically. </p> <p>The mailbox is empty. Checking for new emails automatically. </p>
<div class="spinner"> <div class="spinner">
@ -260,7 +259,8 @@ function niceDate($date) {
<div class="rect5"></div> <div class="rect5"></div>
</div> </div>
</div> </div>
<?php } ?> <?php
} ?>
</div> </div>
</div> </div>
</main> </main>

View file

@ -62,7 +62,6 @@ class ImapClient {
} else { } else {
return null; return null;
} }
} }
/** /**
@ -71,8 +70,7 @@ class ImapClient {
* @param $user User * @param $user User
* @return array of emails * @return array of emails
*/ */
private private function _load_emails(array $mail_ids, User $user) {
function _load_emails(array $mail_ids, User $user) {
$emails = array(); $emails = array();
foreach ($mail_ids as $id) { foreach ($mail_ids as $id) {
$mail = $this->mailbox->getMail($id); $mail = $this->mailbox->getMail($id);
@ -87,8 +85,7 @@ class ImapClient {
/** /**
* deletes messages older than X days. * deletes messages older than X days.
*/ */
public public function delete_old_messages(string $delete_messages_older_than) {
function delete_old_messages(string $delete_messages_older_than) {
$ids = $this->mailbox->searchMailbox('BEFORE ' . date('d-M-Y', strtotime($delete_messages_older_than))); $ids = $this->mailbox->searchMailbox('BEFORE ' . date('d-M-Y', strtotime($delete_messages_older_than)));
foreach ($ids as $id) { foreach ($ids as $id) {
$this->mailbox->deleteMail($id); $this->mailbox->deleteMail($id);

View file

@ -20,7 +20,7 @@ class User {
public function isInvalid(array $config_domains): bool { public function isInvalid(array $config_domains): bool {
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)) { } elseif (!in_array($this->domain, $config_domains)) {
return true; return true;
} else { } else {
return false; return false;
@ -77,5 +77,4 @@ class User {
$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
} }
} }