-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
332 lines (280 loc) · 7.87 KB
/
index.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
// Set Maildir
$maildir = '/var/www/Maildir/new/';
// Hide mailbox list?
$hide_mailbox_list = false;
/*--------------------------------------------------------------------------*/
require_once('MimeMailParser.class.php');
function get_all_mailboxes()
{
global $maildir;
$users = array();
$dates = array();
foreach ( scandir($maildir) as $filename )
{
$email = htmlentities(file_get_contents($maildir . $filename));
if ( preg_match('/Original-To: (.*)@(.*)/', $email, $addresses) )
{
preg_match('/Date: (.*)/', $email, $date);
$date_stamp = strtotime($date[1]);
if ( array_key_exists($addresses[1], $users) )
{
$users[$addresses[1]] += 1;
if ( $date_stamp > $dates[$addresses[1]] )
{
$dates[$addresses[1]] = $date_stamp;
}
}
else
{
$users[$addresses[1]] = 1;
$dates[$addresses[1]] = $date_stamp;
}
}
}
ksort($users);
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>' .
'<?xml-stylesheet type="text/xsl" href="web/mailias.xsl"?>' .
'<Postoffice/>');
foreach ( $users as $user => $count )
{
$ms = intval((time() - $dates[$user])/60);
$hs = intval((time() - $dates[$user])/3600);
$ds = intval((time() - $dates[$user])/86400);
if ( $ds > 1 ) $latest = sprintf("about %d days ago", $ds);
else if ( $ds == 1 ) $latest = sprintf("yesterday");
else if ( $hs > 1 ) $latest = sprintf("about %d hours ago", $hs);
else if ( $hs == 1 ) $latest = sprintf("about an hour ago");
else if ( $ms > 1 ) $latest = sprintf("about %d minutes ago", $ms);
else $latest = sprintf("just now");
$node = $xml->addChild('Mailbox');
$node->addChild('User', $user);
$node->addChild('Count', $count);
$node->addChild('Latest', $latest);
}
Header('Content-type: text/xml; charset=UTF-8');
print($xml->asXML());
}
function scan_mail_dir($dir)
{
$files = array();
foreach (scandir($dir) as $file)
{
$files[$file] = filemtime($dir . '/' . $file);
}
arsort($files);
$files = array_keys($files);
return ($files) ? $files : false;
}
function get_mailbox($user)
{
global $maildir;
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>' .
'<?xml-stylesheet type="text/xsl" href="web/mailias.xsl"?>' .
'<Mailbox/>');
$xml->addChild('User', $user);
foreach ( scan_mail_dir($maildir) as $filename )
{
$email = htmlentities(file_get_contents($maildir . $filename));
if ( preg_match("/Original-To: [email protected]/", $email) )
{
preg_match('/Date: (.*)/', $email, $date);
preg_match('/From: (.*)/', $email, $from);
preg_match('/Subject: (.*)/', $email, $subject);
$node = $xml->addChild('Email');
$node->addChild('From', $from[1]);
$node->addChild('Subject', $subject[1]);
$node->addChild('Date', $date[1]);
$node->addChild('Link', $filename);
}
}
Header('Content-type: text/xml; charset=UTF-8');
print($xml->asXML());
}
function get_mailbox_rss($user)
{
global $maildir;
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/';
$uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$dc = 'http://purl.org/dc/elements/1.1/';
$atom = 'http://www.w3.org/2005/Atom';
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>' .
'<rss version="2.0" xmlns:dc="'.$dc.'" xmlns:atom="'.$atom.'"/>');
$channel = $xml->addChild('channel');
$channel->addChild('title', 'Mailbox: '.$user);
$channel->addChild('link', $url);
$link = $channel->addChild('atom:link', '', $atom);
$link->addAttribute('href', $uri);
$link->addAttribute('rel', 'self');
$channel->addChild('description', 'mAiLIAS: Personal ad hoc email addresses.');
foreach ( scandir($maildir) as $filename )
{
$email = htmlentities(file_get_contents($maildir . $filename));
if ( preg_match("/Original-To: [email protected]/", $email) )
{
preg_match('/Date: (.*)/', $email, $date);
preg_match('/From: (.*)/', $email, $from);
preg_match('/Subject: (.*)/', $email, $subject);
$date_stamp = strtotime($date[1]);
$date = date("c", $date_stamp);
$node = $channel->addChild('item');
$node->addChild('title', $subject[1]);
$node->addChild('link', $url.$user.'/'.$filename);
$node->addChild('guid', $url.$user.'/'.$filename);
$node->addChild('description');
$node->addChild('dc:creator', $from[1], $dc);
$node->addChild('dc:date', $date, $dc);
}
}
Header('Content-type: text/xml; charset=UTF-8');
print($xml->asXML());
}
function get_email($filename)
{
global $maildir;
class SimpleXMLExtended extends SimpleXMLElement {
public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
$email = file_get_contents($maildir . $filename);
$Parser = new MimeMailParser();
$Parser->setText($email);
$to = $Parser->getHeader('x-original-to');
$from = $Parser->getHeader('from');
$subject = $Parser->getHeader('subject');
$date = $Parser->getHeader('date');
$body = $Parser->getMessageBody('html') ?: $Parser->getMessageBody('text');
$xml = new SimpleXMLExtended(
'<?xml version="1.0"?>' .
'<?xml-stylesheet type="text/xsl" href="../web/mailias.xsl"?>' .
'<Message/>');
$xml->addChild('To', $to);
$xml->addChild('From', $from);
$xml->addChild('Subject', $subject);
$xml->addChild('Date', $date);
$xml->addChild('Text')->addCData($body);
$xml->addChild('Raw')->addCData($email);
Header('Content-type: text/xml; charset=UTF-8');
print($xml->asXML());
}
function delete_email($filename)
{
global $maildir;
$handle = realpath($maildir . $filename);
if ( $handle !== ($maildir . $filename) )
{
// Can't have relative paths in the file name!
return;
}
else
{
if ( file_exists($handle) === false )
{
// File not found!
return;
}
else
{
unlink($handle);
}
}
}
function forward_email($filename, $to_address)
{
global $maildir;
$handle = realpath($maildir . $filename);
if ( $handle !== ($maildir . $filename) )
{
// Can't have relative paths in the file name!
return;
}
else
{
if ( file_exists($handle) === false )
{
// File not found!
return;
}
else
{
$email = file_get_contents($handle);
preg_match('/From:.*<(.*)>/', $email, $from);
$lSmtpTalk = array(
array('220', 'HELO '.$_SERVER['SERVER_NAME'].chr(10)),
array('250', 'MAIL FROM: <' . $from[1] . '>'.chr(10)),
array('250', 'RCPT TO: <' . $to_address . '>'.chr(10)),
array('250', 'DATA'.chr(10)),
array('354', $email.chr(10).'.'.chr(10)),
array('250', 'QUIT'.chr(10)),
array('221', ''));
$lConnection = fsockopen('localhost', 25, $errno, $errstr, 1);
if (!$lConnection) die('Cant relay, no connnection');
for ($i=0;$i<count($lSmtpTalk);$i++) {
$lRes = fgets($lConnection, 256);
if (substr($lRes, 0, 3) !== $lSmtpTalk[$i][0])
die('Got '.$lRes.' - expected: '.$lSmtpTalk[$i][0]);
if ($lSmtpTalk[$i][1] !== '')
fputs($lConnection, $lSmtpTalk[$i][1]);
}
fclose($lConnection);
}
}
}
/*--------------------------------------------------------------------------*/
$user = isset($_GET['user']) ? $_GET['user'] : '';
$mail = isset($_GET['id']) ? $_GET['id'] : '';
$del = isset($_GET['del']) ? $_GET['del'] : '';
$push = isset($_GET['push']) ? $_GET['push'] : '';
$rss = isset($_GET['rss']) ? $_GET['rss'] : '';
if ( $user != '' )
{
if ( $mail != '' )
{
if ( $del == '1' )
{
delete_email($mail);
}
else if ( $push != '' )
{
forward_email($mail, $push);
}
else
{
get_email($mail);
}
}
else
{
if ( $rss == '1' )
{
get_mailbox_rss($user);
}
else
{
get_mailbox($user);
}
}
}
else
{
if ( $hide_mailbox_list == true )
{
$xml = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>' .
'<?xml-stylesheet type="text/xsl" href="web/mailias.xsl"?>' .
'<Mailias/>');
Header('Content-type: text/xml; charset=UTF-8');
print($xml->asXML());
}
else
{
get_all_mailboxes();
}
}
?>