remove old todo when creating a release
[phpmyadmin/crack.git] / import.php
blob77aae0ca79c0432a932acf65c273f49ba8312cd2
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 $GLOBALS['js_include'][] = '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 $message = PMA_Message::error('strUploadLimit');
54 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
55 $message->addParam('[/a]');
56 $message->display();
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);
66 // Import functions
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';
79 } else {
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';
85 } else {
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);
93 } else {
94 $common = PMA_generate_common_url();
96 $err_url = $goto
97 . '?' . $common
98 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
102 if (strlen($db)) {
103 PMA_DBI_select_db($db);
106 @set_time_limit($cfg['ExecTimeLimit']);
107 if (!empty($cfg['MemoryLimit'])) {
108 @ini_set('memory_limit', $cfg['MemoryLimit']);
111 $timestamp = time();
112 if (isset($allow_interrupt)) {
113 $maximum_time = ini_get('max_execution_time');
114 } else {
115 $maximum_time = 0;
118 // set default values
119 $timeout_passed = FALSE;
120 $error = FALSE;
121 $read_multiply = 1;
122 $finished = FALSE;
123 $offset = 0;
124 $max_sql_len = 0;
125 $file_to_unlink = '';
126 $sql_query = '';
127 $sql_query_disabled = FALSE;
128 $go_sql = FALSE;
129 $executed_queries = 0;
130 $run_query = TRUE;
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;
150 break;
151 case 1: // bookmarked query that have to be displayed
152 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
153 $run_query = FALSE;
154 break;
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);
158 $run_query = FALSE;
159 $error = TRUE; // this is kind of hack to skip processing the query
160 break;
162 } // end bookmarks reading
164 // Do no run query if we show PHP code
165 if (isset($GLOBALS['show_as_php'])) {
166 $run_query = FALSE;
167 $go_sql = TRUE;
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';
173 $bfields = array(
174 'dbase' => $db,
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'));
197 // 2 MB as default
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;
213 } else {
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
219 // handle filenames
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"
237 // directory
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');
256 $error = TRUE;
257 } else {
258 switch ($compression) {
259 case 'application/bzip2':
260 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
261 $import_handle = @bzopen($import_file, 'r');
262 } else {
263 $message = PMA_Message::error('strUnsupportedCompressionDetected');
264 $message->addParam($compression);
265 $error = TRUE;
267 break;
268 case 'application/gzip':
269 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
270 $import_handle = @gzopen($import_file, 'r');
271 } else {
272 $message = PMA_Message::error('strUnsupportedCompressionDetected');
273 $message->addParam($compression);
274 $error = TRUE;
276 break;
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');
284 $error = TRUE;
285 } elseif ($import_handle->GetError(0) != 0) {
286 $message = PMA_Message::rawError($strErrorInZipFile . ' ' . $import_handle->GetErrorMsg(0));
287 $error = TRUE;
288 } else {
289 $import_text = $import_handle->GetData(0);
291 // We don't need to store it further
292 $import_handle = '';
293 } else {
294 $message = PMA_Message::error('strUnsupportedCompressionDetected');
295 $message->addParam($compression);
296 $error = TRUE;
298 break;
299 case 'none':
300 $import_handle = @fopen($import_file, 'r');
301 break;
302 default:
303 $message = PMA_Message::error('strUnsupportedCompressionDetected');
304 $message->addParam($compression);
305 $error = TRUE;
306 break;
309 if (!$error && $import_handle === FALSE) {
310 $message = PMA_Message::error('strFileCouldNotBeRead');
311 $error = TRUE;
313 } elseif (!$error) {
314 if (!isset($import_text) || empty($import_text)) {
315 $message = PMA_Message::error('strNoDataReceived');
316 $error = TRUE;
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;
336 while ($skip > 0) {
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;
341 unset($skip);
344 if (!$error) {
345 // Check for file existance
346 if (!file_exists('./libraries/import/' . $format . '.php')) {
347 $error = TRUE;
348 $message = PMA_Message::error('strCanNotLoadImportPlugins');
349 } else {
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();
379 } else {
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);
404 if ($go_sql) {
405 require './sql.php';
406 } else {
407 $active_page = $goto;
408 require './' . $goto;
410 exit();