Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: read_dump.lib.php,v 2.11 2006/01/17 17:02:30 cybot_tm 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 // 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();
30  
31 for ($i = 0; $i < $sql_len; ++$i) {
32 $char = $sql[$i];
33  
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;
44 }
45 // Backquotes or no backslashes before quotes: it's indeed the
46 // end of the string -> exit the loop
47 elseif ($string_start == '`' || $sql[$i-1] != '\\') {
48 $string_start = '';
49 $in_string = FALSE;
50 break;
51 }
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++;
60 }
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;
67 }
68 // ... else loop
69 else {
70 $i++;
71 }
72 } // end if...elseif...else
73 } // end for
74 } // end if (in string)
75  
76 // lets skip comments (/*, -- and #)
77 elseif (($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;
82 }
83 if ($char == '/') {
84 $i++;
85 }
86 }
87  
88 // We are not in a string, first check for delimiter...
89 elseif ($char == ';') {
90 // if delimiter found, add the parsed part to the returned array
91 $ret[] = array('query' => substr($sql, 0, $i), 'empty' => $nothing);
92 $nothing = TRUE;
93 $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
94 $sql_len = strlen($sql);
95 if ($sql_len) {
96 $i = -1;
97 } else {
98 // The submited statement(s) end(s) here
99 return TRUE;
100 }
101 } // end elseif (is delimiter)
102  
103 // ... then check for start of a string,...
104 elseif (($char == '"') || ($char == '\'') || ($char == '`')) {
105 $in_string = TRUE;
106 $nothing = FALSE;
107 $string_start = $char;
108 } // end elseif (is start of string)
109  
110 elseif ($nothing) {
111 $nothing = FALSE;
112 }
113  
114 // loic1: send a fake header each 30 sec. to bypass browser timeout
115 $time1 = time();
116 if ($time1 >= $time0 + 30) {
117 $time0 = $time1;
118 header('X-pmaPing: Pong');
119 } // end if
120 } // end for
121  
122 // add any rest to the returned array
123 if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
124 $ret[] = array('query' => $sql, 'empty' => $nothing);
125 }
126  
127 return TRUE;
128 } // end of the 'PMA_splitSqlFile()' function
129  
130  
131 /**
132 * Reads (and decompresses) a (compressed) file into a string
133 *
134 * @param string the path to the file
135 * @param string the MIME type of the file, if empty MIME type is autodetected
136 *
137 * @global array the phpMyAdmin configuration
138 *
139 * @return string the content of the file or
140 * boolean FALSE in case of an error.
141 */
142 function PMA_readFile($path, $mime = '') {
143 global $cfg;
144  
145 if (!file_exists($path)) {
146 return FALSE;
147 }
148 switch ($mime) {
149 case '':
150 $file = @fopen($path, 'rb');
151 if (!$file) {
152 return FALSE;
153 }
154 $test = fread($file, 3);
155 fclose($file);
156 if ($test[0] == chr(31) && $test[1] == chr(139)) {
157 return PMA_readFile($path, 'application/x-gzip');
158 }
159 if ($test == 'BZh') {
160 return PMA_readFile($path, 'application/x-bzip');
161 }
162 return PMA_readFile($path, 'text/plain');
163 case 'text/plain':
164 $file = @fopen($path, 'rb');
165 if (!$file) {
166 return FALSE;
167 }
168 $content = fread($file, filesize($path));
169 fclose($file);
170 break;
171 case 'application/x-gzip':
172 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
173 $file = @gzopen($path, 'rb');
174 if (!$file) {
175 return FALSE;
176 }
177 $content = '';
178 while (!gzeof($file)) {
179 $content .= gzgetc($file);
180 }
181 gzclose($file);
182 } else {
183 return FALSE;
184 }
185 break;
186 case 'application/x-bzip':
187 if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
188 $file = @fopen($path, 'rb');
189 if (!$file) {
190 return FALSE;
191 }
192 $content = fread($file, filesize($path));
193 fclose($file);
194 $content = bzdecompress($content);
195 } else {
196 return FALSE;
197 }
198 break;
199 default:
200 return FALSE;
201 }
202 return $content;
203 }
204  
205 ?>