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 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(
"/
((?<![\"']) # don't look inside quotes
(\b
( # protocol or www.
@ -49,9 +47,10 @@ class AutoLinkExtension
$href = 'http://' . $href;
}
return '<a href="' . $href . '" rel="noreferrer">' . $url . '</a>';
}, $string);
},
$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.
$config['prefer_plaintext'] = true;

View file

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

View file

@ -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 {
}
}
}

View file

@ -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); ?>
<a class="list-group-item list-group-item-action email-list-item" data-toggle="collapse"
href="#mail-box-<?php echo $email->id ?>"
@ -217,39 +216,39 @@ function niceDate($date) {
Delete
</a>
</div>
<?php
$safeHtml = $purifier->purify($email->textHtml);
<?php
$safeHtml = $purifier->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;
}
} ?>
</div>
</div>
</div>
<?php } ?>
<?php
} ?>
<?php
if (empty($emails)) { ?>
if (empty($emails)) {
?>
<div id="empty-mailbox">
<p>The mailbox is empty. Checking for new emails automatically. </p>
<div class="spinner">
@ -260,7 +259,8 @@ function niceDate($date) {
<div class="rect5"></div>
</div>
</div>
<?php } ?>
<?php
} ?>
</div>
</div>
</main>

View file

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

View file

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