Translation update done using Pootle.
[phpmyadmin/dkf.git] / import.php
blob0dab666819de7990e8dc83032d5820f04ee7c496
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 * @version $Id$
8 * @package phpMyAdmin
9 */
11 /**
12 * Get the variables sent or posted to this script and a core script
14 require_once './libraries/common.inc.php';
15 //require_once './libraries/display_import_functions.lib.php';
16 $GLOBALS['js_include'][] = 'functions.js';
18 // reset import messages for ajax request
19 $_SESSION['Import_message']['message'] = null;
20 $_SESSION['Import_message']['go_back_url'] = null;
21 // default values
22 $GLOBALS['reload'] = false;
24 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
25 if (!empty($sql_query)) {
26 // run SQL query
27 $import_text = $sql_query;
28 $import_type = 'query';
29 $format = 'sql';
31 // refresh left frame on changes in table or db structure
32 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
33 $GLOBALS['reload'] = true;
36 $sql_query = '';
37 } elseif (!empty($sql_localfile)) {
38 // run SQL file on server
39 $local_import_file = $sql_localfile;
40 $import_type = 'queryfile';
41 $format = 'sql';
42 unset($sql_localfile);
43 } elseif (!empty($sql_file)) {
44 // run uploaded SQL file
45 $import_file = $sql_file;
46 $import_type = 'queryfile';
47 $format = 'sql';
48 unset($sql_file);
49 } elseif (!empty($id_bookmark)) {
50 // run bookmark
51 $import_type = 'query';
52 $format = 'sql';
55 // If we didn't get any parameters, either user called this directly, or
56 // upload limit has been reached, let's assume the second possibility.
58 if ($_POST == array() && $_GET == array()) {
59 require_once './libraries/header.inc.php';
60 $message = PMA_Message::error('strUploadLimit');
61 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
62 $message->addParam('[/a]');
64 // so we can obtain the message
65 $_SESSION['Import_message']['message'] = $message->getDisplay();
66 $_SESSION['Import_message']['go_back_url'] = $goto;
68 $message->display();
69 require './libraries/footer.inc.php';
72 // Check needed parameters
73 PMA_checkParameters(array('import_type', 'format'));
75 // We don't want anything special in format
76 $format = PMA_securePath($format);
78 // Import functions
79 require_once './libraries/import.lib.php';
81 // Create error and goto url
82 if ($import_type == 'table') {
83 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
84 $_SESSION['Import_message']['go_back_url'] = $err_url;
85 $goto = 'tbl_import.php';
86 } elseif ($import_type == 'database') {
87 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
88 $_SESSION['Import_message']['go_back_url'] = $err_url;
89 $goto = 'db_import.php';
90 } elseif ($import_type == 'server') {
91 $err_url = 'server_import.php?' . PMA_generate_common_url();
92 $_SESSION['Import_message']['go_back_url'] = $err_url;
93 $goto = 'server_import.php';
94 } else {
95 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
96 if (strlen($table) && strlen($db)) {
97 $goto = 'tbl_structure.php';
98 } elseif (strlen($db)) {
99 $goto = 'db_structure.php';
100 } else {
101 $goto = 'server_sql.php';
104 if (strlen($table) && strlen($db)) {
105 $common = PMA_generate_common_url($db, $table);
106 } elseif (strlen($db)) {
107 $common = PMA_generate_common_url($db);
108 } else {
109 $common = PMA_generate_common_url();
111 $err_url = $goto
112 . '?' . $common
113 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
114 $_SESSION['Import_message']['go_back_url'] = $err_url;
118 if (strlen($db)) {
119 PMA_DBI_select_db($db);
122 @set_time_limit($cfg['ExecTimeLimit']);
123 if (!empty($cfg['MemoryLimit'])) {
124 @ini_set('memory_limit', $cfg['MemoryLimit']);
127 $timestamp = time();
128 if (isset($allow_interrupt)) {
129 $maximum_time = ini_get('max_execution_time');
130 } else {
131 $maximum_time = 0;
134 // set default values
135 $timeout_passed = FALSE;
136 $error = FALSE;
137 $read_multiply = 1;
138 $finished = FALSE;
139 $offset = 0;
140 $max_sql_len = 0;
141 $file_to_unlink = '';
142 $sql_query = '';
143 $sql_query_disabled = FALSE;
144 $go_sql = FALSE;
145 $executed_queries = 0;
146 $run_query = TRUE;
147 $charset_conversion = FALSE;
148 $reset_charset = FALSE;
149 $bookmark_created = FALSE;
151 // Bookmark Support: get a query back from bookmark if required
152 if (!empty($id_bookmark)) {
153 require_once './libraries/bookmark.lib.php';
154 switch ($action_bookmark) {
155 case 0: // bookmarked query that have to be run
156 $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
157 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
158 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
161 // refresh left frame on changes in table or db structure
162 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
163 $GLOBALS['reload'] = true;
166 break;
167 case 1: // bookmarked query that have to be displayed
168 $import_text = PMA_Bookmark_get($db, $id_bookmark);
169 $run_query = FALSE;
170 break;
171 case 2: // bookmarked query that have to be deleted
172 $import_text = PMA_Bookmark_get($db, $id_bookmark);
173 PMA_Bookmark_delete($db, $id_bookmark);
174 $run_query = FALSE;
175 $error = TRUE; // this is kind of hack to skip processing the query
176 break;
178 } // end bookmarks reading
180 // Do no run query if we show PHP code
181 if (isset($GLOBALS['show_as_php'])) {
182 $run_query = FALSE;
183 $go_sql = TRUE;
186 // Store the query as a bookmark before executing it if bookmarklabel was given
187 if (!empty($bkm_label) && !empty($import_text)) {
188 require_once './libraries/bookmark.lib.php';
189 $bfields = array(
190 'dbase' => $db,
191 'user' => $cfg['Bookmark']['user'],
192 'query' => urlencode($import_text),
193 'label' => $bkm_label
196 // Should we replace bookmark?
197 if (isset($bkm_replace)) {
198 $bookmarks = PMA_Bookmark_getList($db);
199 foreach ($bookmarks as $key => $val) {
200 if ($val == $bkm_label) {
201 PMA_Bookmark_delete($db, $key);
206 PMA_Bookmark_save($bfields, isset($bkm_all_users));
208 $bookmark_created = TRUE;
209 } // end store bookmarks
211 // We can not read all at once, otherwise we can run out of memory
212 $memory_limit = trim(@ini_get('memory_limit'));
213 // 2 MB as default
214 if (empty($memory_limit)) {
215 $memory_limit = 2 * 1024 * 1024;
217 // In case no memory limit we work on 10MB chunks
218 if ($memory_limit == -1) {
219 $memory_limit = 10 * 1024 * 1024;
222 // Calculate value of the limit
223 if (strtolower(substr($memory_limit, -1)) == 'm') {
224 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
225 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
226 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
227 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
228 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
229 } else {
230 $memory_limit = (int)$memory_limit;
233 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
235 // handle filenames
236 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
238 // sanitize $local_import_file as it comes from a POST
239 $local_import_file = PMA_securePath($local_import_file);
241 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
242 } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
243 $import_file = 'none';
246 // Do we have file to import?
248 if ($import_file != 'none' && !$error) {
249 // work around open_basedir and other limitations
250 $open_basedir = @ini_get('open_basedir');
252 // If we are on a server with open_basedir, we must move the file
253 // before opening it. The doc explains how to create the "./tmp"
254 // directory
256 if (!empty($open_basedir)) {
258 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
260 if (is_writable($tmp_subdir)) {
263 $import_file_new = $tmp_subdir . basename($import_file);
264 if (move_uploaded_file($import_file, $import_file_new)) {
265 $import_file = $import_file_new;
266 $file_to_unlink = $import_file_new;
269 $size = filesize($import_file);
274 * Handle file compression
275 * @todo duplicate code exists in File.class.php
277 $compression = PMA_detectCompression($import_file);
278 if ($compression === FALSE) {
279 $message = PMA_Message::error('strFileCouldNotBeRead');
280 $error = TRUE;
281 } else {
282 switch ($compression) {
283 case 'application/bzip2':
284 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
285 $import_handle = @bzopen($import_file, 'r');
286 } else {
287 $message = PMA_Message::error('strUnsupportedCompressionDetected');
288 $message->addParam($compression);
289 $error = TRUE;
291 break;
292 case 'application/gzip':
293 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
294 $import_handle = @gzopen($import_file, 'r');
295 } else {
296 $message = PMA_Message::error('strUnsupportedCompressionDetected');
297 $message->addParam($compression);
298 $error = TRUE;
300 break;
301 case 'application/zip':
302 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
304 * Load interface for zip extension.
306 include_once './libraries/zip_extension.lib.php';
307 $result = PMA_getZipContents($import_file);
308 if (! empty($result['error'])) {
309 $message = PMA_Message::rawError($result['error']);
310 $error = TRUE;
311 } else {
312 $import_text = $result['data'];
314 } else {
315 $message = PMA_Message::error('strUnsupportedCompressionDetected');
316 $message->addParam($compression);
317 $error = TRUE;
319 break;
320 case 'none':
321 $import_handle = @fopen($import_file, 'r');
322 break;
323 default:
324 $message = PMA_Message::error('strUnsupportedCompressionDetected');
325 $message->addParam($compression);
326 $error = TRUE;
327 break;
330 // use isset() because zip compression type does not use a handle
331 if (!$error && isset($import_handle) && $import_handle === FALSE) {
332 $message = PMA_Message::error('strFileCouldNotBeRead');
333 $error = TRUE;
335 } elseif (!$error) {
336 if (!isset($import_text) || empty($import_text)) {
337 $message = PMA_Message::error('strNoDataReceived');
338 $error = TRUE;
342 // so we can obtain the message
343 //$_SESSION['Import_message'] = $message->getDisplay();
345 // Convert the file's charset if necessary
346 if ($cfg['AllowAnywhereRecoding'] && isset($charset_of_file)) {
347 if ($charset_of_file != $charset) {
348 $charset_conversion = TRUE;
350 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
351 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
352 // We can not show query in this case, it is in different charset
353 $sql_query_disabled = TRUE;
354 $reset_charset = TRUE;
357 // Something to skip?
358 if (!$error && isset($skip)) {
359 $original_skip = $skip;
360 while ($skip > 0) {
361 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
362 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
363 $skip -= $read_limit;
365 unset($skip);
368 if (!$error) {
369 // Check for file existance
370 if (!file_exists('./libraries/import/' . $format . '.php')) {
371 $error = TRUE;
372 $message = PMA_Message::error('strCanNotLoadImportPlugins');
373 } else {
374 // Do the real import
375 $plugin_param = $import_type;
376 require './libraries/import/' . $format . '.php';
380 if (! $error && FALSE !== $import_handle && NULL !== $import_handle) {
381 fclose($import_handle);
384 // Cleanup temporary file
385 if ($file_to_unlink != '') {
386 unlink($file_to_unlink);
389 // Reset charset back, if we did some changes
390 if ($reset_charset) {
391 PMA_DBI_query('SET CHARACTER SET utf8');
392 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
395 // Show correct message
396 if (!empty($id_bookmark) && $action_bookmark == 2) {
397 $message = PMA_Message::success('strBookmarkDeleted');
398 $display_query = $import_text;
399 $error = FALSE; // unset error marker, it was used just to skip processing
400 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
401 $message = PMA_Message::notice('strShowingBookmark');
402 } elseif ($bookmark_created) {
403 $special_message = '[br]' . sprintf(__('Bookmark %s created'), htmlspecialchars($bkm_label));
404 } elseif ($finished && !$error) {
405 if ($import_type == 'query') {
406 $message = PMA_Message::success();
407 } else {
408 if ($import_notice) {
409 $message = PMA_Message::success('<em>'.__('Import has been successfully finished, %d queries executed.').'</em>');
410 $message->addParam($executed_queries);
412 $message->addString($import_notice);
413 $message->addString('(' . $_FILES['import_file']['name'] . ')');
414 } else {
415 $message = PMA_Message::success('strImportSuccessfullyFinished');
416 $message->addParam($executed_queries);
417 $message->addString('(' . $_FILES['import_file']['name'] . ')');
422 // Did we hit timeout? Tell it user.
423 if ($timeout_passed) {
424 $message = PMA_Message::error('strTimeoutPassed');
425 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
426 $message->addString('strTimeoutNothingParsed');
430 // if there is any message, copy it into $_SESSION as well, so we can obtain it by AJAX call
431 if (isset($message)) {
432 $_SESSION['Import_message']['message'] = $message->getDisplay();
433 // $_SESSION['Import_message']['go_back_url'] = $goto.'?'. PMA_generate_common_url();
435 // Parse and analyze the query, for correct db and table name
436 // in case of a query typed in the query window
437 // (but if the query is too large, in case of an imported file, the parser
438 // can choke on it so avoid parsing)
439 if (strlen($sql_query) <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
440 require_once './libraries/parse_analyze.lib.php';
443 // There was an error?
444 if (isset($my_die)) {
445 foreach ($my_die AS $key => $die) {
446 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
450 if ($go_sql) {
451 require './sql.php';
452 } else {
453 $active_page = $goto;
454 require './' . $goto;
456 exit();