Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / import.php
blob1b3205aa8d35b1c60db4c241a4c36929a7b5d943
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Core script for import, this is just the glue around all other stuff
6 * @package PhpMyAdmin
7 */
9 /**
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/sql.lib.php';
14 require_once 'libraries/bookmark.lib.php';
15 //require_once 'libraries/display_import_functions.lib.php';
17 if (isset($_REQUEST['show_as_php'])) {
18 $GLOBALS['show_as_php'] = $_REQUEST['show_as_php'];
21 /**
22 * Sets globals from $_POST
24 $post_params = array(
25 'action_bookmark',
26 'allow_interrupt',
27 'bkm_label',
28 'bookmark_variable',
29 'charset_of_file',
30 'format',
31 'id_bookmark',
32 'import_type',
33 'is_js_confirmed',
34 'MAX_FILE_SIZE',
35 'message_to_show',
36 'noplugin',
37 'skip_queries',
38 'local_import_file'
41 // TODO: adapt full list of allowed parameters, as in export.php
42 foreach ($post_params as $one_post_param) {
43 if (isset($_POST[$one_post_param])) {
44 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
48 // reset import messages for ajax request
49 $_SESSION['Import_message']['message'] = null;
50 $_SESSION['Import_message']['go_back_url'] = null;
51 // default values
52 $GLOBALS['reload'] = false;
54 // Use to identify curren cycle is executing
55 // a multiquery statement or stored routine
56 if (!isset($_SESSION['is_multi_query'])) {
57 $_SESSION['is_multi_query'] = false;
60 // Are we just executing plain query or sql file?
61 // (eg. non import, but query box/window run)
62 if (! empty($sql_query)) {
63 // run SQL query
64 $import_text = $sql_query;
65 $import_type = 'query';
66 $format = 'sql';
68 // refresh navigation and main panels
69 if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
70 $GLOBALS['reload'] = true;
73 // refresh navigation panel only
74 if (preg_match(
75 '/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i',
76 $sql_query
77 )) {
78 $ajax_reload['reload'] = true;
81 // do a dynamic reload if table is RENAMED
82 // (by sending the instruction to the AJAX response handler)
83 if (preg_match(
84 '/^RENAME\s+TABLE\s+(.*?)\s+TO\s+(.*?)($|;|\s)/i',
85 $sql_query,
86 $rename_table_names
87 )) {
88 $ajax_reload['table_name'] = PMA_Util::unQuote($rename_table_names[2]);
89 $ajax_reload['reload'] = true;
92 $sql_query = '';
93 } elseif (! empty($sql_localfile)) {
94 // run SQL file on server
95 $local_import_file = $sql_localfile;
96 $import_type = 'queryfile';
97 $format = 'sql';
98 unset($sql_localfile);
99 } elseif (! empty($sql_file)) {
100 // run uploaded SQL file
101 $import_file = $sql_file;
102 $import_type = 'queryfile';
103 $format = 'sql';
104 unset($sql_file);
105 } elseif (! empty($id_bookmark)) {
106 // run bookmark
107 $import_type = 'query';
108 $format = 'sql';
111 // If we didn't get any parameters, either user called this directly, or
112 // upload limit has been reached, let's assume the second possibility.
114 if ($_POST == array() && $_GET == array()) {
115 $message = PMA_Message::error(
116 __('You probably tried to upload a file that is too large. Please refer to %sdocumentation%s for a workaround for this limit.')
118 $message->addParam('[doc@faq1-16]');
119 $message->addParam('[/doc]');
121 // so we can obtain the message
122 $_SESSION['Import_message']['message'] = $message->getDisplay();
123 $_SESSION['Import_message']['go_back_url'] = $goto;
125 $message->display();
126 exit; // the footer is displayed automatically
130 * Sets globals from $_POST patterns, for import plugins
131 * We only need to load the selected plugin
134 if (! in_array(
135 $format,
136 array(
137 'csv',
138 'ldi',
139 'mediawiki',
140 'ods',
141 'shp',
142 'sql',
143 'xml'
147 // this should not happen for a normal user
148 // but only during an attack
149 PMA_fatalError('Incorrect format parameter');
152 $post_patterns = array(
153 '/^force_file_/',
154 '/^'. $format . '_/'
156 foreach (array_keys($_POST) as $post_key) {
157 foreach ($post_patterns as $one_post_pattern) {
158 if (preg_match($one_post_pattern, $post_key)) {
159 $GLOBALS[$post_key] = $_POST[$post_key];
164 // Check needed parameters
165 PMA_Util::checkParameters(array('import_type', 'format'));
167 // We don't want anything special in format
168 $format = PMA_securePath($format);
170 // Import functions
171 require_once 'libraries/import.lib.php';
173 // Create error and goto url
174 if ($import_type == 'table') {
175 $err_url = 'tbl_import.php?' . PMA_URL_getCommon($db, $table);
176 $_SESSION['Import_message']['go_back_url'] = $err_url;
177 $goto = 'tbl_import.php';
178 } elseif ($import_type == 'database') {
179 $err_url = 'db_import.php?' . PMA_URL_getCommon($db);
180 $_SESSION['Import_message']['go_back_url'] = $err_url;
181 $goto = 'db_import.php';
182 } elseif ($import_type == 'server') {
183 $err_url = 'server_import.php?' . PMA_URL_getCommon();
184 $_SESSION['Import_message']['go_back_url'] = $err_url;
185 $goto = 'server_import.php';
186 } else {
187 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
188 if (strlen($table) && strlen($db)) {
189 $goto = 'tbl_structure.php';
190 } elseif (strlen($db)) {
191 $goto = 'db_structure.php';
192 } else {
193 $goto = 'server_sql.php';
196 if (strlen($table) && strlen($db)) {
197 $common = PMA_URL_getCommon($db, $table);
198 } elseif (strlen($db)) {
199 $common = PMA_URL_getCommon($db);
200 } else {
201 $common = PMA_URL_getCommon();
203 $err_url = $goto . '?' . $common
204 . (preg_match('@^tbl_[a-z]*\.php$@', $goto)
205 ? '&amp;table=' . htmlspecialchars($table)
206 : '');
207 $_SESSION['Import_message']['go_back_url'] = $err_url;
211 if (strlen($db)) {
212 $GLOBALS['dbi']->selectDb($db);
215 @set_time_limit($cfg['ExecTimeLimit']);
216 if (! empty($cfg['MemoryLimit'])) {
217 @ini_set('memory_limit', $cfg['MemoryLimit']);
220 $timestamp = time();
221 if (isset($allow_interrupt)) {
222 $maximum_time = ini_get('max_execution_time');
223 } else {
224 $maximum_time = 0;
227 // set default values
228 $timeout_passed = false;
229 $error = false;
230 $read_multiply = 1;
231 $finished = false;
232 $offset = 0;
233 $max_sql_len = 0;
234 $file_to_unlink = '';
235 $sql_query = '';
236 $sql_query_disabled = false;
237 $go_sql = false;
238 $executed_queries = 0;
239 $run_query = true;
240 $charset_conversion = false;
241 $reset_charset = false;
242 $bookmark_created = false;
244 // Bookmark Support: get a query back from bookmark if required
245 if (! empty($id_bookmark)) {
246 $id_bookmark = (int)$id_bookmark;
247 include_once 'libraries/bookmark.lib.php';
248 switch ($action_bookmark) {
249 case 0: // bookmarked query that have to be run
250 $import_text = PMA_Bookmark_get(
251 $db,
252 $id_bookmark,
253 'id',
254 isset($action_bookmark_all)
256 if (isset($bookmark_variable) && ! empty($bookmark_variable)) {
257 $import_text = preg_replace(
258 '|/\*(.*)\[VARIABLE\](.*)\*/|imsU',
259 '${1}' . PMA_Util::sqlAddSlashes($bookmark_variable) . '${2}',
260 $import_text
264 // refresh navigation and main panels
265 if (preg_match(
266 '/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i',
267 $import_text
268 )) {
269 $GLOBALS['reload'] = true;
272 // refresh navigation panel only
273 if (preg_match(
274 '/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i',
275 $import_text
278 $ajax_reload['reload'] = true;
280 break;
281 case 1: // bookmarked query that have to be displayed
282 $import_text = PMA_Bookmark_get($db, $id_bookmark);
283 if ($GLOBALS['is_ajax_request'] == true) {
284 $message = PMA_Message::success(__('Showing bookmark'));
285 $response = PMA_Response::getInstance();
286 $response->isSuccess($message->isSuccess());
287 $response->addJSON('message', $message);
288 $response->addJSON('sql_query', $import_text);
289 $response->addJSON('action_bookmark', $action_bookmark);
290 exit;
291 } else {
292 $run_query = false;
294 break;
295 case 2: // bookmarked query that have to be deleted
296 $import_text = PMA_Bookmark_get($db, $id_bookmark);
297 PMA_Bookmark_delete($db, $id_bookmark);
298 if ($GLOBALS['is_ajax_request'] == true) {
299 $message = PMA_Message::success(__('The bookmark has been deleted.'));
300 $response = PMA_Response::getInstance();
301 $response->isSuccess($message->isSuccess());
302 $response->addJSON('message', $message);
303 $response->addJSON('action_bookmark', $action_bookmark);
304 $response->addJSON('id_bookmark', $id_bookmark);
305 exit;
306 } else {
307 $run_query = false;
308 $error = true; // this is kind of hack to skip processing the query
310 break;
312 } // end bookmarks reading
314 // Do no run query if we show PHP code
315 if (isset($GLOBALS['show_as_php'])) {
316 $run_query = false;
317 $go_sql = true;
320 // We can not read all at once, otherwise we can run out of memory
321 $memory_limit = trim(@ini_get('memory_limit'));
322 // 2 MB as default
323 if (empty($memory_limit)) {
324 $memory_limit = 2 * 1024 * 1024;
326 // In case no memory limit we work on 10MB chunks
327 if ($memory_limit == -1) {
328 $memory_limit = 10 * 1024 * 1024;
331 // Calculate value of the limit
332 if (strtolower(substr($memory_limit, -1)) == 'm') {
333 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
334 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
335 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
336 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
337 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
338 } else {
339 $memory_limit = (int)$memory_limit;
342 // Just to be sure, there might be lot of memory needed for uncompression
343 $read_limit = $memory_limit / 8;
345 // handle filenames
346 if (isset($_FILES['import_file'])) {
347 $import_file = $_FILES['import_file']['tmp_name'];
349 if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) {
351 // sanitize $local_import_file as it comes from a POST
352 $local_import_file = PMA_securePath($local_import_file);
354 $import_file = PMA_Util::userDir($cfg['UploadDir'])
355 . $local_import_file;
357 } elseif (empty($import_file) || ! is_uploaded_file($import_file)) {
358 $import_file = 'none';
361 // Do we have file to import?
363 if ($import_file != 'none' && ! $error) {
364 // work around open_basedir and other limitations
365 $open_basedir = @ini_get('open_basedir');
367 // If we are on a server with open_basedir, we must move the file
368 // before opening it. The doc explains how to create the "./tmp"
369 // directory
371 if (! empty($open_basedir)) {
373 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/');
375 if (is_writable($tmp_subdir)) {
378 $import_file_new = $tmp_subdir . basename($import_file) . uniqid();
379 if (move_uploaded_file($import_file, $import_file_new)) {
380 $import_file = $import_file_new;
381 $file_to_unlink = $import_file_new;
384 $size = filesize($import_file);
389 * Handle file compression
390 * @todo duplicate code exists in File.class.php
392 $compression = PMA_detectCompression($import_file);
393 if ($compression === false) {
394 $message = PMA_Message::error(__('File could not be read'));
395 $error = true;
396 } else {
397 switch ($compression) {
398 case 'application/bzip2':
399 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
400 $import_handle = @bzopen($import_file, 'r');
401 } else {
402 $message = PMA_Message::error(
403 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
405 $message->addParam($compression);
406 $error = true;
408 break;
409 case 'application/gzip':
410 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
411 $import_handle = @gzopen($import_file, 'r');
412 } else {
413 $message = PMA_Message::error(
414 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
416 $message->addParam($compression);
417 $error = true;
419 break;
420 case 'application/zip':
421 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
423 * Load interface for zip extension.
425 include_once 'libraries/zip_extension.lib.php';
426 $result = PMA_getZipContents($import_file);
427 if (! empty($result['error'])) {
428 $message = PMA_Message::rawError($result['error']);
429 $error = true;
430 } else {
431 $import_text = $result['data'];
433 } else {
434 $message = PMA_Message::error(
435 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
437 $message->addParam($compression);
438 $error = true;
440 break;
441 case 'none':
442 $import_handle = @fopen($import_file, 'r');
443 break;
444 default:
445 $message = PMA_Message::error(
446 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
448 $message->addParam($compression);
449 $error = true;
450 break;
453 // use isset() because zip compression type does not use a handle
454 if (! $error && isset($import_handle) && $import_handle === false) {
455 $message = PMA_Message::error(__('File could not be read'));
456 $error = true;
458 } elseif (! $error) {
459 if (! isset($import_text) || empty($import_text)) {
460 $message = PMA_Message::error(
461 __('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].')
463 $error = true;
467 // so we can obtain the message
468 //$_SESSION['Import_message'] = $message->getDisplay();
470 // Convert the file's charset if necessary
471 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
472 if ($charset_of_file != 'utf-8') {
473 $charset_conversion = true;
475 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
476 if (PMA_DRIZZLE) {
477 // Drizzle doesn't support other character sets,
478 // so we can't fallback to SET NAMES - throw an error
479 $error = true;
480 $message = PMA_Message::error(
481 __('Cannot convert file\'s character set without character set conversion library')
483 } else {
484 $GLOBALS['dbi']->query('SET NAMES \'' . $charset_of_file . '\'');
485 // We can not show query in this case, it is in different charset
486 $sql_query_disabled = true;
487 $reset_charset = true;
491 // Something to skip?
492 if (! $error && isset($skip)) {
493 $original_skip = $skip;
494 while ($skip > 0) {
495 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
496 // Disable read progresivity, otherwise we eat all memory!
497 $read_multiply = 1;
498 $skip -= $read_limit;
500 unset($skip);
503 // This array contain the data like numberof valid sql queries in the statement
504 // and complete valid sql statement (which affected for rows)
505 $sql_data = array('valid_sql' => array(), 'valid_queries' => 0);
507 if (! $error) {
508 // Check for file existance
509 include_once "libraries/plugin_interface.lib.php";
510 $import_plugin = PMA_getPlugin(
511 "import",
512 $format,
513 'libraries/plugins/import/',
514 $import_type
516 if ($import_plugin == null) {
517 $error = true;
518 $message = PMA_Message::error(
519 __('Could not load import plugins, please check your installation!')
521 } else {
522 // Do the real import
523 $import_plugin->doImport($sql_data);
527 if (! $error && false !== $import_handle && null !== $import_handle) {
528 fclose($import_handle);
531 // Cleanup temporary file
532 if ($file_to_unlink != '') {
533 unlink($file_to_unlink);
536 // Reset charset back, if we did some changes
537 if ($reset_charset) {
538 $GLOBALS['dbi']->query('SET CHARACTER SET utf8');
539 $GLOBALS['dbi']->query(
540 'SET SESSION collation_connection =\'' . $collation_connection . '\''
544 // Show correct message
545 if (! empty($id_bookmark) && $action_bookmark == 2) {
546 $message = PMA_Message::success(__('The bookmark has been deleted.'));
547 $display_query = $import_text;
548 $error = false; // unset error marker, it was used just to skip processing
549 } elseif (! empty($id_bookmark) && $action_bookmark == 1) {
550 $message = PMA_Message::notice(__('Showing bookmark'));
551 } elseif ($bookmark_created) {
552 $special_message = '[br]' . sprintf(
553 __('Bookmark %s created'),
554 htmlspecialchars($bkm_label)
556 } elseif ($finished && ! $error) {
557 if ($import_type == 'query') {
558 $message = PMA_Message::success();
559 } else {
560 if ($import_notice) {
561 $message = PMA_Message::success(
562 '<em>'
563 . __('Import has been successfully finished, %d queries executed.')
564 . '</em>'
566 $message->addParam($executed_queries);
568 $message->addString($import_notice);
569 if (isset($local_import_file)) {
570 $message->addString('(' . $local_import_file . ')');
571 } else {
572 $message->addString('(' . $_FILES['import_file']['name'] . ')');
574 } else {
575 $message = PMA_Message::success(
576 __('Import has been successfully finished, %d queries executed.')
578 $message->addParam($executed_queries);
579 if (isset($local_import_file)) {
580 $message->addString('(' . $local_import_file . ')');
581 } else {
582 $message->addString('(' . $_FILES['import_file']['name'] . ')');
588 // Did we hit timeout? Tell it user.
589 if ($timeout_passed) {
590 $message = PMA_Message::error(
591 __('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.')
593 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
594 $message->addString(
595 __('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.')
600 // if there is any message, copy it into $_SESSION as well,
601 // so we can obtain it by AJAX call
602 if (isset($message)) {
603 $_SESSION['Import_message']['message'] = $message->getDisplay();
605 // Parse and analyze the query, for correct db and table name
606 // in case of a query typed in the query window
607 // (but if the query is too large, in case of an imported file, the parser
608 // can choke on it so avoid parsing)
609 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
610 include_once 'libraries/parse_analyze.inc.php';
613 // There was an error?
614 if (isset($my_die)) {
615 foreach ($my_die as $key => $die) {
616 PMA_Util::mysqlDie(
617 $die['error'], $die['sql'], '', $err_url, $error
622 if ($go_sql) {
623 // parse sql query
624 include_once 'libraries/parse_analyze.inc.php';
626 PMA_executeQueryAndSendQueryResponse(
627 $analyzed_sql_results, false, $db, $table, null, $import_text, null,
628 false, null,
629 null, null, null, $goto, $pmaThemeImage, null, null, null, $sql_query,
630 null, null
632 } else if ($result) {
633 $response = PMA_Response::getInstance();
634 $response->isSuccess(true);
635 $response->addJSON('message', PMA_Message::success($msg));
636 $response->addJSON(
637 'sql_query',
638 PMA_Util::getMessage($msg, $sql_query, 'success')
640 } else if ($result == false) {
641 $response = PMA_Response::getInstance();
642 $response->isSuccess(false);
643 $response->addJSON('message', PMA_Message::error($msg));
644 } else {
645 $active_page = $goto;
646 include '' . $goto;