Passed
Push — master ( 0897d6...647d71 )
by Malte
03:33
created

Header::decodeArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 5
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/*
3
* File: Header.php
4
* Category: -
5
* Author: M.Goldenbaum
6
* Created: 17.09.20 20:38
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\PHPIMAP;
14
15
16
use Carbon\Carbon;
17
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
18
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
19
20
/**
21
 * Class Header
22
 *
23
 * @package Webklex\PHPIMAP
24
 */
25
class Header {
26
27
    /**
28
     * Raw header
29
     *
30
     * @var string $raw
31
     */
32
    public $raw = "";
33
34
    /**
35
     * Attribute holder
36
     *
37
     * @var Attribute[]|array $attributes
38
     */
39
    protected $attributes = [];
40
41
    /**
42
     * Config holder
43
     *
44
     * @var array $config
45
     */
46
    protected $config = [];
47
48
    /**
49
     * Fallback Encoding
50
     *
51
     * @var string
52
     */
53
    public $fallback_encoding = 'UTF-8';
54
55
    /**
56
     * Convert parsed values to attributes
57
     *
58
     * @var bool
59
     */
60
    protected $attributize = false;
61
62
    /**
63
     * Header constructor.
64
     * @param string $raw_header
65
     * @param boolean $attributize
66
     *
67
     * @throws InvalidMessageDateException
68
     */
69
    public function __construct(string $raw_header, bool $attributize = true) {
70
        $this->raw = $raw_header;
71
        $this->config = ClientManager::get('options');
72
        $this->attributize = $attributize;
73
        $this->parse();
74
    }
75
76
    /**
77
     * Call dynamic attribute setter and getter methods
78
     * @param string $method
79
     * @param array $arguments
80
     *
81
     * @return Attribute|mixed
82
     * @throws MethodNotFoundException
83
     */
84
    public function __call(string $method, array $arguments) {
85
        if (strtolower(substr($method, 0, 3)) === 'get') {
86
            $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3));
87
88
            if (in_array($name, array_keys($this->attributes))) {
89
                return $this->attributes[$name];
90
            }
91
92
        }
93
94
        throw new MethodNotFoundException("Method " . self::class . '::' . $method . '() is not supported');
95
    }
96
97
    /**
98
     * Magic getter
99
     * @param $name
100
     *
101
     * @return Attribute|null
102
     */
103
    public function __get($name) {
104
        return $this->get($name);
105
    }
106
107
    /**
108
     * Get a specific header attribute
109
     * @param $name
110
     *
111
     * @return Attribute|mixed
112
     */
113
    public function get($name) {
114
        if (isset($this->attributes[$name])) {
115
            return $this->attributes[$name];
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * Set a specific attribute
123
     * @param string $name
124
     * @param array|mixed $value
125
     * @param boolean $strict
126
     *
127
     * @return Attribute
128
     */
129
    public function set(string $name, $value, bool $strict = false) {
130
        if (isset($this->attributes[$name]) && $strict === false) {
131
            if ($this->attributize) {
132
                $this->attributes[$name]->add($value, true);
133
            } else {
134
                if (isset($this->attributes[$name])) {
135
                    if (is_array($this->attributes[$name]) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
136
                        $this->attributes[$name] = [$this->attributes[$name], $value];
137
                    } else {
138
                        $this->attributes[$name][] = $value;
139
                    }
140
                } else {
141
                    $this->attributes[$name] = $value;
142
                }
143
            }
144
        } elseif ($this->attributize == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
145
            $this->attributes[$name] = $value;
146
        } else {
147
            $this->attributes[$name] = new Attribute($name, $value);
148
        }
149
150
        return $this->attributes[$name];
151
    }
152
153
    /**
154
     * Perform a regex match all on the raw header and return the first result
155
     * @param $pattern
156
     *
157
     * @return mixed|null
158
     */
159
    public function find($pattern) {
160
        if (preg_match_all($pattern, $this->raw, $matches)) {
161
            if (isset($matches[1])) {
162
                if (count($matches[1]) > 0) {
163
                    return $matches[1][0];
164
                }
165
            }
166
        }
167
        return null;
168
    }
169
170
    /**
171
     * Try to find a boundary if possible
172
     *
173
     * @return string|null
174
     */
175
    public function getBoundary() {
176
        $regex = $this->config["boundary"] ?? "/boundary=(.*?(?=;)|(.*))/i";
177
        $boundary = $this->find($regex);
178
179
        if ($boundary === null) {
180
            return null;
181
        }
182
183
        return $this->clearBoundaryString($boundary);
184
    }
185
186
    /**
187
     * Remove all unwanted chars from a given boundary
188
     * @param string $str
189
     *
190
     * @return string
191
     */
192
    private function clearBoundaryString(string $str): string {
193
        return str_replace(['"', '\r', '\n', "\n", "\r", ";", "\s"], "", $str);
194
    }
195
196
    /**
197
     * Parse the raw headers
198
     *
199
     * @throws InvalidMessageDateException
200
     */
201
    protected function parse() {
202
        $header = $this->rfc822_parse_headers($this->raw);
203
204
        $this->extractAddresses($header);
205
206
        if (property_exists($header, 'subject')) {
207
            $this->set("subject", $this->decode($header->subject));
208
        }
209
        if (property_exists($header, 'references')) {
210
            $this->set("references", $this->decode($header->references));
211
        }
212
        if (property_exists($header, 'message_id')) {
213
            $this->set("message_id", str_replace(['<', '>'], '', $header->message_id));
214
        }
215
216
        $this->parseDate($header);
217
        foreach ($header as $key => $value) {
218
            $key = trim(rtrim(strtolower($key)));
219
            if (!isset($this->attributes[$key])) {
220
                $this->set($key, $value);
221
            }
222
        }
223
224
        $this->extractHeaderExtensions();
225
        $this->findPriority();
226
    }
227
228
    /**
229
     * Parse mail headers from a string
230
     * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php
231
     * @param $raw_headers
232
     *
233
     * @return object
234
     */
235
    public function rfc822_parse_headers($raw_headers) {
236
        $headers = [];
237
        $imap_headers = [];
238
        if (extension_loaded('imap') && $this->config["rfc822"]) {
239
            $raw_imap_headers = (array)\imap_rfc822_parse_headers($this->raw);
240
            foreach ($raw_imap_headers as $key => $values) {
241
                $key = str_replace("-", "_", $key);
242
                $imap_headers[$key] = $values;
243
            }
244
        }
245
        $lines = explode("\r\n", str_replace("\r\n\t", ' ', $raw_headers));
246
        $prev_header = null;
247
        foreach ($lines as $line) {
248
            if (substr($line, 0, 1) === "\n") {
249
                $line = substr($line, 1);
250
            }
251
252
            if (substr($line, 0, 1) === "\t") {
253
                $line = substr($line, 1);
254
                $line = trim(rtrim($line));
255
                if ($prev_header !== null) {
256
                    $headers[$prev_header][] = $line;
257
                }
258
            } elseif (substr($line, 0, 1) === " ") {
259
                $line = substr($line, 1);
260
                $line = trim(rtrim($line));
261
                if ($prev_header !== null) {
262
                    if (!isset($headers[$prev_header])) {
263
                        $headers[$prev_header] = "";
264
                    }
265
                    if (is_array($headers[$prev_header])) {
266
                        $headers[$prev_header][] = $line;
267
                    } else {
268
                        $headers[$prev_header] .= $line;
269
                    }
270
                }
271
            } else {
272
                if (($pos = strpos($line, ":")) > 0) {
273
                    $key = trim(rtrim(strtolower(substr($line, 0, $pos))));
274
                    $key = str_replace("-", "_", $key);
275
276
                    $value = trim(rtrim(substr($line, $pos + 1)));
277
                    if (isset($headers[$key])) {
278
                        $headers[$key][] = $value;
279
                    } else {
280
                        $headers[$key] = [$value];
281
                    }
282
                    $prev_header = $key;
283
                }
284
            }
285
        }
286
287
        foreach ($headers as $key => $values) {
288
            if (isset($imap_headers[$key])) continue;
289
            $value = null;
290
            switch ($key) {
291
                case 'from':
292
                case 'to':
293
                case 'cc':
294
                case 'bcc':
295
                case 'reply_to':
296
                case 'sender':
297
                    $value = $this->decodeAddresses($values);
298
                    $headers[$key . "address"] = implode(", ", $values);
299
                    break;
300
                case 'subject':
301
                    $value = implode(" ", $values);
302
                    break;
303
                default:
304
                    if (is_array($values)) {
305
                        foreach ($values as $k => $v) {
306
                            if ($v == "") {
307
                                unset($values[$k]);
308
                            }
309
                        }
310
                        $available_values = count($values);
311
                        if ($available_values === 1) {
312
                            $value = array_pop($values);
313
                        } elseif ($available_values === 2) {
314
                            $value = implode(" ", $values);
315
                        } elseif ($available_values > 2) {
316
                            $value = array_values($values);
317
                        } else {
318
                            $value = "";
319
                        }
320
                    }
321
                    break;
322
            }
323
            $headers[$key] = $value;
324
        }
325
326
        return (object)array_merge($headers, $imap_headers);
327
    }
328
329
    /**
330
     * Decode MIME header elements
331
     * @link https://php.net/manual/en/function.imap-mime-header-decode.php
332
     * @param string $text The MIME text
333
     *
334
     * @return array The decoded elements are returned in an array of objects, where each
335
     * object has two properties, charset and text.
336
     */
337
    public function mime_header_decode(string $text): array {
338
        if (extension_loaded('imap')) {
339
            return \imap_mime_header_decode($text);
340
        }
341
        $charset = $this->getEncoding($text);
342
        return [(object)[
343
            "charset" => $charset,
344
            "text"    => $this->convertEncoding($text, $charset)
345
        ]];
346
    }
347
348
    /**
349
     * Check if a given pair of strings has ben decoded
350
     * @param $encoded
351
     * @param $decoded
352
     *
353
     * @return bool
354
     */
355
    private function notDecoded($encoded, $decoded): bool {
356
        return 0 === strpos($decoded, '=?')
357
            && strlen($decoded) - 2 === strpos($decoded, '?=')
358
            && false !== strpos($encoded, $decoded);
359
    }
360
361
    /**
362
     * Convert the encoding
363
     * @param $str
364
     * @param string $from
365
     * @param string $to
366
     *
367
     * @return mixed|string
368
     */
369
    public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") {
370
371
        $from = EncodingAliases::get($from, $this->fallback_encoding);
372
        $to = EncodingAliases::get($to, $this->fallback_encoding);
373
374
        if ($from === $to) {
375
            return $str;
376
        }
377
378
        // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
379
        //     ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
380
        //     https://stackoverflow.com/a/11303410
381
        //
382
        // us-ascii is the same as ASCII:
383
        //     ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
384
        //     prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
385
        //     based on the typographical symbols predominantly in use there.
386
        //     https://en.wikipedia.org/wiki/ASCII
387
        //
388
        // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
389
        if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') {
390
            return $str;
391
        }
392
393
        try {
394
            if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') {
395
                return iconv($from, $to, $str);
396
            } else {
397
                if (!$from) {
398
                    return mb_convert_encoding($str, $to);
399
                }
400
                return mb_convert_encoding($str, $to, $from);
401
            }
402
        } catch (\Exception $e) {
403
            if (strstr($from, '-')) {
404
                $from = str_replace('-', '', $from);
405
                return $this->convertEncoding($str, $from, $to);
406
            } else {
407
                return $str;
408
            }
409
        }
410
    }
411
412
    /**
413
     * Get the encoding of a given abject
414
     * @param object|string $structure
415
     *
416
     * @return string
417
     */
418
    public function getEncoding($structure): string {
419
        if (property_exists($structure, 'parameters')) {
420
            foreach ($structure->parameters as $parameter) {
421
                if (strtolower($parameter->attribute) == "charset") {
422
                    return EncodingAliases::get($parameter->value, $this->fallback_encoding);
423
                }
424
            }
425
        } elseif (property_exists($structure, 'charset')) {
426
            return EncodingAliases::get($structure->charset, $this->fallback_encoding);
427
        } elseif (is_string($structure) === true) {
428
            return mb_detect_encoding($structure);
429
        }
430
431
        return $this->fallback_encoding;
432
    }
433
434
    /**
435
     * Test if a given value is utf-8 encoded
436
     * @param $value
437
     *
438
     * @return bool
439
     */
440
    private function is_uft8($value): bool {
441
        return strpos(strtolower($value), '=?utf-8?') === 0;
442
    }
443
444
    /**
445
     * Try to decode a specific header
446
     * @param mixed $value
447
     *
448
     * @return mixed
449
     */
450
    private function decode($value) {
451
        if (is_array($value)) {
452
            return $this->decodeArray($value);
453
        }
454
        $original_value = $value;
455
        $decoder = $this->config['decoder']['message'];
456
457
        if ($value !== null) {
458
            $is_utf8_base = $this->is_uft8($value);
459
460
            if ($decoder === 'utf-8' && extension_loaded('imap')) {
461
                $value = \imap_utf8($value);
462
                $is_utf8_base = $this->is_uft8($value);
463
                if ($is_utf8_base) {
464
                    $value = mb_decode_mimeheader($value);
465
                }
466
                if ($this->notDecoded($original_value, $value)) {
467
                    $decoded_value = $this->mime_header_decode($value);
468
                    if (count($decoded_value) > 0) {
469
                        if (property_exists($decoded_value[0], "text")) {
470
                            $value = $decoded_value[0]->text;
471
                        }
472
                    }
473
                }
474
            } elseif ($decoder === 'iconv' && $is_utf8_base) {
475
                $value = iconv_mime_decode($value);
476
            } elseif ($is_utf8_base) {
477
                $value = mb_decode_mimeheader($value);
478
            }
479
480
            if ($this->is_uft8($value)) {
481
                $value = mb_decode_mimeheader($value);
482
            }
483
484
            if ($this->notDecoded($original_value, $value)) {
485
                $value = $this->convertEncoding($original_value, $this->getEncoding($original_value));
486
            }
487
        }
488
489
        return $value;
490
    }
491
492
    /**
493
     * Decode a given array
494
     * @param array $values
495
     *
496
     * @return array
497
     */
498
    private function decodeArray(array $values): array {
499
        foreach ($values as $key => $value) {
500
            $values[$key] = $this->decode($value);
501
        }
502
        return $values;
503
    }
504
505
    /**
506
     * Try to extract the priority from a given raw header string
507
     */
508
    private function findPriority() {
509
        if (($priority = $this->get("x_priority")) === null) return;
510
        switch ((int)"$priority") {
511
            case IMAP::MESSAGE_PRIORITY_HIGHEST;
512
                $priority = IMAP::MESSAGE_PRIORITY_HIGHEST;
513
                break;
514
            case IMAP::MESSAGE_PRIORITY_HIGH;
515
                $priority = IMAP::MESSAGE_PRIORITY_HIGH;
516
                break;
517
            case IMAP::MESSAGE_PRIORITY_NORMAL;
518
                $priority = IMAP::MESSAGE_PRIORITY_NORMAL;
519
                break;
520
            case IMAP::MESSAGE_PRIORITY_LOW;
521
                $priority = IMAP::MESSAGE_PRIORITY_LOW;
522
                break;
523
            case IMAP::MESSAGE_PRIORITY_LOWEST;
524
                $priority = IMAP::MESSAGE_PRIORITY_LOWEST;
525
                break;
526
            default:
527
                $priority = IMAP::MESSAGE_PRIORITY_UNKNOWN;
528
                break;
529
        }
530
531
        $this->set("priority", $priority);
532
    }
533
534
    /**
535
     * Extract a given part as address array from a given header
536
     * @param $values
537
     *
538
     * @return array
539
     */
540
    private function decodeAddresses($values): array {
541
        $addresses = [];
542
543
        if (extension_loaded('mailparse') && $this->config["rfc822"]) {
544
            foreach ($values as $address) {
545
                foreach (\mailparse_rfc822_parse_addresses($address) as $parsed_address) {
546
                    if (isset($parsed_address['address'])) {
547
                        $mail_address = explode('@', $parsed_address['address']);
548
                        if (count($mail_address) == 2) {
549
                            $addresses[] = (object)[
550
                                "personal" => $parsed_address['display'] ?? '',
551
                                "mailbox"  => $mail_address[0],
552
                                "host"     => $mail_address[1],
553
                            ];
554
                        }
555
                    }
556
                }
557
            }
558
559
            return $addresses;
560
        }
561
562
        foreach ($values as $address) {
563
            foreach (preg_split('/, (?=(?:[^"]*"[^"]*")*[^"]*$)/', $address) as $split_address) {
564
                $split_address = trim(rtrim($split_address));
565
566
                if (strpos($split_address, ",") == strlen($split_address) - 1) {
567
                    $split_address = substr($split_address, 0, -1);
568
                }
569
                if (preg_match(
570
                    '/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
571
                    $split_address,
572
                    $matches
573
                )) {
574
                    $name = trim(rtrim($matches["name"]));
575
                    $email = trim(rtrim($matches["email"]));
576
                    list($mailbox, $host) = array_pad(explode("@", $email), 2, null);
577
                    $addresses[] = (object)[
578
                        "personal" => $name,
579
                        "mailbox"  => $mailbox,
580
                        "host"     => $host,
581
                    ];
582
                }
583
            }
584
        }
585
586
        return $addresses;
587
    }
588
589
    /**
590
     * Extract a given part as address array from a given header
591
     * @param object $header
592
     */
593
    private function extractAddresses($header) {
594
        foreach (['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key) {
595
            if (property_exists($header, $key)) {
596
                $this->set($key, $this->parseAddresses($header->$key));
597
            }
598
        }
599
    }
600
601
    /**
602
     * Parse Addresses
603
     * @param $list
604
     *
605
     * @return array
606
     */
607
    private function parseAddresses($list): array {
608
        $addresses = [];
609
610
        if (is_array($list) === false) {
611
            return $addresses;
612
        }
613
614
        foreach ($list as $item) {
615
            $address = (object)$item;
616
617
            if (!property_exists($address, 'mailbox')) {
618
                $address->mailbox = false;
619
            }
620
            if (!property_exists($address, 'host')) {
621
                $address->host = false;
622
            }
623
            if (!property_exists($address, 'personal')) {
624
                $address->personal = false;
625
            } else {
626
                $personalParts = $this->mime_header_decode($address->personal);
627
628
                if (is_array($personalParts)) {
629
                    $address->personal = '';
630
                    foreach ($personalParts as $p) {
631
                        $address->personal .= $this->convertEncoding($p->text, $this->getEncoding($p));
632
                    }
633
                }
634
635
                if (strpos($address->personal, "'") === 0) {
636
                    $address->personal = str_replace("'", "", $address->personal);
637
                }
638
            }
639
640
            $address->mail = ($address->mailbox && $address->host) ? $address->mailbox . '@' . $address->host : false;
641
            $address->full = ($address->personal) ? $address->personal . ' <' . $address->mail . '>' : $address->mail;
0 ignored issues
show
Bug introduced by
Are you sure $address->mail of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

641
            $address->full = ($address->personal) ? $address->personal . ' <' . /** @scrutinizer ignore-type */ $address->mail . '>' : $address->mail;
Loading history...
642
643
            $addresses[] = new Address($address);
644
        }
645
646
        return $addresses;
647
    }
648
649
    /**
650
     * Search and extract potential header extensions
651
     */
652
    private function extractHeaderExtensions() {
653
        foreach ($this->attributes as $key => $value) {
654
            if (is_array($value)) {
655
                $value = implode(", ", $value);
656
            } else {
657
                $value = (string)$value;
658
            }
659
            // Only parse strings and don't parse any attributes like the user-agent
660
            if (($key == "user_agent") === false) {
661
                if (($pos = strpos($value, ";")) !== false) {
662
                    $original = substr($value, 0, $pos);
663
                    $this->set($key, trim(rtrim($original)), true);
664
665
                    // Get all potential extensions
666
                    $extensions = explode(";", substr($value, $pos + 1));
667
                    foreach ($extensions as $extension) {
668
                        if (($pos = strpos($extension, "=")) !== false) {
669
                            $key = substr($extension, 0, $pos);
670
                            $key = trim(rtrim(strtolower($key)));
671
672
                            if (isset($this->attributes[$key]) === false) {
673
                                $value = substr($extension, $pos + 1);
674
                                $value = str_replace('"', "", $value);
675
                                $value = trim(rtrim($value));
676
677
                                $this->set($key, $value);
678
                            }
679
                        }
680
                    }
681
                }
682
            }
683
        }
684
    }
685
686
    /**
687
     * Exception handling for invalid dates
688
     *
689
     * Currently known invalid formats:
690
     * ^ Datetime                                   ^ Problem                           ^ Cause
691
     * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification     | A Windows feature
692
     * | Thu, 8 Nov 2018 08:54:58 -0200 (-02)       |
693
     * |                                            | and invalid timezone (max 6 char) |
694
     * | 04 Jan 2018 10:12:47 UT                    | Missing letter "C"                | Unknown
695
     * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
696
     * |                                            | mail server                       |
697
     * | Sat, 31 Aug 2013 20:08:23 +0580            | Invalid timezone                  | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
698
     *
699
     * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues)
700
     *
701
     * @param object $header
702
     *
703
     * @throws InvalidMessageDateException
704
     */
705
    private function parseDate($header) {
706
707
        if (property_exists($header, 'date')) {
708
            $date = $header->date;
709
710
            if (preg_match('/\+0580/', $date)) {
711
                $date = str_replace('+0580', '+0530', $date);
712
            }
713
714
            $date = trim(rtrim($date));
715
            try {
716
                if(strpos($date, '&nbsp;') !== false){
717
                    $date = str_replace('&nbsp;', ' ', $date);
718
                }
719
                $parsed_date = Carbon::parse($date);
720
            } catch (\Exception $e) {
721
                switch (true) {
722
                    case preg_match('/([0-9]{4}\.[0-9]{1,2}\.[0-9]{1,2}\-[0-9]{1,2}\.[0-9]{1,2}.[0-9]{1,2})+$/i', $date) > 0:
723
                        $date = Carbon::createFromFormat("Y.m.d-H.i.s", $date);
724
                        break;
725
                    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:
726
                    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}\ UT)+$/i', $date) > 0:
727
                        $date .= 'C';
728
                        break;
729
                    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]{2,4}\ \(\+[0-9]{1,2}\))+$/i', $date) > 0:
730
                    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:
731
                    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:
732
                    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:
733
                    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:
734
                        $array = explode('(', $date);
735
                        $array = array_reverse($array);
736
                        $date = trim(array_pop($array));
737
                        break;
738
                }
739
                try {
740
                    $parsed_date = Carbon::parse($date);
741
                } catch (\Exception $_e) {
742
                    if (!isset($this->config["fallback_date"])) {
743
                        throw new InvalidMessageDateException("Invalid message date. ID:" . $this->get("message_id") . " Date:" . $header->date . "/" . $date, 1100, $e);
744
                    } else {
745
                        $parsed_date = Carbon::parse($this->config["fallback_date"]);
746
                    }
747
                }
748
            }
749
750
            $this->set("date", $parsed_date);
751
        }
752
    }
753
754
    /**
755
     * Get all available attributes
756
     *
757
     * @return array
758
     */
759
    public function getAttributes(): array {
760
        return $this->attributes;
761
    }
762
763
}
764