String.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Specialized string class for phpMyAdmin.
  4. * The SQL Parser code relies heavily on these functions.
  5. *
  6. * @package PhpMyAdmin-String
  7. */
  8. /**
  9. * Specialized string class for phpMyAdmin.
  10. * The SQL Parser code relies heavily on these functions.
  11. *
  12. * @package PhpMyAdmin-String
  13. */
  14. class PMA_String
  15. {
  16. /**
  17. * @var PMA_StringType
  18. */
  19. private $_type;
  20. /**
  21. * @var PMA_StringByte
  22. */
  23. private $_byte;
  24. /**
  25. * Constructor
  26. */
  27. public function __construct()
  28. {
  29. if (@function_exists('mb_strlen')) {
  30. mb_internal_encoding('utf-8');
  31. include_once 'libraries/StringMB.class.php';
  32. $this->_byte = new PMA_StringMB();
  33. } else {
  34. include_once 'libraries/StringNative.class.php';
  35. $this->_byte = new PMA_StringNative();
  36. }
  37. if (@extension_loaded('ctype')) {
  38. include_once 'libraries/StringCType.class.php';
  39. $this->_type = new PMA_StringCType();
  40. } else {
  41. include_once 'libraries/StringNativeType.class.php';
  42. $this->_type = new PMA_StringNativeType();
  43. }
  44. }
  45. /**
  46. * Checks if a given character position in the string is escaped or not
  47. *
  48. * @param string $string string to check for
  49. * @param integer $pos the character to check for
  50. * @param integer $start starting position in the string
  51. *
  52. * @return boolean whether the character is escaped or not
  53. */
  54. public function charIsEscaped($string, $pos, $start = 0)
  55. {
  56. $pos = max(intval($pos), 0);
  57. $start = max(intval($start), 0);
  58. $len = $this->strlen($string);
  59. // Base case:
  60. // Check for string length or invalid input or special case of input
  61. // (pos == $start)
  62. if ($pos <= $start || $len <= max($pos, $start)) {
  63. return false;
  64. }
  65. $pos--;
  66. $escaped = false;
  67. while ($pos >= $start && $this->substr($string, $pos, 1) == '\\') {
  68. $escaped = !$escaped;
  69. $pos--;
  70. } // end while
  71. return $escaped;
  72. }
  73. /**
  74. * Checks if a character is an SQL identifier
  75. *
  76. * @param string $c character to check for
  77. * @param boolean $dot_is_valid whether the dot character is valid or not
  78. *
  79. * @return boolean whether the character is an SQL identifier or not
  80. */
  81. public function isSqlIdentifier($c, $dot_is_valid = false)
  82. {
  83. return ($this->isAlnum($c)
  84. || ($ord_c = $this->ord($c)) && $ord_c >= 192 && $ord_c != 215 &&
  85. $ord_c != 249
  86. || $c == '_'
  87. || $c == '$'
  88. || ($dot_is_valid != false && $c == '.'));
  89. }
  90. /**
  91. * Returns length of string depending on current charset.
  92. *
  93. * @param string $string string to count
  94. *
  95. * @return int string length
  96. */
  97. public function strlen($string)
  98. {
  99. return $this->_byte->strlen($string);
  100. }
  101. /**
  102. * Returns substring from string, works depending on current charset.
  103. *
  104. * @param string $string string to count
  105. * @param int $start start of substring
  106. * @param int $length length of substring
  107. *
  108. * @return string the sub string
  109. */
  110. public function substr($string, $start, $length = 2147483647)
  111. {
  112. return $this->_byte->substr($string, $start, $length);
  113. }
  114. /**
  115. * Returns postion of $needle in $haystack or false if not found
  116. *
  117. * @param string $haystack the string being checked
  118. * @param string $needle the string to find in haystack
  119. * @param int $offset the search offset
  120. *
  121. * @return integer position of $needle in $haystack or false
  122. */
  123. public function strpos($haystack, $needle, $offset = 0)
  124. {
  125. return $this->_byte->strpos($haystack, $needle, $offset);
  126. }
  127. /**
  128. * Make a string lowercase
  129. *
  130. * @param string $string the string being lowercased
  131. *
  132. * @return string the lower case string
  133. */
  134. public function strtolower($string)
  135. {
  136. return $this->_byte->strtolower($string);
  137. }
  138. /**
  139. * Get the ordinal value of a string
  140. *
  141. * @param string $string the string for which ord is required
  142. *
  143. * @return string the ord value
  144. */
  145. public function ord($string)
  146. {
  147. return $this->_byte->ord($string);
  148. }
  149. /**
  150. * Checks if a character is an alphanumeric one
  151. *
  152. * @param string $c character to check for
  153. *
  154. * @return boolean whether the character is an alphanumeric one or not
  155. */
  156. public function isAlnum($c)
  157. {
  158. return $this->_type->isAlnum($c);
  159. }
  160. /**
  161. * Checks if a character is an alphabetic one
  162. *
  163. * @param string $c character to check for
  164. *
  165. * @return boolean whether the character is an alphabetic one or not
  166. */
  167. public function isAlpha($c)
  168. {
  169. return $this->_type->isAlpha($c);
  170. }
  171. /**
  172. * Checks if a character is a digit
  173. *
  174. * @param string $c character to check for
  175. *
  176. * @return boolean whether the character is a digit or not
  177. */
  178. public function isDigit($c)
  179. {
  180. return $this->_type->isDigit($c);
  181. }
  182. /**
  183. * Checks if a character is an upper alphabetic one
  184. *
  185. * @param string $c character to check for
  186. *
  187. * @return boolean whether the character is an upper alphabetic one or not
  188. */
  189. public function isUpper($c)
  190. {
  191. return $this->_type->isUpper($c);
  192. }
  193. /**
  194. * Checks if a character is a lower alphabetic one
  195. *
  196. * @param string $c character to check for
  197. *
  198. * @return boolean whether the character is a lower alphabetic one or not
  199. */
  200. public function isLower($c)
  201. {
  202. return $this->_type->isLower($c);
  203. }
  204. /**
  205. * Checks if a character is a space one
  206. *
  207. * @param string $c character to check for
  208. *
  209. * @return boolean whether the character is a space one or not
  210. */
  211. public function isSpace($c)
  212. {
  213. return $this->_type->isSpace($c);
  214. }
  215. /**
  216. * Checks if a character is an hexadecimal digit
  217. *
  218. * @param string $c character to check for
  219. *
  220. * @return boolean whether the character is an hexadecimal digit or not
  221. */
  222. public function isHexDigit($c)
  223. {
  224. return $this->_type->isHexDigit($c);
  225. }
  226. /**
  227. * Checks if a number is in a range
  228. *
  229. * @param integer $num number to check for
  230. * @param integer $lower lower bound
  231. * @param integer $upper upper bound
  232. *
  233. * @return boolean whether the number is in the range or not
  234. */
  235. public function numberInRangeInclusive($num, $lower, $upper)
  236. {
  237. return $this->_type->numberInRangeInclusive($num, $lower, $upper);
  238. }
  239. }
  240. ?>