2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Core script for import, this is just the glue around all other stuff
6 * @uses PMA_Bookmark_getList()
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;
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)) {
28 $import_text = $sql_query;
29 $import_type = 'query';
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;
38 } elseif (!empty($sql_localfile)) {
39 // run SQL file on server
40 $local_import_file = $sql_localfile;
41 $import_type = 'queryfile';
43 unset($sql_localfile);
44 } elseif (!empty($sql_file)) {
45 // run uploaded SQL file
46 $import_file = $sql_file;
47 $import_type = 'queryfile';
50 } elseif (!empty($id_bookmark)) {
52 $import_type = 'query';
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;
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);
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';
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';
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);
110 $common = PMA_generate_common_url();
114 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ?
'&table=' . urlencode($table) : '');
115 $_SESSION['Import_message']['go_back_url'] = $err_url;
120 PMA_DBI_select_db($db);
123 @set_time_limit
($cfg['ExecTimeLimit']);
124 if (!empty($cfg['MemoryLimit'])) {
125 @ini_set
('memory_limit', $cfg['MemoryLimit']);
129 if (isset($allow_interrupt)) {
130 $maximum_time = ini_get('max_execution_time');
135 // set default values
136 $timeout_passed = FALSE;
142 $file_to_unlink = '';
144 $sql_query_disabled = FALSE;
146 $executed_queries = 0;
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 $id_bookmark = (int)$id_bookmark;
155 require_once './libraries/bookmark.lib.php';
156 switch ($action_bookmark) {
157 case 0: // bookmarked query that have to be run
158 $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
159 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
160 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
163 // refresh left frame on changes in table or db structure
164 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
165 $GLOBALS['reload'] = true;
169 case 1: // bookmarked query that have to be displayed
170 $import_text = PMA_Bookmark_get($db, $id_bookmark);
173 case 2: // bookmarked query that have to be deleted
174 $import_text = PMA_Bookmark_get($db, $id_bookmark);
175 PMA_Bookmark_delete($db, $id_bookmark);
177 $error = TRUE; // this is kind of hack to skip processing the query
180 } // end bookmarks reading
182 // Do no run query if we show PHP code
183 if (isset($GLOBALS['show_as_php'])) {
188 // Store the query as a bookmark before executing it if bookmarklabel was given
189 if (!empty($bkm_label) && !empty($import_text)) {
190 require_once './libraries/bookmark.lib.php';
193 'user' => $cfg['Bookmark']['user'],
194 'query' => urlencode($import_text),
195 'label' => $bkm_label
198 // Should we replace bookmark?
199 if (isset($bkm_replace)) {
200 $bookmarks = PMA_Bookmark_getList($db);
201 foreach ($bookmarks as $key => $val) {
202 if ($val == $bkm_label) {
203 PMA_Bookmark_delete($db, $key);
208 PMA_Bookmark_save($bfields, isset($bkm_all_users));
210 $bookmark_created = TRUE;
211 } // end store bookmarks
213 // We can not read all at once, otherwise we can run out of memory
214 $memory_limit = trim(@ini_get
('memory_limit'));
216 if (empty($memory_limit)) {
217 $memory_limit = 2 * 1024 * 1024;
219 // In case no memory limit we work on 10MB chunks
220 if ($memory_limit == -1) {
221 $memory_limit = 10 * 1024 * 1024;
224 // Calculate value of the limit
225 if (strtolower(substr($memory_limit, -1)) == 'm') {
226 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
227 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
228 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
229 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
230 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
232 $memory_limit = (int)$memory_limit;
235 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
238 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
240 // sanitize $local_import_file as it comes from a POST
241 $local_import_file = PMA_securePath($local_import_file);
243 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
244 } elseif (empty($import_file) ||
!is_uploaded_file($import_file)) {
245 $import_file = 'none';
248 // Do we have file to import?
250 if ($import_file != 'none' && !$error) {
251 // work around open_basedir and other limitations
252 $open_basedir = @ini_get
('open_basedir');
254 // If we are on a server with open_basedir, we must move the file
255 // before opening it. The doc explains how to create the "./tmp"
258 if (!empty($open_basedir)) {
260 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
262 if (is_writable($tmp_subdir)) {
265 $import_file_new = $tmp_subdir . basename($import_file);
266 if (move_uploaded_file($import_file, $import_file_new)) {
267 $import_file = $import_file_new;
268 $file_to_unlink = $import_file_new;
271 $size = filesize($import_file);
276 * Handle file compression
277 * @todo duplicate code exists in File.class.php
279 $compression = PMA_detectCompression($import_file);
280 if ($compression === FALSE) {
281 $message = PMA_Message
::error('strFileCouldNotBeRead');
284 switch ($compression) {
285 case 'application/bzip2':
286 if ($cfg['BZipDump'] && @function_exists
('bzopen')) {
287 $import_handle = @bzopen
($import_file, 'r');
289 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
290 $message->addParam($compression);
294 case 'application/gzip':
295 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
296 $import_handle = @gzopen
($import_file, 'r');
298 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
299 $message->addParam($compression);
303 case 'application/zip':
304 if ($cfg['ZipDump'] && @function_exists
('zip_open')) {
306 * Load interface for zip extension.
308 include_once './libraries/zip_extension.lib.php';
309 $result = PMA_getZipContents($import_file);
310 if (! empty($result['error'])) {
311 $message = PMA_Message
::rawError($result['error']);
314 $import_text = $result['data'];
317 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
318 $message->addParam($compression);
323 $import_handle = @fopen
($import_file, 'r');
326 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
327 $message->addParam($compression);
332 // use isset() because zip compression type does not use a handle
333 if (!$error && isset($import_handle) && $import_handle === FALSE) {
334 $message = PMA_Message
::error('strFileCouldNotBeRead');
338 if (!isset($import_text) ||
empty($import_text)) {
339 $message = PMA_Message
::error('strNoDataReceived');
344 // so we can obtain the message
345 //$_SESSION['Import_message'] = $message->getDisplay();
347 // Convert the file's charset if necessary
348 if ($cfg['AllowAnywhereRecoding'] && isset($charset_of_file)) {
349 if ($charset_of_file != $charset) {
350 $charset_conversion = TRUE;
352 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
353 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
354 // We can not show query in this case, it is in different charset
355 $sql_query_disabled = TRUE;
356 $reset_charset = TRUE;
359 // Something to skip?
360 if (!$error && isset($skip)) {
361 $original_skip = $skip;
363 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
364 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
365 $skip -= $read_limit;
371 // Check for file existance
372 if (!file_exists('./libraries/import/' . $format . '.php')) {
374 $message = PMA_Message
::error('strCanNotLoadImportPlugins');
376 // Do the real import
377 $plugin_param = $import_type;
378 require './libraries/import/' . $format . '.php';
382 if (! $error && FALSE !== $import_handle && NULL !== $import_handle) {
383 fclose($import_handle);
386 // Cleanup temporary file
387 if ($file_to_unlink != '') {
388 unlink($file_to_unlink);
391 // Reset charset back, if we did some changes
392 if ($reset_charset) {
393 PMA_DBI_query('SET CHARACTER SET utf8');
394 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
397 // Show correct message
398 if (!empty($id_bookmark) && $action_bookmark == 2) {
399 $message = PMA_Message
::success('strBookmarkDeleted');
400 $display_query = $import_text;
401 $error = FALSE; // unset error marker, it was used just to skip processing
402 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
403 $message = PMA_Message
::notice('strShowingBookmark');
404 } elseif ($bookmark_created) {
405 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
406 } elseif ($finished && !$error) {
407 if ($import_type == 'query') {
408 $message = PMA_Message
::success();
410 if ($import_notice) {
411 $message = PMA_Message
::success('<em>'.$GLOBALS['strImportSuccessfullyFinished'].'</em>');
412 $message->addParam($executed_queries);
414 $message->addString($import_notice);
416 $message = PMA_Message
::success('strImportSuccessfullyFinished');
417 $message->addParam($executed_queries);
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 // we want to see the results of the last query that returned at least a row
451 if (! empty($last_query_with_results)) {
452 // but we want to show intermediate results too
453 $disp_query = $sql_query;
454 $disp_message = $strSuccess;
455 $sql_query = $last_query_with_results;
462 $active_page = $goto;
463 require './' . $goto;