bug #1918531 [compatibility] Navigation is not w3.org valid
[phpmyadmin/dkf.git] / import.php
blobbf57255262feee2838a35c98e18352bb5c231a82
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 * @version $Id$
7 */
9 /**
10 * Get the variables sent or posted to this script and a core script
12 require_once './libraries/common.inc.php';
13 $js_to_run = 'functions.js';
15 // default values
16 $GLOBALS['reload'] = false;
18 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
19 if (!empty($sql_query)) {
20 // run SQL query
21 $import_text = $sql_query;
22 $import_type = 'query';
23 $format = 'sql';
25 // refresh left frame on changes in table or db structure
26 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
27 $GLOBALS['reload'] = true;
30 $sql_query = '';
31 } elseif (!empty($sql_localfile)) {
32 // run SQL file on server
33 $local_import_file = $sql_localfile;
34 $import_type = 'queryfile';
35 $format = 'sql';
36 unset($sql_localfile);
37 } elseif (!empty($sql_file)) {
38 // run uploaded SQL file
39 $import_file = $sql_file;
40 $import_type = 'queryfile';
41 $format = 'sql';
42 unset($sql_file);
43 } elseif (!empty($id_bookmark)) {
44 // run bookmark
45 $import_type = 'query';
46 $format = 'sql';
49 // If we didn't get any parameters, either user called this directly, or
50 // upload limit has been reached, let's assume the second possibility.
51 if ($_POST == array() && $_GET == array()) {
52 require_once './libraries/header.inc.php';
53 $show_error_header = TRUE;
54 PMA_showMessage(sprintf($strUploadLimit, '[a@./Documentation.html#faq1_16@_blank]', '[/a]'));
55 require './libraries/footer.inc.php';
58 // Check needed parameters
59 PMA_checkParameters(array('import_type', 'format'));
61 // We don't want anything special in format
62 $format = PMA_securePath($format);
64 // Import functions
65 require_once './libraries/import.lib.php';
67 // Create error and goto url
68 if ($import_type == 'table') {
69 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
70 $goto = 'tbl_import.php';
71 } elseif ($import_type == 'database') {
72 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
73 $goto = 'db_import.php';
74 } elseif ($import_type == 'server') {
75 $err_url = 'server_import.php?' . PMA_generate_common_url();
76 $goto = 'server_import.php';
77 } else {
78 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
79 if (strlen($table) && strlen($db)) {
80 $goto = 'tbl_structure.php';
81 } elseif (strlen($db)) {
82 $goto = 'db_structure.php';
83 } else {
84 $goto = 'server_sql.php';
87 if (strlen($table) && strlen($db)) {
88 $common = PMA_generate_common_url($db, $table);
89 } elseif (strlen($db)) {
90 $common = PMA_generate_common_url($db);
91 } else {
92 $common = PMA_generate_common_url();
94 $err_url = $goto
95 . '?' . $common
96 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
100 if (strlen($db)) {
101 PMA_DBI_select_db($db);
104 @set_time_limit($cfg['ExecTimeLimit']);
105 if (!empty($cfg['MemoryLimit'])) {
106 @ini_set('memory_limit', $cfg['MemoryLimit']);
109 $timestamp = time();
110 if (isset($allow_interrupt)) {
111 $maximum_time = ini_get('max_execution_time');
112 } else {
113 $maximum_time = 0;
116 // set default values
117 $timeout_passed = FALSE;
118 $error = FALSE;
119 $read_multiply = 1;
120 $finished = FALSE;
121 $offset = 0;
122 $max_sql_len = 0;
123 $file_to_unlink = '';
124 $sql_query = '';
125 $sql_query_disabled = FALSE;
126 $go_sql = FALSE;
127 $executed_queries = 0;
128 $run_query = TRUE;
129 $charset_conversion = FALSE;
130 $reset_charset = FALSE;
131 $bookmark_created = FALSE;
133 // Bookmark Support: get a query back from bookmark if required
134 if (!empty($id_bookmark)) {
135 require_once './libraries/bookmark.lib.php';
136 switch ($action_bookmark) {
137 case 0: // bookmarked query that have to be run
138 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark, 'id', isset($action_bookmark_all));
139 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
140 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
143 // refresh left frame on changes in table or db structure
144 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
145 $GLOBALS['reload'] = true;
148 break;
149 case 1: // bookmarked query that have to be displayed
150 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
151 $run_query = FALSE;
152 break;
153 case 2: // bookmarked query that have to be deleted
154 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
155 PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
156 $run_query = FALSE;
157 $error = TRUE; // this is kind of hack to skip processing the query
158 break;
160 } // end bookmarks reading
162 // Do no run query if we show PHP code
163 if (isset($GLOBALS['show_as_php'])) {
164 $run_query = FALSE;
165 $go_sql = TRUE;
168 // Store the query as a bookmark before executing it if bookmarklabel was given
169 if (!empty($bkm_label) && !empty($import_text)) {
170 require_once './libraries/bookmark.lib.php';
171 $bfields = array(
172 'dbase' => $db,
173 'user' => $cfg['Bookmark']['user'],
174 'query' => urlencode($import_text),
175 'label' => $bkm_label
178 // Should we replace bookmark?
179 if (isset($bkm_replace)) {
180 $bookmarks = PMA_listBookmarks($db, $cfg['Bookmark']);
181 foreach ($bookmarks as $key => $val) {
182 if ($val == $bkm_label) {
183 PMA_deleteBookmarks($db, $cfg['Bookmark'], $key);
188 PMA_addBookmarks($bfields, $cfg['Bookmark'], isset($bkm_all_users));
190 $bookmark_created = TRUE;
191 } // end store bookmarks
193 // We can not read all at once, otherwise we can run out of memory
194 $memory_limit = trim(@ini_get('memory_limit'));
195 // 2 MB as default
196 if (empty($memory_limit)) {
197 $memory_limit = 2 * 1024 * 1024;
199 // In case no memory limit we work on 10MB chunks
200 if ($memory_limit = -1) {
201 $memory_limit = 10 * 1024 * 1024;
204 // Calculate value of the limit
205 if (strtolower(substr($memory_limit, -1)) == 'm') {
206 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
207 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
208 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
209 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
210 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
211 } else {
212 $memory_limit = (int)$memory_limit;
215 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
217 // handle filenames
218 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
220 // sanitize $local_import_file as it comes from a POST
221 $local_import_file = PMA_securePath($local_import_file);
223 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
224 } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
225 $import_file = 'none';
228 // Do we have file to import?
229 if ($import_file != 'none' && !$error) {
230 // work around open_basedir and other limitations
231 $open_basedir = @ini_get('open_basedir');
233 // If we are on a server with open_basedir, we must move the file
234 // before opening it. The doc explains how to create the "./tmp"
235 // directory
237 if (!empty($open_basedir)) {
239 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
241 // function is_writeable() is valid on PHP3 and 4
242 if (is_writeable($tmp_subdir)) {
243 $import_file_new = $tmp_subdir . basename($import_file);
244 if (move_uploaded_file($import_file, $import_file_new)) {
245 $import_file = $import_file_new;
246 $file_to_unlink = $import_file_new;
251 // Handle file compression
252 $compression = PMA_detectCompression($import_file);
253 if ($compression === FALSE) {
254 $message = $strFileCouldNotBeRead;
255 $show_error_header = TRUE;
256 $error = TRUE;
257 } else {
258 switch ($compression) {
259 case 'application/bzip2':
260 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
261 $import_handle = @bzopen($import_file, 'r');
262 } else {
263 $message = sprintf($strUnsupportedCompressionDetected, $compression);
264 $show_error_header = TRUE;
265 $error = TRUE;
267 break;
268 case 'application/gzip':
269 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
270 $import_handle = @gzopen($import_file, 'r');
271 } else {
272 $message = sprintf($strUnsupportedCompressionDetected, $compression);
273 $show_error_header = TRUE;
274 $error = TRUE;
276 break;
277 case 'application/zip':
278 if ($cfg['GZipDump'] && @function_exists('gzinflate')) {
279 include_once './libraries/unzip.lib.php';
280 $import_handle = new SimpleUnzip();
281 $import_handle->ReadFile($import_file);
282 if ($import_handle->Count() == 0) {
283 $message = $strNoFilesFoundInZip;
284 $show_error_header = TRUE;
285 $error = TRUE;
286 } elseif ($import_handle->GetError(0) != 0) {
287 $message = $strErrorInZipFile . ' ' . $import_handle->GetErrorMsg(0);
288 $show_error_header = TRUE;
289 $error = TRUE;
290 } else {
291 $import_text = $import_handle->GetData(0);
293 // We don't need to store it further
294 $import_handle = '';
295 } else {
296 $message = sprintf($strUnsupportedCompressionDetected, $compression);
297 $show_error_header = TRUE;
298 $error = TRUE;
300 break;
301 case 'none':
302 $import_handle = @fopen($import_file, 'r');
303 break;
304 default:
305 $message = sprintf($strUnsupportedCompressionDetected, $compression);
306 $show_error_header = TRUE;
307 $error = TRUE;
308 break;
311 if (!$error && $import_handle === FALSE) {
312 $message = $strFileCouldNotBeRead;
313 $show_error_header = TRUE;
314 $error = TRUE;
316 } elseif (!$error) {
317 if (!isset($import_text) || empty($import_text)) {
318 $message = $strNoDataReceived;
319 $show_error_header = TRUE;
320 $error = TRUE;
324 // Convert the file's charset if necessary
325 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
326 && isset($charset_of_file)) {
327 if ($charset_of_file != $charset) {
328 $charset_conversion = TRUE;
330 } elseif (PMA_MYSQL_INT_VERSION >= 40100
331 && isset($charset_of_file) && $charset_of_file != 'utf8') {
332 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
333 // We can not show query in this case, it is in different charset
334 $sql_query_disabled = TRUE;
335 $reset_charset = TRUE;
338 // Something to skip?
339 if (!$error && isset($skip)) {
340 $original_skip = $skip;
341 while ($skip > 0) {
342 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
343 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
344 $skip -= $read_limit;
346 unset($skip);
349 if (!$error) {
350 // Check for file existance
351 if (!file_exists('./libraries/import/' . $format . '.php')) {
352 $error = TRUE;
353 $message = $strCanNotLoadImportPlugins;
354 $show_error_header = TRUE;
355 } else {
356 // Do the real import
357 $plugin_param = $import_type;
358 require './libraries/import/' . $format . '.php';
362 // Cleanup temporary file
363 if ($file_to_unlink != '') {
364 unlink($file_to_unlink);
367 // Reset charset back, if we did some changes
368 if ($reset_charset) {
369 PMA_DBI_query('SET CHARACTER SET utf8');
370 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
373 // Show correct message
374 if (!empty($id_bookmark) && $action_bookmark == 2) {
375 $message = $strBookmarkDeleted;
376 $display_query = $import_text;
377 $error = FALSE; // unset error marker, it was used just to skip processing
378 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
379 $message = $strShowingBookmark;
380 } elseif ($bookmark_created) {
381 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
382 } elseif ($finished && !$error) {
383 if ($import_type == 'query') {
384 $message = $strSuccess;
385 } else {
386 $message = sprintf($strImportSuccessfullyFinished, $executed_queries);
390 // Did we hit timeout? Tell it user.
391 if ($timeout_passed) {
392 $message = $strTimeoutPassed;
393 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
394 $message .= ' ' . $strTimeoutNothingParsed;
398 // Parse and analyze the query, for correct db and table name
399 // in case of a query typed in the query window
400 require_once './libraries/parse_analyze.lib.php';
402 // There was an error?
403 if (isset($my_die)) {
404 foreach ($my_die AS $key => $die) {
405 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
409 if ($go_sql) {
410 require './sql.php';
411 } else {
412 $active_page = $goto;
413 require './' . $goto;
415 exit();