Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / import.php
blob56732aeb53d1d8bfa9250ae4f8c91b4759270814
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 * @package phpMyAdmin
7 */
9 /**
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;
18 // default values
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)) {
23 // run SQL query
24 $import_text = $sql_query;
25 $import_type = 'query';
26 $format = 'sql';
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;
33 $sql_query = '';
34 } elseif (!empty($sql_localfile)) {
35 // run SQL file on server
36 $local_import_file = $sql_localfile;
37 $import_type = 'queryfile';
38 $format = 'sql';
39 unset($sql_localfile);
40 } elseif (!empty($sql_file)) {
41 // run uploaded SQL file
42 $import_file = $sql_file;
43 $import_type = 'queryfile';
44 $format = 'sql';
45 unset($sql_file);
46 } elseif (!empty($id_bookmark)) {
47 // run bookmark
48 $import_type = 'query';
49 $format = 'sql';
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 require_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;
65 $message->display();
66 require './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);
75 // Import functions
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';
91 } else {
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';
97 } else {
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);
105 } else {
106 $common = PMA_generate_common_url();
108 $err_url = $goto
109 . '?' . $common
110 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . htmlspecialchars($table) : '');
111 $_SESSION['Import_message']['go_back_url'] = $err_url;
115 if (strlen($db)) {
116 PMA_DBI_select_db($db);
119 @set_time_limit($cfg['ExecTimeLimit']);
120 if (!empty($cfg['MemoryLimit'])) {
121 @ini_set('memory_limit', $cfg['MemoryLimit']);
124 $timestamp = time();
125 if (isset($allow_interrupt)) {
126 $maximum_time = ini_get('max_execution_time');
127 } else {
128 $maximum_time = 0;
131 // set default values
132 $timeout_passed = false;
133 $error = false;
134 $read_multiply = 1;
135 $finished = false;
136 $offset = 0;
137 $max_sql_len = 0;
138 $file_to_unlink = '';
139 $sql_query = '';
140 $sql_query_disabled = false;
141 $go_sql = false;
142 $executed_queries = 0;
143 $run_query = true;
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 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 != 'utf-8') {
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();