1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
6
|
|
|
*/ |
7
|
|
|
namespace ZBateson\MailMimeParser\Header\Part; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Stream\Helper\CharsetConverter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents a single mime header part token, with the possibility of it being |
13
|
|
|
* MIME-Encoded as per RFC-2047. |
14
|
|
|
* |
15
|
|
|
* MimeLiteralPart automatically decodes the value if it's encoded. |
16
|
|
|
* |
17
|
|
|
* @author Zaahid Bateson |
18
|
|
|
*/ |
19
|
|
|
class MimeLiteralPart extends LiteralPart |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string regex pattern matching a mime-encoded part |
23
|
|
|
*/ |
24
|
|
|
const MIME_PART_PATTERN = '=\?[A-Za-z\-_0-9]+\?[QBqb]\?[^\?]+\?='; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool set to true to ignore spaces before this part |
28
|
|
|
*/ |
29
|
|
|
protected $canIgnoreSpacesBefore = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool set to true to ignore spaces after this part |
33
|
|
|
*/ |
34
|
|
|
protected $canIgnoreSpacesAfter = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Decoding the passed token value if it's mime-encoded and assigns the |
38
|
|
|
* decoded value to a member variable. Sets canIgnoreSpacesBefore and |
39
|
|
|
* canIgnoreSpacesAfter. |
40
|
|
|
* |
41
|
|
|
* @param string $token |
42
|
|
|
*/ |
43
|
7 |
|
public function __construct($token) |
44
|
|
|
{ |
45
|
7 |
|
$this->value = $this->decodeMime($token); |
46
|
|
|
// preg_match returns int |
47
|
7 |
|
$pattern = self::MIME_PART_PATTERN; |
48
|
7 |
|
$this->canIgnoreSpacesBefore = (bool) preg_match("/^\s*{$pattern}/", $token); |
49
|
7 |
|
$this->canIgnoreSpacesAfter = (bool) preg_match("/{$pattern}\s*\$/", $token); |
50
|
7 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Finds and replaces mime parts with their values. |
54
|
|
|
* |
55
|
|
|
* The method splits the token value into an array on mime-part-patterns, |
56
|
|
|
* either replacing a mime part with its value by calling iconv_mime_decode |
57
|
|
|
* or converts the encoding on the text part by calling convertEncoding. |
58
|
|
|
* |
59
|
|
|
* @param string $value |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
7 |
|
protected function decodeMime($value) |
63
|
|
|
{ |
64
|
7 |
|
$pattern = self::MIME_PART_PATTERN; |
65
|
7 |
|
$value = preg_replace("/($pattern)\\s+(?=$pattern)/", '$1', $value); |
66
|
7 |
|
$aMimeParts = preg_split("/($pattern)/", $value, -1, PREG_SPLIT_DELIM_CAPTURE); |
67
|
7 |
|
$ret = ''; |
68
|
7 |
|
foreach ($aMimeParts as $entity) { |
69
|
7 |
|
$ret .= $this->decodeMatchedEntity($entity); |
70
|
7 |
|
} |
71
|
7 |
|
return $ret; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Decodes a single mime-encoded entity. |
76
|
|
|
* |
77
|
|
|
* Attempts to detect mb/iconv charset support, normalize the charset name, |
78
|
|
|
* and decode the body before returning it, by calling either |
79
|
|
|
* mb_decode_mimeheader for an mb-supported charset, or iconv_mime_decode |
80
|
|
|
* otherwise. |
81
|
|
|
* |
82
|
|
|
* @param string $entity |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
7 |
|
private function decodeMatchedEntity($entity) |
86
|
|
|
{ |
87
|
7 |
|
if (preg_match("/^=\?([A-Za-z\-_0-9]+)\?([QBqb])\?([^\?]+\?=)$/", $entity, $matches)) { |
|
|
|
|
88
|
6 |
|
$body = $matches[3]; |
89
|
6 |
|
if (strtoupper($matches[2]) === 'Q') { |
90
|
6 |
|
$body = preg_replace_callback('/=[0-9a-f]{2}/i', function($val) { |
91
|
1 |
|
return strtoupper($val[0]); |
92
|
6 |
|
}, $body); |
93
|
6 |
|
} |
94
|
6 |
|
$mbSupported = false; |
95
|
6 |
|
$charset = CharsetConverter::findSupportedCharset($matches[1], $mbSupported); |
96
|
6 |
|
$normalized = '=?' . $charset . '?' . $matches[2] . '?' |
97
|
6 |
|
. str_replace('_', '=20', $body); |
98
|
6 |
|
if ($mbSupported) { |
99
|
6 |
|
return mb_decode_mimeheader($normalized); |
100
|
|
|
} |
101
|
1 |
|
return iconv_mime_decode($normalized, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8'); |
102
|
|
|
} |
103
|
7 |
|
return $this->convertEncoding($entity); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns true if spaces before this part should be ignored. |
108
|
|
|
* |
109
|
|
|
* Overridden to return $this->canIgnoreSpacesBefore which is setup in the |
110
|
|
|
* constructor. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
3 |
|
public function ignoreSpacesBefore() |
115
|
|
|
{ |
116
|
3 |
|
return $this->canIgnoreSpacesBefore; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns true if spaces before this part should be ignored. |
121
|
|
|
* |
122
|
|
|
* Overridden to return $this->canIgnoreSpacesAfter which is setup in the |
123
|
|
|
* constructor. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
3 |
|
public function ignoreSpacesAfter() |
128
|
|
|
{ |
129
|
3 |
|
return $this->canIgnoreSpacesAfter; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.