Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?php
  2.  
  3. /** No bugs detected in iconv. */
  4. const ICONV_OK = 0;
  5.  
  6. /** Iconv truncates output if converting from UTF-8 to another
  7. * character set with //IGNORE, and a non-encodable character is found */
  8. const ICONV_TRUNCATES = 1;
  9.  
  10. /** Iconv does not support //IGNORE, making it unusable for
  11. * transcoding purposes */
  12. const ICONV_UNUSABLE = 2;
  13.  
  14.  
  15. /**
  16. * iconv wrapper which mutes errors, but doesn't work around bugs.
  17. * @param string $in Input encoding
  18. * @param string $out Output encoding
  19. * @param string $text The text to convert
  20. * @return string
  21. */
  22. function unsafeIconv($in, $out, $text)
  23. {
  24. set_error_handler( 'muteErrorHandler');
  25. $r = iconv($in, $out, $text);
  26. restore_error_handler();
  27. return $r;
  28. }
  29.  
  30.  
  31. function muteErrorHandler($errno, $errstr)
  32. {
  33. }
  34.  
  35. /**
  36. * glibc iconv has a known bug where it doesn't handle the magic
  37. * //IGNORE stanza correctly. In particular, rather than ignore
  38. * characters, it will return an EILSEQ after consuming some number
  39. * of characters, and expect you to restart iconv as if it were
  40. * an E2BIG. Old versions of PHP did not respect the errno, and
  41. * returned the fragment, so as a result you would see iconv
  42. * mysteriously truncating output. We can work around this by
  43. * manually chopping our input into segments of about 8000
  44. * characters, as long as PHP ignores the error code. If PHP starts
  45. * paying attention to the error code, iconv becomes unusable.
  46. *
  47. * @return int Error code indicating severity of bug.
  48. */
  49. function testIconvTruncateBug()
  50. {
  51. // better not use iconv, otherwise infinite loop!
  52. $r = unsafeIconv('utf-8', 'ascii//IGNORE', "\xCE\xB1" . str_repeat('a', 9000));
  53. if ($r === false) {
  54. $code = ICONV_UNUSABLE;
  55. } elseif (($c = strlen($r)) < 9000) {
  56. $code = ICONV_TRUNCATES;
  57. } elseif ($c > 9000) {
  58. trigger_error(
  59. 'Your copy of iconv is extremely buggy. Please notify HTML Purifier maintainers: ' .
  60. 'include your iconv version as per phpversion()',
  61. E_USER_ERROR
  62. );
  63. } else {
  64. $code = ICONV_OK;
  65. }
  66. return $code;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement