sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / read_dump.lib.php
blobe2d823a677ce98323afc0a232623970cb6bcdc1b
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
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
17 * @access public
19 function PMA_splitSqlFile(&$ret, $sql, $release)
21 // do not trim, see bug #1030644
22 //$sql = trim($sql);
23 $sql = rtrim($sql, "\n\r");
24 $sql_len = strlen($sql);
25 $char = '';
26 $string_start = '';
27 $in_string = FALSE;
28 $nothing = TRUE;
29 $time0 = time();
31 for ($i = 0; $i < $sql_len; ++$i) {
32 $char = $sql[$i];
34 // We are in a string, check for not escaped end of strings except for
35 // backquotes that can't be escaped
36 if ($in_string) {
37 for (;;) {
38 $i = strpos($sql, $string_start, $i);
39 // No end of string found -> add the current substring to the
40 // returned array
41 if (!$i) {
42 $ret[] = array('query' => $sql, 'empty' => $nothing);
43 return TRUE;
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] != '\\') {
48 $string_start = '';
49 $in_string = FALSE;
50 break;
52 // one or more Backslashes before the presumed end of string...
53 else {
54 // ... first checks for escaped backslashes
55 $j = 2;
56 $escaped_backslash = FALSE;
57 while ($i-$j > 0 && $sql[$i-$j] == '\\') {
58 $escaped_backslash = !$escaped_backslash;
59 $j++;
61 // ... if escaped backslashes: it's really the end of the
62 // string -> exit the loop
63 if ($escaped_backslash) {
64 $string_start = '';
65 $in_string = FALSE;
66 break;
68 // ... else loop
69 else {
70 $i++;
72 } // end if...elseif...else
73 } // end for
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?
80 if ($i === FALSE) {
81 break;
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);
90 $nothing = TRUE;
91 $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
92 $sql_len = strlen($sql);
93 if ($sql_len) {
94 $i = -1;
95 } else {
96 // The submited statement(s) end(s) here
97 return TRUE;
99 } // end else if (is delimiter)
101 // ... then check for start of a string,...
102 else if (($char == '"') || ($char == '\'') || ($char == '`')) {
103 $in_string = TRUE;
104 $nothing = FALSE;
105 $string_start = $char;
106 } // end else if (is start of string)
108 elseif ($nothing) {
109 $nothing = FALSE;
112 // loic1: send a fake header each 30 sec. to bypass browser timeout
113 $time1 = time();
114 if ($time1 >= $time0 + 30) {
115 $time0 = $time1;
116 header('X-pmaPing: Pong');
117 } // end if
118 } // end for
120 // add any rest to the returned array
121 if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
122 $ret[] = array('query' => $sql, 'empty' => $nothing);
125 return TRUE;
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 = '') {
141 global $cfg;
143 if (!file_exists($path)) {
144 return FALSE;
146 switch ($mime) {
147 case '':
148 $file = @fopen($path, 'rb');
149 if (!$file) {
150 return FALSE;
152 $test = fread($file, 3);
153 fclose($file);
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');
157 case 'text/plain':
158 $file = @fopen($path, 'rb');
159 if (!$file) {
160 return FALSE;
162 $content = fread($file, filesize($path));
163 fclose($file);
164 break;
165 case 'application/x-gzip':
166 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
167 $file = @gzopen($path, 'rb');
168 if (!$file) {
169 return FALSE;
171 $content = '';
172 while (!gzeof($file)) {
173 $content .= gzgetc($file);
175 gzclose($file);
176 } else {
177 return FALSE;
179 break;
180 case 'application/x-bzip':
181 if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
182 $file = @fopen($path, 'rb');
183 if (!$file) {
184 return FALSE;
186 $content = fread($file, filesize($path));
187 fclose($file);
188 $content = bzdecompress($content);
189 } else {
190 return FALSE;
192 break;
193 default:
194 return FALSE;
196 return $content;