PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/install/data/new_normalizer.php

http://github.com/phpbb/phpbb3
PHP | 197 lines | 112 code | 23 blank | 62 comment | 19 complexity | 79001f1e5de231a896c71af50ecc078c MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. /**
  14. * @ignore
  15. */
  16. if (!defined('IN_PHPBB'))
  17. {
  18. exit;
  19. }
  20. /**
  21. * A wrapper function for the normalizer which takes care of including the class if required and modifies the passed strings
  22. * to be in NFC (Normalization Form Composition).
  23. *
  24. * @param mixed $strings a string or an array of strings to normalize
  25. * @return mixed the normalized content, preserving array keys if array given.
  26. */
  27. function utf8_new_normalize_nfc($strings)
  28. {
  29. if (empty($strings))
  30. {
  31. return $strings;
  32. }
  33. if (!is_array($strings))
  34. {
  35. utf_new_normalizer::nfc($strings);
  36. }
  37. else if (is_array($strings))
  38. {
  39. foreach ($strings as $key => $string)
  40. {
  41. if (is_array($string))
  42. {
  43. foreach ($string as $_key => $_string)
  44. {
  45. utf_new_normalizer::nfc($strings[$key][$_key]);
  46. }
  47. }
  48. else
  49. {
  50. utf_new_normalizer::nfc($strings[$key]);
  51. }
  52. }
  53. }
  54. return $strings;
  55. }
  56. class utf_new_normalizer
  57. {
  58. /**
  59. * Validate, cleanup and normalize a string
  60. *
  61. * The ultimate convenience function! Clean up invalid UTF-8 sequences,
  62. * and convert to Normal Form C, canonical composition.
  63. *
  64. * @param string &$str The dirty string
  65. * @return string The same string, all shiny and cleaned-up
  66. */
  67. function cleanup(&$str)
  68. {
  69. // The string below is the list of all autorized characters, sorted by frequency in latin text
  70. $pos = strspn($str, "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x0D");
  71. $len = strlen($str);
  72. if ($pos == $len)
  73. {
  74. // ASCII strings with no special chars return immediately
  75. return;
  76. }
  77. // Note: we do not check for $GLOBALS['utf_canonical_decomp']. It is assumed they are always loaded together
  78. if (!isset($GLOBALS['utf_nfc_qc']))
  79. {
  80. global $phpbb_root_path, $phpEx;
  81. include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
  82. }
  83. if (!isset($GLOBALS['utf_canonical_decomp']))
  84. {
  85. global $phpbb_root_path, $phpEx;
  86. include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
  87. }
  88. // Replace any byte in the range 0x00..0x1F, except for \r, \n and \t
  89. // We replace those characters with a 0xFF byte, which is illegal in UTF-8 and will in turn be replaced with a UTF replacement char
  90. $str = strtr(
  91. $str,
  92. "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
  93. "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
  94. );
  95. $str = utf_new_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
  96. }
  97. /**
  98. * Validate and normalize a UTF string to NFC
  99. *
  100. * @param string &$str Unchecked UTF string
  101. * @return string The string, validated and in normal form
  102. */
  103. function nfc(&$str)
  104. {
  105. $pos = strspn($str, UTF8_ASCII_RANGE);
  106. $len = strlen($str);
  107. if ($pos == $len)
  108. {
  109. // ASCII strings return immediately
  110. return;
  111. }
  112. if (!isset($GLOBALS['utf_nfc_qc']))
  113. {
  114. global $phpbb_root_path, $phpEx;
  115. include($phpbb_root_path . 'includes/utf/data/utf_nfc_qc.' . $phpEx);
  116. }
  117. if (!isset($GLOBALS['utf_canonical_decomp']))
  118. {
  119. global $phpbb_root_path, $phpEx;
  120. include($phpbb_root_path . 'includes/utf/data/utf_canonical_decomp.' . $phpEx);
  121. }
  122. $str = utf_new_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfc_qc'], $GLOBALS['utf_canonical_decomp']);
  123. }
  124. /**
  125. * Validate and normalize a UTF string to NFKC
  126. *
  127. * @param string &$str Unchecked UTF string
  128. * @return string The string, validated and in normal form
  129. */
  130. function nfkc(&$str)
  131. {
  132. $pos = strspn($str, UTF8_ASCII_RANGE);
  133. $len = strlen($str);
  134. if ($pos == $len)
  135. {
  136. // ASCII strings return immediately
  137. return;
  138. }
  139. if (!isset($GLOBALS['utf_nfkc_qc']))
  140. {
  141. global $phpbb_root_path, $phpEx;
  142. include($phpbb_root_path . 'includes/utf/data/utf_nfkc_qc.' . $phpEx);
  143. }
  144. if (!isset($GLOBALS['utf_compatibility_decomp']))
  145. {
  146. global $phpbb_root_path, $phpEx;
  147. include($phpbb_root_path . 'includes/utf/data/utf_compatibility_decomp.' . $phpEx);
  148. }
  149. $str = utf_new_normalizer::recompose($str, $pos, $len, $GLOBALS['utf_nfkc_qc'], $GLOBALS['utf_compatibility_decomp']);
  150. }
  151. /**
  152. * Recompose a UTF string
  153. *
  154. * @param string $str Unchecked UTF string
  155. * @param integer $pos Position of the first UTF char (in bytes)
  156. * @param integer $len Length of the string (in bytes)
  157. * @param array &$qc Quick-check array, passed by reference but never modified
  158. * @param array &$decomp_map Decomposition mapping, passed by reference but never modified
  159. * @return string The string, validated and recomposed
  160. *
  161. * @access private
  162. */
  163. function recompose($str, $pos, $len, &$qc, &$decomp_map)
  164. {
  165. global $utf_canonical_comp;
  166. // Load the canonical composition table
  167. if (!isset($utf_canonical_comp))
  168. {
  169. global $phpbb_root_path, $phpEx;
  170. include($phpbb_root_path . 'includes/utf/data/utf_canonical_comp.' . $phpEx);
  171. }
  172. return utf_normalizer::recompose($str, $pos, $len, $qc, $decomp_map);
  173. }
  174. }