Translated using Weblate (Swedish)
[phpmyadmin.git] / export.php
blob53574eaf0b9da4f1d7bb5025c1ac21be332bec4e
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 'table_select',
31 'limit_to',
32 'limit_from',
33 'allrows',
34 'output_format',
35 'filename_template',
36 'remember_template',
37 'charset_of_file',
38 'compression',
39 'what',
40 'htmlword_structure_or_data',
41 'htmlword_null',
42 'htmlword_columns',
43 'mediawiki_structure_or_data',
44 'mediawiki_caption',
45 'pdf_report_title',
46 'pdf_structure_or_data',
47 'odt_structure_or_data',
48 'odt_relation',
49 'odt_comments',
50 'odt_mime',
51 'odt_columns',
52 'odt_null',
53 'codegen_structure_or_data',
54 'codegen_format',
55 'excel_null',
56 'excel_columns',
57 'excel_edition',
58 'excel_structure_or_data',
59 'yaml_structure_or_data',
60 'ods_null',
61 'ods_structure_or_data',
62 'ods_columns',
63 'json_structure_or_data',
64 'xml_structure_or_data',
65 'xml_export_functions',
66 'xml_export_procedures',
67 'xml_export_tables',
68 'xml_export_triggers',
69 'xml_export_views',
70 'xml_export_contents',
71 'texytext_structure_or_data',
72 'texytext_columns',
73 'texytext_null',
74 'phparray_structure_or_data',
75 'sql_include_comments',
76 'sql_header_comment',
77 'sql_dates',
78 'sql_relation',
79 'sql_mime',
80 'sql_use_transaction',
81 'sql_disable_fk',
82 'sql_compatibility',
83 'sql_structure_or_data',
84 'sql_drop_table',
85 'sql_procedure_function',
86 'sql_create_table_statements',
87 'sql_if_not_exists',
88 'sql_auto_increment',
89 'sql_backquotes',
90 'sql_truncate',
91 'sql_delayed',
92 'sql_ignore',
93 'sql_type',
94 'sql_insert_syntax',
95 'sql_max_query_size',
96 'sql_hex_for_blob',
97 'sql_utc_time',
98 'csv_separator',
99 'csv_enclosed',
100 'csv_escaped',
101 'csv_terminated',
102 'csv_null',
103 'csv_columns',
104 'csv_structure_or_data',
105 'latex_caption',
106 'latex_structure_or_data',
107 'latex_structure_caption',
108 'latex_structure_continued_caption',
109 'latex_structure_label',
110 'latex_relation',
111 'latex_comments',
112 'latex_mime',
113 'latex_columns',
114 'latex_data_caption',
115 'latex_data_continued_caption',
116 'latex_data_label',
117 'latex_null'
120 foreach ($post_params as $one_post_param) {
121 if (isset($_POST[$one_post_param])) {
122 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
126 // sanitize this parameter which will be used below in a file inclusion
127 $what = PMA_securePath($what);
129 PMA_Util::checkParameters(array('what', 'export_type'));
131 // export class instance, not array of properties, as before
132 $export_plugin = PMA_getPlugin(
133 "export",
134 $what,
135 'libraries/plugins/export/',
136 array(
137 'export_type' => $export_type,
138 'single_table' => isset($single_table)
142 // Backward compatbility
143 $type = $what;
145 // Check export type
146 if (! isset($export_plugin)) {
147 PMA_fatalError(__('Bad type!'));
151 * valid compression methods
153 $compression_methods = array(
154 'zip',
155 'gzip',
156 'bzip2',
160 * init and variable checking
162 $compression = false;
163 $onserver = false;
164 $save_on_server = false;
165 $buffer_needed = false;
167 // Is it a quick or custom export?
168 if ($_REQUEST['quick_or_custom'] == 'quick') {
169 $quick_export = true;
170 } else {
171 $quick_export = false;
174 if ($_REQUEST['output_format'] == 'astext') {
175 $asfile = false;
176 } else {
177 $asfile = true;
178 if (in_array($_REQUEST['compression'], $compression_methods)) {
179 $compression = $_REQUEST['compression'];
180 $buffer_needed = true;
182 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
183 || (! $quick_export && ! empty($_REQUEST['onserver']))
185 if ($quick_export) {
186 $onserver = $_REQUEST['quick_export_onserver'];
187 } else {
188 $onserver = $_REQUEST['onserver'];
190 // Will we save dump on server?
191 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
195 // Does export require to be into file?
196 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
197 $message = PMA_Message::error(
198 __('Selected export type has to be saved in file!')
200 if ($export_type == 'server') {
201 $active_page = 'server_export.php';
202 include 'server_export.php';
203 } elseif ($export_type == 'database') {
204 $active_page = 'db_export.php';
205 include 'db_export.php';
206 } else {
207 $active_page = 'tbl_export.php';
208 include 'tbl_export.php';
210 exit();
213 // Generate error url and check for needed variables
214 if ($export_type == 'server') {
215 $err_url = 'server_export.php?' . PMA_generate_common_url();
216 } elseif ($export_type == 'database' && strlen($db)) {
217 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
218 // Check if we have something to export
219 if (isset($table_select)) {
220 $tables = $table_select;
221 } else {
222 $tables = array();
224 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
225 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
226 } else {
227 PMA_fatalError(__('Bad parameters!'));
231 * Increase time limit for script execution and initializes some variables
233 @set_time_limit($cfg['ExecTimeLimit']);
234 if (! empty($cfg['MemoryLimit'])) {
235 @ini_set('memory_limit', $cfg['MemoryLimit']);
238 // Start with empty buffer
239 $dump_buffer = '';
240 $dump_buffer_len = 0;
242 // We send fake headers to avoid browser timeout when buffering
243 $time_start = time();
247 * Detect ob_gzhandler
249 * @return bool
251 function PMA_isGzHandlerEnabled()
253 return in_array('ob_gzhandler', ob_list_handlers());
257 * Detect whether gzencode is needed; it might not be needed if
258 * the server is already compressing by itself
260 * @return bool Whether gzencode is needed
262 function PMA_gzencodeNeeded()
264 if (@function_exists('gzencode')
265 && ! @ini_get('zlib.output_compression')
266 // Here, we detect Apache's mod_deflate so we bet that
267 // this module is active for this instance of phpMyAdmin
268 // and therefore, will gzip encode the content
269 && ! (function_exists('apache_get_modules')
270 && in_array('mod_deflate', apache_get_modules()))
271 && ! PMA_isGzHandlerEnabled()
273 return true;
274 } else {
275 return false;
280 * Output handler for all exports, if needed buffering, it stores data into
281 * $dump_buffer, otherwise it prints thems out.
283 * @param string $line the insert statement
285 * @return bool Whether output succeeded
287 function PMA_exportOutputHandler($line)
289 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
291 // Kanji encoding convert feature
292 if ($GLOBALS['output_kanji_conversion']) {
293 $line = PMA_kanji_str_conv(
294 $line,
295 $GLOBALS['knjenc'],
296 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
299 // If we have to buffer data, we will perform everything at once at the end
300 if ($GLOBALS['buffer_needed']) {
302 $dump_buffer .= $line;
303 if ($GLOBALS['onfly_compression']) {
305 $dump_buffer_len += strlen($line);
307 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
308 if ($GLOBALS['output_charset_conversion']) {
309 $dump_buffer = PMA_convert_string(
310 'utf-8',
311 $GLOBALS['charset_of_file'],
312 $dump_buffer
315 // as bzipped
316 if ($GLOBALS['compression'] == 'bzip2'
317 && @function_exists('bzcompress')
319 $dump_buffer = bzcompress($dump_buffer);
320 } elseif ($GLOBALS['compression'] == 'gzip'
321 && PMA_gzencodeNeeded()
323 // as a gzipped file
324 // without the optional parameter level because it bugs
325 $dump_buffer = gzencode($dump_buffer);
327 if ($GLOBALS['save_on_server']) {
328 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
329 if (! $write_result || ($write_result != strlen($dump_buffer))) {
330 $GLOBALS['message'] = PMA_Message::error(
331 __('Insufficient space to save the file %s.')
333 $GLOBALS['message']->addParam($save_filename);
334 return false;
336 } else {
337 echo $dump_buffer;
339 $dump_buffer = '';
340 $dump_buffer_len = 0;
342 } else {
343 $time_now = time();
344 if ($time_start >= $time_now + 30) {
345 $time_start = $time_now;
346 header('X-pmaPing: Pong');
347 } // end if
349 } else {
350 if ($GLOBALS['asfile']) {
351 if ($GLOBALS['output_charset_conversion']) {
352 $line = PMA_convert_string(
353 'utf-8',
354 $GLOBALS['charset_of_file'],
355 $line
358 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
359 $write_result = @fwrite($GLOBALS['file_handle'], $line);
360 if (! $write_result || ($write_result != strlen($line))) {
361 $GLOBALS['message'] = PMA_Message::error(
362 __('Insufficient space to save the file %s.')
364 $GLOBALS['message']->addParam($save_filename);
365 return false;
367 $time_now = time();
368 if ($time_start >= $time_now + 30) {
369 $time_start = $time_now;
370 header('X-pmaPing: Pong');
371 } // end if
372 } else {
373 // We export as file - output normally
374 echo $line;
376 } else {
377 // We export as html - replace special chars
378 echo htmlspecialchars($line);
381 return true;
382 } // end of the 'PMA_exportOutputHandler()' function
384 // Defines the default <CR><LF> format.
385 // For SQL always use \n as MySQL wants this on all platforms.
386 if ($what == 'sql') {
387 $crlf = "\n";
388 } else {
389 $crlf = PMA_Util::whichCrlf();
392 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
394 // Do we need to convert charset?
395 $output_charset_conversion = $asfile
396 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
397 && isset($charset_of_file) && $charset_of_file != 'utf-8'
398 && $type != 'xls';
400 // Use on the fly compression?
401 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
402 && ($compression == 'gzip' || $compression == 'bzip2');
403 if ($onfly_compression) {
404 $memory_limit = trim(@ini_get('memory_limit'));
405 // 2 MB as default
406 if (empty($memory_limit)) {
407 $memory_limit = 2 * 1024 * 1024;
410 if (strtolower(substr($memory_limit, -1)) == 'm') {
411 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
412 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
413 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
414 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
415 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
416 } else {
417 $memory_limit = (int)$memory_limit;
420 // Some of memory is needed for other thins and as treshold.
421 // Nijel: During export I had allocated (see memory_get_usage function)
422 // approx 1.2MB so this comes from that.
423 if ($memory_limit > 1500000) {
424 $memory_limit -= 1500000;
427 // Some memory is needed for compression, assume 1/3
428 $memory_limit /= 8;
431 // Generate filename and mime type if needed
432 if ($asfile) {
433 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
434 if ($export_type == 'server') {
435 if (isset($remember_template)) {
436 $GLOBALS['PMA_Config']->setUserValue(
437 'pma_server_filename_template',
438 'Export/file_template_server',
439 $filename_template
442 } elseif ($export_type == 'database') {
443 if (isset($remember_template)) {
444 $GLOBALS['PMA_Config']->setUserValue(
445 'pma_db_filename_template',
446 'Export/file_template_database',
447 $filename_template
450 } else {
451 if (isset($remember_template)) {
452 $GLOBALS['PMA_Config']->setUserValue(
453 'pma_table_filename_template',
454 'Export/file_template_table',
455 $filename_template
459 $filename = PMA_Util::expandUserString($filename_template);
460 // remove dots in filename (coming from either the template or already
461 // part of the filename) to avoid a remote code execution vulnerability
462 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
464 // Grab basic dump extension and mime type
465 // Check if the user already added extension;
466 // get the substring where the extension would be if it was included
467 $extension_start_pos = strlen($filename) - strlen(
468 $export_plugin->getProperties()->getExtension()
469 ) - 1;
470 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
471 $required_extension = "." . $export_plugin->getProperties()->getExtension();
472 if (strtolower($user_extension) != $required_extension) {
473 $filename .= $required_extension;
475 $mime_type = $export_plugin->getProperties()->getMimeType();
477 // If dump is going to be compressed, set correct mime_type and add
478 // compression to extension
479 if ($compression == 'bzip2') {
480 $filename .= '.bz2';
481 $mime_type = 'application/x-bzip2';
482 } elseif ($compression == 'gzip') {
483 $filename .= '.gz';
484 $mime_type = 'application/x-gzip';
485 } elseif ($compression == 'zip') {
486 $filename .= '.zip';
487 $mime_type = 'application/zip';
491 // Open file on server if needed
492 if ($save_on_server) {
493 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
494 . preg_replace('@[/\\\\]@', '_', $filename);
495 unset($message);
496 if (file_exists($save_filename)
497 && ((! $quick_export && empty($onserverover))
498 || ($quick_export
499 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
501 $message = PMA_Message::error(
502 __('File %s already exists on server, change filename or check overwrite option.')
504 $message->addParam($save_filename);
505 } else {
506 if (is_file($save_filename) && ! is_writable($save_filename)) {
507 $message = PMA_Message::error(
508 __('The web server does not have permission to save the file %s.')
510 $message->addParam($save_filename);
511 } else {
512 if (! $file_handle = @fopen($save_filename, 'w')) {
513 $message = PMA_Message::error(
514 __('The web server does not have permission to save the file %s.')
516 $message->addParam($save_filename);
520 if (isset($message)) {
521 if ($export_type == 'server') {
522 $active_page = 'server_export.php';
523 include 'server_export.php';
524 } elseif ($export_type == 'database') {
525 $active_page = 'db_export.php';
526 include 'db_export.php';
527 } else {
528 $active_page = 'tbl_export.php';
529 include 'tbl_export.php';
531 exit();
536 * Send headers depending on whether the user chose to download a dump file
537 * or not
539 if (! $save_on_server) {
540 if ($asfile) {
541 // Download
542 // (avoid rewriting data containing HTML with anchors and forms;
543 // this was reported to happen under Plesk)
544 @ini_set('url_rewriter.tags', '');
545 $filename = PMA_sanitizeFilename($filename);
547 PMA_downloadHeader($filename, $mime_type);
548 } else {
549 // HTML
550 if ($export_type == 'database') {
551 $num_tables = count($tables);
552 if ($num_tables == 0) {
553 $message = PMA_Message::error(__('No tables found in database.'));
554 $active_page = 'db_export.php';
555 include 'db_export.php';
556 exit();
559 $backup_cfgServer = $cfg['Server'];
560 $cfg['Server'] = $backup_cfgServer;
561 unset($backup_cfgServer);
562 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
563 //echo ' <pre>' . "\n";
566 * Displays a back button with all the $_REQUEST data in the URL
567 * (store in a variable to also display after the textarea)
569 $back_button = '<p>[ <a href="';
570 if ($export_type == 'server') {
571 $back_button .= 'server_export.php?' . PMA_generate_common_url();
572 } elseif ($export_type == 'database') {
573 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
574 } else {
575 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
578 // Convert the multiple select elements from an array to a string
579 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
580 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
581 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
582 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
585 foreach ($_REQUEST as $name => $value) {
586 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
588 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
590 echo $back_button;
591 echo ' <form name="nofunction">' . "\n"
592 // remove auto-select for now: there is no way to select
593 // only a part of the text; anyway, it should obey
594 // $cfg['TextareaAutoSelect']
595 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
596 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
597 } // end download
600 // Fake loop just to allow skip of remain of this code by break, I'd really
601 // need exceptions here :-)
602 do {
604 // Add possibly some comments to export
605 if (! $export_plugin->exportHeader($db)) {
606 break;
609 // Will we need relation & co. setup?
610 $do_relation = isset($GLOBALS[$what . '_relation']);
611 $do_comments = isset($GLOBALS[$what . '_include_comments']);
612 $do_mime = isset($GLOBALS[$what . '_mime']);
613 if ($do_relation || $do_comments || $do_mime) {
614 $cfgRelation = PMA_getRelationsParam();
616 if ($do_mime) {
617 include_once 'libraries/transformations.lib.php';
620 // Include dates in export?
621 $do_dates = isset($GLOBALS[$what . '_dates']);
624 * Builds the dump
626 // Gets the number of tables if a dump of a database has been required
627 if ($export_type == 'server') {
628 if (isset($db_select)) {
629 $tmp_select = implode($db_select, '|');
630 $tmp_select = '|' . $tmp_select . '|';
632 // Walk over databases
633 foreach ($GLOBALS['pma']->databases as $current_db) {
634 if ((isset($tmp_select)
635 && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
636 || ! isset($tmp_select)
638 if (! $export_plugin->exportDBHeader($current_db)) {
639 break 2;
641 if (! $export_plugin->exportDBCreate($current_db)) {
642 break 2;
644 if (method_exists($export_plugin, 'exportRoutines')
645 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
646 && isset($GLOBALS['sql_procedure_function'])
648 $export_plugin->exportRoutines($current_db);
651 $tables = PMA_DBI_get_tables($current_db);
652 $views = array();
653 foreach ($tables as $table) {
654 // if this is a view, collect it for later;
655 // views must be exported after the tables
656 $is_view = PMA_Table::isView($current_db, $table);
657 if ($is_view) {
658 $views[] = $table;
660 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
661 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
663 // for a view, export a stand-in definition of the table
664 // to resolve view dependencies
665 if (! $export_plugin->exportStructure(
666 $current_db, $table, $crlf, $err_url,
667 $is_view ? 'stand_in' : 'create_table', $export_type,
668 $do_relation, $do_comments, $do_mime, $do_dates
669 )) {
670 break 3;
673 // if this is a view or a merge table, don't export data
674 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
675 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
676 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
678 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
679 . '.' . PMA_Util::backquote($table);
680 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
681 break 3;
684 // now export the triggers (needs to be done after the data
685 // because triggers can modify already imported tables)
686 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
687 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
689 if (! $export_plugin->exportStructure(
690 $current_db, $table, $crlf, $err_url,
691 'triggers', $export_type,
692 $do_relation, $do_comments, $do_mime, $do_dates
693 )) {
694 break 2;
698 foreach ($views as $view) {
699 // no data export for a view
700 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
701 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
703 if (! $export_plugin->exportStructure(
704 $current_db, $view, $crlf, $err_url,
705 'create_view', $export_type,
706 $do_relation, $do_comments, $do_mime, $do_dates
707 )) {
708 break 3;
712 if (! $export_plugin->exportDBFooter($current_db)) {
713 break 2;
717 } elseif ($export_type == 'database') {
718 if (! $export_plugin->exportDBHeader($db)) {
719 break;
722 if (method_exists($export_plugin, 'exportRoutines')
723 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
724 && isset($GLOBALS['sql_procedure_function'])
726 $export_plugin->exportRoutines($db);
729 $i = 0;
730 $views = array();
731 // $tables contains the choices from the user (via $table_select)
732 foreach ($tables as $table) {
733 // if this is a view, collect it for later; views must be exported after
734 // the tables
735 $is_view = PMA_Table::isView($db, $table);
736 if ($is_view) {
737 $views[] = $table;
739 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
740 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
742 // for a view, export a stand-in definition of the table
743 // to resolve view dependencies
744 if (! $export_plugin->exportStructure(
745 $db, $table, $crlf, $err_url,
746 $is_view ? 'stand_in' : 'create_table', $export_type,
747 $do_relation, $do_comments, $do_mime, $do_dates
748 )) {
749 break 2;
752 // if this is a view or a merge table, don't export data
753 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
754 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
755 && ! ($is_view || PMA_Table::isMerge($db, $table))
757 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
758 . '.' . PMA_Util::backquote($table);
759 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
760 break 2;
763 // now export the triggers (needs to be done after the data because
764 // triggers can modify already imported tables)
765 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
766 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
768 if (! $export_plugin->exportStructure(
769 $db, $table, $crlf, $err_url,
770 'triggers', $export_type,
771 $do_relation, $do_comments, $do_mime, $do_dates
772 )) {
773 break 2;
777 foreach ($views as $view) {
778 // no data export for a view
779 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
780 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
782 if (! $export_plugin->exportStructure(
783 $db, $view, $crlf, $err_url,
784 'create_view', $export_type,
785 $do_relation, $do_comments, $do_mime, $do_dates
786 )) {
787 break 2;
792 if (! $export_plugin->exportDBFooter($db)) {
793 break;
795 } else {
796 if (! $export_plugin->exportDBHeader($db)) {
797 break;
799 // We export just one table
800 // $allrows comes from the form when "Dump all rows" has been selected
801 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
802 $add_query = ' LIMIT '
803 . (($limit_from > 0) ? $limit_from . ', ' : '')
804 . $limit_to;
805 } else {
806 $add_query = '';
809 $is_view = PMA_Table::isView($db, $table);
810 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
811 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
813 if (! $export_plugin->exportStructure(
814 $db, $table, $crlf, $err_url,
815 $is_view ? 'create_view' : 'create_table', $export_type,
816 $do_relation, $do_comments, $do_mime, $do_dates
817 )) {
818 break;
821 // If this is an export of a single view, we have to export data;
822 // for example, a PDF report
823 // if it is a merge table, no data is exported
824 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
825 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
826 && ! PMA_Table::isMerge($db, $table)
828 if (! empty($sql_query)) {
829 // only preg_replace if needed
830 if (! empty($add_query)) {
831 // remove trailing semicolon before adding a LIMIT
832 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
834 $local_query = $sql_query . $add_query;
835 PMA_DBI_select_db($db);
836 } else {
837 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
838 . '.' . PMA_Util::backquote($table) . $add_query;
840 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
841 break;
844 // now export the triggers (needs to be done after the data because
845 // triggers can modify already imported tables)
846 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
847 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
849 if (! $export_plugin->exportStructure(
850 $db, $table, $crlf, $err_url,
851 'triggers', $export_type,
852 $do_relation, $do_comments, $do_mime, $do_dates
853 )) {
854 break 2;
857 if (! $export_plugin->exportDBFooter($db)) {
858 break;
861 if (! $export_plugin->exportFooter()) {
862 break;
865 } while (false);
866 // End of fake loop
868 if ($save_on_server && isset($message)) {
869 if ($export_type == 'server') {
870 $active_page = 'server_export.php';
871 include 'server_export.php';
872 } elseif ($export_type == 'database') {
873 $active_page = 'db_export.php';
874 include 'db_export.php';
875 } else {
876 $active_page = 'tbl_export.php';
877 include 'tbl_export.php';
879 exit();
883 * Send the dump as a file...
885 if (! empty($asfile)) {
886 // Convert the charset if required.
887 if ($output_charset_conversion) {
888 $dump_buffer = PMA_convert_string(
889 'utf-8',
890 $GLOBALS['charset_of_file'],
891 $dump_buffer
895 // Do the compression
896 // 1. as a zipped file
897 if ($compression == 'zip') {
898 if (@function_exists('gzcompress')) {
899 $zipfile = new ZipFile();
900 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
901 $dump_buffer = $zipfile->file();
903 } elseif ($compression == 'bzip2') {
904 // 2. as a bzipped file
905 if (@function_exists('bzcompress')) {
906 $dump_buffer = bzcompress($dump_buffer);
908 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
909 // 3. as a gzipped file
910 // without the optional parameter level because it bugs
911 $dump_buffer = gzencode($dump_buffer);
914 /* If we saved on server, we have to close file now */
915 if ($save_on_server) {
916 $write_result = @fwrite($file_handle, $dump_buffer);
917 fclose($file_handle);
918 if (strlen($dump_buffer) > 0
919 && (! $write_result || ($write_result != strlen($dump_buffer)))
921 $message = new PMA_Message(
922 __('Insufficient space to save the file %s.'),
923 PMA_Message::ERROR,
924 $save_filename
926 } else {
927 $message = new PMA_Message(
928 __('Dump has been saved to file %s.'),
929 PMA_Message::SUCCESS,
930 $save_filename
934 if ($export_type == 'server') {
935 $active_page = 'server_export.php';
936 include_once 'server_export.php';
937 } elseif ($export_type == 'database') {
938 $active_page = 'db_export.php';
939 include_once 'db_export.php';
940 } else {
941 $active_page = 'tbl_export.php';
942 include_once 'tbl_export.php';
944 exit();
945 } else {
946 PMA_Response::getInstance()->disable();
947 echo $dump_buffer;
949 } else {
951 * Displays the dump...
953 * Close the html tags and add the footers if dump is displayed on screen
955 echo '</textarea>' . "\n"
956 . ' </form>' . "\n";
957 echo $back_button;
959 echo "\n";
960 echo '</div>' . "\n";
961 echo "\n";
963 <script type="text/javascript">
964 //<![CDATA[
965 var $body = $("body");
966 $("#textSQLDUMP")
967 .width($body.width() - 50)
968 .height($body.height() - 100);
969 //]]>
970 </script>
971 <?php
972 } // end if