Passed
Push — master ( 5127af...662963 )
by Malte
02:15
created

Header::extractHeaderExtensions()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

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