mail: imap function to parse sender addresses
[minimedit.git] / mail / index.php
index 76ec0334a224b5fc5b560b743ac517b94a81269c..b4846cc8f48b5c4f8e21420562175a44eade63e9 100644 (file)
@@ -1,13 +1,15 @@
 <?php
 $mailbox = 'mail/inbox';
-@list ($msgid) = explode('/', ltrim($Args, '/'));
+@list ($msgid) = explode('/', ltrim($Page->path, '/'));
 
 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
+       $headlist['from'] = imap_rfc822_parse_adrlist($headlist['From'], '');
+       array_walk($headlist['from'], function ($row) {
+               $row->display = $row->personal ?? $row->mailbox;
+       });
        return $headlist;
 }
 
@@ -16,12 +18,12 @@ if ($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');
+       $Page->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']));
+       printf('<dt>Verzender:</dt><dd>%s</dd>', htmlspecialchars($head['From']));
        print '</dl>';
 
        if (preg_match('{^text/plain}', $head['Content-Type'] ?? 'text/plain')) {
@@ -45,8 +47,8 @@ if ($msgid) {
 
 if (!$User->admin('user')) {
        http_response_code(403);
-       $Place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
-       $Place['maillist'] = '';
+       $Page->place['warn'] = "Geen gebruikersrechten om e-mails in te zien.";
+       $Page->place['maillist'] = '';
        return TRUE;
 }
 
@@ -59,17 +61,23 @@ array_splice($rows, 0, -50);
 ob_start();
 print '<ul>';
 foreach (array_reverse($rows) as $filename) {
+       if (!is_readable($filename)) {
+               continue;
+       }
+
+       printf('<li><a href="%s">', "/{$Page->handler}/".basename($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>';
+       printf(' <em class="right">%s</em>',
+               htmlspecialchars(implode(', ', array_column($head['from'], 'display')))
+       );
        print "</a></li>\n";
 }
 print "</ul>\n";
-$Place['maillist'] = ob_get_clean();
+$Page->place['maillist'] = ob_get_clean();