Passed
Push — master ( de50ce...27cb7e )
by Malte
02:12
created

Header   F

Complexity

Total Complexity 137

Size/Duplication

Total Lines 678
Duplicated Lines 0 %

Importance

Changes 15
Bugs 2 Features 6
Metric Value
wmc 137
eloc 303
c 15
b 2
f 6
dl 0
loc 678
rs 2

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 2 1
A get() 0 6 2
A parse() 0 25 6
A find() 0 9 4
F rfc822_parse_headers() 0 93 28
B set() 0 22 7
A __call() 0 11 3
B convertEncoding() 0 39 10
A getEncoding() 0 14 6
A notDecoded() 0 4 3
A mime_header_decode() 0 8 2
B findPriority() 0 24 7
A decodeAddresses() 0 23 4
C parseAddresses() 0 40 12
A is_uft8() 0 2 1
B extractHeaderExtensions() 0 26 8
A decodeArray() 0 5 2
C parseDate() 0 37 12
C decode() 0 40 14
A getAttributes() 0 2 1
A extractAddresses() 0 4 3

How to fix   Complexity   

Complex Class

Complex classes like Header 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 Header, and based on these observations, apply Extract Interface, too.

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

592
            $address->full = ($address->personal) ? $address->personal.' <'./** @scrutinizer ignore-type */ $address->mail.'>' : $address->mail;
Loading history...
593
594
            $addresses[] = new Address($address);
595
        }
596
597
        return $addresses;
598
    }
599
600
    /**
601
     * Search and extract potential header extensions
602
     */
603
    private function extractHeaderExtensions(){
604
        foreach ($this->attributes as $key => $value) {
605
            if (is_array($value)) {
606
                $value = implode(", ", $value);
607
            }else{
608
                $value = (string)$value;
609
            }
610
            // Only parse strings and don't parse any attributes like the user-agent
611
            if (in_array($key, ["user_agent"]) === false) {
612
                if (($pos = strpos($value, ";")) !== false){
613
                    $original = substr($value, 0, $pos);
614
                    $this->set($key, trim(rtrim($original)), true);
615
616
                    // Get all potential extensions
617
                    $extensions = explode(";", substr($value, $pos + 1));
618
                    foreach($extensions as $extension) {
619
                        if (($pos = strpos($extension, "=")) !== false){
620
                            $key = substr($extension, 0, $pos);
621
                            $key = trim(rtrim(strtolower($key)));
622
623
                            if (isset($this->attributes[$key]) === false) {
624
                                $value = substr($extension, $pos + 1);
625
                                $value = str_replace('"', "", $value);
626
                                $value = trim(rtrim($value));
627
628
                                $this->set($key, $value);
629
                            }
630
                        }
631
                    }
632
                }
633
            }
634
        }
635
    }
636
637
    /**
638
     * Exception handling for invalid dates
639
     *
640
     * Currently known invalid formats:
641
     * ^ Datetime                                   ^ Problem                           ^ Cause
642
     * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification     | A Windows feature
643
     * | Thu, 8 Nov 2018 08:54:58 -0200 (-02)       |
644
     * |                                            | and invalid timezone (max 6 char) |
645
     * | 04 Jan 2018 10:12:47 UT                    | Missing letter "C"                | Unknown
646
     * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
647
     * |                                            | mail server                       |
648
     * | Sat, 31 Aug 2013 20:08:23 +0580            | Invalid timezone                  | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
649
     *
650
     * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues)
651
     *
652
     * @param object $header
653
     *
654
     * @throws InvalidMessageDateException
655
     */
656
    private function parseDate($header) {
657
658
        if (property_exists($header, 'date')) {
659
            $parsed_date = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parsed_date is dead and can be removed.
Loading history...
660
            $date = $header->date;
661
662
            if(preg_match('/\+0580/', $date)) {
663
                $date = str_replace('+0580', '+0530', $date);
664
            }
665
666
            $date = trim(rtrim($date));
667
            try {
668
                $parsed_date = Carbon::parse($date);
669
            } catch (\Exception $e) {
670
                switch (true) {
671
                    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:
672
                    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:
673
                        $date .= 'C';
674
                        break;
675
                    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:
676
                    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:
677
                    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:
678
                    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:
679
                    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:
680
                        $array = explode('(', $date);
681
                        $array = array_reverse($array);
682
                        $date = trim(array_pop($array));
683
                        break;
684
                }
685
                try{
686
                    $parsed_date = Carbon::parse($date);
687
                } catch (\Exception $_e) {
688
                    throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
689
                }
690
            }
691
692
            $this->set("date", $parsed_date);
693
        }
694
    }
695
696
    /**
697
     * Get all available attributes
698
     *
699
     * @return array
700
     */
701
    public function getAttributes() {
702
        return $this->attributes;
703
    }
704
705
}
706