Passed
Push — master ( ab922f...5941ce )
by Malte
02:27
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 $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')) {
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
245
        $lines = explode("\r\n", $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($text){
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) {
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) {
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) {
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($values) {
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) {
541
        $addresses = [];
542
        foreach($values as $address) {
543
            $address = trim(rtrim($address));
544
            if (strpos($address, ",") == strlen($address) - 1) {
545
                $address = substr($address, 0, -1);
546
            }
547
            if (preg_match(
548
                '/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
549
                $address,
550
                $matches
551
            )){
552
                $name = trim(rtrim($matches["name"]));
553
                $email = trim(rtrim($matches["email"]));
554
                list($mailbox, $host) = array_pad(explode("@", $email), 2, null);
555
                $addresses[] = (object) [
556
                    "personal" => $name,
557
                    "mailbox" => $mailbox,
558
                    "host" => $host,
559
                ];
560
            }
561
        }
562
        return $addresses;
563
    }
564
565
    /**
566
     * Extract a given part as address array from a given header
567
     * @param object $header
568
     */
569
    private function extractAddresses($header) {
570
        foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){
571
            if (property_exists($header, $key)) {
572
                $this->set($key, $this->parseAddresses($header->$key));
573
            }
574
        }
575
    }
576
577
    /**
578
     * Parse Addresses
579
     * @param $list
580
     *
581
     * @return array
582
     */
583
    private function parseAddresses($list) {
584
        $addresses = [];
585
586
        if (is_array($list) === false) {
587
            return $addresses;
588
        }
589
590
        foreach ($list as $item) {
591
            $address = (object) $item;
592
593
            if (!property_exists($address, 'mailbox')) {
594
                $address->mailbox = false;
595
            }
596
            if (!property_exists($address, 'host')) {
597
                $address->host = false;
598
            }
599
            if (!property_exists($address, 'personal')) {
600
                $address->personal = false;
601
            } else {
602
                $personalParts = $this->mime_header_decode($address->personal);
603
604
                if(is_array($personalParts)) {
605
                    $address->personal = '';
606
                    foreach ($personalParts as $p) {
607
                        $address->personal .= $this->convertEncoding($p->text, $this->getEncoding($p));
608
                    }
609
                }
610
611
                if (strpos($address->personal, "'") === 0) {
612
                    $address->personal = str_replace("'", "", $address->personal);
613
                }
614
            }
615
616
            $address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false;
617
            $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

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