-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathrfc2047-mime.php
More file actions
336 lines (297 loc) · 10.6 KB
/
rfc2047-mime.php
File metadata and controls
336 lines (297 loc) · 10.6 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* Implements the decoder from RFC 2047:
* MIME Part 3: Message Header Extensions for Non-ASCII Text.
*
* This module contains decoding functions for supported MIME
* encodings as are used with email servers which don’t support
* or haven’t activated UTF-8 support.
*
* @see https://www.rfc-editor.org/rfc/rfc2047
*
* @package WordPress
* @subpackage rfc2044
*/
/**
* Decodes text potentially containing RFC2047 MIME encoded words.
* Returns decoded text as UTF-8, if supported, else `null`.
*
* Example:
*
* // Quoted forms have non-printable ASCII encoded as octets.
* 'this is some text' === wp_decode_rfc2047( '=?iso-8859-1?q?this=20is_some=20text?=' );
* '👌' === wp_decode_rfc2047( '=?utf-8?q?=F0=9F=91=8C?=' );
*
* // Binary forms are base64-encoded.
* '👌' === wp_decode_rfc2047( '=?utf-8?B??=8J+RjA==?=' );
* 'םולש ןב ילטפנ' === wp_decode_rfc2047( '=?iso-8859-8?b?7eXs+SDv4SDp7Oj08A==?=' );
*
* // Character sets are re-encoded into UTF-8
* '100¥' === wp_decode_rfc2047( '=?iso-8859-1?Q?500=A5?=' );
* '🏴' === wp_decode_rfc2047( '=?GB-18030?Q?=949=C82=D36=A01=D36=9F6=D36=9F9=D36=A08=D36=A01=D36=A25?=' );
*
* // Linear white-space is collapsed.
* 'ab c d e' === wp_decode_rfc2047( '=?ASCII?Q?a?= =?ASCII?Q?b?= c d=?ASCII?Q?=20?==?ASCII?Q?e?=' )
*
* // Error-handling is up to the call site.
* '=?UTF-8?Q?=6f?=' === wp_decode_rfc2047( '=?UTF-8?Q?=6f?=' );
* '=?UTF-8?Q?=6f?=' === wp_decode_rfc2047( '=?UTF-8?Q?=6f?=', 'preserve-errors' );
* '�' === wp_decode_rfc2047( '=?UTF-8?Q?=6f?=', 'replace-errors' );
* null === wp_decode_rfc2047( '=?UTF-8?Q?=6f?=', 'bail-on-error' );
*
* // Invalid character encodings are errors.
* null === wp_decode_rfc2047( '=?UTF-8?Q?=C0?=', 'bail-on-error' );
*
* @see https://www.rfc-editor.org/rfc/rfc2047
*
* @since {WP_VERSION}
*
* @param string $encoded US-ASCII text potentially containing MIME encoded words.
* @param ?('preserve-errors'|'replace-errors'|'bail-on-error') $errors Optional. How to handle invalid encoded words.
* Default is to preserve invalid encoded words as plaintext.
* @return string Decoded string in UTF-8, if supported, else `null`.
*/
function wp_decode_rfc2047( $encoded, $errors = 'preserve-errors' ) {
/**
* {@see iconv_mime_decode()} which does not give control over error-handling
* at the granularity necessary for this decoder.
*/
$decoded = '';
$end = strlen( $encoded );
$at = 0;
$was_at = 0;
set_error_handler(
static function ( $errno, $errstr ) {
if (
str_starts_with( $errstr, 'mb_convert_encoding():' ) ||
str_starts_with( $errstr, 'iconv():' )
) {
throw new Error( $errstr );
}
return false;
},
E_WARNING
);
while ( $at < $end ) {
$encoded_word_at = strpos( $encoded, '=?', $at );
if ( $encoded_word_at === false ) {
break;
}
/*
* > charset = token
* > token = 1*<Any CHAR except SPACE, CTLs, and especials>
* > especials = "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / <"> / "/" / "[" / "]" / "?" / "." / "="
* > CHAR = %00–%7F
* > CTL = %00–%1F
* > SPACE = %20
*/
$charset_at = $encoded_word_at + 2;
$charset_length = strspn( $encoded, "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz{|}~", $charset_at );
if ( $charset_length < 1 ) {
$at = $charset_at;
continue;
}
$after_charset = $charset_at + $charset_length;
if ( $after_charset >= $end || '?' !== $encoded[ $after_charset ] ) {
$at = $after_charset;
continue;
}
$encoding_at = $after_charset + 1;
$encoding_length = strspn( $encoded, "!#$%&'*+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz{|}~", $encoding_at );
if ( $encoding_length < 1 ) {
$at = $encoding_at;
continue;
}
$after_encoding = $encoding_at + $encoding_length;
if ( $after_encoding >= $end || '?' !== $encoded[ $after_encoding ] ) {
$at = $after_encoding;
continue;
}
// > encoded-text = 1*<Any printable ASCII character other than "?" or SPACE>
$chunk_at = $after_encoding + 1;
$chunk_length = strspn( $encoded, "!\"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~", $chunk_at );
if ( $chunk_length < 1 ) {
$at = $chunk_at;
continue;
}
$closer_at = $chunk_at + $chunk_length;
if ( $closer_at >= $end || '?' !== $encoded[ $closer_at ] || '=' !== $encoded[ $closer_at + 1 ] ) {
$at = $closer_at;
continue;
}
$after_encoded_word = $closer_at + 2;
/*
* RFC2047 says the total length MUST be no more than 75 characters,
* but doesn’t indicate resolution when the length is greater than this.
*
* - Should this be treated as unencoded text?
* - Should this be corrupted and rejected?
* - Should this be decoded anyway?
*
* Given that the intent is to fit encoded-words within a single line of
* a header and to ensure parsers need not lookahead too far, this will
* be decoded if possible. The failure was in the encoder, not here.
*/
if ( 1 !== $encoding_length || 1 !== strspn( $encoded, 'bBqQ', $encoding_at, 1 ) ) {
goto handle_invalid;
}
/*
* > If the mail reader does not support the character set used, it may
* > (a) display the 'encoded-word' as ordinary text (i.e., as it appears
* > in the header), (b) make a "best effort" to display using such
* > characters as are available, or (c) substitute an appropriate message
* > indicating that the decoded text could not be displayed.
*
* > For the ISO-8859-* character sets, the mail reading program must at
* > least be able to display the characters which are also in the ASCII set.
*/
// Shorten the charset to ignore any RFC2184/RC2231 language tag.
$charset_length = strcspn( $encoded, '*', $charset_at, $charset_length );
/**
* Disregard over-long charset names. This value was chosen by inspecting the
* names returned by {@see mb_convert_encoding()} and {@see mb_encoding_aliases()}.
*
* The goal is to pragmatically balance supporting all possible charsets and
* over-eagerly allocating strings, only to disregard them immediately.
*/
if ( $charset_length > 32 ) {
goto handle_invalid;
}
/*
* Only UTF-8 is supported without conversion mechanisms. When errors are
* preserved, the ISO-8859 family’s ASCII-compatible characters will remain.
*/
$charset = substr( $encoded, $charset_at, $charset_length );
if (
! in_array( strtoupper( $charset ), array( 'ASCII', 'US-ASCII', 'UTF8', 'UTF-8' ), true ) &&
! function_exists( 'mb_convert_encoding' ) &&
! function_exists( 'iconv' )
) {
goto handle_invalid;
}
/*
* > A mail reader need not attempt to display the text associated with an
* > 'encoded-word' that is incorrectly formed. However, a mail reader
* > MUST NOT prevent the display or handling of a message because an
* > 'encoded-word' is incorrectly formed.
*/
$encoding = $encoded[ $encoding_at ];
if ( 'b' === $encoding || 'B' === $encoding ) {
$decoded_chunk = base64_decode( substr( $encoded, $chunk_at, $chunk_length ), false );
if ( false === $decoded_chunk ) {
goto handle_invalid;
}
} else {
// @todo There is no error-handling indication here for the Q decoding.
$failed_decode = false;
$decoded_chunk = substr( $encoded, $chunk_at, $chunk_length );
$decoded_chunk = strtr( $decoded_chunk, '_', ' ' );
$decoded_chunk = preg_replace_callback(
'/=[0-9A-F]{2}|=/', // Lower-case are not allowed.
function ( $matches ) use ( &$failed_decode ) {
if ( '=' === $matches[0] ) {
$failed_decode = true;
return $matches[0];
}
return hex2bin( substr( $matches[0], 1, 2 ) );
},
$decoded_chunk
);
if ( $failed_decode ) {
goto handle_invalid;
}
}
// Re-encode into UTF-8.
if ( in_array( strtoupper( $charset ), array( 'ASCII', 'US-ASCII', 'UTF8', 'UTF-8' ), true ) ) {
// Skip re-encoding for this one.
} elseif ( function_exists( 'mb_convert_encoding' ) ) {
try {
$decoded_chunk = mb_convert_encoding( $decoded_chunk, 'UTF-8', $charset );
} catch ( \Throwable $exception ) {
goto handle_invalid;
}
} elseif ( function_exists( 'iconv' ) ) {
$decoded_chunk = iconv( $charset, 'UTF-8', $decoded_chunk );
}
// Verify the encoding.
if ( false === $decoded_chunk || ! wp_is_valid_utf8( $decoded_chunk ) ) {
goto handle_invalid;
}
// Append the decoded chunk.
$prefix_length = $encoded_word_at - $was_at;
if ( $prefix_length === 0 || rfc2047_only_LWS( $encoded, $was_at, $prefix_length ) ) {
$decoded .= $decoded_chunk;
} else {
$prefix = substr( $encoded, $was_at, $prefix_length );
$decoded .= "{$prefix}{$decoded_chunk}";
}
$was_at = $after_encoded_word;
$at = $was_at;
continue;
handle_invalid:
$at = $after_encoded_word;
switch ( $errors ) {
case 'bail-on-error':
restore_error_handler();
return null;
case 'preserve-errors':
break;
case 'replace-errors':
$prefix_length = $encoded_word_at - $was_at;
if ( $prefix_length === 0 || rfc2047_only_LWS( $encoded, $was_at, $prefix_length ) ) {
$decoded .= "\u{FFFD}";
} else {
$prefix = substr( $encoded, $was_at, $prefix_length );
$decoded .= "{$prefix}\u{FFFD}";
}
$was_at = $after_encoded_word;
break;
default:
_doing_it_wrong(
__FUNCTION__,
"Use only one of 'preserve-errors' or 'replace-errors' for error-handling.",
'{WP_VERSION}'
);
restore_error_handler();
return null;
}
}
if ( $at === 0 ) {
return $encoded;
}
$decoded .= substr( $encoded, $was_at );
restore_error_handler();
return $decoded;
}
/**
* Determines if a span of text represents only linear white space.
*
* @since {WP_VERSION}
* @access private
*
* @param string $string
* @param int $start
* @param int $length
* @return bool
*/
function rfc2047_only_LWS( $string, $start, $length ) {
$at = $start;
$end = $start + $length;
$one = false;
while ( $at < $end ) {
$had_crlf = false;
// Advance past one optional CRLF.
if ( $at + 1 < $end && "\r" === $string[ $at ] && "\n" === $string[ $at + 1 ] ) {
$had_crlf = true;
$at += 2;
}
// Advance past any SPACE / HTAB
$horizontal_spaces = strspn( $string, " \t", $at, $end - $at );
if ( 0 === $horizontal_spaces ) {
return ! $had_crlf && $one && $at === $end;
}
$one = true;
$at += $horizontal_spaces;
}
return $one;
}