mail: primitive maildir message reader
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 28 Nov 2020 23:48:38 +0000 (00:48 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 5 Dec 2020 00:41:21 +0000 (01:41 +0100)
Minimal admin interface to access site emails;
formatted similarly to issue page.

mail/index.html [new file with mode: 0644]
mail/index.php [new file with mode: 0644]

diff --git a/mail/index.html b/mail/index.html
new file mode 100644 (file)
index 0000000..58e128c
--- /dev/null
@@ -0,0 +1,3 @@
+<h2>E-mails</h2>
+
+[[maillist]]
diff --git a/mail/index.php b/mail/index.php
new file mode 100644 (file)
index 0000000..76ec033
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+$mailbox = 'mail/inbox';
+@list ($msgid) = explode('/', ltrim($Args, '/'));
+
+function parsemailhead($headerdata)
+{
+       $headlist = iconv_mime_decode_headers($headerdata, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
+       $headlist['date'] = DateTime::createFromFormat(DateTimeInterface::RFC2822.'+', $headlist['Date']);
+       $headlist['from'] = mailparse_rfc822_parse_addresses($headlist['From']);
+       //TODO: imap_rfc822_parse_adrlist() alternative
+       return $headlist;
+}
+
+if ($msgid) {
+       $filename = "$mailbox/$msgid";
+       list ($headerdata, $rawbody) = explode("\n\n", file_get_contents($filename), 2);
+       $head = parsemailhead($headerdata);
+
+       $Article->title = 'Mailbericht ' . $head['date']->format('Y-m-d H:i');
+       printf("<h2>%s</h2>\n", htmlspecialchars($head['Subject'] ?? 'Mailbericht zonder onderwerp'));
+
+       print '<dl class="terse">';
+       printf('<dt>Ontvangen:</dt><dd>%s</dd>', $head['date']->format('c'));
+       printf('<dt>Verzender:</dt><dd>%s</dd>', htmlspecialchars($head['from'][0]['display']));
+       print '</dl>';
+
+       if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
+               $body = $rawbody;
+               if (($head['Content-Transfer-Encoding'] ?? '') === 'quoted-printable') {
+                       $body = quoted_printable_decode($body);
+               }
+               printf('<pre>%s</pre>', htmlspecialchars($body));
+       }
+       else {
+               printf('<p>Geen ondersteuning voor <em>%s</em>.</p>', htmlspecialchars($head['Content-Type']));
+
+               /* TODO
+               $mime = mailparse_msg_parse_file($filename);
+               $part = mailparse_msg_get_part($mime, '1');
+               mailparse_msg_extract_part_file($part, $filename);
+               */
+       }
+       return;
+}
+
+if (!$User->admin('user')) {
+       http_response_code(403);
+       $Place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
+       $Place['maillist'] = '';
+       return TRUE;
+}
+
+$rows = glob("$mailbox/*");
+if (!$rows) {
+       throw new Exception('Kon inbox niet openen.');
+}
+array_splice($rows, 0, -50);
+
+ob_start();
+print '<ul>';
+foreach (array_reverse($rows) as $filename) {
+       list ($headerdata) = explode("\n\n", file_get_contents($filename));
+       $head = parsemailhead($headerdata);
+
+       printf('<li><a href="%s">', "/$Page/".basename($filename));
+
+       print $head['Subject'];
+       printf(' <small class="date">%s</small>',
+               showdate(explode('-', $head['date']->format('Y-m-d')))
+       );
+       print ' <em class="right">'.htmlspecialchars($head['from'][0]['display']).'</em>';
+       print "</a></li>\n";
+}
+print "</ul>\n";
+$Place['maillist'] = ob_get_clean();