2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Core script for import, this is just the glue around all other stuff
6 * @uses PMA_Bookmark_getList()
11 * Get the variables sent or posted to this script and a core script
13 require_once './libraries/common.inc.php';
14 //require_once './libraries/display_import_functions.lib.php';
15 $GLOBALS['js_include'][] = 'functions.js';
17 // reset import messages for ajax request
18 $_SESSION['Import_message']['message'] = null;
19 $_SESSION['Import_message']['go_back_url'] = null;
21 $GLOBALS['reload'] = false;
23 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
24 if (!empty($sql_query)) {
26 $import_text = $sql_query;
27 $import_type = 'query';
30 // refresh left frame on changes in table or db structure
31 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
32 $GLOBALS['reload'] = true;
36 } elseif (!empty($sql_localfile)) {
37 // run SQL file on server
38 $local_import_file = $sql_localfile;
39 $import_type = 'queryfile';
41 unset($sql_localfile);
42 } elseif (!empty($sql_file)) {
43 // run uploaded SQL file
44 $import_file = $sql_file;
45 $import_type = 'queryfile';
48 } elseif (!empty($id_bookmark)) {
50 $import_type = 'query';
54 // If we didn't get any parameters, either user called this directly, or
55 // upload limit has been reached, let's assume the second possibility.
57 if ($_POST == array() && $_GET == array()) {
58 require_once './libraries/header.inc.php';
59 $message = PMA_Message
::error(__('You probably tried to upload too large file. Please refer to %sdocumentation%s for ways to workaround this limit.'));
60 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
61 $message->addParam('[/a]');
63 // so we can obtain the message
64 $_SESSION['Import_message']['message'] = $message->getDisplay();
65 $_SESSION['Import_message']['go_back_url'] = $goto;
68 require './libraries/footer.inc.php';
71 // Check needed parameters
72 PMA_checkParameters(array('import_type', 'format'));
74 // We don't want anything special in format
75 $format = PMA_securePath($format);
78 require_once './libraries/import.lib.php';
80 // Create error and goto url
81 if ($import_type == 'table') {
82 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
83 $_SESSION['Import_message']['go_back_url'] = $err_url;
84 $goto = 'tbl_import.php';
85 } elseif ($import_type == 'database') {
86 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
87 $_SESSION['Import_message']['go_back_url'] = $err_url;
88 $goto = 'db_import.php';
89 } elseif ($import_type == 'server') {
90 $err_url = 'server_import.php?' . PMA_generate_common_url();
91 $_SESSION['Import_message']['go_back_url'] = $err_url;
92 $goto = 'server_import.php';
94 if (empty($goto) ||
!preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
95 if (strlen($table) && strlen($db)) {
96 $goto = 'tbl_structure.php';
97 } elseif (strlen($db)) {
98 $goto = 'db_structure.php';
100 $goto = 'server_sql.php';
103 if (strlen($table) && strlen($db)) {
104 $common = PMA_generate_common_url($db, $table);
105 } elseif (strlen($db)) {
106 $common = PMA_generate_common_url($db);
108 $common = PMA_generate_common_url();
112 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ?
'&table=' . urlencode($table) : '');
113 $_SESSION['Import_message']['go_back_url'] = $err_url;
118 PMA_DBI_select_db($db);
121 @set_time_limit
($cfg['ExecTimeLimit']);
122 if (!empty($cfg['MemoryLimit'])) {
123 @ini_set
('memory_limit', $cfg['MemoryLimit']);
127 if (isset($allow_interrupt)) {
128 $maximum_time = ini_get('max_execution_time');
133 // set default values
134 $timeout_passed = FALSE;
140 $file_to_unlink = '';
142 $sql_query_disabled = FALSE;
144 $executed_queries = 0;
146 $charset_conversion = FALSE;
147 $reset_charset = FALSE;
148 $bookmark_created = FALSE;
150 // Bookmark Support: get a query back from bookmark if required
151 if (!empty($id_bookmark)) {
152 require_once './libraries/bookmark.lib.php';
153 switch ($action_bookmark) {
154 case 0: // bookmarked query that have to be run
155 $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
156 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
157 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
160 // refresh left frame on changes in table or db structure
161 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
162 $GLOBALS['reload'] = true;
166 case 1: // bookmarked query that have to be displayed
167 $import_text = PMA_Bookmark_get($db, $id_bookmark);
170 case 2: // bookmarked query that have to be deleted
171 $import_text = PMA_Bookmark_get($db, $id_bookmark);
172 PMA_Bookmark_delete($db, $id_bookmark);
174 $error = TRUE; // this is kind of hack to skip processing the query
177 } // end bookmarks reading
179 // Do no run query if we show PHP code
180 if (isset($GLOBALS['show_as_php'])) {
185 // Store the query as a bookmark before executing it if bookmarklabel was given
186 if (!empty($bkm_label) && !empty($import_text)) {
187 require_once './libraries/bookmark.lib.php';
190 'user' => $cfg['Bookmark']['user'],
191 'query' => urlencode($import_text),
192 'label' => $bkm_label
195 // Should we replace bookmark?
196 if (isset($bkm_replace)) {
197 $bookmarks = PMA_Bookmark_getList($db);
198 foreach ($bookmarks as $key => $val) {
199 if ($val == $bkm_label) {
200 PMA_Bookmark_delete($db, $key);
205 PMA_Bookmark_save($bfields, isset($bkm_all_users));
207 $bookmark_created = TRUE;
208 } // end store bookmarks
210 // We can not read all at once, otherwise we can run out of memory
211 $memory_limit = trim(@ini_get
('memory_limit'));
213 if (empty($memory_limit)) {
214 $memory_limit = 2 * 1024 * 1024;
216 // In case no memory limit we work on 10MB chunks
217 if ($memory_limit == -1) {
218 $memory_limit = 10 * 1024 * 1024;
221 // Calculate value of the limit
222 if (strtolower(substr($memory_limit, -1)) == 'm') {
223 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
224 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
225 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
226 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
227 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
229 $memory_limit = (int)$memory_limit;
232 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
235 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
237 // sanitize $local_import_file as it comes from a POST
238 $local_import_file = PMA_securePath($local_import_file);
240 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
241 } elseif (empty($import_file) ||
!is_uploaded_file($import_file)) {
242 $import_file = 'none';
245 // Do we have file to import?
247 if ($import_file != 'none' && !$error) {
248 // work around open_basedir and other limitations
249 $open_basedir = @ini_get
('open_basedir');
251 // If we are on a server with open_basedir, we must move the file
252 // before opening it. The doc explains how to create the "./tmp"
255 if (!empty($open_basedir)) {
257 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
259 if (is_writable($tmp_subdir)) {
262 $import_file_new = $tmp_subdir . basename($import_file);
263 if (move_uploaded_file($import_file, $import_file_new)) {
264 $import_file = $import_file_new;
265 $file_to_unlink = $import_file_new;
268 $size = filesize($import_file);
273 * Handle file compression
274 * @todo duplicate code exists in File.class.php
276 $compression = PMA_detectCompression($import_file);
277 if ($compression === FALSE) {
278 $message = PMA_Message
::error(__('File could not be read'));
281 switch ($compression) {
282 case 'application/bzip2':
283 if ($cfg['BZipDump'] && @function_exists
('bzopen')) {
284 $import_handle = @bzopen
($import_file, 'r');
286 $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.'));
287 $message->addParam($compression);
291 case 'application/gzip':
292 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
293 $import_handle = @gzopen
($import_file, 'r');
295 $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.'));
296 $message->addParam($compression);
300 case 'application/zip':
301 if ($cfg['ZipDump'] && @function_exists
('zip_open')) {
303 * Load interface for zip extension.
305 include_once './libraries/zip_extension.lib.php';
306 $result = PMA_getZipContents($import_file);
307 if (! empty($result['error'])) {
308 $message = PMA_Message
::rawError($result['error']);
311 $import_text = $result['data'];
314 $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.'));
315 $message->addParam($compression);
320 $import_handle = @fopen
($import_file, 'r');
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);
329 // use isset() because zip compression type does not use a handle
330 if (!$error && isset($import_handle) && $import_handle === FALSE) {
331 $message = PMA_Message
::error(__('File could not be read'));
335 if (!isset($import_text) ||
empty($import_text)) {
336 $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].'));
341 // so we can obtain the message
342 //$_SESSION['Import_message'] = $message->getDisplay();
344 // Convert the file's charset if necessary
345 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
&& isset($charset_of_file)) {
346 if ($charset_of_file != $charset) {
347 $charset_conversion = TRUE;
349 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
350 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
351 // We can not show query in this case, it is in different charset
352 $sql_query_disabled = TRUE;
353 $reset_charset = TRUE;
356 // Something to skip?
357 if (!$error && isset($skip)) {
358 $original_skip = $skip;
360 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
361 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
362 $skip -= $read_limit;
368 // Check for file existance
369 if (!file_exists('./libraries/import/' . $format . '.php')) {
371 $message = PMA_Message
::error(__('Could not load import plugins, please check your installation!'));
373 // Do the real import
374 $plugin_param = $import_type;
375 require './libraries/import/' . $format . '.php';
379 if (! $error && FALSE !== $import_handle && NULL !== $import_handle) {
380 fclose($import_handle);
383 // Cleanup temporary file
384 if ($file_to_unlink != '') {
385 unlink($file_to_unlink);
388 // Reset charset back, if we did some changes
389 if ($reset_charset) {
390 PMA_DBI_query('SET CHARACTER SET utf8');
391 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
394 // Show correct message
395 if (!empty($id_bookmark) && $action_bookmark == 2) {
396 $message = PMA_Message
::success(__('The bookmark has been deleted.'));
397 $display_query = $import_text;
398 $error = FALSE; // unset error marker, it was used just to skip processing
399 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
400 $message = PMA_Message
::notice(__('Showing bookmark'));
401 } elseif ($bookmark_created) {
402 $special_message = '[br]' . sprintf(__('Bookmark %s created'), htmlspecialchars($bkm_label));
403 } elseif ($finished && !$error) {
404 if ($import_type == 'query') {
405 $message = PMA_Message
::success();
407 if ($import_notice) {
408 $message = PMA_Message
::success('<em>'.__('Import has been successfully finished, %d queries executed.').'</em>');
409 $message->addParam($executed_queries);
411 $message->addString($import_notice);
412 $message->addString('(' . $_FILES['import_file']['name'] . ')');
414 $message = PMA_Message
::success(__('Import has been successfully finished, %d queries executed.'));
415 $message->addParam($executed_queries);
416 $message->addString('(' . $_FILES['import_file']['name'] . ')');
421 // Did we hit timeout? Tell it user.
422 if ($timeout_passed) {
423 $message = PMA_Message
::error(__('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.'));
424 if ($offset == 0 ||
(isset($original_skip) && $original_skip == $offset)) {
425 $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.'));
429 // if there is any message, copy it into $_SESSION as well, so we can obtain it by AJAX call
430 if (isset($message)) {
431 $_SESSION['Import_message']['message'] = $message->getDisplay();
432 // $_SESSION['Import_message']['go_back_url'] = $goto.'?'. PMA_generate_common_url();
434 // Parse and analyze the query, for correct db and table name
435 // in case of a query typed in the query window
436 // (but if the query is too large, in case of an imported file, the parser
437 // can choke on it so avoid parsing)
438 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
439 require_once './libraries/parse_analyze.lib.php';
442 // There was an error?
443 if (isset($my_die)) {
444 foreach ($my_die AS $key => $die) {
445 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
449 // we want to see the results of the last query that returned at least a row
450 if (! empty($last_query_with_results)) {
451 // but we want to show intermediate results too
452 $disp_query = $sql_query;
453 $disp_message = __('Your SQL query has been executed successfully');
454 $sql_query = $last_query_with_results;
461 $active_page = $goto;
462 require './' . $goto;