home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / string.lib.php < prev    next >
Encoding:
PHP Script  |  2003-11-26  |  8.6 KB  |  321 lines

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