Translation update done using Pootle.
[phpmyadmin/last10db.git] / import.php
blobdbc4d56ad3d2e2585315f20fbfa31224787aea3d
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 require_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;
164 break;
165 case 1: // bookmarked query that have to be displayed
166 $import_text = PMA_Bookmark_get($db, $id_bookmark);
167 $run_query = FALSE;
168 break;
169 case 2: // bookmarked query that have to be deleted
170 $import_text = PMA_Bookmark_get($db, $id_bookmark);
171 PMA_Bookmark_delete($db, $id_bookmark);
172 $run_query = FALSE;
173 $error = TRUE; // this is kind of hack to skip processing the query
174 break;
176 } // end bookmarks reading
178 // Do no run query if we show PHP code
179 if (isset($GLOBALS['show_as_php'])) {
180 $run_query = FALSE;
181 $go_sql = TRUE;
184 // Store the query as a bookmark before executing it if bookmarklabel was given
185 if (!empty($bkm_label) && !empty($import_text)) {
186 require_once './libraries/bookmark.lib.php';
187 $bfields = array(
188 'dbase' => $db,
189 'user' => $cfg['Bookmark']['user'],
190 'query' => urlencode($import_text),
191 'label' => $bkm_label
194 // Should we replace bookmark?
195 if (isset($bkm_replace)) {
196 $bookmarks = PMA_Bookmark_getList($db);
197 foreach ($bookmarks as $key => $val) {
198 if ($val == $bkm_label) {
199 PMA_Bookmark_delete($db, $key);
204 PMA_Bookmark_save($bfields, isset($bkm_all_users));
206 $bookmark_created = TRUE;
207 } // end store bookmarks
209 // We can not read all at once, otherwise we can run out of memory
210 $memory_limit = trim(@ini_get('memory_limit'));
211 // 2 MB as default
212 if (empty($memory_limit)) {
213 $memory_limit = 2 * 1024 * 1024;
215 // In case no memory limit we work on 10MB chunks
216 if ($memory_limit == -1) {
217 $memory_limit = 10 * 1024 * 1024;
220 // Calculate value of the limit
221 if (strtolower(substr($memory_limit, -1)) == 'm') {
222 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
223 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
224 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
225 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
226 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
227 } else {
228 $memory_limit = (int)$memory_limit;
231 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
233 // handle filenames
234 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
236 // sanitize $local_import_file as it comes from a POST
237 $local_import_file = PMA_securePath($local_import_file);
239 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
240 } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
241 $import_file = 'none';
244 // Do we have file to import?
246 if ($import_file != 'none' && !$error) {
247 // work around open_basedir and other limitations
248 $open_basedir = @ini_get('open_basedir');
250 // If we are on a server with open_basedir, we must move the file
251 // before opening it. The doc explains how to create the "./tmp"
252 // directory
254 if (!empty($open_basedir)) {
256 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
258 if (is_writable($tmp_subdir)) {
261 $import_file_new = $tmp_subdir . basename($import_file);
262 if (move_uploaded_file($import_file, $import_file_new)) {
263 $import_file = $import_file_new;
264 $file_to_unlink = $import_file_new;
267 $size = filesize($import_file);
272 * Handle file compression
273 * @todo duplicate code exists in File.class.php
275 $compression = PMA_detectCompression($import_file);
276 if ($compression === FALSE) {
277 $message = PMA_Message::error(__('File could not be read'));
278 $error = TRUE;
279 } else {
280 switch ($compression) {
281 case 'application/bzip2':
282 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
283 $import_handle = @bzopen($import_file, 'r');
284 } else {
285 $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.'));
286 $message->addParam($compression);
287 $error = TRUE;
289 break;
290 case 'application/gzip':
291 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
292 $import_handle = @gzopen($import_file, 'r');
293 } else {
294 $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.'));
295 $message->addParam($compression);
296 $error = TRUE;
298 break;
299 case 'application/zip':
300 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
302 * Load interface for zip extension.
304 include_once './libraries/zip_extension.lib.php';
305 $result = PMA_getZipContents($import_file);
306 if (! empty($result['error'])) {
307 $message = PMA_Message::rawError($result['error']);
308 $error = TRUE;
309 } else {
310 $import_text = $result['data'];
312 } else {
313 $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.'));
314 $message->addParam($compression);
315 $error = TRUE;
317 break;
318 case 'none':
319 $import_handle = @fopen($import_file, 'r');
320 break;
321 default:
322 $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.'));
323 $message->addParam($compression);
324 $error = TRUE;
325 break;
328 // use isset() because zip compression type does not use a handle
329 if (!$error && isset($import_handle) && $import_handle === FALSE) {
330 $message = PMA_Message::error(__('File could not be read'));
331 $error = TRUE;
333 } elseif (!$error) {
334 if (!isset($import_text) || empty($import_text)) {
335 $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].'));
336 $error = TRUE;
340 // so we can obtain the message
341 //$_SESSION['Import_message'] = $message->getDisplay();
343 // Convert the file's charset if necessary
344 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE && isset($charset_of_file)) {
345 if ($charset_of_file != $charset) {
346 $charset_conversion = TRUE;
348 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
349 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
350 // We can not show query in this case, it is in different charset
351 $sql_query_disabled = TRUE;
352 $reset_charset = TRUE;
355 // Something to skip?
356 if (!$error && isset($skip)) {
357 $original_skip = $skip;
358 while ($skip > 0) {
359 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
360 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
361 $skip -= $read_limit;
363 unset($skip);
366 if (!$error) {
367 // Check for file existance
368 if (!file_exists('./libraries/import/' . $format . '.php')) {
369 $error = TRUE;
370 $message = PMA_Message::error(__('Could not load import plugins, please check your installation!'));
371 } else {
372 // Do the real import
373 $plugin_param = $import_type;
374 require './libraries/import/' . $format . '.php';
378 if (! $error && FALSE !== $import_handle && NULL !== $import_handle) {
379 fclose($import_handle);
382 // Cleanup temporary file
383 if ($file_to_unlink != '') {
384 unlink($file_to_unlink);
387 // Reset charset back, if we did some changes
388 if ($reset_charset) {
389 PMA_DBI_query('SET CHARACTER SET utf8');
390 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
393 // Show correct message
394 if (!empty($id_bookmark) && $action_bookmark == 2) {
395 $message = PMA_Message::success(__('The bookmark has been deleted.'));
396 $display_query = $import_text;
397 $error = FALSE; // unset error marker, it was used just to skip processing
398 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
399 $message = PMA_Message::notice(__('Showing bookmark'));
400 } elseif ($bookmark_created) {
401 $special_message = '[br]' . sprintf(__('Bookmark %s created'), htmlspecialchars($bkm_label));
402 } elseif ($finished && !$error) {
403 if ($import_type == 'query') {
404 $message = PMA_Message::success();
405 } else {
406 if ($import_notice) {
407 $message = PMA_Message::success('<em>'.__('Import has been successfully finished, %d queries executed.').'</em>');
408 $message->addParam($executed_queries);
410 $message->addString($import_notice);
411 $message->addString('(' . $_FILES['import_file']['name'] . ')');
412 } else {
413 $message = PMA_Message::success(__('Import has been successfully finished, %d queries executed.'));
414 $message->addParam($executed_queries);
415 $message->addString('(' . $_FILES['import_file']['name'] . ')');
420 // Did we hit timeout? Tell it user.
421 if ($timeout_passed) {
422 $message = PMA_Message::error(__('Script timeout passed, if you want to finish import, please resubmit same file and import will resume.'));
423 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
424 $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.'));
428 // if there is any message, copy it into $_SESSION as well, so we can obtain it by AJAX call
429 if (isset($message)) {
430 $_SESSION['Import_message']['message'] = $message->getDisplay();
431 // $_SESSION['Import_message']['go_back_url'] = $goto.'?'. PMA_generate_common_url();
433 // Parse and analyze the query, for correct db and table name
434 // in case of a query typed in the query window
435 // (but if the query is too large, in case of an imported file, the parser
436 // can choke on it so avoid parsing)
437 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
438 require_once './libraries/parse_analyze.lib.php';
441 // There was an error?
442 if (isset($my_die)) {
443 foreach ($my_die AS $key => $die) {
444 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
448 // we want to see the results of the last query that returned at least a row
449 if (! empty($last_query_with_results)) {
450 // but we want to show intermediate results too
451 $disp_query = $sql_query;
452 $disp_message = __('Your SQL query has been executed successfully');
453 $sql_query = $last_query_with_results;
454 $go_sql = true;
457 if ($go_sql) {
458 require './sql.php';
459 } else {
460 $active_page = $goto;
461 require './' . $goto;
463 exit();