1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Laminas\Mail\Header;
use Laminas\Mail\Headers;
use Laminas\Mime\Mime;
/**
* Utility class used for creating wrapped or MIME-encoded versions of header
* values.
*/
abstract class HeaderWrap
{
/**
* Wrap a long header line
*
* @param string $value
* @param HeaderInterface $header
* @return string
*/
public static function wrap($value, HeaderInterface $header)
{
if ($header instanceof UnstructuredInterface) {
return static::wrapUnstructuredHeader($value, $header);
} elseif ($header instanceof StructuredInterface) {
return static::wrapStructuredHeader($value, $header);
}
return $value;
}
/**
* Wrap an unstructured header line
*
* Wrap at 78 characters or before, based on whitespace.
*
* @param string $value
* @param HeaderInterface $header
* @return string
*/
protected static function wrapUnstructuredHeader($value, HeaderInterface $header)
{
$encoding = $header->getEncoding();
if ($encoding == 'ASCII') {
return wordwrap($value, 78, Headers::FOLDING);
}
return static::mimeEncodeValue($value, $encoding, 78);
}
/**
* Wrap a structured header line
*
* @param string $value
* @param StructuredInterface $header
* @return string
*/
protected static function wrapStructuredHeader($value, StructuredInterface $header)
{
$delimiter = $header->getDelimiter();
$length = strlen($value);
$lines = [];
$temp = '';
for ($i = 0; $i < $length; $i++) {
$temp .= $value[$i];
if ($value[$i] == $delimiter) {
$lines[] = $temp;
$temp = '';
}
}
return implode(Headers::FOLDING, $lines);
}
/**
* MIME-encode a value
*
* Performs quoted-printable encoding on a value, setting maximum
* line-length to 998.
*
* @param string $value
* @param string $encoding
* @param int $lineLength maximum line-length, by default 998
* @return string Returns the mime encode value without the last line ending
*/
public static function mimeEncodeValue($value, $encoding, $lineLength = 998)
{
return Mime::encodeQuotedPrintableHeader($value, $encoding, $lineLength, Headers::EOL);
}
/**
* MIME-decode a value
*
* Performs quoted-printable decoding on a value.
*
* @param string $value
* @return string Returns the mime encode value without the last line ending
*/
public static function mimeDecodeValue($value)
{
// unfold first, because iconv_mime_decode is discarding "\n" with no apparent reason
// making the resulting value no longer valid.
// see https://tools.ietf.org/html/rfc2822#section-2.2.3 about unfolding
$parts = explode(Headers::FOLDING, $value);
$value = implode(' ', $parts);
$decodedValue = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
// imap (unlike iconv) can handle multibyte headers which are splitted across multiple line
if (self::isNotDecoded($value, $decodedValue) && extension_loaded('imap')) {
return array_reduce(
imap_mime_header_decode(imap_utf8($value)),
function ($accumulator, $headerPart) {
return $accumulator . $headerPart->text;
},
''
);
}
return $decodedValue;
}
private static function isNotDecoded($originalValue, $value)
{
return 0 === strpos($value, '=?')
&& strlen($value) - 2 === strpos($value, '?=')
&& false !== strpos($originalValue, $value);
}
/**
* Test if is possible apply MIME-encoding
*
* @param string $value
* @return bool
*/
public static function canBeEncoded($value)
{
// avoid any wrapping by specifying line length long enough
// "test" -> 4
// "x-test: =?ISO-8859-1?B?dGVzdA==?=" -> 33
// 8 +2 +3 +3 -> 16
$charset = 'UTF-8';
$lineLength = strlen($value) * 4 + strlen($charset) + 16;
$preferences = [
'scheme' => 'Q',
'input-charset' => $charset,
'output-charset' => $charset,
'line-length' => $lineLength,
];
$encoded = iconv_mime_encode('x-test', $value, $preferences);
return (false !== $encoded);
}
}