Documentation TYPO3 par Ameos

read_dump.lib.php

00001 <?php
00002 /* $Id: read_dump.lib.php,v 2.10 2004/10/19 12:49:21 nijel Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
00004 
00019 function PMA_splitSqlFile(&$ret, $sql, $release)
00020 {
00021     // do not trim, see bug #1030644
00022     //$sql          = trim($sql);
00023     $sql          = rtrim($sql, "\n\r");
00024     $sql_len      = strlen($sql);
00025     $char         = '';
00026     $string_start = '';
00027     $in_string    = FALSE;
00028     $nothing      = TRUE;
00029     $time0        = time();
00030 
00031     for ($i = 0; $i < $sql_len; ++$i) {
00032         $char = $sql[$i];
00033 
00034         // We are in a string, check for not escaped end of strings except for
00035         // backquotes that can't be escaped
00036         if ($in_string) {
00037             for (;;) {
00038                 $i         = strpos($sql, $string_start, $i);
00039                 // No end of string found -> add the current substring to the
00040                 // returned array
00041                 if (!$i) {
00042                     $ret[] = array('query' => $sql, 'empty' => $nothing);
00043                     return TRUE;
00044                 }
00045                 // Backquotes or no backslashes before quotes: it's indeed the
00046                 // end of the string -> exit the loop
00047                 else if ($string_start == '`' || $sql[$i-1] != '\\') {
00048                     $string_start      = '';
00049                     $in_string         = FALSE;
00050                     break;
00051                 }
00052                 // one or more Backslashes before the presumed end of string...
00053                 else {
00054                     // ... first checks for escaped backslashes
00055                     $j                     = 2;
00056                     $escaped_backslash     = FALSE;
00057                     while ($i-$j > 0 && $sql[$i-$j] == '\\') {
00058                         $escaped_backslash = !$escaped_backslash;
00059                         $j++;
00060                     }
00061                     // ... if escaped backslashes: it's really the end of the
00062                     // string -> exit the loop
00063                     if ($escaped_backslash) {
00064                         $string_start  = '';
00065                         $in_string     = FALSE;
00066                         break;
00067                     }
00068                     // ... else loop
00069                     else {
00070                         $i++;
00071                     }
00072                 } // end if...elseif...else
00073             } // end for
00074         } // end if (in string)
00075 
00076         // lets skip comments (/*, -- and #)
00077         else if (($char == '-' && $sql_len > $i + 2 && $sql[$i + 1] == '-' && $sql[$i + 2] <= ' ') || $char == '#' || ($char == '/' && $sql_len > $i + 1 && $sql[$i + 1] == '*')) {
00078             $i = strpos($sql, $char == '/' ? '*/' : "\n", $i);
00079             // didn't we hit end of string?
00080             if ($i === FALSE) {
00081                 break;
00082             }
00083             if ($char == '/') $i++;
00084         }
00085 
00086         // We are not in a string, first check for delimiter...
00087         else if ($char == ';') {
00088             // if delimiter found, add the parsed part to the returned array
00089             $ret[]      = array('query' => substr($sql, 0, $i), 'empty' => $nothing);
00090             $nothing    = TRUE;
00091             $sql        = ltrim(substr($sql, min($i + 1, $sql_len)));
00092             $sql_len    = strlen($sql);
00093             if ($sql_len) {
00094                 $i      = -1;
00095             } else {
00096                 // The submited statement(s) end(s) here
00097                 return TRUE;
00098             }
00099         } // end else if (is delimiter)
00100 
00101         // ... then check for start of a string,...
00102         else if (($char == '"') || ($char == '\'') || ($char == '`')) {
00103             $in_string    = TRUE;
00104             $nothing      = FALSE;
00105             $string_start = $char;
00106         } // end else if (is start of string)
00107 
00108         elseif ($nothing) {
00109             $nothing = FALSE;
00110         }
00111 
00112         // loic1: send a fake header each 30 sec. to bypass browser timeout
00113         $time1     = time();
00114         if ($time1 >= $time0 + 30) {
00115             $time0 = $time1;
00116             header('X-pmaPing: Pong');
00117         } // end if
00118     } // end for
00119 
00120     // add any rest to the returned array
00121     if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
00122         $ret[] = array('query' => $sql, 'empty' => $nothing);
00123     }
00124 
00125     return TRUE;
00126 } // end of the 'PMA_splitSqlFile()' function
00127 
00128 
00140 function PMA_readFile($path, $mime = '') {
00141     global $cfg;
00142 
00143     if (!file_exists($path)) {
00144         return FALSE;
00145     }
00146     switch ($mime) {
00147         case '':
00148             $file = @fopen($path, 'rb');
00149             if (!$file) {
00150                 return FALSE;
00151             }
00152             $test = fread($file, 3);
00153             fclose($file);
00154             if ($test[0] == chr(31) && $test[1] == chr(139)) return PMA_readFile($path, 'application/x-gzip');
00155             if ($test == 'BZh') return PMA_readFile($path, 'application/x-bzip');
00156             return PMA_readFile($path, 'text/plain');
00157         case 'text/plain':
00158             $file = @fopen($path, 'rb');
00159             if (!$file) {
00160                 return FALSE;
00161             }
00162             $content = fread($file, filesize($path));
00163             fclose($file);
00164             break;
00165         case 'application/x-gzip':
00166             if ($cfg['GZipDump'] && @function_exists('gzopen')) {
00167                 $file = @gzopen($path, 'rb');
00168                 if (!$file) {
00169                     return FALSE;
00170                 }
00171                 $content = '';
00172                 while (!gzeof($file)) {
00173                     $content .= gzgetc($file);
00174                 }
00175                 gzclose($file);
00176             } else {
00177                 return FALSE;
00178             }
00179            break;
00180         case 'application/x-bzip':
00181             if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
00182                 $file = @fopen($path, 'rb');
00183                 if (!$file) {
00184                     return FALSE;
00185                 }
00186                 $content = fread($file, filesize($path));
00187                 fclose($file);
00188                 $content = bzdecompress($content);
00189             } else {
00190                 return FALSE;
00191             }
00192            break;
00193         default:
00194            return FALSE;
00195     }
00196     return $content;
00197 }
00198 
00199 ?>


Généré par Les spécialistes TYPO3 avec  doxygen 1.4.6