PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/string.lib.php

https://bitbucket.org/DenizYldrm/openemr
PHP | 226 lines | 155 code | 10 blank | 61 comment | 25 complexity | daca7b9f9aef2146f67e0f8ca13c2fcf MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, MPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  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. * @version $Id$
  16. * @uses PMA_PHP_INT_VERSION
  17. * @uses PMA_dl()
  18. * @uses extension_loaded()
  19. * @uses substr()
  20. * @uses function_exists()
  21. * @uses mb_internal_encoding()
  22. * @uses defined()
  23. * @todo a .lib filename should not have code in main(), split or rename file
  24. */
  25. if (! defined('PHPMYADMIN')) {
  26. exit;
  27. }
  28. /* Try to load mbstring, unless we're using buggy php version */
  29. if (PMA_PHP_INT_VERSION != 40203) {
  30. if (!@extension_loaded('mbstring')) {
  31. PMA_dl('mbstring');
  32. }
  33. }
  34. /**
  35. * windows-* and tis-620 are not supported and are not multibyte,
  36. * others can be ignored as they're not multibyte
  37. *
  38. * @global boolean $GLOBALS['using_mb_charset']
  39. */
  40. $GLOBALS['using_mb_charset'] =
  41. substr($GLOBALS['charset'], 0, 8) != 'windows-' &&
  42. substr($GLOBALS['charset'], 0, 9) != 'iso-8859-' &&
  43. substr($GLOBALS['charset'], 0, 3) != 'cp-' &&
  44. $GLOBALS['charset'] != 'koi8-r' &&
  45. $GLOBALS['charset'] != 'tis-620';
  46. $GLOBALS['PMA_allow_mbstr'] = @function_exists('mb_strlen') && $GLOBALS['using_mb_charset'];
  47. if ($GLOBALS['PMA_allow_mbstr']) {
  48. // the hebrew lang file uses iso-8859-8-i, encoded RTL,
  49. // but mb_internal_encoding only supports iso-8859-8
  50. if ($GLOBALS['charset'] == 'iso-8859-8-i'){
  51. mb_internal_encoding('iso-8859-8');
  52. } else {
  53. mb_internal_encoding($GLOBALS['charset']);
  54. }
  55. }
  56. // This is for handling input better
  57. if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
  58. $GLOBALS['PMA_strpos'] = 'mb_strpos';
  59. require './libraries/string_mb.lib.php';
  60. } else {
  61. $GLOBALS['PMA_strpos'] = 'strpos';
  62. require './libraries/string_native.lib.php';
  63. }
  64. if (!@extension_loaded('ctype')) {
  65. PMA_dl('ctype');
  66. }
  67. if (@extension_loaded('ctype')) {
  68. require './libraries/string_type_ctype.lib.php';
  69. } else {
  70. require './libraries/string_type_native.lib.php';
  71. }
  72. /**
  73. * This checks if a string actually exists inside another string
  74. * We try to do it in a PHP3-portable way.
  75. * We don't care about the position it is in.
  76. *
  77. * @uses PMA_STR_pos()
  78. * @param string string to search for
  79. * @param string string to search in
  80. * @return boolean whether the needle is in the haystack or not
  81. * @todo rename PMA_STR_inStr()
  82. */
  83. function PMA_STR_strInStr($needle, $haystack)
  84. {
  85. // PMA_STR_pos($haystack, $needle) !== false
  86. // return (is_integer(PMA_STR_pos($haystack, $needle)));
  87. return (bool) PMA_STR_pos(' ' . $haystack, $needle);
  88. } // end of the "PMA_STR_strInStr()" function
  89. /**
  90. * Checks if a given character position in the string is escaped or not
  91. *
  92. * @uses PMA_strlen()
  93. * @uses PMA_substr()
  94. * @uses max()
  95. * @uses intval()
  96. * @param string string to check for
  97. * @param integer the character to check for
  98. * @param integer starting position in the string
  99. * @return boolean whether the character is escaped or not
  100. */
  101. function PMA_STR_charIsEscaped($string, $pos, $start = 0)
  102. {
  103. $pos = max(intval($pos), 0);
  104. $start = max(intval($start), 0);
  105. $len = PMA_strlen($string);
  106. // Base case:
  107. // Check for string length or invalid input or special case of input
  108. // (pos == $start)
  109. if ($pos <= $start || $len <= max($pos, $start)) {
  110. return false;
  111. }
  112. $pos--;
  113. $escaped = false;
  114. while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
  115. $escaped = !$escaped;
  116. $pos--;
  117. } // end while
  118. return $escaped;
  119. } // end of the "PMA_STR_charIsEscaped()" function
  120. /**
  121. * Checks if a number is in a range
  122. *
  123. * @param integer number to check for
  124. * @param integer lower bound
  125. * @param integer upper bound
  126. * @return boolean whether the number is in the range or not
  127. */
  128. function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
  129. {
  130. return ($num >= $lower && $num <= $upper);
  131. } // end of the "PMA_STR_numberInRangeInclusive()" function
  132. /**
  133. * Checks if a character is an accented character
  134. *
  135. * Presently this only works for some character sets. More work may be needed
  136. * to fix it.
  137. *
  138. * @uses PMA_STR_numberInRangeInclusive()
  139. * @uses ord()
  140. * @param string character to check for
  141. * @return boolean whether the character is an accented one or not
  142. */
  143. function PMA_STR_isAccented($c)
  144. {
  145. $ord_min1 = 192; //ord('A');
  146. $ord_max1 = 214; //ord('Z');
  147. $ord_min2 = 216; //ord('A');
  148. $ord_max2 = 246; //ord('Z');
  149. $ord_min3 = 248; //ord('A');
  150. $ord_max3 = 255; //ord('Z');
  151. $ord_c = ord($c);
  152. return PMA_STR_numberInRangeInclusive($ord_c, $ord_min1, $ord_max1)
  153. || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2)
  154. || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2);
  155. } // end of the "PMA_STR_isAccented()" function
  156. /**
  157. * Checks if a character is an SQL identifier
  158. *
  159. * @uses PMA_STR_isAlnum()
  160. * @uses PMA_STR_isAccented()
  161. * @param string character to check for
  162. * @param boolean whether the dot character is valid or not
  163. * @return boolean whether the character is an SQL identifier or not
  164. */
  165. function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
  166. {
  167. return (PMA_STR_isAlnum($c)
  168. || PMA_STR_isAccented($c)
  169. || $c == '_'
  170. || $c == '$'
  171. || ($dot_is_valid != false && $c == '.'));
  172. } // end of the "PMA_STR_isSqlIdentifier()" function
  173. /**
  174. * Binary search of a value in a sorted array
  175. *
  176. * $arr MUST be sorted, due to binary search
  177. *
  178. * @param string string to search for
  179. * @param array sorted array to search into
  180. * @param integer size of sorted array to search into
  181. *
  182. * @return boolean whether the string has been found or not
  183. */
  184. function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
  185. {
  186. $top = $arrsize - 1;
  187. $bottom = 0;
  188. $found = false;
  189. while ($top >= $bottom && $found == false) {
  190. $mid = intval(($top + $bottom) / 2);
  191. $res = strcmp($str, $arr[$mid]);
  192. if ($res == 0) {
  193. $found = true;
  194. } elseif ($res < 0) {
  195. $top = $mid - 1;
  196. } else {
  197. $bottom = $mid + 1;
  198. }
  199. } // end while
  200. return $found;
  201. } // end of the "PMA_STR_binarySearchInArr()" function
  202. ?>