8fb49c7f8d27ef144c1dd9f4bdab064d7e0d94ee
[minimedit.git] / mail / index.php
1 <?php
2 $mailbox = 'mail/inbox';
3 @list ($msgid) = explode('/', ltrim($Page->path, '/'));
4
5 function parsemailhead($headerdata)
6 {
7         $headlist = iconv_mime_decode_headers($headerdata, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
8         $headlist['date'] = DateTime::createFromFormat(DateTimeInterface::RFC2822.'+', $headlist['Date']);
9         $headlist['from'] = imap_rfc822_parse_adrlist($headlist['From'], '');
10         array_walk($headlist['from'], function ($row) {
11                 $row->display = $row->personal ?? $row->mailbox;
12         });
13         return $headlist;
14 }
15
16 if ($msgid) {
17         $filename = "$mailbox/$msgid";
18         list ($headerdata, $rawbody) = explode("\n\n", file_get_contents($filename), 2);
19         $head = parsemailhead($headerdata);
20         $head['date']->setTimezone(new DateTimeZone(date_default_timezone_get()));
21
22         $Page->title = 'Mailbericht ' . $head['date']->format('Y-m-d H:i');
23         printf("<h2>%s</h2>\n", htmlspecialchars($head['Subject'] ?? 'Mailbericht zonder onderwerp'));
24
25         printf('<h3><a href="mailto:%s">%s</a> <small class="date" title="%s">%s</small></h3>'."\n",
26                 htmlspecialchars($head['From']),
27                 htmlspecialchars(implode(', ', array_column($head['from'], 'display'))),
28                 htmlspecialchars($head['Date']),
29                 showdate(preg_split('/\D/', $head['date']->format('c')))
30         );
31         print '</dl>';
32
33         if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
34                 $body = $rawbody;
35                 if (($head['Content-Transfer-Encoding'] ?? '') === 'quoted-printable') {
36                         $body = quoted_printable_decode($body);
37                 }
38                 printf('<pre>%s</pre>', htmlspecialchars($body));
39         }
40         else {
41                 printf('<p>Geen ondersteuning voor <em>%s</em>.</p>', htmlspecialchars($head['Content-Type']));
42
43                 /* TODO
44                 $mime = mailparse_msg_parse_file($filename);
45                 $part = mailparse_msg_get_part($mime, '1');
46                 mailparse_msg_extract_part_file($part, $filename);
47                 */
48         }
49         return;
50 }
51
52 if (!$User->admin('user')) {
53         http_response_code(403);
54         $Page->place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
55         $Page->place['maillist'] = '';
56         return TRUE;
57 }
58
59 $rows = glob("$mailbox/*");
60 if (!$rows) {
61         throw new Exception('Kon inbox niet openen.');
62 }
63
64 $nav = [
65         'start' => $_GET['start'] ?? 0,
66         'n'     => $_GET['n'] ?? 10,
67         'total' => count($rows),
68 ];
69 $rows = array_slice(array_reverse($rows), $nav['start'], $nav['n']);
70
71 ob_start();
72 print '<ul>';
73 foreach (array_reverse($rows) as $filename) {
74         if (!is_readable($filename)) {
75                 continue;
76         }
77
78         printf('<li><a href="%s">', "/{$Page->handler}/".basename($filename));
79
80         list ($headerdata) = explode("\n\n", file_get_contents($filename));
81         $head = parsemailhead($headerdata);
82
83         print $head['Subject'];
84         printf(' <small class="date" title="%s">%s</small>',
85                 htmlspecialchars($head['Date']),
86                 showdate(explode('-', $head['date']->format('Y-m-d')))
87         );
88         printf(' <em class="right" title="%s">%s</em>',
89                 htmlspecialchars($head['From']),
90                 htmlspecialchars(implode(', ', array_column($head['from'], 'display')))
91         );
92         print "</a></li>\n";
93 }
94 print "</ul>\n";
95
96 print $Page->widget('nav', $nav);
97
98 $Page->place['maillist'] = ob_get_clean();