3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Removes comment lines and splits up large sql files into individual queries
8 * Last revision: September 23, 2001 - gandon
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)
15 * @return boolean always true
19 function PMA_splitSqlFile(&$ret, $sql, $release)
21 // do not trim, see bug #1030644
23 $sql = rtrim($sql, "\n\r");
24 $sql_len = strlen($sql);
31 for ($i = 0; $i < $sql_len; ++
$i) {
34 // We are in a string, check for not escaped end of strings except for
35 // backquotes that can't be escaped
38 $i = strpos($sql, $string_start, $i);
39 // No end of string found -> add the current substring to the
42 $ret[] = array('query' => $sql, 'empty' => $nothing);
45 // Backquotes or no backslashes before quotes: it's indeed the
46 // end of the string -> exit the loop
47 else if ($string_start == '`' ||
$sql[$i-1] != '\\') {
52 // one or more Backslashes before the presumed end of string...
54 // ... first checks for escaped backslashes
56 $escaped_backslash = FALSE;
57 while ($i-$j > 0 && $sql[$i-$j] == '\\') {
58 $escaped_backslash = !$escaped_backslash;
61 // ... if escaped backslashes: it's really the end of the
62 // string -> exit the loop
63 if ($escaped_backslash) {
72 } // end if...elseif...else
74 } // end if (in string)
76 // lets skip comments (/*, -- and #)
77 else if (($char == '-' && $sql_len > $i +
2 && $sql[$i +
1] == '-' && $sql[$i +
2] <= ' ') ||
$char == '#' ||
($char == '/' && $sql_len > $i +
1 && $sql[$i +
1] == '*')) {
78 $i = strpos($sql, $char == '/' ?
'*/' : "\n", $i);
79 // didn't we hit end of string?
83 if ($char == '/') $i++
;
86 // We are not in a string, first check for delimiter...
87 else if ($char == ';') {
88 // if delimiter found, add the parsed part to the returned array
89 $ret[] = array('query' => substr($sql, 0, $i), 'empty' => $nothing);
91 $sql = ltrim(substr($sql, min($i +
1, $sql_len)));
92 $sql_len = strlen($sql);
96 // The submited statement(s) end(s) here
99 } // end else if (is delimiter)
101 // ... then check for start of a string,...
102 else if (($char == '"') ||
($char == '\'') ||
($char == '`')) {
105 $string_start = $char;
106 } // end else if (is start of string)
112 // loic1: send a fake header each 30 sec. to bypass browser timeout
114 if ($time1 >= $time0 +
30) {
116 header('X-pmaPing: Pong');
120 // add any rest to the returned array
121 if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
122 $ret[] = array('query' => $sql, 'empty' => $nothing);
126 } // end of the 'PMA_splitSqlFile()' function
130 * Reads (and decompresses) a (compressed) file into a string
132 * @param string the path to the file
133 * @param string the MIME type of the file, if empty MIME type is autodetected
135 * @global array the phpMyAdmin configuration
137 * @return string the content of the file or
138 * boolean FALSE in case of an error.
140 function PMA_readFile($path, $mime = '') {
143 if (!file_exists($path)) {
148 $file = @fopen
($path, 'rb');
152 $test = fread($file, 3);
154 if ($test[0] == chr(31) && $test[1] == chr(139)) return PMA_readFile($path, 'application/x-gzip');
155 if ($test == 'BZh') return PMA_readFile($path, 'application/x-bzip');
156 return PMA_readFile($path, 'text/plain');
158 $file = @fopen
($path, 'rb');
162 $content = fread($file, filesize($path));
165 case 'application/x-gzip':
166 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
167 $file = @gzopen
($path, 'rb');
172 while (!gzeof($file)) {
173 $content .= gzgetc($file);
180 case 'application/x-bzip':
181 if ($cfg['BZipDump'] && @function_exists
('bzdecompress')) {
182 $file = @fopen
($path, 'rb');
186 $content = fread($file, filesize($path));
188 $content = bzdecompress($content);