added japanese language
[openemr.git] / phpmyadmin / import.php
blobfd81ae014636f25f2f3c4cda59c79e2c8fb9ccaf
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($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.
370 if (! empty($open_basedir)) {
373 * @todo make use of the config's temp dir with fallback to the
374 * system's tmp dir
376 $tmp_subdir = ini_get('upload_tmp_dir');
377 if (empty($tmp_subdir)) {
378 $tmp_subdir = sys_get_temp_dir();
381 if (is_writable($tmp_subdir)) {
383 $import_file_new = $tmp_subdir . basename($import_file) . uniqid();
384 if (move_uploaded_file($import_file, $import_file_new)) {
385 $import_file = $import_file_new;
386 $file_to_unlink = $import_file_new;
389 $size = filesize($import_file);
390 } else {
392 // If the php.ini is misconfigured (eg. there is no /tmp access defined
393 // with open_basedir), $tmp_subdir won't be writable and the user gets
394 // a 'File could not be read!' error (at PMA_detectCompression), which
395 // is not too meaningful. Show a meaningful error message to the user
396 // instead.
398 $message = PMA_Message::error(
399 __('Uploaded file cannot be moved, because the server has open_basedir enabled without access to the %s directory (for temporary files).')
401 $message->addParam($tmp_subdir);
402 PMA_stopImport($message);
407 * Handle file compression
408 * @todo duplicate code exists in File.class.php
410 $compression = PMA_detectCompression($import_file);
411 if ($compression === false) {
412 $message = PMA_Message::error(__('File could not be read!'));
413 PMA_stopImport($message);
414 } else {
415 switch ($compression) {
416 case 'application/bzip2':
417 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
418 $import_handle = @bzopen($import_file, 'r');
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 PMA_stopImport($message);
426 break;
427 case 'application/gzip':
428 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
429 $import_handle = @gzopen($import_file, 'r');
430 } else {
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 PMA_stopImport($message);
437 break;
438 case 'application/zip':
439 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
441 * Load interface for zip extension.
443 include_once 'libraries/zip_extension.lib.php';
444 $zipResult = PMA_getZipContents($import_file);
445 if (! empty($zipResult['error'])) {
446 $message = PMA_Message::rawError($zipResult['error']);
447 PMA_stopImport($message);
448 } else {
449 $import_text = $zipResult['data'];
451 } else {
452 $message = PMA_Message::error(
453 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
455 $message->addParam($compression);
456 PMA_stopImport($message);
458 break;
459 case 'none':
460 $import_handle = @fopen($import_file, 'r');
461 break;
462 default:
463 $message = PMA_Message::error(
464 __('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.')
466 $message->addParam($compression);
467 PMA_stopImport($message);
468 break;
471 // use isset() because zip compression type does not use a handle
472 if (! $error && isset($import_handle) && $import_handle === false) {
473 $message = PMA_Message::error(__('File could not be read!'));
474 PMA_stopImport($message);
476 } elseif (! $error) {
477 if (! isset($import_text) || empty($import_text)) {
478 $message = PMA_Message::error(
479 __('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].')
481 PMA_stopImport($message);
485 // so we can obtain the message
486 //$_SESSION['Import_message'] = $message->getDisplay();
488 // Convert the file's charset if necessary
489 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
490 if ($charset_of_file != 'utf-8') {
491 $charset_conversion = true;
493 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
494 if (PMA_DRIZZLE) {
495 // Drizzle doesn't support other character sets,
496 // so we can't fallback to SET NAMES - throw an error
497 $message = PMA_Message::error(
498 __('Cannot convert file\'s character set without character set conversion library!')
500 PMA_stopImport($message);
501 } else {
502 $GLOBALS['dbi']->query('SET NAMES \'' . $charset_of_file . '\'');
503 // We can not show query in this case, it is in different charset
504 $sql_query_disabled = true;
505 $reset_charset = true;
509 // Something to skip?
510 if (! $error && isset($skip)) {
511 $original_skip = $skip;
512 while ($skip > 0) {
513 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
514 // Disable read progressivity, otherwise we eat all memory!
515 $read_multiply = 1;
516 $skip -= $read_limit;
518 unset($skip);
521 // This array contain the data like numberof valid sql queries in the statement
522 // and complete valid sql statement (which affected for rows)
523 $sql_data = array('valid_sql' => array(), 'valid_queries' => 0);
525 if (! $error) {
526 // Check for file existance
527 include_once "libraries/plugin_interface.lib.php";
528 $import_plugin = PMA_getPlugin(
529 "import",
530 $format,
531 'libraries/plugins/import/',
532 $import_type
534 if ($import_plugin == null) {
535 $message = PMA_Message::error(
536 __('Could not load import plugins, please check your installation!')
538 PMA_stopImport($message);
539 } else {
540 // Do the real import
541 $import_plugin->doImport($sql_data);
545 if (false !== $import_handle && null !== $import_handle) {
546 fclose($import_handle);
549 // Cleanup temporary file
550 if ($file_to_unlink != '') {
551 unlink($file_to_unlink);
554 // Reset charset back, if we did some changes
555 if ($reset_charset) {
556 $GLOBALS['dbi']->query('SET CHARACTER SET utf8');
557 $GLOBALS['dbi']->query(
558 'SET SESSION collation_connection =\'' . $collation_connection . '\''
562 // Show correct message
563 if (! empty($id_bookmark) && $action_bookmark == 2) {
564 $message = PMA_Message::success(__('The bookmark has been deleted.'));
565 $display_query = $import_text;
566 $error = false; // unset error marker, it was used just to skip processing
567 } elseif (! empty($id_bookmark) && $action_bookmark == 1) {
568 $message = PMA_Message::notice(__('Showing bookmark'));
569 } elseif ($bookmark_created) {
570 $special_message = '[br]' . sprintf(
571 __('Bookmark %s has been created.'),
572 htmlspecialchars($bkm_label)
574 } elseif ($finished && ! $error) {
575 if ($import_type == 'query') {
576 $message = PMA_Message::success();
577 } else {
578 $message = PMA_Message::success(
579 '<em>'
580 . __('Import has been successfully finished, %d queries executed.')
581 . '</em>'
583 $message->addParam($executed_queries);
585 if ($import_notice) {
586 $message->addString($import_notice);
588 if (isset($local_import_file)) {
589 $message->addString('(' . htmlspecialchars($local_import_file) . ')');
590 } else {
591 $message->addString(
592 '(' . htmlspecialchars($_FILES['import_file']['name']) . ')'
598 // Did we hit timeout? Tell it user.
599 if ($timeout_passed) {
600 $message = PMA_Message::error(
601 __('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.')
603 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
604 $message->addString(
605 __('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.')
610 // if there is any message, copy it into $_SESSION as well,
611 // so we can obtain it by AJAX call
612 if (isset($message)) {
613 $_SESSION['Import_message']['message'] = $message->getDisplay();
615 // Parse and analyze the query, for correct db and table name
616 // in case of a query typed in the query window
617 // (but if the query is too large, in case of an imported file, the parser
618 // can choke on it so avoid parsing)
619 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
620 include_once 'libraries/parse_analyze.inc.php';
623 // There was an error?
624 if (isset($my_die)) {
625 foreach ($my_die as $key => $die) {
626 PMA_Util::mysqlDie(
627 $die['error'], $die['sql'], false, $err_url, $error
632 if ($go_sql) {
633 // parse sql query
634 include_once 'libraries/parse_analyze.inc.php';
636 if (isset($ajax_reload) && $ajax_reload['reload'] === true) {
637 $response = PMA_Response::getInstance();
638 $response->addJSON('ajax_reload', $ajax_reload);
640 PMA_executeQueryAndSendQueryResponse(
641 $analyzed_sql_results, false, $db, $table, null, $import_text, null,
642 $analyzed_sql_results['is_affected'], null,
643 null, null, null, $goto, $pmaThemeImage, null, null, null, $sql_query,
644 null, null
646 } else if ($result) {
647 // Save a Bookmark with more than one queries (if Bookmark label given).
648 if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
649 PMA_storeTheQueryAsBookmark(
650 $db, $GLOBALS['cfg']['Bookmark']['user'],
651 $import_text, $_POST['bkm_label'],
652 isset($_POST['bkm_replace']) ? $_POST['bkm_replace'] : null
656 $response = PMA_Response::getInstance();
657 $response->isSuccess(true);
658 $response->addJSON('message', PMA_Message::success($msg));
659 $response->addJSON(
660 'sql_query',
661 PMA_Util::getMessage($msg, $sql_query, 'success')
663 } else if ($result == false) {
664 $response = PMA_Response::getInstance();
665 $response->isSuccess(false);
666 $response->addJSON('message', PMA_Message::error($msg));
667 } else {
668 $active_page = $goto;
669 include '' . $goto;