|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00007 class HTMLPurifier_Encoder 00008 { 00009 00013 private function __construct() { 00014 trigger_error('Cannot instantiate encoder, call methods statically', E_USER_ERROR); 00015 } 00016 00020 public static function muteErrorHandler() {} 00021 00047 public static function cleanUTF8($str, $force_php = false) { 00048 00049 // UTF-8 validity is checked since PHP 4.3.5 00050 // This is an optimization: if the string is already valid UTF-8, no 00051 // need to do PHP stuff. 99% of the time, this will be the case. 00052 // The regexp matches the XML char production, as well as well as excluding 00053 // non-SGML codepoints U+007F to U+009F 00054 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)) { 00055 return $str; 00056 } 00057 00058 $mState = 0; // cached expected number of octets after the current octet 00059 // until the beginning of the next UTF8 character sequence 00060 $mUcs4 = 0; // cached Unicode character 00061 $mBytes = 1; // cached expected number of octets in the current sequence 00062 00063 // original code involved an $out that was an array of Unicode 00064 // codepoints. Instead of having to convert back into UTF-8, we've 00065 // decided to directly append valid UTF-8 characters onto a string 00066 // $out once they're done. $char accumulates raw bytes, while $mUcs4 00067 // turns into the Unicode code point, so there's some redundancy. 00068 00069 $out = ''; 00070 $char = ''; 00071 00072 $len = strlen($str); 00073 for($i = 0; $i < $len; $i++) { 00074 $in = ord($str{$i}); 00075 $char .= $str[$i]; // append byte to char 00076 if (0 == $mState) { 00077 // When mState is zero we expect either a US-ASCII character 00078 // or a multi-octet sequence. 00079 if (0 == (0x80 & ($in))) { 00080 // US-ASCII, pass straight through. 00081 if (($in <= 31 || $in == 127) && 00082 !($in == 9 || $in == 13 || $in == 10) // save \r\t\n 00083 ) { 00084 // control characters, remove 00085 } else { 00086 $out .= $char; 00087 } 00088 // reset 00089 $char = ''; 00090 $mBytes = 1; 00091 } elseif (0xC0 == (0xE0 & ($in))) { 00092 // First octet of 2 octet sequence 00093 $mUcs4 = ($in); 00094 $mUcs4 = ($mUcs4 & 0x1F) << 6; 00095 $mState = 1; 00096 $mBytes = 2; 00097 } elseif (0xE0 == (0xF0 & ($in))) { 00098 // First octet of 3 octet sequence 00099 $mUcs4 = ($in); 00100 $mUcs4 = ($mUcs4 & 0x0F) << 12; 00101 $mState = 2; 00102 $mBytes = 3; 00103 } elseif (0xF0 == (0xF8 & ($in))) { 00104 // First octet of 4 octet sequence 00105 $mUcs4 = ($in); 00106 $mUcs4 = ($mUcs4 & 0x07) << 18; 00107 $mState = 3; 00108 $mBytes = 4; 00109 } elseif (0xF8 == (0xFC & ($in))) { 00110 // First octet of 5 octet sequence. 00111 // 00112 // This is illegal because the encoded codepoint must be 00113 // either: 00114 // (a) not the shortest form or 00115 // (b) outside the Unicode range of 0-0x10FFFF. 00116 // Rather than trying to resynchronize, we will carry on 00117 // until the end of the sequence and let the later error 00118 // handling code catch it. 00119 $mUcs4 = ($in); 00120 $mUcs4 = ($mUcs4 & 0x03) << 24; 00121 $mState = 4; 00122 $mBytes = 5; 00123 } elseif (0xFC == (0xFE & ($in))) { 00124 // First octet of 6 octet sequence, see comments for 5 00125 // octet sequence. 00126 $mUcs4 = ($in); 00127 $mUcs4 = ($mUcs4 & 1) << 30; 00128 $mState = 5; 00129 $mBytes = 6; 00130 } else { 00131 // Current octet is neither in the US-ASCII range nor a 00132 // legal first octet of a multi-octet sequence. 00133 $mState = 0; 00134 $mUcs4 = 0; 00135 $mBytes = 1; 00136 $char = ''; 00137 } 00138 } else { 00139 // When mState is non-zero, we expect a continuation of the 00140 // multi-octet sequence 00141 if (0x80 == (0xC0 & ($in))) { 00142 // Legal continuation. 00143 $shift = ($mState - 1) * 6; 00144 $tmp = $in; 00145 $tmp = ($tmp & 0x0000003F) << $shift; 00146 $mUcs4 |= $tmp; 00147 00148 if (0 == --$mState) { 00149 // End of the multi-octet sequence. mUcs4 now contains 00150 // the final Unicode codepoint to be output 00151 00152 // Check for illegal sequences and codepoints. 00153 00154 // From Unicode 3.1, non-shortest form is illegal 00155 if (((2 == $mBytes) && ($mUcs4 < 0x0080)) || 00156 ((3 == $mBytes) && ($mUcs4 < 0x0800)) || 00157 ((4 == $mBytes) && ($mUcs4 < 0x10000)) || 00158 (4 < $mBytes) || 00159 // From Unicode 3.2, surrogate characters = illegal 00160 (($mUcs4 & 0xFFFFF800) == 0xD800) || 00161 // Codepoints outside the Unicode range are illegal 00162 ($mUcs4 > 0x10FFFF) 00163 ) { 00164 00165 } elseif (0xFEFF != $mUcs4 && // omit BOM 00166 // check for valid Char unicode codepoints 00167 ( 00168 0x9 == $mUcs4 || 00169 0xA == $mUcs4 || 00170 0xD == $mUcs4 || 00171 (0x20 <= $mUcs4 && 0x7E >= $mUcs4) || 00172 // 7F-9F is not strictly prohibited by XML, 00173 // but it is non-SGML, and thus we don't allow it 00174 (0xA0 <= $mUcs4 && 0xD7FF >= $mUcs4) || 00175 (0x10000 <= $mUcs4 && 0x10FFFF >= $mUcs4) 00176 ) 00177 ) { 00178 $out .= $char; 00179 } 00180 // initialize UTF8 cache (reset) 00181 $mState = 0; 00182 $mUcs4 = 0; 00183 $mBytes = 1; 00184 $char = ''; 00185 } 00186 } else { 00187 // ((0xC0 & (*in) != 0x80) && (mState != 0)) 00188 // Incomplete multi-octet sequence. 00189 // used to result in complete fail, but we'll reset 00190 $mState = 0; 00191 $mUcs4 = 0; 00192 $mBytes = 1; 00193 $char =''; 00194 } 00195 } 00196 } 00197 return $out; 00198 } 00199 00213 // +----------+----------+----------+----------+ 00214 // | 33222222 | 22221111 | 111111 | | 00215 // | 10987654 | 32109876 | 54321098 | 76543210 | bit 00216 // +----------+----------+----------+----------+ 00217 // | | | | 0xxxxxxx | 1 byte 0x00000000..0x0000007F 00218 // | | | 110yyyyy | 10xxxxxx | 2 byte 0x00000080..0x000007FF 00219 // | | 1110zzzz | 10yyyyyy | 10xxxxxx | 3 byte 0x00000800..0x0000FFFF 00220 // | 11110www | 10wwzzzz | 10yyyyyy | 10xxxxxx | 4 byte 0x00010000..0x0010FFFF 00221 // +----------+----------+----------+----------+ 00222 // | 00000000 | 00011111 | 11111111 | 11111111 | Theoretical upper limit of legal scalars: 2097151 (0x001FFFFF) 00223 // | 00000000 | 00010000 | 11111111 | 11111111 | Defined upper limit of legal scalar codes 00224 // +----------+----------+----------+----------+ 00225 00226 public static function unichr($code) { 00227 if($code > 1114111 or $code < 0 or 00228 ($code >= 55296 and $code <= 57343) ) { 00229 // bits are set outside the "valid" range as defined 00230 // by UNICODE 4.1.0 00231 return ''; 00232 } 00233 00234 $x = $y = $z = $w = 0; 00235 if ($code < 128) { 00236 // regular ASCII character 00237 $x = $code; 00238 } else { 00239 // set up bits for UTF-8 00240 $x = ($code & 63) | 128; 00241 if ($code < 2048) { 00242 $y = (($code & 2047) >> 6) | 192; 00243 } else { 00244 $y = (($code & 4032) >> 6) | 128; 00245 if($code < 65536) { 00246 $z = (($code >> 12) & 15) | 224; 00247 } else { 00248 $z = (($code >> 12) & 63) | 128; 00249 $w = (($code >> 18) & 7) | 240; 00250 } 00251 } 00252 } 00253 // set up the actual character 00254 $ret = ''; 00255 if($w) $ret .= chr($w); 00256 if($z) $ret .= chr($z); 00257 if($y) $ret .= chr($y); 00258 $ret .= chr($x); 00259 00260 return $ret; 00261 } 00262 00266 public static function convertToUTF8($str, $config, $context) { 00267 $encoding = $config->get('Core.Encoding'); 00268 if ($encoding === 'utf-8') return $str; 00269 static $iconv = null; 00270 if ($iconv === null) $iconv = function_exists('iconv'); 00271 set_error_handler(array('HTMLPurifier_Encoder', 'muteErrorHandler')); 00272 if ($iconv && !$config->get('Test.ForceNoIconv')) { 00273 $str = iconv($encoding, 'utf-8//IGNORE', $str); 00274 if ($str === false) { 00275 // $encoding is not a valid encoding 00276 restore_error_handler(); 00277 trigger_error('Invalid encoding ' . $encoding, E_USER_ERROR); 00278 return ''; 00279 } 00280 // If the string is bjorked by Shift_JIS or a similar encoding 00281 // that doesn't support all of ASCII, convert the naughty 00282 // characters to their true byte-wise ASCII/UTF-8 equivalents. 00283 $str = strtr($str, HTMLPurifier_Encoder::testEncodingSupportsASCII($encoding)); 00284 restore_error_handler(); 00285 return $str; 00286 } elseif ($encoding === 'iso-8859-1') { 00287 $str = utf8_encode($str); 00288 restore_error_handler(); 00289 return $str; 00290 } 00291 trigger_error('Encoding not supported, please install iconv', E_USER_ERROR); 00292 } 00293 00299 public static function convertFromUTF8($str, $config, $context) { 00300 $encoding = $config->get('Core.Encoding'); 00301 if ($encoding === 'utf-8') return $str; 00302 static $iconv = null; 00303 if ($iconv === null) $iconv = function_exists('iconv'); 00304 if ($escape = $config->get('Core.EscapeNonASCIICharacters')) { 00305 $str = HTMLPurifier_Encoder::convertToASCIIDumbLossless($str); 00306 } 00307 set_error_handler(array('HTMLPurifier_Encoder', 'muteErrorHandler')); 00308 if ($iconv && !$config->get('Test.ForceNoIconv')) { 00309 // Undo our previous fix in convertToUTF8, otherwise iconv will barf 00310 $ascii_fix = HTMLPurifier_Encoder::testEncodingSupportsASCII($encoding); 00311 if (!$escape && !empty($ascii_fix)) { 00312 $clear_fix = array(); 00313 foreach ($ascii_fix as $utf8 => $native) $clear_fix[$utf8] = ''; 00314 $str = strtr($str, $clear_fix); 00315 } 00316 $str = strtr($str, array_flip($ascii_fix)); 00317 // Normal stuff 00318 $str = iconv('utf-8', $encoding . '//IGNORE', $str); 00319 restore_error_handler(); 00320 return $str; 00321 } elseif ($encoding === 'iso-8859-1') { 00322 $str = utf8_decode($str); 00323 restore_error_handler(); 00324 return $str; 00325 } 00326 trigger_error('Encoding not supported', E_USER_ERROR); 00327 } 00328 00345 public static function convertToASCIIDumbLossless($str) { 00346 $bytesleft = 0; 00347 $result = ''; 00348 $working = 0; 00349 $len = strlen($str); 00350 for( $i = 0; $i < $len; $i++ ) { 00351 $bytevalue = ord( $str[$i] ); 00352 if( $bytevalue <= 0x7F ) { //0xxx xxxx 00353 $result .= chr( $bytevalue ); 00354 $bytesleft = 0; 00355 } elseif( $bytevalue <= 0xBF ) { //10xx xxxx 00356 $working = $working << 6; 00357 $working += ($bytevalue & 0x3F); 00358 $bytesleft--; 00359 if( $bytesleft <= 0 ) { 00360 $result .= "&#" . $working . ";"; 00361 } 00362 } elseif( $bytevalue <= 0xDF ) { //110x xxxx 00363 $working = $bytevalue & 0x1F; 00364 $bytesleft = 1; 00365 } elseif( $bytevalue <= 0xEF ) { //1110 xxxx 00366 $working = $bytevalue & 0x0F; 00367 $bytesleft = 2; 00368 } else { //1111 0xxx 00369 $working = $bytevalue & 0x07; 00370 $bytesleft = 3; 00371 } 00372 } 00373 return $result; 00374 } 00375 00387 public static function testEncodingSupportsASCII($encoding, $bypass = false) { 00388 static $encodings = array(); 00389 if (!$bypass) { 00390 if (isset($encodings[$encoding])) return $encodings[$encoding]; 00391 $lenc = strtolower($encoding); 00392 switch ($lenc) { 00393 case 'shift_jis': 00394 return array("\xC2\xA5" => '\\', "\xE2\x80\xBE" => '~'); 00395 case 'johab': 00396 return array("\xE2\x82\xA9" => '\\'); 00397 } 00398 if (strpos($lenc, 'iso-8859-') === 0) return array(); 00399 } 00400 $ret = array(); 00401 set_error_handler(array('HTMLPurifier_Encoder', 'muteErrorHandler')); 00402 if (iconv('UTF-8', $encoding, 'a') === false) return false; 00403 for ($i = 0x20; $i <= 0x7E; $i++) { // all printable ASCII chars 00404 $c = chr($i); // UTF-8 char 00405 $r = iconv('UTF-8', "$encoding//IGNORE", $c); // initial conversion 00406 if ( 00407 $r === '' || 00408 // This line is needed for iconv implementations that do not 00409 // omit characters that do not exist in the target character set 00410 ($r === $c && iconv($encoding, 'UTF-8//IGNORE', $r) !== $c) 00411 ) { 00412 // Reverse engineer: what's the UTF-8 equiv of this byte 00413 // sequence? This assumes that there's no variable width 00414 // encoding that doesn't support ASCII. 00415 $ret[iconv($encoding, 'UTF-8//IGNORE', $c)] = $c; 00416 } 00417 } 00418 restore_error_handler(); 00419 $encodings[$encoding] = $ret; 00420 return $ret; 00421 } 00422 00423 00424 } 00425 00426 // vim: et sw=4 sts=4