| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg\Mail\ImapEngine; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Core; |
| 26 |
use Liuch\DmarcSrg\ErrorHandler; |
| 27 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 28 |
use Liuch\DmarcSrg\Exception\MailboxException; |
| 29 |
|
| 30 |
use DirectoryTree\ImapEngine\Mailbox as IMailbox; |
| 31 |
use DirectoryTree\ImapEngine\Exceptions\Exception as IException; |
| 32 |
use DirectoryTree\ImapEngine\Exceptions\ImapCommandException; |
| 33 |
|
| 34 |
class MailBox extends \Liuch\DmarcSrg\Mail\MailBox |
| 35 |
{ |
| 36 |
private $conn = null; |
| 37 |
private $folder = null; |
| 38 |
|
| 39 |
public function __clone(): void |
| 40 |
{ |
| 41 |
parent::__clone(); |
| 42 |
$this->conn = null; |
| 43 |
$this->folder = null; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function expunge(): void |
| 52 |
{ |
| 53 |
try { |
| 54 |
if ($this->conn && $this->conn->connected()) { |
| 55 |
$this->folder->expunge(); |
| 56 |
} |
| 57 |
} catch (IException $e) { |
| 58 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function cleanup(): void |
| 68 |
{ |
| 69 |
try { |
| 70 |
if ($this->conn && $this->conn->connected()) { |
| 71 |
$this->conn->disconnect(); |
| 72 |
$this->conn = null; |
| 73 |
} |
| 74 |
} catch (IException $e) { |
| 75 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function childMailbox(string $folder) |
| 80 |
{ |
| 81 |
$this->ensureConnection(); |
| 82 |
$mbox = $this->folder->path() . $this->folder->delimiter() . $folder; |
| 83 |
|
| 84 |
$child = clone $this; |
| 85 |
$child->mbox = $mbox; |
| 86 |
return $child; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function check(): array |
| 95 |
{ |
| 96 |
$res = []; |
| 97 |
try { |
| 98 |
$this->ensureConnection(); |
| 99 |
try { |
| 100 |
$res = $this->folder->status(); |
| 101 |
$attrs = $this->folder->flags(); |
| 102 |
if (!in_array('\HasChildren', $attrs)) { |
| 103 |
throw new MailboxException('The mailbox may not have any children mailboxes'); |
| 104 |
} |
| 105 |
if (in_array('\Noselect', $attrs)) { |
| 106 |
throw new MailboxException('The resource is not a mailbox'); |
| 107 |
} |
| 108 |
} catch (IException $e) { |
| 109 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 110 |
throw new MailboxException("Failed to get mailbox status", -1, $e); |
| 111 |
} |
| 112 |
} catch (MailboxException $e) { |
| 113 |
return ErrorHandler::exceptionResult($e); |
| 114 |
} |
| 115 |
return [ |
| 116 |
'error_code' => 0, |
| 117 |
'message' => 'Successfully', |
| 118 |
'status' => [ |
| 119 |
'messages' => $res['MESSAGES'] ?? 0, |
| 120 |
'unseen' => $res['UNSEEN'] ?? 0 |
| 121 |
] |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
public function messages(int $search_criteria, int $sort_criteria): array |
| 126 |
{ |
| 127 |
$this->ensureConnection(); |
| 128 |
|
| 129 |
$query = $this->folder->messages()->withHeaders(); |
| 130 |
switch ($search_criteria) { |
| 131 |
case static::SEARCH_ALL: |
| 132 |
$query->all(); |
| 133 |
break; |
| 134 |
case static::SEARCH_SEEN: |
| 135 |
$query->seen(); |
| 136 |
break; |
| 137 |
case static::SEARCH_UNSEEN: |
| 138 |
$query->unseen(); |
| 139 |
break; |
| 140 |
default: |
| 141 |
throw new LogicException('Wrong search criteria value'); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
switch ($sort_criteria) { |
| 146 |
case static::ORDER_ASCENT: |
| 147 |
|
| 148 |
$descending = false; |
| 149 |
break; |
| 150 |
case static::ORDER_DESCENT: |
| 151 |
|
| 152 |
$descending = true; |
| 153 |
break; |
| 154 |
default: |
| 155 |
throw new LogicException('Wrong sort criteria value'); |
| 156 |
} |
| 157 |
|
| 158 |
$res = []; |
| 159 |
$query->get()->sortBy(function ($i_msg, $key) { |
| 160 |
return $i_msg->date()->getTimestamp(); |
| 161 |
}, SORT_REGULAR, $descending)->each(function ($i_msg) use (&$res) { |
| 162 |
$res[] = new MailMessage([ 'message' => $i_msg, 'mailbox' => $this ]); |
| 163 |
}); |
| 164 |
|
| 165 |
return $res; |
| 166 |
} |
| 167 |
|
| 168 |
public function ensureMailbox($folder): void |
| 169 |
{ |
| 170 |
$fpath = $this->folder->path() . $this->folder->delimiter() . $folder; |
| 171 |
if (!$this->conn->folders()->find($fpath)) { |
| 172 |
try { |
| 173 |
$this->conn->folders()->create($fpath); |
| 174 |
} catch (IException $e) { |
| 175 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 176 |
throw new MailboxException('IMAP: Cannot create a mailbox folder', -1, $e); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
private function ensureConnection(): void |
| 182 |
{ |
| 183 |
if (!$this->conn) { |
| 184 |
if (preg_match('/^(.+):(\d+)$/', $this->host, $host_a) === 1) { |
| 185 |
$host = $host_a[1]; |
| 186 |
$port = intval($host_a[2]); |
| 187 |
} else { |
| 188 |
$host = $this->host; |
| 189 |
$port = 0; |
| 190 |
} |
| 191 |
$i = strrpos($this->host, ':'); |
| 192 |
|
| 193 |
$params = [ |
| 194 |
'host' => $host, |
| 195 |
'username' => $this->uname, |
| 196 |
'password' => $this->passw, |
| 197 |
'encryption' => $this->encrypt == 'none' ? null : $this->encrypt, |
| 198 |
'validate_cert' => !$this->nocert |
| 199 |
]; |
| 200 |
switch ($this->authm) { |
| 201 |
case 'plain': |
| 202 |
break; |
| 203 |
case 'oauth': |
| 204 |
$params['authentication'] = $this->authm; |
| 205 |
break; |
| 206 |
default: |
| 207 |
throw new LogicException('Unknown authentication method'); |
| 208 |
} |
| 209 |
if ($port) { |
| 210 |
$params['port'] = $port; |
| 211 |
} |
| 212 |
$this->conn = new IMailbox($params); |
| 213 |
} elseif ($this->conn->connected()) { |
| 214 |
return; |
| 215 |
} |
| 216 |
try { |
| 217 |
$this->conn->connect(); |
| 218 |
} catch (IException $e) { |
| 219 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 220 |
if ($e instanceof ImapCommandException) { |
| 221 |
throw new MailboxException("IMAP: Authentication failed", -1, $e); |
| 222 |
} else { |
| 223 |
throw new MailboxException("IMAP: Connection failed", -1, $e); |
| 224 |
} |
| 225 |
} |
| 226 |
try { |
| 227 |
$this->folder = $this->conn->folders()->find($this->mbox); |
| 228 |
$this->folder->select(); |
| 229 |
} catch (IException $e) { |
| 230 |
Core::instance()->logger()->error("IMAP error: {$e->getMessage()}"); |
| 231 |
throw new MailboxException("IMAP: Cannot select mailbox", -1, $e); |
| 232 |
} |
| 233 |
} |
| 234 |
} |