BLOB streaming documentation
[phpmyadmin/crack.git] / libraries / string.lib.php
blob6cb2d60e15d91694a49d43e12ad5390945eba507
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
23 if (! defined('PHPMYADMIN')) {
24 exit;
27 $GLOBALS['PMA_allow_mbstr'] = @function_exists('mb_strlen');
29 if ($GLOBALS['PMA_allow_mbstr']) {
30 mb_internal_encoding($GLOBALS['charset']);
33 // This is for handling input better
34 if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
35 $GLOBALS['PMA_strpos'] = 'mb_strpos';
36 $GLOBALS['PMA_substr'] = 'mb_substr';
37 $GLOBALS['PMA_STR_isAlnum'] = 'ctype_alnum';
38 $GLOBALS['PMA_STR_isDigit'] = 'ctype_digit';
39 $GLOBALS['PMA_STR_isSpace'] = 'ctype_space';
40 require './libraries/string_mb.lib.php';
41 } else {
42 $GLOBALS['PMA_strpos'] = 'strpos';
43 $GLOBALS['PMA_substr'] = 'substr';
44 $GLOBALS['PMA_STR_isAlnum'] = 'PMA_STR_isAlnum';
45 $GLOBALS['PMA_STR_isDigit'] = 'PMA_STR_isDigit';
46 $GLOBALS['PMA_STR_isSpace'] = 'PMA_STR_isSpace';
47 require './libraries/string_native.lib.php';
50 if (@extension_loaded('ctype')) {
51 require './libraries/string_type_ctype.lib.php';
52 } else {
53 require './libraries/string_type_native.lib.php';
56 /**
57 * Checks if a given character position in the string is escaped or not
59 * @uses PMA_strlen()
60 * @uses PMA_substr()
61 * @uses max()
62 * @uses intval()
63 * @param string string to check for
64 * @param integer the character to check for
65 * @param integer starting position in the string
66 * @return boolean whether the character is escaped or not
68 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
70 $pos = max(intval($pos), 0);
71 $start = max(intval($start), 0);
72 $len = PMA_strlen($string);
73 // Base case:
74 // Check for string length or invalid input or special case of input
75 // (pos == $start)
76 if ($pos <= $start || $len <= max($pos, $start)) {
77 return false;
80 $pos--;
81 $escaped = false;
82 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
83 $escaped = !$escaped;
84 $pos--;
85 } // end while
87 return $escaped;
88 } // end of the "PMA_STR_charIsEscaped()" function
91 /**
92 * Checks if a number is in a range
94 * @param integer number to check for
95 * @param integer lower bound
96 * @param integer upper bound
97 * @return boolean whether the number is in the range or not
99 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
101 return ($num >= $lower && $num <= $upper);
102 } // end of the "PMA_STR_numberInRangeInclusive()" function
105 * Checks if a character is an SQL identifier
107 * @uses PMA_STR_isAlnum()
108 * @param string character to check for
109 * @param boolean whether the dot character is valid or not
110 * @return boolean whether the character is an SQL identifier or not
112 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
114 return ($GLOBALS['PMA_STR_isAlnum']($c)
115 || ($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249
116 || $c == '_'
117 || $c == '$'
118 || ($dot_is_valid != false && $c == '.'));
119 } // end of the "PMA_STR_isSqlIdentifier()" function
123 * Binary search of a value in a sorted array
125 * $arr MUST be sorted, due to binary search
127 * @param string string to search for
128 * @param array sorted array to search into
129 * @param integer size of sorted array to search into
131 * @return boolean whether the string has been found or not
133 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
135 $top = $arrsize - 1;
136 $bottom = 0;
137 $found = false;
139 while ($top >= $bottom && $found == false) {
140 $mid = intval(($top + $bottom) / 2);
141 $res = strcmp($str, $arr[$mid]);
142 if ($res == 0) {
143 $found = true;
144 } elseif ($res < 0) {
145 $top = $mid - 1;
146 } else {
147 $bottom = $mid + 1;
149 } // end while
151 return $found;
152 } // end of the "PMA_STR_binarySearchInArr()" function