2023-11-08 21:29:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class OpenTrashmailBackend{
|
|
|
|
private $url;
|
|
|
|
private $settings;
|
|
|
|
|
|
|
|
public function __construct($url){
|
|
|
|
$this->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':
|
2023-11-09 22:21:17 +00:00
|
|
|
return $this->readMail($_REQUEST['email']?:$this->url[2],$_REQUEST['id']?:$this->url[3]);
|
2023-11-11 13:36:34 +00:00
|
|
|
case 'listaccounts':
|
|
|
|
if($this->settings['SHOW_ACCOUNT_LIST'])
|
|
|
|
return $this->listAccounts();
|
|
|
|
else return '403 Forbidden';
|
|
|
|
case 'raw-html':
|
|
|
|
return $this->getRawMail($this->url[2],$this->url[3],true);
|
2023-11-09 08:45:31 +00:00
|
|
|
case 'raw':
|
|
|
|
return $this->getRawMail($this->url[2],$this->url[3]);
|
2023-11-08 21:29:13 +00:00
|
|
|
case 'attachment':
|
2023-11-09 08:45:31 +00:00
|
|
|
return $this->getAttachment($this->url[2],$this->url[3]);
|
2023-11-08 21:29:13 +00:00
|
|
|
case 'delete':
|
2023-11-10 20:45:02 +00:00
|
|
|
return $this->deleteMail($_REQUEST['email']?:$this->url[2],$_REQUEST['id']?:$this->url[3]);
|
2023-11-09 22:21:17 +00:00
|
|
|
case 'random':
|
|
|
|
$addr = generateRandomEmail();
|
|
|
|
return $this->listAccount($addr);
|
2023-11-11 13:36:34 +00:00
|
|
|
case 'deleteaccount':
|
|
|
|
return $this->deleteAccount($_REQUEST['email']?:$this->url[2]);
|
2023-11-08 21:29:13 +00:00
|
|
|
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');
|
|
|
|
}
|
2023-11-09 08:45:31 +00:00
|
|
|
return $this->renderTemplate('rss.xml',[
|
2023-11-08 21:29:13 +00:00
|
|
|
'email'=>$email,
|
|
|
|
'emaildata'=>getEmailsOfEmail($email),
|
|
|
|
'url'=>$this->settings['URL'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:41:14 +00:00
|
|
|
//json api
|
|
|
|
else if($this->url[0]=='json')
|
|
|
|
{
|
|
|
|
header("Content-Type: application/json; charset=UTF8");
|
|
|
|
if($this->url[1]=='listaccounts')
|
|
|
|
{
|
|
|
|
if($this->settings['SHOW_ACCOUNT_LIST'])
|
|
|
|
return json_encode(listEmailAdresses());
|
|
|
|
else exit(json_encode(['error'=>'403 Forbidden']));
|
|
|
|
}
|
|
|
|
$email = $this->url[1];
|
|
|
|
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
http_response_code(404);
|
|
|
|
exit(json_encode(['error'=>'Email not found']));
|
|
|
|
}
|
|
|
|
$id = $this->url[2];
|
|
|
|
if($id) //user wants a specific email ID
|
|
|
|
{
|
|
|
|
if(!emailIDExists($email,$id))
|
|
|
|
{
|
|
|
|
http_response_code(404);
|
|
|
|
exit(json_encode(['error'=>'Email ID not found']));
|
|
|
|
}
|
2023-11-11 22:16:45 +00:00
|
|
|
else if(!is_numeric($id))
|
2023-11-11 15:41:14 +00:00
|
|
|
{
|
|
|
|
http_response_code(400);
|
|
|
|
exit(json_encode(['error'=>'Invalid ID']));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return json_encode(getEmail($email,$id));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return json_encode(getEmailsOfEmail($email,true,true));
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:29:13 +00:00
|
|
|
else return false;
|
|
|
|
}
|
2023-11-11 13:36:34 +00:00
|
|
|
|
|
|
|
function deleteAccount($email)
|
|
|
|
{
|
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
|
|
return $this->error('Invalid email address');
|
|
|
|
$path = getDirForEmail($email);
|
|
|
|
if(is_dir($path))
|
|
|
|
delTree($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function listAccounts()
|
|
|
|
{
|
|
|
|
$accounts = listEmailAdresses();
|
|
|
|
return $this->renderTemplate('account-list.html',[
|
|
|
|
'emails'=>$accounts,
|
|
|
|
'dateformat'=>$this->settings['DATEFORMAT']
|
|
|
|
]);
|
|
|
|
}
|
2023-11-08 21:29:13 +00:00
|
|
|
|
2023-11-09 22:21:17 +00:00
|
|
|
function deleteMail($email,$id)
|
|
|
|
{
|
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
|
|
return $this->error('Invalid email address');
|
2023-11-11 22:16:45 +00:00
|
|
|
else if(!is_numeric($id))
|
2023-11-09 22:21:17 +00:00
|
|
|
return $this->error('Invalid id');
|
|
|
|
else if(!emailIDExists($email,$id))
|
|
|
|
return $this->error('Email not found');
|
|
|
|
deleteEmail($email,$id);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-11-11 13:36:34 +00:00
|
|
|
function getRawMail($email,$id,$htmlbody=false)
|
2023-11-08 21:29:13 +00:00
|
|
|
{
|
2023-11-09 08:45:31 +00:00
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
|
|
return $this->error('Invalid email address');
|
2023-11-11 22:16:45 +00:00
|
|
|
else if(!is_numeric($id))
|
2023-11-09 08:45:31 +00:00
|
|
|
return $this->error('Invalid id');
|
|
|
|
else if(!emailIDExists($email,$id))
|
|
|
|
return $this->error('Email not found');
|
|
|
|
$emaildata = getEmail($email,$id);
|
2023-11-11 13:36:34 +00:00
|
|
|
if($htmlbody)
|
|
|
|
exit($emaildata['parsed']['htmlbody']);
|
2023-11-09 08:45:31 +00:00
|
|
|
header('Content-Type: text/plain');
|
|
|
|
echo $emaildata['raw'];
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAttachment($email,$attachment)
|
|
|
|
{
|
|
|
|
$id = substr($attachment,0,13);
|
|
|
|
$attachment = substr($attachment,14);
|
2023-11-08 21:29:13 +00:00
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
|
|
return $this->error('Invalid email address');
|
2023-11-11 22:16:45 +00:00
|
|
|
else if(!is_numeric($id))
|
2023-11-08 21:29:13 +00:00
|
|
|
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');
|
2023-11-11 22:16:45 +00:00
|
|
|
else if(!is_numeric($id))
|
2023-11-08 21:29:13 +00:00
|
|
|
return $this->error('Invalid id');
|
|
|
|
else if(!emailIDExists($email,$id))
|
|
|
|
return $this->error('Email not found');
|
2023-11-09 08:45:31 +00:00
|
|
|
$emaildata = getEmail($email,$id);
|
2023-11-08 21:29:13 +00:00
|
|
|
//$email['raw'] = file_get_contents(getDirForEmail($email['email']).DS.$email['id'].'.json');
|
|
|
|
//$email['parsed'] = json_decode($email['raw'],true);
|
|
|
|
|
2023-11-09 22:21:17 +00:00
|
|
|
//var_dump($emaildata);
|
2023-11-09 08:45:31 +00:00
|
|
|
return $this->renderTemplate('email.html',[
|
|
|
|
'emaildata'=>$emaildata,
|
2023-11-08 21:29:13 +00:00
|
|
|
'email'=>$email,
|
|
|
|
'mailid'=>$id,
|
2023-11-09 22:21:17 +00:00
|
|
|
'dateformat'=>$this->settings['DATEFORMAT']
|
2023-11-08 21:29:13 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function listAccount($email)
|
|
|
|
{
|
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
|
|
|
|
return $this->error('Invalid email address');
|
|
|
|
$emails = getEmailsOfEmail($email);
|
2023-11-09 22:21:17 +00:00
|
|
|
//var_dump($emails);
|
2023-11-09 08:45:31 +00:00
|
|
|
return $this->renderTemplate('email-table.html',[
|
2023-11-12 08:53:34 +00:00
|
|
|
'isadmin'=>($this->settings['ADMIN']==$email),
|
2023-11-08 21:29:13 +00:00
|
|
|
'email'=>$email,
|
|
|
|
'emails'=>$emails,
|
|
|
|
'dateformat'=>$this->settings['DATEFORMAT']
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function error($text)
|
|
|
|
{
|
|
|
|
return '<h1>'.$text.'</h1>';
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:45:31 +00:00
|
|
|
public function renderTemplate($templatename,$variables=[])
|
2023-11-08 21:29:13 +00:00
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
if(is_array($variables))
|
|
|
|
extract($variables);
|
2023-11-09 08:45:31 +00:00
|
|
|
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);
|
2023-11-08 21:29:13 +00:00
|
|
|
$rendered = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
return $rendered;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|