diff --git a/src/imap_client.php b/src/imap_client.php index 75314ce..588b308 100644 --- a/src/imap_client.php +++ b/src/imap_client.php @@ -1,7 +1,5 @@ -// TODO: define return types class ImapClient { /*PhpImap\Mailbox */ @@ -33,15 +31,15 @@ class ImapClient { * @param $mailid integer imap email id * @param $user User * @internal param the $username matching username + * @return true if success */ - public function delete_email(string $mailid, User $user) { + public function delete_email(string $mailid, User $user): bool { if ($this->_load_one_email($mailid, $user) !== null) { $this->mailbox->deleteMail($mailid); $this->mailbox->expungeDeletedMails(); + return true; } else { - // TODO: use return/throw instead - @http_response_code(404); - die('delete error: invalid username/mailid combination'); + return false; } } @@ -54,7 +52,7 @@ class ImapClient { * @internal param the $username matching username */ - public function download_email(int $mailid, User $user) { + public function download_email(int $mailid, User $user): bool { if ($this->_load_one_email($mailid, $user) !== null) { header("Content-Type: message/rfc822; charset=utf-8"); header("Content-Disposition: attachment; filename=\"" . $user->address . "-" . $mailid . ".eml\""); @@ -62,8 +60,9 @@ class ImapClient { $headers = imap_fetchheader($this->mailbox->getImapStream(), $mailid, FT_UID); $body = imap_body($this->mailbox->getImapStream(), $mailid, FT_UID); print $headers . "\n" . $body; + return true; } else { - error(404, 'download error: invalid username/mailid combination'); + return false; } } diff --git a/src/pages.php b/src/pages.php index 5c8a814..7af8f60 100644 --- a/src/pages.php +++ b/src/pages.php @@ -76,7 +76,9 @@ class DownloadEmailPage extends Page { $this->if_invalid_redirect_to_random($user, $this->config_domains); $download_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); - $imapClient->download_email($download_email_id, $user); + if (!$imapClient->download_email($download_email_id, $user)) { + $this->error(404, 'download error: invalid username/mailid combination'); + } } } @@ -99,8 +101,11 @@ class DeleteEmailPage extends Page { $this->if_invalid_redirect_to_random($user, $this->config_domains); $delete_email_id = filter_var($this->email_id, FILTER_SANITIZE_NUMBER_INT); - $imapClient->delete_email($delete_email_id, $user); - header("location: ?" . $user->address); + if ($imapClient->delete_email($delete_email_id, $user)) { + header("location: ?" . $user->address); + } else { + $this->error(404, 'delete error: invalid username/mailid combination'); + } } }