908e53165c013d4383bc697d59eeb5ec5ca172e2
[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) {
53         return;
54 }
55 if (!$User->admin('user')) {
56         http_response_code(403);
57         $Page->place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
58         $Page->place['maillist'] = '';
59         return TRUE;
60 }
61
62 $rows = glob("$mailbox/*");
63 if (!$rows) {
64         throw new Exception('Kon inbox niet openen.');
65 }
66
67 $nav = [
68         'start' => $_GET['start'] ?? 0,
69         'n'     => $_GET['n'] ?? 10,
70         'total' => count($rows),
71 ];
72 $rows = array_slice(array_reverse($rows), $nav['start'], $nav['n']);
73
74 ob_start();
75 print '<ul>';
76 foreach (array_reverse($rows) as $filename) {
77         if (!is_readable($filename)) {
78                 continue;
79         }
80
81         printf('<li><a href="%s">', "/{$Page->handler}/".basename($filename));
82
83         list ($headerdata) = explode("\n\n", file_get_contents($filename));
84         $head = parsemailhead($headerdata);
85
86         print $head['Subject'];
87         printf(' <small class="date" title="%s">%s</small>',
88                 htmlspecialchars($head['Date']),
89                 showdate(explode('-', $head['date']->format('Y-m-d')))
90         );
91         printf(' <em class="right" title="%s">%s</em>',
92                 htmlspecialchars($head['From']),
93                 htmlspecialchars(implode(', ', array_column($head['from'], 'display')))
94         );
95         print "</a></li>\n";
96 }
97 print "</ul>\n";
98
99 print $Page->widget('nav', $nav);
100
101 $Page->place['maillist'] = ob_get_clean();