Prevent null-byte injection in preg_replace()
[phpmyadmin.git] / export.php
blob8e8fde2e66e3b6fa4a42fc315810f5eea17e2f81
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get the variables sent or posted to this script and a core script
12 require_once 'libraries/common.inc.php';
13 require_once 'libraries/zip.lib.php';
14 require_once 'libraries/plugin_interface.lib.php';
16 /**
17 * Sets globals from $_POST
19 * - Please keep the parameters in order of their appearance in the form
20 * - Some of these parameters are not used, as the code below directly
21 * verifies from the superglobal $_POST or $_REQUEST
23 $post_params = array(
24 'db',
25 'table',
26 'single_table',
27 'export_type',
28 'export_method',
29 'quick_or_custom',
30 'limit_to',
31 'limit_from',
32 'allrows',
33 'output_format',
34 'filename_template',
35 'remember_template',
36 'charset_of_file',
37 'compression',
38 'what',
39 'htmlword_structure_or_data',
40 'htmlword_null',
41 'htmlword_columns',
42 'mediawiki_structure_or_data',
43 'mediawiki_caption',
44 'pdf_report_title',
45 'pdf_structure_or_data',
46 'odt_structure_or_data',
47 'odt_relation',
48 'odt_comments',
49 'odt_mime',
50 'odt_columns',
51 'odt_null',
52 'codegen_structure_or_data',
53 'codegen_format',
54 'excel_null',
55 'excel_columns',
56 'excel_edition',
57 'excel_structure_or_data',
58 'yaml_structure_or_data',
59 'ods_null',
60 'ods_structure_or_data',
61 'ods_columns',
62 'json_structure_or_data',
63 'xml_structure_or_data',
64 'xml_export_functions',
65 'xml_export_procedures',
66 'xml_export_tables',
67 'xml_export_triggers',
68 'xml_export_views',
69 'xml_export_contents',
70 'texytext_structure_or_data',
71 'texytext_columns',
72 'texytext_null',
73 'phparray_structure_or_data',
74 'sql_include_comments',
75 'sql_header_comment',
76 'sql_dates',
77 'sql_relation',
78 'sql_mime',
79 'sql_use_transaction',
80 'sql_disable_fk',
81 'sql_compatibility',
82 'sql_structure_or_data',
83 'sql_drop_table',
84 'sql_procedure_function',
85 'sql_create_table_statements',
86 'sql_if_not_exists',
87 'sql_auto_increment',
88 'sql_backquotes',
89 'sql_truncate',
90 'sql_delayed',
91 'sql_ignore',
92 'sql_type',
93 'sql_insert_syntax',
94 'sql_max_query_size',
95 'sql_hex_for_blob',
96 'sql_utc_time',
97 'csv_separator',
98 'csv_enclosed',
99 'csv_escaped',
100 'csv_terminated',
101 'csv_null',
102 'csv_columns',
103 'csv_structure_or_data',
104 'latex_caption',
105 'latex_structure_or_data',
106 'latex_structure_caption',
107 'latex_structure_continued_caption',
108 'latex_structure_label',
109 'latex_relation',
110 'latex_comments',
111 'latex_mime',
112 'latex_columns',
113 'latex_data_caption',
114 'latex_data_continued_caption',
115 'latex_data_label',
116 'latex_null'
119 foreach ($post_params as $one_post_param) {
120 if (isset($_POST[$one_post_param])) {
121 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
125 // sanitize this parameter which will be used below in a file inclusion
126 $what = PMA_securePath($what);
128 PMA_Util::checkParameters(array('what', 'export_type'));
130 // export class instance, not array of properties, as before
131 $export_plugin = PMA_getPlugin(
132 "export",
133 $what,
134 'libraries/plugins/export/',
135 array(
136 'export_type' => $export_type,
137 'single_table' => isset($single_table)
141 // Backward compatbility
142 $type = $what;
144 // Check export type
145 if (! isset($export_plugin)) {
146 PMA_fatalError(__('Bad type!'));
150 * valid compression methods
152 $compression_methods = array(
153 'zip',
154 'gzip',
155 'bzip2',
159 * init and variable checking
161 $compression = false;
162 $onserver = false;
163 $save_on_server = false;
164 $buffer_needed = false;
166 // Is it a quick or custom export?
167 if ($_REQUEST['quick_or_custom'] == 'quick') {
168 $quick_export = true;
169 } else {
170 $quick_export = false;
173 if ($_REQUEST['output_format'] == 'astext') {
174 $asfile = false;
175 } else {
176 $asfile = true;
177 if (in_array($_REQUEST['compression'], $compression_methods)) {
178 $compression = $_REQUEST['compression'];
179 $buffer_needed = true;
181 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
182 || (! $quick_export && ! empty($_REQUEST['onserver']))
184 if ($quick_export) {
185 $onserver = $_REQUEST['quick_export_onserver'];
186 } else {
187 $onserver = $_REQUEST['onserver'];
189 // Will we save dump on server?
190 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
194 // Does export require to be into file?
195 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
196 $message = PMA_Message::error(
197 __('Selected export type has to be saved in file!')
199 if ($export_type == 'server') {
200 $active_page = 'server_export.php';
201 include 'server_export.php';
202 } elseif ($export_type == 'database') {
203 $active_page = 'db_export.php';
204 include 'db_export.php';
205 } else {
206 $active_page = 'tbl_export.php';
207 include 'tbl_export.php';
209 exit();
212 // Generate error url and check for needed variables
213 if ($export_type == 'server') {
214 $err_url = 'server_export.php?' . PMA_generate_common_url();
215 } elseif ($export_type == 'database' && strlen($db)) {
216 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
217 // Check if we have something to export
218 if (isset($table_select)) {
219 $tables = $table_select;
220 } else {
221 $tables = array();
223 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
224 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
225 } else {
226 PMA_fatalError(__('Bad parameters!'));
230 * Increase time limit for script execution and initializes some variables
232 @set_time_limit($cfg['ExecTimeLimit']);
233 if (! empty($cfg['MemoryLimit'])) {
234 @ini_set('memory_limit', $cfg['MemoryLimit']);
237 // Start with empty buffer
238 $dump_buffer = '';
239 $dump_buffer_len = 0;
241 // We send fake headers to avoid browser timeout when buffering
242 $time_start = time();
246 * Detect ob_gzhandler
248 * @return bool
250 function PMA_isGzHandlerEnabled()
252 return in_array('ob_gzhandler', ob_list_handlers());
256 * Detect whether gzencode is needed; it might not be needed if
257 * the server is already compressing by itself
259 * @return bool Whether gzencode is needed
261 function PMA_gzencodeNeeded()
263 if (@function_exists('gzencode')
264 && ! @ini_get('zlib.output_compression')
265 // Here, we detect Apache's mod_deflate so we bet that
266 // this module is active for this instance of phpMyAdmin
267 // and therefore, will gzip encode the content
268 && ! (function_exists('apache_get_modules')
269 && in_array('mod_deflate', apache_get_modules()))
270 && ! PMA_isGzHandlerEnabled()
272 return true;
273 } else {
274 return false;
279 * Output handler for all exports, if needed buffering, it stores data into
280 * $dump_buffer, otherwise it prints thems out.
282 * @param string $line the insert statement
284 * @return bool Whether output succeeded
286 function PMA_exportOutputHandler($line)
288 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
290 // Kanji encoding convert feature
291 if ($GLOBALS['output_kanji_conversion']) {
292 $line = PMA_kanji_str_conv(
293 $line,
294 $GLOBALS['knjenc'],
295 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
298 // If we have to buffer data, we will perform everything at once at the end
299 if ($GLOBALS['buffer_needed']) {
301 $dump_buffer .= $line;
302 if ($GLOBALS['onfly_compression']) {
304 $dump_buffer_len += strlen($line);
306 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
307 if ($GLOBALS['output_charset_conversion']) {
308 $dump_buffer = PMA_convert_string(
309 'utf-8',
310 $GLOBALS['charset_of_file'],
311 $dump_buffer
314 // as bzipped
315 if ($GLOBALS['compression'] == 'bzip2'
316 && @function_exists('bzcompress')
318 $dump_buffer = bzcompress($dump_buffer);
319 } elseif ($GLOBALS['compression'] == 'gzip'
320 && PMA_gzencodeNeeded()
322 // as a gzipped file
323 // without the optional parameter level because it bugs
324 $dump_buffer = gzencode($dump_buffer);
326 if ($GLOBALS['save_on_server']) {
327 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
328 if (! $write_result || ($write_result != strlen($dump_buffer))) {
329 $GLOBALS['message'] = PMA_Message::error(
330 __('Insufficient space to save the file %s.')
332 $GLOBALS['message']->addParam($save_filename);
333 return false;
335 } else {
336 echo $dump_buffer;
338 $dump_buffer = '';
339 $dump_buffer_len = 0;
341 } else {
342 $time_now = time();
343 if ($time_start >= $time_now + 30) {
344 $time_start = $time_now;
345 header('X-pmaPing: Pong');
346 } // end if
348 } else {
349 if ($GLOBALS['asfile']) {
350 if ($GLOBALS['output_charset_conversion']) {
351 $line = PMA_convert_string(
352 'utf-8',
353 $GLOBALS['charset_of_file'],
354 $line
357 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
358 $write_result = @fwrite($GLOBALS['file_handle'], $line);
359 if (! $write_result || ($write_result != strlen($line))) {
360 $GLOBALS['message'] = PMA_Message::error(
361 __('Insufficient space to save the file %s.')
363 $GLOBALS['message']->addParam($save_filename);
364 return false;
366 $time_now = time();
367 if ($time_start >= $time_now + 30) {
368 $time_start = $time_now;
369 header('X-pmaPing: Pong');
370 } // end if
371 } else {
372 // We export as file - output normally
373 echo $line;
375 } else {
376 // We export as html - replace special chars
377 echo htmlspecialchars($line);
380 return true;
381 } // end of the 'PMA_exportOutputHandler()' function
383 // Defines the default <CR><LF> format.
384 // For SQL always use \n as MySQL wants this on all platforms.
385 if ($what == 'sql') {
386 $crlf = "\n";
387 } else {
388 $crlf = PMA_Util::whichCrlf();
391 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
393 // Do we need to convert charset?
394 $output_charset_conversion = $asfile
395 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
396 && isset($charset_of_file) && $charset_of_file != 'utf-8'
397 && $type != 'xls';
399 // Use on the fly compression?
400 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
401 && ($compression == 'gzip' || $compression == 'bzip2');
402 if ($onfly_compression) {
403 $memory_limit = trim(@ini_get('memory_limit'));
404 // 2 MB as default
405 if (empty($memory_limit)) {
406 $memory_limit = 2 * 1024 * 1024;
409 if (strtolower(substr($memory_limit, -1)) == 'm') {
410 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
411 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
412 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
413 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
414 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
415 } else {
416 $memory_limit = (int)$memory_limit;
419 // Some of memory is needed for other thins and as treshold.
420 // Nijel: During export I had allocated (see memory_get_usage function)
421 // approx 1.2MB so this comes from that.
422 if ($memory_limit > 1500000) {
423 $memory_limit -= 1500000;
426 // Some memory is needed for compression, assume 1/3
427 $memory_limit /= 8;
430 // Generate filename and mime type if needed
431 if ($asfile) {
432 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
433 if ($export_type == 'server') {
434 if (isset($remember_template)) {
435 $GLOBALS['PMA_Config']->setUserValue(
436 'pma_server_filename_template',
437 'Export/file_template_server',
438 $filename_template
441 } elseif ($export_type == 'database') {
442 if (isset($remember_template)) {
443 $GLOBALS['PMA_Config']->setUserValue(
444 'pma_db_filename_template',
445 'Export/file_template_database',
446 $filename_template
449 } else {
450 if (isset($remember_template)) {
451 $GLOBALS['PMA_Config']->setUserValue(
452 'pma_table_filename_template',
453 'Export/file_template_table',
454 $filename_template
458 $filename = PMA_Util::expandUserString($filename_template);
459 $filename = PMA_sanitizeFilename($filename);
461 // Grab basic dump extension and mime type
462 // Check if the user already added extension;
463 // get the substring where the extension would be if it was included
464 $extension_start_pos = strlen($filename) - strlen(
465 $export_plugin->getProperties()->getExtension()
466 ) - 1;
467 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
468 $required_extension = "." . $export_plugin->getProperties()->getExtension();
469 if (strtolower($user_extension) != $required_extension) {
470 $filename .= $required_extension;
472 $mime_type = $export_plugin->getProperties()->getMimeType();
474 // If dump is going to be compressed, set correct mime_type and add
475 // compression to extension
476 if ($compression == 'bzip2') {
477 $filename .= '.bz2';
478 $mime_type = 'application/x-bzip2';
479 } elseif ($compression == 'gzip') {
480 $filename .= '.gz';
481 $mime_type = 'application/x-gzip';
482 } elseif ($compression == 'zip') {
483 $filename .= '.zip';
484 $mime_type = 'application/zip';
488 // Open file on server if needed
489 if ($save_on_server) {
490 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
491 . preg_replace('@[/\\\\]@', '_', $filename);
492 unset($message);
493 if (file_exists($save_filename)
494 && ((! $quick_export && empty($onserverover))
495 || ($quick_export
496 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
498 $message = PMA_Message::error(
499 __('File %s already exists on server, change filename or check overwrite option.')
501 $message->addParam($save_filename);
502 } else {
503 if (is_file($save_filename) && ! is_writable($save_filename)) {
504 $message = PMA_Message::error(
505 __('The web server does not have permission to save the file %s.')
507 $message->addParam($save_filename);
508 } else {
509 if (! $file_handle = @fopen($save_filename, 'w')) {
510 $message = PMA_Message::error(
511 __('The web server does not have permission to save the file %s.')
513 $message->addParam($save_filename);
517 if (isset($message)) {
518 if ($export_type == 'server') {
519 $active_page = 'server_export.php';
520 include 'server_export.php';
521 } elseif ($export_type == 'database') {
522 $active_page = 'db_export.php';
523 include 'db_export.php';
524 } else {
525 $active_page = 'tbl_export.php';
526 include 'tbl_export.php';
528 exit();
533 * Send headers depending on whether the user chose to download a dump file
534 * or not
536 if (! $save_on_server) {
537 if ($asfile) {
538 // Download
539 // (avoid rewriting data containing HTML with anchors and forms;
540 // this was reported to happen under Plesk)
541 @ini_set('url_rewriter.tags', '');
542 $filename = PMA_sanitizeFilename($filename);
544 PMA_downloadHeader($filename, $mime_type);
545 } else {
546 // HTML
547 if ($export_type == 'database') {
548 $num_tables = count($tables);
549 if ($num_tables == 0) {
550 $message = PMA_Message::error(__('No tables found in database.'));
551 $active_page = 'db_export.php';
552 include 'db_export.php';
553 exit();
556 $backup_cfgServer = $cfg['Server'];
557 $cfg['Server'] = $backup_cfgServer;
558 unset($backup_cfgServer);
559 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
560 //echo ' <pre>' . "\n";
563 * Displays a back button with all the $_REQUEST data in the URL
564 * (store in a variable to also display after the textarea)
566 $back_button = '<p>[ <a href="';
567 if ($export_type == 'server') {
568 $back_button .= 'server_export.php?' . PMA_generate_common_url();
569 } elseif ($export_type == 'database') {
570 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
571 } else {
572 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
575 // Convert the multiple select elements from an array to a string
576 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
577 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
578 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
579 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
582 foreach ($_REQUEST as $name => $value) {
583 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
585 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
587 echo $back_button;
588 echo ' <form name="nofunction">' . "\n"
589 // remove auto-select for now: there is no way to select
590 // only a part of the text; anyway, it should obey
591 // $cfg['TextareaAutoSelect']
592 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
593 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
594 } // end download
597 // Fake loop just to allow skip of remain of this code by break, I'd really
598 // need exceptions here :-)
599 do {
601 // Add possibly some comments to export
602 if (! $export_plugin->exportHeader($db)) {
603 break;
606 // Will we need relation & co. setup?
607 $do_relation = isset($GLOBALS[$what . '_relation']);
608 $do_comments = isset($GLOBALS[$what . '_include_comments']);
609 $do_mime = isset($GLOBALS[$what . '_mime']);
610 if ($do_relation || $do_comments || $do_mime) {
611 $cfgRelation = PMA_getRelationsParam();
613 if ($do_mime) {
614 include_once 'libraries/transformations.lib.php';
617 // Include dates in export?
618 $do_dates = isset($GLOBALS[$what . '_dates']);
621 * Builds the dump
623 // Gets the number of tables if a dump of a database has been required
624 if ($export_type == 'server') {
625 if (isset($db_select)) {
626 $tmp_select = implode($db_select, '|');
627 $tmp_select = '|' . $tmp_select . '|';
629 // Walk over databases
630 foreach ($GLOBALS['pma']->databases as $current_db) {
631 if ((isset($tmp_select)
632 && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
633 || ! isset($tmp_select)
635 if (! $export_plugin->exportDBHeader($current_db)) {
636 break 2;
638 if (! $export_plugin->exportDBCreate($current_db)) {
639 break 2;
641 if (method_exists($export_plugin, 'exportRoutines')
642 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
643 && isset($GLOBALS['sql_procedure_function'])
645 $export_plugin->exportRoutines($current_db);
648 $tables = PMA_DBI_get_tables($current_db);
649 $views = array();
650 foreach ($tables as $table) {
651 // if this is a view, collect it for later;
652 // views must be exported after the tables
653 $is_view = PMA_Table::isView($current_db, $table);
654 if ($is_view) {
655 $views[] = $table;
657 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
658 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
660 // for a view, export a stand-in definition of the table
661 // to resolve view dependencies
662 if (! $export_plugin->exportStructure(
663 $current_db, $table, $crlf, $err_url,
664 $is_view ? 'stand_in' : 'create_table', $export_type,
665 $do_relation, $do_comments, $do_mime, $do_dates
666 )) {
667 break 3;
670 // if this is a view or a merge table, don't export data
671 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
672 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
673 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
675 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
676 . '.' . PMA_Util::backquote($table);
677 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
678 break 3;
681 // now export the triggers (needs to be done after the data
682 // because triggers can modify already imported tables)
683 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
684 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
686 if (! $export_plugin->exportStructure(
687 $current_db, $table, $crlf, $err_url,
688 'triggers', $export_type,
689 $do_relation, $do_comments, $do_mime, $do_dates
690 )) {
691 break 2;
695 foreach ($views as $view) {
696 // no data export for a view
697 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
698 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
700 if (! $export_plugin->exportStructure(
701 $current_db, $view, $crlf, $err_url,
702 'create_view', $export_type,
703 $do_relation, $do_comments, $do_mime, $do_dates
704 )) {
705 break 3;
709 if (! $export_plugin->exportDBFooter($current_db)) {
710 break 2;
714 } elseif ($export_type == 'database') {
715 if (! $export_plugin->exportDBHeader($db)) {
716 break;
719 if (method_exists($export_plugin, 'exportRoutines')
720 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
721 && isset($GLOBALS['sql_procedure_function'])
723 $export_plugin->exportRoutines($db);
726 $i = 0;
727 $views = array();
728 // $tables contains the choices from the user (via $table_select)
729 foreach ($tables as $table) {
730 // if this is a view, collect it for later; views must be exported after
731 // the tables
732 $is_view = PMA_Table::isView($db, $table);
733 if ($is_view) {
734 $views[] = $table;
736 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
737 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
739 // for a view, export a stand-in definition of the table
740 // to resolve view dependencies
741 if (! $export_plugin->exportStructure(
742 $db, $table, $crlf, $err_url,
743 $is_view ? 'stand_in' : 'create_table', $export_type,
744 $do_relation, $do_comments, $do_mime, $do_dates
745 )) {
746 break 2;
749 // if this is a view or a merge table, don't export data
750 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
751 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
752 && ! ($is_view || PMA_Table::isMerge($db, $table))
754 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
755 . '.' . PMA_Util::backquote($table);
756 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
757 break 2;
760 // now export the triggers (needs to be done after the data because
761 // triggers can modify already imported tables)
762 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
763 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
765 if (! $export_plugin->exportStructure(
766 $db, $table, $crlf, $err_url,
767 'triggers', $export_type,
768 $do_relation, $do_comments, $do_mime, $do_dates
769 )) {
770 break 2;
774 foreach ($views as $view) {
775 // no data export for a view
776 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
777 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
779 if (! $export_plugin->exportStructure(
780 $db, $view, $crlf, $err_url,
781 'create_view', $export_type,
782 $do_relation, $do_comments, $do_mime, $do_dates
783 )) {
784 break 2;
789 if (! $export_plugin->exportDBFooter($db)) {
790 break;
792 } else {
793 if (! $export_plugin->exportDBHeader($db)) {
794 break;
796 // We export just one table
797 // $allrows comes from the form when "Dump all rows" has been selected
798 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
799 $add_query = ' LIMIT '
800 . (($limit_from > 0) ? $limit_from . ', ' : '')
801 . $limit_to;
802 } else {
803 $add_query = '';
806 $is_view = PMA_Table::isView($db, $table);
807 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
808 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
810 if (! $export_plugin->exportStructure(
811 $db, $table, $crlf, $err_url,
812 $is_view ? 'create_view' : 'create_table', $export_type,
813 $do_relation, $do_comments, $do_mime, $do_dates
814 )) {
815 break;
818 // If this is an export of a single view, we have to export data;
819 // for example, a PDF report
820 // if it is a merge table, no data is exported
821 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
822 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
823 && ! PMA_Table::isMerge($db, $table)
825 if (! empty($sql_query)) {
826 // only preg_replace if needed
827 if (! empty($add_query)) {
828 // remove trailing semicolon before adding a LIMIT
829 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
831 $local_query = $sql_query . $add_query;
832 PMA_DBI_select_db($db);
833 } else {
834 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
835 . '.' . PMA_Util::backquote($table) . $add_query;
837 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
838 break;
841 // now export the triggers (needs to be done after the data because
842 // triggers can modify already imported tables)
843 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
844 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
846 if (! $export_plugin->exportStructure(
847 $db, $table, $crlf, $err_url,
848 'triggers', $export_type,
849 $do_relation, $do_comments, $do_mime, $do_dates
850 )) {
851 break 2;
854 if (! $export_plugin->exportDBFooter($db)) {
855 break;
858 if (! $export_plugin->exportFooter()) {
859 break;
862 } while (false);
863 // End of fake loop
865 if ($save_on_server && isset($message)) {
866 if ($export_type == 'server') {
867 $active_page = 'server_export.php';
868 include 'server_export.php';
869 } elseif ($export_type == 'database') {
870 $active_page = 'db_export.php';
871 include 'db_export.php';
872 } else {
873 $active_page = 'tbl_export.php';
874 include 'tbl_export.php';
876 exit();
880 * Send the dump as a file...
882 if (! empty($asfile)) {
883 // Convert the charset if required.
884 if ($output_charset_conversion) {
885 $dump_buffer = PMA_convert_string(
886 'utf-8',
887 $GLOBALS['charset_of_file'],
888 $dump_buffer
892 // Do the compression
893 // 1. as a zipped file
894 if ($compression == 'zip') {
895 if (@function_exists('gzcompress')) {
896 $zipfile = new ZipFile();
897 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
898 $dump_buffer = $zipfile->file();
900 } elseif ($compression == 'bzip2') {
901 // 2. as a bzipped file
902 if (@function_exists('bzcompress')) {
903 $dump_buffer = bzcompress($dump_buffer);
905 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
906 // 3. as a gzipped file
907 // without the optional parameter level because it bugs
908 $dump_buffer = gzencode($dump_buffer);
911 /* If we saved on server, we have to close file now */
912 if ($save_on_server) {
913 $write_result = @fwrite($file_handle, $dump_buffer);
914 fclose($file_handle);
915 if (strlen($dump_buffer) > 0
916 && (! $write_result || ($write_result != strlen($dump_buffer)))
918 $message = new PMA_Message(
919 __('Insufficient space to save the file %s.'),
920 PMA_Message::ERROR,
921 $save_filename
923 } else {
924 $message = new PMA_Message(
925 __('Dump has been saved to file %s.'),
926 PMA_Message::SUCCESS,
927 $save_filename
931 if ($export_type == 'server') {
932 $active_page = 'server_export.php';
933 include_once 'server_export.php';
934 } elseif ($export_type == 'database') {
935 $active_page = 'db_export.php';
936 include_once 'db_export.php';
937 } else {
938 $active_page = 'tbl_export.php';
939 include_once 'tbl_export.php';
941 exit();
942 } else {
943 PMA_Response::getInstance()->disable();
944 echo $dump_buffer;
946 } else {
948 * Displays the dump...
950 * Close the html tags and add the footers if dump is displayed on screen
952 echo '</textarea>' . "\n"
953 . ' </form>' . "\n";
954 echo $back_button;
956 echo "\n";
957 echo '</div>' . "\n";
958 echo "\n";
960 <script type="text/javascript">
961 //<![CDATA[
962 var $body = $("body");
963 $("#textSQLDUMP")
964 .width($body.width() - 50)
965 .height($body.height() - 100);
966 //]]>
967 </script>
968 <?php
969 } // end if