| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP; |
| 14 |
|
| 15 |
use Carbon\Carbon; |
| 16 |
use Webklex\PHPIMAP\Connection\Protocols\Response; |
| 17 |
use Webklex\PHPIMAP\Exceptions\AuthFailedException; |
| 18 |
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException; |
| 19 |
use Webklex\PHPIMAP\Exceptions\EventNotFoundException; |
| 20 |
use Webklex\PHPIMAP\Exceptions\FolderFetchingException; |
| 21 |
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException; |
| 22 |
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException; |
| 23 |
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; |
| 24 |
use Webklex\PHPIMAP\Exceptions\MessageNotFoundException; |
| 25 |
use Webklex\PHPIMAP\Exceptions\NotSupportedCapabilityException; |
| 26 |
use Webklex\PHPIMAP\Exceptions\ResponseException; |
| 27 |
use Webklex\PHPIMAP\Exceptions\RuntimeException; |
| 28 |
use Webklex\PHPIMAP\Query\WhereQuery; |
| 29 |
use Webklex\PHPIMAP\Support\FolderCollection; |
| 30 |
use Webklex\PHPIMAP\Traits\HasEvents; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class Folder { |
| 38 |
use HasEvents; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected Client $client; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public string $path; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public string $name; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public string $full_name; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public FolderCollection $children; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public string $delimiter; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public bool $no_inferiors; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public bool $no_select; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public bool $marked; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public bool $has_children; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public bool $referral; |
| 120 |
|
| 121 |
|
| 122 |
public array $status; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function __construct(Client $client, string $folder_name, string $delimiter, array $attributes) { |
| 132 |
$this->client = $client; |
| 133 |
|
| 134 |
$this->events["message"] = $client->getDefaultEvents("message"); |
| 135 |
$this->events["folder"] = $client->getDefaultEvents("folder"); |
| 136 |
|
| 137 |
$this->setDelimiter($delimiter); |
| 138 |
$this->path = $folder_name; |
| 139 |
$this->full_name = $this->decodeName($folder_name); |
| 140 |
$this->name = $this->getSimpleName($this->delimiter, $this->full_name); |
| 141 |
$this->children = new FolderCollection(); |
| 142 |
$this->has_children = false; |
| 143 |
|
| 144 |
$this->parseAttributes($attributes); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function query(array $extensions = []): WhereQuery { |
| 160 |
$this->getClient()->checkConnection(); |
| 161 |
$this->getClient()->openFolder($this->path); |
| 162 |
$extensions = count($extensions) > 0 ? $extensions : $this->getClient()->extensions; |
| 163 |
|
| 164 |
return new WhereQuery($this->getClient(), $extensions); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function search(array $extensions = []): WhereQuery { |
| 180 |
return $this->query($extensions); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function messages(array $extensions = []): WhereQuery { |
| 196 |
return $this->query($extensions); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function hasChildren(): bool { |
| 205 |
return $this->has_children; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function setChildren(FolderCollection $children): Folder { |
| 215 |
$this->children = $children; |
| 216 |
|
| 217 |
return $this; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function getChildren(): FolderCollection { |
| 226 |
return $this->children; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
protected function decodeName($name): string|array|bool|null { |
| 237 |
$parts = []; |
| 238 |
foreach (explode($this->delimiter, $name) as $item) { |
| 239 |
$parts[] = EncodingAliases::convert($item, "UTF7-IMAP"); |
| 240 |
} |
| 241 |
|
| 242 |
return implode($this->delimiter, $parts); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
protected function getSimpleName($delimiter, $full_name): string|bool { |
| 253 |
$arr = explode($delimiter, $full_name); |
| 254 |
return end($arr); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
protected function parseAttributes($attributes): void { |
| 262 |
$this->no_inferiors = in_array('\NoInferiors', $attributes, true) || \in_array('\Noinferiors', $attributes, true); |
| 263 |
$this->no_select = in_array('\NoSelect', $attributes, true) || \in_array('\Noselect', $attributes, true); |
| 264 |
$this->marked = in_array('\Marked', $attributes); |
| 265 |
$this->referral = in_array('\Referral', $attributes); |
| 266 |
$this->has_children = in_array('\HasChildren', $attributes); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function move(string $new_name, bool $expunge = true): array { |
| 285 |
$this->client->checkConnection(); |
| 286 |
$status = $this->client->getConnection()->renameFolder($this->full_name, $new_name)->validatedData(); |
| 287 |
if ($expunge) $this->client->expunge(); |
| 288 |
|
| 289 |
$folder = $this->client->getFolder($new_name); |
| 290 |
$this->dispatch("folder", "moved", $this, $folder); |
| 291 |
|
| 292 |
return $status; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function overview(?string $sequence = null): array { |
| 310 |
$this->client->openFolder($this->path); |
| 311 |
$sequence = $sequence === null ? "1:*" : $sequence; |
| 312 |
$uid = $this->client->getConfig()->get('options.sequence', IMAP::ST_MSGN); |
| 313 |
$response = $this->client->getConnection()->overview($sequence, $uid); |
| 314 |
return $response->validatedData(); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public function appendMessage(string $message, ?array $options = null, Carbon|string|null $internal_date = null): array { |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
if ($internal_date instanceof Carbon) { |
| 339 |
$internal_date = $internal_date->format('d-M-Y H:i:s O'); |
| 340 |
} |
| 341 |
|
| 342 |
return $this->client->getConnection()->appendMessage($this->path, $message, $options, $internal_date)->validatedData(); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public function rename(string $new_name, bool $expunge = true): array { |
| 361 |
return $this->move($new_name, $expunge); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function delete(bool $expunge = true): array { |
| 378 |
$status = $this->client->getConnection()->deleteFolder($this->path)->validatedData(); |
| 379 |
if ($this->client->getActiveFolder() == $this->path){ |
| 380 |
$this->client->setActiveFolder(); |
| 381 |
} |
| 382 |
|
| 383 |
if ($expunge) $this->client->expunge(); |
| 384 |
|
| 385 |
$this->dispatch("folder", "deleted", $this); |
| 386 |
|
| 387 |
return $status; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function subscribe(): array { |
| 402 |
$this->client->openFolder($this->path); |
| 403 |
return $this->client->getConnection()->subscribeFolder($this->path)->validatedData(); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public function unsubscribe(): array { |
| 418 |
$this->client->openFolder($this->path); |
| 419 |
return $this->client->getConnection()->unsubscribeFolder($this->path)->validatedData(); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
l |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public function idle(callable $callback, int $timeout = 300): void { |
| 436 |
$this->client->setTimeout($timeout); |
| 437 |
|
| 438 |
if (!in_array("IDLE", $this->client->getConnection()->getCapabilities()->validatedData())) { |
| 439 |
throw new Exceptions\NotSupportedCapabilityException("IMAP server does not support IDLE"); |
| 440 |
} |
| 441 |
|
| 442 |
$idle_client = $this->client->clone(); |
| 443 |
$idle_client->connect(); |
| 444 |
$idle_client->openFolder($this->path, true); |
| 445 |
$idle_client->getConnection()->idle(); |
| 446 |
|
| 447 |
$last_action = Carbon::now()->addSeconds($timeout); |
| 448 |
|
| 449 |
$sequence = $this->client->getConfig()->get('options.sequence', IMAP::ST_MSGN); |
| 450 |
|
| 451 |
while (true) { |
| 452 |
try { |
| 453 |
|
| 454 |
$line = $idle_client->getConnection()->nextLine(Response::empty()); |
| 455 |
} catch (Exceptions\RuntimeException $e) { |
| 456 |
if(strpos($e->getMessage(), "empty response") >= 0 && $idle_client->getConnection()->connected()) { |
| 457 |
continue; |
| 458 |
} |
| 459 |
if(!str_contains($e->getMessage(), "connection closed")) { |
| 460 |
throw $e; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
if (($pos = strpos($line, "EXISTS")) !== false) { |
| 465 |
$msgn = (int)substr($line, 2, $pos - 2); |
| 466 |
|
| 467 |
|
| 468 |
if (!$this->client->isConnected() || $last_action->isBefore(Carbon::now())) { |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
$this->client->getConnection()->reset(); |
| 483 |
|
| 484 |
$this->client->connect(); |
| 485 |
} |
| 486 |
$last_action = Carbon::now()->addSeconds($timeout); |
| 487 |
|
| 488 |
|
| 489 |
$this->client->openFolder($this->path, true); |
| 490 |
|
| 491 |
$message = $this->query()->getMessageByMsgn($msgn); |
| 492 |
$message->setSequence($sequence); |
| 493 |
$callback($message); |
| 494 |
|
| 495 |
$this->dispatch("message", "new", $message); |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public function status(): array { |
| 512 |
return $this->client->getConnection()->folderStatus($this->path)->validatedData(); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
public function getStatus(): array { |
| 529 |
return $this->status(); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public function loadStatus(): Folder { |
| 543 |
$this->status = $this->examine(); |
| 544 |
return $this; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public function examine(): array { |
| 559 |
return $this->client->getConnection()->examineFolder($this->path)->validatedData(); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
public function select(): array { |
| 574 |
return $this->client->getConnection()->selectFolder($this->path)->validatedData(); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
public function getClient(): Client { |
| 583 |
return $this->client; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public function setDelimiter($delimiter): void { |
| 591 |
if (in_array($delimiter, [null, '', ' ', false]) === true) { |
| 592 |
$delimiter = $this->client->getConfig()->get('options.delimiter', '/'); |
| 593 |
} |
| 594 |
|
| 595 |
$this->delimiter = $delimiter; |
| 596 |
} |
| 597 |
} |