| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Connection\Protocols; |
| 14 |
|
| 15 |
use Webklex\PHPIMAP\ClientManager; |
| 16 |
use Webklex\PHPIMAP\Config; |
| 17 |
use Webklex\PHPIMAP\Exceptions\AuthFailedException; |
| 18 |
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException; |
| 19 |
use Webklex\PHPIMAP\Exceptions\MethodNotSupportedException; |
| 20 |
use Webklex\PHPIMAP\Exceptions\RuntimeException; |
| 21 |
use Webklex\PHPIMAP\IMAP; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class LegacyProtocol extends Protocol { |
| 29 |
|
| 30 |
protected string $protocol = "imap"; |
| 31 |
protected string $host = "localhost"; |
| 32 |
protected int $port = 993; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function __construct(Config $config, bool $cert_validation = true, mixed $encryption = false) { |
| 41 |
$this->config = $config; |
| 42 |
$this->setCertValidation($cert_validation); |
| 43 |
$this->encryption = $encryption; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function __destruct() { |
| 50 |
$this->logout(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function connect(string $host, ?int $port = null): void { |
| 59 |
if ($this->encryption) { |
| 60 |
$encryption = strtolower($this->encryption); |
| 61 |
if ($encryption == "ssl") { |
| 62 |
$port = $port === null ? 993 : $port; |
| 63 |
} |
| 64 |
} |
| 65 |
$port = $port === null ? 143 : $port; |
| 66 |
$this->host = $host; |
| 67 |
$this->port = $port; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function login(string $user, string $password): Response { |
| 78 |
return $this->response()->wrap(function($response) use ($user, $password) { |
| 79 |
|
| 80 |
try { |
| 81 |
$this->stream = \imap_open( |
| 82 |
$this->getAddress(), |
| 83 |
$user, |
| 84 |
$password, |
| 85 |
0, |
| 86 |
$attempts = 3, |
| 87 |
$this->config->get('options.open') |
| 88 |
); |
| 89 |
$response->addCommand("imap_open"); |
| 90 |
} catch (\ErrorException $e) { |
| 91 |
$errors = \imap_errors(); |
| 92 |
$message = $e->getMessage() . '. ' . implode("; ", (is_array($errors) ? $errors : array())); |
| 93 |
throw new AuthFailedException($message); |
| 94 |
} |
| 95 |
|
| 96 |
if (!$this->stream) { |
| 97 |
$errors = \imap_errors(); |
| 98 |
$message = implode("; ", (is_array($errors) ? $errors : array())); |
| 99 |
throw new AuthFailedException($message); |
| 100 |
} |
| 101 |
|
| 102 |
$errors = \imap_errors(); |
| 103 |
$response->addCommand("imap_errors"); |
| 104 |
if (is_array($errors)) { |
| 105 |
$status = $this->examineFolder(); |
| 106 |
$response->stack($status); |
| 107 |
if ($status->data()['exists'] !== 0) { |
| 108 |
$message = implode("; ", $errors); |
| 109 |
throw new RuntimeException($message); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ($this->stream !== false) { |
| 114 |
return ["TAG" . $response->Noun() . " OK [] Logged in\r\n"]; |
| 115 |
} |
| 116 |
|
| 117 |
$response->addError("failed to login"); |
| 118 |
return []; |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function authenticate(string $user, string $token): Response { |
| 130 |
return $this->login($user, $token); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
protected function getAddress(): string { |
| 139 |
$address = "{" . $this->host . ":" . $this->port . "/" . $this->protocol; |
| 140 |
if (!$this->cert_validation) { |
| 141 |
$address .= '/novalidate-cert'; |
| 142 |
} |
| 143 |
if (in_array($this->encryption, ['tls', 'notls', 'ssl'])) { |
| 144 |
$address .= '/' . $this->encryption; |
| 145 |
} elseif ($this->encryption === "starttls") { |
| 146 |
$address .= '/tls'; |
| 147 |
} |
| 148 |
|
| 149 |
$address .= '}'; |
| 150 |
|
| 151 |
return $address; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function logout(): Response { |
| 160 |
return $this->response()->wrap(function($response) { |
| 161 |
|
| 162 |
if ($this->stream) { |
| 163 |
$this->uid_cache = []; |
| 164 |
$response->addCommand("imap_close"); |
| 165 |
if (\imap_close($this->stream, IMAP::CL_EXPUNGE)) { |
| 166 |
$this->stream = false; |
| 167 |
return [ |
| 168 |
0 => "BYE Logging out\r\n", |
| 169 |
1 => "TAG" . $response->Noun() . " OK Logout completed (0.001 + 0.000 secs).\r\n", |
| 170 |
]; |
| 171 |
} |
| 172 |
$this->stream = false; |
| 173 |
} |
| 174 |
return []; |
| 175 |
}); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function getCapabilities(): Response { |
| 184 |
throw new MethodNotSupportedException(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function selectFolder(string $folder = 'INBOX'): Response { |
| 195 |
$flags = IMAP::OP_READONLY; |
| 196 |
if (in_array($this->protocol, ["pop3", "nntp"])) { |
| 197 |
$flags = IMAP::NIL; |
| 198 |
} |
| 199 |
if ($this->stream === false) { |
| 200 |
throw new RuntimeException("failed to reopen stream."); |
| 201 |
} |
| 202 |
|
| 203 |
return $this->response("imap_reopen")->wrap(function($response) use ($folder, $flags) { |
| 204 |
|
| 205 |
\imap_reopen($this->stream, $this->getAddress() . $folder, $flags, 3); |
| 206 |
$this->uid_cache = []; |
| 207 |
|
| 208 |
$status = $this->examineFolder($folder); |
| 209 |
$response->stack($status); |
| 210 |
|
| 211 |
return $status->data(); |
| 212 |
}); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function examineFolder(string $folder = 'INBOX'): Response { |
| 223 |
if (str_starts_with($folder, ".")) { |
| 224 |
throw new RuntimeException("Segmentation fault prevented. Folders starts with an illegal char '.'."); |
| 225 |
} |
| 226 |
return $this->response("imap_status")->wrap(function($response) use ($folder) { |
| 227 |
|
| 228 |
$status = \imap_status($this->stream, $this->getAddress() . $folder, IMAP::SA_ALL); |
| 229 |
|
| 230 |
return $status ? [ |
| 231 |
"flags" => [], |
| 232 |
"exists" => $status->messages, |
| 233 |
"recent" => $status->recent, |
| 234 |
"unseen" => $status->unseen, |
| 235 |
"uidnext" => $status->uidnext, |
| 236 |
] : []; |
| 237 |
}); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response { |
| 247 |
throw new MethodNotSupportedException(); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function content(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response { |
| 259 |
return $this->response()->wrap(function($response) use ($uids, $uid) { |
| 260 |
|
| 261 |
|
| 262 |
$result = []; |
| 263 |
$uids = is_array($uids) ? $uids : [$uids]; |
| 264 |
foreach ($uids as $id) { |
| 265 |
$response->addCommand("imap_fetchbody"); |
| 266 |
$result[$id] = \imap_fetchbody($this->stream, $id, "", $uid === IMAP::ST_UID ? IMAP::ST_UID : IMAP::NIL); |
| 267 |
} |
| 268 |
|
| 269 |
return $result; |
| 270 |
}); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function headers(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response { |
| 282 |
return $this->response()->wrap(function($response) use ($uids, $uid) { |
| 283 |
|
| 284 |
|
| 285 |
$result = []; |
| 286 |
$uids = is_array($uids) ? $uids : [$uids]; |
| 287 |
foreach ($uids as $id) { |
| 288 |
$response->addCommand("imap_fetchheader"); |
| 289 |
$result[$id] = \imap_fetchheader($this->stream, $id, $uid ? IMAP::ST_UID : IMAP::NIL); |
| 290 |
} |
| 291 |
|
| 292 |
return $result; |
| 293 |
}); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function flags(int|array $uids, int|string $uid = IMAP::ST_UID): Response { |
| 304 |
return $this->response()->wrap(function($response) use ($uids, $uid) { |
| 305 |
|
| 306 |
|
| 307 |
$result = []; |
| 308 |
$uids = is_array($uids) ? $uids : [$uids]; |
| 309 |
foreach ($uids as $id) { |
| 310 |
$response->addCommand("imap_fetch_overview"); |
| 311 |
$raw_flags = \imap_fetch_overview($this->stream, $id, $uid ? IMAP::ST_UID : IMAP::NIL); |
| 312 |
$flags = []; |
| 313 |
if (is_array($raw_flags) && isset($raw_flags[0])) { |
| 314 |
$raw_flags = (array)$raw_flags[0]; |
| 315 |
foreach ($raw_flags as $flag => $value) { |
| 316 |
if ($value === 1 && in_array($flag, ["size", "uid", "msgno", "update"]) === false) { |
| 317 |
$flags[] = "\\" . ucfirst($flag); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
$result[$id] = $flags; |
| 322 |
} |
| 323 |
|
| 324 |
return $result; |
| 325 |
}); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function sizes(int|array $uids, int|string $uid = IMAP::ST_UID): Response { |
| 336 |
return $this->response()->wrap(function($response) use ($uids, $uid) { |
| 337 |
|
| 338 |
$result = []; |
| 339 |
$uids = is_array($uids) ? $uids : [$uids]; |
| 340 |
$uid_text = implode("','", $uids); |
| 341 |
$response->addCommand("imap_fetch_overview"); |
| 342 |
if ($uid == IMAP::ST_UID) { |
| 343 |
$raw_overview = \imap_fetch_overview($this->stream, $uid_text, IMAP::FT_UID); |
| 344 |
} else { |
| 345 |
$raw_overview = \imap_fetch_overview($this->stream, $uid_text); |
| 346 |
} |
| 347 |
if ($raw_overview !== false) { |
| 348 |
foreach ($raw_overview as $overview_element) { |
| 349 |
$overview_element = (array)$overview_element; |
| 350 |
$result[$overview_element[$uid == IMAP::ST_UID ? 'uid' : 'msgno']] = $overview_element['size']; |
| 351 |
} |
| 352 |
} |
| 353 |
return $result; |
| 354 |
}); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function getUid(?int $id = null): Response { |
| 364 |
return $this->response()->wrap(function($response) use ($id) { |
| 365 |
|
| 366 |
if ($id === null) { |
| 367 |
if ($this->enable_uid_cache && $this->uid_cache) { |
| 368 |
return $this->uid_cache; |
| 369 |
} |
| 370 |
|
| 371 |
$overview = $this->overview("1:*"); |
| 372 |
$response->stack($overview); |
| 373 |
$uids = []; |
| 374 |
foreach ($overview->data() as $set) { |
| 375 |
$uids[$set->msgno] = $set->uid; |
| 376 |
} |
| 377 |
|
| 378 |
$this->setUidCache($uids); |
| 379 |
return $uids; |
| 380 |
} |
| 381 |
|
| 382 |
$response->addCommand("imap_uid"); |
| 383 |
$uid = \imap_uid($this->stream, $id); |
| 384 |
if ($uid) { |
| 385 |
return $uid; |
| 386 |
} |
| 387 |
|
| 388 |
return []; |
| 389 |
}); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public function getMessageNumber(string $id): Response { |
| 399 |
return $this->response("imap_msgno")->wrap(function($response) use ($id) { |
| 400 |
|
| 401 |
return \imap_msgno($this->stream, $id); |
| 402 |
}); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public function overview(string $sequence, int|string $uid = IMAP::ST_UID): Response { |
| 413 |
return $this->response("imap_fetch_overview")->wrap(function($response) use ($sequence, $uid) { |
| 414 |
|
| 415 |
return \imap_fetch_overview($this->stream, $sequence, $uid ? IMAP::ST_UID : IMAP::NIL) ?: []; |
| 416 |
}); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public function folders(string $reference = '', string $folder = '*'): Response { |
| 427 |
return $this->response("imap_getmailboxes")->wrap(function($response) use ($reference, $folder) { |
| 428 |
|
| 429 |
$result = []; |
| 430 |
|
| 431 |
$items = \imap_getmailboxes($this->stream, $this->getAddress(), $reference . $folder); |
| 432 |
if (is_array($items)) { |
| 433 |
foreach ($items as $item) { |
| 434 |
$name = $this->decodeFolderName($item->name); |
| 435 |
$result[$name] = ['delimiter' => $item->delimiter, 'flags' => []]; |
| 436 |
} |
| 437 |
} else { |
| 438 |
throw new RuntimeException(\imap_last_error()); |
| 439 |
} |
| 440 |
|
| 441 |
return $result; |
| 442 |
}); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public function store(array|string $flags, int $from, ?int $to = null, ?string $mode = null, bool $silent = true, int|string $uid = IMAP::ST_UID, ?string $item = null): Response { |
| 459 |
$flag = trim(is_array($flags) ? implode(" ", $flags) : $flags); |
| 460 |
|
| 461 |
return $this->response()->wrap(function($response) use ($mode, $from, $flag, $uid, $silent) { |
| 462 |
|
| 463 |
|
| 464 |
if ($mode == "+") { |
| 465 |
$response->addCommand("imap_setflag_full"); |
| 466 |
$status = \imap_setflag_full($this->stream, $from, $flag, $uid ? IMAP::ST_UID : IMAP::NIL); |
| 467 |
} else { |
| 468 |
$response->addCommand("imap_clearflag_full"); |
| 469 |
$status = \imap_clearflag_full($this->stream, $from, $flag, $uid ? IMAP::ST_UID : IMAP::NIL); |
| 470 |
} |
| 471 |
|
| 472 |
if ($silent === true) { |
| 473 |
if ($status) { |
| 474 |
return [ |
| 475 |
"TAG" . $response->Noun() . " OK Store completed (0.001 + 0.000 secs).\r\n" |
| 476 |
]; |
| 477 |
} |
| 478 |
return []; |
| 479 |
} |
| 480 |
|
| 481 |
return $this->flags($from); |
| 482 |
}); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public function appendMessage(string $folder, string $message, ?array $flags = null, mixed $date = null): Response { |
| 495 |
return $this->response("imap_append")->wrap(function($response) use ($folder, $message, $flags, $date) { |
| 496 |
|
| 497 |
if ($date != null) { |
| 498 |
if ($date instanceof \Carbon\Carbon) { |
| 499 |
$date = $date->format('d-M-Y H:i:s O'); |
| 500 |
} |
| 501 |
if (\imap_append($this->stream, $this->getAddress() . $folder, $message, $flags, $date)) { |
| 502 |
return [ |
| 503 |
"OK Append completed (0.001 + 0.000 secs).\r\n" |
| 504 |
]; |
| 505 |
} |
| 506 |
} else if (\imap_append($this->stream, $this->getAddress() . $folder, $message, $flags)) { |
| 507 |
return [ |
| 508 |
"OK Append completed (0.001 + 0.000 secs).\r\n" |
| 509 |
]; |
| 510 |
} |
| 511 |
return []; |
| 512 |
}); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public function copyMessage(string $folder, $from, ?int $to = null, int|string $uid = IMAP::ST_UID): Response { |
| 526 |
return $this->response("imap_mail_copy")->wrap(function($response) use ($from, $folder, $uid) { |
| 527 |
|
| 528 |
|
| 529 |
if (\imap_mail_copy($this->stream, $from, $this->getAddress() . $folder, $uid ? IMAP::ST_UID : IMAP::NIL)) { |
| 530 |
return [ |
| 531 |
"TAG" . $response->Noun() . " OK Copy completed (0.001 + 0.000 secs).\r\n" |
| 532 |
]; |
| 533 |
} |
| 534 |
throw new ImapBadRequestException("Invalid ID $from"); |
| 535 |
}); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public function copyManyMessages(array $messages, string $folder, int|string $uid = IMAP::ST_UID): Response { |
| 547 |
return $this->response()->wrap(function($response) use ($messages, $folder, $uid) { |
| 548 |
|
| 549 |
foreach ($messages as $msg) { |
| 550 |
$copy_response = $this->copyMessage($folder, $msg, null, $uid); |
| 551 |
$response->stack($copy_response); |
| 552 |
if (empty($copy_response->data())) { |
| 553 |
return [ |
| 554 |
"TAG" . $response->Noun() . " BAD Copy failed (0.001 + 0.000 secs).\r\n", |
| 555 |
"Invalid ID $msg\r\n" |
| 556 |
]; |
| 557 |
} |
| 558 |
} |
| 559 |
return [ |
| 560 |
"TAG" . $response->Noun() . " OK Copy completed (0.001 + 0.000 secs).\r\n" |
| 561 |
]; |
| 562 |
}); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
public function moveMessage(string $folder, $from, ?int $to = null, int|string $uid = IMAP::ST_UID): Response { |
| 576 |
return $this->response("imap_mail_move")->wrap(function($response) use ($from, $folder, $uid) { |
| 577 |
if (\imap_mail_move($this->stream, $from, $this->getAddress() . $folder, $uid ? IMAP::ST_UID : IMAP::NIL)) { |
| 578 |
return [ |
| 579 |
"TAG" . $response->Noun() . " OK Move completed (0.001 + 0.000 secs).\r\n" |
| 580 |
]; |
| 581 |
} |
| 582 |
throw new ImapBadRequestException("Invalid ID $from"); |
| 583 |
}); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public function moveManyMessages(array $messages, string $folder, int|string $uid = IMAP::ST_UID): Response { |
| 596 |
return $this->response()->wrap(function($response) use ($messages, $folder, $uid) { |
| 597 |
foreach ($messages as $msg) { |
| 598 |
$move_response = $this->moveMessage($folder, $msg, null, $uid); |
| 599 |
$response = $response->include($response); |
| 600 |
if (empty($move_response->data())) { |
| 601 |
return [ |
| 602 |
"TAG" . $response->Noun() . " BAD Move failed (0.001 + 0.000 secs).\r\n", |
| 603 |
"Invalid ID $msg\r\n" |
| 604 |
]; |
| 605 |
} |
| 606 |
} |
| 607 |
return [ |
| 608 |
"TAG" . $response->Noun() . " OK Move completed (0.001 + 0.000 secs).\r\n" |
| 609 |
]; |
| 610 |
}); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public function ID($ids = null): Response { |
| 623 |
throw new MethodNotSupportedException(); |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
public function createFolder(string $folder): Response { |
| 633 |
return $this->response("imap_createmailbox")->wrap(function($response) use ($folder) { |
| 634 |
return \imap_createmailbox($this->stream, $this->getAddress() . $folder) ? [ |
| 635 |
0 => "TAG" . $response->Noun() . " OK Create completed (0.004 + 0.000 + 0.003 secs).\r\n", |
| 636 |
] : []; |
| 637 |
}); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
public function renameFolder(string $old, string $new): Response { |
| 648 |
return $this->response("imap_renamemailbox")->wrap(function($response) use ($old, $new) { |
| 649 |
return \imap_renamemailbox($this->stream, $this->getAddress() . $old, $this->getAddress() . $new) ? [ |
| 650 |
0 => "TAG" . $response->Noun() . " OK Move completed (0.004 + 0.000 + 0.003 secs).\r\n", |
| 651 |
] : []; |
| 652 |
}); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
public function deleteFolder(string $folder): Response { |
| 662 |
return $this->response("imap_deletemailbox")->wrap(function($response) use ($folder) { |
| 663 |
return \imap_deletemailbox($this->stream, $this->getAddress() . $folder) ? [ |
| 664 |
0 => "OK Delete completed (0.004 + 0.000 + 0.003 secs).\r\n", |
| 665 |
] : []; |
| 666 |
}); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
public function subscribeFolder(string $folder): Response { |
| 676 |
throw new MethodNotSupportedException(); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
public function unsubscribeFolder(string $folder): Response { |
| 686 |
throw new MethodNotSupportedException(); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
public function expunge(): Response { |
| 695 |
return $this->response("imap_expunge")->wrap(function($response) { |
| 696 |
return \imap_expunge($this->stream) ? [ |
| 697 |
0 => "TAG" . $response->Noun() . " OK Expunge completed (0.001 + 0.000 secs).\r\n", |
| 698 |
] : []; |
| 699 |
}); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public function noop(): Response { |
| 708 |
throw new MethodNotSupportedException(); |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
public function idle() { |
| 717 |
throw new MethodNotSupportedException(); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public function done() { |
| 726 |
throw new MethodNotSupportedException(); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
public function search(array $params, int|string $uid = IMAP::ST_UID): Response { |
| 737 |
return $this->response("imap_search")->wrap(function($response) use ($params, $uid) { |
| 738 |
$response->setCanBeEmpty(true); |
| 739 |
$result = \imap_search($this->stream, $params[0], $uid ? IMAP::ST_UID : IMAP::NIL); |
| 740 |
return $result ?: []; |
| 741 |
}); |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
public function enableDebug() { |
| 748 |
$this->debug = true; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
public function disableDebug() { |
| 755 |
$this->debug = false; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
protected function decodeFolderName($name): array|bool|string|null { |
| 767 |
preg_match('#\{(.*)}(.*)#', $name, $preg); |
| 768 |
return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP"); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
public function getProtocol(): string { |
| 775 |
return $this->protocol; |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
public function getQuota($username): Response { |
| 785 |
return $this->response("imap_get_quota")->wrap(function($response) use ($username) { |
| 786 |
$result = \imap_get_quota($this->stream, 'user.' . $username); |
| 787 |
return $result ?: []; |
| 788 |
}); |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
public function getQuotaRoot(string $quota_root = 'INBOX'): Response { |
| 798 |
return $this->response("imap_get_quotaroot")->wrap(function($response) use ($quota_root) { |
| 799 |
$result = \imap_get_quotaroot($this->stream, $this->getAddress() . $quota_root); |
| 800 |
return $result ?: []; |
| 801 |
}); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
public function setProtocol(string $protocol): LegacyProtocol { |
| 809 |
if (($pos = strpos($protocol, "legacy")) > 0) { |
| 810 |
$protocol = substr($protocol, 0, ($pos + 2) * -1); |
| 811 |
} |
| 812 |
$this->protocol = $protocol; |
| 813 |
return $this; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
protected function response(?string $command = ""): Response { |
| 823 |
return Response::make(0, $command == "" ? [] : [$command], [], $this->debug); |
| 824 |
} |
| 825 |
} |