From 752cd0e35faebbb22767cf597e577599d3d7cf75 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 9 Nov 2023 09:45:31 +0100 Subject: [PATCH] few reworks --- web/inc/OpenTrashmailBackend.class.php | 41 ++++++++++++++----- web/index.php | 6 ++- web/partials/email.html.php | 29 ------------- .../email-table.html.php | 2 +- web/templates/email.html.php | 36 ++++++++++++++++ web/{ => templates}/index.html | 0 web/{partials => templates}/rss.xml.php | 7 ++-- 7 files changed, 75 insertions(+), 46 deletions(-) delete mode 100644 web/partials/email.html.php rename web/{partials => templates}/email-table.html.php (94%) create mode 100644 web/templates/email.html.php rename web/{ => templates}/index.html (100%) rename web/{partials => templates}/rss.xml.php (80%) diff --git a/web/inc/OpenTrashmailBackend.class.php b/web/inc/OpenTrashmailBackend.class.php index 8769f3c..2bd817b 100644 --- a/web/inc/OpenTrashmailBackend.class.php +++ b/web/inc/OpenTrashmailBackend.class.php @@ -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 '

'.$text.'

'; } - 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(); diff --git a/web/index.php b/web/index.php index ef71b32..0090aea 100644 --- a/web/index.php +++ b/web/index.php @@ -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(); diff --git a/web/partials/email.html.php b/web/partials/email.html.php deleted file mode 100644 index fd0a7ad..0000000 --- a/web/partials/email.html.php +++ /dev/null @@ -1,29 +0,0 @@ -
-
Subject:
-
- Reciepients: -
- - - -
-
- -
- Attachments -
- - No attachments - -
    - -
  • - -
  • - -
-
-
-
- -
\ No newline at end of file diff --git a/web/partials/email-table.html.php b/web/templates/email-table.html.php similarity index 94% rename from web/partials/email-table.html.php rename to web/templates/email-table.html.php index 5d95b0a..b57a4ce 100644 --- a/web/partials/email-table.html.php +++ b/web/templates/email-table.html.php @@ -38,4 +38,4 @@ -
\ No newline at end of file + \ No newline at end of file diff --git a/web/templates/email.html.php b/web/templates/email.html.php new file mode 100644 index 0000000..877eef4 --- /dev/null +++ b/web/templates/email.html.php @@ -0,0 +1,36 @@ +
+
Subject:
+
+ Reciepients: +
+ + + +
+
+ +
+ Attachments +
+ + No attachments + +
    + +
  • + +
  • + +
+
+
+
+ + + + + \ No newline at end of file diff --git a/web/index.html b/web/templates/index.html similarity index 100% rename from web/index.html rename to web/templates/index.html diff --git a/web/partials/rss.xml.php b/web/templates/rss.xml.php similarity index 80% rename from web/partials/rss.xml.php rename to web/templates/rss.xml.php index 72bfe96..68d7357 100644 --- a/web/partials/rss.xml.php +++ b/web/templates/rss.xml.php @@ -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[] = ''; - $att_text[] = "$fn"; + $att_url = $url . '/api/attachment/' . $email . '/' . $filename; + $att_text[] = "$fn"; } ?> @@ -34,7 +33,7 @@ Email from:
Email to:
0) ? 'Attachments:
' . array2ul($att_text) . '
' : '') ?> - View raw email
+ View raw email

---------

]]>