implement exact TO/CC match #2

This commit is contained in:
Synox 2016-11-26 18:46:08 +01:00
parent d800acdee6
commit 6953b653b9

View File

@ -30,14 +30,19 @@ function print_inbox($username) {
if (strlen($name) === 0) { if (strlen($name) === 0) {
error(400, 'invalid username'); error(400, 'invalid username');
} }
$to = get_address($name, $config['mailHostname']); $address = get_address($name, $config['mailHostname']);
$mail_ids = search_mails($to, $mailbox); $mail_ids = search_mails($address, $mailbox);
$emails = array(); $emails = array();
foreach ($mail_ids as $id) { foreach ($mail_ids as $id) {
$emails[] = $mailbox->getMail($id); $mail = $mailbox->getMail($id);
// imap_search also returns partials matches. The mails have to be filtered again:
if (!array_key_exists($address, $mail->to) && !array_key_exists($address, $mail->cc)) {
continue;
}
$emails[] = $mail;
} }
$address = get_address($name, $config['mailHostname']);
$data = array("mails" => $emails, 'username' => $name, 'address' => $address); $data = array("mails" => $emails, 'username' => $name, 'address' => $address);
print(json_encode($data)); print(json_encode($data));
@ -48,9 +53,9 @@ function print_inbox($username) {
* Search for mails with the recipient $to. * Search for mails with the recipient $to.
* @return array mail ids * @return array mail ids
*/ */
function search_mails($to, $mailbox) { function search_mails($address, $mailbox) {
$filterTO = 'TO "' . $to . '"'; $filterTO = 'TO "' . $address . '"';
$filterCC = 'CC "' . $to . '"'; $filterCC = 'CC "' . $address . '"';
$mailsIdsTo = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, $filterTO); $mailsIdsTo = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, $filterTO);
$mailsIdsCc = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, $filterCC); $mailsIdsCc = imap_sort($mailbox->getImapStream(), SORTARRIVAL, true, SE_UID, $filterCC);
return array_merge($mailsIdsTo, $mailsIdsCc); return array_merge($mailsIdsTo, $mailsIdsCc);