Add backward compatibility code to affect above renaming.
[phpmyadmin/crack.git] / export.php
blobb887b2b1964c297edaf37e6b2b360eb030ad701e
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Get the variables sent or posted to this script and a core script
7 */
8 require_once('./libraries/common.lib.php');
9 require_once('./libraries/zip.lib.php');
10 require_once('./libraries/plugin_interface.lib.php');
12 PMA_checkParameters(array('what', 'export_type'));
14 // Scan plugins
15 $export_list = PMA_getPlugins('./libraries/export/', array('export_type' => $export_type, 'single_table' => isset($single_table)));
17 // Backward compatbility
18 $type = $what;
20 // Check export type
21 if (!isset($export_list[$type])) {
22 die('Bad type!');
25 // Does export require to be into file?
26 if (isset($export_list[$type]['force_file']) && ! isset($asfile)) {
27 $message = $strExportMustBeFile;
28 $GLOBALS['show_error_header'] = true;
29 $js_to_run = 'functions.js';
30 require_once('./libraries/header.inc.php');
31 if ($export_type == 'server') {
32 $active_page = 'server_export.php';
33 require('./server_export.php');
34 } elseif ($export_type == 'database') {
35 $active_page = 'db_export.php';
36 require('./db_export.php');
37 } else {
38 $active_page = 'tbl_export.php';
39 require('./tbl_export.php');
41 exit();
44 // Generate error url and check for needed variables
45 if ($export_type == 'server') {
46 $err_url = 'server_export.php?' . PMA_generate_common_url();
47 } elseif ($export_type == 'database' && isset($db) && strlen($db)) {
48 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
49 } elseif ($export_type == 'table' && isset($db) && strlen($db) && isset($table) && strlen($table)) {
50 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
51 } else {
52 die('Bad parameters!');
55 // Get the functions specific to the export type
56 require('./libraries/export/' . PMA_securePath($type) . '.php');
58 /**
59 * Increase time limit for script execution and initializes some variables
61 @set_time_limit($cfg['ExecTimeLimit']);
62 if (!empty($cfg['MemoryLimit'])) {
63 @ini_set('memory_limit', $cfg['MemoryLimit']);
66 // Start with empty buffer
67 $dump_buffer = '';
68 $dump_buffer_len = 0;
70 // We send fake headers to avoid browser timeout when buffering
71 $time_start = time();
74 /**
75 * Output handler for all exports, if needed buffering, it stores data into
76 * $dump_buffer, otherwise it prints thems out.
78 * @param string the insert statement
80 * @return bool Whether output suceeded
82 function PMA_exportOutputHandler($line)
84 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
86 // Kanji encoding convert feature
87 if ($GLOBALS['output_kanji_conversion']) {
88 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
90 // If we have to buffer data, we will perform everything at once at the end
91 if ($GLOBALS['buffer_needed']) {
93 $dump_buffer .= $line;
94 if ($GLOBALS['onfly_compression']) {
96 $dump_buffer_len += strlen($line);
98 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
99 if ($GLOBALS['output_charset_conversion']) {
100 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
102 // as bzipped
103 if ($GLOBALS['compression'] == 'bzip' && @function_exists('bzcompress')) {
104 $dump_buffer = bzcompress($dump_buffer);
106 // as a gzipped file
107 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists('gzencode')) {
108 // without the optional parameter level because it bug
109 $dump_buffer = gzencode($dump_buffer);
111 if ($GLOBALS['save_on_server']) {
112 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
113 if (!$write_result || ($write_result != strlen($dump_buffer))) {
114 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
115 $GLOBALS['show_error_header'] = TRUE;
116 return FALSE;
118 } else {
119 echo $dump_buffer;
121 $dump_buffer = '';
122 $dump_buffer_len = 0;
124 } else {
125 $time_now = time();
126 if ($time_start >= $time_now + 30) {
127 $time_start = $time_now;
128 header('X-pmaPing: Pong');
129 } // end if
131 } else {
132 if ($GLOBALS['asfile']) {
133 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
134 $write_result = @fwrite($GLOBALS['file_handle'], $line);
135 if (!$write_result || ($write_result != strlen($line))) {
136 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
137 $GLOBALS['show_error_header'] = TRUE;
138 return FALSE;
140 $time_now = time();
141 if ($time_start >= $time_now + 30) {
142 $time_start = $time_now;
143 header('X-pmaPing: Pong');
144 } // end if
145 } else {
146 // We export as file - output normally
147 if ($GLOBALS['output_charset_conversion']) {
148 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
150 echo $line;
152 } else {
153 // We export as html - replace special chars
154 echo htmlspecialchars($line);
157 return TRUE;
158 } // end of the 'PMA_exportOutputHandler()' function
160 // Will we save dump on server?
161 $save_on_server = isset($cfg['SaveDir']) && !empty($cfg['SaveDir']) && !empty($onserver);
163 // Ensure compressed formats are associated with the download feature
164 if (empty($asfile)) {
165 if ($save_on_server) {
166 $asfile = TRUE;
167 } elseif (isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip')) {
168 $asfile = TRUE;
169 } else {
170 $asfile = FALSE;
172 } else {
173 $asfile = TRUE;
176 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
177 if ($what == 'sql') {
178 $crlf = "\n";
179 } else {
180 $crlf = PMA_whichCrlf();
183 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
185 // Do we need to convert charset?
186 $output_charset_conversion = $asfile &&
187 $cfg['AllowAnywhereRecoding'] && $allow_recoding
188 && isset($charset_of_file) && $charset_of_file != $charset
189 && $type != 'xls';
191 // Set whether we will need buffering
192 $buffer_needed = isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip');
194 // Use on fly compression?
195 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && isset($compression) && ($compression == 'gzip' | $compression == 'bzip');
196 if ($onfly_compression) {
197 $memory_limit = trim(@ini_get('memory_limit'));
198 // 2 MB as default
199 if (empty($memory_limit)) {
200 $memory_limit = 2 * 1024 * 1024;
203 if (strtolower(substr($memory_limit, -1)) == 'm') {
204 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
205 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
206 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
207 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
208 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
209 } else {
210 $memory_limit = (int)$memory_limit;
213 // Some of memory is needed for other thins and as treshold.
214 // Nijel: During export I had allocated (see memory_get_usage function)
215 // approx 1.2MB so this comes from that.
216 if ($memory_limit > 1500000) {
217 $memory_limit -= 1500000;
220 // Some memory is needed for compression, assume 1/3
221 $memory_limit /= 8;
224 // Generate filename and mime type if needed
225 if ($asfile) {
226 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
227 if ($export_type == 'server') {
228 if (isset($remember_template)) {
229 setcookie('pma_server_filename_template', $filename_template, 0, $GLOBALS['cookie_path'], '', $GLOBALS['is_https']);
231 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template));
232 } elseif ($export_type == 'database') {
233 if (isset($remember_template)) {
234 setcookie('pma_db_filename_template', $filename_template, 0, $GLOBALS['cookie_path'], '', $GLOBALS['is_https']);
236 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)));
237 } else {
238 if (isset($remember_template)) {
239 setcookie('pma_table_filename_template', $filename_template, 0, $GLOBALS['cookie_path'], '', $GLOBALS['is_https']);
241 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))));
244 // convert filename to iso-8859-1, it is safer
245 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
246 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
247 } else {
248 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
251 // Grab basic dump extension and mime type
252 $filename .= '.' . $export_list[$type]['extension'];
253 $mime_type = $export_list[$type]['mime_type'];
255 // If dump is going to be compressed, set correct encoding or mime_type and add
256 // compression to extension
257 $content_encoding = '';
258 if (isset($compression) && $compression == 'bzip') {
259 $filename .= '.bz2';
260 // browsers don't like this:
261 //$content_encoding = 'x-bzip2';
262 $mime_type = 'application/x-bzip2';
263 } elseif (isset($compression) && $compression == 'gzip') {
264 $filename .= '.gz';
265 // Needed to avoid recompression by server modules like mod_gzip.
266 // It seems necessary to check about zlib.output_compression
267 // to avoid compressing twice
268 if (!@ini_get('zlib.output_compression')) {
269 $content_encoding = 'x-gzip';
270 $mime_type = 'application/x-gzip';
272 } elseif (isset($compression) && $compression == 'zip') {
273 $filename .= '.zip';
274 $mime_type = 'application/zip';
278 // Open file on server if needed
279 if ($save_on_server) {
280 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
281 unset($message);
282 if (file_exists($save_filename) && empty($onserverover)) {
283 $message = sprintf($strFileAlreadyExists, htmlspecialchars($save_filename));
284 $GLOBALS['show_error_header'] = TRUE;
285 } else {
286 if (is_file($save_filename) && !is_writable($save_filename)) {
287 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
288 $GLOBALS['show_error_header'] = TRUE;
289 } else {
290 if (!$file_handle = @fopen($save_filename, 'w')) {
291 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
292 $GLOBALS['show_error_header'] = TRUE;
296 if (isset($message)) {
297 $js_to_run = 'functions.js';
298 require_once('./libraries/header.inc.php');
299 if ($export_type == 'server') {
300 $active_page = 'server_export.php';
301 require('./server_export.php');
302 } elseif ($export_type == 'database') {
303 $active_page = 'db_export.php';
304 require('./db_export.php');
305 } else {
306 $active_page = 'tbl_export.php';
307 require('./tbl_export.php');
309 exit();
314 * Send headers depending on whether the user chose to download a dump file
315 * or not
317 if (!$save_on_server) {
318 if ($asfile ) {
319 // Download
320 if (!empty($content_encoding)) {
321 header('Content-Encoding: ' . $content_encoding);
323 header('Content-Type: ' . $mime_type);
324 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
325 // lem9: Tested behavior of
326 // IE 5.50.4807.2300
327 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
328 // IE 6.0.2900.2180
329 // Firefox 1.0.6
330 // in http and https
331 header('Content-Disposition: attachment; filename="' . $filename . '"');
332 if (PMA_USR_BROWSER_AGENT == 'IE') {
333 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
334 header('Pragma: public');
335 } else {
336 header('Pragma: no-cache');
338 } else {
339 // HTML
340 // Check if we have something to export
341 if ($export_type == 'database') {
342 if (isset($table_select)) {
343 $tables = $table_select;
344 } else {
345 $tables = array();
347 $num_tables = count($tables);
348 if ($num_tables == 0) {
349 $message = $strNoTablesFound;
350 $js_to_run = 'functions.js';
351 require_once('./libraries/header.inc.php');
352 $active_page = 'db_export.php';
353 require('./db_export.php');
354 exit();
357 $backup_cfgServer = $cfg['Server'];
358 require_once('./libraries/header.inc.php');
359 $cfg['Server'] = $backup_cfgServer;
360 unset($backup_cfgServer);
361 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
362 //echo ' <pre>' . "\n";
363 echo ' <form name="nofunction">' . "\n"
364 // remove auto-select for now: there is no way to select
365 // only a part of the text; anyway, it should obey
366 // $cfg['TextareaAutoSelect']
367 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
368 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
369 } // end download
372 // Fake loop just to allow skip of remain of this code by break, I'd really
373 // need exceptions here :-)
374 do {
376 // Add possibly some comments to export
377 if (!PMA_exportHeader()) {
378 break;
381 // Will we need relation & co. setup?
382 $do_relation = isset($GLOBALS[$what . '_relation']);
383 $do_comments = isset($GLOBALS[$what . '_comments']);
384 $do_mime = isset($GLOBALS[$what . '_mime']);
385 if ($do_relation || $do_comments || $do_mime) {
386 require_once('./libraries/relation.lib.php');
387 $cfgRelation = PMA_getRelationsParam();
389 if ($do_mime) {
390 require_once('./libraries/transformations.lib.php');
393 // Include dates in export?
394 $do_dates = isset($GLOBALS[$what . '_dates']);
397 * Builds the dump
399 // Gets the number of tables if a dump of a database has been required
400 if ($export_type == 'server') {
401 if (isset($db_select)) {
402 $tmp_select = implode($db_select, '|');
403 $tmp_select = '|' . $tmp_select . '|';
405 // Walk over databases
406 foreach ($GLOBALS['PMA_List_Database']->items as $current_db) {
407 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
408 || !isset($tmp_select)) {
409 if (!PMA_exportDBHeader($current_db)) {
410 break 2;
412 if (!PMA_exportDBCreate($current_db)) {
413 break 2;
415 $tables = PMA_DBI_get_tables($current_db);
416 $views = array();
417 foreach ($tables as $table) {
418 // if this is a view, collect it for later; views must be exported
419 // after the tables
420 if (PMA_Table::isView($current_db, $table)) {
421 $views[] = $table;
422 continue;
424 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
425 if (isset($GLOBALS[$what . '_structure'])) {
426 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
427 break 3;
430 if (isset($GLOBALS[$what . '_data'])) {
431 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
432 break 3;
436 foreach($views as $view) {
437 // no data export for a view
438 if (isset($GLOBALS[$what . '_structure'])) {
439 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
440 break 3;
444 if (!PMA_exportDBFooter($current_db)) {
445 break 2;
449 } elseif ($export_type == 'database') {
450 if (!PMA_exportDBHeader($db)) {
451 break;
454 //if (isset($table_select)) {
455 // $tmp_select = implode($table_select, '|');
456 // $tmp_select = '|' . $tmp_select . '|';
458 $i = 0;
459 $views = array();
460 // $tables contains the choices from the user (via $table_select)
461 foreach ($tables as $table) {
462 // if this is a view, collect it for later; views must be exported after
463 // the tables
464 if (PMA_Table::isView($db, $table)) {
465 $views[] = $table;
466 continue;
468 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
469 //if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
470 //|| !isset($tmp_select)) {
472 if (isset($GLOBALS[$what . '_structure'])) {
473 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
474 break 2;
477 if (isset($GLOBALS[$what . '_data'])) {
478 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
479 break 2;
484 foreach ($views as $view) {
485 // no data export for a view
486 if (isset($GLOBALS[$what . '_structure'])) {
487 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
488 break 2;
493 if (!PMA_exportDBFooter($db)) {
494 break;
496 } else {
497 if (!PMA_exportDBHeader($db)) {
498 break;
500 // We export just one table
502 if ($limit_to > 0 && $limit_from >= 0) {
503 $add_query = ' LIMIT '
504 . (($limit_from > 0) ? $limit_from . ', ' : '')
505 . $limit_to;
506 } else {
507 $add_query = '';
510 if (!empty($sql_query)) {
511 // only preg_replace if needed
512 if (!empty($add_query)) {
513 // remove trailing semicolon before adding a LIMIT
514 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
516 $local_query = $sql_query . $add_query;
517 PMA_DBI_select_db($db);
518 } else {
519 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
522 if (isset($GLOBALS[$what . '_structure'])) {
523 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
524 break;
527 // I think we have to export data for a single view; for example PDF report
528 //if (isset($GLOBALS[$what . '_data']) && ! PMA_table::isView($db, $table)) {
529 if (isset($GLOBALS[$what . '_data'])) {
530 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
531 break;
534 if (!PMA_exportDBFooter($db)) {
535 break;
538 if (!PMA_exportFooter()) {
539 break;
542 } while (FALSE);
543 // End of fake loop
545 if ($save_on_server && isset($message)) {
546 $js_to_run = 'functions.js';
547 require_once('./libraries/header.inc.php');
548 if ($export_type == 'server') {
549 $active_page = 'server_export.php';
550 require('./server_export.php');
551 } elseif ($export_type == 'database') {
552 $active_page = 'db_export.php';
553 require('./db_export.php');
554 } else {
555 $active_page = 'tbl_export.php';
556 require('./tbl_export.php');
558 exit();
562 * Send the dump as a file...
564 if (!empty($asfile)) {
565 // Convert the charset if required.
566 if ($output_charset_conversion) {
567 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
570 // Do the compression
571 // 1. as a gzipped file
572 if (isset($compression) && $compression == 'zip') {
573 if (@function_exists('gzcompress')) {
574 $zipfile = new zipfile();
575 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
576 $dump_buffer = $zipfile -> file();
579 // 2. as a bzipped file
580 elseif (isset($compression) && $compression == 'bzip') {
581 if (@function_exists('bzcompress')) {
582 $dump_buffer = bzcompress($dump_buffer);
583 if ($dump_buffer === -8) {
584 require_once('./libraries/header.inc.php');
585 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
586 require_once('./libraries/footer.inc.php');
590 // 3. as a gzipped file
591 elseif (isset($compression) && $compression == 'gzip') {
592 if (@function_exists('gzencode')) {
593 // without the optional parameter level because it bug
594 $dump_buffer = gzencode($dump_buffer);
598 /* If ve saved on server, we have to close file now */
599 if ($save_on_server) {
600 $write_result = @fwrite($file_handle, $dump_buffer);
601 fclose($file_handle);
602 if (strlen($dump_buffer) !=0 && (!$write_result || ($write_result != strlen($dump_buffer)))) {
603 $message = sprintf($strNoSpace, htmlspecialchars($save_filename));
604 } else {
605 $message = sprintf($strDumpSaved, htmlspecialchars($save_filename));
608 $js_to_run = 'functions.js';
609 require_once('./libraries/header.inc.php');
610 if ($export_type == 'server') {
611 $active_page = 'server_export.php';
612 require_once('./server_export.php');
613 } elseif ($export_type == 'database') {
614 $active_page = 'db_export.php';
615 require_once('./db_export.php');
616 } else {
617 $active_page = 'tbl_export.php';
618 require_once('./tbl_export.php');
620 exit();
621 } else {
622 echo $dump_buffer;
626 * Displays the dump...
628 else {
630 * Close the html tags and add the footers in dump is displayed on screen
632 //echo ' </pre>' . "\n";
633 echo '</textarea>' . "\n"
634 . ' </form>' . "\n";
635 echo '</div>' . "\n";
636 echo "\n";
638 <script type="text/javascript" language="javascript">
639 //<![CDATA[
640 var bodyWidth=null; var bodyHeight=null;
641 if (document.getElementById('textSQLDUMP')) {
642 bodyWidth = self.innerWidth;
643 bodyHeight = self.innerHeight;
644 if (!bodyWidth && !bodyHeight) {
645 if (document.compatMode && document.compatMode == "BackCompat") {
646 bodyWidth = document.body.clientWidth;
647 bodyHeight = document.body.clientHeight;
648 } else if (document.compatMode && document.compatMode == "CSS1Compat") {
649 bodyWidth = document.documentElement.clientWidth;
650 bodyHeight = document.documentElement.clientHeight;
653 document.getElementById('textSQLDUMP').style.width=(bodyWidth-50) + 'px';
654 document.getElementById('textSQLDUMP').style.height=(bodyHeight-100) + 'px';
656 //]]>
657 </script>
658 <?php
659 require_once('./libraries/footer.inc.php');
660 } // end if