Translated using Weblate (Spanish)
[phpmyadmin.git] / export.php
blob575f90fe366477596e13aa1f841290ae996dd0b2
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 'db_select',
31 'table_select',
32 'limit_to',
33 'limit_from',
34 'allrows',
35 'output_format',
36 'filename_template',
37 'remember_template',
38 'charset_of_file',
39 'compression',
40 'what',
41 'htmlword_structure_or_data',
42 'htmlword_null',
43 'htmlword_columns',
44 'mediawiki_structure_or_data',
45 'mediawiki_caption',
46 'pdf_report_title',
47 'pdf_structure_or_data',
48 'odt_structure_or_data',
49 'odt_relation',
50 'odt_comments',
51 'odt_mime',
52 'odt_columns',
53 'odt_null',
54 'codegen_structure_or_data',
55 'codegen_format',
56 'excel_null',
57 'excel_columns',
58 'excel_edition',
59 'excel_structure_or_data',
60 'yaml_structure_or_data',
61 'ods_null',
62 'ods_structure_or_data',
63 'ods_columns',
64 'json_structure_or_data',
65 'xml_structure_or_data',
66 'xml_export_functions',
67 'xml_export_procedures',
68 'xml_export_tables',
69 'xml_export_triggers',
70 'xml_export_views',
71 'xml_export_contents',
72 'texytext_structure_or_data',
73 'texytext_columns',
74 'texytext_null',
75 'phparray_structure_or_data',
76 'sql_include_comments',
77 'sql_header_comment',
78 'sql_dates',
79 'sql_relation',
80 'sql_mime',
81 'sql_use_transaction',
82 'sql_disable_fk',
83 'sql_compatibility',
84 'sql_structure_or_data',
85 'sql_drop_table',
86 'sql_procedure_function',
87 'sql_create_table_statements',
88 'sql_if_not_exists',
89 'sql_auto_increment',
90 'sql_backquotes',
91 'sql_truncate',
92 'sql_delayed',
93 'sql_ignore',
94 'sql_type',
95 'sql_insert_syntax',
96 'sql_max_query_size',
97 'sql_hex_for_blob',
98 'sql_utc_time',
99 'csv_separator',
100 'csv_enclosed',
101 'csv_escaped',
102 'csv_terminated',
103 'csv_null',
104 'csv_columns',
105 'csv_structure_or_data',
106 'latex_caption',
107 'latex_structure_or_data',
108 'latex_structure_caption',
109 'latex_structure_continued_caption',
110 'latex_structure_label',
111 'latex_relation',
112 'latex_comments',
113 'latex_mime',
114 'latex_columns',
115 'latex_data_caption',
116 'latex_data_continued_caption',
117 'latex_data_label',
118 'latex_null'
121 foreach ($post_params as $one_post_param) {
122 if (isset($_POST[$one_post_param])) {
123 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
127 // sanitize this parameter which will be used below in a file inclusion
128 $what = PMA_securePath($what);
130 PMA_Util::checkParameters(array('what', 'export_type'));
132 // export class instance, not array of properties, as before
133 $export_plugin = PMA_getPlugin(
134 "export",
135 $what,
136 'libraries/plugins/export/',
137 array(
138 'export_type' => $export_type,
139 'single_table' => isset($single_table)
143 // Backward compatbility
144 $type = $what;
146 // Check export type
147 if (! isset($export_plugin)) {
148 PMA_fatalError(__('Bad type!'));
152 * valid compression methods
154 $compression_methods = array(
155 'zip',
156 'gzip',
157 'bzip2',
161 * init and variable checking
163 $compression = false;
164 $onserver = false;
165 $save_on_server = false;
166 $buffer_needed = false;
168 // Is it a quick or custom export?
169 if ($_REQUEST['quick_or_custom'] == 'quick') {
170 $quick_export = true;
171 } else {
172 $quick_export = false;
175 if ($_REQUEST['output_format'] == 'astext') {
176 $asfile = false;
177 } else {
178 $asfile = true;
179 if (in_array($_REQUEST['compression'], $compression_methods)) {
180 $compression = $_REQUEST['compression'];
181 $buffer_needed = true;
183 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
184 || (! $quick_export && ! empty($_REQUEST['onserver']))
186 if ($quick_export) {
187 $onserver = $_REQUEST['quick_export_onserver'];
188 } else {
189 $onserver = $_REQUEST['onserver'];
191 // Will we save dump on server?
192 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
196 // Does export require to be into file?
197 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
198 $message = PMA_Message::error(
199 __('Selected export type has to be saved in file!')
201 if ($export_type == 'server') {
202 $active_page = 'server_export.php';
203 include 'server_export.php';
204 } elseif ($export_type == 'database') {
205 $active_page = 'db_export.php';
206 include 'db_export.php';
207 } else {
208 $active_page = 'tbl_export.php';
209 include 'tbl_export.php';
211 exit();
214 // Generate error url and check for needed variables
215 if ($export_type == 'server') {
216 $err_url = 'server_export.php?' . PMA_generate_common_url();
217 } elseif ($export_type == 'database' && strlen($db)) {
218 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
219 // Check if we have something to export
220 if (isset($table_select)) {
221 $tables = $table_select;
222 } else {
223 $tables = array();
225 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
226 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
227 } else {
228 PMA_fatalError(__('Bad parameters!'));
232 * Increase time limit for script execution and initializes some variables
234 @set_time_limit($cfg['ExecTimeLimit']);
235 if (! empty($cfg['MemoryLimit'])) {
236 @ini_set('memory_limit', $cfg['MemoryLimit']);
239 // Start with empty buffer
240 $dump_buffer = '';
241 $dump_buffer_len = 0;
243 // We send fake headers to avoid browser timeout when buffering
244 $time_start = time();
248 * Detect ob_gzhandler
250 * @return bool
252 function PMA_isGzHandlerEnabled()
254 return in_array('ob_gzhandler', ob_list_handlers());
258 * Detect whether gzencode is needed; it might not be needed if
259 * the server is already compressing by itself
261 * @return bool Whether gzencode is needed
263 function PMA_gzencodeNeeded()
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 if (@function_exists('gzencode')
269 && ! @ini_get('zlib.output_compression')
270 && ! (function_exists('apache_get_modules')
271 && in_array('mod_deflate', apache_get_modules()))
272 && ! PMA_isGzHandlerEnabled()
274 return true;
275 } else {
276 return false;
281 * Output handler for all exports, if needed buffering, it stores data into
282 * $dump_buffer, otherwise it prints thems out.
284 * @param string $line the insert statement
286 * @return bool Whether output succeeded
288 function PMA_exportOutputHandler($line)
290 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
292 // Kanji encoding convert feature
293 if ($GLOBALS['output_kanji_conversion']) {
294 $line = PMA_kanji_str_conv(
295 $line,
296 $GLOBALS['knjenc'],
297 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
300 // If we have to buffer data, we will perform everything at once at the end
301 if ($GLOBALS['buffer_needed']) {
303 $dump_buffer .= $line;
304 if ($GLOBALS['onfly_compression']) {
306 $dump_buffer_len += strlen($line);
308 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
309 if ($GLOBALS['output_charset_conversion']) {
310 $dump_buffer = PMA_convertString(
311 'utf-8',
312 $GLOBALS['charset_of_file'],
313 $dump_buffer
316 // as bzipped
317 if ($GLOBALS['compression'] == 'bzip2'
318 && @function_exists('bzcompress')
320 $dump_buffer = bzcompress($dump_buffer);
321 } elseif ($GLOBALS['compression'] == 'gzip'
322 && PMA_gzencodeNeeded()
324 // as a gzipped file
325 // without the optional parameter level because it bugs
326 $dump_buffer = gzencode($dump_buffer);
328 if ($GLOBALS['save_on_server']) {
329 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
330 if (! $write_result || ($write_result != strlen($dump_buffer))) {
331 $GLOBALS['message'] = PMA_Message::error(
332 __('Insufficient space to save the file %s.')
334 $GLOBALS['message']->addParam($save_filename);
335 return false;
337 } else {
338 echo $dump_buffer;
340 $dump_buffer = '';
341 $dump_buffer_len = 0;
343 } else {
344 $time_now = time();
345 if ($time_start >= $time_now + 30) {
346 $time_start = $time_now;
347 header('X-pmaPing: Pong');
348 } // end if
350 } else {
351 if ($GLOBALS['asfile']) {
352 if ($GLOBALS['output_charset_conversion']) {
353 $line = PMA_convertString(
354 'utf-8',
355 $GLOBALS['charset_of_file'],
356 $line
359 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
360 $write_result = @fwrite($GLOBALS['file_handle'], $line);
361 if (! $write_result || ($write_result != strlen($line))) {
362 $GLOBALS['message'] = PMA_Message::error(
363 __('Insufficient space to save the file %s.')
365 $GLOBALS['message']->addParam($save_filename);
366 return false;
368 $time_now = time();
369 if ($time_start >= $time_now + 30) {
370 $time_start = $time_now;
371 header('X-pmaPing: Pong');
372 } // end if
373 } else {
374 // We export as file - output normally
375 echo $line;
377 } else {
378 // We export as html - replace special chars
379 echo htmlspecialchars($line);
382 return true;
383 } // end of the 'PMA_exportOutputHandler()' function
385 // Defines the default <CR><LF> format.
386 // For SQL always use \n as MySQL wants this on all platforms.
387 if ($what == 'sql') {
388 $crlf = "\n";
389 } else {
390 $crlf = PMA_Util::whichCrlf();
393 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
395 // Do we need to convert charset?
396 $output_charset_conversion = $asfile
397 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
398 && isset($charset_of_file) && $charset_of_file != 'utf-8'
399 && $type != 'xls';
401 // Use on the fly compression?
402 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
403 && ($compression == 'gzip' || $compression == 'bzip2');
404 if ($onfly_compression) {
405 $memory_limit = trim(@ini_get('memory_limit'));
406 // 2 MB as default
407 if (empty($memory_limit)) {
408 $memory_limit = 2 * 1024 * 1024;
411 if (strtolower(substr($memory_limit, -1)) == 'm') {
412 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
413 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
414 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
415 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
416 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
417 } else {
418 $memory_limit = (int)$memory_limit;
421 // Some of memory is needed for other thins and as treshold.
422 // Nijel: During export I had allocated (see memory_get_usage function)
423 // approx 1.2MB so this comes from that.
424 if ($memory_limit > 1500000) {
425 $memory_limit -= 1500000;
428 // Some memory is needed for compression, assume 1/3
429 $memory_limit /= 8;
432 // Generate filename and mime type if needed
433 if ($asfile) {
434 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
435 if ($export_type == 'server') {
436 if (isset($remember_template)) {
437 $GLOBALS['PMA_Config']->setUserValue(
438 'pma_server_filename_template',
439 'Export/file_template_server',
440 $filename_template
443 } elseif ($export_type == 'database') {
444 if (isset($remember_template)) {
445 $GLOBALS['PMA_Config']->setUserValue(
446 'pma_db_filename_template',
447 'Export/file_template_database',
448 $filename_template
451 } else {
452 if (isset($remember_template)) {
453 $GLOBALS['PMA_Config']->setUserValue(
454 'pma_table_filename_template',
455 'Export/file_template_table',
456 $filename_template
460 $filename = PMA_Util::expandUserString($filename_template);
461 // remove dots in filename (coming from either the template or already
462 // part of the filename) to avoid a remote code execution vulnerability
463 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
465 // Grab basic dump extension and mime type
466 // Check if the user already added extension;
467 // get the substring where the extension would be if it was included
468 $extension_start_pos = strlen($filename) - strlen(
469 $export_plugin->getProperties()->getExtension()
470 ) - 1;
471 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
472 $required_extension = "." . $export_plugin->getProperties()->getExtension();
473 if (strtolower($user_extension) != $required_extension) {
474 $filename .= $required_extension;
476 $mime_type = $export_plugin->getProperties()->getMimeType();
478 // If dump is going to be compressed, set correct mime_type and add
479 // compression to extension
480 if ($compression == 'bzip2') {
481 $filename .= '.bz2';
482 $mime_type = 'application/x-bzip2';
483 } elseif ($compression == 'gzip') {
484 $filename .= '.gz';
485 $mime_type = 'application/x-gzip';
486 } elseif ($compression == 'zip') {
487 $filename .= '.zip';
488 $mime_type = 'application/zip';
492 // Open file on server if needed
493 if ($save_on_server) {
494 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
495 . preg_replace('@[/\\\\]@', '_', $filename);
496 unset($message);
497 if (file_exists($save_filename)
498 && ((! $quick_export && empty($onserverover))
499 || ($quick_export
500 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
502 $message = PMA_Message::error(
503 __('File %s already exists on server, change filename or check overwrite option.')
505 $message->addParam($save_filename);
506 } else {
507 if (is_file($save_filename) && ! is_writable($save_filename)) {
508 $message = PMA_Message::error(
509 __('The web server does not have permission to save the file %s.')
511 $message->addParam($save_filename);
512 } else {
513 if (! $file_handle = @fopen($save_filename, 'w')) {
514 $message = PMA_Message::error(
515 __('The web server does not have permission to save the file %s.')
517 $message->addParam($save_filename);
521 if (isset($message)) {
522 if ($export_type == 'server') {
523 $active_page = 'server_export.php';
524 include 'server_export.php';
525 } elseif ($export_type == 'database') {
526 $active_page = 'db_export.php';
527 include 'db_export.php';
528 } else {
529 $active_page = 'tbl_export.php';
530 include 'tbl_export.php';
532 exit();
537 * Send headers depending on whether the user chose to download a dump file
538 * or not
540 if (! $save_on_server) {
541 if ($asfile) {
542 // Download
543 // (avoid rewriting data containing HTML with anchors and forms;
544 // this was reported to happen under Plesk)
545 @ini_set('url_rewriter.tags', '');
546 $filename = PMA_sanitizeFilename($filename);
548 PMA_downloadHeader($filename, $mime_type);
549 } else {
550 // HTML
551 if ($export_type == 'database') {
552 $num_tables = count($tables);
553 if ($num_tables == 0) {
554 $message = PMA_Message::error(__('No tables found in database.'));
555 $active_page = 'db_export.php';
556 include 'db_export.php';
557 exit();
560 $backup_cfgServer = $cfg['Server'];
561 $cfg['Server'] = $backup_cfgServer;
562 unset($backup_cfgServer);
563 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
564 //echo ' <pre>' . "\n";
567 * Displays a back button with all the $_REQUEST data in the URL
568 * (store in a variable to also display after the textarea)
570 $back_button = '<p>[ <a href="';
571 if ($export_type == 'server') {
572 $back_button .= 'server_export.php?' . PMA_generate_common_url();
573 } elseif ($export_type == 'database') {
574 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
575 } else {
576 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
579 // Convert the multiple select elements from an array to a string
580 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
581 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
582 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
583 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
586 foreach ($_REQUEST as $name => $value) {
587 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
589 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
591 echo $back_button;
592 echo ' <form name="nofunction">' . "\n"
593 // remove auto-select for now: there is no way to select
594 // only a part of the text; anyway, it should obey
595 // $cfg['TextareaAutoSelect']
596 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
597 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
598 } // end download
601 // Fake loop just to allow skip of remain of this code by break, I'd really
602 // need exceptions here :-)
603 do {
605 // Add possibly some comments to export
606 if (! $export_plugin->exportHeader($db)) {
607 break;
610 // Will we need relation & co. setup?
611 $do_relation = isset($GLOBALS[$what . '_relation']);
612 $do_comments = isset($GLOBALS[$what . '_include_comments']);
613 $do_mime = isset($GLOBALS[$what . '_mime']);
614 if ($do_relation || $do_comments || $do_mime) {
615 $cfgRelation = PMA_getRelationsParam();
617 if ($do_mime) {
618 include_once 'libraries/transformations.lib.php';
621 // Include dates in export?
622 $do_dates = isset($GLOBALS[$what . '_dates']);
625 * Builds the dump
627 // Gets the number of tables if a dump of a database has been required
628 if ($export_type == 'server') {
629 if (isset($db_select)) {
630 $tmp_select = implode($db_select, '|');
631 $tmp_select = '|' . $tmp_select . '|';
633 // Walk over databases
634 foreach ($GLOBALS['pma']->databases as $current_db) {
635 if ((isset($tmp_select)
636 && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
637 || ! isset($tmp_select)
639 if (! $export_plugin->exportDBHeader($current_db)) {
640 break 2;
642 if (! $export_plugin->exportDBCreate($current_db)) {
643 break 2;
645 if (method_exists($export_plugin, 'exportRoutines')
646 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
647 && isset($GLOBALS['sql_procedure_function'])
649 $export_plugin->exportRoutines($current_db);
652 $tables = PMA_DBI_getTables($current_db);
653 $views = array();
654 foreach ($tables as $table) {
655 // if this is a view, collect it for later;
656 // views must be exported after the tables
657 $is_view = PMA_Table::isView($current_db, $table);
658 if ($is_view) {
659 $views[] = $table;
661 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
662 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
664 // for a view, export a stand-in definition of the table
665 // to resolve view dependencies
666 if (! $export_plugin->exportStructure(
667 $current_db, $table, $crlf, $err_url,
668 $is_view ? 'stand_in' : 'create_table', $export_type,
669 $do_relation, $do_comments, $do_mime, $do_dates
670 )) {
671 break 3;
674 // if this is a view or a merge table, don't export data
675 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
676 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
677 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
679 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
680 . '.' . PMA_Util::backquote($table);
681 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
682 break 3;
685 // now export the triggers (needs to be done after the data
686 // because triggers can modify already imported tables)
687 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
688 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
690 if (! $export_plugin->exportStructure(
691 $current_db, $table, $crlf, $err_url,
692 'triggers', $export_type,
693 $do_relation, $do_comments, $do_mime, $do_dates
694 )) {
695 break 2;
699 foreach ($views as $view) {
700 // no data export for a view
701 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
702 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
704 if (! $export_plugin->exportStructure(
705 $current_db, $view, $crlf, $err_url,
706 'create_view', $export_type,
707 $do_relation, $do_comments, $do_mime, $do_dates
708 )) {
709 break 3;
713 if (! $export_plugin->exportDBFooter($current_db)) {
714 break 2;
718 } elseif ($export_type == 'database') {
719 if (! $export_plugin->exportDBHeader($db)) {
720 break;
723 if (method_exists($export_plugin, 'exportRoutines')
724 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
725 && isset($GLOBALS['sql_procedure_function'])
727 $export_plugin->exportRoutines($db);
730 $i = 0;
731 $views = array();
732 // $tables contains the choices from the user (via $table_select)
733 foreach ($tables as $table) {
734 // if this is a view, collect it for later; views must be exported after
735 // the tables
736 $is_view = PMA_Table::isView($db, $table);
737 if ($is_view) {
738 $views[] = $table;
740 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
741 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
743 // for a view, export a stand-in definition of the table
744 // to resolve view dependencies
745 if (! $export_plugin->exportStructure(
746 $db, $table, $crlf, $err_url,
747 $is_view ? 'stand_in' : 'create_table', $export_type,
748 $do_relation, $do_comments, $do_mime, $do_dates
749 )) {
750 break 2;
753 // if this is a view or a merge table, don't export data
754 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
755 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
756 && ! ($is_view || PMA_Table::isMerge($db, $table))
758 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
759 . '.' . PMA_Util::backquote($table);
760 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
761 break 2;
764 // now export the triggers (needs to be done after the data because
765 // triggers can modify already imported tables)
766 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
767 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
769 if (! $export_plugin->exportStructure(
770 $db, $table, $crlf, $err_url,
771 'triggers', $export_type,
772 $do_relation, $do_comments, $do_mime, $do_dates
773 )) {
774 break 2;
778 foreach ($views as $view) {
779 // no data export for a view
780 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
781 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
783 if (! $export_plugin->exportStructure(
784 $db, $view, $crlf, $err_url,
785 'create_view', $export_type,
786 $do_relation, $do_comments, $do_mime, $do_dates
787 )) {
788 break 2;
793 if (! $export_plugin->exportDBFooter($db)) {
794 break;
796 } else {
797 if (! $export_plugin->exportDBHeader($db)) {
798 break;
800 // We export just one table
801 // $allrows comes from the form when "Dump all rows" has been selected
802 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
803 $add_query = ' LIMIT '
804 . (($limit_from > 0) ? $limit_from . ', ' : '')
805 . $limit_to;
806 } else {
807 $add_query = '';
810 $is_view = PMA_Table::isView($db, $table);
811 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
812 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
814 if (! $export_plugin->exportStructure(
815 $db, $table, $crlf, $err_url,
816 $is_view ? 'create_view' : 'create_table', $export_type,
817 $do_relation, $do_comments, $do_mime, $do_dates
818 )) {
819 break;
822 // If this is an export of a single view, we have to export data;
823 // for example, a PDF report
824 // if it is a merge table, no data is exported
825 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
826 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
827 && ! PMA_Table::isMerge($db, $table)
829 if (! empty($sql_query)) {
830 // only preg_replace if needed
831 if (! empty($add_query)) {
832 // remove trailing semicolon before adding a LIMIT
833 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
835 $local_query = $sql_query . $add_query;
836 PMA_DBI_selectDb($db);
837 } else {
838 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
839 . '.' . PMA_Util::backquote($table) . $add_query;
841 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
842 break;
845 // now export the triggers (needs to be done after the data because
846 // triggers can modify already imported tables)
847 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
848 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
850 if (! $export_plugin->exportStructure(
851 $db, $table, $crlf, $err_url,
852 'triggers', $export_type,
853 $do_relation, $do_comments, $do_mime, $do_dates
854 )) {
855 break 2;
858 if (! $export_plugin->exportDBFooter($db)) {
859 break;
862 if (! $export_plugin->exportFooter()) {
863 break;
866 } while (false);
867 // End of fake loop
869 if ($save_on_server && isset($message)) {
870 if ($export_type == 'server') {
871 $active_page = 'server_export.php';
872 include 'server_export.php';
873 } elseif ($export_type == 'database') {
874 $active_page = 'db_export.php';
875 include 'db_export.php';
876 } else {
877 $active_page = 'tbl_export.php';
878 include 'tbl_export.php';
880 exit();
884 * Send the dump as a file...
886 if (! empty($asfile)) {
887 // Convert the charset if required.
888 if ($output_charset_conversion) {
889 $dump_buffer = PMA_convertString(
890 'utf-8',
891 $GLOBALS['charset_of_file'],
892 $dump_buffer
896 // Do the compression
897 // 1. as a zipped file
898 if ($compression == 'zip') {
899 if (@function_exists('gzcompress')) {
900 $zipfile = new ZipFile();
901 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
902 $dump_buffer = $zipfile->file();
904 } elseif ($compression == 'bzip2') {
905 // 2. as a bzipped file
906 if (@function_exists('bzcompress')) {
907 $dump_buffer = bzcompress($dump_buffer);
909 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
910 // 3. as a gzipped file
911 // without the optional parameter level because it bugs
912 $dump_buffer = gzencode($dump_buffer);
915 /* If we saved on server, we have to close file now */
916 if ($save_on_server) {
917 $write_result = @fwrite($file_handle, $dump_buffer);
918 fclose($file_handle);
919 if (strlen($dump_buffer) > 0
920 && (! $write_result || ($write_result != strlen($dump_buffer)))
922 $message = new PMA_Message(
923 __('Insufficient space to save the file %s.'),
924 PMA_Message::ERROR,
925 $save_filename
927 } else {
928 $message = new PMA_Message(
929 __('Dump has been saved to file %s.'),
930 PMA_Message::SUCCESS,
931 $save_filename
935 if ($export_type == 'server') {
936 $active_page = 'server_export.php';
937 include_once 'server_export.php';
938 } elseif ($export_type == 'database') {
939 $active_page = 'db_export.php';
940 include_once 'db_export.php';
941 } else {
942 $active_page = 'tbl_export.php';
943 include_once 'tbl_export.php';
945 exit();
946 } else {
947 PMA_Response::getInstance()->disable();
948 echo $dump_buffer;
950 } else {
952 * Displays the dump...
954 * Close the html tags and add the footers if dump is displayed on screen
956 echo '</textarea>' . "\n"
957 . ' </form>' . "\n";
958 echo $back_button;
960 echo "\n";
961 echo '</div>' . "\n";
962 echo "\n";
964 <script type="text/javascript">
965 //<![CDATA[
966 var $body = $("body");
967 $("#textSQLDUMP")
968 .width($body.width() - 50)
969 .height($body.height() - 100);
970 //]]>
971 </script>
972 <?php
973 } // end if