2.11.11.3 release
[phpmyadmin/madhuracj.git] / import.php
bloba1743e3a3f8e62ce493b49a7a48ba8534c6f42d2
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 $id_bookmark = (int)$id_bookmark;
136 require_once './libraries/bookmark.lib.php';
137 switch ($action_bookmark) {
138 case 0: // bookmarked query that have to be run
139 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark, 'id', isset($action_bookmark_all));
140 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
141 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
144 // refresh left frame on changes in table or db structure
145 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
146 $GLOBALS['reload'] = true;
149 break;
150 case 1: // bookmarked query that have to be displayed
151 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
152 $run_query = FALSE;
153 break;
154 case 2: // bookmarked query that have to be deleted
155 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
156 PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
157 $run_query = FALSE;
158 $error = TRUE; // this is kind of hack to skip processing the query
159 break;
161 } // end bookmarks reading
163 // Do no run query if we show PHP code
164 if (isset($GLOBALS['show_as_php'])) {
165 $run_query = FALSE;
166 $go_sql = TRUE;
169 // Store the query as a bookmark before executing it if bookmarklabel was given
170 if (!empty($bkm_label) && !empty($import_text)) {
171 require_once './libraries/bookmark.lib.php';
172 $bfields = array(
173 'dbase' => $db,
174 'user' => $cfg['Bookmark']['user'],
175 'query' => urlencode($import_text),
176 'label' => $bkm_label
179 // Should we replace bookmark?
180 if (isset($bkm_replace)) {
181 $bookmarks = PMA_listBookmarks($db, $cfg['Bookmark']);
182 foreach ($bookmarks as $key => $val) {
183 if ($val == $bkm_label) {
184 PMA_deleteBookmarks($db, $cfg['Bookmark'], $key);
189 PMA_addBookmarks($bfields, $cfg['Bookmark'], isset($bkm_all_users));
191 $bookmark_created = TRUE;
192 } // end store bookmarks
194 // We can not read all at once, otherwise we can run out of memory
195 $memory_limit = trim(@ini_get('memory_limit'));
196 // 2 MB as default
197 if (empty($memory_limit)) {
198 $memory_limit = 2 * 1024 * 1024;
200 // In case no memory limit we work on 10MB chunks
201 if ($memory_limit = -1) {
202 $memory_limit = 10 * 1024 * 1024;
205 // Calculate value of the limit
206 if (strtolower(substr($memory_limit, -1)) == 'm') {
207 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
208 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
209 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
210 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
211 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
212 } else {
213 $memory_limit = (int)$memory_limit;
216 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
218 // handle filenames
219 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
221 // sanitize $local_import_file as it comes from a POST
222 $local_import_file = PMA_securePath($local_import_file);
224 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
225 } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
226 $import_file = 'none';
229 // Do we have file to import?
230 if ($import_file != 'none' && !$error) {
231 // work around open_basedir and other limitations
232 $open_basedir = @ini_get('open_basedir');
234 // If we are on a server with open_basedir, we must move the file
235 // before opening it. The doc explains how to create the "./tmp"
236 // directory
238 if (!empty($open_basedir)) {
240 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
242 // function is_writeable() is valid on PHP3 and 4
243 if (is_writeable($tmp_subdir)) {
244 $import_file_new = $tmp_subdir . basename($import_file);
245 if (move_uploaded_file($import_file, $import_file_new)) {
246 $import_file = $import_file_new;
247 $file_to_unlink = $import_file_new;
252 // Handle file compression
253 $compression = PMA_detectCompression($import_file);
254 if ($compression === FALSE) {
255 $message = $strFileCouldNotBeRead;
256 $show_error_header = TRUE;
257 $error = TRUE;
258 } else {
259 switch ($compression) {
260 case 'application/bzip2':
261 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
262 $import_handle = @bzopen($import_file, 'r');
263 } else {
264 $message = sprintf($strUnsupportedCompressionDetected, $compression);
265 $show_error_header = TRUE;
266 $error = TRUE;
268 break;
269 case 'application/gzip':
270 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
271 $import_handle = @gzopen($import_file, 'r');
272 } else {
273 $message = sprintf($strUnsupportedCompressionDetected, $compression);
274 $show_error_header = TRUE;
275 $error = TRUE;
277 break;
278 case 'application/zip':
279 if ($cfg['GZipDump'] && @function_exists('gzinflate')) {
280 include_once './libraries/unzip.lib.php';
281 $import_handle = new SimpleUnzip();
282 $import_handle->ReadFile($import_file);
283 if ($import_handle->Count() == 0) {
284 $message = $strNoFilesFoundInZip;
285 $show_error_header = TRUE;
286 $error = TRUE;
287 } elseif ($import_handle->GetError(0) != 0) {
288 $message = $strErrorInZipFile . ' ' . $import_handle->GetErrorMsg(0);
289 $show_error_header = TRUE;
290 $error = TRUE;
291 } else {
292 $import_text = $import_handle->GetData(0);
294 // We don't need to store it further
295 $import_handle = '';
296 } else {
297 $message = sprintf($strUnsupportedCompressionDetected, $compression);
298 $show_error_header = TRUE;
299 $error = TRUE;
301 break;
302 case 'none':
303 $import_handle = @fopen($import_file, 'r');
304 break;
305 default:
306 $message = sprintf($strUnsupportedCompressionDetected, $compression);
307 $show_error_header = TRUE;
308 $error = TRUE;
309 break;
312 if (!$error && $import_handle === FALSE) {
313 $message = $strFileCouldNotBeRead;
314 $show_error_header = TRUE;
315 $error = TRUE;
317 } elseif (!$error) {
318 if (!isset($import_text) || empty($import_text)) {
319 $message = $strNoDataReceived;
320 $show_error_header = TRUE;
321 $error = TRUE;
325 // Convert the file's charset if necessary
326 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
327 && isset($charset_of_file)) {
328 if ($charset_of_file != $charset) {
329 $charset_conversion = TRUE;
331 } elseif (PMA_MYSQL_INT_VERSION >= 40100
332 && isset($charset_of_file) && $charset_of_file != 'utf8') {
333 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
334 // We can not show query in this case, it is in different charset
335 $sql_query_disabled = TRUE;
336 $reset_charset = TRUE;
339 // Something to skip?
340 if (!$error && isset($skip)) {
341 $original_skip = $skip;
342 while ($skip > 0) {
343 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
344 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
345 $skip -= $read_limit;
347 unset($skip);
350 if (!$error) {
351 // Check for file existance
352 if (!file_exists('./libraries/import/' . $format . '.php')) {
353 $error = TRUE;
354 $message = $strCanNotLoadImportPlugins;
355 $show_error_header = TRUE;
356 } else {
357 // Do the real import
358 $plugin_param = $import_type;
359 require './libraries/import/' . $format . '.php';
363 // Cleanup temporary file
364 if ($file_to_unlink != '') {
365 unlink($file_to_unlink);
368 // Reset charset back, if we did some changes
369 if ($reset_charset) {
370 PMA_DBI_query('SET CHARACTER SET utf8');
371 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
374 // Show correct message
375 if (!empty($id_bookmark) && $action_bookmark == 2) {
376 $message = $strBookmarkDeleted;
377 $display_query = $import_text;
378 $error = FALSE; // unset error marker, it was used just to skip processing
379 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
380 $message = $strShowingBookmark;
381 } elseif ($bookmark_created) {
382 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
383 } elseif ($finished && !$error) {
384 if ($import_type == 'query') {
385 $message = $strSuccess;
386 } else {
387 $message = sprintf($strImportSuccessfullyFinished, $executed_queries);
391 // Did we hit timeout? Tell it user.
392 if ($timeout_passed) {
393 $message = $strTimeoutPassed;
394 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
395 $message .= ' ' . $strTimeoutNothingParsed;
399 // Parse and analyze the query, for correct db and table name
400 // in case of a query typed in the query window
401 require_once './libraries/parse_analyze.lib.php';
403 // There was an error?
404 if (isset($my_die)) {
405 foreach ($my_die AS $key => $die) {
406 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
410 if ($go_sql) {
411 require './sql.php';
412 } else {
413 $active_page = $goto;
414 require './' . $goto;
416 exit();