Documentation TYPO3 par Ameos

read_dump.lib.php

00001 <?php
00002 /* $Id: read_dump.lib.php,v 2.2 2003/11/26 22:52:23 rabus Exp $ */
00003 // vim: expandtab sw=4 ts=4 sts=4:
00004 
00019 function PMA_splitSqlFile(&$ret, $sql, $release)
00020 {
00021     $sql          = trim($sql);
00022     $sql_len      = strlen($sql);
00023     $char         = '';
00024     $string_start = '';
00025     $in_string    = FALSE;
00026     $time0        = time();
00027 
00028     for ($i = 0; $i < $sql_len; ++$i) {
00029         $char = $sql[$i];
00030 
00031         // We are in a string, check for not escaped end of strings except for
00032         // backquotes that can't be escaped
00033         if ($in_string) {
00034             for (;;) {
00035                 $i         = strpos($sql, $string_start, $i);
00036                 // No end of string found -> add the current substring to the
00037                 // returned array
00038                 if (!$i) {
00039                     $ret[] = $sql;
00040                     return TRUE;
00041                 }
00042                 // Backquotes or no backslashes before quotes: it's indeed the
00043                 // end of the string -> exit the loop
00044                 else if ($string_start == '`' || $sql[$i-1] != '\\') {
00045                     $string_start      = '';
00046                     $in_string         = FALSE;
00047                     break;
00048                 }
00049                 // one or more Backslashes before the presumed end of string...
00050                 else {
00051                     // ... first checks for escaped backslashes
00052                     $j                     = 2;
00053                     $escaped_backslash     = FALSE;
00054                     while ($i-$j > 0 && $sql[$i-$j] == '\\') {
00055                         $escaped_backslash = !$escaped_backslash;
00056                         $j++;
00057                     }
00058                     // ... if escaped backslashes: it's really the end of the
00059                     // string -> exit the loop
00060                     if ($escaped_backslash) {
00061                         $string_start  = '';
00062                         $in_string     = FALSE;
00063                         break;
00064                     }
00065                     // ... else loop
00066                     else {
00067                         $i++;
00068                     }
00069                 } // end if...elseif...else
00070             } // end for
00071         } // end if (in string)
00072 
00073         // We are not in a string, first check for delimiter...
00074         else if ($char == ';') {
00075             // if delimiter found, add the parsed part to the returned array
00076             $ret[]      = substr($sql, 0, $i);
00077             $sql        = ltrim(substr($sql, min($i + 1, $sql_len)));
00078             $sql_len    = strlen($sql);
00079             if ($sql_len) {
00080                 $i      = -1;
00081             } else {
00082                 // The submited statement(s) end(s) here
00083                 return TRUE;
00084             }
00085         } // end else if (is delimiter)
00086 
00087         // ... then check for start of a string,...
00088         else if (($char == '"') || ($char == '\'') || ($char == '`')) {
00089             $in_string    = TRUE;
00090             $string_start = $char;
00091         } // end else if (is start of string)
00092 
00093         // ... for start of a comment (and remove this comment if found)...
00094         else if ($char == '#'
00095                  || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
00096             // starting position of the comment depends on the comment type
00097             $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
00098             // if no "\n" exits in the remaining string, checks for "\r"
00099             // (Mac eol style)
00100             $end_of_comment   = (strpos(' ' . $sql, "\012", $i+2))
00101                               ? strpos(' ' . $sql, "\012", $i+2)
00102                               : strpos(' ' . $sql, "\015", $i+2);
00103             if (!$end_of_comment) {
00104                 // no eol found after '#', add the parsed part to the returned
00105                 // array if required and exit
00106                 if ($start_of_comment > 0) {
00107                     $ret[]    = trim(substr($sql, 0, $start_of_comment));
00108                 }
00109                 return TRUE;
00110             } else {
00111                 $sql          = substr($sql, 0, $start_of_comment)
00112                               . ltrim(substr($sql, $end_of_comment));
00113                 $sql_len      = strlen($sql);
00114                 $i--;
00115             } // end if...else
00116         } // end else if (is comment)
00117 
00118         // ... and finally disactivate the "/*!...*/" syntax if MySQL < 3.22.07
00119         else if ($release < 32270
00120                  && ($char == '!' && $i > 1  && $sql[$i-2] . $sql[$i-1] == '/*')) {
00121             $sql[$i] = ' ';
00122         } // end else if
00123 
00124         // loic1: send a fake header each 30 sec. to bypass browser timeout
00125         $time1     = time();
00126         if ($time1 >= $time0 + 30) {
00127             $time0 = $time1;
00128             header('X-pmaPing: Pong');
00129         } // end if
00130     } // end for
00131 
00132     // add any rest to the returned array
00133     if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
00134         $ret[] = $sql;
00135     }
00136 
00137     return TRUE;
00138 } // end of the 'PMA_splitSqlFile()' function
00139 
00140 
00152 function PMA_readFile($path, $mime = '') {
00153     global $cfg;
00154 
00155     if (!file_exists($path)) {
00156         return FALSE;
00157     }
00158     switch ($mime) {
00159         case '':
00160             $file = @fopen($path, 'rb');
00161             if (!$file) {
00162                 return FALSE;
00163             }
00164             $test = fread($file, 3);
00165             fclose($file);
00166             if ($test[0] == chr(31) && $test[1] == chr(139)) return PMA_readFile($path, 'application/x-gzip');
00167             if ($test == 'BZh') return PMA_readFile($path, 'application/x-bzip');
00168             return PMA_readFile($path, 'text/plain');
00169         case 'text/plain':
00170             $file = @fopen($path, 'rb');
00171             if (!$file) {
00172                 return FALSE;
00173             }
00174             $content = fread($file, filesize($path));
00175             fclose($file);
00176             break;
00177         case 'application/x-gzip':
00178             if ($cfg['GZipDump'] && @function_exists('gzopen')) {
00179                 $file = @gzopen($path, 'rb');
00180                 if (!$file) {
00181                     return FALSE;
00182                 }
00183                 $content = '';
00184                 while (!gzeof($file)) {
00185                     $content .= gzgetc($file);
00186                 }
00187                 gzclose($file);
00188             } else {
00189                 return FALSE;
00190             }
00191            break;
00192         case 'application/x-bzip':
00193             if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
00194                 $file = @fopen($path, 'rb');
00195                 if (!$file) {
00196                     return FALSE;
00197                 }
00198                 $content = fread($file, filesize($path));
00199                 fclose($file);
00200                 $content = bzdecompress($content);
00201             } else {
00202                 return FALSE;
00203             }
00204            break;
00205         default:
00206            return FALSE;
00207     }
00208     return $content;
00209 }
00210 
00211 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6