3 // vim: expandtab sw=4 ts=4 sts=4:
5 /* Core script for import, this is just the glue around all other stuff */
8 * Get the variables sent or posted to this script and a core script
10 require_once('./libraries/common.lib.php');
11 $js_to_run = 'functions.js';
13 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
14 if (!empty($sql_query)) {
16 $import_text = $sql_query;
17 $import_type = 'query';
20 } elseif (!empty($sql_localfile)) {
21 // run SQL file on server
22 $local_import_file = $sql_localfile;
23 $import_type = 'queryfile';
25 unset($sql_localfile);
26 } elseif (!empty($sql_file)) {
27 // run uploaded SQL file
28 $import_file = $sql_file;
29 $import_type = 'queryfile';
32 } elseif (!empty($id_bookmark)) {
34 $import_type = 'query';
38 // If we didn't get any parameters, either user called this directly, or
39 // upload limit has been reached, let's assume the second possibility.
40 if ($_POST == array() && $_GET == array()) {
41 require_once('./header.inc.php');
42 $show_error_header = TRUE;
43 PMA_showMessage(sprintf($strUploadLimit, '[a@./Documentation.html#faq1_16@_blank]', '[/a]'));
44 require('./footer.inc.php');
47 // Check needed parameters
48 PMA_checkParameters(array('import_type', 'what'));
50 // We don't want anything special in what
51 $what = PMA_securePath($what);
54 require_once('./libraries/import.lib.php');
56 // Create error and goto url
57 if ($import_type == 'table') {
58 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
59 $goto = 'tbl_import.php';
60 } elseif ($import_type == 'database') {
61 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
62 $goto = 'db_import.php';
63 } elseif ($import_type == 'server') {
64 $err_url = 'server_import.php?' . PMA_generate_common_url();
65 $goto = 'server_import.php';
67 if (empty($goto) ||
!preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
68 if (isset($table) && isset($db)) {
69 $goto = 'tbl_properties_structure.php';
70 } elseif (isset($db)) {
71 $goto = 'db_details_structure.php';
73 $goto = 'server_sql.php';
76 if (isset($table) && isset($db)) {
77 $common = PMA_generate_common_url($db, $table);
78 } elseif (isset($db)) {
79 $common = PMA_generate_common_url($db);
81 $common = PMA_generate_common_url();
85 . (preg_match('@^tbl_properties(_[a-z]*)?\.php$@', $goto) ?
'&table=' . urlencode($table) : '');
90 PMA_DBI_select_db($db);
93 @set_time_limit
($cfg['ExecTimeLimit']);
95 if (isset($allow_interrupt)) {
96 $maximum_time = ini_get('max_execution_time');
101 // set default values
102 $timeout_passed = FALSE;
108 $file_to_unlink = '';
110 $sql_query_disabled = FALSE;
113 $executed_queries = 0;
115 $charset_conversion = FALSE;
116 $reset_charset = FALSE;
117 $bookmark_created = FALSE;
119 // Bookmark Support: get a query back from bookmark if required
120 if (!empty($id_bookmark)) {
121 require_once('./libraries/bookmark.lib.php');
122 switch ($action_bookmark) {
123 case 0: // bookmarked query that have to be run
124 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark,'id', isset($action_bookmark_all));
125 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
126 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
129 case 1: // bookmarked query that have to be displayed
130 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
133 case 2: // bookmarked query that have to be deleted
134 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
135 PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
137 $error = TRUE; // this is kind of hack to skip processing the query
140 } // end bookmarks reading
142 // Store the query as a bookmark before executing it if bookmarklabel was given
143 if (!empty($bkm_label) && !empty($import_text)) {
144 require_once('./libraries/bookmark.lib.php');
147 'user' => $cfg['Bookmark']['user'],
148 'query' => urlencode($import_text),
149 'label' => $bkm_label
152 // Should we replace bookmark?
153 if (isset($bkm_replace)) {
154 $bookmarks = PMA_listBookmarks($db, $cfg['Bookmark']);
155 foreach($bookmarks as $key => $val) {
156 if ($val == $bkm_label) {
157 PMA_deleteBookmarks($db, $cfg['Bookmark'], $key);
162 PMA_addBookmarks($bfields, $cfg['Bookmark'], isset($bkm_all_users));
164 $bookmark_created = TRUE;
165 } // end store bookmarks
167 // We can not read all at once, otherwise we can run out of memory
168 $memory_limit = trim(@ini_get
('memory_limit'));
170 if (empty($memory_limit)) $memory_limit = 2 * 1024 * 1024;
172 // Calculate value of the limit
173 if (strtolower(substr($memory_limit, -1)) == 'm') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
174 elseif (strtolower(substr($memory_limit, -1)) == 'k') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
175 elseif (strtolower(substr($memory_limit, -1)) == 'g') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
176 else $memory_limit = (int)$memory_limit;
178 $read_limit = $memory_limit / 4; // Just to be sure, there might be lot of memory needed for uncompression
181 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
183 // sanitize $local_import_file as it comes from a POST
184 $local_import_file = PMA_securePath($local_import_file);
186 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
187 } else if (empty($import_file) ||
!is_uploaded_file($import_file)) {
188 $import_file = 'none';
191 // Do we have file to import?
192 if ($import_file != 'none' && !$error) {
193 // work around open_basedir and other limitations
194 $open_basedir = @ini_get
('open_basedir');
196 // If we are on a server with open_basedir, we must move the file
197 // before opening it. The doc explains how to create the "./tmp"
200 if (!empty($open_basedir)) {
202 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
204 // function is_writeable() is valid on PHP3 and 4
205 if (is_writeable($tmp_subdir)) {
206 $import_file_new = $tmp_subdir . basename($import_file);
207 if (move_uploaded_file($import_file, $import_file_new)) {
208 $import_file = $import_file_new;
209 $file_to_unlink = $import_file_new;
214 // Handle file compression
215 $compression = PMA_detectCompression($import_file);
216 if ($compression === FALSE) {
217 $message = $strFileCouldNotBeRead;
218 $show_error_header = TRUE;
221 switch ($compression) {
222 case 'application/bzip2':
223 if ($cfg['BZipDump'] && @function_exists
('bzopen')) {
224 $import_handle = @bzopen
($import_file, 'r');
226 $message = sprintf($strUnsupportedCompressionDetected, $compression);
227 $show_error_header = TRUE;
231 case 'application/gzip':
232 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
233 $import_handle = @gzopen
($import_file, 'r');
235 $message = sprintf($strUnsupportedCompressionDetected, $compression);
236 $show_error_header = TRUE;
240 case 'application/zip':
241 if ($cfg['GZipDump'] && @function_exists
('gzinflate')) {
242 include_once('./libraries/unzip.lib.php');
243 $import_handle = new SimpleUnzip();
244 $import_handle->ReadFile($import_file);
245 if ($import_handle->Count() == 0) {
246 $message = $strNoFilesFoundInZip;
247 $show_error_header = TRUE;
249 } elseif ($import_handle->GetError(0) != 0) {
250 $message = $strErrorInZipFile . ' ' . $import_handle->GetErrorMsg(0);
251 $show_error_header = TRUE;
254 $import_text = $import_handle->GetData(0);
256 // We don't need to store it further
259 $message = sprintf($strUnsupportedCompressionDetected, $compression);
260 $show_error_header = TRUE;
265 $import_handle = @fopen
($import_file, 'r');
268 $message = sprintf($strUnsupportedCompressionDetected, $compression);
269 $show_error_header = TRUE;
274 if (!$error && $import_handle === FALSE) {
275 $message = $strFileCouldNotBeRead;
276 $show_error_header = TRUE;
280 if (!isset($import_text) ||
empty($import_text)) {
281 $message = $strNothingToImport;
282 $show_error_header = TRUE;
287 // Convert the file's charset if necessary
288 if (PMA_MYSQL_INT_VERSION
< 40100
289 && $cfg['AllowAnywhereRecoding'] && $allow_recoding
290 && isset($charset_of_file) && $charset_of_file != $charset) {
291 $charset_conversion = TRUE;
292 } else if (PMA_MYSQL_INT_VERSION
>= 40100
293 && isset($charset_of_file) && $charset_of_file != 'utf8') {
294 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
295 // We can not show query in this case, it is in different charset
296 $sql_query_disabled = TRUE;
297 $reset_charset = TRUE;
300 // Something to skip?
301 if (!$error && isset($skip)) {
302 $original_skip = $skip;
304 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
305 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
306 $skip -= $read_limit;
312 // Check for file existance
313 if (!file_exists('./libraries/import/' . $what . '.php')) {
315 $message = $strCanNotLoadImportPlugins;
316 $show_error_header = TRUE;
318 // Do the real import
319 require('./libraries/import/' . $what . '.php');
323 // Cleanup temporary file
324 if ($file_to_unlink != '') {
325 unlink($file_to_unlink);
328 // Reset charset back, if we did some changes
329 if ($reset_charset) {
330 PMA_DBI_query('SET CHARACTER SET utf8');
331 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
334 // Show correct message
335 if (!empty($id_bookmark) && $action_bookmark == 2) {
336 $message = $strBookmarkDeleted;
337 $display_query = $import_text;
338 $error = FALSE; // unset error marker, it was used just to skip processing
339 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
340 $message = $strShowingBookmark;
341 } elseif ($bookmark_created) {
342 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
343 } elseif ($finished && !$error) {
344 if ($import_type == 'query') {
345 $message = $strSuccess;
347 $message = $strImportFinished;
351 // Did we hit timeout? Tell it user.
352 if ($timeout_passed) {
353 $message = $strTimeoutPassed;
354 if ($offset == 0 ||
(isset($original_skip) && $original_skip == $offset)) {
355 $message .= ' ' . $strTimeoutNothingParsed;
359 // Display back import page
360 require_once('./header.inc.php');
362 // There was an error?
363 if (isset($my_die)) {
364 foreach ($my_die AS $key => $die) {
365 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
371 // Set pos to zero to possibly append limit
373 require('./sql.php');
375 $active_page = $goto;
376 require('./' . $goto);