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';
16 // reset import messages for ajax request
17 $_SESSION['Import_message']['message'] = null;
18 $_SESSION['Import_message']['go_back_url'] = null;
20 $GLOBALS['reload'] = false;
22 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
23 if (!empty($sql_query)) {
25 $import_text = $sql_query;
26 $import_type = 'query';
29 // refresh left frame on changes in table or db structure
30 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
31 $GLOBALS['reload'] = true;
35 } elseif (!empty($sql_localfile)) {
36 // run SQL file on server
37 $local_import_file = $sql_localfile;
38 $import_type = 'queryfile';
40 unset($sql_localfile);
41 } elseif (!empty($sql_file)) {
42 // run uploaded SQL file
43 $import_file = $sql_file;
44 $import_type = 'queryfile';
47 } elseif (!empty($id_bookmark)) {
49 $import_type = 'query';
53 // If we didn't get any parameters, either user called this directly, or
54 // upload limit has been reached, let's assume the second possibility.
56 if ($_POST == array() && $_GET == array()) {
57 require_once './libraries/header.inc.php';
58 $message = PMA_Message
::error(__('You probably tried to upload too large file. Please refer to %sdocumentation%s for ways to workaround this limit.'));
59 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
60 $message->addParam('[/a]');
62 // so we can obtain the message
63 $_SESSION['Import_message']['message'] = $message->getDisplay();
64 $_SESSION['Import_message']['go_back_url'] = $goto;
67 require './libraries/footer.inc.php';
70 // Check needed parameters
71 PMA_checkParameters(array('import_type', 'format'));
73 // We don't want anything special in format
74 $format = PMA_securePath($format);
77 require_once './libraries/import.lib.php';
79 // Create error and goto url
80 if ($import_type == 'table') {
81 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
82 $_SESSION['Import_message']['go_back_url'] = $err_url;
83 $goto = 'tbl_import.php';
84 } elseif ($import_type == 'database') {
85 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
86 $_SESSION['Import_message']['go_back_url'] = $err_url;
87 $goto = 'db_import.php';
88 } elseif ($import_type == 'server') {
89 $err_url = 'server_import.php?' . PMA_generate_common_url();
90 $_SESSION['Import_message']['go_back_url'] = $err_url;
91 $goto = 'server_import.php';
93 if (empty($goto) ||
!preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
94 if (strlen($table) && strlen($db)) {
95 $goto = 'tbl_structure.php';
96 } elseif (strlen($db)) {
97 $goto = 'db_structure.php';
99 $goto = 'server_sql.php';
102 if (strlen($table) && strlen($db)) {
103 $common = PMA_generate_common_url($db, $table);
104 } elseif (strlen($db)) {
105 $common = PMA_generate_common_url($db);
107 $common = PMA_generate_common_url();
111 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ?
'&table=' . urlencode($table) : '');
112 $_SESSION['Import_message']['go_back_url'] = $err_url;
117 PMA_DBI_select_db($db);
120 @set_time_limit
($cfg['ExecTimeLimit']);
121 if (!empty($cfg['MemoryLimit'])) {
122 @ini_set
('memory_limit', $cfg['MemoryLimit']);
126 if (isset($allow_interrupt)) {
127 $maximum_time = ini_get('max_execution_time');
132 // set default values
133 $timeout_passed = false;
139 $file_to_unlink = '';
141 $sql_query_disabled = false;
143 $executed_queries = 0;
145 $charset_conversion = false;
146 $reset_charset = false;
147 $bookmark_created = false;
149 // Bookmark Support: get a query back from bookmark if required
150 if (!empty($id_bookmark)) {
151 $id_bookmark = (int)$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 != 'utf-8') {
347 $charset_conversion = true;
349 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
351 // Drizzle doesn't support other character sets, so we can't fallback to SET NAMES - throw an error
353 $message = PMA_Message
::error(__('Cannot convert file\'s character set without character set conversion library'));
355 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
356 // We can not show query in this case, it is in different charset
357 $sql_query_disabled = true;
358 $reset_charset = true;
362 // Something to skip?
363 if (!$error && isset($skip)) {
364 $original_skip = $skip;
366 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
367 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
368 $skip -= $read_limit;
374 // Check for file existance
375 if (!file_exists('./libraries/import/' . $format . '.php')) {
377 $message = PMA_Message
::error(__('Could not load import plugins, please check your installation!'));
379 // Do the real import
380 $plugin_param = $import_type;
381 require './libraries/import/' . $format . '.php';
385 if (! $error && false !== $import_handle && NULL !== $import_handle) {
386 fclose($import_handle);
389 // Cleanup temporary file
390 if ($file_to_unlink != '') {
391 unlink($file_to_unlink);
394 // Reset charset back, if we did some changes
395 if ($reset_charset) {
396 PMA_DBI_query('SET CHARACTER SET utf8');
397 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
400 // Show correct message
401 if (!empty($id_bookmark) && $action_bookmark == 2) {
402 $message = PMA_Message
::success(__('The bookmark has been deleted.'));
403 $display_query = $import_text;
404 $error = false; // unset error marker, it was used just to skip processing
405 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
406 $message = PMA_Message
::notice(__('Showing bookmark'));
407 } elseif ($bookmark_created) {
408 $special_message = '[br]' . sprintf(__('Bookmark %s created'), htmlspecialchars($bkm_label));
409 } elseif ($finished && !$error) {
410 if ($import_type == 'query') {
411 $message = PMA_Message
::success();
413 if ($import_notice) {
414 $message = PMA_Message
::success('<em>'.__('Import has been successfully finished, %d queries executed.').'</em>');
415 $message->addParam($executed_queries);
417 $message->addString($import_notice);
418 $message->addString('(' . $_FILES['import_file']['name'] . ')');
420 $message = PMA_Message
::success(__('Import has been successfully finished, %d queries executed.'));
421 $message->addParam($executed_queries);
422 $message->addString('(' . $_FILES['import_file']['name'] . ')');
427 // Did we hit timeout? Tell it user.
428 if ($timeout_passed) {
429 $message = PMA_Message
::error(__('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.'));
430 if ($offset == 0 ||
(isset($original_skip) && $original_skip == $offset)) {
431 $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.'));
435 // if there is any message, copy it into $_SESSION as well, so we can obtain it by AJAX call
436 if (isset($message)) {
437 $_SESSION['Import_message']['message'] = $message->getDisplay();
438 // $_SESSION['Import_message']['go_back_url'] = $goto.'?'. PMA_generate_common_url();
440 // Parse and analyze the query, for correct db and table name
441 // in case of a query typed in the query window
442 // (but if the query is too large, in case of an imported file, the parser
443 // can choke on it so avoid parsing)
444 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
445 require_once './libraries/parse_analyze.lib.php';
448 // There was an error?
449 if (isset($my_die)) {
450 foreach ($my_die AS $key => $die) {
451 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
455 // we want to see the results of the last query that returned at least a row
456 if (! empty($last_query_with_results)) {
457 // but we want to show intermediate results too
458 $disp_query = $sql_query;
459 $disp_message = __('Your SQL query has been executed successfully');
460 $sql_query = $last_query_with_results;
467 $active_page = $goto;
468 require './' . $goto;