2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Core script for import, this is just the glue around all other stuff
10 * Get the variables sent or posted to this script and a core script
12 require_once './libraries/common.inc.php';
13 $GLOBALS['js_include'][] = 'functions.js';
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)) {
21 $import_text = $sql_query;
22 $import_type = 'query';
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;
31 } elseif (!empty($sql_localfile)) {
32 // run SQL file on server
33 $local_import_file = $sql_localfile;
34 $import_type = 'queryfile';
36 unset($sql_localfile);
37 } elseif (!empty($sql_file)) {
38 // run uploaded SQL file
39 $import_file = $sql_file;
40 $import_type = 'queryfile';
43 } elseif (!empty($id_bookmark)) {
45 $import_type = 'query';
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 $message = PMA_Message
::error('strUploadLimit');
54 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
55 $message->addParam('[/a]');
57 require './libraries/footer.inc.php';
60 // Check needed parameters
61 PMA_checkParameters(array('import_type', 'format'));
63 // We don't want anything special in format
64 $format = PMA_securePath($format);
67 require_once './libraries/import.lib.php';
69 // Create error and goto url
70 if ($import_type == 'table') {
71 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
72 $goto = 'tbl_import.php';
73 } elseif ($import_type == 'database') {
74 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
75 $goto = 'db_import.php';
76 } elseif ($import_type == 'server') {
77 $err_url = 'server_import.php?' . PMA_generate_common_url();
78 $goto = 'server_import.php';
80 if (empty($goto) ||
!preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
81 if (strlen($table) && strlen($db)) {
82 $goto = 'tbl_structure.php';
83 } elseif (strlen($db)) {
84 $goto = 'db_structure.php';
86 $goto = 'server_sql.php';
89 if (strlen($table) && strlen($db)) {
90 $common = PMA_generate_common_url($db, $table);
91 } elseif (strlen($db)) {
92 $common = PMA_generate_common_url($db);
94 $common = PMA_generate_common_url();
98 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ?
'&table=' . urlencode($table) : '');
103 PMA_DBI_select_db($db);
106 @set_time_limit
($cfg['ExecTimeLimit']);
107 if (!empty($cfg['MemoryLimit'])) {
108 @ini_set
('memory_limit', $cfg['MemoryLimit']);
112 if (isset($allow_interrupt)) {
113 $maximum_time = ini_get('max_execution_time');
118 // set default values
119 $timeout_passed = FALSE;
125 $file_to_unlink = '';
127 $sql_query_disabled = FALSE;
129 $executed_queries = 0;
131 $charset_conversion = FALSE;
132 $reset_charset = FALSE;
133 $bookmark_created = FALSE;
135 // Bookmark Support: get a query back from bookmark if required
136 if (!empty($id_bookmark)) {
137 require_once './libraries/bookmark.lib.php';
138 switch ($action_bookmark) {
139 case 0: // bookmarked query that have to be run
140 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark, 'id', isset($action_bookmark_all));
141 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
142 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
145 // refresh left frame on changes in table or db structure
146 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
147 $GLOBALS['reload'] = true;
151 case 1: // bookmarked query that have to be displayed
152 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
155 case 2: // bookmarked query that have to be deleted
156 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
157 PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
159 $error = TRUE; // this is kind of hack to skip processing the query
162 } // end bookmarks reading
164 // Do no run query if we show PHP code
165 if (isset($GLOBALS['show_as_php'])) {
170 // Store the query as a bookmark before executing it if bookmarklabel was given
171 if (!empty($bkm_label) && !empty($import_text)) {
172 require_once './libraries/bookmark.lib.php';
175 'user' => $cfg['Bookmark']['user'],
176 'query' => urlencode($import_text),
177 'label' => $bkm_label
180 // Should we replace bookmark?
181 if (isset($bkm_replace)) {
182 $bookmarks = PMA_listBookmarks($db, $cfg['Bookmark']);
183 foreach ($bookmarks as $key => $val) {
184 if ($val == $bkm_label) {
185 PMA_deleteBookmarks($db, $cfg['Bookmark'], $key);
190 PMA_addBookmarks($bfields, $cfg['Bookmark'], isset($bkm_all_users));
192 $bookmark_created = TRUE;
193 } // end store bookmarks
195 // We can not read all at once, otherwise we can run out of memory
196 $memory_limit = trim(@ini_get
('memory_limit'));
198 if (empty($memory_limit)) {
199 $memory_limit = 2 * 1024 * 1024;
201 // In case no memory limit we work on 10MB chunks
202 if ($memory_limit = -1) {
203 $memory_limit = 10 * 1024 * 1024;
206 // Calculate value of the limit
207 if (strtolower(substr($memory_limit, -1)) == 'm') {
208 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
209 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
210 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
211 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
212 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
214 $memory_limit = (int)$memory_limit;
217 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
220 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
222 // sanitize $local_import_file as it comes from a POST
223 $local_import_file = PMA_securePath($local_import_file);
225 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
226 } elseif (empty($import_file) ||
!is_uploaded_file($import_file)) {
227 $import_file = 'none';
230 // Do we have file to import?
231 if ($import_file != 'none' && !$error) {
232 // work around open_basedir and other limitations
233 $open_basedir = @ini_get
('open_basedir');
235 // If we are on a server with open_basedir, we must move the file
236 // before opening it. The doc explains how to create the "./tmp"
239 if (!empty($open_basedir)) {
241 $tmp_subdir = (PMA_IS_WINDOWS ?
'.\\tmp\\' : './tmp/');
243 if (is_writable($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 = PMA_Message
::error('strFileCouldNotBeRead');
258 switch ($compression) {
259 case 'application/bzip2':
260 if ($cfg['BZipDump'] && @function_exists
('bzopen')) {
261 $import_handle = @bzopen
($import_file, 'r');
263 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
264 $message->addParam($compression);
268 case 'application/gzip':
269 if ($cfg['GZipDump'] && @function_exists
('gzopen')) {
270 $import_handle = @gzopen
($import_file, 'r');
272 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
273 $message->addParam($compression);
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 = PMA_Message
::error('strNoFilesFoundInZip');
285 } elseif ($import_handle->GetError(0) != 0) {
286 $message = PMA_Message
::rawError($strErrorInZipFile . ' ' . $import_handle->GetErrorMsg(0));
289 $import_text = $import_handle->GetData(0);
291 // We don't need to store it further
294 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
295 $message->addParam($compression);
300 $import_handle = @fopen
($import_file, 'r');
303 $message = PMA_Message
::error('strUnsupportedCompressionDetected');
304 $message->addParam($compression);
309 if (!$error && $import_handle === FALSE) {
310 $message = PMA_Message
::error('strFileCouldNotBeRead');
314 if (!isset($import_text) ||
empty($import_text)) {
315 $message = PMA_Message
::error('strNoDataReceived');
320 // Convert the file's charset if necessary
321 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
322 && isset($charset_of_file)) {
323 if ($charset_of_file != $charset) {
324 $charset_conversion = TRUE;
326 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
327 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
328 // We can not show query in this case, it is in different charset
329 $sql_query_disabled = TRUE;
330 $reset_charset = TRUE;
333 // Something to skip?
334 if (!$error && isset($skip)) {
335 $original_skip = $skip;
337 PMA_importGetNextChunk($skip < $read_limit ?
$skip : $read_limit);
338 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
339 $skip -= $read_limit;
345 // Check for file existance
346 if (!file_exists('./libraries/import/' . $format . '.php')) {
348 $message = PMA_Message
::error('strCanNotLoadImportPlugins');
350 // Do the real import
351 $plugin_param = $import_type;
352 require './libraries/import/' . $format . '.php';
356 // Cleanup temporary file
357 if ($file_to_unlink != '') {
358 unlink($file_to_unlink);
361 // Reset charset back, if we did some changes
362 if ($reset_charset) {
363 PMA_DBI_query('SET CHARACTER SET utf8');
364 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
367 // Show correct message
368 if (!empty($id_bookmark) && $action_bookmark == 2) {
369 $message = PMA_Message
::success('strBookmarkDeleted');
370 $display_query = $import_text;
371 $error = FALSE; // unset error marker, it was used just to skip processing
372 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
373 $message = PMA_Message
::notice('strShowingBookmark');
374 } elseif ($bookmark_created) {
375 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
376 } elseif ($finished && !$error) {
377 if ($import_type == 'query') {
378 $message = PMA_Message
::success();
380 $message = PMA_Message
::success('strImportSuccessfullyFinished');
381 $message->addParam($executed_queries);
385 // Did we hit timeout? Tell it user.
386 if ($timeout_passed) {
387 $message = PMA_Message
::error('strTimeoutPassed');
388 if ($offset == 0 ||
(isset($original_skip) && $original_skip == $offset)) {
389 $message->addString('strTimeoutNothingParsed');
393 // Parse and analyze the query, for correct db and table name
394 // in case of a query typed in the query window
395 require_once './libraries/parse_analyze.lib.php';
397 // There was an error?
398 if (isset($my_die)) {
399 foreach ($my_die AS $key => $die) {
400 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
407 $active_page = $goto;
408 require './' . $goto;