3.4.0-rc1
[phpmyadmin.git] / import.php
blob78e5ffdb4a36403b182fd6141eea77992180c2ee
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 * @uses PMA_Bookmark_getList()
7 * @package phpMyAdmin
8 */
10 /**
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;
19 // default values
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)) {
24 // run SQL query
25 $import_text = $sql_query;
26 $import_type = 'query';
27 $format = 'sql';
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;
34 $sql_query = '';
35 } elseif (!empty($sql_localfile)) {
36 // run SQL file on server
37 $local_import_file = $sql_localfile;
38 $import_type = 'queryfile';
39 $format = 'sql';
40 unset($sql_localfile);
41 } elseif (!empty($sql_file)) {
42 // run uploaded SQL file
43 $import_file = $sql_file;
44 $import_type = 'queryfile';
45 $format = 'sql';
46 unset($sql_file);
47 } elseif (!empty($id_bookmark)) {
48 // run bookmark
49 $import_type = 'query';
50 $format = 'sql';
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;
66 $message->display();
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);
76 // Import functions
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';
92 } else {
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';
98 } else {
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);
106 } else {
107 $common = PMA_generate_common_url();
109 $err_url = $goto
110 . '?' . $common
111 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
112 $_SESSION['Import_message']['go_back_url'] = $err_url;
116 if (strlen($db)) {
117 PMA_DBI_select_db($db);
120 @set_time_limit($cfg['ExecTimeLimit']);
121 if (!empty($cfg['MemoryLimit'])) {
122 @ini_set('memory_limit', $cfg['MemoryLimit']);
125 $timestamp = time();
126 if (isset($allow_interrupt)) {
127 $maximum_time = ini_get('max_execution_time');
128 } else {
129 $maximum_time = 0;
132 // set default values
133 $timeout_passed = FALSE;
134 $error = FALSE;
135 $read_multiply = 1;
136 $finished = FALSE;
137 $offset = 0;
138 $max_sql_len = 0;
139 $file_to_unlink = '';
140 $sql_query = '';
141 $sql_query_disabled = FALSE;
142 $go_sql = FALSE;
143 $executed_queries = 0;
144 $run_query = TRUE;
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;
165 break;
166 case 1: // bookmarked query that have to be displayed
167 $import_text = PMA_Bookmark_get($db, $id_bookmark);
168 $run_query = FALSE;
169 break;
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);
173 $run_query = FALSE;
174 $error = TRUE; // this is kind of hack to skip processing the query
175 break;
177 } // end bookmarks reading
179 // Do no run query if we show PHP code
180 if (isset($GLOBALS['show_as_php'])) {
181 $run_query = FALSE;
182 $go_sql = TRUE;
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';
188 $bfields = array(
189 'dbase' => $db,
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'));
212 // 2 MB as default
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;
228 } else {
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
234 // handle filenames
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"
253 // directory
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'));
279 $error = TRUE;
280 } else {
281 switch ($compression) {
282 case 'application/bzip2':
283 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
284 $import_handle = @bzopen($import_file, 'r');
285 } else {
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);
288 $error = TRUE;
290 break;
291 case 'application/gzip':
292 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
293 $import_handle = @gzopen($import_file, 'r');
294 } else {
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);
297 $error = TRUE;
299 break;
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']);
309 $error = TRUE;
310 } else {
311 $import_text = $result['data'];
313 } else {
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);
316 $error = TRUE;
318 break;
319 case 'none':
320 $import_handle = @fopen($import_file, 'r');
321 break;
322 default:
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;
326 break;
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'));
332 $error = TRUE;
334 } elseif (!$error) {
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].'));
337 $error = TRUE;
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;
359 while ($skip > 0) {
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;
364 unset($skip);
367 if (!$error) {
368 // Check for file existance
369 if (!file_exists('./libraries/import/' . $format . '.php')) {
370 $error = TRUE;
371 $message = PMA_Message::error(__('Could not load import plugins, please check your installation!'));
372 } else {
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();
406 } else {
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'] . ')');
413 } else {
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;
455 $go_sql = true;
458 if ($go_sql) {
459 require './sql.php';
460 } else {
461 $active_page = $goto;
462 require './' . $goto;
464 exit();