Translated using Weblate (German)
[phpmyadmin.git] / import.php
blob9a835667aa866c926b62504b36888cb69b2c51c7
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('/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
75 $ajax_reload['reload'] = true;
78 // do a dynamic reload if table is RENAMED
79 // (by sending the instruction to the AJAX response handler)
80 if (preg_match('/^RENAME\s+TABLE\s+(.*?)\s+TO\s+(.*?)($|;|\s)/i', $sql_query, $rename_table_names)) {
81 $ajax_reload['table_name'] = PMA_Util::unQuote($rename_table_names[2]);
82 $ajax_reload['reload'] = true;
85 $sql_query = '';
86 } elseif (! empty($sql_localfile)) {
87 // run SQL file on server
88 $local_import_file = $sql_localfile;
89 $import_type = 'queryfile';
90 $format = 'sql';
91 unset($sql_localfile);
92 } elseif (! empty($sql_file)) {
93 // run uploaded SQL file
94 $import_file = $sql_file;
95 $import_type = 'queryfile';
96 $format = 'sql';
97 unset($sql_file);
98 } elseif (! empty($id_bookmark)) {
99 // run bookmark
100 $import_type = 'query';
101 $format = 'sql';
104 // If we didn't get any parameters, either user called this directly, or
105 // upload limit has been reached, let's assume the second possibility.
107 if ($_POST == array() && $_GET == array()) {
108 $message = PMA_Message::error(
109 __('You probably tried to upload a file that is too large. Please refer to %sdocumentation%s for a workaround for this limit.')
111 $message->addParam('[doc@faq1-16]');
112 $message->addParam('[/doc]');
114 // so we can obtain the message
115 $_SESSION['Import_message']['message'] = $message->getDisplay();
116 $_SESSION['Import_message']['go_back_url'] = $goto;
118 $message->display();
119 exit; // the footer is displayed automatically
123 * Sets globals from $_POST patterns, for import plugins
124 * We only need to load the selected plugin
127 if (! in_array(
128 $format,
129 array(
130 'csv',
131 'ldi',
132 'mediawiki',
133 'ods',
134 'shp',
135 'sql',
136 'xml'
140 // this should not happen for a normal user
141 // but only during an attack
142 PMA_fatalError('Incorrect format parameter');
145 $post_patterns = array(
146 '/^force_file_/',
147 '/^'. $format . '_/'
149 foreach (array_keys($_POST) as $post_key) {
150 foreach ($post_patterns as $one_post_pattern) {
151 if (preg_match($one_post_pattern, $post_key)) {
152 $GLOBALS[$post_key] = $_POST[$post_key];
157 // Check needed parameters
158 PMA_Util::checkParameters(array('import_type', 'format'));
160 // We don't want anything special in format
161 $format = PMA_securePath($format);
163 // Import functions
164 require_once 'libraries/import.lib.php';
166 // Create error and goto url
167 if ($import_type == 'table') {
168 $err_url = 'tbl_import.php?' . PMA_URL_getCommon($db, $table);
169 $_SESSION['Import_message']['go_back_url'] = $err_url;
170 $goto = 'tbl_import.php';
171 } elseif ($import_type == 'database') {
172 $err_url = 'db_import.php?' . PMA_URL_getCommon($db);
173 $_SESSION['Import_message']['go_back_url'] = $err_url;
174 $goto = 'db_import.php';
175 } elseif ($import_type == 'server') {
176 $err_url = 'server_import.php?' . PMA_URL_getCommon();
177 $_SESSION['Import_message']['go_back_url'] = $err_url;
178 $goto = 'server_import.php';
179 } else {
180 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
181 if (strlen($table) && strlen($db)) {
182 $goto = 'tbl_structure.php';
183 } elseif (strlen($db)) {
184 $goto = 'db_structure.php';
185 } else {
186 $goto = 'server_sql.php';
189 if (strlen($table) && strlen($db)) {
190 $common = PMA_URL_getCommon($db, $table);
191 } elseif (strlen($db)) {
192 $common = PMA_URL_getCommon($db);
193 } else {
194 $common = PMA_URL_getCommon();
196 $err_url = $goto . '?' . $common
197 . (preg_match('@^tbl_[a-z]*\.php$@', $goto)
198 ? '&amp;table=' . htmlspecialchars($table)
199 : '');
200 $_SESSION['Import_message']['go_back_url'] = $err_url;
204 if (strlen($db)) {
205 $GLOBALS['dbi']->selectDb($db);
208 @set_time_limit($cfg['ExecTimeLimit']);
209 if (! empty($cfg['MemoryLimit'])) {
210 @ini_set('memory_limit', $cfg['MemoryLimit']);
213 $timestamp = time();
214 if (isset($allow_interrupt)) {
215 $maximum_time = ini_get('max_execution_time');
216 } else {
217 $maximum_time = 0;
220 // set default values
221 $timeout_passed = false;
222 $error = false;
223 $read_multiply = 1;
224 $finished = false;
225 $offset = 0;
226 $max_sql_len = 0;
227 $file_to_unlink = '';
228 $sql_query = '';
229 $sql_query_disabled = false;
230 $go_sql = false;
231 $executed_queries = 0;
232 $run_query = true;
233 $charset_conversion = false;
234 $reset_charset = false;
235 $bookmark_created = false;
237 // Bookmark Support: get a query back from bookmark if required
238 if (! empty($id_bookmark)) {
239 $id_bookmark = (int)$id_bookmark;
240 include_once 'libraries/bookmark.lib.php';
241 switch ($action_bookmark) {
242 case 0: // bookmarked query that have to be run
243 $import_text = PMA_Bookmark_get(
244 $db,
245 $id_bookmark,
246 'id',
247 isset($action_bookmark_all)
249 if (isset($bookmark_variable) && ! empty($bookmark_variable)) {
250 $import_text = preg_replace(
251 '|/\*(.*)\[VARIABLE\](.*)\*/|imsU',
252 '${1}' . PMA_Util::sqlAddSlashes($bookmark_variable) . '${2}',
253 $import_text
257 // refresh navigation and main panels
258 if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
259 $GLOBALS['reload'] = true;
262 // refresh navigation panel only
263 if (preg_match('/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
264 $ajax_reload['reload'] = true;
266 break;
267 case 1: // bookmarked query that have to be displayed
268 $import_text = PMA_Bookmark_get($db, $id_bookmark);
269 if ($GLOBALS['is_ajax_request'] == true) {
270 $message = PMA_Message::success(__('Showing bookmark'));
271 $response = PMA_Response::getInstance();
272 $response->isSuccess($message->isSuccess());
273 $response->addJSON('message', $message);
274 $response->addJSON('sql_query', $import_text);
275 $response->addJSON('action_bookmark', $action_bookmark);
276 exit;
277 } else {
278 $run_query = false;
280 break;
281 case 2: // bookmarked query that have to be deleted
282 $import_text = PMA_Bookmark_get($db, $id_bookmark);
283 PMA_Bookmark_delete($db, $id_bookmark);
284 if ($GLOBALS['is_ajax_request'] == true) {
285 $message = PMA_Message::success(__('The bookmark has been deleted.'));
286 $response = PMA_Response::getInstance();
287 $response->isSuccess($message->isSuccess());
288 $response->addJSON('message', $message);
289 $response->addJSON('action_bookmark', $action_bookmark);
290 $response->addJSON('id_bookmark', $id_bookmark);
291 exit;
292 } else {
293 $run_query = false;
294 $error = true; // this is kind of hack to skip processing the query
296 break;
298 } // end bookmarks reading
300 // Do no run query if we show PHP code
301 if (isset($GLOBALS['show_as_php'])) {
302 $run_query = false;
303 $go_sql = true;
306 // We can not read all at once, otherwise we can run out of memory
307 $memory_limit = trim(@ini_get('memory_limit'));
308 // 2 MB as default
309 if (empty($memory_limit)) {
310 $memory_limit = 2 * 1024 * 1024;
312 // In case no memory limit we work on 10MB chunks
313 if ($memory_limit == -1) {
314 $memory_limit = 10 * 1024 * 1024;
317 // Calculate value of the limit
318 if (strtolower(substr($memory_limit, -1)) == 'm') {
319 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
320 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
321 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
322 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
323 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
324 } else {
325 $memory_limit = (int)$memory_limit;
328 // Just to be sure, there might be lot of memory needed for uncompression
329 $read_limit = $memory_limit / 8;
331 // handle filenames
332 if (isset($_FILES['import_file'])) {
333 $import_file = $_FILES['import_file']['tmp_name'];
335 if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) {
337 // sanitize $local_import_file as it comes from a POST
338 $local_import_file = PMA_securePath($local_import_file);
340 $import_file = PMA_Util::userDir($cfg['UploadDir'])
341 . $local_import_file;
343 } elseif (empty($import_file) || ! is_uploaded_file($import_file)) {
344 $import_file = 'none';
347 // Do we have file to import?
349 if ($import_file != 'none' && ! $error) {
350 // work around open_basedir and other limitations
351 $open_basedir = @ini_get('open_basedir');
353 // If we are on a server with open_basedir, we must move the file
354 // before opening it. The doc explains how to create the "./tmp"
355 // directory
357 if (! empty($open_basedir)) {
359 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/');
361 if (is_writable($tmp_subdir)) {
364 $import_file_new = $tmp_subdir . basename($import_file) . uniqid();
365 if (move_uploaded_file($import_file, $import_file_new)) {
366 $import_file = $import_file_new;
367 $file_to_unlink = $import_file_new;
370 $size = filesize($import_file);
375 * Handle file compression
376 * @todo duplicate code exists in File.class.php
378 $compression = PMA_detectCompression($import_file);
379 if ($compression === false) {
380 $message = PMA_Message::error(__('File could not be read'));
381 $error = true;
382 } else {
383 switch ($compression) {
384 case 'application/bzip2':
385 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
386 $import_handle = @bzopen($import_file, 'r');
387 } else {
388 $message = PMA_Message::error(
389 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
391 $message->addParam($compression);
392 $error = true;
394 break;
395 case 'application/gzip':
396 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
397 $import_handle = @gzopen($import_file, 'r');
398 } else {
399 $message = PMA_Message::error(
400 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
402 $message->addParam($compression);
403 $error = true;
405 break;
406 case 'application/zip':
407 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
409 * Load interface for zip extension.
411 include_once 'libraries/zip_extension.lib.php';
412 $result = PMA_getZipContents($import_file);
413 if (! empty($result['error'])) {
414 $message = PMA_Message::rawError($result['error']);
415 $error = true;
416 } else {
417 $import_text = $result['data'];
419 } else {
420 $message = PMA_Message::error(
421 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
423 $message->addParam($compression);
424 $error = true;
426 break;
427 case 'none':
428 $import_handle = @fopen($import_file, 'r');
429 break;
430 default:
431 $message = PMA_Message::error(
432 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
434 $message->addParam($compression);
435 $error = true;
436 break;
439 // use isset() because zip compression type does not use a handle
440 if (! $error && isset($import_handle) && $import_handle === false) {
441 $message = PMA_Message::error(__('File could not be read'));
442 $error = true;
444 } elseif (! $error) {
445 if (! isset($import_text) || empty($import_text)) {
446 $message = PMA_Message::error(
447 __('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].')
449 $error = true;
453 // so we can obtain the message
454 //$_SESSION['Import_message'] = $message->getDisplay();
456 // Convert the file's charset if necessary
457 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
458 if ($charset_of_file != 'utf-8') {
459 $charset_conversion = true;
461 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
462 if (PMA_DRIZZLE) {
463 // Drizzle doesn't support other character sets,
464 // so we can't fallback to SET NAMES - throw an error
465 $error = true;
466 $message = PMA_Message::error(
467 __('Cannot convert file\'s character set without character set conversion library')
469 } else {
470 $GLOBALS['dbi']->query('SET NAMES \'' . $charset_of_file . '\'');
471 // We can not show query in this case, it is in different charset
472 $sql_query_disabled = true;
473 $reset_charset = true;
477 // Something to skip?
478 if (! $error && isset($skip)) {
479 $original_skip = $skip;
480 while ($skip > 0) {
481 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
482 // Disable read progresivity, otherwise we eat all memory!
483 $read_multiply = 1;
484 $skip -= $read_limit;
486 unset($skip);
489 // This array contain the data like numberof valid sql queries in the statement
490 // and complete valid sql statement (which affected for rows)
491 $sql_data = array('valid_sql' => array(), 'valid_queries' => 0);
493 if (! $error) {
494 // Check for file existance
495 include_once "libraries/plugin_interface.lib.php";
496 $import_plugin = PMA_getPlugin(
497 "import",
498 $format,
499 'libraries/plugins/import/',
500 $import_type
502 if ($import_plugin == null) {
503 $error = true;
504 $message = PMA_Message::error(
505 __('Could not load import plugins, please check your installation!')
507 } else {
508 // Do the real import
509 $import_plugin->doImport($sql_data);
513 if (! $error && false !== $import_handle && null !== $import_handle) {
514 fclose($import_handle);
517 // Cleanup temporary file
518 if ($file_to_unlink != '') {
519 unlink($file_to_unlink);
522 // Reset charset back, if we did some changes
523 if ($reset_charset) {
524 $GLOBALS['dbi']->query('SET CHARACTER SET utf8');
525 $GLOBALS['dbi']->query(
526 'SET SESSION collation_connection =\'' . $collation_connection . '\''
530 // Show correct message
531 if (! empty($id_bookmark) && $action_bookmark == 2) {
532 $message = PMA_Message::success(__('The bookmark has been deleted.'));
533 $display_query = $import_text;
534 $error = false; // unset error marker, it was used just to skip processing
535 } elseif (! empty($id_bookmark) && $action_bookmark == 1) {
536 $message = PMA_Message::notice(__('Showing bookmark'));
537 } elseif ($bookmark_created) {
538 $special_message = '[br]' . sprintf(
539 __('Bookmark %s created'),
540 htmlspecialchars($bkm_label)
542 } elseif ($finished && ! $error) {
543 if ($import_type == 'query') {
544 $message = PMA_Message::success();
545 } else {
546 if ($import_notice) {
547 $message = PMA_Message::success(
548 '<em>'
549 . __('Import has been successfully finished, %d queries executed.')
550 . '</em>'
552 $message->addParam($executed_queries);
554 $message->addString($import_notice);
555 if (isset($local_import_file)) {
556 $message->addString('(' . $local_import_file . ')');
557 } else {
558 $message->addString('(' . $_FILES['import_file']['name'] . ')');
560 } else {
561 $message = PMA_Message::success(
562 __('Import has been successfully finished, %d queries executed.')
564 $message->addParam($executed_queries);
565 if (isset($local_import_file)) {
566 $message->addString('(' . $local_import_file . ')');
567 } else {
568 $message->addString('(' . $_FILES['import_file']['name'] . ')');
574 // Did we hit timeout? Tell it user.
575 if ($timeout_passed) {
576 $message = PMA_Message::error(
577 __('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.')
579 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
580 $message->addString(
581 __('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.')
586 // if there is any message, copy it into $_SESSION as well,
587 // so we can obtain it by AJAX call
588 if (isset($message)) {
589 $_SESSION['Import_message']['message'] = $message->getDisplay();
591 // Parse and analyze the query, for correct db and table name
592 // in case of a query typed in the query window
593 // (but if the query is too large, in case of an imported file, the parser
594 // can choke on it so avoid parsing)
595 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
596 include_once 'libraries/parse_analyze.inc.php';
599 // There was an error?
600 if (isset($my_die)) {
601 foreach ($my_die as $key => $die) {
602 PMA_Util::mysqlDie(
603 $die['error'], $die['sql'], '', $err_url, $error
608 if ($go_sql) {
609 // parse sql query
610 include_once 'libraries/parse_analyze.inc.php';
612 PMA_executeQueryAndSendQueryResponse(
613 $analyzed_sql_results, false, $db, $table, null, null, null, false, null,
614 null, null, null, $goto, $pmaThemeImage, null, null, null, $sql_query,
615 null, null
617 } else if ($result) {
618 $response = PMA_Response::getInstance();
619 $response->isSuccess(true);
620 $response->addJSON('message', PMA_Message::success($msg));
621 $response->addJSON(
622 'sql_query',
623 PMA_Util::getMessage($msg, $sql_query, 'success')
625 } else if ($result == false) {
626 $response = PMA_Response::getInstance();
627 $response->isSuccess(false);
628 $response->addJSON('message', PMA_Message::error($msg));
629 } else {
630 $active_page = $goto;
631 include '' . $goto;