Merge branch 'cytopia:master' into support-meilisearch

This commit is contained in:
Minh Chu 2022-12-16 11:59:28 +07:00 committed by GitHub
commit eb5ecc836c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 53 deletions

View File

@ -1,22 +0,0 @@
class HtmlEmail extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
let emailContent;
const templateId = this.dataset.templateId;
try {
emailContent = document.getElementById(templateId).innerHTML;
} catch (error) {
console.log(error);
return;
}
const container = document.createElement('div');
container.innerHTML = emailContent;
this.shadowRoot.appendChild(container);
}
}
customElements.define('html-email', HtmlEmail);

View File

@ -24,6 +24,28 @@ require $VEN_DIR . DIRECTORY_SEPARATOR . 'Mail' . DIRECTORY_SEPARATOR .'mimeDeco
require $LIB_DIR . DIRECTORY_SEPARATOR . 'Mail.php'; require $LIB_DIR . DIRECTORY_SEPARATOR . 'Mail.php';
require $LIB_DIR . DIRECTORY_SEPARATOR . 'Sort.php'; require $LIB_DIR . DIRECTORY_SEPARATOR . 'Sort.php';
if (isset($_GET['get-body']) && is_numeric($_GET['get-body'])) {
$messageNumber = $_GET['get-body'];
$MyMbox = new \devilbox\Mail('/var/mail/devilbox');
$message = $MyMbox->getMessage($messageNumber-1);
$structure = $message['decoded'];
$body = null;
if (isset($structure->body)) {
$body = $structure->body;
}
elseif(isset($structure->parts[1]->body)) {
$body = $structure->parts[1]->body;
}
elseif(isset($structure->parts[0]->body)) {
$body = $structure->parts[0]->body;
}
exit(json_encode(array(
'raw' => htmlentities($message['raw']),
'body' => $body,
)));
}
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) { if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
$message = $_GET['delete']; $message = $_GET['delete'];
@ -171,17 +193,6 @@ $messages = $MyMbox->get($sortOrderArr);
<?php <?php
$message = htmlentities($data['raw']); $message = htmlentities($data['raw']);
$structure = $data['decoded']; $structure = $data['decoded'];
$body = null;
if (isset($structure->body)) {
$body = $structure->body;
}
elseif(isset($structure->parts[1]->body)) {
$body = $structure->parts[1]->body;
}
elseif(isset($structure->parts[0]->body)) {
$body = $structure->parts[0]->body;
}
?> ?>
<tr id="<?php echo $data['num'];?>" class="subject"> <tr id="<?php echo $data['num'];?>" class="subject">
<td><?php echo $data['num'];?></td> <td><?php echo $data['num'];?></td>
@ -198,17 +209,13 @@ $messages = $MyMbox->get($sortOrderArr);
<tr id="mail-<?php echo $data['num'];?>" style="display:none"> <tr id="mail-<?php echo $data['num'];?>" style="display:none">
<td></td> <td></td>
<td colspan="5"> <td colspan="5">
<?php if ($body !== null): ?> <div class="email-body"></div>
<template id="mail-body-<?=$data['num']?>"><?=$body?></template> <div class="alert alert-warning" role="alert" style="display:none">
<html-email data-template-id="mail-body-<?=$data['num']?>"></html-email>
<?php else: ?>
<div class="alert alert-warning" role="alert">
No valid body found No valid body found
</div> </div>
<?php endif; ?>
<hr> <hr>
<p><a class="btn btn-primary" data-toggle="collapse" href="#email-<?php echo $data['num'];?>" aria-expanded="false" aria-controls="email-<?php echo $data['num'];?>">Raw source</a></p> <p><a class="btn btn-primary" data-toggle="collapse" href="#email-<?php echo $data['num'];?>" aria-expanded="false" aria-controls="email-<?php echo $data['num'];?>">Raw source</a></p>
<div class="collapse" id="email-<?php echo $data['num'];?>"><pre><?php echo $message;?></pre></div> <div class="collapse" id="email-<?php echo $data['num'];?>"><pre class="raw-email-body"></pre></div>
</td> </td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
@ -227,12 +234,34 @@ $messages = $MyMbox->get($sortOrderArr);
<script> <script>
$(function() { $(function() {
$('.subject').click(function() { $('.subject').click(function() {
var id = ($(this).attr('id')); const id = ($(this).attr('id'));
$('#mail-'+id).toggle(); const row = $('#mail-'+id);
row.toggle();
const bodyElement = row.find('.email-body')[0];
if(bodyElement.shadowRoot !== null){
// We've already fetched the message content.
return;
}
bodyElement.attachShadow({ mode: 'open' });
bodyElement.shadowRoot.innerHTML = 'Loading...';
$.get('?get-body=' + id, function(response){
response = JSON.parse(response);
row.find('.raw-email-body').html(response.raw);
const body = response.body;
if(body === null){
row.find('.alert').show();
}
else{
bodyElement.shadowRoot.innerHTML = body;
}
})
}) })
// Handler for .ready() called. // Handler for .ready() called.
}); });
</script> </script>
<script src="/assets/js/html-email.js"></script>
</body> </body>
</html> </html>

View File

@ -70,6 +70,22 @@ class Mail
} }
/**
* Returns a single message
*
* @param int $messageIndex The zero-based index of the message to return.
*/
public function getMessage($messageIndex){
$message = $this->_Mbox->get($messageIndex);
$Decoder = new \Mail_mimeDecode($message, "\r\n");
return array(
'num' => $messageIndex + 1,
'raw' => $message,
'decoded' => $Decoder->decode($this->_defaultMimeParams)
);
}
/** /**
* Retrieve emails. * Retrieve emails.
* *
@ -87,13 +103,7 @@ class Mail
// Get messages in reverse order (last entry first) // Get messages in reverse order (last entry first)
for ($n = $total; $n >= 0; --$n) { for ($n = $total; $n >= 0; --$n) {
$message = $this->_Mbox->get($n); $messages[] = $this->getMessage($n);
$Decoder = new \Mail_mimeDecode($message, "\r\n");
$messages[] = array(
'num' => $n + 1,
'raw' => $message,
'decoded' => $Decoder->decode($this->_defaultMimeParams)
);
} }
// Optionally sort messages // Optionally sort messages

View File

@ -71,4 +71,4 @@ run "curl -sS --fail -XPOST 'http://localhost:${HOST_PORT_HTTPD}/mail.php' -d 'e
# Validate # Validate
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_MAIL}' >/dev/null" "${RETRIES}" run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_MAIL}' >/dev/null" "${RETRIES}"
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_SUBJ}' >/dev/null" "${RETRIES}" run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_SUBJ}' >/dev/null" "${RETRIES}"
run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php' | tac | tac | grep '${MY_MESS}' >/dev/null" "${RETRIES}" run "curl -sS --fail 'http://localhost:${HOST_PORT_HTTPD}/mail.php?get-body=1' | tac | tac | grep '${MY_MESS}' >/dev/null" "${RETRIES}"