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