Allow emails to be sorted

This commit is contained in:
cytopia 2016-11-05 02:57:10 +01:00
parent 9fe5aa2769
commit 2a05d1a0a8
No known key found for this signature in database
GPG Key ID: 6D56EDB8695128A2
3 changed files with 290 additions and 25 deletions

View File

@ -1,39 +1,90 @@
<?php
require '../config.php';
//
// $_POST submit for sending a test email
//
if (isset($_GET['email']) && isset($_GET['subject']) && isset($_GET['message'])) {
$mail = $_GET['email'];
$subj = $_GET['subject'];
$mess = $_GET['message'];
mail($mail, $subj, $mess);
header('Location: /mail.php');
exit();
}
//
// Includes
//
require '../config.php';
require '../include/vendor/Mail/Mbox.php';
require '../include/vendor/Mail/mimeDecode.php';
require '../include/lib/Mail.php';
require '../include/lib/Sort.php';
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$params['crlf'] = "\r\n";
$mbox = new Mail_Mbox('/var/mail/mailtrap');
//
// Setup Sort/Order
//
// Sort/Order settings
$defaultSort = array('sort' => 'date', 'order' => 'DESC');
$allowedSorts = array('date', 'subject', 'x-original-to');
$allowedOrders = array('ASC', 'DESC');
$GET_sortKeys = array('sort' => 'sort', 'order' => 'order');
// Get sort/order
$MySort = new \devilbox\Sort($defaultSort, $allowedSorts, $allowedOrders, $GET_sortKeys);
$sort = $MySort->getSort();
$order = $MySort->getOrder();
// Evaluate Sorters/Orderers
$orderDate = '<a href="/mail.php?sort=date&order=ASC#received"><i class="fa fa-sort" aria-hidden="true"></i></a>';
$orderTo = '<a href="/mail.php?sort=x-original-to&order=ASC#received"><i class="fa fa-sort" aria-hidden="true"></i></a>';
$orderSubj = '<a href="/mail.php?sort=subject&order=ASC#received"><i class="fa fa-sort" aria-hidden="true"></i></a>';
if ($sort == 'date') {
if ($order == 'ASC') {
$orderDate = '<a href="/mail.php?sort=date&order=DESC#received"><i class="fa fa-sort" aria-hidden="true"></i></a> <i class="fa fa-sort-numeric-asc" aria-hidden="true"></i>';
} else {
$orderDate = '<a href="/mail.php?sort=date&order=ASC#received"><i class="fa fa-sort" aria-hidden="true"></i></a> <i class="fa fa-sort-numeric-desc" aria-hidden="true"></i> ';
}
} else if ($sort == 'subject') {
if ($order == 'ASC') {
$orderSubj = '<a href="/mail.php?sort=subject&order=DESC#received"><i class="fa fa-sort" aria-hidden="true"></i></a> <i class="fa fa-sort-alpha-asc" aria-hidden="true"></i>';
} else {
$orderSubj = '<a href="/mail.php?sort=subject&order=ASC#received"><i class="fa fa-sort" aria-hidden="true"></i></a> <i class="fa fa-sort-alpha-desc" aria-hidden="true"></i>';
}
} else if ($sort == 'x-original-to') {
if ($order == 'ASC') {
$orderTo = '<a href="/mail.php?sort=x-original-to&order=DESC#received"><i class="fa fa-sort" aria-hidden="true"></i></a> <i class="fa fa-sort-alpha-asc" aria-hidden="true"></i>';
} else {
$orderTo = '<a href="/mail.php?sort=x-original-to&order=ASC#received"><i class="fa fa-sort" aria-hidden="true"></i></a> <i class="fa fa-sort-alpha-desc" aria-hidden="true"></i>';
}
}
//
// Mbox Reader
//
$MyMbox = new \devilbox\Mail('/var/mail/mailtrap');
// If default sort is on, use NULL, so we do not have to sort the mails after retrieval,
// because they are being read in the default sort/order anyway
$sortOrderArr = $MySort->isDefault($sort, $order) ? null : array($sort => $order);
$messages = $MyMbox->get($sortOrderArr);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php require '../include/head.php'; ?>
<?php $FONT_AWESOME = TRUE; require '../include/head.php'; ?>
</head>
<body>
<?php require '../include/navigation.php'; ?>
<div class="container">
<h1>Mail</h1>
<br/>
<br/>
@ -76,34 +127,32 @@ $mbox = new Mail_Mbox('/var/mail/mailtrap');
<div class="row">
<div class="col-md-12">
<h3>Received Emails</h3>
<h3 id="received">Received Emails</h3>
<br/>
</div>
</div>
<div class="row">
<div class="col-md-12">
<?php $mbox->open(); ?>
<table class="table table-striped table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th style="word-break: normal;">Date</th>
<th>To</th>
<th>Subject</th>
<th>Date <?php echo $orderDate;?></th>
<th>To <?php echo $orderTo;?></th>
<th>Subject <?php echo $orderSubj;?></th>
</tr>
</thead>
<tbody>
<?php for ($n = ($mbox->size()-1); $n >= 0; $n--): ?>
<?php foreach ($messages as $data): ?>
<?php
$message = $mbox->get($n);
$decode = new Mail_mimeDecode($message, "\r\n");
$structure = $decode->decode($params);
$message = $data['raw'];
$structure = $data['decoded'];
?>
<tr id="<?php echo $n;?>" class="subject">
<td><?php echo $n;?></td>
<tr id="<?php echo $data['num'];?>" class="subject">
<td><?php echo $data['num'];?></td>
<td>
<?php echo date('H:i', strtotime($structure->headers['date']));?><br/>
<small><?php echo date('Y-m-d', strtotime($structure->headers['date']));?></small>
@ -112,19 +161,22 @@ $mbox = new Mail_Mbox('/var/mail/mailtrap');
<td><?php echo $structure->headers['subject'];?></td>
</tr>
<tr></tr>
<tr id="mail-<?php echo $n;?>" style="display:none">
<tr id="mail-<?php echo $data['num'];?>" style="display:none">
<td></td>
<td colspan="3">
<pre><?php echo $message;?></pre>
</td>
</tr>
<?php endfor; ?>
<?php endforeach; ?>
</tbody>
</table>
<?php $mbox->close(); ?>
</div>
</div>
</div><!-- /.container -->
<?php require '../include/footer.php'; ?>

View File

@ -0,0 +1,113 @@
<?php
namespace devilbox;
/**
* @requires Pear::Mail_Mbox (http://pear.php.net/package/Mail_Mbox)
* @requires Pear::Mail_mimeDecode (http://pear.php.net/package/Mail_mime)
*/
class Mail
{
/**
* Mbox object.
* @var object
*/
private $_Mbox;
/**
* Default options for Mime decoding.
* @var array
*/
private $_defaultMimeParams = array(
'include_bodies' => true,
'decode_bodies' => true,
'decode_headers' => true,
'crlf' => "\r\n"
);
/**
* Contstructor
*
* @param string $mboxPath Path to mbox file.
*/
function __construct($mboxPath)
{
$this->_Mbox = new \Mail_Mbox($mboxPath);
$this->_Mbox->open();
}
/**
* Destructor
*/
function __destruct()
{
$this->_Mbox->close();
}
/**
* Set/Overwrite Mime dedocing options.
*
* @param mixed[] $mimeParams Options for mime decoding.
*/
public function setMimeParams($mimeParams)
{
$this->_defaultMimeParams = array_merge($this->_defaultMimeParams, $mimeParams);
}
/**
* Retrieve emails.
*
* @param mixed[] $sort array($sort => $order) Array
* @return mixed[] Array of emails
*/
public function get($sort = null)
{
// Stores all messages
$messages = array();
// Total number of emails in mbox file
$total = $this->_Mbox->size() - 1;
// Get messages in reverse order (last entry first)
for ($n = $total; $n >= 0; --$n) {
$message = $this->_Mbox->get($n);
$Decoder = new \Mail_mimeDecode($message, "\r\n");
$messages[] = array(
'num' => $n + 1,
'raw' => $message,
'decoded' => $Decoder->decode($this->_defaultMimeParams)
);
}
// Optionally sort messages
if (is_array($sort) && (array_values($sort)[0] == 'ASC' || array_values($sort)[0] == 'DESC')) {
$key = array_keys($sort)[0];
$order = array_values($sort)[0];
$sorter = function ($a, $b) use ($key, $order) {
$val1 = $a['decoded']->headers[$key];
$val2 = $b['decoded']->headers[$key];
// Convert date strings to timestamps for comparison
if (strtotime($val1) !== false && strtotime($val2) !== false) {
$val1 = strtotime($val1);
$val2 = strtotime($val2);
}
if ($order === 'ASC') {
return (strcmp($val1, $val2) > 0);
} else {
return (strcmp($val1, $val2) <= 0);
}
};
usort($messages, $sorter);
}
return $messages;
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace devilbox;
class Sort
{
private $_default_sort;
private $_default_order;
private $_allowedSorts;
private $_allowedOrders;
private $_GET_sort;
private $_GET_order;
/**
* Constructor
*
* @param mixed[] $defaults Array of default sort/order values
* @param mixed[] $allowedSorts Array of allowed sorts
* @param mixed[] $allowedOrders Array of allowed orders
* @param mixed[] $GET_sortKeys Array of sort/order $_GET key names
*/
function __construct($defaults, $allowedSorts, $allowedOrders, $GET_sortKeys)
{
// Default sort/order
$this->_default_sort = $defaults['sort'];
$this->_default_order = $defaults['order'];
// Allowed sort/order array
$this->_allowedSorts = $allowedSorts;
$this->_allowedOrders = $allowedOrders;
// $_GET keys for sort and order
$this->_GET_sort = $GET_sortKeys['sort'];
$this->_GET_order = $GET_sortKeys['order'];
}
/**
* Get active or default sort value.
*
* @return string
*/
public function getSort()
{
$sort = null;
// Get current sort value from $_GET
if (isset($_GET[$this->_GET_sort])) {
$sort = $_GET[$this->_GET_sort];
}
// If sort is not allowed, reset to default
if (!in_array($sort, $this->_allowedSorts)) {
$sort = $this->_default_sort;
}
// All good, return sort
return $sort;
}
/**
* Get active or default order value.
*
* @return string
*/
public function getOrder()
{
$order = null;
// Get current order value from $_GET
if (isset($_GET[$this->_GET_order])) {
$order = $_GET[$this->_GET_order];
}
// If order is not allowed, reset to default
if (!in_array($order, $this->_allowedOrders)) {
$order = $this->_default_order;
}
// All good, return order
return $order;
}
/**
* Are we using default sort/order?
*
* @param string $sort Sort value
* @param string $order Order value
* @return boolean
*/
public function isDefault($sort, $order)
{
return ($this->_default_sort == $sort && $this->_default_order == $order);
}
}