From 092123175dfc78fc89e631af88a5e3c5f43c2baa Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 8 Nov 2023 22:29:13 +0100 Subject: [PATCH] added rss to new api and new config option --- example.config.ini | 4 + web/inc/OpenTrashmailBackend.class.php | 119 +++++++++++++++++++++++++ web/inc/core.php | 10 +++ web/index.html | 4 +- web/index.php | 106 ++-------------------- web/partials/email-table.html.php | 4 +- web/partials/email.html.php | 6 +- web/partials/rss.xml.php | 45 ++++++++++ 8 files changed, 194 insertions(+), 104 deletions(-) create mode 100644 web/inc/OpenTrashmailBackend.class.php create mode 100644 web/partials/rss.xml.php diff --git a/example.config.ini b/example.config.ini index 86f2dec..ee7da6f 100644 --- a/example.config.ini +++ b/example.config.ini @@ -5,6 +5,10 @@ ; pro tip: Use a wildcard domain like *.yourdomain.com to auto-generate the subdomains (you'll need to add a wildcard dns record too) DOMAINS=yourdomain,sub.yourdomain,*.mydom.com +; This variable needs to be set in order for RSS to work +; The URL of your webserver hosting the GUI. No trailing slash +URL="http://localhost:8080" + ; Enable to show a list of all existing accounts with mail ;SHOW_ACCOUNT_LIST=true diff --git a/web/inc/OpenTrashmailBackend.class.php b/web/inc/OpenTrashmailBackend.class.php new file mode 100644 index 0000000..8769f3c --- /dev/null +++ b/web/inc/OpenTrashmailBackend.class.php @@ -0,0 +1,119 @@ +url = $url; + $this->settings = loadSettings(); + } + public function run(){ + + // api calls + if($this->url[0]=='api') + { + switch($this->url[1]){ + case 'address': + return $this->listAccount($_REQUEST['email']?:$this->url[2]); + case 'read': + return $this->readMail($_REQUEST['email'],$_REQUEST['id']); + case 'attachment': + return $this->getAttachment($this->url[2],$this->url[3],$this->url[4]); + case 'delete': + return $this->deleteMail($_REQUEST['email'],$_REQUEST['id']); + default: + return false; + } + } + + // rss feed + else if($this->url[0]=='rss') + { + header("Content-Type: application/rss+xml; charset=UTF8"); + $email = $this->url[1]; + if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) { + http_response_code(404); + exit('Error: Email not found'); + } + return $this->renderPartial('rss.xml',[ + 'email'=>$email, + 'emaildata'=>getEmailsOfEmail($email), + 'url'=>$this->settings['URL'], + ]); + } + + else return false; + } + + function getAttachment($email,$id,$attachment) + { + 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'); + else if(!attachmentExists($email,$id,$attachment)) + return $this->error('Attachment not found'); + $dir = getDirForEmail($email); + $file = $dir.DS.'attachments'.DS.$id.'-'.$attachment; + $mime = mime_content_type($file); + header('Content-Type: '.$mime); + header('Content-Length: ' . filesize($file)); + readfile($file); + exit; + } + + function readMail($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'); + $email = 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',[ + 'email'=>$email, + 'mailid'=>$id, + ]); + + } + + public function listAccount($email) + { + if(!filter_var($email, FILTER_VALIDATE_EMAIL)) + return $this->error('Invalid email address'); + $emails = getEmailsOfEmail($email); + var_dump($emails); + return $this->renderPartial('email-table.html',[ + 'email'=>$email, + 'emails'=>$emails, + 'dateformat'=>$this->settings['DATEFORMAT'] + ]); + } + + public function error($text) + { + return '

'.$text.'

'; + } + + public function renderPartial($partialname,$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'); + $rendered = ob_get_contents(); + ob_end_clean(); + + return $rendered; + } + +} \ No newline at end of file diff --git a/web/inc/core.php b/web/inc/core.php index df4893e..ed787f2 100644 --- a/web/inc/core.php +++ b/web/inc/core.php @@ -144,4 +144,14 @@ function loadSettings() function escape($str) { return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); +} + +function array2ul($array) +{ + $out = ""; + return $out; } \ No newline at end of file diff --git a/web/index.html b/web/index.html index 3e778ce..d0042a4 100644 --- a/web/index.html +++ b/web/index.html @@ -6,14 +6,14 @@ - Hello, world! + Open Trashmail