Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: import.php,v 2.17.2.2 2006/03/04 12:41:28 lem9 Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /* Core script for import, this is just the glue around all other stuff */
6  
7 /**
8 * Get the variables sent or posted to this script and a core script
9 */
10 require_once('./libraries/common.lib.php');
11 $js_to_run = 'functions.js';
12  
13 // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
14 if (!empty($sql_query)) {
15 // run SQL query
16 $import_text = $sql_query;
17 $import_type = 'query';
18 $format = 'sql';
19 unset($sql_query);
20 } elseif (!empty($sql_localfile)) {
21 // run SQL file on server
22 $local_import_file = $sql_localfile;
23 $import_type = 'queryfile';
24 $format = 'sql';
25 unset($sql_localfile);
26 } elseif (!empty($sql_file)) {
27 // run uploaded SQL file
28 $import_file = $sql_file;
29 $import_type = 'queryfile';
30 $format = 'sql';
31 unset($sql_file);
32 } elseif (!empty($id_bookmark)) {
33 // run bookmark
34 $import_type = 'query';
35 $format = 'sql';
36 }
37  
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('./libraries/header.inc.php');
42 $show_error_header = TRUE;
43 PMA_showMessage(sprintf($strUploadLimit, '[a@./Documentation.html#faq1_16@_blank]', '[/a]'));
44 require('./libraries/footer.inc.php');
45 }
46  
47 // Check needed parameters
48 PMA_checkParameters(array('import_type', 'format'));
49  
50 // We don't want anything special in format
51 $format = PMA_securePath($format);
52  
53 // Import functions
54 require_once('./libraries/import.lib.php');
55  
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';
66 } else {
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';
72 } else {
73 $goto = 'server_sql.php';
74 }
75 }
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);
80 } else {
81 $common = PMA_generate_common_url();
82 }
83 $err_url = $goto
84 . '?' . $common
85 . (preg_match('@^tbl_properties(_[a-z]*)?\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
86 }
87  
88  
89 if (isset($db)) {
90 PMA_DBI_select_db($db);
91 }
92  
93 @set_time_limit($cfg['ExecTimeLimit']);
94 if (!empty($cfg['MemoryLimit'])) {
95 @ini_set('memory_limit', $cfg['MemoryLimit']);
96 }
97  
98 $timestamp = time();
99 if (isset($allow_interrupt)) {
100 $maximum_time = ini_get('max_execution_time');
101 } else {
102 $maximum_time = 0;
103 }
104  
105 // set default values
106 $timeout_passed = FALSE;
107 $error = FALSE;
108 $read_multiply = 1;
109 $finished = FALSE;
110 $offset = 0;
111 $max_sql_len = 0;
112 $file_to_unlink = '';
113 $sql_query = '';
114 $sql_query_disabled = FALSE;
115 $go_sql = FALSE;
116 $reload = FALSE;
117 $executed_queries = 0;
118 $run_query = TRUE;
119 $charset_conversion = FALSE;
120 $reset_charset = FALSE;
121 $bookmark_created = FALSE;
122  
123 // Bookmark Support: get a query back from bookmark if required
124 if (!empty($id_bookmark)) {
125 require_once('./libraries/bookmark.lib.php');
126 switch ($action_bookmark) {
127 case 0: // bookmarked query that have to be run
128 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark, 'id', isset($action_bookmark_all));
129 if (isset($bookmark_variable) && !empty($bookmark_variable)) {
130 $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
131 }
132 break;
133 case 1: // bookmarked query that have to be displayed
134 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
135 $run_query = FALSE;
136 break;
137 case 2: // bookmarked query that have to be deleted
138 $import_text = PMA_queryBookmarks($db, $cfg['Bookmark'], $id_bookmark);
139 PMA_deleteBookmarks($db, $cfg['Bookmark'], $id_bookmark);
140 $run_query = FALSE;
141 $error = TRUE; // this is kind of hack to skip processing the query
142 break;
143 }
144 } // end bookmarks reading
145  
146 // Store the query as a bookmark before executing it if bookmarklabel was given
147 if (!empty($bkm_label) && !empty($import_text)) {
148 require_once('./libraries/bookmark.lib.php');
149 $bfields = array(
150 'dbase' => $db,
151 'user' => $cfg['Bookmark']['user'],
152 'query' => urlencode($import_text),
153 'label' => $bkm_label
154 );
155  
156 // Should we replace bookmark?
157 if (isset($bkm_replace)) {
158 $bookmarks = PMA_listBookmarks($db, $cfg['Bookmark']);
159 foreach ($bookmarks as $key => $val) {
160 if ($val == $bkm_label) {
161 PMA_deleteBookmarks($db, $cfg['Bookmark'], $key);
162 }
163 }
164 }
165  
166 PMA_addBookmarks($bfields, $cfg['Bookmark'], isset($bkm_all_users));
167  
168 $bookmark_created = TRUE;
169 } // end store bookmarks
170  
171 // We can not read all at once, otherwise we can run out of memory
172 $memory_limit = trim(@ini_get('memory_limit'));
173 // 2 MB as default
174 if (empty($memory_limit)) {
175 $memory_limit = 2 * 1024 * 1024;
176 }
177 // In case no memory limit we work on 10MB chunks
178 if ($memory_limit = -1) {
179 $memory_limit = 10 * 1024 * 1024;
180 }
181  
182 // Calculate value of the limit
183 if (strtolower(substr($memory_limit, -1)) == 'm') {
184 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
185 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
186 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
187 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
188 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
189 } else {
190 $memory_limit = (int)$memory_limit;
191 }
192  
193 $read_limit = $memory_limit / 4; // Just to be sure, there might be lot of memory needed for uncompression
194  
195 // handle filenames
196 if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
197  
198 // sanitize $local_import_file as it comes from a POST
199 $local_import_file = PMA_securePath($local_import_file);
200  
201 $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
202 } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
203 $import_file = 'none';
204 }
205  
206 // Do we have file to import?
207 if ($import_file != 'none' && !$error) {
208 // work around open_basedir and other limitations
209 $open_basedir = @ini_get('open_basedir');
210  
211 // If we are on a server with open_basedir, we must move the file
212 // before opening it. The doc explains how to create the "./tmp"
213 // directory
214  
215 if (!empty($open_basedir)) {
216  
217 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
218  
219 // function is_writeable() is valid on PHP3 and 4
220 if (is_writeable($tmp_subdir)) {
221 $import_file_new = $tmp_subdir . basename($import_file);
222 if (move_uploaded_file($import_file, $import_file_new)) {
223 $import_file = $import_file_new;
224 $file_to_unlink = $import_file_new;
225 }
226 }
227 }
228  
229 // Handle file compression
230 $compression = PMA_detectCompression($import_file);
231 if ($compression === FALSE) {
232 $message = $strFileCouldNotBeRead;
233 $show_error_header = TRUE;
234 $error = TRUE;
235 } else {
236 switch ($compression) {
237 case 'application/bzip2':
238 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
239 $import_handle = @bzopen($import_file, 'r');
240 } else {
241 $message = sprintf($strUnsupportedCompressionDetected, $compression);
242 $show_error_header = TRUE;
243 $error = TRUE;
244 }
245 break;
246 case 'application/gzip':
247 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
248 $import_handle = @gzopen($import_file, 'r');
249 } else {
250 $message = sprintf($strUnsupportedCompressionDetected, $compression);
251 $show_error_header = TRUE;
252 $error = TRUE;
253 }
254 break;
255 case 'application/zip':
256 if ($cfg['GZipDump'] && @function_exists('gzinflate')) {
257 include_once('./libraries/unzip.lib.php');
258 $import_handle = new SimpleUnzip();
259 $import_handle->ReadFile($import_file);
260 if ($import_handle->Count() == 0) {
261 $message = $strNoFilesFoundInZip;
262 $show_error_header = TRUE;
263 $error = TRUE;
264 } elseif ($import_handle->GetError(0) != 0) {
265 $message = $strErrorInZipFile . ' ' . $import_handle->GetErrorMsg(0);
266 $show_error_header = TRUE;
267 $error = TRUE;
268 } else {
269 $import_text = $import_handle->GetData(0);
270 }
271 // We don't need to store it further
272 $import_handle = '';
273 } else {
274 $message = sprintf($strUnsupportedCompressionDetected, $compression);
275 $show_error_header = TRUE;
276 $error = TRUE;
277 }
278 break;
279 case 'none':
280 $import_handle = @fopen($import_file, 'r');
281 break;
282 default:
283 $message = sprintf($strUnsupportedCompressionDetected, $compression);
284 $show_error_header = TRUE;
285 $error = TRUE;
286 break;
287 }
288 }
289 if (!$error && $import_handle === FALSE) {
290 $message = $strFileCouldNotBeRead;
291 $show_error_header = TRUE;
292 $error = TRUE;
293 }
294 } elseif (!$error) {
295 if (!isset($import_text) || empty($import_text)) {
296 $message = $strNothingToImport;
297 $show_error_header = TRUE;
298 $error = TRUE;
299 }
300 }
301  
302 // Convert the file's charset if necessary
303 if ($cfg['AllowAnywhereRecoding'] && $allow_recoding
304 && isset($charset_of_file)) {
305 if ($charset_of_file != $charset) {
306 $charset_conversion = TRUE;
307 }
308 } elseif (PMA_MYSQL_INT_VERSION >= 40100
309 && isset($charset_of_file) && $charset_of_file != 'utf8') {
310 PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
311 // We can not show query in this case, it is in different charset
312 $sql_query_disabled = TRUE;
313 $reset_charset = TRUE;
314 }
315  
316 // Something to skip?
317 if (!$error && isset($skip)) {
318 $original_skip = $skip;
319 while ($skip > 0) {
320 PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
321 $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
322 $skip -= $read_limit;
323 }
324 unset($skip);
325 }
326  
327 if (!$error) {
328 // Check for file existance
329 if (!file_exists('./libraries/import/' . $format . '.php')) {
330 $error = TRUE;
331 $message = $strCanNotLoadImportPlugins;
332 $show_error_header = TRUE;
333 } else {
334 // Do the real import
335 $plugin_param = $import_type;
336 require('./libraries/import/' . $format . '.php');
337 }
338 }
339  
340 // Cleanup temporary file
341 if ($file_to_unlink != '') {
342 unlink($file_to_unlink);
343 }
344  
345 // Reset charset back, if we did some changes
346 if ($reset_charset) {
347 PMA_DBI_query('SET CHARACTER SET utf8');
348 PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
349 }
350  
351 // Show correct message
352 if (!empty($id_bookmark) && $action_bookmark == 2) {
353 $message = $strBookmarkDeleted;
354 $display_query = $import_text;
355 $error = FALSE; // unset error marker, it was used just to skip processing
356 } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
357 $message = $strShowingBookmark;
358 } elseif ($bookmark_created) {
359 $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
360 } elseif ($finished && !$error) {
361 if ($import_type == 'query') {
362 $message = $strSuccess;
363 } else {
364 $message = sprintf($strImportSuccessfullyFinished, $executed_queries);
365 }
366 }
367  
368 // Did we hit timeout? Tell it user.
369 if ($timeout_passed) {
370 $message = $strTimeoutPassed;
371 if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
372 $message .= ' ' . $strTimeoutNothingParsed;
373 }
374 }
375  
376 // Display back import page
377 require_once('./libraries/header.inc.php');
378  
379 // There was an error?
380 if (isset($my_die)) {
381 foreach ($my_die AS $key => $die) {
382 PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
383 echo '<hr />';
384 }
385 }
386  
387 if ($go_sql) {
388 if (isset($_GET['pos'])) {
389 // comes from the Refresh link
390 $pos = $_GET['pos'];
391 } else {
392 // Set pos to zero to possibly append limit
393 $pos = 0;
394 }
395 require('./sql.php');
396 } else {
397 $active_page = $goto;
398 require('./' . $goto);
399 }
400 exit();
401 ?>