2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Core script for import, this is just the glue around all other stuff
10 * Get the variables sent or posted to this script and a core script
12 require_once 'libraries/common.inc.php';
13 //require_once 'libraries/display_import_functions.lib.php';
15 if (isset($_REQUEST['show_as_php'])) {
16 $GLOBALS['show_as_php'] = $_REQUEST['show_as_php'];
20 * Sets globals from $_POST
38 foreach ($post_params as $one_post_param) {
39 if (isset($_POST[$one_post_param])) {
40 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
44 // reset import messages for ajax request
45 $_SESSION['Import_message']['message'] = null;
46 $_SESSION['Import_message']['go_back_url'] = null;
48 $GLOBALS['reload'] = false;
50 // Use to identify curren cycle is executing
51 // a multiquery statement or stored routine
52 if (!isset($_SESSION['is_multi_query'])) {
53 $_SESSION['is_multi_query'] = false;
56 // Are we just executing plain query or sql file?
57 // (eg. non import, but query box/window run)
58 if (! empty($sql_query)) {
60 $import_text = $sql_query;
61 $import_type = 'query';
64 // refresh navigation and main panels
65 if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
66 $GLOBALS['reload'] = true;
69 // refresh navigation panel only
70 if (preg_match('/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
71 $ajax_reload['reload'] = true;
74 // do a dynamic reload if table is RENAMED
75 // (by sending the instruction to the AJAX response handler)
76 if (preg_match('/^RENAME\s+TABLE\s+(.*?)\s+TO\s+(.*?)($|;|\s)/i', $sql_query, $rename_table_names)) {
77 $ajax_reload['table_name'] = PMA_Util
::unQuote($rename_table_names[2]);
78 $ajax_reload['reload'] = true;
82 } elseif (! empty($sql_localfile)) {
83 // run SQL file on server
84 $local_import_file = $sql_localfile;
85 $import_type = 'queryfile';
87 unset($sql_localfile);
88 } elseif (! empty($sql_file)) {
89 // run uploaded SQL file
90 $import_file = $sql_file;
91 $import_type = 'queryfile';
94 } elseif (! empty($id_bookmark)) {
96 $import_type = 'query';
100 // If we didn't get any parameters, either user called this directly, or
101 // upload limit has been reached, let's assume the second possibility.
103 if ($_POST == array() && $_GET == array()) {
104 $message = PMA_Message
::error(
105 __('You probably tried to upload a file that is too large. Please refer to %sdocumentation%s for a workaround for this limit.')
107 $message->addParam('[doc@faq1-16]');
108 $message->addParam('[/doc]');
110 // so we can obtain the message
111 $_SESSION['Import_message']['message'] = $message->getDisplay();
112 $_SESSION['Import_message']['go_back_url'] = $goto;
115 exit; // the footer is displayed automatically
119 * Sets globals from $_POST patterns, for import plugins
120 * We only need to load the selected plugin
123 $post_patterns = array(
127 foreach (array_keys($_POST) as $post_key) {
128 foreach ($post_patterns as $one_post_pattern) {
129 if (preg_match($one_post_pattern, $post_key)) {
130 $GLOBALS[$post_key] = $_POST[$post_key];
135 // Check needed parameters
136 PMA_Util
::checkParameters(array('import_type', 'format'));
138 // We don't want anything special in format
139 $format = PMA_securePath($format);
142 require_once 'libraries/import.lib.php';
144 // Create error and goto url
145 if ($import_type == 'table') {
146 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
147 $_SESSION['Import_message']['go_back_url'] = $err_url;
148 $goto = 'tbl_import.php';
149 } elseif ($import_type == 'database') {
150 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
151 $_SESSION['Import_message']['go_back_url'] = $err_url;
152 $goto = 'db_import.php';
153 } elseif ($import_type == 'server') {
154 $err_url = 'server_import.php?' . PMA_generate_common_url();
155 $_SESSION['Import_message']['go_back_url'] = $err_url;
156 $goto = 'server_import.php';
158 if (empty($goto) ||
!preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
159 if (strlen($table) && strlen($db)) {
160 $goto = 'tbl_structure.php';
161 } elseif (strlen($db)) {
162 $goto = 'db_structure.php';
164 $goto = 'server_sql.php';
167 if (strlen($table) && strlen($db)) {
168 $common = PMA_generate_common_url($db, $table);
169 } elseif (strlen($db)) {
170 $common = PMA_generate_common_url($db);
172 $common = PMA_generate_common_url();
174 $err_url = $goto . '?' . $common
175 . (preg_match('@^tbl_[a-z]*\.php$@', $goto)
176 ?
'&table=' . htmlspecialchars($table)
178 $_SESSION['Import_message']['go_back_url'] = $err_url;
183 PMA_DBI_select_db($db);
186 @set_time_limit
($cfg['ExecTimeLimit']);
187 if (! empty($cfg['MemoryLimit'])) {
188 @ini_set
('memory_limit', $cfg['MemoryLimit']);
192 if (isset($allow_interrupt)) {
193 $maximum_time = ini_get('max_execution_time');
198 // set default values
199 $timeout_passed = false;
205 $file_to_unlink = '';
207 $sql_query_disabled = false;
209 $executed_queries = 0;
211 $charset_conversion = false;
212 $reset_charset = false;
213 $bookmark_created = false;
215 // Bookmark Support: get a query back from bookmark if required
216 if (! empty($id_bookmark)) {
217 $id_bookmark = (int)$id_bookmark;
218 include_once 'libraries/bookmark.lib.php';
219 switch ($action_bookmark) {
220 case 0: // bookmarked query that have to be run
221 $import_text = PMA_Bookmark_get(
225 isset($action_bookmark_all)
227 if (isset($bookmark_variable) && ! empty($bookmark_variable)) {
228 $import_text = preg_replace(
229 '|/\*(.*)\[VARIABLE\](.*)\*/|imsU',
230 '${1}' . PMA_Util
::sqlAddSlashes($bookmark_variable) . '${2}',
235 // refresh navigation and main panels
236 if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
237 $GLOBALS['reload'] = true;
240 // refresh navigation panel only
241 if (preg_match('/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
242 $ajax_reload['reload'] = true;
245 case 1: // bookmarked query that have to be displayed
246 $import_text = PMA_Bookmark_get($db, $id_bookmark);
247 if ($GLOBALS['is_ajax_request'] == true) {
248 $message = PMA_Message
::success(__('Showing bookmark'));
249 $response = PMA_Response
::getInstance();
250 $response->isSuccess($message->isSuccess());
251 $response->addJSON('message', $message);
252 $response->addJSON('sql_query', $import_text);
253 $response->addJSON('action_bookmark', $action_bookmark);
259 case 2: // bookmarked query that have to be deleted
260 $import_text = PMA_Bookmark_get($db, $id_bookmark);
261 PMA_Bookmark_delete($db, $id_bookmark);
262 if ($GLOBALS['is_ajax_request'] == true) {
263 $message = PMA_Message
::success(__('The bookmark has been deleted.'));
264 $response = PMA_Response
::getInstance();
265 $response->isSuccess($message->isSuccess());
266 $response->addJSON('message', $message);
267 $response->addJSON('action_bookmark', $action_bookmark);
268 $response->addJSON('id_bookmark', $id_bookmark);
272 $error = true; // this is kind of hack to skip processing the query
276 } // end bookmarks reading
278 // Do no run query if we show PHP code
279 if (isset($GLOBALS['show_as_php'])) {
284 // We can not read all at once, otherwise we can run out of memory
285 $memory_limit = trim(@ini_get
('memory_limit'));
287 if (empty($memory_limit)) {
288 $memory_limit = 2 * 1024 * 1024;
290 // In case no memory limit we work on 10MB chunks
291 if ($memory_limit == -1) {
292 $memory_limit = 10 * 1024 * 1024;
295 // Calculate value of the limit
296 if (strtolower(substr($memory_limit, -1)) == 'm') {
297 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
298 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
299 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
300 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
301 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
303 $memory_limit = (int)$memory_limit;
306 // Just to be sure, there might be lot of memory needed for uncompression
307 $read_limit = $memory_limit / 8;
310 if (isset($_FILES['import_file'])) {
311 $import_file = $_FILES['import_file']['tmp_name'];
313 if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) {
315 // sanitize $local_import_file as it comes from a POST
316 $local_import_file = PMA_securePath($local_import_file);
318 $import_file = PMA_Util
::userDir($cfg['UploadDir'])
319 . $local_import_file;
321 } elseif (empty($import_file) ||
! is_uploaded_file($import_file)) {
322 $import_file = 'none';
325 // Do we have file to import?
327 if ($import_file != 'none' && ! $error) {
328 // work around open_basedir and other limitations
329 $open_basedir = @ini_get
('open_basedir');
331 // If we are on a server with open_basedir, we must move the file
332 // before opening it. The doc explains how to create the "./tmp"
335 if (! empty($open_basedir)) {
337 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : 'tmp/');
339 if (is_writable($tmp_subdir)) {
342 $import_file_new = $tmp_subdir . basename($import_file) . uniqid();
343 if (move_uploaded_file($import_file, $import_file_new)) {
344 $import_file = $import_file_new;
345 $file_to_unlink = $import_file_new;
348 $size = filesize($import_file);
353 * Handle file compression
354 * @todo duplicate code exists in File.class.php
356 $compression = PMA_detectCompression($import_file);
357 if ($compression === false) {
358 $message = PMA_Message
::error(__('File could not be read'));
361 switch ($compression) {
362 case 'application/bzip2':
363 if ($cfg['BZipDump'] && @function_exists
('bzopen')) {
364 $import_handle = @bzopen
($import_file, 'r');
366 $message = PMA_Message
::error(
367 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
369 $message->addParam($compression);
373 case 'application/gzip':
374 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
375 $import_handle = @gzopen
($import_file, 'r');
377 $message = PMA_Message
::error(
378 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
380 $message->addParam($compression);
384 case 'application/zip':
385 if ($cfg['ZipDump'] && @function_exists
('zip_open')) {
387 * Load interface for zip extension.
389 include_once 'libraries/zip_extension.lib.php';
390 $result = PMA_getZipContents($import_file);
391 if (! empty($result['error'])) {
392 $message = PMA_Message
::rawError($result['error']);
395 $import_text = $result['data'];
398 $message = PMA_Message
::error(
399 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
401 $message->addParam($compression);
406 $import_handle = @fopen
($import_file, 'r');
409 $message = PMA_Message
::error(
410 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
412 $message->addParam($compression);
417 // use isset() because zip compression type does not use a handle
418 if (! $error && isset($import_handle) && $import_handle === false) {
419 $message = PMA_Message
::error(__('File could not be read'));
422 } elseif (! $error) {
423 if (! isset($import_text) ||
empty($import_text)) {
424 $message = PMA_Message
::error(
425 __('No data was received to import. Either no file name was submitted, or the file size exceeded the maximum size permitted by your PHP configuration. See [doc@faq1-16]FAQ 1.16[/doc].')
431 // so we can obtain the message
432 //$_SESSION['Import_message'] = $message->getDisplay();
434 // Convert the file's charset if necessary
435 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
&& isset($charset_of_file)) {
436 if ($charset_of_file != 'utf-8') {
437 $charset_conversion = true;
439 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
441 // Drizzle doesn't support other character sets,
442 // so we can't fallback to SET NAMES - throw an error
444 $message = PMA_Message
::error(
445 __('Cannot convert file\'s character set without character set conversion library')
448 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
449 // We can not show query in this case, it is in different charset
450 $sql_query_disabled = true;
451 $reset_charset = true;
455 // Something to skip?
456 if (! $error && isset($skip)) {
457 $original_skip = $skip;
459 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
460 // Disable read progresivity, otherwise we eat all memory!
462 $skip -= $read_limit;
467 // This array contain the data like numberof valid sql queries in the statement
468 // and complete valid sql statement (which affected for rows)
469 $sql_data = array('valid_sql' => array(), 'valid_queries' => 0);
472 // Check for file existance
473 include_once "libraries/plugin_interface.lib.php";
474 $import_plugin = PMA_getPlugin(
477 'libraries/plugins/import/'
479 if ($import_plugin == null) {
481 $message = PMA_Message
::error(
482 __('Could not load import plugins, please check your installation!')
485 // Do the real import
486 $import_plugin->doImport($sql_data);
490 if (! $error && false !== $import_handle && null !== $import_handle) {
491 fclose($import_handle);
494 // Cleanup temporary file
495 if ($file_to_unlink != '') {
496 unlink($file_to_unlink);
499 // Reset charset back, if we did some changes
500 if ($reset_charset) {
501 PMA_DBI_query('SET CHARACTER SET utf8');
503 'SET SESSION collation_connection =\'' . $collation_connection . '\''
507 // Show correct message
508 if (! empty($id_bookmark) && $action_bookmark == 2) {
509 $message = PMA_Message
::success(__('The bookmark has been deleted.'));
510 $display_query = $import_text;
511 $error = false; // unset error marker, it was used just to skip processing
512 } elseif (! empty($id_bookmark) && $action_bookmark == 1) {
513 $message = PMA_Message
::notice(__('Showing bookmark'));
514 } elseif ($bookmark_created) {
515 $special_message = '[br]' . sprintf(
516 __('Bookmark %s created'),
517 htmlspecialchars($bkm_label)
519 } elseif ($finished && ! $error) {
520 if ($import_type == 'query') {
521 $message = PMA_Message
::success();
523 if ($import_notice) {
524 $message = PMA_Message
::success(
525 '<em>' . __('Import has been successfully finished, %d queries executed.') . '</em>'
527 $message->addParam($executed_queries);
529 $message->addString($import_notice);
530 $message->addString('(' . $_FILES['import_file']['name'] . ')');
532 $message = PMA_Message
::success(
533 __('Import has been successfully finished, %d queries executed.')
535 $message->addParam($executed_queries);
536 $message->addString('(' . $_FILES['import_file']['name'] . ')');
541 // Did we hit timeout? Tell it user.
542 if ($timeout_passed) {
543 $message = PMA_Message
::error(
544 __('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.')
546 if ($offset == 0 ||
(isset($original_skip) && $original_skip == $offset)) {
548 __('However on last run no data has been parsed, this usually means phpMyAdmin won\'t be able to finish this import unless you increase php time limits.')
553 // if there is any message, copy it into $_SESSION as well,
554 // so we can obtain it by AJAX call
555 if (isset($message)) {
556 $_SESSION['Import_message']['message'] = $message->getDisplay();
558 // Parse and analyze the query, for correct db and table name
559 // in case of a query typed in the query window
560 // (but if the query is too large, in case of an imported file, the parser
561 // can choke on it so avoid parsing)
562 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
563 include_once 'libraries/parse_analyze.lib.php';
566 // There was an error?
567 if (isset($my_die)) {
568 foreach ($my_die AS $key => $die) {
570 $die['error'], $die['sql'], '', $err_url, $error
575 // we want to see the results of the last query that returned at least a row
576 if (! empty($last_query_with_results)) {
577 // but we want to show intermediate results too
578 $disp_query = $sql_query;
579 $disp_message = __('Your SQL query has been executed successfully');
580 $sql_query = $last_query_with_results;
587 $active_page = $goto;