Translated using Weblate (Korean)
[phpmyadmin.git] / export.php
blob90064006ae2ceb32c226ad60b068cad42836ef18
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 // csv_replace should have been here but we use it directly from $_POST
107 'latex_caption',
108 'latex_structure_or_data',
109 'latex_structure_caption',
110 'latex_structure_continued_caption',
111 'latex_structure_label',
112 'latex_relation',
113 'latex_comments',
114 'latex_mime',
115 'latex_columns',
116 'latex_data_caption',
117 'latex_data_continued_caption',
118 'latex_data_label',
119 'latex_null'
122 foreach ($post_params as $one_post_param) {
123 if (isset($_POST[$one_post_param])) {
124 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
128 // sanitize this parameter which will be used below in a file inclusion
129 $what = PMA_securePath($what);
131 PMA_Util::checkParameters(array('what', 'export_type'));
133 // export class instance, not array of properties, as before
134 $export_plugin = PMA_getPlugin(
135 "export",
136 $what,
137 'libraries/plugins/export/',
138 array(
139 'export_type' => $export_type,
140 'single_table' => isset($single_table)
144 // Backward compatbility
145 $type = $what;
147 // Check export type
148 if (! isset($export_plugin)) {
149 PMA_fatalError(__('Bad type!'));
153 * valid compression methods
155 $compression_methods = array(
156 'zip',
157 'gzip',
158 'bzip2',
162 * init and variable checking
164 $compression = false;
165 $onserver = false;
166 $save_on_server = false;
167 $buffer_needed = false;
169 // Is it a quick or custom export?
170 if ($_REQUEST['quick_or_custom'] == 'quick') {
171 $quick_export = true;
172 } else {
173 $quick_export = false;
176 if ($_REQUEST['output_format'] == 'astext') {
177 $asfile = false;
178 } else {
179 $asfile = true;
180 if (in_array($_REQUEST['compression'], $compression_methods)) {
181 $compression = $_REQUEST['compression'];
182 $buffer_needed = true;
184 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
185 || (! $quick_export && ! empty($_REQUEST['onserver']))
187 if ($quick_export) {
188 $onserver = $_REQUEST['quick_export_onserver'];
189 } else {
190 $onserver = $_REQUEST['onserver'];
192 // Will we save dump on server?
193 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
197 // Does export require to be into file?
198 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
199 $message = PMA_Message::error(
200 __('Selected export type has to be saved in file!')
202 if ($export_type == 'server') {
203 $active_page = 'server_export.php';
204 include 'server_export.php';
205 } elseif ($export_type == 'database') {
206 $active_page = 'db_export.php';
207 include 'db_export.php';
208 } else {
209 $active_page = 'tbl_export.php';
210 include 'tbl_export.php';
212 exit();
215 // Generate error url and check for needed variables
216 if ($export_type == 'server') {
217 $err_url = 'server_export.php?' . PMA_generate_common_url();
218 } elseif ($export_type == 'database' && strlen($db)) {
219 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
220 // Check if we have something to export
221 if (isset($table_select)) {
222 $tables = $table_select;
223 } else {
224 $tables = array();
226 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
227 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
228 } else {
229 PMA_fatalError(__('Bad parameters!'));
233 * Increase time limit for script execution and initializes some variables
235 @set_time_limit($cfg['ExecTimeLimit']);
236 if (! empty($cfg['MemoryLimit'])) {
237 @ini_set('memory_limit', $cfg['MemoryLimit']);
240 // Start with empty buffer
241 $dump_buffer = '';
242 $dump_buffer_len = 0;
244 // We send fake headers to avoid browser timeout when buffering
245 $time_start = time();
249 * Detect ob_gzhandler
251 * @return bool
253 function PMA_isGzHandlerEnabled()
255 return in_array('ob_gzhandler', ob_list_handlers());
259 * Detect whether gzencode is needed; it might not be needed if
260 * the server is already compressing by itself
262 * @return bool Whether gzencode is needed
264 function PMA_gzencodeNeeded()
266 if (@function_exists('gzencode')
267 && ! @ini_get('zlib.output_compression')
268 // Here, we detect Apache's mod_deflate so we bet that
269 // this module is active for this instance of phpMyAdmin
270 // and therefore, will gzip encode the content
271 && ! (function_exists('apache_get_modules')
272 && in_array('mod_deflate', apache_get_modules()))
273 && ! PMA_isGzHandlerEnabled()
275 return true;
276 } else {
277 return false;
282 * Output handler for all exports, if needed buffering, it stores data into
283 * $dump_buffer, otherwise it prints thems out.
285 * @param string $line the insert statement
287 * @return bool Whether output succeeded
289 function PMA_exportOutputHandler($line)
291 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
293 // Kanji encoding convert feature
294 if ($GLOBALS['output_kanji_conversion']) {
295 $line = PMA_kanji_str_conv(
296 $line,
297 $GLOBALS['knjenc'],
298 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
301 // If we have to buffer data, we will perform everything at once at the end
302 if ($GLOBALS['buffer_needed']) {
304 $dump_buffer .= $line;
305 if ($GLOBALS['onfly_compression']) {
307 $dump_buffer_len += strlen($line);
309 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
310 if ($GLOBALS['output_charset_conversion']) {
311 $dump_buffer = PMA_convert_string(
312 'utf-8',
313 $GLOBALS['charset_of_file'],
314 $dump_buffer
317 // as bzipped
318 if ($GLOBALS['compression'] == 'bzip2'
319 && @function_exists('bzcompress')
321 $dump_buffer = bzcompress($dump_buffer);
322 } elseif ($GLOBALS['compression'] == 'gzip'
323 && PMA_gzencodeNeeded()
325 // as a gzipped file
326 // without the optional parameter level because it bugs
327 $dump_buffer = gzencode($dump_buffer);
329 if ($GLOBALS['save_on_server']) {
330 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
331 if (! $write_result || ($write_result != strlen($dump_buffer))) {
332 $GLOBALS['message'] = PMA_Message::error(
333 __('Insufficient space to save the file %s.')
335 $GLOBALS['message']->addParam($save_filename);
336 return false;
338 } else {
339 echo $dump_buffer;
341 $dump_buffer = '';
342 $dump_buffer_len = 0;
344 } else {
345 $time_now = time();
346 if ($time_start >= $time_now + 30) {
347 $time_start = $time_now;
348 header('X-pmaPing: Pong');
349 } // end if
351 } else {
352 if ($GLOBALS['asfile']) {
353 if ($GLOBALS['output_charset_conversion']) {
354 $line = PMA_convert_string(
355 'utf-8',
356 $GLOBALS['charset_of_file'],
357 $line
360 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
361 $write_result = @fwrite($GLOBALS['file_handle'], $line);
362 if (! $write_result || ($write_result != strlen($line))) {
363 $GLOBALS['message'] = PMA_Message::error(
364 __('Insufficient space to save the file %s.')
366 $GLOBALS['message']->addParam($save_filename);
367 return false;
369 $time_now = time();
370 if ($time_start >= $time_now + 30) {
371 $time_start = $time_now;
372 header('X-pmaPing: Pong');
373 } // end if
374 } else {
375 // We export as file - output normally
376 echo $line;
378 } else {
379 // We export as html - replace special chars
380 echo htmlspecialchars($line);
383 return true;
384 } // end of the 'PMA_exportOutputHandler()' function
386 // Defines the default <CR><LF> format.
387 // For SQL always use \n as MySQL wants this on all platforms.
388 if ($what == 'sql') {
389 $crlf = "\n";
390 } else {
391 $crlf = PMA_Util::whichCrlf();
394 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
396 // Do we need to convert charset?
397 $output_charset_conversion = $asfile
398 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
399 && isset($charset_of_file) && $charset_of_file != 'utf-8'
400 && $type != 'xls';
402 // Use on the fly compression?
403 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
404 && ($compression == 'gzip' || $compression == 'bzip2');
405 if ($onfly_compression) {
406 $memory_limit = trim(@ini_get('memory_limit'));
407 // 2 MB as default
408 if (empty($memory_limit)) {
409 $memory_limit = 2 * 1024 * 1024;
412 if (strtolower(substr($memory_limit, -1)) == 'm') {
413 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
414 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
415 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
416 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
417 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
418 } else {
419 $memory_limit = (int)$memory_limit;
422 // Some of memory is needed for other thins and as treshold.
423 // Nijel: During export I had allocated (see memory_get_usage function)
424 // approx 1.2MB so this comes from that.
425 if ($memory_limit > 1500000) {
426 $memory_limit -= 1500000;
429 // Some memory is needed for compression, assume 1/3
430 $memory_limit /= 8;
433 // Generate filename and mime type if needed
434 if ($asfile) {
435 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
436 if ($export_type == 'server') {
437 if (isset($remember_template)) {
438 $GLOBALS['PMA_Config']->setUserValue(
439 'pma_server_filename_template',
440 'Export/file_template_server',
441 $filename_template
444 } elseif ($export_type == 'database') {
445 if (isset($remember_template)) {
446 $GLOBALS['PMA_Config']->setUserValue(
447 'pma_db_filename_template',
448 'Export/file_template_database',
449 $filename_template
452 } else {
453 if (isset($remember_template)) {
454 $GLOBALS['PMA_Config']->setUserValue(
455 'pma_table_filename_template',
456 'Export/file_template_table',
457 $filename_template
461 $filename = PMA_Util::expandUserString($filename_template);
462 // remove dots in filename (coming from either the template or already
463 // part of the filename) to avoid a remote code execution vulnerability
464 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
466 // Grab basic dump extension and mime type
467 // Check if the user already added extension;
468 // get the substring where the extension would be if it was included
469 $extension_start_pos = strlen($filename) - strlen(
470 $export_plugin->getProperties()->getExtension()
471 ) - 1;
472 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
473 $required_extension = "." . $export_plugin->getProperties()->getExtension();
474 if (strtolower($user_extension) != $required_extension) {
475 $filename .= $required_extension;
477 $mime_type = $export_plugin->getProperties()->getMimeType();
479 // If dump is going to be compressed, set correct mime_type and add
480 // compression to extension
481 if ($compression == 'bzip2') {
482 $filename .= '.bz2';
483 $mime_type = 'application/x-bzip2';
484 } elseif ($compression == 'gzip') {
485 $filename .= '.gz';
486 $mime_type = 'application/x-gzip';
487 } elseif ($compression == 'zip') {
488 $filename .= '.zip';
489 $mime_type = 'application/zip';
493 // Open file on server if needed
494 if ($save_on_server) {
495 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
496 . preg_replace('@[/\\\\]@', '_', $filename);
497 unset($message);
498 if (file_exists($save_filename)
499 && ((! $quick_export && empty($onserverover))
500 || ($quick_export
501 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
503 $message = PMA_Message::error(
504 __('File %s already exists on server, change filename or check overwrite option.')
506 $message->addParam($save_filename);
507 } else {
508 if (is_file($save_filename) && ! is_writable($save_filename)) {
509 $message = PMA_Message::error(
510 __('The web server does not have permission to save the file %s.')
512 $message->addParam($save_filename);
513 } else {
514 if (! $file_handle = @fopen($save_filename, 'w')) {
515 $message = PMA_Message::error(
516 __('The web server does not have permission to save the file %s.')
518 $message->addParam($save_filename);
522 if (isset($message)) {
523 if ($export_type == 'server') {
524 $active_page = 'server_export.php';
525 include 'server_export.php';
526 } elseif ($export_type == 'database') {
527 $active_page = 'db_export.php';
528 include 'db_export.php';
529 } else {
530 $active_page = 'tbl_export.php';
531 include 'tbl_export.php';
533 exit();
538 * Send headers depending on whether the user chose to download a dump file
539 * or not
541 if (! $save_on_server) {
542 if ($asfile) {
543 // Download
544 // (avoid rewriting data containing HTML with anchors and forms;
545 // this was reported to happen under Plesk)
546 @ini_set('url_rewriter.tags', '');
547 $filename = PMA_sanitizeFilename($filename);
549 PMA_downloadHeader($filename, $mime_type);
550 } else {
551 // HTML
552 if ($export_type == 'database') {
553 $num_tables = count($tables);
554 if ($num_tables == 0) {
555 $message = PMA_Message::error(__('No tables found in database.'));
556 $active_page = 'db_export.php';
557 include 'db_export.php';
558 exit();
561 $backup_cfgServer = $cfg['Server'];
562 $cfg['Server'] = $backup_cfgServer;
563 unset($backup_cfgServer);
564 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
565 //echo ' <pre>' . "\n";
568 * Displays a back button with all the $_REQUEST data in the URL
569 * (store in a variable to also display after the textarea)
571 $back_button = '<p>[ <a href="';
572 if ($export_type == 'server') {
573 $back_button .= 'server_export.php?' . PMA_generate_common_url();
574 } elseif ($export_type == 'database') {
575 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
576 } else {
577 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
580 // Convert the multiple select elements from an array to a string
581 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
582 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
583 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
584 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
587 foreach ($_REQUEST as $name => $value) {
588 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
590 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
592 echo $back_button;
593 echo ' <form name="nofunction">' . "\n"
594 // remove auto-select for now: there is no way to select
595 // only a part of the text; anyway, it should obey
596 // $cfg['TextareaAutoSelect']
597 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
598 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
599 } // end download
602 // Fake loop just to allow skip of remain of this code by break, I'd really
603 // need exceptions here :-)
604 do {
606 // Add possibly some comments to export
607 if (! $export_plugin->exportHeader($db)) {
608 break;
611 // Will we need relation & co. setup?
612 $do_relation = isset($GLOBALS[$what . '_relation']);
613 $do_comments = isset($GLOBALS[$what . '_include_comments']);
614 $do_mime = isset($GLOBALS[$what . '_mime']);
615 if ($do_relation || $do_comments || $do_mime) {
616 $cfgRelation = PMA_getRelationsParam();
618 if ($do_mime) {
619 include_once 'libraries/transformations.lib.php';
622 // Include dates in export?
623 $do_dates = isset($GLOBALS[$what . '_dates']);
626 * Builds the dump
628 // Gets the number of tables if a dump of a database has been required
629 if ($export_type == 'server') {
630 if (isset($db_select)) {
631 $tmp_select = implode($db_select, '|');
632 $tmp_select = '|' . $tmp_select . '|';
634 // Walk over databases
635 foreach ($GLOBALS['pma']->databases as $current_db) {
636 if ((isset($tmp_select)
637 && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
638 || ! isset($tmp_select)
640 if (! $export_plugin->exportDBHeader($current_db)) {
641 break 2;
643 if (! $export_plugin->exportDBCreate($current_db)) {
644 break 2;
646 if (method_exists($export_plugin, 'exportRoutines')
647 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
648 && isset($GLOBALS['sql_procedure_function'])
650 $export_plugin->exportRoutines($current_db);
653 $tables = PMA_DBI_get_tables($current_db);
654 $views = array();
655 foreach ($tables as $table) {
656 // if this is a view, collect it for later;
657 // views must be exported after the tables
658 $is_view = PMA_Table::isView($current_db, $table);
659 if ($is_view) {
660 $views[] = $table;
662 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
663 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
665 // for a view, export a stand-in definition of the table
666 // to resolve view dependencies
667 if (! $export_plugin->exportStructure(
668 $current_db, $table, $crlf, $err_url,
669 $is_view ? 'stand_in' : 'create_table', $export_type,
670 $do_relation, $do_comments, $do_mime, $do_dates
671 )) {
672 break 3;
675 // if this is a view or a merge table, don't export data
676 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
677 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
678 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
680 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
681 . '.' . PMA_Util::backquote($table);
682 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
683 break 3;
686 // now export the triggers (needs to be done after the data
687 // because triggers can modify already imported tables)
688 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
689 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
691 if (! $export_plugin->exportStructure(
692 $current_db, $table, $crlf, $err_url,
693 'triggers', $export_type,
694 $do_relation, $do_comments, $do_mime, $do_dates
695 )) {
696 break 2;
700 foreach ($views as $view) {
701 // no data export for a view
702 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
703 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
705 if (! $export_plugin->exportStructure(
706 $current_db, $view, $crlf, $err_url,
707 'create_view', $export_type,
708 $do_relation, $do_comments, $do_mime, $do_dates
709 )) {
710 break 3;
714 if (! $export_plugin->exportDBFooter($current_db)) {
715 break 2;
719 } elseif ($export_type == 'database') {
720 if (! $export_plugin->exportDBHeader($db)) {
721 break;
724 if (method_exists($export_plugin, 'exportRoutines')
725 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
726 && isset($GLOBALS['sql_procedure_function'])
728 $export_plugin->exportRoutines($db);
731 $i = 0;
732 $views = array();
733 // $tables contains the choices from the user (via $table_select)
734 foreach ($tables as $table) {
735 // if this is a view, collect it for later; views must be exported after
736 // the tables
737 $is_view = PMA_Table::isView($db, $table);
738 if ($is_view) {
739 $views[] = $table;
741 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
742 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
744 // for a view, export a stand-in definition of the table
745 // to resolve view dependencies
746 if (! $export_plugin->exportStructure(
747 $db, $table, $crlf, $err_url,
748 $is_view ? 'stand_in' : 'create_table', $export_type,
749 $do_relation, $do_comments, $do_mime, $do_dates
750 )) {
751 break 2;
754 // if this is a view or a merge table, don't export data
755 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
756 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
757 && ! ($is_view || PMA_Table::isMerge($db, $table))
759 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
760 . '.' . PMA_Util::backquote($table);
761 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
762 break 2;
765 // now export the triggers (needs to be done after the data because
766 // triggers can modify already imported tables)
767 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
768 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
770 if (! $export_plugin->exportStructure(
771 $db, $table, $crlf, $err_url,
772 'triggers', $export_type,
773 $do_relation, $do_comments, $do_mime, $do_dates
774 )) {
775 break 2;
779 foreach ($views as $view) {
780 // no data export for a view
781 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
782 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
784 if (! $export_plugin->exportStructure(
785 $db, $view, $crlf, $err_url,
786 'create_view', $export_type,
787 $do_relation, $do_comments, $do_mime, $do_dates
788 )) {
789 break 2;
794 if (! $export_plugin->exportDBFooter($db)) {
795 break;
797 } else {
798 if (! $export_plugin->exportDBHeader($db)) {
799 break;
801 // We export just one table
802 // $allrows comes from the form when "Dump all rows" has been selected
803 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
804 $add_query = ' LIMIT '
805 . (($limit_from > 0) ? $limit_from . ', ' : '')
806 . $limit_to;
807 } else {
808 $add_query = '';
811 $is_view = PMA_Table::isView($db, $table);
812 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
813 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
815 if (! $export_plugin->exportStructure(
816 $db, $table, $crlf, $err_url,
817 $is_view ? 'create_view' : 'create_table', $export_type,
818 $do_relation, $do_comments, $do_mime, $do_dates
819 )) {
820 break;
823 // If this is an export of a single view, we have to export data;
824 // for example, a PDF report
825 // if it is a merge table, no data is exported
826 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
827 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
828 && ! PMA_Table::isMerge($db, $table)
830 if (! empty($sql_query)) {
831 // only preg_replace if needed
832 if (! empty($add_query)) {
833 // remove trailing semicolon before adding a LIMIT
834 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
836 $local_query = $sql_query . $add_query;
837 PMA_DBI_select_db($db);
838 } else {
839 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
840 . '.' . PMA_Util::backquote($table) . $add_query;
842 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
843 break;
846 // now export the triggers (needs to be done after the data because
847 // triggers can modify already imported tables)
848 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
849 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
851 if (! $export_plugin->exportStructure(
852 $db, $table, $crlf, $err_url,
853 'triggers', $export_type,
854 $do_relation, $do_comments, $do_mime, $do_dates
855 )) {
856 break 2;
859 if (! $export_plugin->exportDBFooter($db)) {
860 break;
863 if (! $export_plugin->exportFooter()) {
864 break;
867 } while (false);
868 // End of fake loop
870 if ($save_on_server && isset($message)) {
871 if ($export_type == 'server') {
872 $active_page = 'server_export.php';
873 include 'server_export.php';
874 } elseif ($export_type == 'database') {
875 $active_page = 'db_export.php';
876 include 'db_export.php';
877 } else {
878 $active_page = 'tbl_export.php';
879 include 'tbl_export.php';
881 exit();
885 * Send the dump as a file...
887 if (! empty($asfile)) {
888 // Convert the charset if required.
889 if ($output_charset_conversion) {
890 $dump_buffer = PMA_convert_string(
891 'utf-8',
892 $GLOBALS['charset_of_file'],
893 $dump_buffer
897 // Do the compression
898 // 1. as a zipped file
899 if ($compression == 'zip') {
900 if (@function_exists('gzcompress')) {
901 $zipfile = new ZipFile();
902 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
903 $dump_buffer = $zipfile->file();
905 } elseif ($compression == 'bzip2') {
906 // 2. as a bzipped file
907 if (@function_exists('bzcompress')) {
908 $dump_buffer = bzcompress($dump_buffer);
910 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
911 // 3. as a gzipped file
912 // without the optional parameter level because it bugs
913 $dump_buffer = gzencode($dump_buffer);
916 /* If we saved on server, we have to close file now */
917 if ($save_on_server) {
918 $write_result = @fwrite($file_handle, $dump_buffer);
919 fclose($file_handle);
920 if (strlen($dump_buffer) > 0
921 && (! $write_result || ($write_result != strlen($dump_buffer)))
923 $message = new PMA_Message(
924 __('Insufficient space to save the file %s.'),
925 PMA_Message::ERROR,
926 $save_filename
928 } else {
929 $message = new PMA_Message(
930 __('Dump has been saved to file %s.'),
931 PMA_Message::SUCCESS,
932 $save_filename
936 if ($export_type == 'server') {
937 $active_page = 'server_export.php';
938 include_once 'server_export.php';
939 } elseif ($export_type == 'database') {
940 $active_page = 'db_export.php';
941 include_once 'db_export.php';
942 } else {
943 $active_page = 'tbl_export.php';
944 include_once 'tbl_export.php';
946 exit();
947 } else {
948 PMA_Response::getInstance()->disable();
949 echo $dump_buffer;
951 } else {
953 * Displays the dump...
955 * Close the html tags and add the footers if dump is displayed on screen
957 echo '</textarea>' . "\n"
958 . ' </form>' . "\n";
959 echo $back_button;
961 echo "\n";
962 echo '</div>' . "\n";
963 echo "\n";
965 <script type="text/javascript">
966 //<![CDATA[
967 var $body = $("body");
968 $("#textSQLDUMP")
969 .width($body.width() - 50)
970 .height($body.height() - 100);
971 //]]>
972 </script>
973 <?php
974 } // end if