patch #2735162 [status] Server status - replication
[phpmyadmin/crack.git] / import.php
bloba361b1ba7854846b7dce2da78e5fb428cbf089d4
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 $GLOBALS['js_include'][] = 'functions.js';
17 // default values
18 $GLOBALS['reload'] = false;
20 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
21 if (!empty($sql_query)) {
22 // run SQL query
23 $import_text = $sql_query;
24 $import_type = 'query';
25 $format = 'sql';
27 // refresh left frame on changes in table or db structure
28 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
29 $GLOBALS['reload'] = true;
32 $sql_query = '';
33 } elseif (!empty($sql_localfile)) {
34 // run SQL file on server
35 $local_import_file = $sql_localfile;
36 $import_type = 'queryfile';
37 $format = 'sql';
38 unset($sql_localfile);
39 } elseif (!empty($sql_file)) {
40 // run uploaded SQL file
41 $import_file = $sql_file;
42 $import_type = 'queryfile';
43 $format = 'sql';
44 unset($sql_file);
45 } elseif (!empty($id_bookmark)) {
46 // run bookmark
47 $import_type = 'query';
48 $format = 'sql';
51 // If we didn't get any parameters, either user called this directly, or
52 // upload limit has been reached, let's assume the second possibility.
53 if ($_POST == array() && $_GET == array()) {
54 require_once './libraries/header.inc.php';
55 $message = PMA_Message::error('strUploadLimit');
56 $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
57 $message->addParam('[/a]');
58 $message->display();
59 require './libraries/footer.inc.php';
62 // Check needed parameters
63 PMA_checkParameters(array('import_type', 'format'));
65 // We don't want anything special in format
66 $format = PMA_securePath($format);
68 // Import functions
69 require_once './libraries/import.lib.php';
71 // Create error and goto url
72 if ($import_type == 'table') {
73 $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
74 $goto = 'tbl_import.php';
75 } elseif ($import_type == 'database') {
76 $err_url = 'db_import.php?' . PMA_generate_common_url($db);
77 $goto = 'db_import.php';
78 } elseif ($import_type == 'server') {
79 $err_url = 'server_import.php?' . PMA_generate_common_url();
80 $goto = 'server_import.php';
81 } else {
82 if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
83 if (strlen($table) && strlen($db)) {
84 $goto = 'tbl_structure.php';
85 } elseif (strlen($db)) {
86 $goto = 'db_structure.php';
87 } else {
88 $goto = 'server_sql.php';
91 if (strlen($table) && strlen($db)) {
92 $common = PMA_generate_common_url($db, $table);
93 } elseif (strlen($db)) {
94 $common = PMA_generate_common_url($db);
95 } else {
96 $common = PMA_generate_common_url();
98 $err_url = $goto
99 . '?' . $common
100 . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
104 if (strlen($db)) {
105 PMA_DBI_select_db($db);
108 @set_time_limit($cfg['ExecTimeLimit']);
109 if (!empty($cfg['MemoryLimit'])) {
110 @ini_set('memory_limit', $cfg['MemoryLimit']);
113 $timestamp = time();
114 if (isset($allow_interrupt)) {
115 $maximum_time = ini_get('max_execution_time');
116 } else {
117 $maximum_time = 0;
120 // set default values
121 $timeout_passed = FALSE;
122 $error = FALSE;
123 $read_multiply = 1;
124 $finished = FALSE;
125 $offset = 0;
126 $max_sql_len = 0;
127 $file_to_unlink = '';
128 $sql_query = '';
129 $sql_query_disabled = FALSE;
130 $go_sql = FALSE;
131 $executed_queries = 0;
132 $run_query = TRUE;
133 $charset_conversion = FALSE;
134 $reset_charset = FALSE;
135 $bookmark_created = FALSE;
137 // Bookmark Support: get a query back from bookmark if required
138 if (!empty($id_bookmark)) {
139 require_once './libraries/bookmark.lib.php';
140 switch ($action_bookmark) {
141 case 0: // bookmarked query that have to be run
142 $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
143 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
144 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
147 // refresh left frame on changes in table or db structure
148 if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
149 $GLOBALS['reload'] = true;
152 break;
153 case 1: // bookmarked query that have to be displayed
154 $import_text = PMA_Bookmark_get($db, $id_bookmark);
155 $run_query = FALSE;
156 break;
157 case 2: // bookmarked query that have to be deleted
158 $import_text = PMA_Bookmark_get($db, $id_bookmark);
159 PMA_Bookmark_delete($db, $id_bookmark);
160 $run_query = FALSE;
161 $error = TRUE; // this is kind of hack to skip processing the query
162 break;
164 } // end bookmarks reading
166 // Do no run query if we show PHP code
167 if (isset($GLOBALS['show_as_php'])) {
168 $run_query = FALSE;
169 $go_sql = TRUE;
172 // Store the query as a bookmark before executing it if bookmarklabel was given
173 if (!empty($bkm_label) && !empty($import_text)) {
174 require_once './libraries/bookmark.lib.php';
175 $bfields = array(
176 'dbase' => $db,
177 'user' => $cfg['Bookmark']['user'],
178 'query' => urlencode($import_text),
179 'label' => $bkm_label
182 // Should we replace bookmark?
183 if (isset($bkm_replace)) {
184 $bookmarks = PMA_Bookmark_getList($db);
185 foreach ($bookmarks as $key => $val) {
186 if ($val == $bkm_label) {
187 PMA_Bookmark_delete($db, $key);
192 PMA_Bookmark_save($bfields, isset($bkm_all_users));
194 $bookmark_created = TRUE;
195 } // end store bookmarks
197 // We can not read all at once, otherwise we can run out of memory
198 $memory_limit = trim(@ini_get('memory_limit'));
199 // 2 MB as default
200 if (empty($memory_limit)) {
201 $memory_limit = 2 * 1024 * 1024;
203 // In case no memory limit we work on 10MB chunks
204 if ($memory_limit == -1) {
205 $memory_limit = 10 * 1024 * 1024;
208 // Calculate value of the limit
209 if (strtolower(substr($memory_limit, -1)) == 'm') {
210 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
211 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
212 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
213 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
214 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
215 } else {
216 $memory_limit = (int)$memory_limit;
219 $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
221 // handle filenames
222 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
224 // sanitize $local_import_file as it comes from a POST
225 $local_import_file = PMA_securePath($local_import_file);
227 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
228 } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
229 $import_file = 'none';
232 // Do we have file to import?
233 if ($import_file != 'none' && !$error) {
234 // work around open_basedir and other limitations
235 $open_basedir = @ini_get('open_basedir');
237 // If we are on a server with open_basedir, we must move the file
238 // before opening it. The doc explains how to create the "./tmp"
239 // directory
241 if (!empty($open_basedir)) {
243 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
245 if (is_writable($tmp_subdir)) {
246 $import_file_new = $tmp_subdir . basename($import_file);
247 if (move_uploaded_file($import_file, $import_file_new)) {
248 $import_file = $import_file_new;
249 $file_to_unlink = $import_file_new;
255 * Handle file compression
256 * @todo duplicate code exists in File.class.php
258 $compression = PMA_detectCompression($import_file);
259 if ($compression === FALSE) {
260 $message = PMA_Message::error('strFileCouldNotBeRead');
261 $error = TRUE;
262 } else {
263 switch ($compression) {
264 case 'application/bzip2':
265 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
266 $import_handle = @bzopen($import_file, 'r');
267 } else {
268 $message = PMA_Message::error('strUnsupportedCompressionDetected');
269 $message->addParam($compression);
270 $error = TRUE;
272 break;
273 case 'application/gzip':
274 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
275 $import_handle = @gzopen($import_file, 'r');
276 } else {
277 $message = PMA_Message::error('strUnsupportedCompressionDetected');
278 $message->addParam($compression);
279 $error = TRUE;
281 break;
282 case 'application/zip':
283 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
285 * Load interface for zip extension.
287 include_once './libraries/zip_extension.lib.php';
288 $result = PMA_getZipContents($import_file);
289 if (! empty($result['error'])) {
290 $message = PMA_Message::rawError($result['error']);
291 $error = TRUE;
292 } else {
293 $import_text = $result['data'];
295 } else {
296 $message = PMA_Message::error('strUnsupportedCompressionDetected');
297 $message->addParam($compression);
298 $error = TRUE;
300 break;
301 case 'none':
302 $import_handle = @fopen($import_file, 'r');
303 break;
304 default:
305 $message = PMA_Message::error('strUnsupportedCompressionDetected');
306 $message->addParam($compression);
307 $error = TRUE;
308 break;
311 if (!$error && $import_handle === FALSE) {
312 $message = PMA_Message::error('strFileCouldNotBeRead');
313 $error = TRUE;
315 } elseif (!$error) {
316 if (!isset($import_text) || empty($import_text)) {
317 $message = PMA_Message::error('strNoDataReceived');
318 $error = TRUE;
322 // Convert the file's charset if necessary
323 if ($cfg['AllowAnywhereRecoding'] && isset($charset_of_file)) {
324 if ($charset_of_file != $charset) {
325 $charset_conversion = TRUE;
327 } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
328 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
329 // We can not show query in this case, it is in different charset
330 $sql_query_disabled = TRUE;
331 $reset_charset = TRUE;
334 // Something to skip?
335 if (!$error && isset($skip)) {
336 $original_skip = $skip;
337 while ($skip > 0) {
338 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
339 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
340 $skip -= $read_limit;
342 unset($skip);
345 if (!$error) {
346 // Check for file existance
347 if (!file_exists('./libraries/import/' . $format . '.php')) {
348 $error = TRUE;
349 $message = PMA_Message::error('strCanNotLoadImportPlugins');
350 } else {
351 // Do the real import
352 $plugin_param = $import_type;
353 require './libraries/import/' . $format . '.php';
357 if (! $error && FALSE !== $import_handle && NULL !== $import_handle) {
358 fclose($import_handle);
361 // Cleanup temporary file
362 if ($file_to_unlink != '') {
363 unlink($file_to_unlink);
366 // Reset charset back, if we did some changes
367 if ($reset_charset) {
368 PMA_DBI_query('SET CHARACTER SET utf8');
369 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
372 // Show correct message
373 if (!empty($id_bookmark) && $action_bookmark == 2) {
374 $message = PMA_Message::success('strBookmarkDeleted');
375 $display_query = $import_text;
376 $error = FALSE; // unset error marker, it was used just to skip processing
377 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
378 $message = PMA_Message::notice('strShowingBookmark');
379 } elseif ($bookmark_created) {
380 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
381 } elseif ($finished && !$error) {
382 if ($import_type == 'query') {
383 $message = PMA_Message::success();
384 } else {
385 $message = PMA_Message::success('strImportSuccessfullyFinished');
386 $message->addParam($executed_queries);
390 // Did we hit timeout? Tell it user.
391 if ($timeout_passed) {
392 $message = PMA_Message::error('strTimeoutPassed');
393 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
394 $message->addString('strTimeoutNothingParsed');
398 // Parse and analyze the query, for correct db and table name
399 // in case of a query typed in the query window
400 require_once './libraries/parse_analyze.lib.php';
402 // There was an error?
403 if (isset($my_die)) {
404 foreach ($my_die AS $key => $die) {
405 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
409 if ($go_sql) {
410 require './sql.php';
411 } else {
412 $active_page = $goto;
413 require './' . $goto;
415 exit();