Completed
Push — master ( cc8b1f...bbfee2 )
by Malte
02:26
created

Header::decodeAddresses()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 17
c 3
b 1
f 1
dl 0
loc 23
rs 9.7
cc 4
nc 5
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[] $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
     * Header constructor.
57
     * @param $raw_header
58
     *
59
     * @throws InvalidMessageDateException
60
     */
61
    public function __construct($raw_header) {
62
        $this->raw = $raw_header;
63
        $this->config = ClientManager::get('options');
64
        $this->parse();
65
    }
66
67
    /**
68
     * Call dynamic attribute setter and getter methods
69
     * @param string $method
70
     * @param array $arguments
71
     *
72
     * @return Attribute
73
     * @throws MethodNotFoundException
74
     */
75
    public function __call($method, $arguments) {
76
        if(strtolower(substr($method, 0, 3)) === 'get') {
77
            $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3));
78
79
            if(in_array($name, array_keys($this->attributes))) {
80
                return $this->attributes[$name];
81
            }
82
83
        }
84
85
        throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
86
    }
87
88
    /**
89
     * Magic getter
90
     * @param $name
91
     *
92
     * @return Attribute|null
93
     */
94
    public function __get($name) {
95
        return $this->get($name);
96
    }
97
98
    /**
99
     * Get a specific header attribute
100
     * @param $name
101
     *
102
     * @return Attribute|null
103
     */
104
    public function get($name) {
105
        if(isset($this->attributes[$name])) {
106
            return $this->attributes[$name];
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * Set a specific attribute
114
     * @param string $name
115
     * @param array|mixed $value
116
     * @param boolean $strict
117
     *
118
     * @return Attribute
119
     */
120
    public function set($name, $value, $strict = false) {
121
        if(isset($this->attributes[$name]) && $strict === false) {
122
            $this->attributes[$name]->add($value, true);
123
        }else{
124
            $this->attributes[$name] = new Attribute($name, $value);
125
        }
126
127
        return $this->attributes[$name];
128
    }
129
130
    /**
131
     * Perform a regex match all on the raw header and return the first result
132
     * @param $pattern
133
     *
134
     * @return mixed|null
135
     */
136
    public function find($pattern) {
137
        if (preg_match_all($pattern, $this->raw, $matches)) {
138
            if (isset($matches[1])) {
139
                if(count($matches[1]) > 0) {
140
                    return $matches[1][0];
141
                }
142
            }
143
        }
144
        return null;
145
    }
146
147
    /**
148
     * Parse the raw headers
149
     *
150
     * @throws InvalidMessageDateException
151
     */
152
    protected function parse(){
153
        $header = $this->rfc822_parse_headers($this->raw);
154
155
        $this->extractAddresses($header);
156
157
        if (property_exists($header, 'subject')) {
158
            $this->set("subject", $this->decode($header->subject));
159
        }
160
        if (property_exists($header, 'references')) {
161
            $this->set("references", $this->decode($header->references));
162
        }
163
        if (property_exists($header, 'message_id')) {
164
            $this->set("message_id", str_replace(['<', '>'], '', $header->message_id));
165
        }
166
167
        $this->parseDate($header);
168
        foreach ($header as $key => $value) {
169
            $key = trim(rtrim(strtolower($key)));
170
            if(!isset($this->attributes[$key])){
171
                $this->set($key, $value);
172
            }
173
        }
174
175
        $this->extractHeaderExtensions();
176
        $this->findPriority();
177
    }
178
179
    /**
180
     * Parse mail headers from a string
181
     * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php
182
     * @param $raw_headers
183
     *
184
     * @return object
185
     */
186
    public function rfc822_parse_headers($raw_headers){
187
        $headers = [];
188
        $imap_headers = [];
189
        if (extension_loaded('imap')) {
190
            $raw_imap_headers = (array) \imap_rfc822_parse_headers($this->raw);
191
            foreach($raw_imap_headers as $key => $values) {
192
                $key = str_replace("-", "_", $key);
193
                $imap_headers[$key] = $values;
194
            }
195
        }
196
197
        $lines = explode("\r\n", $raw_headers);
198
        $prev_header = null;
199
        foreach($lines as $line) {
200
            if (substr($line, 0, 1) === "\n") {
201
                $line = substr($line, 1);
202
            }
203
204
            if (substr($line, 0, 1) === "\t") {
205
                $line = substr($line, 1);
206
                $line = trim(rtrim($line));
207
                if ($prev_header !== null) {
208
                    $headers[$prev_header][] = $line;
209
                }
210
            }elseif (substr($line, 0, 1) === " ") {
211
                $line = substr($line, 1);
212
                $line = trim(rtrim($line));
213
                if ($prev_header !== null) {
214
                    if (!isset($headers[$prev_header])) {
215
                        $headers[$prev_header] = "";
216
                    }
217
                    if (is_array($headers[$prev_header])) {
218
                        $headers[$prev_header][] = $line;
219
                    }else{
220
                        $headers[$prev_header] .= $line;
221
                    }
222
                }
223
            }else{
224
                if (($pos = strpos($line, ":")) > 0) {
225
                    $key = trim(rtrim(strtolower(substr($line, 0, $pos))));
226
                    $key = str_replace("-", "_", $key);
227
228
                    $value = trim(rtrim(substr($line, $pos + 1)));
229
                    if (isset($headers[$key])) {
230
                        $headers[$key][]  = $value;
231
                    }else{
232
                        $headers[$key] = [$value];
233
                    }
234
                    $prev_header = $key;
235
                }
236
            }
237
        }
238
239
        foreach($headers as $key => $values) {
240
            if (isset($imap_headers[$key])) continue;
241
            $value = null;
242
            switch($key){
243
                case 'from':
244
                case 'to':
245
                case 'cc':
246
                case 'bcc':
247
                case 'reply_to':
248
                case 'sender':
249
                    $value = $this->decodeAddresses($values);
250
                    $headers[$key."address"] = implode(", ", $values);
251
                    break;
252
                case 'subject':
253
                    $value = implode(" ", $values);
254
                    break;
255
                default:
256
                    if (is_array($values)) {
257
                        foreach($values as $k => $v) {
258
                            if ($v == "") {
259
                                unset($values[$k]);
260
                            }
261
                        }
262
                        $available_values = count($values);
263
                        if ($available_values === 1) {
264
                            $value = array_pop($values);
265
                        } elseif ($available_values === 2) {
266
                            $value = implode(" ", $values);
267
                        } elseif ($available_values > 2) {
268
                            $value = array_values($values);
269
                        } else {
270
                            $value = "";
271
                        }
272
                    }
273
                    break;
274
            }
275
            $headers[$key] = $value;
276
        }
277
278
        return (object) array_merge($headers, $imap_headers);
279
    }
280
281
    /**
282
     * Decode MIME header elements
283
     * @link https://php.net/manual/en/function.imap-mime-header-decode.php
284
     * @param string $text The MIME text
285
     *
286
     * @return array The decoded elements are returned in an array of objects, where each
287
     * object has two properties, charset and text.
288
     */
289
    public function mime_header_decode($text){
290
        if (extension_loaded('imap')) {
291
            return \imap_mime_header_decode($text);
292
        }
293
        $charset = $this->getEncoding($text);
294
        return [(object)[
295
            "charset" => $charset,
296
            "text" => $this->convertEncoding($text, $charset)
297
        ]];
298
    }
299
300
    /**
301
     * Check if a given pair of strings has ben decoded
302
     * @param $encoded
303
     * @param $decoded
304
     *
305
     * @return bool
306
     */
307
    private function notDecoded($encoded, $decoded) {
308
        return 0 === strpos($decoded, '=?')
309
            && strlen($decoded) - 2 === strpos($decoded, '?=')
310
            && false !== strpos($encoded, $decoded);
311
    }
312
313
    /**
314
     * Convert the encoding
315
     * @param $str
316
     * @param string $from
317
     * @param string $to
318
     *
319
     * @return mixed|string
320
     */
321
    public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") {
322
323
        $from = EncodingAliases::get($from, $this->fallback_encoding);
324
        $to = EncodingAliases::get($to, $this->fallback_encoding);
325
326
        if ($from === $to) {
327
            return $str;
328
        }
329
330
        // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
331
        //     ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
332
        //     https://stackoverflow.com/a/11303410
333
        //
334
        // us-ascii is the same as ASCII:
335
        //     ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
336
        //     prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
337
        //     based on the typographical symbols predominantly in use there.
338
        //     https://en.wikipedia.org/wiki/ASCII
339
        //
340
        // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
341
        if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') {
342
            return $str;
343
        }
344
345
        try {
346
            if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') {
347
                return iconv($from, $to, $str);
348
            } else {
349
                if (!$from) {
350
                    return mb_convert_encoding($str, $to);
351
                }
352
                return mb_convert_encoding($str, $to, $from);
353
            }
354
        } catch (\Exception $e) {
355
            if (strstr($from, '-')) {
356
                $from = str_replace('-', '', $from);
357
                return $this->convertEncoding($str, $from, $to);
358
            } else {
359
                return $str;
360
            }
361
        }
362
    }
363
364
    /**
365
     * Get the encoding of a given abject
366
     * @param object|string $structure
367
     *
368
     * @return string
369
     */
370
    public function getEncoding($structure) {
371
        if (property_exists($structure, 'parameters')) {
372
            foreach ($structure->parameters as $parameter) {
373
                if (strtolower($parameter->attribute) == "charset") {
374
                    return EncodingAliases::get($parameter->value, $this->fallback_encoding);
375
                }
376
            }
377
        }elseif (property_exists($structure, 'charset')) {
378
            return EncodingAliases::get($structure->charset, $this->fallback_encoding);
379
        }elseif (is_string($structure) === true){
380
            return mb_detect_encoding($structure);
381
        }
382
383
        return $this->fallback_encoding;
384
    }
385
386
    /**
387
     * Try to decode a specific header
388
     * @param mixed $value
389
     *
390
     * @return mixed
391
     */
392
    private function decode($value) {
393
        if (is_array($value)) {
394
            return $this->decodeArray($value);
395
        }
396
        $original_value = $value;
397
        $decoder = $this->config['decoder']['message'];
398
399
        if ($value !== null) {
400
            if($decoder === 'utf-8' && extension_loaded('imap')) {
401
                $value = \imap_utf8($value);
402
                if (strpos(strtolower($value), '=?utf-8?') === 0) {
403
                    $value = mb_decode_mimeheader($value);
404
                }
405
                if ($this->notDecoded($original_value, $value)) {
406
                    $decoded_value = $this->mime_header_decode($value);
407
                    if (count($decoded_value) > 0) {
408
                        if(property_exists($decoded_value[0], "text")) {
409
                            $value = $decoded_value[0]->text;
410
                        }
411
                    }
412
                }
413
            }elseif($decoder === 'iconv') {
414
                $value = iconv_mime_decode($value);
415
            }else{
416
                $value = mb_decode_mimeheader($value);
417
            }
418
419
            if (strpos(strtolower($value), '=?utf-8?') === 0) {
420
                $value = mb_decode_mimeheader($value);
421
            }
422
423
            if ($this->notDecoded($original_value, $value)) {
424
                $value = $this->convertEncoding($original_value, $this->getEncoding($original_value));
425
            }
426
        }
427
428
        return $value;
429
    }
430
431
    /**
432
     * Decode a given array
433
     * @param array $values
434
     *
435
     * @return array
436
     */
437
    private function decodeArray($values) {
438
        foreach($values as $key => $value) {
439
            $values[$key] = $this->decode($value);
440
        }
441
        return $values;
442
    }
443
444
    /**
445
     * Try to extract the priority from a given raw header string
446
     */
447
    private function findPriority() {
448
        if(($priority = $this->get("x_priority")) === null) return;
449
        switch((int)"$priority"){
450
            case IMAP::MESSAGE_PRIORITY_HIGHEST;
451
                $priority = IMAP::MESSAGE_PRIORITY_HIGHEST;
452
                break;
453
            case IMAP::MESSAGE_PRIORITY_HIGH;
454
                $priority = IMAP::MESSAGE_PRIORITY_HIGH;
455
                break;
456
            case IMAP::MESSAGE_PRIORITY_NORMAL;
457
                $priority = IMAP::MESSAGE_PRIORITY_NORMAL;
458
                break;
459
            case IMAP::MESSAGE_PRIORITY_LOW;
460
                $priority = IMAP::MESSAGE_PRIORITY_LOW;
461
                break;
462
            case IMAP::MESSAGE_PRIORITY_LOWEST;
463
                $priority = IMAP::MESSAGE_PRIORITY_LOWEST;
464
                break;
465
            default:
466
                $priority = IMAP::MESSAGE_PRIORITY_UNKNOWN;
467
                break;
468
        }
469
470
        $this->set("priority", $priority);
471
    }
472
473
    /**
474
     * Extract a given part as address array from a given header
475
     * @param $values
476
     *
477
     * @return array
478
     */
479
    private function decodeAddresses($values) {
480
        $addresses = [];
481
        foreach($values as $address) {
482
            $address = trim(rtrim($address));
483
            if (strpos($address, ",") == strlen($address) - 1) {
484
                $address = substr($address, 0, -1);
485
            }
486
            if (preg_match(
487
                '/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
488
                $address,
489
                $matches
490
            )){
491
                $name = trim(rtrim($matches["name"]));
492
                $email = trim(rtrim($matches["email"]));
493
                list($mailbox, $host) = array_pad(explode("@", $email), 2, null);
494
                $addresses[] = (object) [
495
                    "personal" => $name,
496
                    "mailbox" => $mailbox,
497
                    "host" => $host,
498
                ];
499
            }
500
        }
501
        return $addresses;
502
    }
503
504
    /**
505
     * Extract a given part as address array from a given header
506
     * @param object $header
507
     */
508
    private function extractAddresses($header) {
509
        foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){
510
            if (property_exists($header, $key)) {
511
                $this->set($key, $this->parseAddresses($header->$key));
512
            }
513
        }
514
    }
515
516
    /**
517
     * Parse Addresses
518
     * @param $list
519
     *
520
     * @return array
521
     */
522
    private function parseAddresses($list) {
523
        $addresses = [];
524
525
        if (is_array($list) === false) {
526
            return $addresses;
527
        }
528
529
        foreach ($list as $item) {
530
            $address = (object) $item;
531
532
            if (!property_exists($address, 'mailbox')) {
533
                $address->mailbox = false;
534
            }
535
            if (!property_exists($address, 'host')) {
536
                $address->host = false;
537
            }
538
            if (!property_exists($address, 'personal')) {
539
                $address->personal = false;
540
            } else {
541
                $personalParts = $this->mime_header_decode($address->personal);
542
543
                if(is_array($personalParts)) {
544
                    $address->personal = '';
545
                    foreach ($personalParts as $p) {
546
                        $address->personal .= $this->convertEncoding($p->text, $this->getEncoding($p));
547
                    }
548
                }
549
550
                if (strpos($address->personal, "'") === 0) {
551
                    $address->personal = str_replace("'", "", $address->personal);
552
                }
553
            }
554
555
            $address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false;
556
            $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

556
            $address->full = ($address->personal) ? $address->personal.' <'./** @scrutinizer ignore-type */ $address->mail.'>' : $address->mail;
Loading history...
557
558
            $addresses[] = new Address($address);
559
        }
560
561
        return $addresses;
562
    }
563
564
    /**
565
     * Search and extract potential header extensions
566
     */
567
    private function extractHeaderExtensions(){
568
        foreach ($this->attributes as $key => $value) {
569
            $value = (string)$value;
570
            // Only parse strings and don't parse any attributes like the user-agent
571
            if (in_array($key, ["user_agent"]) === false) {
572
                if (($pos = strpos($value, ";")) !== false){
573
                    $original = substr($value, 0, $pos);
574
                    $this->set($key, trim(rtrim($original)), true);
575
576
                    // Get all potential extensions
577
                    $extensions = explode(";", substr($value, $pos + 1));
578
                    foreach($extensions as $extension) {
579
                        if (($pos = strpos($extension, "=")) !== false){
580
                            $key = substr($extension, 0, $pos);
581
                            $key = trim(rtrim(strtolower($key)));
582
583
                            if (isset($this->attributes[$key]) === false) {
584
                                $value = substr($extension, $pos + 1);
585
                                $value = str_replace('"', "", $value);
586
                                $value = trim(rtrim($value));
587
588
                                $this->set($key, $value);
589
                            }
590
                        }
591
                    }
592
                }
593
            }
594
        }
595
    }
596
597
    /**
598
     * Exception handling for invalid dates
599
     *
600
     * Currently known invalid formats:
601
     * ^ Datetime                                   ^ Problem                           ^ Cause
602
     * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification     | A Windows feature
603
     * | Thu, 8 Nov 2018 08:54:58 -0200 (-02)       |
604
     * |                                            | and invalid timezone (max 6 char) |
605
     * | 04 Jan 2018 10:12:47 UT                    | Missing letter "C"                | Unknown
606
     * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
607
     * |                                            | mail server                       |
608
     * | Sat, 31 Aug 2013 20:08:23 +0580            | Invalid timezone                  | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
609
     *
610
     * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues)
611
     *
612
     * @param object $header
613
     *
614
     * @throws InvalidMessageDateException
615
     */
616
    private function parseDate($header) {
617
618
        if (property_exists($header, 'date')) {
619
            $parsed_date = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parsed_date is dead and can be removed.
Loading history...
620
            $date = $header->date;
621
622
            if(preg_match('/\+0580/', $date)) {
623
                $date = str_replace('+0580', '+0530', $date);
624
            }
625
626
            $date = trim(rtrim($date));
627
            try {
628
                $parsed_date = Carbon::parse($date);
629
            } catch (\Exception $e) {
630
                switch (true) {
631
                    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:
632
                    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:
633
                        $date .= 'C';
634
                        break;
635
                    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:
636
                    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:
637
                    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:
638
                    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:
639
                    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:
640
                        $array = explode('(', $date);
641
                        $array = array_reverse($array);
642
                        $date = trim(array_pop($array));
643
                        break;
644
                }
645
                try{
646
                    $parsed_date = Carbon::parse($date);
647
                } catch (\Exception $_e) {
648
                    throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
649
                }
650
            }
651
652
            $this->set("date", $parsed_date);
653
        }
654
    }
655
656
    /**
657
     * Get all available attributes
658
     *
659
     * @return array
660
     */
661
    public function getAttributes() {
662
        return $this->attributes;
663
    }
664
665
}
666