page: store placeholder values in $Page object
[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'] = mailparse_rfc822_parse_addresses($headlist['From']);
10         //TODO: imap_rfc822_parse_adrlist() alternative
11         return $headlist;
12 }
13
14 if ($msgid) {
15         $filename = "$mailbox/$msgid";
16         list ($headerdata, $rawbody) = explode("\n\n", file_get_contents($filename), 2);
17         $head = parsemailhead($headerdata);
18
19         $Page->title = 'Mailbericht ' . $head['date']->format('Y-m-d H:i');
20         printf("<h2>%s</h2>\n", htmlspecialchars($head['Subject'] ?? 'Mailbericht zonder onderwerp'));
21
22         print '<dl class="terse">';
23         printf('<dt>Ontvangen:</dt><dd>%s</dd>', $head['date']->format('c'));
24         printf('<dt>Verzender:</dt><dd>%s</dd>', htmlspecialchars($head['from'][0]['display']));
25         print '</dl>';
26
27         if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
28                 $body = $rawbody;
29                 if (($head['Content-Transfer-Encoding'] ?? '') === 'quoted-printable') {
30                         $body = quoted_printable_decode($body);
31                 }
32                 printf('<pre>%s</pre>', htmlspecialchars($body));
33         }
34         else {
35                 printf('<p>Geen ondersteuning voor <em>%s</em>.</p>', htmlspecialchars($head['Content-Type']));
36
37                 /* TODO
38                 $mime = mailparse_msg_parse_file($filename);
39                 $part = mailparse_msg_get_part($mime, '1');
40                 mailparse_msg_extract_part_file($part, $filename);
41                 */
42         }
43         return;
44 }
45
46 if (!$User->admin('user')) {
47         http_response_code(403);
48         $Page->place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
49         $Page->place['maillist'] = '';
50         return TRUE;
51 }
52
53 $rows = glob("$mailbox/*");
54 if (!$rows) {
55         throw new Exception('Kon inbox niet openen.');
56 }
57 array_splice($rows, 0, -50);
58
59 ob_start();
60 print '<ul>';
61 foreach (array_reverse($rows) as $filename) {
62         list ($headerdata) = explode("\n\n", file_get_contents($filename));
63         $head = parsemailhead($headerdata);
64
65         printf('<li><a href="%s">', "/{$Page->handler}/".basename($filename));
66
67         print $head['Subject'];
68         printf(' <small class="date">%s</small>',
69                 showdate(explode('-', $head['date']->format('Y-m-d')))
70         );
71         print ' <em class="right">'.htmlspecialchars($head['from'][0]['display']).'</em>';
72         print "</a></li>\n";
73 }
74 print "</ul>\n";
75 $Page->place['maillist'] = ob_get_clean();