]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * A UTF-8 specific character encoder that handles cleaning and transforming.\r | |
5 | * @note All functions in this class should be static.\r | |
6 | */\r | |
7 | class HTMLPurifier_Encoder\r | |
8 | {\r | |
9 | \r | |
10 | /**\r | |
11 | * Constructor throws fatal error if you attempt to instantiate class\r | |
12 | */\r | |
13 | private function __construct()\r | |
14 | {\r | |
15 | trigger_error('Cannot instantiate encoder, call methods statically', E_USER_ERROR);\r | |
16 | }\r | |
17 | \r | |
18 | /**\r | |
19 | * Error-handler that mutes errors, alternative to shut-up operator.\r | |
20 | */\r | |
21 | public static function muteErrorHandler()\r | |
22 | {\r | |
23 | }\r | |
24 | \r | |
25 | /**\r | |
26 | * iconv wrapper which mutes errors, but doesn't work around bugs.\r | |
27 | * @param string $in Input encoding\r | |
28 | * @param string $out Output encoding\r | |
29 | * @param string $text The text to convert\r | |
30 | * @return string\r | |
31 | */\r | |
32 | public static function unsafeIconv($in, $out, $text)\r | |
33 | {\r | |
34 | set_error_handler(array('HTMLPurifier_Encoder', 'muteErrorHandler'));\r | |
35 | $r = iconv($in, $out, $text);\r | |
36 | restore_error_handler();\r | |
37 | return $r;\r | |
38 | }\r | |
39 | \r | |
40 | /**\r | |
41 | * iconv wrapper which mutes errors and works around bugs.\r | |
42 | * @param string $in Input encoding\r | |
43 | * @param string $out Output encoding\r | |
44 | * @param string $text The text to convert\r | |
45 | * @param int $max_chunk_size\r | |
46 | * @return string\r | |
47 | */\r | |
48 | public static function iconv($in, $out, $text, $max_chunk_size = 8000)\r | |
49 | {\r | |
50 | $code = self::testIconvTruncateBug();\r | |
51 | if ($code == self::ICONV_OK) {\r | |
52 | return self::unsafeIconv($in, $out, $text);\r | |
53 | } elseif ($code == self::ICONV_TRUNCATES) {\r | |
54 | // we can only work around this if the input character set\r | |
55 | // is utf-8\r | |
56 | if ($in == 'utf-8') {\r | |
57 | if ($max_chunk_size < 4) {\r | |
58 | trigger_error('max_chunk_size is too small', E_USER_WARNING);\r | |
59 | return false;\r | |
60 | }\r | |
61 | // split into 8000 byte chunks, but be careful to handle\r | |
62 | // multibyte boundaries properly\r | |
63 | if (($c = strlen($text)) <= $max_chunk_size) {\r | |
64 | return self::unsafeIconv($in, $out, $text);\r | |
65 | }\r | |
66 | $r = '';\r | |
67 | $i = 0;\r | |
68 | while (true) {\r | |
69 | if ($i + $max_chunk_size >= $c) {\r | |
70 | $r .= self::unsafeIconv($in, $out, substr($text, $i));\r | |
71 | break;\r | |
72 | }\r | |
73 | // wibble the boundary\r | |
74 | if (0x80 != (0xC0 & ord($text[$i + $max_chunk_size]))) {\r | |
75 | $chunk_size = $max_chunk_size;\r | |
76 | } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 1]))) {\r | |
77 | $chunk_size = $max_chunk_size - 1;\r | |
78 | } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 2]))) {\r | |
79 | $chunk_size = $max_chunk_size - 2;\r | |
80 | } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 3]))) {\r | |
81 | $chunk_size = $max_chunk_size - 3;\r | |
82 | } else {\r | |
83 | return false; // rather confusing UTF-8...\r | |
84 | }\r | |
85 | $chunk = substr($text, $i, $chunk_size); // substr doesn't mind overlong lengths\r | |
86 | $r .= self::unsafeIconv($in, $out, $chunk);\r | |
87 | $i += $chunk_size;\r | |
88 | }\r | |
89 | return $r;\r | |
90 | } else {\r | |
91 | return false;\r | |
92 | }\r | |
93 | } else {\r | |
94 | return false;\r | |
95 | }\r | |
96 | }\r | |
97 | \r | |
98 | /**\r | |
99 | * Cleans a UTF-8 string for well-formedness and SGML validity\r | |
100 | *\r | |
101 | * It will parse according to UTF-8 and return a valid UTF8 string, with\r | |
102 | * non-SGML codepoints excluded.\r | |
103 | *\r | |
104 | * @param string $str The string to clean\r | |
105 | * @param bool $force_php\r | |
106 | * @return string\r | |
107 | *\r | |
108 | * @note Just for reference, the non-SGML code points are 0 to 31 and\r | |
109 | * 127 to 159, inclusive. However, we allow code points 9, 10\r | |
110 | * and 13, which are the tab, line feed and carriage return\r | |
111 | * respectively. 128 and above the code points map to multibyte\r | |
112 | * UTF-8 representations.\r | |
113 | *\r | |
114 | * @note Fallback code adapted from utf8ToUnicode by Henri Sivonen and\r | |
115 | * hsivonen@iki.fi at <http://iki.fi/hsivonen/php-utf8/> under the\r | |
116 | * LGPL license. Notes on what changed are inside, but in general,\r | |
117 | * the original code transformed UTF-8 text into an array of integer\r | |
118 | * Unicode codepoints. Understandably, transforming that back to\r | |
119 | * a string would be somewhat expensive, so the function was modded to\r | |
120 | * directly operate on the string. However, this discourages code\r | |
121 | * reuse, and the logic enumerated here would be useful for any\r | |
122 | * function that needs to be able to understand UTF-8 characters.\r | |
123 | * As of right now, only smart lossless character encoding converters\r | |
124 | * would need that, and I'm probably not going to implement them.\r | |
125 | * Once again, PHP 6 should solve all our problems.\r | |
126 | */\r | |
127 | public static function cleanUTF8($str, $force_php = false)\r | |
128 | {\r | |
129 | // UTF-8 validity is checked since PHP 4.3.5\r | |
130 | // This is an optimization: if the string is already valid UTF-8, no\r | |
131 | // need to do PHP stuff. 99% of the time, this will be the case.\r | |
132 | // The regexp matches the XML char production, as well as well as excluding\r | |
133 | // non-SGML codepoints U+007F to U+009F\r | |
134 | if (preg_match(\r | |
135 | '/^[\x{9}\x{A}\x{D}\x{20}-\x{7E}\x{A0}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]*$/Du',\r | |
136 | $str\r | |
137 | )) {\r | |
138 | return $str;\r | |
139 | }\r | |
140 | \r | |
141 | $mState = 0; // cached expected number of octets after the current octet\r | |
142 | // until the beginning of the next UTF8 character sequence\r | |
143 | $mUcs4 = 0; // cached Unicode character\r | |
144 | $mBytes = 1; // cached expected number of octets in the current sequence\r | |
145 | \r | |
146 | // original code involved an $out that was an array of Unicode\r | |
147 | // codepoints. Instead of having to convert back into UTF-8, we've\r | |
148 | // decided to directly append valid UTF-8 characters onto a string\r | |
149 | // $out once they're done. $char accumulates raw bytes, while $mUcs4\r | |
150 | // turns into the Unicode code point, so there's some redundancy.\r | |
151 | \r | |
152 | $out = '';\r | |
153 | $char = '';\r | |
154 | \r | |
155 | $len = strlen($str);\r | |
156 | for ($i = 0; $i < $len; $i++) {\r | |
157 | $in = ord($str{$i});\r | |
158 | $char .= $str[$i]; // append byte to char\r | |
159 | if (0 == $mState) {\r | |
160 | // When mState is zero we expect either a US-ASCII character\r | |
161 | // or a multi-octet sequence.\r | |
162 | if (0 == (0x80 & ($in))) {\r | |
163 | // US-ASCII, pass straight through.\r | |
164 | if (($in <= 31 || $in == 127) &&\r | |
165 | !($in == 9 || $in == 13 || $in == 10) // save \r\t\n\r | |
166 | ) {\r | |
167 | // control characters, remove\r | |
168 | } else {\r | |
169 | $out .= $char;\r | |
170 | }\r | |
171 | // reset\r | |
172 | $char = '';\r | |
173 | $mBytes = 1;\r | |
174 | } elseif (0xC0 == (0xE0 & ($in))) {\r | |
175 | // First octet of 2 octet sequence\r | |
176 | $mUcs4 = ($in);\r | |
177 | $mUcs4 = ($mUcs4 & 0x1F) << 6;\r | |
178 | $mState = 1;\r | |
179 | $mBytes = 2;\r | |
180 | } elseif (0xE0 == (0xF0 & ($in))) {\r | |
181 | // First octet of 3 octet sequence\r | |
182 | $mUcs4 = ($in);\r | |
183 | $mUcs4 = ($mUcs4 & 0x0F) << 12;\r | |
184 | $mState = 2;\r | |
185 | $mBytes = 3;\r | |
186 | } elseif (0xF0 == (0xF8 & ($in))) {\r | |
187 | // First octet of 4 octet sequence\r | |
188 | $mUcs4 = ($in);\r | |
189 | $mUcs4 = ($mUcs4 & 0x07) << 18;\r | |
190 | $mState = 3;\r | |
191 | $mBytes = 4;\r | |
192 | } elseif (0xF8 == (0xFC & ($in))) {\r | |
193 | // First octet of 5 octet sequence.\r | |
194 | //\r | |
195 | // This is illegal because the encoded codepoint must be\r | |
196 | // either:\r | |
197 | // (a) not the shortest form or\r | |
198 | // (b) outside the Unicode range of 0-0x10FFFF.\r | |
199 | // Rather than trying to resynchronize, we will carry on\r | |
200 | // until the end of the sequence and let the later error\r | |
201 | // handling code catch it.\r | |
202 | $mUcs4 = ($in);\r | |
203 | $mUcs4 = ($mUcs4 & 0x03) << 24;\r | |
204 | $mState = 4;\r | |
205 | $mBytes = 5;\r | |
206 | } elseif (0xFC == (0xFE & ($in))) {\r | |
207 | // First octet of 6 octet sequence, see comments for 5\r | |
208 | // octet sequence.\r | |
209 | $mUcs4 = ($in);\r | |
210 | $mUcs4 = ($mUcs4 & 1) << 30;\r | |
211 | $mState = 5;\r | |
212 | $mBytes = 6;\r | |
213 | } else {\r | |
214 | // Current octet is neither in the US-ASCII range nor a\r | |
215 | // legal first octet of a multi-octet sequence.\r | |
216 | $mState = 0;\r | |
217 | $mUcs4 = 0;\r | |
218 | $mBytes = 1;\r | |
219 | $char = '';\r | |
220 | }\r | |
221 | } else {\r | |
222 | // When mState is non-zero, we expect a continuation of the\r | |
223 | // multi-octet sequence\r | |
224 | if (0x80 == (0xC0 & ($in))) {\r | |
225 | // Legal continuation.\r | |
226 | $shift = ($mState - 1) * 6;\r | |
227 | $tmp = $in;\r | |
228 | $tmp = ($tmp & 0x0000003F) << $shift;\r | |
229 | $mUcs4 |= $tmp;\r | |
230 | \r | |
231 | if (0 == --$mState) {\r | |
232 | // End of the multi-octet sequence. mUcs4 now contains\r | |
233 | // the final Unicode codepoint to be output\r | |
234 | \r | |
235 | // Check for illegal sequences and codepoints.\r | |
236 | \r | |
237 | // From Unicode 3.1, non-shortest form is illegal\r | |
238 | if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||\r | |
239 | ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||\r | |
240 | ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||\r | |
241 | (4 < $mBytes) ||\r | |
242 | // From Unicode 3.2, surrogate characters = illegal\r | |
243 | (($mUcs4 & 0xFFFFF800) == 0xD800) ||\r | |
244 | // Codepoints outside the Unicode range are illegal\r | |
245 | ($mUcs4 > 0x10FFFF)\r | |
246 | ) {\r | |
247 | \r | |
248 | } elseif (0xFEFF != $mUcs4 && // omit BOM\r | |
249 | // check for valid Char unicode codepoints\r | |
250 | (\r | |
251 | 0x9 == $mUcs4 ||\r | |
252 | 0xA == $mUcs4 ||\r | |
253 | 0xD == $mUcs4 ||\r | |
254 | (0x20 <= $mUcs4 && 0x7E >= $mUcs4) ||\r | |
255 | // 7F-9F is not strictly prohibited by XML,\r | |
256 | // but it is non-SGML, and thus we don't allow it\r | |
257 | (0xA0 <= $mUcs4 && 0xD7FF >= $mUcs4) ||\r | |
258 | (0x10000 <= $mUcs4 && 0x10FFFF >= $mUcs4)\r | |
259 | )\r | |
260 | ) {\r | |
261 | $out .= $char;\r | |
262 | }\r | |
263 | // initialize UTF8 cache (reset)\r | |
264 | $mState = 0;\r | |
265 | $mUcs4 = 0;\r | |
266 | $mBytes = 1;\r | |
267 | $char = '';\r | |
268 | }\r | |
269 | } else {\r | |
270 | // ((0xC0 & (*in) != 0x80) && (mState != 0))\r | |
271 | // Incomplete multi-octet sequence.\r | |
272 | // used to result in complete fail, but we'll reset\r | |
273 | $mState = 0;\r | |
274 | $mUcs4 = 0;\r | |
275 | $mBytes = 1;\r | |
276 | $char ='';\r | |
277 | }\r | |
278 | }\r | |
279 | }\r | |
280 | return $out;\r | |
281 | }\r | |
282 | \r | |
283 | /**\r | |
284 | * Translates a Unicode codepoint into its corresponding UTF-8 character.\r | |
285 | * @note Based on Feyd's function at\r | |
286 | * <http://forums.devnetwork.net/viewtopic.php?p=191404#191404>,\r | |
287 | * which is in public domain.\r | |
288 | * @note While we're going to do code point parsing anyway, a good\r | |
289 | * optimization would be to refuse to translate code points that\r | |
290 | * are non-SGML characters. However, this could lead to duplication.\r | |
291 | * @note This is very similar to the unichr function in\r | |
292 | * maintenance/generate-entity-file.php (although this is superior,\r | |
293 | * due to its sanity checks).\r | |
294 | */\r | |
295 | \r | |
296 | // +----------+----------+----------+----------+\r | |
297 | // | 33222222 | 22221111 | 111111 | |\r | |
298 | // | 10987654 | 32109876 | 54321098 | 76543210 | bit\r | |
299 | // +----------+----------+----------+----------+\r | |
300 | // | | | | 0xxxxxxx | 1 byte 0x00000000..0x0000007F\r | |
301 | // | | | 110yyyyy | 10xxxxxx | 2 byte 0x00000080..0x000007FF\r | |
302 | // | | 1110zzzz | 10yyyyyy | 10xxxxxx | 3 byte 0x00000800..0x0000FFFF\r | |
303 | // | 11110www | 10wwzzzz | 10yyyyyy | 10xxxxxx | 4 byte 0x00010000..0x0010FFFF\r | |
304 | // +----------+----------+----------+----------+\r | |
305 | // | 00000000 | 00011111 | 11111111 | 11111111 | Theoretical upper limit of legal scalars: 2097151 (0x001FFFFF)\r | |
306 | // | 00000000 | 00010000 | 11111111 | 11111111 | Defined upper limit of legal scalar codes\r | |
307 | // +----------+----------+----------+----------+\r | |
308 | \r | |
309 | public static function unichr($code)\r | |
310 | {\r | |
311 | if ($code > 1114111 or $code < 0 or\r | |
312 | ($code >= 55296 and $code <= 57343) ) {\r | |
313 | // bits are set outside the "valid" range as defined\r | |
314 | // by UNICODE 4.1.0\r | |
315 | return '';\r | |
316 | }\r | |
317 | \r | |
318 | $x = $y = $z = $w = 0;\r | |
319 | if ($code < 128) {\r | |
320 | // regular ASCII character\r | |
321 | $x = $code;\r | |
322 | } else {\r | |
323 | // set up bits for UTF-8\r | |
324 | $x = ($code & 63) | 128;\r | |
325 | if ($code < 2048) {\r | |
326 | $y = (($code & 2047) >> 6) | 192;\r | |
327 | } else {\r | |
328 | $y = (($code & 4032) >> 6) | 128;\r | |
329 | if ($code < 65536) {\r | |
330 | $z = (($code >> 12) & 15) | 224;\r | |
331 | } else {\r | |
332 | $z = (($code >> 12) & 63) | 128;\r | |
333 | $w = (($code >> 18) & 7) | 240;\r | |
334 | }\r | |
335 | }\r | |
336 | }\r | |
337 | // set up the actual character\r | |
338 | $ret = '';\r | |
339 | if ($w) {\r | |
340 | $ret .= chr($w);\r | |
341 | }\r | |
342 | if ($z) {\r | |
343 | $ret .= chr($z);\r | |
344 | }\r | |
345 | if ($y) {\r | |
346 | $ret .= chr($y);\r | |
347 | }\r | |
348 | $ret .= chr($x);\r | |
349 | \r | |
350 | return $ret;\r | |
351 | }\r | |
352 | \r | |
353 | /**\r | |
354 | * @return bool\r | |
355 | */\r | |
356 | public static function iconvAvailable()\r | |
357 | {\r | |
358 | static $iconv = null;\r | |
359 | if ($iconv === null) {\r | |
360 | $iconv = function_exists('iconv') && self::testIconvTruncateBug() != self::ICONV_UNUSABLE;\r | |
361 | }\r | |
362 | return $iconv;\r | |
363 | }\r | |
364 | \r | |
365 | /**\r | |
366 | * Convert a string to UTF-8 based on configuration.\r | |
367 | * @param string $str The string to convert\r | |
368 | * @param HTMLPurifier_Config $config\r | |
369 | * @param HTMLPurifier_Context $context\r | |
370 | * @return string\r | |
371 | */\r | |
372 | public static function convertToUTF8($str, $config, $context)\r | |
373 | {\r | |
374 | $encoding = $config->get('Core.Encoding');\r | |
375 | if ($encoding === 'utf-8') {\r | |
376 | return $str;\r | |
377 | }\r | |
378 | static $iconv = null;\r | |
379 | if ($iconv === null) {\r | |
380 | $iconv = self::iconvAvailable();\r | |
381 | }\r | |
382 | if ($iconv && !$config->get('Test.ForceNoIconv')) {\r | |
383 | // unaffected by bugs, since UTF-8 support all characters\r | |
384 | $str = self::unsafeIconv($encoding, 'utf-8//IGNORE', $str);\r | |
385 | if ($str === false) {\r | |
386 | // $encoding is not a valid encoding\r | |
387 | trigger_error('Invalid encoding ' . $encoding, E_USER_ERROR);\r | |
388 | return '';\r | |
389 | }\r | |
390 | // If the string is bjorked by Shift_JIS or a similar encoding\r | |
391 | // that doesn't support all of ASCII, convert the naughty\r | |
392 | // characters to their true byte-wise ASCII/UTF-8 equivalents.\r | |
393 | $str = strtr($str, self::testEncodingSupportsASCII($encoding));\r | |
394 | return $str;\r | |
395 | } elseif ($encoding === 'iso-8859-1') {\r | |
396 | $str = utf8_encode($str);\r | |
397 | return $str;\r | |
398 | }\r | |
399 | $bug = HTMLPurifier_Encoder::testIconvTruncateBug();\r | |
400 | if ($bug == self::ICONV_OK) {\r | |
401 | trigger_error('Encoding not supported, please install iconv', E_USER_ERROR);\r | |
402 | } else {\r | |
403 | trigger_error(\r | |
404 | 'You have a buggy version of iconv, see https://bugs.php.net/bug.php?id=48147 ' .\r | |
405 | 'and http://sourceware.org/bugzilla/show_bug.cgi?id=13541',\r | |
406 | E_USER_ERROR\r | |
407 | );\r | |
408 | }\r | |
409 | }\r | |
410 | \r | |
411 | /**\r | |
412 | * Converts a string from UTF-8 based on configuration.\r | |
413 | * @param string $str The string to convert\r | |
414 | * @param HTMLPurifier_Config $config\r | |
415 | * @param HTMLPurifier_Context $context\r | |
416 | * @return string\r | |
417 | * @note Currently, this is a lossy conversion, with unexpressable\r | |
418 | * characters being omitted.\r | |
419 | */\r | |
420 | public static function convertFromUTF8($str, $config, $context)\r | |
421 | {\r | |
422 | $encoding = $config->get('Core.Encoding');\r | |
423 | if ($escape = $config->get('Core.EscapeNonASCIICharacters')) {\r | |
424 | $str = self::convertToASCIIDumbLossless($str);\r | |
425 | }\r | |
426 | if ($encoding === 'utf-8') {\r | |
427 | return $str;\r | |
428 | }\r | |
429 | static $iconv = null;\r | |
430 | if ($iconv === null) {\r | |
431 | $iconv = self::iconvAvailable();\r | |
432 | }\r | |
433 | if ($iconv && !$config->get('Test.ForceNoIconv')) {\r | |
434 | // Undo our previous fix in convertToUTF8, otherwise iconv will barf\r | |
435 | $ascii_fix = self::testEncodingSupportsASCII($encoding);\r | |
436 | if (!$escape && !empty($ascii_fix)) {\r | |
437 | $clear_fix = array();\r | |
438 | foreach ($ascii_fix as $utf8 => $native) {\r | |
439 | $clear_fix[$utf8] = '';\r | |
440 | }\r | |
441 | $str = strtr($str, $clear_fix);\r | |
442 | }\r | |
443 | $str = strtr($str, array_flip($ascii_fix));\r | |
444 | // Normal stuff\r | |
445 | $str = self::iconv('utf-8', $encoding . '//IGNORE', $str);\r | |
446 | return $str;\r | |
447 | } elseif ($encoding === 'iso-8859-1') {\r | |
448 | $str = utf8_decode($str);\r | |
449 | return $str;\r | |
450 | }\r | |
451 | trigger_error('Encoding not supported', E_USER_ERROR);\r | |
452 | // You might be tempted to assume that the ASCII representation\r | |
453 | // might be OK, however, this is *not* universally true over all\r | |
454 | // encodings. So we take the conservative route here, rather\r | |
455 | // than forcibly turn on %Core.EscapeNonASCIICharacters\r | |
456 | }\r | |
457 | \r | |
458 | /**\r | |
459 | * Lossless (character-wise) conversion of HTML to ASCII\r | |
460 | * @param string $str UTF-8 string to be converted to ASCII\r | |
461 | * @return string ASCII encoded string with non-ASCII character entity-ized\r | |
462 | * @warning Adapted from MediaWiki, claiming fair use: this is a common\r | |
463 | * algorithm. If you disagree with this license fudgery,\r | |
464 | * implement it yourself.\r | |
465 | * @note Uses decimal numeric entities since they are best supported.\r | |
466 | * @note This is a DUMB function: it has no concept of keeping\r | |
467 | * character entities that the projected character encoding\r | |
468 | * can allow. We could possibly implement a smart version\r | |
469 | * but that would require it to also know which Unicode\r | |
470 | * codepoints the charset supported (not an easy task).\r | |
471 | * @note Sort of with cleanUTF8() but it assumes that $str is\r | |
472 | * well-formed UTF-8\r | |
473 | */\r | |
474 | public static function convertToASCIIDumbLossless($str)\r | |
475 | {\r | |
476 | $bytesleft = 0;\r | |
477 | $result = '';\r | |
478 | $working = 0;\r | |
479 | $len = strlen($str);\r | |
480 | for ($i = 0; $i < $len; $i++) {\r | |
481 | $bytevalue = ord($str[$i]);\r | |
482 | if ($bytevalue <= 0x7F) { //0xxx xxxx\r | |
483 | $result .= chr($bytevalue);\r | |
484 | $bytesleft = 0;\r | |
485 | } elseif ($bytevalue <= 0xBF) { //10xx xxxx\r | |
486 | $working = $working << 6;\r | |
487 | $working += ($bytevalue & 0x3F);\r | |
488 | $bytesleft--;\r | |
489 | if ($bytesleft <= 0) {\r | |
490 | $result .= "&#" . $working . ";";\r | |
491 | }\r | |
492 | } elseif ($bytevalue <= 0xDF) { //110x xxxx\r | |
493 | $working = $bytevalue & 0x1F;\r | |
494 | $bytesleft = 1;\r | |
495 | } elseif ($bytevalue <= 0xEF) { //1110 xxxx\r | |
496 | $working = $bytevalue & 0x0F;\r | |
497 | $bytesleft = 2;\r | |
498 | } else { //1111 0xxx\r | |
499 | $working = $bytevalue & 0x07;\r | |
500 | $bytesleft = 3;\r | |
501 | }\r | |
502 | }\r | |
503 | return $result;\r | |
504 | }\r | |
505 | \r | |
506 | /** No bugs detected in iconv. */\r | |
507 | const ICONV_OK = 0;\r | |
508 | \r | |
509 | /** Iconv truncates output if converting from UTF-8 to another\r | |
510 | * character set with //IGNORE, and a non-encodable character is found */\r | |
511 | const ICONV_TRUNCATES = 1;\r | |
512 | \r | |
513 | /** Iconv does not support //IGNORE, making it unusable for\r | |
514 | * transcoding purposes */\r | |
515 | const ICONV_UNUSABLE = 2;\r | |
516 | \r | |
517 | /**\r | |
518 | * glibc iconv has a known bug where it doesn't handle the magic\r | |
519 | * //IGNORE stanza correctly. In particular, rather than ignore\r | |
520 | * characters, it will return an EILSEQ after consuming some number\r | |
521 | * of characters, and expect you to restart iconv as if it were\r | |
522 | * an E2BIG. Old versions of PHP did not respect the errno, and\r | |
523 | * returned the fragment, so as a result you would see iconv\r | |
524 | * mysteriously truncating output. We can work around this by\r | |
525 | * manually chopping our input into segments of about 8000\r | |
526 | * characters, as long as PHP ignores the error code. If PHP starts\r | |
527 | * paying attention to the error code, iconv becomes unusable.\r | |
528 | *\r | |
529 | * @return int Error code indicating severity of bug.\r | |
530 | */\r | |
531 | public static function testIconvTruncateBug()\r | |
532 | {\r | |
533 | static $code = null;\r | |
534 | if ($code === null) {\r | |
535 | // better not use iconv, otherwise infinite loop!\r | |
536 | $r = self::unsafeIconv('utf-8', 'ascii//IGNORE', "\xCE\xB1" . str_repeat('a', 9000));\r | |
537 | if ($r === false) {\r | |
538 | $code = self::ICONV_UNUSABLE;\r | |
539 | } elseif (($c = strlen($r)) < 9000) {\r | |
540 | $code = self::ICONV_TRUNCATES;\r | |
541 | } elseif ($c > 9000) {\r | |
542 | trigger_error(\r | |
543 | 'Your copy of iconv is extremely buggy. Please notify HTML Purifier maintainers: ' .\r | |
544 | 'include your iconv version as per phpversion()',\r | |
545 | E_USER_ERROR\r | |
546 | );\r | |
547 | } else {\r | |
548 | $code = self::ICONV_OK;\r | |
549 | }\r | |
550 | }\r | |
551 | return $code;\r | |
552 | }\r | |
553 | \r | |
554 | /**\r | |
555 | * This expensive function tests whether or not a given character\r | |
556 | * encoding supports ASCII. 7/8-bit encodings like Shift_JIS will\r | |
557 | * fail this test, and require special processing. Variable width\r | |
558 | * encodings shouldn't ever fail.\r | |
559 | *\r | |
560 | * @param string $encoding Encoding name to test, as per iconv format\r | |
561 | * @param bool $bypass Whether or not to bypass the precompiled arrays.\r | |
562 | * @return Array of UTF-8 characters to their corresponding ASCII,\r | |
563 | * which can be used to "undo" any overzealous iconv action.\r | |
564 | */\r | |
565 | public static function testEncodingSupportsASCII($encoding, $bypass = false)\r | |
566 | {\r | |
567 | // All calls to iconv here are unsafe, proof by case analysis:\r | |
568 | // If ICONV_OK, no difference.\r | |
569 | // If ICONV_TRUNCATE, all calls involve one character inputs,\r | |
570 | // so bug is not triggered.\r | |
571 | // If ICONV_UNUSABLE, this call is irrelevant\r | |
572 | static $encodings = array();\r | |
573 | if (!$bypass) {\r | |
574 | if (isset($encodings[$encoding])) {\r | |
575 | return $encodings[$encoding];\r | |
576 | }\r | |
577 | $lenc = strtolower($encoding);\r | |
578 | switch ($lenc) {\r | |
579 | case 'shift_jis':\r | |
580 | return array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~');\r | |
581 | case 'johab':\r | |
582 | return array("\xE2\x82\xA9" => '\\');\r | |
583 | }\r | |
584 | if (strpos($lenc, 'iso-8859-') === 0) {\r | |
585 | return array();\r | |
586 | }\r | |
587 | }\r | |
588 | $ret = array();\r | |
589 | if (self::unsafeIconv('UTF-8', $encoding, 'a') === false) {\r | |
590 | return false;\r | |
591 | }\r | |
592 | for ($i = 0x20; $i <= 0x7E; $i++) { // all printable ASCII chars\r | |
593 | $c = chr($i); // UTF-8 char\r | |
594 | $r = self::unsafeIconv('UTF-8', "$encoding//IGNORE", $c); // initial conversion\r | |
595 | if ($r === '' ||\r | |
596 | // This line is needed for iconv implementations that do not\r | |
597 | // omit characters that do not exist in the target character set\r | |
598 | ($r === $c && self::unsafeIconv($encoding, 'UTF-8//IGNORE', $r) !== $c)\r | |
599 | ) {\r | |
600 | // Reverse engineer: what's the UTF-8 equiv of this byte\r | |
601 | // sequence? This assumes that there's no variable width\r | |
602 | // encoding that doesn't support ASCII.\r | |
603 | $ret[self::unsafeIconv($encoding, 'UTF-8//IGNORE', $c)] = $c;\r | |
604 | }\r | |
605 | }\r | |
606 | $encodings[$encoding] = $ret;\r | |
607 | return $ret;\r | |
608 | }\r | |
609 | }\r | |
610 | \r | |
611 | // vim: et sw=4 sts=4\r |