home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / string.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  6.5 KB  |  224 lines

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