Completed
Push — master ( 09eb28...f7ce01 )
by Malte
02:12
created

Header::parse()   B

Complexity

Conditions 8
Paths 72

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 28
rs 8.4444
cc 8
nc 72
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 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(strtolower(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 $value
364
     *
365
     * @return string|null
366
     */
367
    private function decode($value) {
368
        $original_value = $value;
369
        $decoder = $this->config['decoder']['message'];
370
371
        if ($value !== null) {
372
            if($decoder === 'utf-8' && extension_loaded('imap')) {
373
                $value = \imap_utf8($value);
374
                if (strpos(strtolower($value), '=?utf-8?') === 0) {
375
                    $value = mb_decode_mimeheader($value);
376
                }
377
                if ($this->notDecoded($original_value, $value)) {
378
                    $decoded_value = $this->mime_header_decode($value);
379
                    if (count($decoded_value) > 0) {
380
                        if(property_exists($decoded_value[0], "text")) {
381
                            $value = $decoded_value[0]->text;
382
                        }
383
                    }
384
                }
385
            }elseif($decoder === 'iconv') {
386
                $value = iconv_mime_decode($value);
387
            }else{
388
                $value = mb_decode_mimeheader($value);
389
            }
390
391
            if (strpos(strtolower($value), '=?utf-8?') === 0) {
392
                $value = mb_decode_mimeheader($value);
393
            }
394
395
            if ($this->notDecoded($original_value, $value)) {
396
                $value = $this->convertEncoding($original_value, $this->getEncoding($original_value));
397
            }
398
        }
399
400
        return $value;
401
    }
402
403
    /**
404
     * Try to extract the priority from a given raw header string
405
     */
406
    private function findPriority() {
407
        if(($priority = $this->get("x-priority")) === null) return;
408
        switch($priority){
409
            case IMAP::MESSAGE_PRIORITY_HIGHEST;
410
                $priority = IMAP::MESSAGE_PRIORITY_HIGHEST;
411
                break;
412
            case IMAP::MESSAGE_PRIORITY_HIGH;
413
                $priority = IMAP::MESSAGE_PRIORITY_HIGH;
414
                break;
415
            case IMAP::MESSAGE_PRIORITY_NORMAL;
416
                $priority = IMAP::MESSAGE_PRIORITY_NORMAL;
417
                break;
418
            case IMAP::MESSAGE_PRIORITY_LOW;
419
                $priority = IMAP::MESSAGE_PRIORITY_LOW;
420
                break;
421
            case IMAP::MESSAGE_PRIORITY_LOWEST;
422
                $priority = IMAP::MESSAGE_PRIORITY_LOWEST;
423
                break;
424
            default:
425
                $priority = IMAP::MESSAGE_PRIORITY_UNKNOWN;
426
                break;
427
        }
428
429
        $this->attributes["priority"] = $priority;
430
    }
431
432
    /**
433
     * Extract a given part as address array from a given header
434
     * @param $values
435
     *
436
     * @return array
437
     */
438
    private function decodeAddresses($values) {
439
        $addresses = [];
440
        foreach($values as $address) {
441
            if (preg_match(
442
                '/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
443
                $address,
444
                $matches
445
            )){
446
                $name = trim(rtrim($matches["name"]));
447
                $email = trim(rtrim($matches["email"]));
448
                list($mailbox, $host) = array_pad(explode("@", $email), 2, null);
449
                $addresses[] = (object) [
450
                    "personal" => $name,
451
                    "mailbox" => $mailbox,
452
                    "host" => $host,
453
                ];
454
            }
455
        }
456
        return $addresses;
457
    }
458
459
    /**
460
     * Extract a given part as address array from a given header
461
     * @param object $header
462
     */
463
    private function extractAddresses($header) {
464
        foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){
465
            if (property_exists($header, $key)) {
466
                $this->attributes[$key] = $this->parseAddresses($header->$key);
467
            }
468
        }
469
    }
470
471
    /**
472
     * Parse Addresses
473
     * @param $list
474
     *
475
     * @return array
476
     */
477
    private function parseAddresses($list) {
478
        $addresses = [];
479
480
        if (is_array($list) === false) {
481
            return $addresses;
482
        }
483
484
        foreach ($list as $item) {
485
            $address = (object) $item;
486
487
            if (!property_exists($address, 'mailbox')) {
488
                $address->mailbox = false;
489
            }
490
            if (!property_exists($address, 'host')) {
491
                $address->host = false;
492
            }
493
            if (!property_exists($address, 'personal')) {
494
                $address->personal = false;
495
            } else {
496
                $personalParts = $this->mime_header_decode($address->personal);
497
498
                if(is_array($personalParts)) {
499
                    $address->personal = '';
500
                    foreach ($personalParts as $p) {
501
                        $address->personal .= $this->convertEncoding($p->text, $this->getEncoding($p));
502
                    }
503
                }
504
            }
505
506
            $address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false;
507
            $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

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