few reworks

This commit is contained in:
Chris 2023-11-09 09:45:31 +01:00
parent 092123175d
commit 752cd0e35f
7 changed files with 75 additions and 46 deletions

View file

@ -18,8 +18,10 @@ class OpenTrashmailBackend{
return $this->listAccount($_REQUEST['email']?:$this->url[2]);
case 'read':
return $this->readMail($_REQUEST['email'],$_REQUEST['id']);
case 'raw':
return $this->getRawMail($this->url[2],$this->url[3]);
case 'attachment':
return $this->getAttachment($this->url[2],$this->url[3],$this->url[4]);
return $this->getAttachment($this->url[2],$this->url[3]);
case 'delete':
return $this->deleteMail($_REQUEST['email'],$_REQUEST['id']);
default:
@ -36,7 +38,7 @@ class OpenTrashmailBackend{
http_response_code(404);
exit('Error: Email not found');
}
return $this->renderPartial('rss.xml',[
return $this->renderTemplate('rss.xml',[
'email'=>$email,
'emaildata'=>getEmailsOfEmail($email),
'url'=>$this->settings['URL'],
@ -46,8 +48,24 @@ class OpenTrashmailBackend{
else return false;
}
function getAttachment($email,$id,$attachment)
function getRawMail($email,$id)
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
return $this->error('Invalid email address');
else if(!ctype_digit($id))
return $this->error('Invalid id');
else if(!emailIDExists($email,$id))
return $this->error('Email not found');
$emaildata = getEmail($email,$id);
header('Content-Type: text/plain');
echo $emaildata['raw'];
exit;
}
function getAttachment($email,$attachment)
{
$id = substr($attachment,0,13);
$attachment = substr($attachment,14);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
return $this->error('Invalid email address');
else if(!ctype_digit($id))
@ -73,12 +91,13 @@ class OpenTrashmailBackend{
return $this->error('Invalid id');
else if(!emailIDExists($email,$id))
return $this->error('Email not found');
$email = getEmail($email,$id);
$emaildata = getEmail($email,$id);
//$email['raw'] = file_get_contents(getDirForEmail($email['email']).DS.$email['id'].'.json');
//$email['parsed'] = json_decode($email['raw'],true);
var_dump($email);
return $this->renderPartial('email.html',[
var_dump($emaildata);
return $this->renderTemplate('email.html',[
'emaildata'=>$emaildata,
'email'=>$email,
'mailid'=>$id,
]);
@ -91,7 +110,7 @@ class OpenTrashmailBackend{
return $this->error('Invalid email address');
$emails = getEmailsOfEmail($email);
var_dump($emails);
return $this->renderPartial('email-table.html',[
return $this->renderTemplate('email-table.html',[
'email'=>$email,
'emails'=>$emails,
'dateformat'=>$this->settings['DATEFORMAT']
@ -103,13 +122,15 @@ class OpenTrashmailBackend{
return '<h1>'.$text.'</h1>';
}
public function renderPartial($partialname,$variables=[])
public function renderTemplate($templatename,$variables=[])
{
ob_start();
if(is_array($variables))
extract($variables);
if(file_exists(ROOT.DS.'partials'.DS.$partialname.'.php'))
include(ROOT.DS.'partials'.DS.$partialname.'.php');
if(file_exists(ROOT.DS.'templates'.DS.$templatename.'.php'))
include(ROOT.DS.'templates'.DS.$templatename.'.php');
else if(file_exists(ROOT.DS.'templates'.DS.$templatename))
include(ROOT.DS.'templates'.DS.$templatename);
$rendered = ob_get_contents();
ob_end_clean();

View file

@ -7,14 +7,16 @@ include_once(ROOT.DS.'inc'.DS.'core.php');
$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),'/')));
$backend = new OpenTrashmailBackend($url);
if($_SERVER['HTTP_HX_REQUEST']!='true')
{
if(count($url)==0 || !file_exists(ROOT.DS.implode('/', $url)))
if($url[0]!='api' && $url[0]!='rss')
exit(file_get_contents(ROOT.DS.'index.html'));
exit($backend->renderTemplate('index.html'));
}
$backend = new OpenTrashmailBackend($url);
$answer = $backend->run();

View file

@ -1,29 +0,0 @@
<article>
<header>Subject: <?= escape($email['parsed']['subject']) ?></header>
<header>
Reciepients:
<div>
<?php foreach($email['rcpts'] as $to): ?>
<small class="badge"><?= escape($to) ?></small>
<?php endforeach; ?>
</div>
</header>
<?= nl2br(escape($email['parsed']['body'])) ?>
<footer>
Attachments
<div>
<?php if(count($email['parsed']['attachments'])==0): ?>
<small class="secondary">No attachments</small>
<?php endif; ?>
<ul>
<?php foreach($email['parsed']['attachments'] as $attachment): ?>
<li>
<a href="/api/attachment/<?= $mailid ?>/<?= $attachment ?>"><?= escape($attachment) ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
</footer>
</article>
<div hx-push-url="/eml/<?= $email ?>/<?= $mailid ?>" hx-trigger="load">

View file

@ -38,4 +38,4 @@
<?php endforeach; ?>
</table>
<div hx-push-url="/eml/<?= $email ?>" hx-trigger="load">
<script>history.pushState({email:"<?= $email ?>"}, "", "/eml/<?= $email ?>");</script>

View file

@ -0,0 +1,36 @@
<article>
<header>Subject: <?= escape($emaildata['parsed']['subject']) ?></header>
<header>
Reciepients:
<div>
<?php foreach ($emaildata['rcpts'] as $to) : ?>
<small class="badge"><?= escape($to) ?></small>
<?php endforeach; ?>
</div>
</header>
<?= nl2br(escape($emaildata['parsed']['body'])) ?>
<footer>
Attachments
<div>
<?php if (count($emaildata['parsed']['attachments']) == 0) : ?>
<small class="secondary">No attachments</small>
<?php endif; ?>
<ul>
<?php foreach ($emaildata['parsed']['attachments'] as $attachment) : ?>
<li>
<a target="_blank" href="/api/attachment/<?= $email ?>/<?= $attachment ?>"><?= escape($attachment) ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
</footer>
</article>
<article>
<header>Raw email</header>
<a href="/api/raw/<?= $email ?>/<?= $mailid ?>" target="_blank">Open in new Window</a>
<pre><button hx-get="/api/raw/<?= $email ?>/<?= $mailid ?>" hx-swap="outerHTML">Load Raw Email</button></pre>
</article>
<script>history.pushState({email:"<?= $email ?>"}, "", "/eml/<?= $email ?>/<?= $mailid ?>");</script>

View file

@ -20,9 +20,8 @@
$parts = explode('-', $filename);
$fid = $parts[0];
$fn = $parts[1];
$url = 'https://' . $url . '/api.php?a=attachment&email=' . $email . '&id=' . $fid . '&filename=' . $fn;
//$encl[] = '<enclosure url="'.rawurlencode($url).'" length="'.filesize($filepath).'" type="'.mime_content_type($filepath).'" />';
$att_text[] = "<a href='$url' target='_blank'>$fn</a>";
$att_url = $url . '/api/attachment/' . $email . '/' . $filename;
$att_text[] = "<a href='$att_url' target='_blank'>$fn</a>";
}
?>
<item>
@ -34,7 +33,7 @@
Email from: <?= escape($d['from']) ?><br/>
Email to: <?= escape(implode(';',$data['rcpts'])) ?><br/>
<?= ((count($att_text) > 0) ? 'Attachments:<br/>' . array2ul($att_text) . '<br/>' : '') ?>
<a href="<?= $url ?>api/eml/test@0xv.eu/1699459401553/raw">View raw email</a> <br/>
<a href="<?= $url ?>api/raw/test@0xv.eu/1699459401553">View raw email</a> <br/>
<br/>---------<br/><br/>
<?= ($data['parsed']['htmlbody'] ? $data['parsed']['htmlbody'] : nl2br(htmlentities($data['parsed']['body']))) ?>
]]>