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

  1. <?php
  2. /* $Id: read_dump.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.  * Removes comment lines and splits up large sql files into individual queries
  7.  *
  8.  * Last revision: September 23, 2001 - gandon
  9.  *
  10.  * @param   array    the splitted sql commands
  11.  * @param   string   the sql commands
  12.  * @param   integer  the MySQL release number (because certains php3 versions
  13.  *                   can't get the value of a constant from within a function)
  14.  *
  15.  * @return  boolean  always true
  16.  *
  17.  * @access  public
  18.  */
  19. function PMA_splitSqlFile(&$ret, $sql, $release)
  20. {
  21.     $sql          = trim($sql);
  22.     $sql_len      = strlen($sql);
  23.     $char         = '';
  24.     $string_start = '';
  25.     $in_string    = FALSE;
  26.     $time0        = time();
  27.  
  28.     for ($i = 0; $i < $sql_len; ++$i) {
  29.         $char = $sql[$i];
  30.  
  31.         // We are in a string, check for not escaped end of strings except for
  32.         // backquotes that can't be escaped
  33.         if ($in_string) {
  34.             for (;;) {
  35.                 $i         = strpos($sql, $string_start, $i);
  36.                 // No end of string found -> add the current substring to the
  37.                 // returned array
  38.                 if (!$i) {
  39.                     $ret[] = $sql;
  40.                     return TRUE;
  41.                 }
  42.                 // Backquotes or no backslashes before quotes: it's indeed the
  43.                 // end of the string -> exit the loop
  44.                 else if ($string_start == '`' || $sql[$i-1] != '\\') {
  45.                     $string_start      = '';
  46.                     $in_string         = FALSE;
  47.                     break;
  48.                 }
  49.                 // one or more Backslashes before the presumed end of string...
  50.                 else {
  51.                     // ... first checks for escaped backslashes
  52.                     $j                     = 2;
  53.                     $escaped_backslash     = FALSE;
  54.                     while ($i-$j > 0 && $sql[$i-$j] == '\\') {
  55.                         $escaped_backslash = !$escaped_backslash;
  56.                         $j++;
  57.                     }
  58.                     // ... if escaped backslashes: it's really the end of the
  59.                     // string -> exit the loop
  60.                     if ($escaped_backslash) {
  61.                         $string_start  = '';
  62.                         $in_string     = FALSE;
  63.                         break;
  64.                     }
  65.                     // ... else loop
  66.                     else {
  67.                         $i++;
  68.                     }
  69.                 } // end if...elseif...else
  70.             } // end for
  71.         } // end if (in string)
  72.  
  73.         // We are not in a string, first check for delimiter...
  74.         else if ($char == ';') {
  75.             // if delimiter found, add the parsed part to the returned array
  76.             $ret[]      = substr($sql, 0, $i);
  77.             $sql        = ltrim(substr($sql, min($i + 1, $sql_len)));
  78.             $sql_len    = strlen($sql);
  79.             if ($sql_len) {
  80.                 $i      = -1;
  81.             } else {
  82.                 // The submited statement(s) end(s) here
  83.                 return TRUE;
  84.             }
  85.         } // end else if (is delimiter)
  86.  
  87.         // ... then check for start of a string,...
  88.         else if (($char == '"') || ($char == '\'') || ($char == '`')) {
  89.             $in_string    = TRUE;
  90.             $string_start = $char;
  91.         } // end else if (is start of string)
  92.  
  93.         // ... for start of a comment (and remove this comment if found)...
  94.         else if ($char == '#'
  95.                  || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
  96.             // starting position of the comment depends on the comment type
  97.             $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
  98.             // if no "\n" exits in the remaining string, checks for "\r"
  99.             // (Mac eol style)
  100.             $end_of_comment   = (strpos(' ' . $sql, "\012", $i+2))
  101.                               ? strpos(' ' . $sql, "\012", $i+2)
  102.                               : strpos(' ' . $sql, "\015", $i+2);
  103.             if (!$end_of_comment) {
  104.                 // no eol found after '#', add the parsed part to the returned
  105.                 // array if required and exit
  106.                 if ($start_of_comment > 0) {
  107.                     $ret[]    = trim(substr($sql, 0, $start_of_comment));
  108.                 }
  109.                 return TRUE;
  110.             } else {
  111.                 $sql          = substr($sql, 0, $start_of_comment)
  112.                               . ltrim(substr($sql, $end_of_comment));
  113.                 $sql_len      = strlen($sql);
  114.                 $i--;
  115.             } // end if...else
  116.         } // end else if (is comment)
  117.  
  118.         // ... and finally disactivate the "/*!...*/" syntax if MySQL < 3.22.07
  119.         else if ($release < 32270
  120.                  && ($char == '!' && $i > 1  && $sql[$i-2] . $sql[$i-1] == '/*')) {
  121.             $sql[$i] = ' ';
  122.         } // end else if
  123.  
  124.         // loic1: send a fake header each 30 sec. to bypass browser timeout
  125.         $time1     = time();
  126.         if ($time1 >= $time0 + 30) {
  127.             $time0 = $time1;
  128.             header('X-pmaPing: Pong');
  129.         } // end if
  130.     } // end for
  131.  
  132.     // add any rest to the returned array
  133.     if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
  134.         $ret[] = $sql;
  135.     }
  136.  
  137.     return TRUE;
  138. } // end of the 'PMA_splitSqlFile()' function
  139.  
  140.  
  141. /**
  142.  * Reads (and decompresses) a (compressed) file into a string
  143.  *
  144.  * @param   string   the path to the file
  145.  * @param   string   the MIME type of the file, if empty MIME type is autodetected
  146.  *
  147.  * @global  array    the phpMyAdmin configuration
  148.  *
  149.  * @return  string   the content of the file or
  150.  *          boolean  FALSE in case of an error.
  151.  */
  152. function PMA_readFile($path, $mime = '') {
  153.     global $cfg;
  154.  
  155.     if (!file_exists($path)) {
  156.         return FALSE;
  157.     }
  158.     switch ($mime) {
  159.         case '':
  160.             $file = @fopen($path, 'rb');
  161.             if (!$file) {
  162.                 return FALSE;
  163.             }
  164.             $test = fread($file, 3);
  165.             fclose($file);
  166.             if ($test[0] == chr(31) && $test[1] == chr(139)) return PMA_readFile($path, 'application/x-gzip');
  167.             if ($test == 'BZh') return PMA_readFile($path, 'application/x-bzip');
  168.             return PMA_readFile($path, 'text/plain');
  169.         case 'text/plain':
  170.             $file = @fopen($path, 'rb');
  171.             if (!$file) {
  172.                 return FALSE;
  173.             }
  174.             $content = fread($file, filesize($path));
  175.             fclose($file);
  176.             break;
  177.         case 'application/x-gzip':
  178.             if ($cfg['GZipDump'] && @function_exists('gzopen')) {
  179.                 $file = @gzopen($path, 'rb');
  180.                 if (!$file) {
  181.                     return FALSE;
  182.                 }
  183.                 $content = '';
  184.                 while (!gzeof($file)) {
  185.                     $content .= gzgetc($file);
  186.                 }
  187.                 gzclose($file);
  188.             } else {
  189.                 return FALSE;
  190.             }
  191.            break;
  192.         case 'application/x-bzip':
  193.             if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
  194.                 $file = @fopen($path, 'rb');
  195.                 if (!$file) {
  196.                     return FALSE;
  197.                 }
  198.                 $content = fread($file, filesize($path));
  199.                 fclose($file);
  200.                 $content = bzdecompress($content);
  201.             } else {
  202.                 return FALSE;
  203.             }
  204.            break;
  205.         default:
  206.            return FALSE;
  207.     }
  208.     return $content;
  209. }
  210.  
  211. ?>
  212.