2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Core script for import, this is just the glue around all other stuff
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 // reset import messages for ajax request
16 $_SESSION['Import_message']['message'] = null;
17 $_SESSION['Import_message']['go_back_url'] = null;
19 $GLOBALS['reload'] = false;
21 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
22 if (!empty($sql_query)) {
24 $import_text = $sql_query;
25 $import_type = 'query';
28 // refresh left frame on changes in table or db structure
29 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
30 $GLOBALS['reload'] = true;
34 } elseif (!empty($sql_localfile)) {
35 // run SQL file on server
36 $local_import_file = $sql_localfile;
37 $import_type = 'queryfile';
39 unset($sql_localfile);
40 } elseif (!empty($sql_file)) {
41 // run uploaded SQL file
42 $import_file = $sql_file;
43 $import_type = 'queryfile';
46 } elseif (!empty($id_bookmark)) {
48 $import_type = 'query';
52 // If we didn't get any parameters, either user called this directly, or
53 // upload limit has been reached, let's assume the second possibility.
55 if ($_POST == array() && $_GET == array()) {
56 include_once './libraries/header.inc.php';
57 $message = PMA_Message
::error(__('You probably tried to upload too large file. Please refer to %sdocumentation%s for ways to workaround this limit.'));
58 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
59 $message->addParam('[/a]');
61 // so we can obtain the message
62 $_SESSION['Import_message']['message'] = $message->getDisplay();
63 $_SESSION['Import_message']['go_back_url'] = $goto;
66 include './libraries/footer.inc.php';
69 // Check needed parameters
70 PMA_checkParameters(array('import_type', 'format'));
72 // We don't want anything special in format
73 $format = PMA_securePath($format);
76 require_once './libraries/import.lib.php';
78 // Create error and goto url
79 if ($import_type == 'table') {
80 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
81 $_SESSION['Import_message']['go_back_url'] = $err_url;
82 $goto = 'tbl_import.php';
83 } elseif ($import_type == 'database') {
84 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
85 $_SESSION['Import_message']['go_back_url'] = $err_url;
86 $goto = 'db_import.php';
87 } elseif ($import_type == 'server') {
88 $err_url = 'server_import.php?' . PMA_generate_common_url();
89 $_SESSION['Import_message']['go_back_url'] = $err_url;
90 $goto = 'server_import.php';
92 if (empty($goto) ||
!preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
93 if (strlen($table) && strlen($db)) {
94 $goto = 'tbl_structure.php';
95 } elseif (strlen($db)) {
96 $goto = 'db_structure.php';
98 $goto = 'server_sql.php';
101 if (strlen($table) && strlen($db)) {
102 $common = PMA_generate_common_url($db, $table);
103 } elseif (strlen($db)) {
104 $common = PMA_generate_common_url($db);
106 $common = PMA_generate_common_url();
110 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ?
'&table=' . htmlspecialchars($table) : '');
111 $_SESSION['Import_message']['go_back_url'] = $err_url;
116 PMA_DBI_select_db($db);
119 @set_time_limit
($cfg['ExecTimeLimit']);
120 if (!empty($cfg['MemoryLimit'])) {
121 @ini_set
('memory_limit', $cfg['MemoryLimit']);
125 if (isset($allow_interrupt)) {
126 $maximum_time = ini_get('max_execution_time');
131 // set default values
132 $timeout_passed = false;
138 $file_to_unlink = '';
140 $sql_query_disabled = false;
142 $executed_queries = 0;
144 $charset_conversion = false;
145 $reset_charset = false;
146 $bookmark_created = false;
148 // Bookmark Support: get a query back from bookmark if required
149 if (!empty($id_bookmark)) {
150 $id_bookmark = (int)$id_bookmark;
151 include_once './libraries/bookmark.lib.php';
152 switch ($action_bookmark) {
153 case 0: // bookmarked query that have to be run
154 $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
155 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
156 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddSlashes($bookmark_variable) . '${2}', $import_text);
159 // refresh left frame on changes in table or db structure
160 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
161 $GLOBALS['reload'] = true;
165 case 1: // bookmarked query that have to be displayed
166 $import_text = PMA_Bookmark_get($db, $id_bookmark);
167 if ($GLOBALS['is_ajax_request'] == true) {
168 $extra_data['sql_query'] = $import_text;
169 $extra_data['action_bookmark'] = $action_bookmark;
170 $message = PMA_Message
::success(__('Showing bookmark'));
171 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
176 case 2: // bookmarked query that have to be deleted
177 $import_text = PMA_Bookmark_get($db, $id_bookmark);
178 PMA_Bookmark_delete($db, $id_bookmark);
179 if ($GLOBALS['is_ajax_request'] == true) {
180 $message = PMA_Message
::success(__('The bookmark has been deleted.'));
181 $extra_data['action_bookmark'] = $action_bookmark;
182 $extra_data['id_bookmark'] = $id_bookmark;
183 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
186 $error = true; // this is kind of hack to skip processing the query
190 } // end bookmarks reading
192 // Do no run query if we show PHP code
193 if (isset($GLOBALS['show_as_php'])) {
198 // Store the query as a bookmark before executing it if bookmarklabel was given
199 if (!empty($bkm_label) && !empty($import_text)) {
200 include_once './libraries/bookmark.lib.php';
203 'user' => $cfg['Bookmark']['user'],
204 'query' => urlencode($import_text),
205 'label' => $bkm_label
208 // Should we replace bookmark?
209 if (isset($bkm_replace)) {
210 $bookmarks = PMA_Bookmark_getList($db);
211 foreach ($bookmarks as $key => $val) {
212 if ($val == $bkm_label) {
213 PMA_Bookmark_delete($db, $key);
218 PMA_Bookmark_save($bfields, isset($bkm_all_users));
220 $bookmark_created = true;
221 } // end store bookmarks
223 // We can not read all at once, otherwise we can run out of memory
224 $memory_limit = trim(@ini_get
('memory_limit'));
226 if (empty($memory_limit)) {
227 $memory_limit = 2 * 1024 * 1024;
229 // In case no memory limit we work on 10MB chunks
230 if ($memory_limit == -1) {
231 $memory_limit = 10 * 1024 * 1024;
234 // Calculate value of the limit
235 if (strtolower(substr($memory_limit, -1)) == 'm') {
236 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
237 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
238 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
239 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
240 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
242 $memory_limit = (int)$memory_limit;
245 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
248 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
250 // sanitize $local_import_file as it comes from a POST
251 $local_import_file = PMA_securePath($local_import_file);
253 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
254 } elseif (empty($import_file) ||
!is_uploaded_file($import_file)) {
255 $import_file = 'none';
258 // Do we have file to import?
260 if ($import_file != 'none' && !$error) {
261 // work around open_basedir and other limitations
262 $open_basedir = @ini_get
('open_basedir');
264 // If we are on a server with open_basedir, we must move the file
265 // before opening it. The doc explains how to create the "./tmp"
268 if (!empty($open_basedir)) {
270 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
272 if (is_writable($tmp_subdir)) {
275 $import_file_new = $tmp_subdir . basename($import_file);
276 if (move_uploaded_file($import_file, $import_file_new)) {
277 $import_file = $import_file_new;
278 $file_to_unlink = $import_file_new;
281 $size = filesize($import_file);
286 * Handle file compression
287 * @todo duplicate code exists in File.class.php
289 $compression = PMA_detectCompression($import_file);
290 if ($compression === false) {
291 $message = PMA_Message
::error(__('File could not be read'));
294 switch ($compression) {
295 case 'application/bzip2':
296 if ($cfg['BZipDump'] && @function_exists
('bzopen')) {
297 $import_handle = @bzopen
($import_file, 'r');
299 $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.'));
300 $message->addParam($compression);
304 case 'application/gzip':
305 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
306 $import_handle = @gzopen
($import_file, 'r');
308 $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.'));
309 $message->addParam($compression);
313 case 'application/zip':
314 if ($cfg['ZipDump'] && @function_exists
('zip_open')) {
316 * Load interface for zip extension.
318 include_once './libraries/zip_extension.lib.php';
319 $result = PMA_getZipContents($import_file);
320 if (! empty($result['error'])) {
321 $message = PMA_Message
::rawError($result['error']);
324 $import_text = $result['data'];
327 $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.'));
328 $message->addParam($compression);
333 $import_handle = @fopen
($import_file, 'r');
336 $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.'));
337 $message->addParam($compression);
342 // use isset() because zip compression type does not use a handle
343 if (!$error && isset($import_handle) && $import_handle === false) {
344 $message = PMA_Message
::error(__('File could not be read'));
348 if (! isset($import_text) ||
empty($import_text)) {
349 $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].'));
354 // so we can obtain the message
355 //$_SESSION['Import_message'] = $message->getDisplay();
357 // Convert the file's charset if necessary
358 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
&& isset($charset_of_file)) {
359 if ($charset_of_file != 'utf-8') {
360 $charset_conversion = true;
362 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
364 // Drizzle doesn't support other character sets, so we can't fallback to SET NAMES - throw an error
366 $message = PMA_Message
::error(__('Cannot convert file\'s character set without character set conversion library'));
368 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
369 // We can not show query in this case, it is in different charset
370 $sql_query_disabled = true;
371 $reset_charset = true;
375 // Something to skip?
376 if (!$error && isset($skip)) {
377 $original_skip = $skip;
379 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
380 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
381 $skip -= $read_limit;
387 // Check for file existance
388 if (!file_exists('./libraries/import/' . $format . '.php')) {
390 $message = PMA_Message
::error(__('Could not load import plugins, please check your installation!'));
392 // Do the real import
393 $plugin_param = $import_type;
394 include './libraries/import/' . $format . '.php';
398 if (! $error && false !== $import_handle && null !== $import_handle) {
399 fclose($import_handle);
402 // Cleanup temporary file
403 if ($file_to_unlink != '') {
404 unlink($file_to_unlink);
407 // Reset charset back, if we did some changes
408 if ($reset_charset) {
409 PMA_DBI_query('SET CHARACTER SET utf8');
410 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
413 // Show correct message
414 if (!empty($id_bookmark) && $action_bookmark == 2) {
415 $message = PMA_Message
::success(__('The bookmark has been deleted.'));
416 $display_query = $import_text;
417 $error = false; // unset error marker, it was used just to skip processing
418 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
419 $message = PMA_Message
::notice(__('Showing bookmark'));
420 } elseif ($bookmark_created) {
421 $special_message = '[br]' . sprintf(__('Bookmark %s created'), htmlspecialchars($bkm_label));
422 } elseif ($finished && !$error) {
423 if ($import_type == 'query') {
424 $message = PMA_Message
::success();
426 if ($import_notice) {
427 $message = PMA_Message
::success('<em>'.__('Import has been successfully finished, %d queries executed.').'</em>');
428 $message->addParam($executed_queries);
430 $message->addString($import_notice);
431 $message->addString('(' . $_FILES['import_file']['name'] . ')');
433 $message = PMA_Message
::success(__('Import has been successfully finished, %d queries executed.'));
434 $message->addParam($executed_queries);
435 $message->addString('(' . $_FILES['import_file']['name'] . ')');
440 // Did we hit timeout? Tell it user.
441 if ($timeout_passed) {
442 $message = PMA_Message
::error(__('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.'));
443 if ($offset == 0 ||
(isset($original_skip) && $original_skip == $offset)) {
444 $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.'));
448 // if there is any message, copy it into $_SESSION as well, so we can obtain it by AJAX call
449 if (isset($message)) {
450 $_SESSION['Import_message']['message'] = $message->getDisplay();
451 // $_SESSION['Import_message']['go_back_url'] = $goto.'?'. PMA_generate_common_url();
453 // Parse and analyze the query, for correct db and table name
454 // in case of a query typed in the query window
455 // (but if the query is too large, in case of an imported file, the parser
456 // can choke on it so avoid parsing)
457 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
458 include_once './libraries/parse_analyze.lib.php';
461 // There was an error?
462 if (isset($my_die)) {
463 foreach ($my_die AS $key => $die) {
464 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
468 // we want to see the results of the last query that returned at least a row
469 if (! empty($last_query_with_results)) {
470 // but we want to show intermediate results too
471 $disp_query = $sql_query;
472 $disp_message = __('Your SQL query has been executed successfully');
473 $sql_query = $last_query_with_results;
480 $active_page = $goto;
481 include './' . $goto;