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