Drop multiple databases.
[phpmyadmin/crack.git] / read_dump.php3
blob3e8976f9acbad2ecbaba0aa977cf9d66c777c750
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Removes comment lines and splits up large sql files into individual queries
9 * Last revision: September 23, 2001 - gandon
11 * @param array the splitted sql commands
12 * @param string the sql commands
13 * @param integer the MySQL release number (because certains php3 versions
14 * can't get the value of a constant from within a function)
16 * @return boolean always true
18 * @access public
20 function PMA_splitSqlFile(&$ret, $sql, $release)
22 $sql = trim($sql);
23 $sql_len = strlen($sql);
24 $char = '';
25 $string_start = '';
26 $in_string = FALSE;
27 $time0 = time();
29 for ($i = 0; $i < $sql_len; ++$i) {
30 $char = $sql[$i];
32 // We are in a string, check for not escaped end of strings except for
33 // backquotes that can't be escaped
34 if ($in_string) {
35 for (;;) {
36 $i = strpos($sql, $string_start, $i);
37 // No end of string found -> add the current substring to the
38 // returned array
39 if (!$i) {
40 $ret[] = $sql;
41 return TRUE;
43 // Backquotes or no backslashes before quotes: it's indeed the
44 // end of the string -> exit the loop
45 else if ($string_start == '`' || $sql[$i-1] != '\\') {
46 $string_start = '';
47 $in_string = FALSE;
48 break;
50 // one or more Backslashes before the presumed end of string...
51 else {
52 // ... first checks for escaped backslashes
53 $j = 2;
54 $escaped_backslash = FALSE;
55 while ($i-$j > 0 && $sql[$i-$j] == '\\') {
56 $escaped_backslash = !$escaped_backslash;
57 $j++;
59 // ... if escaped backslashes: it's really the end of the
60 // string -> exit the loop
61 if ($escaped_backslash) {
62 $string_start = '';
63 $in_string = FALSE;
64 break;
66 // ... else loop
67 else {
68 $i++;
70 } // end if...elseif...else
71 } // end for
72 } // end if (in string)
74 // We are not in a string, first check for delimiter...
75 else if ($char == ';') {
76 // if delimiter found, add the parsed part to the returned array
77 $ret[] = substr($sql, 0, $i);
78 $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
79 $sql_len = strlen($sql);
80 if ($sql_len) {
81 $i = -1;
82 } else {
83 // The submited statement(s) end(s) here
84 return TRUE;
86 } // end else if (is delimiter)
88 // ... then check for start of a string,...
89 else if (($char == '"') || ($char == '\'') || ($char == '`')) {
90 $in_string = TRUE;
91 $string_start = $char;
92 } // end else if (is start of string)
94 // ... for start of a comment (and remove this comment if found)...
95 else if ($char == '#'
96 || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
97 // starting position of the comment depends on the comment type
98 $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
99 // if no "\n" exits in the remaining string, checks for "\r"
100 // (Mac eol style)
101 $end_of_comment = (strpos(' ' . $sql, "\012", $i+2))
102 ? strpos(' ' . $sql, "\012", $i+2)
103 : strpos(' ' . $sql, "\015", $i+2);
104 if (!$end_of_comment) {
105 // no eol found after '#', add the parsed part to the returned
106 // array if required and exit
107 if ($start_of_comment > 0) {
108 $ret[] = trim(substr($sql, 0, $start_of_comment));
110 return TRUE;
111 } else {
112 $sql = substr($sql, 0, $start_of_comment)
113 . ltrim(substr($sql, $end_of_comment));
114 $sql_len = strlen($sql);
115 $i--;
116 } // end if...else
117 } // end else if (is comment)
119 // ... and finally disactivate the "/*!...*/" syntax if MySQL < 3.22.07
120 else if ($release < 32270
121 && ($char == '!' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '/*')) {
122 $sql[$i] = ' ';
123 } // end else if
125 // loic1: send a fake header each 30 sec. to bypass browser timeout
126 $time1 = time();
127 if ($time1 >= $time0 + 30) {
128 $time0 = $time1;
129 header('X-pmaPing: Pong');
130 } // end if
131 } // end for
133 // add any rest to the returned array
134 if (!empty($sql) && ereg('[^[:space:]]+', $sql)) {
135 $ret[] = $sql;
138 return TRUE;
139 } // end of the 'PMA_splitSqlFile()' function
142 if (!function_exists('is_uploaded_file')) {
144 * Emulates the 'is_uploaded_file()' function for old php versions.
145 * Grabbed at the php manual:
146 * http://www.php.net/manual/en/features.file-upload.php
148 * @param string the name of the file to check
150 * @return boolean wether the file has been uploaded or not
152 * @access public
154 function is_uploaded_file($filename) {
155 if (!$tmp_file = @get_cfg_var('upload_tmp_dir')) {
156 $tmp_file = tempnam('','');
157 $deleted = @unlink($tmp_file);
158 $tmp_file = dirname($tmp_file);
160 $tmp_file .= '/' . basename($filename);
162 // User might have trailing slash in php.ini...
163 return (ereg_replace('/+', '/', $tmp_file) == $filename);
164 } // end of the 'is_uploaded_file()' emulated function
165 } // end if
168 * Reads (and decompresses) a (compressed) file into a string
170 * @param string the path to the file
171 * @param string the MIME type of the file
173 * @global array the phpMyAdmin configuration
175 * @return string the content of the file or
176 * boolean FALSE in case of an error.
178 function PMA_readFile($path, $mime = 'text/plain') {
179 global $cfg;
181 switch ($mime) {
182 case 'text/plain':
183 $file = fopen($path, 'rb');
184 $content = fread($file, filesize($path));
185 fclose($file);
186 break;
187 case 'application/x-gzip':
188 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
189 $file = gzopen($path, 'rb');
190 $content = '';
191 while (!gzeof($file)) {
192 $content .= gzgetc($file);
194 gzclose($file);
195 } else {
196 return FALSE;
198 break;
199 case 'application/x-bzip':
200 if ($cfg['BZipDump'] && @function_exists('bzdecompress')) {
201 $file = fopen($path, 'rb');
202 $content = fread($file, filesize($path));
203 fclose($file);
204 $content = bzdecompress($content);
205 } else {
206 return FALSE;
208 break;
209 default:
210 return FALSE;
212 if (!file_exists($path)) {
213 return FALSE;
215 return $content;
220 * Gets some core libraries
222 require('./libraries/grab_globals.lib.php3');
223 require('./libraries/common.lib.php3');
227 * Increases the max. allowed time to run a script
229 @set_time_limit($cfg['ExecTimeLimit']);
233 * Defines the url to return to in case of error in a sql statement
235 if (!isset($goto)
236 || ($goto != 'db_details.php3' && $goto != 'tbl_properties.php3')) {
237 $goto = 'db_details.php3';
239 $err_url = $goto
240 . '?' . PMA_generate_common_url($db)
241 . (($goto == 'tbl_properties.php3') ? '&amp;table=' . urlencode($table) : '');
245 * Set up default values for some variables
247 $view_bookmark = 0;
248 $sql_bookmark = isset($sql_bookmark) ? $sql_bookmark : '';
249 $sql_query = isset($sql_query) ? $sql_query : '';
250 if (!empty($sql_localfile) && $cfg['UploadDir'] != '') {
251 $sql_file = $cfg['UploadDir'] . $sql_localfile;
252 } else if (empty($sql_file)) {
253 $sql_file = 'none';
258 * Bookmark Support: get a query back from bookmark if required
260 if (!empty($id_bookmark)) {
261 include('./libraries/bookmark.lib.php3');
262 switch ($action_bookmark) {
263 case 0: // bookmarked query that have to be run
264 $sql_query = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
265 break;
266 case 1: // bookmarked query that have to be displayed
267 $sql_query = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
268 $view_bookmark = 1;
269 break;
270 case 2: // bookmarked query that have to be deleted
271 $sql_query = PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
272 break;
274 } // end if
278 * Prepares the sql query
280 // Gets the query from a file if required
281 if ($sql_file != 'none') {
282 // loic1 : fixed a security issue
283 // if ((file_exists($sql_file) && is_uploaded_file($sql_file))
284 // || file_exists($cfg['UploadDir'] . $sql_localfile)) {
285 if (file_exists($sql_file)
286 && ((isset($sql_localfile) && $sql_file == $cfg['UploadDir'] . $sql_localfile) || is_uploaded_file($sql_file))) {
287 $open_basedir = '';
288 if (PMA_PHP_INT_VERSION >= 40000) {
289 $open_basedir = @ini_get('open_basedir');
291 if (empty($open_basedir)) {
292 $open_basedir = @get_cfg_var('open_basedir');
295 // If we are on a server with open_basedir, we must move the file
296 // before opening it. The doc explains how to create the "./tmp"
297 // directory
299 if (!empty($open_basedir)) {
301 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
303 // function is_writeable() is valid on PHP3 and 4
304 if (!is_writeable($tmp_subdir)) {
305 // if we cannot move the file, let PHP report the error
306 error_reporting(E_ALL);
307 $sql_query = PMA_readFile($sql_file, $sql_file_compression);
309 else {
310 $sql_file_new = $tmp_subdir . basename($sql_file);
311 if (PMA_PHP_INT_VERSION < 40003) {
312 copy($sql_file, $sql_file_new);
313 } else {
314 move_uploaded_file($sql_file, $sql_file_new);
316 $sql_query = PMA_readFile($sql_file_new, $sql_file_compression);
317 unlink($sql_file_new);
320 else {
321 // read from the normal upload dir
322 $sql_query = PMA_readFile($sql_file, $sql_file_compression);
325 if (get_magic_quotes_runtime() == 1) {
326 $sql_query = stripslashes($sql_query);
328 // Convert the file's charset if necessary
329 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
330 && isset($charset_of_file) && $charset_of_file != $charset) {
331 $sql_query = PMA_convert_string($charset_of_file, $charset, $sql_query);
333 } // end uploaded file stuff
335 else if (empty($id_bookmark) && get_magic_quotes_gpc() == 1) {
336 $sql_query = stripslashes($sql_query);
339 // Kanji convert SQL textfile 2002/1/4 by Y.Kawada
340 if (@function_exists('PMA_kanji_str_conv')) {
341 $sql_tmp = trim($sql_query);
342 PMA_change_enc_order();
343 $sql_query = PMA_kanji_str_conv($sql_tmp, $knjenc, isset($xkana) ? $xkana : '');
344 PMA_change_enc_order();
345 } else {
346 $sql_query = trim($sql_query);
349 // $sql_query come from the query textarea, if it's a reposted query gets its
350 // 'true' value
351 if (!empty($prev_sql_query)) {
352 $prev_sql_query = urldecode($prev_sql_query);
353 if ($sql_query == trim(htmlspecialchars($prev_sql_query))) {
354 $sql_query = $prev_sql_query;
358 // Drop database is not allowed -> ensure the query can be run
359 if (!$cfg['AllowUserDropDatabase']
360 && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE ', $sql_query)) {
361 // Checks if the user is a Superuser
362 // TODO: set a global variable with this information
363 // loic1: optimized query
364 $result = @PMA_mysql_query('USE mysql');
365 if (PMA_mysql_error()) {
366 include('./header.inc.php3');
367 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
370 define('PMA_CHK_DROP', 1);
373 * Executes the query
375 if ($sql_query != '') {
376 $pieces = array();
377 PMA_splitSqlFile($pieces, $sql_query, PMA_MYSQL_INT_VERSION);
378 $pieces_count = count($pieces);
379 if ($pieces_count > 1) {
380 $is_multiple = TRUE;
383 // Copy of the cleaned sql statement for display purpose only (see near the
384 // beginning of "db_details.php3" & "tbl_properties.php3")
385 if ($sql_file != 'none' && $pieces_count > 10) {
386 // Be nice with bandwidth...
387 $sql_query_cpy = $sql_query = '';
388 } else {
389 $sql_query_cpy = implode(";\n", $pieces) . ';';
390 // Be nice with bandwidth... for now, an arbitrary limit of 500,
391 // could be made configurable but probably not necessary
392 if (strlen($sql_query_cpy) > 500) {
393 $sql_query_cpy = $sql_query = '';
397 // really run the query?
398 if ($view_bookmark == 0) {
399 // Only one query to run
400 if ($pieces_count == 1 && !empty($pieces[0])) {
401 // sql.php3 will stripslash the query if get_magic_quotes_gpc
402 if (get_magic_quotes_gpc() == 1) {
403 $sql_query = addslashes($pieces[0]);
404 } else {
405 $sql_query = $pieces[0];
407 if (eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $sql_query)) {
408 $reload = 1;
410 include('./sql.php3');
411 exit();
414 // Runs multiple queries
415 else if (PMA_mysql_select_db($db)) {
416 $mult = TRUE;
417 for ($i = 0; $i < $pieces_count; $i++) {
418 $a_sql_query = $pieces[$i];
419 $result = PMA_mysql_query($a_sql_query);
420 if ($result == FALSE) { // readdump failed
421 $my_die = $a_sql_query;
422 break;
424 if (!isset($reload) && eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $a_sql_query)) {
425 $reload = 1;
427 } // end for
428 } // end else if
429 } // end if (really run the query)
430 unset($pieces);
431 } // end if
436 * MySQL error
438 if (isset($my_die)) {
439 $js_to_run = 'functions.js';
440 include('./header.inc.php3');
441 PMA_mysqlDie('', $my_die, '', $err_url);
446 * Go back to the calling script
448 // Checks for a valid target script
449 if (isset($table) && $table == '') {
450 unset($table);
452 if (isset($db) && $db == '') {
453 unset($db);
455 $is_db = $is_table = FALSE;
456 if ($goto == 'tbl_properties.php3') {
457 if (!isset($table)) {
458 $goto = 'db_details.php3';
459 } else {
460 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
461 if (!($is_table && @mysql_numrows($is_table))) {
462 $goto = 'db_details.php3';
463 unset($table);
465 } // end if... else...
467 if ($goto == 'db_details.php3') {
468 if (isset($table)) {
469 unset($table);
471 if (!isset($db)) {
472 $goto = 'main.php3';
473 } else {
474 $is_db = @PMA_mysql_select_db($db);
475 if (!$is_db) {
476 $goto = 'main.php3';
477 unset($db);
479 } // end if... else...
481 // Defines the message to be displayed
482 if (!empty($id_bookmark) && $action_bookmark == 2) {
483 $message = $strBookmarkDeleted;
484 } else if (!isset($sql_query_cpy)) {
485 $message = $strNoQuery;
486 } else if ($sql_query_cpy == '') {
487 $message = "$strSuccess&nbsp;:<br />$strTheContent ($pieces_count $strInstructions)&nbsp;";
488 } else {
489 $message = $strSuccess;
491 // Loads to target script
492 if ($goto == 'db_details.php3' || $goto == 'tbl_properties.php3') {
493 $js_to_run = 'functions.js';
495 if ($goto != 'main.php3') {
496 include('./header.inc.php3');
498 require('./' . $goto);