| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Connection\Protocols; |
| 14 |
|
| 15 |
use Exception; |
| 16 |
use Throwable; |
| 17 |
use Webklex\PHPIMAP\Config; |
| 18 |
use Webklex\PHPIMAP\Exceptions\AuthFailedException; |
| 19 |
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException; |
| 20 |
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException; |
| 21 |
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException; |
| 22 |
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; |
| 23 |
use Webklex\PHPIMAP\Exceptions\MessageNotFoundException; |
| 24 |
use Webklex\PHPIMAP\Exceptions\ResponseException; |
| 25 |
use Webklex\PHPIMAP\Exceptions\RuntimeException; |
| 26 |
use Webklex\PHPIMAP\Header; |
| 27 |
use Webklex\PHPIMAP\IMAP; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ImapProtocol extends Protocol { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected int $noun = 0; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct(Config $config, bool $cert_validation = true, mixed $encryption = false) { |
| 51 |
$this->config = $config; |
| 52 |
$this->setCertValidation($cert_validation); |
| 53 |
$this->encryption = $encryption; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function __destruct() { |
| 60 |
$this->logout(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function connect(string $host, ?int $port = null): bool { |
| 71 |
$transport = 'tcp'; |
| 72 |
$encryption = ''; |
| 73 |
|
| 74 |
if ($this->encryption) { |
| 75 |
$encryption = strtolower($this->encryption); |
| 76 |
if (in_array($encryption, ['ssl', 'tls'])) { |
| 77 |
$transport = $encryption; |
| 78 |
$port = $port === null ? 993 : $port; |
| 79 |
} |
| 80 |
} |
| 81 |
$port = $port === null ? 143 : $port; |
| 82 |
try { |
| 83 |
$response = new Response(0, $this->debug); |
| 84 |
$this->stream = $this->createStream($transport, $host, $port, $this->connection_timeout); |
| 85 |
if (!$this->stream || !$this->assumedNextLine($response, '* OK')) { |
| 86 |
throw new ConnectionFailedException('connection refused'); |
| 87 |
} |
| 88 |
if ($encryption == 'starttls') { |
| 89 |
$this->enableStartTls(); |
| 90 |
} |
| 91 |
} catch (Exception $e) { |
| 92 |
throw new ConnectionFailedException('connection failed', 0, $e); |
| 93 |
} |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function connected(): bool { |
| 104 |
if ((bool)$this->stream) { |
| 105 |
try { |
| 106 |
$this->requestAndResponse('NOOP'); |
| 107 |
return true; |
| 108 |
} catch (ImapServerErrorException|RuntimeException) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function enableStartTls(): void { |
| 124 |
$response = $this->requestAndResponse('STARTTLS'); |
| 125 |
$result = $response->successful() && stream_socket_enable_crypto($this->stream, true, $this->getCryptoMethod()); |
| 126 |
if (!$result) { |
| 127 |
throw new ConnectionFailedException('failed to enable TLS'); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function nextLine(Response $response): string { |
| 138 |
$line = ""; |
| 139 |
while (($next_char = fread($this->stream, 1)) !== false && !in_array($next_char, ["", "\n"])) { |
| 140 |
$line .= $next_char; |
| 141 |
} |
| 142 |
if ($line === "" && ($next_char === false || $next_char === "")) { |
| 143 |
throw new RuntimeException('empty response'); |
| 144 |
} |
| 145 |
$line .= "\n"; |
| 146 |
$response->addResponse($line); |
| 147 |
if ($this->debug) echo "<< " . $line; |
| 148 |
return $line; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected function assumedNextLine(Response $response, string $start): bool { |
| 160 |
return str_starts_with($this->nextLine($response), $start); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
protected function assumedNextLineIgnoreUntagged(Response $response, string $start): bool { |
| 175 |
do { |
| 176 |
$line = $this->nextLine($response); |
| 177 |
} while (!(str_starts_with($start, '*')) && $this->isUntaggedLine($line)); |
| 178 |
|
| 179 |
return str_starts_with($line, $start); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected function nextTaggedLine(Response $response, ?string &$tag): string { |
| 190 |
$line = $this->nextLine($response); |
| 191 |
if (str_contains($line, ' ')) { |
| 192 |
list($tag, $line) = explode(' ', $line, 2); |
| 193 |
} |
| 194 |
|
| 195 |
return $line ?? ''; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
protected function nextTaggedLineIgnoreUntagged(Response $response, &$tag): string { |
| 208 |
do { |
| 209 |
$line = $this->nextLine($response); |
| 210 |
} while ($this->isUntaggedLine($line)); |
| 211 |
|
| 212 |
list($tag, $line) = explode(' ', $line, 2); |
| 213 |
|
| 214 |
return $line; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
protected function assumedNextTaggedLine(Response $response, string $start, &$tag): bool { |
| 227 |
return str_contains($this->nextTaggedLine($response, $tag), $start); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
protected function assumedNextTaggedLineIgnoreUntagged(Response $response, string $start, &$tag): bool { |
| 239 |
$line = $this->nextTaggedLineIgnoreUntagged($response, $tag); |
| 240 |
return strpos($line, $start) !== false; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
protected function isUntaggedLine(string $line) : bool { |
| 253 |
return str_starts_with($line, '* '); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected function decodeLine(Response $response, string $line): array { |
| 265 |
$tokens = []; |
| 266 |
$stack = []; |
| 267 |
|
| 268 |
|
| 269 |
$line = rtrim($line) . ' '; |
| 270 |
while (($pos = strpos($line, ' ')) !== false) { |
| 271 |
$token = substr($line, 0, $pos); |
| 272 |
if (!strlen($token)) { |
| 273 |
$line = substr($line, $pos + 1); |
| 274 |
continue; |
| 275 |
} |
| 276 |
while ($token[0] == '(') { |
| 277 |
$stack[] = $tokens; |
| 278 |
$tokens = []; |
| 279 |
$token = substr($token, 1); |
| 280 |
} |
| 281 |
if ($token[0] == '"') { |
| 282 |
if (preg_match('%^\(*\"((.|\\\|\")*?)\"( |$)%', $line, $matches)) { |
| 283 |
$tokens[] = $matches[1]; |
| 284 |
$line = substr($line, strlen($matches[0])); |
| 285 |
continue; |
| 286 |
} |
| 287 |
} |
| 288 |
if ($token[0] == '{') { |
| 289 |
$endPos = strpos($token, '}'); |
| 290 |
$chars = substr($token, 1, $endPos - 1); |
| 291 |
if (is_numeric($chars)) { |
| 292 |
$token = ''; |
| 293 |
while (strlen($token) < $chars) { |
| 294 |
$token .= $this->nextLine($response); |
| 295 |
} |
| 296 |
$line = ''; |
| 297 |
if (strlen($token) > $chars) { |
| 298 |
$line = substr($token, $chars); |
| 299 |
$token = substr($token, 0, $chars); |
| 300 |
} else { |
| 301 |
$line .= $this->nextLine($response); |
| 302 |
} |
| 303 |
$tokens[] = $token; |
| 304 |
$line = trim($line) . ' '; |
| 305 |
continue; |
| 306 |
} |
| 307 |
} |
| 308 |
if ($stack && $token[strlen($token) - 1] == ')') { |
| 309 |
|
| 310 |
$braces = strlen($token); |
| 311 |
$token = rtrim($token, ')'); |
| 312 |
|
| 313 |
$braces -= strlen($token) + 1; |
| 314 |
|
| 315 |
if (rtrim($token) != '') { |
| 316 |
$tokens[] = rtrim($token); |
| 317 |
} |
| 318 |
$token = $tokens; |
| 319 |
$tokens = array_pop($stack); |
| 320 |
|
| 321 |
while ($braces-- > 0) { |
| 322 |
$tokens[] = $token; |
| 323 |
$token = $tokens; |
| 324 |
$tokens = array_pop($stack); |
| 325 |
} |
| 326 |
} |
| 327 |
$tokens[] = $token; |
| 328 |
$line = substr($line, $pos + 1); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
while ($stack) { |
| 333 |
$child = $tokens; |
| 334 |
$tokens = array_pop($stack); |
| 335 |
$tokens[] = $child; |
| 336 |
} |
| 337 |
|
| 338 |
return $tokens; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function readLine(Response $response, array|string &$tokens = [], string $wantedTag = '*', bool $dontParse = false): bool { |
| 352 |
$line = $this->nextTaggedLine($response, $tag); |
| 353 |
if (!$dontParse) { |
| 354 |
$tokens = $this->decodeLine($response, $line); |
| 355 |
} else { |
| 356 |
$tokens = $line; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
return $tag == $wantedTag; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function readResponse(Response $response, string $tag, bool $dontParse = false): array { |
| 376 |
$lines = []; |
| 377 |
$tokens = ""; |
| 378 |
do { |
| 379 |
$readAll = $this->readLine($response, $tokens, $tag, $dontParse); |
| 380 |
$lines[] = $tokens; |
| 381 |
} while (!$readAll); |
| 382 |
|
| 383 |
$original = $tokens; |
| 384 |
if ($dontParse) { |
| 385 |
|
| 386 |
$tokens = [trim(substr($tokens, 0, 3))]; |
| 387 |
} |
| 388 |
|
| 389 |
$original = is_array($original) ? $original : [$original]; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
if ($tokens[0] == 'OK') { |
| 394 |
return $lines ?: [true]; |
| 395 |
} elseif ($tokens[0] == 'NO' || $tokens[0] == 'BAD' || $tokens[0] == 'BYE') { |
| 396 |
throw new ImapServerErrorException($this->stringifyArray($original)); |
| 397 |
} |
| 398 |
|
| 399 |
throw new ImapBadRequestException($this->stringifyArray($original)); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
private function stringifyArray(array $arr): string { |
| 409 |
$string = ""; |
| 410 |
foreach ($arr as $value) { |
| 411 |
if (is_array($value)) { |
| 412 |
$string .= "(" . $this->stringifyArray($value) . ")"; |
| 413 |
} else { |
| 414 |
$string .= $value . " "; |
| 415 |
} |
| 416 |
} |
| 417 |
return $string; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public function sendRequest(string $command, array $tokens = [], ?string &$tag = null): Response { |
| 430 |
if (!$tag) { |
| 431 |
$this->noun++; |
| 432 |
$tag = 'TAG' . $this->noun; |
| 433 |
} |
| 434 |
|
| 435 |
$line = $tag . ' ' . $command; |
| 436 |
|
| 437 |
$response = new Response($this->noun, $this->debug); |
| 438 |
|
| 439 |
foreach ($tokens as $token) { |
| 440 |
if (is_array($token)) { |
| 441 |
$this->write($response, $line . ' ' . $token[0]); |
| 442 |
if (!$this->assumedNextLine($response, '+ ')) { |
| 443 |
throw new RuntimeException('failed to send literal string'); |
| 444 |
} |
| 445 |
$line = $token[1]; |
| 446 |
} else { |
| 447 |
$line .= ' ' . $token; |
| 448 |
} |
| 449 |
} |
| 450 |
$this->write($response, $line); |
| 451 |
|
| 452 |
return $response; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public function write(Response $response, string $data): void { |
| 464 |
$command = $data . "\r\n"; |
| 465 |
if ($this->debug) echo ">> " . $command . "\n"; |
| 466 |
|
| 467 |
$response->addCommand($command); |
| 468 |
|
| 469 |
if (fwrite($this->stream, $command) === false) { |
| 470 |
throw new RuntimeException('failed to write - connection closed?'); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function requestAndResponse(string $command, array $tokens = [], bool $dontParse = false): Response { |
| 487 |
$response = $this->sendRequest($command, $tokens, $tag); |
| 488 |
$response->setResult($this->readResponse($response, $tag, $dontParse)); |
| 489 |
|
| 490 |
return $response; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public function escapeString(array|string $string): array|string { |
| 501 |
if (func_num_args() < 2) { |
| 502 |
if (str_contains($string, "\n")) { |
| 503 |
return ['{' . strlen($string) . '}', $string]; |
| 504 |
} else { |
| 505 |
return '"' . str_replace(['\\', '"'], ['\\\\', '\\"'], $string) . '"'; |
| 506 |
} |
| 507 |
} |
| 508 |
$result = []; |
| 509 |
foreach (func_get_args() as $string) { |
| 510 |
$result[] = $this->escapeString($string); |
| 511 |
} |
| 512 |
return $result; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public function escapeList(array $list): string { |
| 522 |
$result = []; |
| 523 |
foreach ($list as $v) { |
| 524 |
if (!is_array($v)) { |
| 525 |
$result[] = $v; |
| 526 |
continue; |
| 527 |
} |
| 528 |
$result[] = $this->escapeList($v); |
| 529 |
} |
| 530 |
return '(' . implode(' ', $result) . ')'; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public function login(string $user, string $password): Response { |
| 545 |
try { |
| 546 |
$command = 'LOGIN'; |
| 547 |
$params = $this->escapeString($user, $password); |
| 548 |
|
| 549 |
return $this->requestAndResponse($command, $params, true); |
| 550 |
} catch (RuntimeException $e) { |
| 551 |
throw new AuthFailedException("failed to authenticate", 0, $e); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public function authenticate(string $user, string $token): Response { |
| 564 |
try { |
| 565 |
$authenticateParams = ['XOAUTH2', base64_encode("user=$user\1auth=Bearer $token\1\1")]; |
| 566 |
$response = $this->sendRequest('AUTHENTICATE', $authenticateParams); |
| 567 |
|
| 568 |
while (true) { |
| 569 |
$tokens = ""; |
| 570 |
$is_plus = $this->readLine($response, $tokens, '+', true); |
| 571 |
if ($is_plus) { |
| 572 |
|
| 573 |
error_log("got an extra server challenge: $tokens"); |
| 574 |
|
| 575 |
$response->stack($this->sendRequest('')); |
| 576 |
} else { |
| 577 |
if (preg_match('/^NO /i', $tokens) || |
| 578 |
preg_match('/^BAD /i', $tokens)) { |
| 579 |
error_log("got failure response: $tokens"); |
| 580 |
return $response->addError("got failure response: $tokens"); |
| 581 |
} else if (preg_match("/^OK /i", $tokens)) { |
| 582 |
return $response->setResult(is_array($tokens) ? $tokens : [$tokens]); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
} catch (RuntimeException $e) { |
| 587 |
throw new AuthFailedException("failed to authenticate", 0, $e); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function logout(): Response { |
| 597 |
if (!$this->stream) { |
| 598 |
$this->reset(); |
| 599 |
return new Response(0, $this->debug); |
| 600 |
} elseif ($this->meta()["timed_out"]) { |
| 601 |
$this->reset(); |
| 602 |
return new Response(0, $this->debug); |
| 603 |
} |
| 604 |
|
| 605 |
$result = null; |
| 606 |
try { |
| 607 |
$result = $this->requestAndResponse('LOGOUT', [], true); |
| 608 |
fclose($this->stream); |
| 609 |
} catch (Throwable) { |
| 610 |
} |
| 611 |
|
| 612 |
$this->reset(); |
| 613 |
|
| 614 |
return $result ?? new Response(0, $this->debug); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public function reset(): void { |
| 623 |
$this->stream = null; |
| 624 |
$this->uid_cache = []; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
public function getCapabilities(): Response { |
| 638 |
$response = $this->requestAndResponse('CAPABILITY'); |
| 639 |
|
| 640 |
if (!$response->getResponse()) return $response; |
| 641 |
|
| 642 |
return $response->setResult($response->validatedData()[0]); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
public function examineOrSelect(string $command = 'EXAMINE', string $folder = 'INBOX'): Response { |
| 654 |
$response = $this->sendRequest($command, [$this->escapeString($folder)], $tag); |
| 655 |
|
| 656 |
$result = []; |
| 657 |
$tokens = []; |
| 658 |
while (!$this->readLine($response, $tokens, $tag)) { |
| 659 |
if ($tokens[0] == 'FLAGS') { |
| 660 |
array_shift($tokens); |
| 661 |
$result['flags'] = $tokens; |
| 662 |
continue; |
| 663 |
} |
| 664 |
switch ($tokens[1]) { |
| 665 |
case 'EXISTS': |
| 666 |
case 'RECENT': |
| 667 |
$result[strtolower($tokens[1])] = (int)$tokens[0]; |
| 668 |
break; |
| 669 |
case '[UIDVALIDITY': |
| 670 |
$result['uidvalidity'] = (int)$tokens[2]; |
| 671 |
break; |
| 672 |
case '[UIDNEXT': |
| 673 |
$result['uidnext'] = (int)$tokens[2]; |
| 674 |
break; |
| 675 |
case '[UNSEEN': |
| 676 |
$result['unseen'] = (int)$tokens[2]; |
| 677 |
break; |
| 678 |
case '[NONEXISTENT]': |
| 679 |
throw new RuntimeException("folder doesn't exist"); |
| 680 |
default: |
| 681 |
|
| 682 |
break; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
$response->setResult($result); |
| 687 |
|
| 688 |
if ($tokens[0] != 'OK') { |
| 689 |
$response->addError("request failed"); |
| 690 |
} |
| 691 |
return $response; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
public function selectFolder(string $folder = 'INBOX'): Response { |
| 702 |
$this->uid_cache = []; |
| 703 |
|
| 704 |
return $this->examineOrSelect('SELECT', $folder); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
public function examineFolder(string $folder = 'INBOX'): Response { |
| 715 |
return $this->examineOrSelect('EXAMINE', $folder); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response { |
| 731 |
$response = $this->requestAndResponse('STATUS', [$this->escapeString($folder), $this->escapeList($arguments)]); |
| 732 |
$data = $response->validatedData(); |
| 733 |
|
| 734 |
if (!isset($data[0]) || !isset($data[0][2])) { |
| 735 |
throw new RuntimeException("folder status could not be fetched"); |
| 736 |
} |
| 737 |
|
| 738 |
$result = []; |
| 739 |
$key = null; |
| 740 |
foreach ($data[0][2] as $value) { |
| 741 |
if ($key === null) { |
| 742 |
$key = $value; |
| 743 |
} else { |
| 744 |
$result[strtolower($key)] = (int)$value; |
| 745 |
$key = null; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
$response->setResult($result); |
| 750 |
|
| 751 |
return $response; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
public function fetch(array|string $items, array|int $from, mixed $to = null, int|string $uid = IMAP::ST_UID): Response { |
| 770 |
if (is_array($from) && count($from) > 1) { |
| 771 |
$set = implode(',', $from); |
| 772 |
} elseif (is_array($from) && count($from) === 1) { |
| 773 |
$from = array_values($from); |
| 774 |
$set = $from[0] . ':' . $from[0]; |
| 775 |
} elseif ($to === null) { |
| 776 |
$set = $from . ':' . $from; |
| 777 |
} elseif ($to == INF) { |
| 778 |
$set = $from . ':*'; |
| 779 |
} else { |
| 780 |
$set = $from . ':' . (int)$to; |
| 781 |
} |
| 782 |
|
| 783 |
$items = (array)$items; |
| 784 |
$itemList = $this->escapeList($items); |
| 785 |
|
| 786 |
$response = $this->sendRequest($this->buildUIDCommand("FETCH", $uid), [$set, $itemList], $tag); |
| 787 |
$result = []; |
| 788 |
$tokens = []; |
| 789 |
while (!$this->readLine($response, $tokens, $tag)) { |
| 790 |
|
| 791 |
if ($tokens[1] != 'FETCH') { |
| 792 |
continue; |
| 793 |
} |
| 794 |
|
| 795 |
$uidKey = 0; |
| 796 |
$data = []; |
| 797 |
|
| 798 |
|
| 799 |
if ($uid === IMAP::ST_UID) { |
| 800 |
$count = count($tokens[2]); |
| 801 |
if ($tokens[2][$count - 2] == 'UID') { |
| 802 |
$uidKey = $count - 1; |
| 803 |
} else if ($tokens[2][0] == 'UID') { |
| 804 |
$uidKey = 1; |
| 805 |
} else { |
| 806 |
$found = array_search('UID', $tokens[2]); |
| 807 |
if ($found === false || $found === -1) { |
| 808 |
continue; |
| 809 |
} |
| 810 |
|
| 811 |
$uidKey = $found + 1; |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
if ($to === null && !is_array($from) && ($uid === IMAP::ST_UID ? $tokens[2][$uidKey] != $from : $tokens[0] != $from)) { |
| 817 |
continue; |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
if (count($items) == 1) { |
| 822 |
if ($tokens[2][0] == $items[0]) { |
| 823 |
$data = $tokens[2][1]; |
| 824 |
} elseif ($uid === IMAP::ST_UID && $tokens[2][2] == $items[0]) { |
| 825 |
$data = $tokens[2][3]; |
| 826 |
} else { |
| 827 |
$expectedResponse = 0; |
| 828 |
|
| 829 |
$count = count($tokens[2]); |
| 830 |
|
| 831 |
for ($i = 2; $i < $count; $i += 2) { |
| 832 |
if ($tokens[2][$i] != $items[0]) { |
| 833 |
continue; |
| 834 |
} |
| 835 |
$data = $tokens[2][$i + 1]; |
| 836 |
$expectedResponse = 1; |
| 837 |
break; |
| 838 |
} |
| 839 |
if (!$expectedResponse) { |
| 840 |
continue; |
| 841 |
} |
| 842 |
} |
| 843 |
} else { |
| 844 |
while (key($tokens[2]) !== null) { |
| 845 |
$data[current($tokens[2])] = next($tokens[2]); |
| 846 |
next($tokens[2]); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
if ($to === null && !is_array($from) && ($uid === IMAP::ST_UID ? $tokens[2][$uidKey] == $from : $tokens[0] == $from)) { |
| 852 |
|
| 853 |
if (!$this->readLine($response, $tokens, $tag)) |
| 854 |
return $response->setResult($data); |
| 855 |
} |
| 856 |
if ($uid === IMAP::ST_UID) { |
| 857 |
$result[$tokens[2][$uidKey]] = $data; |
| 858 |
} else { |
| 859 |
$result[$tokens[0]] = $data; |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
if ($to === null && !is_array($from)) { |
| 864 |
throw new RuntimeException('the single id was not found in response'); |
| 865 |
} |
| 866 |
|
| 867 |
return $response->setResult($result); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
public function content(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response { |
| 881 |
$rfc = $rfc ?? "RFC822"; |
| 882 |
$item = $rfc === "BODY" ? "BODY[TEXT]" : "$rfc.TEXT"; |
| 883 |
return $this->fetch([$item], is_array($uids) ? $uids : [$uids], null, $uid); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
public function headers(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response { |
| 897 |
return $this->fetch(["$rfc.HEADER"], is_array($uids) ? $uids : [$uids], null, $uid); |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
public function flags(int|array $uids, int|string $uid = IMAP::ST_UID): Response { |
| 910 |
return $this->fetch(["FLAGS"], is_array($uids) ? $uids : [$uids], null, $uid); |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public function sizes(int|array $uids, int|string $uid = IMAP::ST_UID): Response { |
| 923 |
return $this->fetch(["RFC822.SIZE"], is_array($uids) ? $uids : [$uids], null, $uid); |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
public function getUid(?int $id = null): Response { |
| 934 |
if (!$this->enable_uid_cache || empty($this->uid_cache) || count($this->uid_cache) <= 0) { |
| 935 |
try { |
| 936 |
$this->setUidCache((array)$this->fetch('UID', 1, INF)->data()); |
| 937 |
} catch (RuntimeException) { |
| 938 |
} |
| 939 |
} |
| 940 |
$uids = $this->uid_cache; |
| 941 |
|
| 942 |
if ($id == null) { |
| 943 |
return Response::empty($this->debug)->setResult($uids)->setCanBeEmpty(true); |
| 944 |
} |
| 945 |
|
| 946 |
foreach ($uids as $k => $v) { |
| 947 |
if ($k == $id) { |
| 948 |
return Response::empty($this->debug)->setResult($v); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
if ($this->enable_uid_cache && $this->uid_cache) { |
| 954 |
$this->setUidCache(null); |
| 955 |
return $this->getUid($id); |
| 956 |
} |
| 957 |
|
| 958 |
throw new MessageNotFoundException('unique id not found'); |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
public function getMessageNumber(string $id): Response { |
| 969 |
foreach ($this->getUid()->data() as $k => $v) { |
| 970 |
if ($v == $id) { |
| 971 |
return Response::empty($this->debug)->setResult((int)$k); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
throw new MessageNotFoundException('message number not found: ' . $id); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
public function folders(string $reference = '', string $folder = '*'): Response { |
| 991 |
$response = $this->requestAndResponse('LIST', $this->escapeString($reference, $folder))->setCanBeEmpty(true); |
| 992 |
$list = $response->data(); |
| 993 |
|
| 994 |
$result = []; |
| 995 |
if ($list[0] !== true) { |
| 996 |
foreach ($list as $item) { |
| 997 |
if (count($item) != 4 || $item[0] != 'LIST') { |
| 998 |
continue; |
| 999 |
} |
| 1000 |
$item[3] = str_replace("\\\\", "\\", str_replace("\\\"", "\"", $item[3])); |
| 1001 |
$result[$item[3]] = ['delimiter' => $item[2], 'flags' => $item[1]]; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
return $response->setResult($result); |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
public function store( |
| 1027 |
array|string $flags, int $from, ?int $to = null, ?string $mode = null, bool $silent = true, int|string $uid = IMAP::ST_UID, ?string $item = null |
| 1028 |
): Response { |
| 1029 |
$flags = $this->escapeList(is_array($flags) ? $flags : [$flags]); |
| 1030 |
$set = $this->buildSet($from, $to); |
| 1031 |
|
| 1032 |
$command = $this->buildUIDCommand("STORE", $uid); |
| 1033 |
$item = ($mode == '-' ? "-" : "+") . ($item === null ? "FLAGS" : $item) . ($silent ? '.SILENT' : ""); |
| 1034 |
|
| 1035 |
$response = $this->requestAndResponse($command, [$set, $item, $flags], $silent); |
| 1036 |
|
| 1037 |
if ($silent) { |
| 1038 |
return $response; |
| 1039 |
} |
| 1040 |
|
| 1041 |
$result = []; |
| 1042 |
foreach ($response as $token) { |
| 1043 |
if ($token[1] != 'FETCH' || $token[2][0] != 'FLAGS') { |
| 1044 |
continue; |
| 1045 |
} |
| 1046 |
$result[$token[0]] = $token[2][1]; |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
return $response->setResult($result); |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
public function appendMessage(string $folder, string $message, ?array $flags = null, ?string $date = null): Response { |
| 1068 |
$tokens = []; |
| 1069 |
$tokens[] = $this->escapeString($folder); |
| 1070 |
if ($flags !== null) { |
| 1071 |
$tokens[] = $this->escapeList($flags); |
| 1072 |
} |
| 1073 |
if ($date !== null) { |
| 1074 |
$tokens[] = $this->escapeString($date); |
| 1075 |
} |
| 1076 |
$tokens[] = $this->escapeString($message); |
| 1077 |
|
| 1078 |
return $this->requestAndResponse('APPEND', $tokens, true); |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
public function copyMessage(string $folder, $from, ?int $to = null, int|string $uid = IMAP::ST_UID): Response { |
| 1098 |
$set = $this->buildSet($from, $to); |
| 1099 |
$command = $this->buildUIDCommand("COPY", $uid); |
| 1100 |
|
| 1101 |
return $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true); |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
public function copyManyMessages(array $messages, string $folder, int|string $uid = IMAP::ST_UID): Response { |
| 1119 |
$command = $this->buildUIDCommand("COPY", $uid); |
| 1120 |
|
| 1121 |
$set = implode(',', $messages); |
| 1122 |
$tokens = [$set, $this->escapeString($folder)]; |
| 1123 |
|
| 1124 |
return $this->requestAndResponse($command, $tokens, true); |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
public function moveMessage(string $folder, $from, ?int $to = null, int|string $uid = IMAP::ST_UID): Response { |
| 1144 |
$set = $this->buildSet($from, $to); |
| 1145 |
$command = $this->buildUIDCommand("MOVE", $uid); |
| 1146 |
|
| 1147 |
$result = $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true); |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
if (!$result->boolean()) { |
| 1155 |
$result = $this->copyMessage($folder, $from, $to, $uid); |
| 1156 |
if (!$result->boolean()) { |
| 1157 |
return $result; |
| 1158 |
} |
| 1159 |
$result = $this->store(['\Deleted'], $from, $to, null, true, $uid); |
| 1160 |
if (!$result->boolean()) { |
| 1161 |
return $result; |
| 1162 |
} |
| 1163 |
return $this->expunge(); |
| 1164 |
} |
| 1165 |
return $result; |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
public function moveManyMessages(array $messages, string $folder, int|string $uid = IMAP::ST_UID): Response { |
| 1183 |
$command = $this->buildUIDCommand("MOVE", $uid); |
| 1184 |
$set = implode(',', $messages); |
| 1185 |
$tokens = [$set, $this->escapeString($folder)]; |
| 1186 |
|
| 1187 |
$result = $this->requestAndResponse($command, $tokens, true); |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
if (!$result->boolean()) { |
| 1195 |
$result = $this->copyManyMessages($messages, $folder, $uid); |
| 1196 |
if (!$result->boolean()) { |
| 1197 |
return $result; |
| 1198 |
} |
| 1199 |
foreach ($messages as $message) { |
| 1200 |
$result = $this->store(['\Deleted'], $message, $message, null, true, $uid); |
| 1201 |
if (!$result->boolean()) { |
| 1202 |
return $result; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
return $this->expunge(); |
| 1206 |
} |
| 1207 |
return $result; |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
public function ID($ids = null): Response { |
| 1222 |
$token = "NIL"; |
| 1223 |
if (is_array($ids) && !empty($ids)) { |
| 1224 |
$token = "("; |
| 1225 |
foreach ($ids as $id) { |
| 1226 |
$token .= '"' . $id . '" '; |
| 1227 |
} |
| 1228 |
$token = rtrim($token) . ")"; |
| 1229 |
} |
| 1230 |
|
| 1231 |
return $this->requestAndResponse("ID", [$token], true); |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
public function createFolder(string $folder): Response { |
| 1245 |
return $this->requestAndResponse('CREATE', [$this->escapeString($folder)], true); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
public function renameFolder(string $old, string $new): Response { |
| 1261 |
return $this->requestAndResponse('RENAME', $this->escapeString($old, $new), true); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
public function deleteFolder(string $folder): Response { |
| 1275 |
return $this->requestAndResponse('DELETE', [$this->escapeString($folder)], true); |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
public function subscribeFolder(string $folder): Response { |
| 1289 |
return $this->requestAndResponse('SUBSCRIBE', [$this->escapeString($folder)], true); |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
public function unsubscribeFolder(string $folder): Response { |
| 1303 |
return $this->requestAndResponse('UNSUBSCRIBE', [$this->escapeString($folder)], true); |
| 1304 |
} |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
public function expunge(): Response { |
| 1315 |
$this->uid_cache = []; |
| 1316 |
return $this->requestAndResponse('EXPUNGE'); |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
public function noop(): Response { |
| 1328 |
return $this->requestAndResponse('NOOP'); |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
public function getQuota($username): Response { |
| 1344 |
$command = "GETQUOTA"; |
| 1345 |
$params = ['"#user/' . $username . '"']; |
| 1346 |
|
| 1347 |
return $this->requestAndResponse($command, $params); |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
public function getQuotaRoot(string $quota_root = 'INBOX'): Response { |
| 1363 |
$command = "GETQUOTAROOT"; |
| 1364 |
$params = [$quota_root]; |
| 1365 |
|
| 1366 |
return $this->requestAndResponse($command, $params); |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
public function idle(): void { |
| 1375 |
$response = $this->sendRequest("IDLE"); |
| 1376 |
if (!$this->assumedNextLineIgnoreUntagged($response, '+ ')) { |
| 1377 |
throw new RuntimeException('idle failed'); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
public function done(): bool { |
| 1386 |
$response = new Response($this->noun, $this->debug); |
| 1387 |
$this->write($response, "DONE"); |
| 1388 |
if (!$this->assumedNextTaggedLineIgnoreUntagged($response, 'OK', $tags)) { |
| 1389 |
throw new RuntimeException('done failed'); |
| 1390 |
} |
| 1391 |
return true; |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
public function search(array $params, int|string $uid = IMAP::ST_UID): Response { |
| 1407 |
$command = $this->buildUIDCommand("SEARCH", $uid); |
| 1408 |
$response = $this->requestAndResponse($command, $params)->setCanBeEmpty(true); |
| 1409 |
|
| 1410 |
foreach ($response->data() as $ids) { |
| 1411 |
if ($ids[0] === 'SEARCH') { |
| 1412 |
array_shift($ids); |
| 1413 |
return $response->setResult($ids); |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
return $response; |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
public function overview(string $sequence, int|string $uid = IMAP::ST_UID): Response { |
| 1432 |
$result = []; |
| 1433 |
list($from, $to) = explode(":", $sequence); |
| 1434 |
|
| 1435 |
$response = $this->getUid(); |
| 1436 |
$ids = []; |
| 1437 |
foreach ($response->data() as $msgn => $v) { |
| 1438 |
$id = $uid === IMAP::ST_UID ? $v : $msgn; |
| 1439 |
if (($to >= $id && $from <= $id) || ($to === "*" && $from <= $id)) { |
| 1440 |
$ids[] = $id; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
if (!empty($ids)) { |
| 1444 |
$headers = $this->headers($ids, "RFC822", $uid); |
| 1445 |
$response->stack($headers); |
| 1446 |
foreach ($headers->data() as $id => $raw_header) { |
| 1447 |
$result[$id] = (new Header($raw_header, $this->config))->getAttributes(); |
| 1448 |
} |
| 1449 |
} |
| 1450 |
return $response->setResult($result)->setCanBeEmpty(true); |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
public function enableDebug(): void { |
| 1459 |
$this->debug = true; |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
public function disableDebug(): void { |
| 1468 |
$this->debug = false; |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
public function buildSet($from, $to = null): int|string { |
| 1479 |
$set = (int)$from; |
| 1480 |
if ($to !== null) { |
| 1481 |
$set .= ':' . ($to == INF ? '*' : (int)$to); |
| 1482 |
} |
| 1483 |
return $set; |
| 1484 |
} |
| 1485 |
} |