A UTF-8 specific character encoder that handles cleaning and transforming.
More...
|
static | muteErrorHandler () |
| Error-handler that mutes errors, alternative to shut-up operator. More...
|
|
static | unsafeIconv ($in, $out, $text) |
| iconv wrapper which mutes errors, but doesn't work around bugs. More...
|
|
static | iconv ($in, $out, $text, $max_chunk_size=8000) |
| iconv wrapper which mutes errors and works around bugs. More...
|
|
static | cleanUTF8 ($str, $force_php=false) |
| Cleans a UTF-8 string for well-formedness and SGML validity. More...
|
|
static | unichr ($code) |
| Translates a Unicode codepoint into its corresponding UTF-8 character. More...
|
|
static | iconvAvailable () |
|
static | convertToUTF8 ($str, $config, $context) |
| Converts a string to UTF-8 based on configuration. More...
|
|
static | convertFromUTF8 ($str, $config, $context) |
| Converts a string from UTF-8 based on configuration. More...
|
|
static | convertToASCIIDumbLossless ($str) |
| Lossless (character-wise) conversion of HTML to ASCII. More...
|
|
static | testIconvTruncateBug () |
| glibc iconv has a known bug where it doesn't handle the magic //IGNORE stanza correctly. More...
|
|
static | testEncodingSupportsASCII ($encoding, $bypass=false) |
| This expensive function tests whether or not a given character encoding supports ASCII. More...
|
|
|
const | ICONV_OK = 0 |
| No bugs detected in iconv. More...
|
|
const | ICONV_TRUNCATES = 1 |
| Iconv truncates output if converting from UTF-8 to another character set with //IGNORE, and a non-encodable character is found. More...
|
|
const | ICONV_UNUSABLE = 2 |
| Iconv does not support //IGNORE, making it unusable for transcoding purposes. More...
|
|
|
| __construct () |
| Constructor throws fatal error if you attempt to instantiate class. More...
|
|
A UTF-8 specific character encoder that handles cleaning and transforming.
- Note
- All functions in this class should be static.
Definition at line 7 of file Encoder.php.
◆ __construct()
HTMLPurifier_Encoder::__construct |
( |
| ) |
|
|
private |
Constructor throws fatal error if you attempt to instantiate class.
Definition at line 13 of file Encoder.php.
14 trigger_error(
'Cannot instantiate encoder, call methods statically', E_USER_ERROR);
◆ cleanUTF8()
static HTMLPurifier_Encoder::cleanUTF8 |
( |
|
$str, |
|
|
|
$force_php = false |
|
) |
| |
|
static |
Cleans a UTF-8 string for well-formedness and SGML validity.
It will parse according to UTF-8 and return a valid UTF8 string, with non-SGML codepoints excluded.
- Note
- Just for reference, the non-SGML code points are 0 to 31 and 127 to 159, inclusive. However, we allow code points 9, 10 and 13, which are the tab, line feed and carriage return respectively. 128 and above the code points map to multibyte UTF-8 representations.
-
Fallback code adapted from utf8ToUnicode by Henri Sivonen and hsivo.nosp@m.nen@.nosp@m.iki.f.nosp@m.i at http://iki.fi/hsivonen/php-utf8/ under the LGPL license. Notes on what changed are inside, but in general, the original code transformed UTF-8 text into an array of integer Unicode codepoints. Understandably, transforming that back to a string would be somewhat expensive, so the function was modded to directly operate on the string. However, this discourages code reuse, and the logic enumerated here would be useful for any function that needs to be able to understand UTF-8 characters. As of right now, only smart lossless character encoding converters would need that, and I'm probably not going to implement them. Once again, PHP 6 should solve all our problems.
Definition at line 109 of file Encoder.php.
References $in, and $out.
Referenced by HTMLPurifier_Printer\escape(), HTMLPurifier_AttrDef\expandCSSEscape(), and HTMLPurifier_Lexer\normalize().
116 if (preg_match(
'/^[\x{9}\x{A}\x{D}\x{20}-\x{7E}\x{A0}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]*$/Du', $str)) {
135 for($i = 0; $i < $len; $i++) {
141 if (0 == (0x80 & (
$in))) {
143 if ((
$in <= 31 ||
$in == 127) &&
153 } elseif (0xC0 == (0xE0 & (
$in))) {
156 $mUcs4 = ($mUcs4 & 0x1F) << 6;
159 } elseif (0xE0 == (0xF0 & (
$in))) {
162 $mUcs4 = ($mUcs4 & 0x0F) << 12;
165 } elseif (0xF0 == (0xF8 & (
$in))) {
168 $mUcs4 = ($mUcs4 & 0x07) << 18;
171 } elseif (0xF8 == (0xFC & (
$in))) {
182 $mUcs4 = ($mUcs4 & 0x03) << 24;
185 } elseif (0xFC == (0xFE & (
$in))) {
189 $mUcs4 = ($mUcs4 & 1) << 30;
203 if (0x80 == (0xC0 & (
$in))) {
205 $shift = ($mState - 1) * 6;
207 $tmp = ($tmp & 0x0000003F) << $shift;
210 if (0 == --$mState) {
217 if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
218 ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
219 ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
222 (($mUcs4 & 0xFFFFF800) == 0xD800) ||
227 } elseif (0xFEFF != $mUcs4 &&
233 (0x20 <= $mUcs4 && 0x7E >= $mUcs4) ||
236 (0xA0 <= $mUcs4 && 0xD7FF >= $mUcs4) ||
237 (0x10000 <= $mUcs4 && 0x10FFFF >= $mUcs4)
◆ convertFromUTF8()
static HTMLPurifier_Encoder::convertFromUTF8 |
( |
|
$str, |
|
|
|
$config, |
|
|
|
$context |
|
) |
| |
|
static |
Converts a string from UTF-8 based on configuration.
- Note
- Currently, this is a lossy conversion, with unexpressable characters being omitted.
Definition at line 371 of file Encoder.php.
Referenced by HTMLPurifier\purify().
372 $encoding = $config->get(
'Core.Encoding');
373 if ($escape = $config->get(
'Core.EscapeNonASCIICharacters')) {
374 $str = self::convertToASCIIDumbLossless($str);
376 if ($encoding ===
'utf-8')
return $str;
377 static $iconv = null;
378 if ($iconv === null) $iconv = self::iconvAvailable();
379 if ($iconv && !$config->get(
'Test.ForceNoIconv')) {
381 $ascii_fix = self::testEncodingSupportsASCII($encoding);
382 if (!$escape && !empty($ascii_fix)) {
383 $clear_fix = array();
384 foreach ($ascii_fix as $utf8 => $native) $clear_fix[$utf8] =
'';
385 $str = strtr($str, $clear_fix);
387 $str = strtr($str, array_flip($ascii_fix));
389 $str = self::iconv(
'utf-8', $encoding .
'//IGNORE', $str);
391 } elseif ($encoding ===
'iso-8859-1') {
392 $str = utf8_decode($str);
395 trigger_error(
'Encoding not supported', E_USER_ERROR);
◆ convertToASCIIDumbLossless()
static HTMLPurifier_Encoder::convertToASCIIDumbLossless |
( |
|
$str | ) |
|
|
static |
Lossless (character-wise) conversion of HTML to ASCII.
- Parameters
-
$str | UTF-8 string to be converted to ASCII |
- Returns
- ASCII encoded string with non-ASCII character entity-ized
- Warning
- Adapted from MediaWiki, claiming fair use: this is a common algorithm. If you disagree with this license fudgery, implement it yourself.
- Note
- Uses decimal numeric entities since they are best supported.
-
This is a DUMB function: it has no concept of keeping character entities that the projected character encoding can allow. We could possibly implement a smart version but that would require it to also know which Unicode codepoints the charset supported (not an easy task).
-
Sort of with cleanUTF8() but it assumes that $str is well-formed UTF-8
Definition at line 418 of file Encoder.php.
References $result.
423 for( $i = 0; $i < $len; $i++ ) {
424 $bytevalue = ord( $str[$i] );
425 if( $bytevalue <= 0x7F ) {
428 } elseif( $bytevalue <= 0xBF ) {
429 $working = $working << 6;
430 $working += ($bytevalue & 0x3F);
432 if( $bytesleft <= 0 ) {
433 $result .=
"&#" . $working .
";";
435 } elseif( $bytevalue <= 0xDF ) {
436 $working = $bytevalue & 0x1F;
438 } elseif( $bytevalue <= 0xEF ) {
439 $working = $bytevalue & 0x0F;
442 $working = $bytevalue & 0x07;
◆ convertToUTF8()
static HTMLPurifier_Encoder::convertToUTF8 |
( |
|
$str, |
|
|
|
$config, |
|
|
|
$context |
|
) |
| |
|
static |
Converts a string to UTF-8 based on configuration.
Definition at line 336 of file Encoder.php.
References testIconvTruncateBug().
Referenced by HTMLPurifier\purify().
337 $encoding = $config->get(
'Core.Encoding');
338 if ($encoding ===
'utf-8')
return $str;
339 static $iconv = null;
340 if ($iconv === null) $iconv = self::iconvAvailable();
341 if ($iconv && !$config->get(
'Test.ForceNoIconv')) {
343 $str = self::unsafeIconv($encoding,
'utf-8//IGNORE', $str);
344 if ($str ===
false) {
346 trigger_error(
'Invalid encoding ' . $encoding, E_USER_ERROR);
352 $str = strtr($str, self::testEncodingSupportsASCII($encoding));
354 } elseif ($encoding ===
'iso-8859-1') {
355 $str = utf8_encode($str);
359 if ($bug == self::ICONV_OK) {
360 trigger_error(
'Encoding not supported, please install iconv', E_USER_ERROR);
362 trigger_error(
'You have a buggy version of iconv, see https://bugs.php.net/bug.php?id=48147 and http://sourceware.org/bugzilla/show_bug.cgi?id=13541', E_USER_ERROR);
static testIconvTruncateBug()
glibc iconv has a known bug where it doesn't handle the magic //IGNORE stanza correctly.
◆ iconv()
static HTMLPurifier_Encoder::iconv |
( |
|
$in, |
|
|
|
$out, |
|
|
|
$text, |
|
|
|
$max_chunk_size = 8000 |
|
) |
| |
|
static |
iconv wrapper which mutes errors and works around bugs.
Definition at line 35 of file Encoder.php.
References $in, $out, and $r.
Referenced by unsafeIconv().
36 $code = self::testIconvTruncateBug();
37 if ($code == self::ICONV_OK) {
38 return self::unsafeIconv(
$in,
$out, $text);
39 } elseif ($code == self::ICONV_TRUNCATES) {
43 if ($max_chunk_size < 4) {
44 trigger_error(
'max_chunk_size is too small', E_USER_WARNING);
49 if (($c = strlen($text)) <= $max_chunk_size) {
50 return self::unsafeIconv(
$in,
$out, $text);
55 if ($i + $max_chunk_size >= $c) {
56 $r .= self::unsafeIconv(
$in,
$out, substr($text, $i));
60 if (0x80 != (0xC0 & ord($text[$i + $max_chunk_size]))) {
61 $chunk_size = $max_chunk_size;
62 } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 1]))) {
63 $chunk_size = $max_chunk_size - 1;
64 } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 2]))) {
65 $chunk_size = $max_chunk_size - 2;
66 } elseif (0x80 != (0xC0 & ord($text[$i + $max_chunk_size - 3]))) {
67 $chunk_size = $max_chunk_size - 3;
71 $chunk = substr($text, $i, $chunk_size);
72 $r .= self::unsafeIconv(
$in,
$out, $chunk);
◆ iconvAvailable()
static HTMLPurifier_Encoder::iconvAvailable |
( |
| ) |
|
|
static |
Definition at line 325 of file Encoder.php.
326 static $iconv = null;
327 if ($iconv === null) {
328 $iconv = function_exists(
'iconv') && self::testIconvTruncateBug() != self::ICONV_UNUSABLE;
◆ muteErrorHandler()
static HTMLPurifier_Encoder::muteErrorHandler |
( |
| ) |
|
|
static |
Error-handler that mutes errors, alternative to shut-up operator.
Definition at line 20 of file Encoder.php.
◆ testEncodingSupportsASCII()
static HTMLPurifier_Encoder::testEncodingSupportsASCII |
( |
|
$encoding, |
|
|
|
$bypass = false |
|
) |
| |
|
static |
This expensive function tests whether or not a given character encoding supports ASCII.
7/8-bit encodings like Shift_JIS will fail this test, and require special processing. Variable width encodings shouldn't ever fail.
- Parameters
-
string | $encoding | Encoding name to test, as per iconv format |
bool | $bypass | Whether or not to bypass the precompiled arrays. |
- Returns
- Array of UTF-8 characters to their corresponding ASCII, which can be used to "undo" any overzealous iconv action.
Definition at line 503 of file Encoder.php.
References $r, and $ret.
509 static $encodings = array();
511 if (isset($encodings[$encoding]))
return $encodings[$encoding];
512 $lenc = strtolower($encoding);
515 return array(
"\xC2\xA5" =>
'\\',
"\xE2\x80\xBE" =>
'~');
517 return array(
"\xE2\x82\xA9" =>
'\\');
519 if (strpos($lenc,
'iso-8859-') === 0)
return array();
522 if (self::unsafeIconv(
'UTF-8', $encoding,
'a') ===
false)
return false;
523 for ($i = 0x20; $i <= 0x7E; $i++) {
525 $r = self::unsafeIconv(
'UTF-8',
"$encoding//IGNORE", $c);
530 (
$r === $c && self::unsafeIconv($encoding,
'UTF-8//IGNORE',
$r) !== $c)
535 $ret[self::unsafeIconv($encoding,
'UTF-8//IGNORE', $c)] = $c;
538 $encodings[$encoding] =
$ret;
◆ testIconvTruncateBug()
static HTMLPurifier_Encoder::testIconvTruncateBug |
( |
| ) |
|
|
static |
glibc iconv has a known bug where it doesn't handle the magic //IGNORE stanza correctly.
In particular, rather than ignore characters, it will return an EILSEQ after consuming some number of characters, and expect you to restart iconv as if it were an E2BIG. Old versions of PHP did not respect the errno, and returned the fragment, so as a result you would see iconv mysteriously truncating output. We can work around this by manually chopping our input into segments of about 8000 characters, as long as PHP ignores the error code. If PHP starts paying attention to the error code, iconv becomes unusable.
- Returns
- Error code indicating severity of bug.
Definition at line 474 of file Encoder.php.
References $r.
Referenced by convertToUTF8().
476 if ($code === null) {
478 $r = self::unsafeIconv(
'utf-8',
'ascii//IGNORE',
"\xCE\xB1" . str_repeat(
'a', 9000));
480 $code = self::ICONV_UNUSABLE;
481 } elseif (($c = strlen(
$r)) < 9000) {
482 $code = self::ICONV_TRUNCATES;
483 } elseif ($c > 9000) {
484 trigger_error(
'Your copy of iconv is extremely buggy. Please notify HTML Purifier maintainers: include your iconv version as per phpversion()', E_USER_ERROR);
486 $code = self::ICONV_OK;
◆ unichr()
static HTMLPurifier_Encoder::unichr |
( |
|
$code | ) |
|
|
static |
Translates a Unicode codepoint into its corresponding UTF-8 character.
- Note
- Based on Feyd's function at http://forums.devnetwork.net/viewtopic.php?p=191404#191404, which is in public domain.
-
While we're going to do code point parsing anyway, a good optimization would be to refuse to translate code points that are non-SGML characters. However, this could lead to duplication.
-
This is very similar to the unichr function in maintenance/generate-entity-file.php (although this is superior, due to its sanity checks).
Definition at line 288 of file Encoder.php.
References $ret.
Referenced by HTMLPurifier_AttrDef\expandCSSEscape(), and HTMLPurifier_EntityParser\nonSpecialEntityCallback().
289 if($code > 1114111 or $code < 0 or
290 ($code >= 55296 and $code <= 57343) ) {
296 $x = $y = $z = $w = 0;
302 $x = ($code & 63) | 128;
304 $y = (($code & 2047) >> 6) | 192;
306 $y = (($code & 4032) >> 6) | 128;
308 $z = (($code >> 12) & 15) | 224;
310 $z = (($code >> 12) & 63) | 128;
311 $w = (($code >> 18) & 7) | 240;
317 if($w)
$ret .= chr($w);
318 if($z)
$ret .= chr($z);
319 if($y)
$ret .= chr($y);
◆ unsafeIconv()
static HTMLPurifier_Encoder::unsafeIconv |
( |
|
$in, |
|
|
|
$out, |
|
|
|
$text |
|
) |
| |
|
static |
iconv wrapper which mutes errors, but doesn't work around bugs.
Definition at line 25 of file Encoder.php.
References $in, $out, $r, and iconv().
26 set_error_handler(array(
'HTMLPurifier_Encoder',
'muteErrorHandler'));
28 restore_error_handler();
static iconv($in, $out, $text, $max_chunk_size=8000)
iconv wrapper which mutes errors and works around bugs.
◆ ICONV_OK
const HTMLPurifier_Encoder::ICONV_OK = 0 |
No bugs detected in iconv.
Definition at line 450 of file Encoder.php.
◆ ICONV_TRUNCATES
const HTMLPurifier_Encoder::ICONV_TRUNCATES = 1 |
Iconv truncates output if converting from UTF-8 to another character set with //IGNORE, and a non-encodable character is found.
Definition at line 454 of file Encoder.php.
◆ ICONV_UNUSABLE
const HTMLPurifier_Encoder::ICONV_UNUSABLE = 2 |
Iconv does not support //IGNORE, making it unusable for transcoding purposes.
Definition at line 458 of file Encoder.php.
The documentation for this class was generated from the following file:
- Services/Html/HtmlPurifier/library/HTMLPurifier/Encoder.php