PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phpMyAdmin/libraries/string.lib.php

https://bitbucket.org/jojoluzifer/web-megagenius
PHP | 384 lines | 259 code | 25 blank | 100 comment | 52 complexity | e206eaf48ce99043d79e6c1e1212d570 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, ISC
  1. <?php
  2. /* $Id: string.lib.php 8301 2006-01-17 17:03:02Z cybot_tm $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. /** Specialized String Functions for phpMyAdmin
  5. *
  6. * Copyright 2002 Robin Johnson <robbat2@users.sourceforge.net>
  7. * http://www.orbis-terrarum.net/?l=people.robbat2
  8. *
  9. * Defines a set of function callbacks that have a pure C version available if
  10. * the "ctype" extension is available, but otherwise have PHP versions to use
  11. * (that are slower).
  12. *
  13. * The SQL Parser code relies heavily on these functions.
  14. */
  15. /* Try to load mbstring, unless we're using buggy php version */
  16. if (PMA_PHP_INT_VERSION != 40203) {
  17. if (!@extension_loaded('mbstring')) {
  18. PMA_dl('mbstring');
  19. }
  20. }
  21. /* windows-* and tis-620 are not supported and are not multibyte,
  22. * others can be ignored as they're not multibyte */
  23. $GLOBALS['using_mb_charset'] =
  24. substr($GLOBALS['charset'], 0, 8) != 'windows-' &&
  25. substr($GLOBALS['charset'], 0, 9) != 'iso-8859-' &&
  26. substr($GLOBALS['charset'], 0, 3) != 'cp-' &&
  27. $GLOBALS['charset'] != 'koi8-r' &&
  28. $GLOBALS['charset'] != 'tis-620';
  29. $GLOBALS['PMA_allow_mbstr'] = @function_exists('mb_strlen') && $GLOBALS['using_mb_charset'];
  30. if ($GLOBALS['PMA_allow_mbstr']) {
  31. // the hebrew lang file uses iso-8859-8-i, encoded RTL,
  32. // but mb_internal_encoding only supports iso-8859-8
  33. if ($GLOBALS['charset'] == 'iso-8859-8-i'){
  34. mb_internal_encoding('iso-8859-8');
  35. } else {
  36. mb_internal_encoding($GLOBALS['charset']);
  37. }
  38. }
  39. // This is for handling input better
  40. if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
  41. $GLOBALS['PMA_strpos'] = 'mb_strpos';
  42. $GLOBALS['PMA_strrpos'] = 'mb_strrpos';
  43. } else {
  44. $GLOBALS['PMA_strpos'] = 'strpos';
  45. $GLOBALS['PMA_strrpos'] = 'strrpos';
  46. }
  47. /**
  48. * Returns length of string depending on current charset.
  49. *
  50. * @param string string to count
  51. *
  52. * @return int string length
  53. *
  54. * @access public
  55. *
  56. * @author nijel
  57. */
  58. function PMA_strlen($string)
  59. {
  60. // windows-* charsets are not multibyte and not supported by mb_*
  61. if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
  62. return mb_strlen($string);
  63. } else {
  64. return strlen($string);
  65. }
  66. }
  67. /**
  68. * Returns substring from string, works depending on current charset.
  69. *
  70. * @param string string to count
  71. * @param int start of substring
  72. * @param int length of substring
  73. *
  74. * @return int substring
  75. *
  76. * @access public
  77. *
  78. * @author nijel
  79. */
  80. function PMA_substr($string, $start, $length = 2147483647)
  81. {
  82. if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
  83. return mb_substr($string, $start, $length);
  84. } else {
  85. return substr($string, $start, $length);
  86. }
  87. }
  88. /**
  89. * This checks if a string actually exists inside another string
  90. * We try to do it in a PHP3-portable way.
  91. * We don't care about the position it is in.
  92. *
  93. * @param string string to search for
  94. * @param string string to search in
  95. *
  96. * @return boolean whether the needle is in the haystack or not
  97. */
  98. function PMA_STR_strInStr($needle, $haystack)
  99. {
  100. // $GLOBALS['PMA_strpos']($haystack, $needle) !== FALSE
  101. // return (is_integer($GLOBALS['PMA_strpos']($haystack, $needle)));
  102. return $GLOBALS['PMA_strpos'](' ' . $haystack, $needle);
  103. } // end of the "PMA_STR_strInStr()" function
  104. /**
  105. * Checks if a given character position in the string is escaped or not
  106. *
  107. * @param string string to check for
  108. * @param integer the character to check for
  109. * @param integer starting position in the string
  110. *
  111. * @return boolean whether the character is escaped or not
  112. */
  113. function PMA_STR_charIsEscaped($string, $pos, $start = 0)
  114. {
  115. $len = PMA_strlen($string);
  116. // Base case:
  117. // Check for string length or invalid input or special case of input
  118. // (pos == $start)
  119. if (($pos == $start) || ($len <= $pos)) {
  120. return FALSE;
  121. }
  122. $p = $pos - 1;
  123. $escaped = FALSE;
  124. while (($p >= $start) && (PMA_substr($string, $p, 1) == '\\')) {
  125. $escaped = !$escaped;
  126. $p--;
  127. } // end while
  128. if ($pos < $start) {
  129. // throw error about strings
  130. }
  131. return $escaped;
  132. } // end of the "PMA_STR_charIsEscaped()" function
  133. /**
  134. * Checks if a number is in a range
  135. *
  136. * @param integer number to check for
  137. * @param integer lower bound
  138. * @param integer upper bound
  139. *
  140. * @return boolean whether the number is in the range or not
  141. */
  142. function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
  143. {
  144. return (($num >= $lower) && ($num <= $upper));
  145. } // end of the "PMA_STR_numberInRangeInclusive()" function
  146. /**
  147. * Checks if a character is a digit
  148. *
  149. * @param string character to check for
  150. *
  151. * @return boolean whether the character is a digit or not
  152. *
  153. * @see PMA_STR_numberInRangeInclusive()
  154. */
  155. function PMA_STR_isDigit($c)
  156. {
  157. $ord_zero = 48; //ord('0');
  158. $ord_nine = 57; //ord('9');
  159. $ord_c = ord($c);
  160. return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
  161. } // end of the "PMA_STR_isDigit()" function
  162. /**
  163. * Checks if a character is an hexadecimal digit
  164. *
  165. * @param string character to check for
  166. *
  167. * @return boolean whether the character is an hexadecimal digit or not
  168. *
  169. * @see PMA_STR_numberInRangeInclusive()
  170. */
  171. function PMA_STR_isHexDigit($c)
  172. {
  173. $ord_Aupper = 65; //ord('A');
  174. $ord_Fupper = 70; //ord('F');
  175. $ord_Alower = 97; //ord('a');
  176. $ord_Flower = 102; //ord('f');
  177. $ord_zero = 48; //ord('0');
  178. $ord_nine = 57; //ord('9');
  179. $ord_c = ord($c);
  180. return (PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine)
  181. || PMA_STR_numberInRangeInclusive($ord_c, $ord_Aupper, $ord_Fupper)
  182. || PMA_STR_numberInRangeInclusive($ord_c, $ord_Alower, $ord_Flower));
  183. } // end of the "PMA_STR_isHexDigit()" function
  184. /**
  185. * Checks if a character is an upper alphabetic one
  186. *
  187. * @param string character to check for
  188. *
  189. * @return boolean whether the character is an upper alphabetic one or
  190. * not
  191. *
  192. * @see PMA_STR_numberInRangeInclusive()
  193. */
  194. function PMA_STR_isUpper($c)
  195. {
  196. $ord_zero = 65; //ord('A');
  197. $ord_nine = 90; //ord('Z');
  198. $ord_c = ord($c);
  199. return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
  200. } // end of the "PMA_STR_isUpper()" function
  201. /**
  202. * Checks if a character is a lower alphabetic one
  203. *
  204. * @param string character to check for
  205. *
  206. * @return boolean whether the character is a lower alphabetic one or
  207. * not
  208. *
  209. * @see PMA_STR_numberInRangeInclusive()
  210. */
  211. function PMA_STR_isLower($c)
  212. {
  213. $ord_zero = 97; //ord('a');
  214. $ord_nine = 122; //ord('z');
  215. $ord_c = ord($c);
  216. return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
  217. } // end of the "PMA_STR_isLower()" function
  218. /**
  219. * Checks if a character is an alphabetic one
  220. *
  221. * @param string character to check for
  222. *
  223. * @return boolean whether the character is an alphabetic one or not
  224. *
  225. * @see PMA_STR_isUpper()
  226. * @see PMA_STR_isLower()
  227. */
  228. function PMA_STR_isAlpha($c)
  229. {
  230. return (PMA_STR_isUpper($c) || PMA_STR_isLower($c));
  231. } // end of the "PMA_STR_isAlpha()" function
  232. /**
  233. * Checks if a character is an alphanumeric one
  234. *
  235. * @param string character to check for
  236. *
  237. * @return boolean whether the character is an alphanumeric one or not
  238. *
  239. * @see PMA_STR_isUpper()
  240. * @see PMA_STR_isLower()
  241. * @see PMA_STR_isDigit()
  242. */
  243. function PMA_STR_isAlnum($c)
  244. {
  245. return (PMA_STR_isUpper($c) || PMA_STR_isLower($c) || PMA_STR_isDigit($c));
  246. } // end of the "PMA_STR_isAlnum()" function
  247. /**
  248. * Checks if a character is a space one
  249. *
  250. * @param string character to check for
  251. *
  252. * @return boolean whether the character is a space one or not
  253. *
  254. * @see PMA_STR_numberInRangeInclusive()
  255. */
  256. function PMA_STR_isSpace($c)
  257. {
  258. $ord_space = 32; //ord(' ')
  259. $ord_tab = 9; //ord('\t')
  260. $ord_CR = 13; //ord('\n')
  261. $ord_NOBR = 160; //ord('U+00A0);
  262. $ord_c = ord($c);
  263. return (($ord_c == $ord_space)
  264. || ($ord_c == $ord_NOBR)
  265. || PMA_STR_numberInRangeInclusive($ord_c, $ord_tab, $ord_CR));
  266. } // end of the "PMA_STR_isSpace()" function
  267. /**
  268. * Checks if a character is an accented character
  269. *
  270. * @note Presently this only works for some character sets. More work
  271. * may be needed to fix it.
  272. *
  273. * @param string character to check for
  274. *
  275. * @return boolean whether the character is an accented one or not
  276. *
  277. * @see PMA_STR_numberInRangeInclusive()
  278. */
  279. function PMA_STR_isAccented($c)
  280. {
  281. $ord_min1 = 192; //ord('A');
  282. $ord_max1 = 214; //ord('Z');
  283. $ord_min2 = 216; //ord('A');
  284. $ord_max2 = 246; //ord('Z');
  285. $ord_min3 = 248; //ord('A');
  286. $ord_max3 = 255; //ord('Z');
  287. $ord_c = ord($c);
  288. return PMA_STR_numberInRangeInclusive($ord_c, $ord_min1, $ord_max1)
  289. || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2)
  290. || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2);
  291. } // end of the "PMA_STR_isAccented()" function
  292. /**
  293. * Checks if a character is an SQL identifier
  294. *
  295. * @param string character to check for
  296. * @param boolean whether the dot character is valid or not
  297. *
  298. * @return boolean whether the character is an SQL identifier or not
  299. *
  300. * @see PMA_STR_isAlnum()
  301. */
  302. function PMA_STR_isSqlIdentifier($c, $dot_is_valid = FALSE)
  303. {
  304. return (PMA_STR_isAlnum($c)
  305. || PMA_STR_isAccented($c)
  306. || ($c == '_') || ($c == '$')
  307. || (($dot_is_valid != FALSE) && ($c == '.')));
  308. } // end of the "PMA_STR_isSqlIdentifier()" function
  309. /**
  310. * Binary search of a value in a sorted array
  311. *
  312. * @param string string to search for
  313. * @param array sorted array to search into
  314. * @param integer size of sorted array to search into
  315. *
  316. * @return boolean whether the string has been found or not
  317. */
  318. function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
  319. {
  320. // $arr MUST be sorted, due to binary search
  321. $top = $arrsize - 1;
  322. $bottom = 0;
  323. $found = FALSE;
  324. while (($top >= $bottom) && ($found == FALSE)) {
  325. $mid = intval(($top + $bottom) / 2);
  326. $res = strcmp($str, $arr[$mid]);
  327. if ($res == 0) {
  328. $found = TRUE;
  329. } elseif ($res < 0) {
  330. $top = $mid - 1;
  331. } else {
  332. $bottom = $mid + 1;
  333. }
  334. } // end while
  335. return $found;
  336. } // end of the "PMA_STR_binarySearchInArr()" function
  337. ?>