Passed
Pull Request — master (#159)
by
unknown
04:38 queued 01:50
created

Header::getBoundary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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