| Total Complexity | 180 |
| Total Lines | 1138 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Message often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Message, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 78 | class Message { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Client instance |
||
| 82 | * |
||
| 83 | * @var Client |
||
| 84 | */ |
||
| 85 | private $client = Client::class; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Default mask |
||
| 89 | * @var string $mask |
||
| 90 | */ |
||
| 91 | protected $mask = MessageMask::class; |
||
| 92 | |||
| 93 | /** @var array $config */ |
||
| 94 | protected $config = []; |
||
| 95 | |||
| 96 | /** @var array $attributes */ |
||
| 97 | protected $attributes = [ |
||
| 98 | 'message_id' => '', |
||
| 99 | 'message_no' => null, |
||
| 100 | 'subject' => '', |
||
| 101 | 'references' => null, |
||
| 102 | 'date' => null, |
||
| 103 | 'from' => [], |
||
| 104 | 'to' => [], |
||
| 105 | 'cc' => [], |
||
| 106 | 'bcc' => [], |
||
| 107 | 'reply_to' => [], |
||
| 108 | 'in_reply_to' => '', |
||
| 109 | 'sender' => [], |
||
| 110 | 'priority' => 0, |
||
| 111 | ]; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The message folder path |
||
| 115 | * |
||
| 116 | * @var string $folder_path |
||
| 117 | */ |
||
| 118 | protected $folder_path; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Fetch body options |
||
| 122 | * |
||
| 123 | * @var integer |
||
| 124 | */ |
||
| 125 | public $fetch_options = null; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Fetch body options |
||
| 129 | * |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | public $fetch_body = null; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Fetch attachments options |
||
| 136 | * |
||
| 137 | * @var bool |
||
| 138 | */ |
||
| 139 | public $fetch_attachment = null; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Fetch flags options |
||
| 143 | * |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | public $fetch_flags = null; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string $header |
||
| 150 | */ |
||
| 151 | public $header = null; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var null|object $header_info |
||
| 155 | */ |
||
| 156 | public $header_info = null; |
||
| 157 | |||
| 158 | /** @var null|string $raw_body */ |
||
| 159 | public $raw_body = null; |
||
| 160 | |||
| 161 | /** @var null $structure */ |
||
| 162 | protected $structure = null; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Message body components |
||
| 166 | * |
||
| 167 | * @var array $bodies |
||
| 168 | * @var AttachmentCollection|array $attachments |
||
| 169 | * @var FlagCollection|array $flags |
||
| 170 | */ |
||
| 171 | public $bodies = []; |
||
| 172 | public $attachments = []; |
||
| 173 | public $flags = []; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * A list of all available and supported flags |
||
| 177 | * |
||
| 178 | * @var array $available_flags |
||
| 179 | */ |
||
| 180 | private $available_flags = ['recent', 'flagged', 'answered', 'deleted', 'seen', 'draft']; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Message constructor. |
||
| 184 | * |
||
| 185 | * @param integer $uid |
||
| 186 | * @param integer|null $msglist |
||
| 187 | * @param Client $client |
||
| 188 | * @param integer|null $fetch_options |
||
| 189 | * @param boolean $fetch_body |
||
| 190 | * @param boolean $fetch_attachment |
||
| 191 | * @param boolean $fetch_flags |
||
| 192 | * |
||
| 193 | * @throws Exceptions\ConnectionFailedException |
||
| 194 | * @throws InvalidMessageDateException |
||
| 195 | */ |
||
| 196 | public function __construct($uid, $msglist, Client $client, $fetch_options = null, $fetch_body = false, $fetch_attachment = false, $fetch_flags = false) { |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Call dynamic attribute setter and getter methods |
||
| 234 | * @param string $method |
||
| 235 | * @param array $arguments |
||
| 236 | * |
||
| 237 | * @return mixed |
||
| 238 | * @throws MethodNotFoundException |
||
| 239 | */ |
||
| 240 | public function __call($method, $arguments) { |
||
| 241 | if(strtolower(substr($method, 0, 3)) === 'get') { |
||
| 242 | $name = snake_case(substr($method, 3)); |
||
| 243 | |||
| 244 | if(in_array($name, array_keys($this->attributes))) { |
||
| 245 | return $this->attributes[$name]; |
||
| 246 | } |
||
| 247 | |||
| 248 | }elseif (strtolower(substr($method, 0, 3)) === 'set') { |
||
| 249 | $name = snake_case(substr($method, 3)); |
||
| 250 | |||
| 251 | if(in_array($name, array_keys($this->attributes))) { |
||
| 252 | $this->attributes[$name] = array_pop($arguments); |
||
| 253 | |||
| 254 | return $this->attributes[$name]; |
||
| 255 | } |
||
| 256 | |||
| 257 | } |
||
| 258 | |||
| 259 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param $name |
||
| 264 | * @param $value |
||
| 265 | * |
||
| 266 | * @return mixed |
||
| 267 | */ |
||
| 268 | public function __set($name, $value) { |
||
| 269 | $this->attributes[$name] = $value; |
||
| 270 | |||
| 271 | return $this->attributes[$name]; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param $name |
||
| 276 | * |
||
| 277 | * @return mixed|null |
||
| 278 | */ |
||
| 279 | public function __get($name) { |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Copy the current Messages to a mailbox |
||
| 289 | * |
||
| 290 | * @param $mailbox |
||
| 291 | * @param int $options |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | * @throws Exceptions\ConnectionFailedException |
||
| 295 | */ |
||
| 296 | public function copy($mailbox, $options = 0) { |
||
| 297 | $this->client->openFolder($this->folder_path); |
||
| 298 | return imap_mail_copy($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Move the current Messages to a mailbox |
||
| 303 | * |
||
| 304 | * @param $mailbox |
||
| 305 | * @param int $options |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | * @throws Exceptions\ConnectionFailedException |
||
| 309 | */ |
||
| 310 | public function move($mailbox, $options = 0) { |
||
| 311 | $this->client->openFolder($this->folder_path); |
||
| 312 | return imap_mail_move($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check if the Message has a text body |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function hasTextBody() { |
||
| 321 | return isset($this->bodies['text']); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the Message text body |
||
| 326 | * |
||
| 327 | * @return mixed |
||
| 328 | */ |
||
| 329 | public function getTextBody() { |
||
| 330 | if (!isset($this->bodies['text'])) { |
||
| 331 | return false; |
||
| 332 | } |
||
| 333 | |||
| 334 | return $this->bodies['text']->content; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Check if the Message has a html body |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | public function hasHTMLBody() { |
||
| 343 | return isset($this->bodies['html']); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get the Message html body |
||
| 348 | * If $replaceImages is callable it should expect string $body as first parameter, $oAttachment as second and return |
||
| 349 | * the resulting $body. |
||
| 350 | * |
||
| 351 | * @var bool|callable $replaceImages |
||
| 352 | * |
||
| 353 | * @return string|null |
||
| 354 | * |
||
| 355 | * @deprecated 1.4.0:2.0.0 No longer needed. Use AttachmentMask::getImageSrc() instead |
||
| 356 | */ |
||
| 357 | public function getHTMLBody($replaceImages = false) { |
||
| 358 | if (!isset($this->bodies['html'])) { |
||
| 359 | return null; |
||
| 360 | } |
||
| 361 | |||
| 362 | $body = $this->bodies['html']->content; |
||
| 363 | if ($replaceImages !== false) { |
||
| 364 | $this->attachments->each(function($oAttachment) use(&$body, $replaceImages) { |
||
| 365 | /** @var Attachment $oAttachment */ |
||
| 366 | if(is_callable($replaceImages)) { |
||
| 367 | $body = $replaceImages($body, $oAttachment); |
||
| 368 | }elseif(is_string($replaceImages)) { |
||
| 369 | call_user_func($replaceImages, [$body, $oAttachment]); |
||
| 370 | }else{ |
||
| 371 | if ($oAttachment->id && $oAttachment->getImgSrc() != null) { |
||
| 372 | $body = str_replace('cid:'.$oAttachment->id, $oAttachment->getImgSrc(), $body); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | }); |
||
| 376 | } |
||
| 377 | |||
| 378 | return $body; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Parse all defined headers |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | * @throws Exceptions\ConnectionFailedException |
||
| 386 | * @throws InvalidMessageDateException |
||
| 387 | */ |
||
| 388 | private function parseHeader() { |
||
| 389 | $this->client->openFolder($this->folder_path); |
||
| 390 | $this->header = $header = imap_fetchheader($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
||
| 391 | |||
| 392 | $this->priority = $this->extractPriority($this->header); |
||
| 393 | |||
| 394 | if ($this->header) { |
||
| 395 | $header = imap_rfc822_parse_headers($this->header); |
||
| 396 | } |
||
| 397 | |||
| 398 | if (property_exists($header, 'subject')) { |
||
| 399 | if($this->config['decoder']['message']['subject'] === 'utf-8') { |
||
| 400 | $this->subject = imap_utf8($header->subject); |
||
| 401 | }else{ |
||
| 402 | $this->subject = mb_decode_mimeheader($header->subject); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $part){ |
||
| 407 | $this->extractHeaderAddressPart($header, $part); |
||
| 408 | } |
||
| 409 | |||
| 410 | if (property_exists($header, 'references')) { |
||
| 411 | $this->references = $header->references; |
||
| 412 | } |
||
| 413 | if (property_exists($header, 'in_reply_to')) { |
||
| 414 | $this->in_reply_to = str_replace(['<', '>'], '', $header->in_reply_to); |
||
| 415 | } |
||
| 416 | if (property_exists($header, 'message_id')) { |
||
| 417 | $this->message_id = str_replace(['<', '>'], '', $header->message_id); |
||
| 418 | } |
||
| 419 | if (property_exists($header, 'Msgno')) { |
||
| 420 | $messageNo = (int) trim($header->Msgno); |
||
| 421 | $this->message_no = ($this->fetch_options == IMAP::FT_UID) ? $messageNo : imap_msgno($this->client->getConnection(), $messageNo); |
||
| 422 | } else { |
||
| 423 | $this->message_no = imap_msgno($this->client->getConnection(), $this->getUid()); |
||
| 424 | } |
||
| 425 | |||
| 426 | $this->date = $this->parseDate($header); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Try to extract the priority from a given raw header string |
||
| 431 | * @param string $header |
||
| 432 | * |
||
| 433 | * @return int|null |
||
| 434 | */ |
||
| 435 | private function extractPriority($header) { |
||
| 436 | if(preg_match('/x\-priority\:.*([0-9]{1,2})/i', $header, $priority)){ |
||
| 437 | $priority = isset($priority[1]) ? (int) $priority[1] : 0; |
||
| 438 | switch($priority){ |
||
| 439 | case IMAP::MESSAGE_PRIORITY_HIGHEST; |
||
| 440 | $priority = IMAP::MESSAGE_PRIORITY_HIGHEST; |
||
| 441 | break; |
||
| 442 | case IMAP::MESSAGE_PRIORITY_HIGH; |
||
| 443 | $priority = IMAP::MESSAGE_PRIORITY_HIGH; |
||
| 444 | break; |
||
| 445 | case IMAP::MESSAGE_PRIORITY_NORMAL; |
||
| 446 | $priority = IMAP::MESSAGE_PRIORITY_NORMAL; |
||
| 447 | break; |
||
| 448 | case IMAP::MESSAGE_PRIORITY_LOW; |
||
| 449 | $priority = IMAP::MESSAGE_PRIORITY_LOW; |
||
| 450 | break; |
||
| 451 | case IMAP::MESSAGE_PRIORITY_LOWEST; |
||
| 452 | $priority = IMAP::MESSAGE_PRIORITY_LOWEST; |
||
| 453 | break; |
||
| 454 | default: |
||
| 455 | $priority = IMAP::MESSAGE_PRIORITY_UNKNOWN; |
||
| 456 | break; |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | return $priority; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Exception handling for invalid dates |
||
| 465 | * |
||
| 466 | * Currently known invalid formats: |
||
| 467 | * ^ Datetime ^ Problem ^ Cause |
||
| 468 | * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
||
| 469 | * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
||
| 470 | * | | and invalid timezone (max 6 char) | |
||
| 471 | * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
||
| 472 | * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
||
| 473 | * | | mail server | |
||
| 474 | * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
||
| 475 | * |
||
| 476 | * Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45) |
||
| 477 | * |
||
| 478 | * @param object $header |
||
| 479 | * |
||
| 480 | * @return Carbon|null |
||
| 481 | * @throws InvalidMessageDateException |
||
| 482 | */ |
||
| 483 | private function parseDate($header) { |
||
| 484 | $parsed_date = null; |
||
| 485 | |||
| 486 | if (property_exists($header, 'date')) { |
||
| 487 | $date = $header->date; |
||
| 488 | |||
| 489 | if(preg_match('/\+0580/', $date)) { |
||
| 490 | $date = str_replace('+0580', '+0530', $date); |
||
| 491 | } |
||
| 492 | |||
| 493 | $date = trim(rtrim($date)); |
||
| 494 | try { |
||
| 495 | $parsed_date = Carbon::parse($date); |
||
| 496 | } catch (\Exception $e) { |
||
| 497 | switch (true) { |
||
| 498 | case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0: |
||
| 499 | case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: |
||
| 500 | case preg_match('/([A-Z]{2,3}\, \ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: |
||
| 501 | case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0: |
||
| 502 | $array = explode('(', $date); |
||
| 503 | $array = array_reverse($array); |
||
| 504 | $date = trim(array_pop($array)); |
||
| 505 | break; |
||
| 506 | case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0: |
||
| 507 | $date .= 'C'; |
||
| 508 | break; |
||
| 509 | } |
||
| 510 | try{ |
||
| 511 | $parsed_date = Carbon::parse($date); |
||
| 512 | } catch (\Exception $_e) { |
||
| 513 | throw new InvalidMessageDateException("Invalid message date. ID:".$this->getMessageId(), 1000, $e); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | return $parsed_date; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Parse additional flags |
||
| 523 | * |
||
| 524 | * @return void |
||
| 525 | * @throws Exceptions\ConnectionFailedException |
||
| 526 | */ |
||
| 527 | private function parseFlags() { |
||
| 528 | $this->flags = FlagCollection::make([]); |
||
| 529 | |||
| 530 | $this->client->openFolder($this->folder_path); |
||
| 531 | $flags = imap_fetch_overview($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
||
| 532 | if (is_array($flags) && isset($flags[0])) { |
||
| 533 | foreach($this->available_flags as $flag) { |
||
| 534 | $this->parseFlag($flags, $flag); |
||
| 535 | } |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Extract a possible flag information from a given array |
||
| 541 | * @param array $flags |
||
| 542 | * @param string $flag |
||
| 543 | */ |
||
| 544 | private function parseFlag($flags, $flag) { |
||
| 545 | $flag = strtolower($flag); |
||
| 546 | |||
| 547 | if (property_exists($flags[0], strtoupper($flag))) { |
||
| 548 | $this->flags->put($flag, $flags[0]->{strtoupper($flag)}); |
||
| 549 | } elseif (property_exists($flags[0], ucfirst($flag))) { |
||
| 550 | $this->flags->put($flag, $flags[0]->{ucfirst($flag)}); |
||
| 551 | } elseif (property_exists($flags[0], $flag)) { |
||
| 552 | $this->flags->put($flag, $flags[0]->$flag); |
||
| 553 | } |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get the current Message header info |
||
| 558 | * |
||
| 559 | * @return object |
||
| 560 | * @throws Exceptions\ConnectionFailedException |
||
| 561 | */ |
||
| 562 | public function getHeaderInfo() { |
||
| 563 | if ($this->header_info == null) { |
||
| 564 | $this->client->openFolder($this->folder_path); |
||
| 565 | $this->header_info = imap_headerinfo($this->client->getConnection(), $this->getMessageNo()); |
||
| 566 | } |
||
| 567 | |||
| 568 | return $this->header_info; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Extract a given part as address array from a given header |
||
| 573 | * @param object $header |
||
| 574 | * @param string $part |
||
| 575 | */ |
||
| 576 | private function extractHeaderAddressPart($header, $part) { |
||
| 577 | if (property_exists($header, $part)) { |
||
| 578 | $this->$part = $this->parseAddresses($header->$part); |
||
| 579 | } |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Parse Addresses |
||
| 584 | * @param $list |
||
| 585 | * |
||
| 586 | * @return array |
||
| 587 | */ |
||
| 588 | private function parseAddresses($list) { |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Parse the Message body |
||
| 622 | * |
||
| 623 | * @return $this |
||
| 624 | * @throws Exceptions\ConnectionFailedException |
||
| 625 | */ |
||
| 626 | public function parseBody() { |
||
| 627 | $this->client->openFolder($this->folder_path); |
||
| 628 | $this->structure = imap_fetchstructure($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
||
| 629 | |||
| 630 | if(property_exists($this->structure, 'parts')){ |
||
| 631 | $parts = $this->structure->parts; |
||
| 632 | |||
| 633 | foreach ($parts as $part) { |
||
| 634 | foreach ($part->parameters as $parameter) { |
||
| 635 | if($parameter->attribute == "charset") { |
||
| 636 | $encoding = $parameter->value; |
||
| 637 | |||
| 638 | $encoding = preg_replace('/Content-Transfer-Encoding/', '', $encoding); |
||
| 639 | $encoding = preg_replace('/iso-8859-8-i/', 'iso-8859-8', $encoding); |
||
| 640 | |||
| 641 | $parameter->value = $encoding; |
||
| 642 | } |
||
| 643 | } |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | $this->fetchStructure($this->structure); |
||
| 648 | |||
| 649 | return $this; |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Fetch the Message structure |
||
| 654 | * |
||
| 655 | * @param $structure |
||
| 656 | * @param mixed $partNumber |
||
| 657 | * |
||
| 658 | * @throws Exceptions\ConnectionFailedException |
||
| 659 | */ |
||
| 660 | private function fetchStructure($structure, $partNumber = null) { |
||
| 661 | $this->client->openFolder($this->folder_path); |
||
| 662 | |||
| 663 | if ($structure->type == IMAP::MESSAGE_TYPE_TEXT && |
||
| 664 | ($structure->ifdisposition == 0 || |
||
| 665 | ($structure->ifdisposition == 1 && !isset($structure->parts) && $partNumber == null) |
||
| 666 | ) |
||
| 667 | ) { |
||
| 668 | if ($structure->subtype == "PLAIN") { |
||
| 669 | if (!$partNumber) { |
||
| 670 | $partNumber = 1; |
||
| 671 | } |
||
| 672 | |||
| 673 | $encoding = $this->getEncoding($structure); |
||
| 674 | |||
| 675 | $content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | IMAP::FT_UID); |
||
| 676 | $content = $this->decodeString($content, $structure->encoding); |
||
| 677 | |||
| 678 | // We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
||
| 679 | // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
||
| 680 | // https://stackoverflow.com/a/11303410 |
||
| 681 | // |
||
| 682 | // us-ascii is the same as ASCII: |
||
| 683 | // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
||
| 684 | // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
||
| 685 | // based on the typographical symbols predominantly in use there. |
||
| 686 | // https://en.wikipedia.org/wiki/ASCII |
||
| 687 | // |
||
| 688 | // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
||
| 689 | if ($encoding != 'us-ascii') { |
||
| 690 | $content = $this->convertEncoding($content, $encoding); |
||
| 691 | } |
||
| 692 | |||
| 693 | $body = new \stdClass; |
||
| 694 | $body->type = "text"; |
||
| 695 | $body->content = $content; |
||
| 696 | |||
| 697 | $this->bodies['text'] = $body; |
||
| 698 | |||
| 699 | $this->fetchAttachment($structure, $partNumber); |
||
| 700 | |||
| 701 | } elseif ($structure->subtype == "HTML") { |
||
| 702 | if (!$partNumber) { |
||
| 703 | $partNumber = 1; |
||
| 704 | } |
||
| 705 | |||
| 706 | $encoding = $this->getEncoding($structure); |
||
| 707 | |||
| 708 | $content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | IMAP::FT_UID); |
||
| 709 | $content = $this->decodeString($content, $structure->encoding); |
||
| 710 | if ($encoding != 'us-ascii') { |
||
| 711 | $content = $this->convertEncoding($content, $encoding); |
||
| 712 | } |
||
| 713 | |||
| 714 | $body = new \stdClass; |
||
| 715 | $body->type = "html"; |
||
| 716 | $body->content = $content; |
||
| 717 | |||
| 718 | $this->bodies['html'] = $body; |
||
| 719 | } |
||
| 720 | } elseif ($structure->type == IMAP::MESSAGE_TYPE_MULTIPART) { |
||
| 721 | foreach ($structure->parts as $index => $subStruct) { |
||
| 722 | $prefix = ""; |
||
| 723 | if ($partNumber) { |
||
| 724 | $prefix = $partNumber."."; |
||
| 725 | } |
||
| 726 | $this->fetchStructure($subStruct, $prefix.($index + 1)); |
||
| 727 | } |
||
| 728 | } else { |
||
| 729 | if ($this->getFetchAttachmentOption() === true) { |
||
| 730 | $this->fetchAttachment($structure, $partNumber); |
||
| 731 | } |
||
| 732 | } |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Fetch the Message attachment |
||
| 737 | * |
||
| 738 | * @param object $structure |
||
| 739 | * @param mixed $partNumber |
||
| 740 | * |
||
| 741 | * @throws Exceptions\ConnectionFailedException |
||
| 742 | */ |
||
| 743 | protected function fetchAttachment($structure, $partNumber) { |
||
| 744 | |||
| 745 | $oAttachment = new Attachment($this, $structure, $partNumber); |
||
| 746 | |||
| 747 | if ($oAttachment->getName() !== null) { |
||
| 748 | if ($oAttachment->getId() !== null) { |
||
| 749 | $this->attachments->put($oAttachment->getId(), $oAttachment); |
||
| 750 | } else { |
||
| 751 | $this->attachments->push($oAttachment); |
||
| 752 | } |
||
| 753 | } |
||
| 754 | } |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Fail proof setter for $fetch_option |
||
| 758 | * |
||
| 759 | * @param $option |
||
| 760 | * |
||
| 761 | * @return $this |
||
| 762 | */ |
||
| 763 | public function setFetchOption($option) { |
||
| 764 | if (is_long($option) === true) { |
||
| 765 | $this->fetch_options = $option; |
||
| 766 | } elseif (is_null($option) === true) { |
||
| 767 | $config = config('imap.options.fetch', IMAP::FT_UID); |
||
| 768 | $this->fetch_options = is_long($config) ? $config : 1; |
||
| 769 | } |
||
| 770 | |||
| 771 | return $this; |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Fail proof setter for $fetch_body |
||
| 776 | * |
||
| 777 | * @param $option |
||
| 778 | * |
||
| 779 | * @return $this |
||
| 780 | */ |
||
| 781 | public function setFetchBodyOption($option) { |
||
| 782 | if (is_bool($option)) { |
||
| 783 | $this->fetch_body = $option; |
||
| 784 | } elseif (is_null($option)) { |
||
| 785 | $config = config('imap.options.fetch_body', true); |
||
| 786 | $this->fetch_body = is_bool($config) ? $config : true; |
||
| 787 | } |
||
| 788 | |||
| 789 | return $this; |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Fail proof setter for $fetch_attachment |
||
| 794 | * |
||
| 795 | * @param $option |
||
| 796 | * |
||
| 797 | * @return $this |
||
| 798 | */ |
||
| 799 | public function setFetchAttachmentOption($option) { |
||
| 800 | if (is_bool($option)) { |
||
| 801 | $this->fetch_attachment = $option; |
||
| 802 | } elseif (is_null($option)) { |
||
| 803 | $config = config('imap.options.fetch_attachment', true); |
||
| 804 | $this->fetch_attachment = is_bool($config) ? $config : true; |
||
| 805 | } |
||
| 806 | |||
| 807 | return $this; |
||
| 808 | } |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Fail proof setter for $fetch_flags |
||
| 812 | * |
||
| 813 | * @param $option |
||
| 814 | * |
||
| 815 | * @return $this |
||
| 816 | */ |
||
| 817 | public function setFetchFlagsOption($option) { |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Decode a given string |
||
| 830 | * |
||
| 831 | * @param $string |
||
| 832 | * @param $encoding |
||
| 833 | * |
||
| 834 | * @return string |
||
| 835 | */ |
||
| 836 | public function decodeString($string, $encoding) { |
||
| 852 | } |
||
| 853 | } |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Convert the encoding |
||
| 857 | * |
||
| 858 | * @param $str |
||
| 859 | * @param string $from |
||
| 860 | * @param string $to |
||
| 861 | * |
||
| 862 | * @return mixed|string |
||
| 863 | */ |
||
| 864 | public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
||
| 865 | |||
| 866 | $from = EncodingAliases::get($from); |
||
| 867 | $to = EncodingAliases::get($to); |
||
| 868 | |||
| 869 | if ($from === $to) { |
||
| 870 | return $str; |
||
| 871 | } |
||
| 872 | |||
| 873 | // We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
||
| 874 | // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
||
| 875 | // https://stackoverflow.com/a/11303410 |
||
| 876 | // |
||
| 877 | // us-ascii is the same as ASCII: |
||
| 878 | // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
||
| 879 | // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
||
| 880 | // based on the typographical symbols predominantly in use there. |
||
| 881 | // https://en.wikipedia.org/wiki/ASCII |
||
| 882 | // |
||
| 883 | // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
||
| 884 | if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') { |
||
| 885 | return $str; |
||
| 886 | } |
||
| 887 | |||
| 888 | if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { |
||
| 889 | return @iconv($from, $to.'//IGNORE', $str); |
||
| 890 | } else { |
||
| 891 | if (!$from) { |
||
| 892 | return mb_convert_encoding($str, $to); |
||
| 893 | } |
||
| 894 | return mb_convert_encoding($str, $to, $from); |
||
| 895 | } |
||
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Get the encoding of a given abject |
||
| 900 | * |
||
| 901 | * @param object|string $structure |
||
| 902 | * |
||
| 903 | * @return string |
||
| 904 | */ |
||
| 905 | public function getEncoding($structure) { |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Find the folder containing this message. |
||
| 921 | * @param null|Folder $folder where to start searching from (top-level inbox by default) |
||
| 922 | * |
||
| 923 | * @return mixed|null|Folder |
||
| 924 | * @throws Exceptions\ConnectionFailedException |
||
| 925 | * @throws Exceptions\MailboxFetchingException |
||
| 926 | * @throws InvalidMessageDateException |
||
| 927 | * @throws MaskNotFoundException |
||
| 928 | */ |
||
| 929 | public function getContainingFolder(Folder $folder = null) { |
||
| 960 | } |
||
| 961 | |||
| 962 | public function getFolder(){ |
||
| 963 | return $this->client->getFolder($this->folder_path); |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Move the Message into an other Folder |
||
| 968 | * @param string $mailbox |
||
| 969 | * @param bool $expunge |
||
| 970 | * @param bool $create_folder |
||
| 971 | * |
||
| 972 | * @return null|Message |
||
| 973 | * @throws Exceptions\ConnectionFailedException |
||
| 974 | * @throws InvalidMessageDateException |
||
| 975 | */ |
||
| 976 | public function moveToFolder($mailbox = 'INBOX', $expunge = false, $create_folder = true) { |
||
| 977 | |||
| 978 | if($create_folder) $this->client->createFolder($mailbox, true); |
||
| 979 | |||
| 980 | $target_folder = $this->client->getFolder($mailbox); |
||
| 981 | $target_status = $target_folder->getStatus(IMAP::SA_ALL); |
||
| 982 | |||
| 983 | $this->client->openFolder($this->folder_path); |
||
| 984 | $status = imap_mail_move($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID); |
||
| 985 | |||
| 986 | if($status === true){ |
||
| 987 | if($expunge) $this->client->expunge(); |
||
| 988 | $this->client->openFolder($target_folder->path); |
||
| 989 | |||
| 990 | return $target_folder->getMessage($target_status->uidnext, null, $this->fetch_options, $this->fetch_body, $this->fetch_attachment, $this->fetch_flags); |
||
| 991 | } |
||
| 992 | |||
| 993 | return null; |
||
| 994 | } |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Delete the current Message |
||
| 998 | * @param bool $expunge |
||
| 999 | * |
||
| 1000 | * @return bool |
||
| 1001 | * @throws Exceptions\ConnectionFailedException |
||
| 1002 | */ |
||
| 1003 | public function delete($expunge = true) { |
||
| 1004 | $this->client->openFolder($this->folder_path); |
||
| 1005 | |||
| 1006 | $status = imap_delete($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
||
| 1007 | if($expunge) $this->client->expunge(); |
||
| 1008 | |||
| 1009 | return $status; |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Restore a deleted Message |
||
| 1014 | * @param boolean $expunge |
||
| 1015 | * |
||
| 1016 | * @return bool |
||
| 1017 | * @throws Exceptions\ConnectionFailedException |
||
| 1018 | */ |
||
| 1019 | public function restore($expunge = true) { |
||
| 1020 | $this->client->openFolder($this->folder_path); |
||
| 1021 | |||
| 1022 | $status = imap_undelete($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
||
| 1023 | if($expunge) $this->client->expunge(); |
||
| 1024 | |||
| 1025 | return $status; |
||
| 1026 | } |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Get all message attachments. |
||
| 1030 | * |
||
| 1031 | * @return AttachmentCollection |
||
| 1032 | */ |
||
| 1033 | public function getAttachments() { |
||
| 1034 | return $this->attachments; |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Checks if there are any attachments present |
||
| 1039 | * |
||
| 1040 | * @return boolean |
||
| 1041 | */ |
||
| 1042 | public function hasAttachments() { |
||
| 1043 | return $this->attachments->isEmpty() === false; |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Set a given flag |
||
| 1048 | * @param string|array $flag |
||
| 1049 | * |
||
| 1050 | * @return bool |
||
| 1051 | * @throws Exceptions\ConnectionFailedException |
||
| 1052 | */ |
||
| 1053 | public function setFlag($flag) { |
||
| 1054 | $this->client->openFolder($this->folder_path); |
||
| 1055 | |||
| 1056 | $flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag); |
||
| 1057 | $status = imap_setflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID); |
||
| 1058 | $this->parseFlags(); |
||
| 1059 | |||
| 1060 | return $status; |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Unset a given flag |
||
| 1065 | * @param string|array $flag |
||
| 1066 | * |
||
| 1067 | * @return bool |
||
| 1068 | * @throws Exceptions\ConnectionFailedException |
||
| 1069 | */ |
||
| 1070 | public function unsetFlag($flag) { |
||
| 1071 | $this->client->openFolder($this->folder_path); |
||
| 1072 | |||
| 1073 | $flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag); |
||
| 1074 | $status = imap_clearflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID); |
||
| 1075 | $this->parseFlags(); |
||
| 1076 | |||
| 1077 | return $status; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * @return null|object|string |
||
| 1082 | * @throws Exceptions\ConnectionFailedException |
||
| 1083 | */ |
||
| 1084 | public function getRawBody() { |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * @return string |
||
| 1096 | */ |
||
| 1097 | public function getHeader() { |
||
| 1098 | return $this->header; |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * @return Client |
||
| 1103 | */ |
||
| 1104 | public function getClient() { |
||
| 1105 | return $this->client; |
||
| 1106 | } |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * @return integer |
||
| 1110 | */ |
||
| 1111 | public function getFetchOptions() { |
||
| 1112 | return $this->fetch_options; |
||
| 1113 | } |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * @return boolean |
||
| 1117 | */ |
||
| 1118 | public function getFetchBodyOption() { |
||
| 1119 | return $this->fetch_body; |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * @return boolean |
||
| 1124 | */ |
||
| 1125 | public function getFetchAttachmentOption() { |
||
| 1126 | return $this->fetch_attachment; |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * @return boolean |
||
| 1131 | */ |
||
| 1132 | public function getFetchFlagsOption() { |
||
| 1133 | return $this->fetch_flags; |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * @return mixed |
||
| 1138 | */ |
||
| 1139 | public function getBodies() { |
||
| 1140 | return $this->bodies; |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * @return FlagCollection |
||
| 1145 | */ |
||
| 1146 | public function getFlags() { |
||
| 1147 | return $this->flags; |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * @return object|null |
||
| 1152 | */ |
||
| 1153 | public function getStructure(){ |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Does this message match another one? |
||
| 1159 | * |
||
| 1160 | * A match means same uid, message id, subject and date/time. |
||
| 1161 | * |
||
| 1162 | * @param null|Message $message |
||
| 1163 | * @return boolean |
||
| 1164 | */ |
||
| 1165 | public function is(Message $message = null) { |
||
| 1166 | if (is_null($message)) { |
||
| 1167 | return false; |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | return $this->uid == $message->uid |
||
| 1171 | && $this->message_id == $message->message_id |
||
| 1172 | && $this->subject == $message->subject |
||
| 1173 | && $this->date->eq($message->date); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * @return array |
||
| 1178 | */ |
||
| 1179 | public function getAttributes(){ |
||
| 1180 | return $this->attributes; |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | /** |
||
| 1184 | * @param $mask |
||
| 1185 | * @return $this |
||
| 1186 | */ |
||
| 1187 | public function setMask($mask){ |
||
| 1188 | if(class_exists($mask)){ |
||
| 1189 | $this->mask = $mask; |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | return $this; |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * @return string |
||
| 1197 | */ |
||
| 1198 | public function getMask(){ |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Get a masked instance by providing a mask name |
||
| 1204 | * @param string|null $mask |
||
| 1205 | * |
||
| 1206 | * @return mixed |
||
| 1207 | * @throws MaskNotFoundException |
||
| 1208 | */ |
||
| 1209 | public function mask($mask = null){ |
||
| 1210 | $mask = $mask !== null ? $mask : $this->mask; |
||
| 1216 | } |
||
| 1217 | } |
||
| 1218 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..