-
Notifications
You must be signed in to change notification settings - Fork 47
/
OpenTrashmailBackend.class.php
224 lines (205 loc) · 7.86 KB
/
OpenTrashmailBackend.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?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':
return $this->readMail($_REQUEST['email']?:$this->url[2],$_REQUEST['id']?:$this->url[3]);
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);
case 'raw':
return $this->getRawMail($this->url[2],$this->url[3]);
case 'attachment':
return $this->getAttachment($this->url[2],$this->url[3]);
case 'delete':
return $this->deleteMail($_REQUEST['email']?:$this->url[2],$_REQUEST['id']?:$this->url[3]);
case 'random':
$addr = generateRandomEmail();
return $this->listAccount($addr);
case 'deleteaccount':
return $this->deleteAccount($_REQUEST['email']?:$this->url[2]);
case 'logs':
if($this->settings['SHOW_LOGS'])
return $this->renderTemplate('logs.html',[
'lines' => (is_numeric($this->url[2])&&$this->url[2]>0)?$this->url[2]:100,
'mailserverlogfile'=>ROOT.DS.'../logs'.DS.'mailserver.log',
'webservererrorlogfile'=>ROOT.DS.'../logs'.DS.'web.error.log',
'webserveraccesslogfile'=>ROOT.DS.'../logs'.DS.'web.access.log',
'configfile' => ROOT.DS.'../config.ini',
]);
else return '403 Forbidden';
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->renderTemplate('rss.xml',[
'email'=>$email,
'emaildata'=>getEmailsOfEmail($email),
'url'=>$this->settings['URL'],
]);
}
//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']));
}
else if(!is_numeric($id))
{
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));
}
else return false;
}
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']
]);
}
function deleteMail($email,$id)
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
return $this->error('Invalid email address');
else if(!is_numeric($id))
return $this->error('Invalid id');
else if(!emailIDExists($email,$id))
return $this->error('Email not found');
deleteEmail($email,$id);
return '';
}
function getRawMail($email,$id,$htmlbody=false)
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
return $this->error('Invalid email address');
else if(!is_numeric($id))
return $this->error('Invalid id');
else if(!emailIDExists($email,$id))
return $this->error('Email not found');
$emaildata = getEmail($email,$id);
if($htmlbody)
exit($emaildata['parsed']['htmlbody']);
header('Content-Type: text/plain');
echo $emaildata['raw'];
exit;
}
function getAttachment($email,$attachment)
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
return $this->error('Invalid email address');
else if(!attachmentExists($email,$attachment))
return $this->error('Attachment not found');
$dir = getDirForEmail($email);
$file = $dir.DS.'attachments'.DS.$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(!is_numeric($id))
return $this->error('Invalid id');
else if(!emailIDExists($email,$id))
return $this->error('Email not found');
$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($emaildata);
return $this->renderTemplate('email.html',[
'emaildata'=>$emaildata,
'email'=>$email,
'mailid'=>$id,
'dateformat'=>$this->settings['DATEFORMAT']
]);
}
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->renderTemplate('email-table.html',[
'isadmin'=>($this->settings['ADMIN']==$email),
'email'=>$email,
'emails'=>$emails,
'dateformat'=>$this->settings['DATEFORMAT']
]);
}
public function error($text)
{
return '<h1>'.$text.'</h1>';
}
public function renderTemplate($templatename,$variables=[])
{
ob_start();
if(is_array($variables))
extract($variables);
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();
return $rendered;
}
}