Passed
Push — master ( 9b186e...d00b04 )
by Malte
01:55
created

Header::decodeArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

523
            $address->full = ($address->personal) ? $address->personal.' <'./** @scrutinizer ignore-type */ $address->mail.'>' : $address->mail;
Loading history...
524
525
            $addresses[] = $address;
526
        }
527
528
        return $addresses;
529
    }
530
531
    /**
532
     * Search and extract potential header extensions
533
     */
534
    private function extractHeaderExtensions(){
535
        foreach ($this->attributes as $key => $value) {
536
            // Only parse strings and don't parse any attributes like the user-agent
537
            if (is_string($value) === true && in_array($key, ["user-agent"]) === false) {
538
                if (($pos = strpos($value, ";")) !== false){
539
                    $original = substr($value, 0, $pos);
540
                    $this->attributes[$key] = trim(rtrim($original));
541
542
                    // Get all potential extensions
543
                    $extensions = explode(";", substr($value, $pos + 1));
544
                    foreach($extensions as $extension) {
545
                        if (($pos = strpos($extension, "=")) !== false){
546
                            $key = substr($extension, 0, $pos);
547
                            $key = trim(rtrim(strtolower($key)));
548
549
                            if (isset($this->attributes[$key]) === false) {
550
                                $value = substr($extension, $pos + 1);
551
                                $value = str_replace('"', "", $value);
552
                                $value = trim(rtrim($value));
553
554
                                $this->attributes[$key] = $value;
555
                            }
556
                        }
557
                    }
558
                }
559
            }
560
        }
561
    }
562
563
    /**
564
     * Exception handling for invalid dates
565
     *
566
     * Currently known invalid formats:
567
     * ^ Datetime                                   ^ Problem                           ^ Cause
568
     * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification     | A Windows feature
569
     * | Thu, 8 Nov 2018 08:54:58 -0200 (-02)       |
570
     * |                                            | and invalid timezone (max 6 char) |
571
     * | 04 Jan 2018 10:12:47 UT                    | Missing letter "C"                | Unknown
572
     * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
573
     * |                                            | mail server                       |
574
     * | Sat, 31 Aug 2013 20:08:23 +0580            | Invalid timezone                  | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
575
     *
576
     * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues)
577
     *
578
     * @param object $header
579
     *
580
     * @throws InvalidMessageDateException
581
     */
582
    private function parseDate($header) {
583
584
        if (property_exists($header, 'date')) {
585
            $parsed_date = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parsed_date is dead and can be removed.
Loading history...
586
            $date = $header->date;
587
588
            if(preg_match('/\+0580/', $date)) {
589
                $date = str_replace('+0580', '+0530', $date);
590
            }
591
592
            $date = trim(rtrim($date));
593
            try {
594
                $parsed_date = Carbon::parse($date);
595
            } catch (\Exception $e) {
596
                switch (true) {
597
                    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:
598
                    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:
599
                        $date .= 'C';
600
                        break;
601
                    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:
602
                    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:
603
                    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:
604
                    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:
605
                    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:
606
                        $array = explode('(', $date);
607
                        $array = array_reverse($array);
608
                        $date = trim(array_pop($array));
609
                        break;
610
                }
611
                try{
612
                    $parsed_date = Carbon::parse($date);
613
                } catch (\Exception $_e) {
614
                    throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
615
                }
616
            }
617
618
            $this->attributes["date"] = $parsed_date;
619
        }
620
    }
621
622
    /**
623
     * Get all available attributes
624
     *
625
     * @return array
626
     */
627
    public function getAttributes() {
628
        return $this->attributes;
629
    }
630
631
}
632