PageRenderTime 109ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/php/plugins/utf8/utils/validation.php

https://bitbucket.org/chiamingyen/cmsimple-and-plugins
PHP | 188 lines | 72 code | 29 blank | 87 comment | 39 complexity | 61263b88f0101e317d5167f2ea4f0d00 MD5 | raw file
  1. <?php
  2. /**
  3. * Tools for validing a UTF-8 string is well formed.
  4. *
  5. * The Original Code is Mozilla Communicator client code.
  6. * The Initial Developer of the Original Code is
  7. * Netscape Communications Corporation.
  8. * Portions created by the Initial Developer are Copyright (C) 1998
  9. * the Initial Developer. All Rights Reserved.
  10. * Ported to PHP by {@link http://hsivonen.iki.fi Henri Sivonen}.
  11. * Slight modifications to fit with phputf8 library by {@link mailto:hfuecks@gmail.com Harry Fuecks}.
  12. *
  13. * @package utf8
  14. * @subpackage validation
  15. * @version $Id: validation.php 4 2012-08-07 21:08:48Z cmb69 $
  16. * @link http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUTF8ToUnicode.cpp
  17. * @link http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUnicodeToUTF8.cpp
  18. * @link http://hsivonen.iki.fi/php-utf8/
  19. */
  20. /**
  21. * Tests a string as to whether it's valid UTF-8 and supported by the
  22. * Unicode standard
  23. *
  24. * Note: this function has been modified to simply return true or false.
  25. *
  26. * @param string UTF-8 encoded string
  27. * @return boolean true if valid
  28. * @author Henri Sivonen <hsivonen@iki.fi>
  29. * @link http://hsivonen.iki.fi/php-utf8/
  30. * @see utf8_compliant()
  31. */
  32. function utf8_is_valid($str) {
  33. $mState = 0; // cached expected number of octets after the current octet
  34. // until the beginning of the next UTF8 character sequence
  35. $mUcs4 = 0; // cached Unicode character
  36. $mBytes = 1; // cached expected number of octets in the current sequence
  37. $len = strlen($str);
  38. for($i = 0; $i < $len; $i++) {
  39. $in = ord($str{$i});
  40. if ( $mState == 0) {
  41. // When mState is zero we expect either a US-ASCII character or a
  42. // multi-octet sequence.
  43. if (0 == (0x80 & ($in))) {
  44. // US-ASCII, pass straight through.
  45. $mBytes = 1;
  46. } else if (0xC0 == (0xE0 & ($in))) {
  47. // First octet of 2 octet sequence
  48. $mUcs4 = ($in);
  49. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  50. $mState = 1;
  51. $mBytes = 2;
  52. } else if (0xE0 == (0xF0 & ($in))) {
  53. // First octet of 3 octet sequence
  54. $mUcs4 = ($in);
  55. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  56. $mState = 2;
  57. $mBytes = 3;
  58. } else if (0xF0 == (0xF8 & ($in))) {
  59. // First octet of 4 octet sequence
  60. $mUcs4 = ($in);
  61. $mUcs4 = ($mUcs4 & 0x07) << 18;
  62. $mState = 3;
  63. $mBytes = 4;
  64. } else if (0xF8 == (0xFC & ($in))) {
  65. /* First octet of 5 octet sequence.
  66. *
  67. * This is illegal because the encoded codepoint must be either
  68. * (a) not the shortest form or
  69. * (b) outside the Unicode range of 0-0x10FFFF.
  70. * Rather than trying to resynchronize, we will carry on until the end
  71. * of the sequence and let the later error handling code catch it.
  72. */
  73. $mUcs4 = ($in);
  74. $mUcs4 = ($mUcs4 & 0x03) << 24;
  75. $mState = 4;
  76. $mBytes = 5;
  77. } else if (0xFC == (0xFE & ($in))) {
  78. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  79. $mUcs4 = ($in);
  80. $mUcs4 = ($mUcs4 & 1) << 30;
  81. $mState = 5;
  82. $mBytes = 6;
  83. } else {
  84. /* Current octet is neither in the US-ASCII range nor a legal first
  85. * octet of a multi-octet sequence.
  86. */
  87. return FALSE;
  88. }
  89. } else {
  90. // When mState is non-zero, we expect a continuation of the multi-octet
  91. // sequence
  92. if (0x80 == (0xC0 & ($in))) {
  93. // Legal continuation.
  94. $shift = ($mState - 1) * 6;
  95. $tmp = $in;
  96. $tmp = ($tmp & 0x0000003F) << $shift;
  97. $mUcs4 |= $tmp;
  98. /**
  99. * End of the multi-octet sequence. mUcs4 now contains the final
  100. * Unicode codepoint to be output
  101. */
  102. if (0 == --$mState) {
  103. /*
  104. * Check for illegal sequences and codepoints.
  105. */
  106. // From Unicode 3.1, non-shortest form is illegal
  107. if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
  108. ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  109. ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
  110. (4 < $mBytes) ||
  111. // From Unicode 3.2, surrogate characters are illegal
  112. (($mUcs4 & 0xFFFFF800) == 0xD800) ||
  113. // Codepoints outside the Unicode range are illegal
  114. ($mUcs4 > 0x10FFFF)) {
  115. return FALSE;
  116. }
  117. //initialize UTF8 cache
  118. $mState = 0;
  119. $mUcs4 = 0;
  120. $mBytes = 1;
  121. }
  122. } else {
  123. /**
  124. *((0xC0 & (*in) != 0x80) && (mState != 0))
  125. * Incomplete multi-octet sequence.
  126. */
  127. return FALSE;
  128. }
  129. }
  130. }
  131. return $mState == 0;
  132. }
  133. /**
  134. * Tests whether a string complies as UTF-8. This will be much
  135. * faster than utf8_is_valid but will pass five and six octet
  136. * UTF-8 sequences, which are not supported by Unicode and
  137. * so cannot be displayed correctly in a browser. In other words
  138. * it is not as strict as utf8_is_valid but it's faster. If you use
  139. * is to validate user input, you place yourself at the risk that
  140. * attackers will be able to inject 5 and 6 byte sequences (which
  141. * may or may not be a significant risk, depending on what you are
  142. * are doing)
  143. *
  144. * @param string UTF-8 string to check
  145. * @return boolean TRUE if string is valid UTF-8
  146. * @link http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php#54805
  147. * @see utf8_is_valid()
  148. */
  149. function utf8_compliant($str) {
  150. if ( strlen($str) == 0 ) {
  151. return TRUE;
  152. }
  153. // If even just the first character can be matched, when the /u
  154. // modifier is used, then it's valid UTF-8. If the UTF-8 is somehow
  155. // invalid, nothing at all will match, even if the string contains
  156. // some valid sequences
  157. return (preg_match('/^.{1}/us',$str,$ar) == 1);
  158. }
  159. ?>