string.lib.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Specialized String Functions for phpMyAdmin
  5. *
  6. * Defines a set of function callbacks that have a pure C version available if
  7. * the "ctype" extension is available, but otherwise have PHP versions to use
  8. * (that are slower).
  9. *
  10. * The SQL Parser code relies heavily on these functions.
  11. *
  12. * @todo a .lib filename should not have code in main(), split or rename file
  13. * @package PhpMyAdmin-String
  14. */
  15. if (! defined('PHPMYADMIN')) {
  16. exit;
  17. }
  18. /**
  19. * Load proper code for handling input.
  20. */
  21. if (@function_exists('mb_strlen')) {
  22. mb_internal_encoding('utf-8');
  23. include './libraries/string_mb.lib.php';
  24. } else {
  25. include './libraries/string_native.lib.php';
  26. }
  27. /**
  28. * Load ctype handler.
  29. */
  30. if (@extension_loaded('ctype')) {
  31. include './libraries/string_type_ctype.lib.php';
  32. } else {
  33. include './libraries/string_type_native.lib.php';
  34. }
  35. /**
  36. * Checks if a given character position in the string is escaped or not
  37. *
  38. * @param string $string string to check for
  39. * @param integer $pos the character to check for
  40. * @param integer $start starting position in the string
  41. *
  42. * @return boolean whether the character is escaped or not
  43. */
  44. function PMA_STR_charIsEscaped($string, $pos, $start = 0)
  45. {
  46. $pos = max(intval($pos), 0);
  47. $start = max(intval($start), 0);
  48. $len = PMA_strlen($string);
  49. // Base case:
  50. // Check for string length or invalid input or special case of input
  51. // (pos == $start)
  52. if ($pos <= $start || $len <= max($pos, $start)) {
  53. return false;
  54. }
  55. $pos--;
  56. $escaped = false;
  57. while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
  58. $escaped = !$escaped;
  59. $pos--;
  60. } // end while
  61. return $escaped;
  62. } // end of the "PMA_STR_charIsEscaped()" function
  63. /**
  64. * Checks if a number is in a range
  65. *
  66. * @param integer $num number to check for
  67. * @param integer $lower lower bound
  68. * @param integer $upper upper bound
  69. *
  70. * @return boolean whether the number is in the range or not
  71. */
  72. function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
  73. {
  74. return ($num >= $lower && $num <= $upper);
  75. } // end of the "PMA_STR_numberInRangeInclusive()" function
  76. /**
  77. * Checks if a character is an SQL identifier
  78. *
  79. * @param string $c character to check for
  80. * @param boolean $dot_is_valid whether the dot character is valid or not
  81. *
  82. * @return boolean whether the character is an SQL identifier or not
  83. */
  84. function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
  85. {
  86. return (PMA_STR_isAlnum($c)
  87. || ($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249
  88. || $c == '_'
  89. || $c == '$'
  90. || ($dot_is_valid != false && $c == '.'));
  91. } // end of the "PMA_STR_isSqlIdentifier()" function
  92. ?>