Fixed descriptions of geometry types - these are column types, not OpenGIS objects
[phpmyadmin.git] / import.php
blobea3db73bd2ca1c1f7ea3a256de0ed63863b61ed4
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'
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;
47 // default values
48 $GLOBALS['reload'] = false;
50 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
51 if (! empty($sql_query)) {
52 // run SQL query
53 $import_text = $sql_query;
54 $import_type = 'query';
55 $format = 'sql';
57 // refresh left frame on changes in table or db structure
58 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
59 $GLOBALS['reload'] = true;
62 $sql_query = '';
63 } elseif (! empty($sql_localfile)) {
64 // run SQL file on server
65 $local_import_file = $sql_localfile;
66 $import_type = 'queryfile';
67 $format = 'sql';
68 unset($sql_localfile);
69 } elseif (! empty($sql_file)) {
70 // run uploaded SQL file
71 $import_file = $sql_file;
72 $import_type = 'queryfile';
73 $format = 'sql';
74 unset($sql_file);
75 } elseif (! empty($id_bookmark)) {
76 // run bookmark
77 $import_type = 'query';
78 $format = 'sql';
81 // If we didn't get any parameters, either user called this directly, or
82 // upload limit has been reached, let's assume the second possibility.
84 if ($_POST == array() && $_GET == array()) {
85 include_once 'libraries/header.inc.php';
86 $message = PMA_Message::error(__('You probably tried to upload too large file. Please refer to %sdocumentation%s for ways to workaround this limit.'));
87 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
88 $message->addParam('[/a]');
90 // so we can obtain the message
91 $_SESSION['Import_message']['message'] = $message->getDisplay();
92 $_SESSION['Import_message']['go_back_url'] = $goto;
94 $message->display();
95 include 'libraries/footer.inc.php';
98 /**
99 * Sets globals from $_POST patterns, for import plugins
100 * We only need to load the selected plugin
103 $post_patterns = array(
104 '/^force_file_/',
105 '/^'. $format . '_/'
107 foreach (array_keys($_POST) as $post_key) {
108 foreach ($post_patterns as $one_post_pattern) {
109 if (preg_match($one_post_pattern, $post_key)) {
110 $GLOBALS[$post_key] = $_POST[$post_key];
115 // Check needed parameters
116 PMA_checkParameters(array('import_type', 'format'));
118 // We don't want anything special in format
119 $format = PMA_securePath($format);
121 // Import functions
122 require_once 'libraries/import.lib.php';
124 // Create error and goto url
125 if ($import_type == 'table') {
126 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
127 $_SESSION['Import_message']['go_back_url'] = $err_url;
128 $goto = 'tbl_import.php';
129 } elseif ($import_type == 'database') {
130 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
131 $_SESSION['Import_message']['go_back_url'] = $err_url;
132 $goto = 'db_import.php';
133 } elseif ($import_type == 'server') {
134 $err_url = 'server_import.php?' . PMA_generate_common_url();
135 $_SESSION['Import_message']['go_back_url'] = $err_url;
136 $goto = 'server_import.php';
137 } else {
138 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
139 if (strlen($table) && strlen($db)) {
140 $goto = 'tbl_structure.php';
141 } elseif (strlen($db)) {
142 $goto = 'db_structure.php';
143 } else {
144 $goto = 'server_sql.php';
147 if (strlen($table) && strlen($db)) {
148 $common = PMA_generate_common_url($db, $table);
149 } elseif (strlen($db)) {
150 $common = PMA_generate_common_url($db);
151 } else {
152 $common = PMA_generate_common_url();
154 $err_url = $goto
155 . '?' . $common
156 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . htmlspecialchars($table) : '');
157 $_SESSION['Import_message']['go_back_url'] = $err_url;
161 if (strlen($db)) {
162 PMA_DBI_select_db($db);
165 @set_time_limit($cfg['ExecTimeLimit']);
166 if (! empty($cfg['MemoryLimit'])) {
167 @ini_set('memory_limit', $cfg['MemoryLimit']);
170 $timestamp = time();
171 if (isset($allow_interrupt)) {
172 $maximum_time = ini_get('max_execution_time');
173 } else {
174 $maximum_time = 0;
177 // set default values
178 $timeout_passed = false;
179 $error = false;
180 $read_multiply = 1;
181 $finished = false;
182 $offset = 0;
183 $max_sql_len = 0;
184 $file_to_unlink = '';
185 $sql_query = '';
186 $sql_query_disabled = false;
187 $go_sql = false;
188 $executed_queries = 0;
189 $run_query = true;
190 $charset_conversion = false;
191 $reset_charset = false;
192 $bookmark_created = false;
194 // Bookmark Support: get a query back from bookmark if required
195 if (! empty($id_bookmark)) {
196 $id_bookmark = (int)$id_bookmark;
197 include_once 'libraries/bookmark.lib.php';
198 switch ($action_bookmark) {
199 case 0: // bookmarked query that have to be run
200 $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
201 if (isset($bookmark_variable) && ! empty($bookmark_variable)) {
202 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddSlashes($bookmark_variable) . '${2}', $import_text);
205 // refresh left frame on changes in table or db structure
206 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
207 $GLOBALS['reload'] = true;
210 break;
211 case 1: // bookmarked query that have to be displayed
212 $import_text = PMA_Bookmark_get($db, $id_bookmark);
213 if ($GLOBALS['is_ajax_request'] == true) {
214 $extra_data['sql_query'] = $import_text;
215 $extra_data['action_bookmark'] = $action_bookmark;
216 $message = PMA_Message::success(__('Showing bookmark'));
217 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
218 } else {
219 $run_query = false;
221 break;
222 case 2: // bookmarked query that have to be deleted
223 $import_text = PMA_Bookmark_get($db, $id_bookmark);
224 PMA_Bookmark_delete($db, $id_bookmark);
225 if ($GLOBALS['is_ajax_request'] == true) {
226 $message = PMA_Message::success(__('The bookmark has been deleted.'));
227 $extra_data['action_bookmark'] = $action_bookmark;
228 $extra_data['id_bookmark'] = $id_bookmark;
229 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
230 } else {
231 $run_query = false;
232 $error = true; // this is kind of hack to skip processing the query
234 break;
236 } // end bookmarks reading
238 // Do no run query if we show PHP code
239 if (isset($GLOBALS['show_as_php'])) {
240 $run_query = false;
241 $go_sql = true;
244 // We can not read all at once, otherwise we can run out of memory
245 $memory_limit = trim(@ini_get('memory_limit'));
246 // 2 MB as default
247 if (empty($memory_limit)) {
248 $memory_limit = 2 * 1024 * 1024;
250 // In case no memory limit we work on 10MB chunks
251 if ($memory_limit == -1) {
252 $memory_limit = 10 * 1024 * 1024;
255 // Calculate value of the limit
256 if (strtolower(substr($memory_limit, -1)) == 'm') {
257 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
258 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
259 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
260 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
261 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
262 } else {
263 $memory_limit = (int)$memory_limit;
266 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
268 // handle filenames
269 if (isset($_FILES['import_file'])) {
270 $import_file = $_FILES['import_file']['tmp_name'];
272 if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) {
274 // sanitize $local_import_file as it comes from a POST
275 $local_import_file = PMA_securePath($local_import_file);
277 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
278 } elseif (empty($import_file) || ! is_uploaded_file($import_file)) {
279 $import_file = 'none';
282 // Do we have file to import?
284 if ($import_file != 'none' && ! $error) {
285 // work around open_basedir and other limitations
286 $open_basedir = @ini_get('open_basedir');
288 // If we are on a server with open_basedir, we must move the file
289 // before opening it. The doc explains how to create the "./tmp"
290 // directory
292 if (! empty($open_basedir)) {
294 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/');
296 if (is_writable($tmp_subdir)) {
299 $import_file_new = $tmp_subdir . basename($import_file) . uniqid();
300 if (move_uploaded_file($import_file, $import_file_new)) {
301 $import_file = $import_file_new;
302 $file_to_unlink = $import_file_new;
305 $size = filesize($import_file);
310 * Handle file compression
311 * @todo duplicate code exists in File.class.php
313 $compression = PMA_detectCompression($import_file);
314 if ($compression === false) {
315 $message = PMA_Message::error(__('File could not be read'));
316 $error = true;
317 } else {
318 switch ($compression) {
319 case 'application/bzip2':
320 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
321 $import_handle = @bzopen($import_file, 'r');
322 } else {
323 $message = PMA_Message::error(__('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.'));
324 $message->addParam($compression);
325 $error = true;
327 break;
328 case 'application/gzip':
329 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
330 $import_handle = @gzopen($import_file, 'r');
331 } else {
332 $message = PMA_Message::error(__('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.'));
333 $message->addParam($compression);
334 $error = true;
336 break;
337 case 'application/zip':
338 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
340 * Load interface for zip extension.
342 include_once 'libraries/zip_extension.lib.php';
343 $result = PMA_getZipContents($import_file);
344 if (! empty($result['error'])) {
345 $message = PMA_Message::rawError($result['error']);
346 $error = true;
347 } else {
348 $import_text = $result['data'];
350 } else {
351 $message = PMA_Message::error(__('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.'));
352 $message->addParam($compression);
353 $error = true;
355 break;
356 case 'none':
357 $import_handle = @fopen($import_file, 'r');
358 break;
359 default:
360 $message = PMA_Message::error(__('You attempted to load file with unsupported compression (%s). Either support for it is not implemented or disabled by your configuration.'));
361 $message->addParam($compression);
362 $error = true;
363 break;
366 // use isset() because zip compression type does not use a handle
367 if (! $error && isset($import_handle) && $import_handle === false) {
368 $message = PMA_Message::error(__('File could not be read'));
369 $error = true;
371 } elseif (! $error) {
372 if (! isset($import_text) || empty($import_text)) {
373 $message = PMA_Message::error(__('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 [a@./Documentation.html#faq1_16@Documentation]FAQ 1.16[/a].'));
374 $error = true;
378 // so we can obtain the message
379 //$_SESSION['Import_message'] = $message->getDisplay();
381 // Convert the file's charset if necessary
382 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
383 if ($charset_of_file != 'utf-8') {
384 $charset_conversion = true;
386 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
387 if (PMA_DRIZZLE) {
388 // Drizzle doesn't support other character sets, so we can't fallback to SET NAMES - throw an error
389 $error = true;
390 $message = PMA_Message::error(__('Cannot convert file\'s character set without character set conversion library'));
391 } else {
392 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
393 // We can not show query in this case, it is in different charset
394 $sql_query_disabled = true;
395 $reset_charset = true;
399 // Something to skip?
400 if (! $error && isset($skip)) {
401 $original_skip = $skip;
402 while ($skip > 0) {
403 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
404 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
405 $skip -= $read_limit;
407 unset($skip);
410 if (! $error) {
411 // Check for file existance
412 if (!file_exists('libraries/import/' . $format . '.php')) {
413 $error = true;
414 $message = PMA_Message::error(__('Could not load import plugins, please check your installation!'));
415 } else {
416 // Do the real import
417 $plugin_param = $import_type;
418 include 'libraries/import/' . $format . '.php';
422 if (! $error && false !== $import_handle && null !== $import_handle) {
423 fclose($import_handle);
426 // Cleanup temporary file
427 if ($file_to_unlink != '') {
428 unlink($file_to_unlink);
431 // Reset charset back, if we did some changes
432 if ($reset_charset) {
433 PMA_DBI_query('SET CHARACTER SET utf8');
434 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
437 // Show correct message
438 if (! empty($id_bookmark) && $action_bookmark == 2) {
439 $message = PMA_Message::success(__('The bookmark has been deleted.'));
440 $display_query = $import_text;
441 $error = false; // unset error marker, it was used just to skip processing
442 } elseif (! empty($id_bookmark) && $action_bookmark == 1) {
443 $message = PMA_Message::notice(__('Showing bookmark'));
444 } elseif ($bookmark_created) {
445 $special_message = '[br]' . sprintf(__('Bookmark %s created'), htmlspecialchars($bkm_label));
446 } elseif ($finished && ! $error) {
447 if ($import_type == 'query') {
448 $message = PMA_Message::success();
449 } else {
450 if ($import_notice) {
451 $message = PMA_Message::success('<em>'.__('Import has been successfully finished, %d queries executed.').'</em>');
452 $message->addParam($executed_queries);
454 $message->addString($import_notice);
455 $message->addString('(' . $_FILES['import_file']['name'] . ')');
456 } else {
457 $message = PMA_Message::success(__('Import has been successfully finished, %d queries executed.'));
458 $message->addParam($executed_queries);
459 $message->addString('(' . $_FILES['import_file']['name'] . ')');
464 // Did we hit timeout? Tell it user.
465 if ($timeout_passed) {
466 $message = PMA_Message::error(__('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.'));
467 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
468 $message->addString(__('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.'));
472 // if there is any message, copy it into $_SESSION as well, so we can obtain it by AJAX call
473 if (isset($message)) {
474 $_SESSION['Import_message']['message'] = $message->getDisplay();
475 // $_SESSION['Import_message']['go_back_url'] = $goto.'?'. PMA_generate_common_url();
477 // Parse and analyze the query, for correct db and table name
478 // in case of a query typed in the query window
479 // (but if the query is too large, in case of an imported file, the parser
480 // can choke on it so avoid parsing)
481 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
482 include_once 'libraries/parse_analyze.lib.php';
485 // There was an error?
486 if (isset($my_die)) {
487 foreach ($my_die AS $key => $die) {
488 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
492 // we want to see the results of the last query that returned at least a row
493 if (! empty($last_query_with_results)) {
494 // but we want to show intermediate results too
495 $disp_query = $sql_query;
496 $disp_message = __('Your SQL query has been executed successfully');
497 $sql_query = $last_query_with_results;
498 $go_sql = true;
501 if ($go_sql) {
502 include 'sql.php';
503 } else {
504 $active_page = $goto;
505 include '' . $goto;
507 exit();