newline at the end...
[phpmyadmin/crack.git] / libraries / string.lib.php
blobd87dbf7f3f54e6d65c2af2761cf6ee1dee73d284
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Specialized String Functions for phpMyAdmin
6 * Copyright 2002 Robin Johnson <robbat2@users.sourceforge.net>
7 * http://www.orbis-terrarum.net/?l=people.robbat2
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).
13 * The SQL Parser code relies heavily on these functions.
15 * @version $Id$
16 * @uses extension_loaded()
17 * @uses substr()
18 * @uses function_exists()
19 * @uses mb_internal_encoding()
20 * @uses defined()
21 * @todo a .lib filename should not have code in main(), split or rename file
22 * @package phpMyAdmin
24 if (! defined('PHPMYADMIN')) {
25 exit;
28 $GLOBALS['PMA_allow_mbstr'] = @function_exists('mb_strlen');
29 $GLOBALS['PMA_allow_ctype'] = @extension_loaded('ctype');
31 if ($GLOBALS['PMA_allow_mbstr']) {
32 mb_internal_encoding($GLOBALS['charset']);
35 /**
36 * Load proper code for handling input.
38 if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
39 $GLOBALS['PMA_strpos'] = 'mb_strpos';
40 $GLOBALS['PMA_substr'] = 'mb_substr';
41 require './libraries/string_mb.lib.php';
42 } else {
43 $GLOBALS['PMA_strpos'] = 'strpos';
44 $GLOBALS['PMA_substr'] = 'substr';
45 require './libraries/string_native.lib.php';
48 /**
49 * Load ctype handler.
51 if ($GLOBALS['PMA_allow_ctype']) {
52 $GLOBALS['PMA_STR_isAlnum'] = 'ctype_alnum';
53 $GLOBALS['PMA_STR_isDigit'] = 'ctype_digit';
54 $GLOBALS['PMA_STR_isSpace'] = 'ctype_space';
55 require './libraries/string_type_ctype.lib.php';
56 } else {
57 $GLOBALS['PMA_STR_isAlnum'] = 'PMA_STR_isAlnum';
58 $GLOBALS['PMA_STR_isDigit'] = 'PMA_STR_isDigit';
59 $GLOBALS['PMA_STR_isSpace'] = 'PMA_STR_isSpace';
60 require './libraries/string_type_native.lib.php';
63 /**
64 * Checks if a given character position in the string is escaped or not
66 * @uses PMA_strlen()
67 * @uses PMA_substr()
68 * @uses max()
69 * @uses intval()
70 * @param string string to check for
71 * @param integer the character to check for
72 * @param integer starting position in the string
73 * @return boolean whether the character is escaped or not
75 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
77 $pos = max(intval($pos), 0);
78 $start = max(intval($start), 0);
79 $len = PMA_strlen($string);
80 // Base case:
81 // Check for string length or invalid input or special case of input
82 // (pos == $start)
83 if ($pos <= $start || $len <= max($pos, $start)) {
84 return false;
87 $pos--;
88 $escaped = false;
89 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
90 $escaped = !$escaped;
91 $pos--;
92 } // end while
94 return $escaped;
95 } // end of the "PMA_STR_charIsEscaped()" function
98 /**
99 * Checks if a number is in a range
101 * @param integer number to check for
102 * @param integer lower bound
103 * @param integer upper bound
104 * @return boolean whether the number is in the range or not
106 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
108 return ($num >= $lower && $num <= $upper);
109 } // end of the "PMA_STR_numberInRangeInclusive()" function
112 * Checks if a character is an SQL identifier
114 * @uses PMA_STR_isAlnum()
115 * @param string character to check for
116 * @param boolean whether the dot character is valid or not
117 * @return boolean whether the character is an SQL identifier or not
119 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
121 return ($GLOBALS['PMA_STR_isAlnum']($c)
122 || ($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249
123 || $c == '_'
124 || $c == '$'
125 || ($dot_is_valid != false && $c == '.'));
126 } // end of the "PMA_STR_isSqlIdentifier()" function
130 * Binary search of a value in a sorted array
132 * $arr MUST be sorted, due to binary search
134 * @param string string to search for
135 * @param array sorted array to search into
136 * @param integer size of sorted array to search into
138 * @return boolean whether the string has been found or not
140 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
142 $top = $arrsize - 1;
143 $bottom = 0;
144 $found = false;
146 while ($top >= $bottom && $found == false) {
147 $mid = intval(($top + $bottom) / 2);
148 $res = strcmp($str, $arr[$mid]);
149 if ($res == 0) {
150 $found = true;
151 } elseif ($res < 0) {
152 $top = $mid - 1;
153 } else {
154 $bottom = $mid + 1;
156 } // end while
158 return $found;
159 } // end of the "PMA_STR_binarySearchInArr()" function