Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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