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