Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap; |
| 6 | |
| 7 | use Ddeboer\Imap\Exception\CreateMailboxException; |
| 8 | use Ddeboer\Imap\Exception\DeleteMailboxException; |
| 9 | use Ddeboer\Imap\Exception\ImapGetmailboxesException; |
| 10 | use Ddeboer\Imap\Exception\ImapNumMsgException; |
| 11 | use Ddeboer\Imap\Exception\ImapQuotaException; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 12 | use Ddeboer\Imap\Exception\MailboxDoesNotExistException; |
| 13 | |
| 14 | /** |
| 15 | * A connection to an IMAP server that is authenticated for a user. |
| 16 | */ |
| 17 | final class Connection implements ConnectionInterface |
| 18 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 19 | private ImapResourceInterface $resource; |
| 20 | private string $server; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 21 | /** |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 22 | * @var null|MailboxInterface[] |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 23 | */ |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 24 | private ?array $mailboxes = null; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 25 | /** |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 26 | * @var null|array<int|string, \stdClass> |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 27 | */ |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 28 | private ?array $mailboxNames = null; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | * |
| 33 | * @throws \InvalidArgumentException |
| 34 | */ |
| 35 | public function __construct(ImapResourceInterface $resource, string $server) |
| 36 | { |
| 37 | $this->resource = $resource; |
| 38 | $this->server = $server; |
| 39 | } |
| 40 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 41 | public function getResource(): ImapResourceInterface |
| 42 | { |
| 43 | return $this->resource; |
| 44 | } |
| 45 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 46 | public function expunge(): bool |
| 47 | { |
| 48 | return \imap_expunge($this->resource->getStream()); |
| 49 | } |
| 50 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 51 | public function close(int $flag = 0): bool |
| 52 | { |
| 53 | $this->resource->clearLastMailboxUsedCache(); |
| 54 | |
| 55 | return \imap_close($this->resource->getStream(), $flag); |
| 56 | } |
| 57 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 58 | public function getQuota(string $root = 'INBOX'): array |
| 59 | { |
| 60 | $errorMessage = null; |
| 61 | $errorNumber = 0; |
| 62 | \set_error_handler(static function ($nr, $message) use (&$errorMessage, &$errorNumber): bool { |
| 63 | $errorMessage = $message; |
| 64 | $errorNumber = $nr; |
| 65 | |
| 66 | return true; |
| 67 | }); |
| 68 | |
| 69 | $return = \imap_get_quotaroot($this->resource->getStream(), $root); |
| 70 | |
| 71 | \restore_error_handler(); |
| 72 | |
| 73 | if (false === $return || null !== $errorMessage) { |
| 74 | throw new ImapQuotaException( |
| 75 | \sprintf( |
| 76 | 'IMAP Quota request failed for "%s"%s', |
| 77 | $root, |
| 78 | null !== $errorMessage ? ': ' . $errorMessage : '' |
| 79 | ), |
| 80 | $errorNumber |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | return $return; |
| 85 | } |
| 86 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 87 | public function getMailboxes(): array |
| 88 | { |
| 89 | $this->initMailboxNames(); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 90 | \assert(null !== $this->mailboxNames); |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 91 | |
| 92 | if (null === $this->mailboxes) { |
| 93 | $this->mailboxes = []; |
| 94 | foreach ($this->mailboxNames as $mailboxName => $mailboxInfo) { |
| 95 | $this->mailboxes[(string) $mailboxName] = $this->getMailbox((string) $mailboxName); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return $this->mailboxes; |
| 100 | } |
| 101 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 102 | public function hasMailbox(string $name): bool |
| 103 | { |
| 104 | $this->initMailboxNames(); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 105 | \assert(null !== $this->mailboxNames); |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 106 | |
| 107 | return isset($this->mailboxNames[$name]); |
| 108 | } |
| 109 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 110 | public function getMailbox(string $name): MailboxInterface |
| 111 | { |
| 112 | if (false === $this->hasMailbox($name)) { |
| 113 | throw new MailboxDoesNotExistException(\sprintf('Mailbox name "%s" does not exist', $name)); |
| 114 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 115 | \assert(isset($this->mailboxNames[$name])); |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 116 | |
| 117 | return new Mailbox($this->resource, $name, $this->mailboxNames[$name]); |
| 118 | } |
| 119 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 120 | #[\ReturnTypeWillChange] |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 121 | public function count() |
| 122 | { |
| 123 | $return = \imap_num_msg($this->resource->getStream()); |
| 124 | |
| 125 | if (false === $return) { |
| 126 | throw new ImapNumMsgException('imap_num_msg failed'); |
| 127 | } |
| 128 | |
| 129 | return $return; |
| 130 | } |
| 131 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 132 | public function ping(): bool |
| 133 | { |
| 134 | return \imap_ping($this->resource->getStream()); |
| 135 | } |
| 136 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 137 | public function createMailbox(string $name): MailboxInterface |
| 138 | { |
| 139 | if (false === \imap_createmailbox($this->resource->getStream(), $this->server . \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8'))) { |
| 140 | throw new CreateMailboxException(\sprintf('Can not create "%s" mailbox at "%s"', $name, $this->server)); |
| 141 | } |
| 142 | |
| 143 | $this->mailboxNames = $this->mailboxes = null; |
| 144 | $this->resource->clearLastMailboxUsedCache(); |
| 145 | |
| 146 | return $this->getMailbox($name); |
| 147 | } |
| 148 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 149 | public function deleteMailbox(MailboxInterface $mailbox): void |
| 150 | { |
| 151 | if (false === \imap_deletemailbox($this->resource->getStream(), $mailbox->getFullEncodedName())) { |
| 152 | throw new DeleteMailboxException(\sprintf('Mailbox "%s" could not be deleted', $mailbox->getName())); |
| 153 | } |
| 154 | |
| 155 | $this->mailboxes = $this->mailboxNames = null; |
| 156 | $this->resource->clearLastMailboxUsedCache(); |
| 157 | } |
| 158 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 159 | private function initMailboxNames(): void |
| 160 | { |
| 161 | if (null !== $this->mailboxNames) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | $this->mailboxNames = []; |
| 166 | $mailboxesInfo = \imap_getmailboxes($this->resource->getStream(), $this->server, '*'); |
| 167 | if (!\is_array($mailboxesInfo)) { |
| 168 | throw new ImapGetmailboxesException('imap_getmailboxes failed'); |
| 169 | } |
| 170 | |
| 171 | foreach ($mailboxesInfo as $mailboxInfo) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 172 | $name = \mb_convert_encoding(\str_replace($this->server, '', $mailboxInfo->name), 'UTF-8', 'UTF7-IMAP'); |
Matthias Andreas Benkard | e39c4f8 | 2021-01-06 17:59:39 +0100 | [diff] [blame] | 173 | \assert(\is_string($name)); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 174 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 175 | $this->mailboxNames[$name] = $mailboxInfo; |
| 176 | } |
| 177 | } |
| 178 | } |