Translated using Weblate (Thai)
[phpmyadmin.git] / import.php
blob1de72fe5b4cbdd818dfe885b15f43741df8fdc09
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/display_import_functions.lib.php';
15 if (isset($_REQUEST['show_as_php'])) {
16 $GLOBALS['show_as_php'] = $_REQUEST['show_as_php'];
19 /**
20 * Sets globals from $_POST
22 $post_params = array(
23 'action_bookmark',
24 'allow_interrupt',
25 'bkm_label',
26 'bookmark_variable',
27 'charset_of_file',
28 'format',
29 'id_bookmark',
30 'import_type',
31 'is_js_confirmed',
32 'MAX_FILE_SIZE',
33 'message_to_show',
34 'noplugin',
35 'skip_queries',
36 'local_import_file'
39 foreach ($post_params as $one_post_param) {
40 if (isset($_POST[$one_post_param])) {
41 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
45 // reset import messages for ajax request
46 $_SESSION['Import_message']['message'] = null;
47 $_SESSION['Import_message']['go_back_url'] = null;
48 // default values
49 $GLOBALS['reload'] = false;
51 // Use to identify curren cycle is executing
52 // a multiquery statement or stored routine
53 if (!isset($_SESSION['is_multi_query'])) {
54 $_SESSION['is_multi_query'] = false;
57 // Are we just executing plain query or sql file?
58 // (eg. non import, but query box/window run)
59 if (! empty($sql_query)) {
60 // run SQL query
61 $import_text = $sql_query;
62 $import_type = 'query';
63 $format = 'sql';
65 // refresh navigation and main panels
66 if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
67 $GLOBALS['reload'] = true;
70 // refresh navigation panel only
71 if (preg_match('/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
72 $ajax_reload['reload'] = true;
75 // do a dynamic reload if table is RENAMED
76 // (by sending the instruction to the AJAX response handler)
77 if (preg_match('/^RENAME\s+TABLE\s+(.*?)\s+TO\s+(.*?)($|;|\s)/i', $sql_query, $rename_table_names)) {
78 $ajax_reload['table_name'] = PMA_Util::unQuote($rename_table_names[2]);
79 $ajax_reload['reload'] = true;
82 $sql_query = '';
83 } elseif (! empty($sql_localfile)) {
84 // run SQL file on server
85 $local_import_file = $sql_localfile;
86 $import_type = 'queryfile';
87 $format = 'sql';
88 unset($sql_localfile);
89 } elseif (! empty($sql_file)) {
90 // run uploaded SQL file
91 $import_file = $sql_file;
92 $import_type = 'queryfile';
93 $format = 'sql';
94 unset($sql_file);
95 } elseif (! empty($id_bookmark)) {
96 // run bookmark
97 $import_type = 'query';
98 $format = 'sql';
101 // If we didn't get any parameters, either user called this directly, or
102 // upload limit has been reached, let's assume the second possibility.
104 if ($_POST == array() && $_GET == array()) {
105 $message = PMA_Message::error(
106 __('You probably tried to upload a file that is too large. Please refer to %sdocumentation%s for a workaround for this limit.')
108 $message->addParam('[doc@faq1-16]');
109 $message->addParam('[/doc]');
111 // so we can obtain the message
112 $_SESSION['Import_message']['message'] = $message->getDisplay();
113 $_SESSION['Import_message']['go_back_url'] = $goto;
115 $message->display();
116 exit; // the footer is displayed automatically
120 * Sets globals from $_POST patterns, for import plugins
121 * We only need to load the selected plugin
124 $post_patterns = array(
125 '/^force_file_/',
126 '/^'. $format . '_/'
128 foreach (array_keys($_POST) as $post_key) {
129 foreach ($post_patterns as $one_post_pattern) {
130 if (preg_match($one_post_pattern, $post_key)) {
131 $GLOBALS[$post_key] = $_POST[$post_key];
136 // Check needed parameters
137 PMA_Util::checkParameters(array('import_type', 'format'));
139 // We don't want anything special in format
140 $format = PMA_securePath($format);
142 // Import functions
143 require_once 'libraries/import.lib.php';
145 // Create error and goto url
146 if ($import_type == 'table') {
147 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
148 $_SESSION['Import_message']['go_back_url'] = $err_url;
149 $goto = 'tbl_import.php';
150 } elseif ($import_type == 'database') {
151 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
152 $_SESSION['Import_message']['go_back_url'] = $err_url;
153 $goto = 'db_import.php';
154 } elseif ($import_type == 'server') {
155 $err_url = 'server_import.php?' . PMA_generate_common_url();
156 $_SESSION['Import_message']['go_back_url'] = $err_url;
157 $goto = 'server_import.php';
158 } else {
159 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
160 if (strlen($table) && strlen($db)) {
161 $goto = 'tbl_structure.php';
162 } elseif (strlen($db)) {
163 $goto = 'db_structure.php';
164 } else {
165 $goto = 'server_sql.php';
168 if (strlen($table) && strlen($db)) {
169 $common = PMA_generate_common_url($db, $table);
170 } elseif (strlen($db)) {
171 $common = PMA_generate_common_url($db);
172 } else {
173 $common = PMA_generate_common_url();
175 $err_url = $goto . '?' . $common
176 . (preg_match('@^tbl_[a-z]*\.php$@', $goto)
177 ? '&amp;table=' . htmlspecialchars($table)
178 : '');
179 $_SESSION['Import_message']['go_back_url'] = $err_url;
183 if (strlen($db)) {
184 PMA_DBI_select_db($db);
187 @set_time_limit($cfg['ExecTimeLimit']);
188 if (! empty($cfg['MemoryLimit'])) {
189 @ini_set('memory_limit', $cfg['MemoryLimit']);
192 $timestamp = time();
193 if (isset($allow_interrupt)) {
194 $maximum_time = ini_get('max_execution_time');
195 } else {
196 $maximum_time = 0;
199 // set default values
200 $timeout_passed = false;
201 $error = false;
202 $read_multiply = 1;
203 $finished = false;
204 $offset = 0;
205 $max_sql_len = 0;
206 $file_to_unlink = '';
207 $sql_query = '';
208 $sql_query_disabled = false;
209 $go_sql = false;
210 $executed_queries = 0;
211 $run_query = true;
212 $charset_conversion = false;
213 $reset_charset = false;
214 $bookmark_created = false;
216 // Bookmark Support: get a query back from bookmark if required
217 if (! empty($id_bookmark)) {
218 $id_bookmark = (int)$id_bookmark;
219 include_once 'libraries/bookmark.lib.php';
220 switch ($action_bookmark) {
221 case 0: // bookmarked query that have to be run
222 $import_text = PMA_Bookmark_get(
223 $db,
224 $id_bookmark,
225 'id',
226 isset($action_bookmark_all)
228 if (isset($bookmark_variable) && ! empty($bookmark_variable)) {
229 $import_text = preg_replace(
230 '|/\*(.*)\[VARIABLE\](.*)\*/|imsU',
231 '${1}' . PMA_Util::sqlAddSlashes($bookmark_variable) . '${2}',
232 $import_text
236 // refresh navigation and main panels
237 if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
238 $GLOBALS['reload'] = true;
241 // refresh navigation panel only
242 if (preg_match('/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
243 $ajax_reload['reload'] = true;
245 break;
246 case 1: // bookmarked query that have to be displayed
247 $import_text = PMA_Bookmark_get($db, $id_bookmark);
248 if ($GLOBALS['is_ajax_request'] == true) {
249 $message = PMA_Message::success(__('Showing bookmark'));
250 $response = PMA_Response::getInstance();
251 $response->isSuccess($message->isSuccess());
252 $response->addJSON('message', $message);
253 $response->addJSON('sql_query', $import_text);
254 $response->addJSON('action_bookmark', $action_bookmark);
255 exit;
256 } else {
257 $run_query = false;
259 break;
260 case 2: // bookmarked query that have to be deleted
261 $import_text = PMA_Bookmark_get($db, $id_bookmark);
262 PMA_Bookmark_delete($db, $id_bookmark);
263 if ($GLOBALS['is_ajax_request'] == true) {
264 $message = PMA_Message::success(__('The bookmark has been deleted.'));
265 $response = PMA_Response::getInstance();
266 $response->isSuccess($message->isSuccess());
267 $response->addJSON('message', $message);
268 $response->addJSON('action_bookmark', $action_bookmark);
269 $response->addJSON('id_bookmark', $id_bookmark);
270 exit;
271 } else {
272 $run_query = false;
273 $error = true; // this is kind of hack to skip processing the query
275 break;
277 } // end bookmarks reading
279 // Do no run query if we show PHP code
280 if (isset($GLOBALS['show_as_php'])) {
281 $run_query = false;
282 $go_sql = true;
285 // We can not read all at once, otherwise we can run out of memory
286 $memory_limit = trim(@ini_get('memory_limit'));
287 // 2 MB as default
288 if (empty($memory_limit)) {
289 $memory_limit = 2 * 1024 * 1024;
291 // In case no memory limit we work on 10MB chunks
292 if ($memory_limit == -1) {
293 $memory_limit = 10 * 1024 * 1024;
296 // Calculate value of the limit
297 if (strtolower(substr($memory_limit, -1)) == 'm') {
298 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
299 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
300 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
301 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
302 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
303 } else {
304 $memory_limit = (int)$memory_limit;
307 // Just to be sure, there might be lot of memory needed for uncompression
308 $read_limit = $memory_limit / 8;
310 // handle filenames
311 if (isset($_FILES['import_file'])) {
312 $import_file = $_FILES['import_file']['tmp_name'];
314 if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) {
316 // sanitize $local_import_file as it comes from a POST
317 $local_import_file = PMA_securePath($local_import_file);
319 $import_file = PMA_Util::userDir($cfg['UploadDir'])
320 . $local_import_file;
322 } elseif (empty($import_file) || ! is_uploaded_file($import_file)) {
323 $import_file = 'none';
326 // Do we have file to import?
328 if ($import_file != 'none' && ! $error) {
329 // work around open_basedir and other limitations
330 $open_basedir = @ini_get('open_basedir');
332 // If we are on a server with open_basedir, we must move the file
333 // before opening it. The doc explains how to create the "./tmp"
334 // directory
336 if (! empty($open_basedir)) {
338 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/');
340 if (is_writable($tmp_subdir)) {
343 $import_file_new = $tmp_subdir . basename($import_file) . uniqid();
344 if (move_uploaded_file($import_file, $import_file_new)) {
345 $import_file = $import_file_new;
346 $file_to_unlink = $import_file_new;
349 $size = filesize($import_file);
354 * Handle file compression
355 * @todo duplicate code exists in File.class.php
357 $compression = PMA_detectCompression($import_file);
358 if ($compression === false) {
359 $message = PMA_Message::error(__('File could not be read'));
360 $error = true;
361 } else {
362 switch ($compression) {
363 case 'application/bzip2':
364 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
365 $import_handle = @bzopen($import_file, 'r');
366 } else {
367 $message = PMA_Message::error(
368 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
370 $message->addParam($compression);
371 $error = true;
373 break;
374 case 'application/gzip':
375 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
376 $import_handle = @gzopen($import_file, 'r');
377 } else {
378 $message = PMA_Message::error(
379 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
381 $message->addParam($compression);
382 $error = true;
384 break;
385 case 'application/zip':
386 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
388 * Load interface for zip extension.
390 include_once 'libraries/zip_extension.lib.php';
391 $result = PMA_getZipContents($import_file);
392 if (! empty($result['error'])) {
393 $message = PMA_Message::rawError($result['error']);
394 $error = true;
395 } else {
396 $import_text = $result['data'];
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 'none':
407 $import_handle = @fopen($import_file, 'r');
408 break;
409 default:
410 $message = PMA_Message::error(
411 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
413 $message->addParam($compression);
414 $error = true;
415 break;
418 // use isset() because zip compression type does not use a handle
419 if (! $error && isset($import_handle) && $import_handle === false) {
420 $message = PMA_Message::error(__('File could not be read'));
421 $error = true;
423 } elseif (! $error) {
424 if (! isset($import_text) || empty($import_text)) {
425 $message = PMA_Message::error(
426 __('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].')
428 $error = true;
432 // so we can obtain the message
433 //$_SESSION['Import_message'] = $message->getDisplay();
435 // Convert the file's charset if necessary
436 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
437 if ($charset_of_file != 'utf-8') {
438 $charset_conversion = true;
440 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
441 if (PMA_DRIZZLE) {
442 // Drizzle doesn't support other character sets,
443 // so we can't fallback to SET NAMES - throw an error
444 $error = true;
445 $message = PMA_Message::error(
446 __('Cannot convert file\'s character set without character set conversion library')
448 } else {
449 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
450 // We can not show query in this case, it is in different charset
451 $sql_query_disabled = true;
452 $reset_charset = true;
456 // Something to skip?
457 if (! $error && isset($skip)) {
458 $original_skip = $skip;
459 while ($skip > 0) {
460 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
461 // Disable read progresivity, otherwise we eat all memory!
462 $read_multiply = 1;
463 $skip -= $read_limit;
465 unset($skip);
468 // This array contain the data like numberof valid sql queries in the statement
469 // and complete valid sql statement (which affected for rows)
470 $sql_data = array('valid_sql' => array(), 'valid_queries' => 0);
472 if (! $error) {
473 // Check for file existance
474 include_once "libraries/plugin_interface.lib.php";
475 $import_plugin = PMA_getPlugin(
476 "import",
477 $format,
478 'libraries/plugins/import/',
479 $import_type
481 if ($import_plugin == null) {
482 $error = true;
483 $message = PMA_Message::error(
484 __('Could not load import plugins, please check your installation!')
486 } else {
487 // Do the real import
488 $import_plugin->doImport($sql_data);
492 if (! $error && false !== $import_handle && null !== $import_handle) {
493 fclose($import_handle);
496 // Cleanup temporary file
497 if ($file_to_unlink != '') {
498 unlink($file_to_unlink);
501 // Reset charset back, if we did some changes
502 if ($reset_charset) {
503 PMA_DBI_query('SET CHARACTER SET utf8');
504 PMA_DBI_query(
505 'SET SESSION collation_connection =\'' . $collation_connection . '\''
509 // Show correct message
510 if (! empty($id_bookmark) && $action_bookmark == 2) {
511 $message = PMA_Message::success(__('The bookmark has been deleted.'));
512 $display_query = $import_text;
513 $error = false; // unset error marker, it was used just to skip processing
514 } elseif (! empty($id_bookmark) && $action_bookmark == 1) {
515 $message = PMA_Message::notice(__('Showing bookmark'));
516 } elseif ($bookmark_created) {
517 $special_message = '[br]' . sprintf(
518 __('Bookmark %s created'),
519 htmlspecialchars($bkm_label)
521 } elseif ($finished && ! $error) {
522 if ($import_type == 'query') {
523 $message = PMA_Message::success();
524 } else {
525 if ($import_notice) {
526 $message = PMA_Message::success(
527 '<em>' . __('Import has been successfully finished, %d queries executed.') . '</em>'
529 $message->addParam($executed_queries);
531 $message->addString($import_notice);
532 if (isset($local_import_file)) {
533 $message->addString('(' . $local_import_file . ')');
534 } else {
535 $message->addString('(' . $_FILES['import_file']['name'] . ')');
537 } else {
538 $message = PMA_Message::success(
539 __('Import has been successfully finished, %d queries executed.')
541 $message->addParam($executed_queries);
542 if (isset($local_import_file)) {
543 $message->addString('(' . $local_import_file . ')');
544 } else {
545 $message->addString('(' . $_FILES['import_file']['name'] . ')');
551 // Did we hit timeout? Tell it user.
552 if ($timeout_passed) {
553 $message = PMA_Message::error(
554 __('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.')
556 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
557 $message->addString(
558 __('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.')
563 // if there is any message, copy it into $_SESSION as well,
564 // so we can obtain it by AJAX call
565 if (isset($message)) {
566 $_SESSION['Import_message']['message'] = $message->getDisplay();
568 // Parse and analyze the query, for correct db and table name
569 // in case of a query typed in the query window
570 // (but if the query is too large, in case of an imported file, the parser
571 // can choke on it so avoid parsing)
572 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
573 include_once 'libraries/parse_analyze.lib.php';
576 // There was an error?
577 if (isset($my_die)) {
578 foreach ($my_die AS $key => $die) {
579 PMA_Util::mysqlDie(
580 $die['error'], $die['sql'], '', $err_url, $error
585 // we want to see the results of the last query that returned at least a row
586 if (! empty($last_query_with_results)) {
587 // but we want to show intermediate results too
588 $disp_query = $sql_query;
589 $disp_message = __('Your SQL query has been executed successfully');
590 $sql_query = $last_query_with_results;
591 $go_sql = true;
594 if ($go_sql) {
595 include 'sql.php';
596 } else {
597 $active_page = $goto;
598 include '' . $goto;