Translated using Weblate (Bulgarian)
[phpmyadmin.git] / export.php
blobcac8b3c8b0f359a01488a5f09ff7fc6e7c4803f0
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 if (!defined('TESTSUITE')) {
13 /**
14 * If we are sending the export file (as opposed to just displaying it
15 * as text), we have to bypass the usual PMA_Response mechanism
17 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
18 define('PMA_BYPASS_GET_INSTANCE', 1);
20 include_once 'libraries/common.inc.php';
21 include_once 'libraries/zip.lib.php';
22 include_once 'libraries/plugin_interface.lib.php';
24 //check if it's the GET request to check export time out
25 if (isset($_GET['check_time_out'])) {
26 if (isset($_SESSION['pma_export_error'])) {
27 $err = $_SESSION['pma_export_error'];
28 unset($_SESSION['pma_export_error']);
29 echo $err;
30 } else {
31 echo "success";
33 exit;
35 /**
36 * Sets globals from $_POST
38 * - Please keep the parameters in order of their appearance in the form
39 * - Some of these parameters are not used, as the code below directly
40 * verifies from the superglobal $_POST or $_REQUEST
42 $post_params = array(
43 'db',
44 'table',
45 'single_table',
46 'export_type',
47 'export_method',
48 'quick_or_custom',
49 'db_select',
50 'table_select',
51 'limit_to',
52 'limit_from',
53 'allrows',
54 'output_format',
55 'filename_template',
56 'remember_template',
57 'charset_of_file',
58 'compression',
59 'what',
60 'knjenc',
61 'xkana',
62 'htmlword_structure_or_data',
63 'htmlword_null',
64 'htmlword_columns',
65 'mediawiki_structure_or_data',
66 'mediawiki_caption',
67 'pdf_report_title',
68 'pdf_structure_or_data',
69 'odt_structure_or_data',
70 'odt_relation',
71 'odt_comments',
72 'odt_mime',
73 'odt_columns',
74 'odt_null',
75 'codegen_structure_or_data',
76 'codegen_format',
77 'excel_null',
78 'excel_removeCRLF',
79 'excel_columns',
80 'excel_edition',
81 'excel_structure_or_data',
82 'yaml_structure_or_data',
83 'ods_null',
84 'ods_structure_or_data',
85 'ods_columns',
86 'json_structure_or_data',
87 'xml_structure_or_data',
88 'xml_export_functions',
89 'xml_export_procedures',
90 'xml_export_tables',
91 'xml_export_triggers',
92 'xml_export_views',
93 'xml_export_contents',
94 'texytext_structure_or_data',
95 'texytext_columns',
96 'texytext_null',
97 'phparray_structure_or_data',
98 'sql_include_comments',
99 'sql_header_comment',
100 'sql_dates',
101 'sql_relation',
102 'sql_mime',
103 'sql_use_transaction',
104 'sql_disable_fk',
105 'sql_compatibility',
106 'sql_structure_or_data',
107 'sql_create_database',
108 'sql_drop_table',
109 'sql_procedure_function',
110 'sql_create_table_statements',
111 'sql_if_not_exists',
112 'sql_auto_increment',
113 'sql_backquotes',
114 'sql_truncate',
115 'sql_delayed',
116 'sql_ignore',
117 'sql_type',
118 'sql_insert_syntax',
119 'sql_max_query_size',
120 'sql_hex_for_blob',
121 'sql_utc_time',
122 'sql_drop_database',
123 'csv_separator',
124 'csv_enclosed',
125 'csv_escaped',
126 'csv_terminated',
127 'csv_null',
128 'csv_removeCRLF',
129 'csv_columns',
130 'csv_structure_or_data',
131 // csv_replace should have been here but we use it directly from $_POST
132 'latex_caption',
133 'latex_structure_or_data',
134 'latex_structure_caption',
135 'latex_structure_continued_caption',
136 'latex_structure_label',
137 'latex_relation',
138 'latex_comments',
139 'latex_mime',
140 'latex_columns',
141 'latex_data_caption',
142 'latex_data_continued_caption',
143 'latex_data_label',
144 'latex_null'
147 foreach ($post_params as $one_post_param) {
148 if (isset($_POST[$one_post_param])) {
149 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
153 // sanitize this parameter which will be used below in a file inclusion
154 $what = PMA_securePath($what);
156 PMA_Util::checkParameters(array('what', 'export_type'));
158 // export class instance, not array of properties, as before
159 $export_plugin = PMA_getPlugin(
160 "export",
161 $what,
162 'libraries/plugins/export/',
163 array(
164 'export_type' => $export_type,
165 'single_table' => isset($single_table)
169 // Backward compatbility
170 $type = $what;
172 // Check export type
173 if (! isset($export_plugin)) {
174 PMA_fatalError(__('Bad type!'));
178 * valid compression methods
180 $compression_methods = array(
181 'zip',
182 'gzip'
186 * init and variable checking
188 $compression = false;
189 $onserver = false;
190 $save_on_server = false;
191 $buffer_needed = false;
193 // Is it a quick or custom export?
194 if ($_REQUEST['quick_or_custom'] == 'quick') {
195 $quick_export = true;
196 } else {
197 $quick_export = false;
200 if ($_REQUEST['output_format'] == 'astext') {
201 $asfile = false;
202 } else {
203 $asfile = true;
204 if (in_array($_REQUEST['compression'], $compression_methods)) {
205 $compression = $_REQUEST['compression'];
206 $buffer_needed = true;
208 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
209 || (! $quick_export && ! empty($_REQUEST['onserver']))
211 if ($quick_export) {
212 $onserver = $_REQUEST['quick_export_onserver'];
213 } else {
214 $onserver = $_REQUEST['onserver'];
216 // Will we save dump on server?
217 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
221 // Does export require to be into file?
222 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
223 $message = PMA_Message::error(
224 __('Selected export type has to be saved in file!')
226 if ($export_type == 'server') {
227 $active_page = 'server_export.php';
228 include 'server_export.php';
229 } elseif ($export_type == 'database') {
230 $active_page = 'db_export.php';
231 include 'db_export.php';
232 } else {
233 $active_page = 'tbl_export.php';
234 include 'tbl_export.php';
236 exit();
239 // Generate error url and check for needed variables
240 if ($export_type == 'server') {
241 $err_url = 'server_export.php?' . PMA_URL_getCommon();
242 } elseif ($export_type == 'database' && strlen($db)) {
243 $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
244 // Check if we have something to export
245 if (isset($table_select)) {
246 $tables = $table_select;
247 } else {
248 $tables = array();
250 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
251 $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
252 } else {
253 PMA_fatalError(__('Bad parameters!'));
257 * Increase time limit for script execution and initializes some variables
259 @set_time_limit($cfg['ExecTimeLimit']);
260 if (! empty($cfg['MemoryLimit'])) {
261 @ini_set('memory_limit', $cfg['MemoryLimit']);
263 register_shutdown_function('PMA_shutdown');
264 // Start with empty buffer
265 $dump_buffer = '';
266 $dump_buffer_len = 0;
268 // We send fake headers to avoid browser timeout when buffering
269 $time_start = time();
273 * Sets a session variable upon a possible fatal error during export
275 * @return void
277 function PMA_shutdown()
279 $a = error_get_last();
280 if ($a != null && strpos($a['message'], "execution time")) {
281 //write in partially downloaded file for future reference of user
282 print_r($a);
283 //set session variable to check if there was error while exporting
284 $_SESSION['pma_export_error'] = $a['message'];
288 * Detect ob_gzhandler
290 * @return bool
292 function PMA_isGzHandlerEnabled()
294 return in_array('ob_gzhandler', ob_list_handlers());
298 * Detect whether gzencode is needed; it might not be needed if
299 * the server is already compressing by itself
301 * @return bool Whether gzencode is needed
303 function PMA_gzencodeNeeded()
306 * We should gzencode only if the function exists
307 * but we don't want to compress twice, therefore
308 * gzencode only if transparent compression is not enabled
309 * and gz compression was not asked via $cfg['OBGzip']
310 * but transparent compression does not apply when saving to server
312 if (@function_exists('gzencode')
313 && ((! @ini_get('zlib.output_compression')
314 && ! PMA_isGzHandlerEnabled())
315 || $GLOBALS['save_on_server'])
317 return true;
318 } else {
319 return false;
324 * Output handler for all exports, if needed buffering, it stores data into
325 * $dump_buffer, otherwise it prints thems out.
327 * @param string $line the insert statement
329 * @return bool Whether output succeeded
331 function PMA_exportOutputHandler($line)
333 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
335 // Kanji encoding convert feature
336 if ($GLOBALS['output_kanji_conversion']) {
337 $line = PMA_Kanji_strConv(
338 $line,
339 $GLOBALS['knjenc'],
340 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
343 // If we have to buffer data, we will perform everything at once at the end
344 if ($GLOBALS['buffer_needed']) {
346 $dump_buffer .= $line;
347 if ($GLOBALS['onfly_compression']) {
349 $dump_buffer_len += strlen($line);
351 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
352 if ($GLOBALS['output_charset_conversion']) {
353 $dump_buffer = PMA_convertString(
354 'utf-8',
355 $GLOBALS['charset_of_file'],
356 $dump_buffer
359 if ($GLOBALS['compression'] == 'gzip'
360 && PMA_gzencodeNeeded()
362 // as a gzipped file
363 // without the optional parameter level because it bugs
364 $dump_buffer = gzencode($dump_buffer);
366 if ($GLOBALS['save_on_server']) {
367 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
368 if ($write_result != strlen($dump_buffer)) {
369 $GLOBALS['message'] = PMA_Message::error(
370 __('Insufficient space to save the file %s.')
372 $GLOBALS['message']->addParam($save_filename);
373 return false;
375 } else {
376 echo $dump_buffer;
378 $dump_buffer = '';
379 $dump_buffer_len = 0;
381 } else {
382 $time_now = time();
383 if ($time_start >= $time_now + 30) {
384 $time_start = $time_now;
385 header('X-pmaPing: Pong');
386 } // end if
388 } else {
389 if ($GLOBALS['asfile']) {
390 if ($GLOBALS['output_charset_conversion']) {
391 $line = PMA_convertString(
392 'utf-8',
393 $GLOBALS['charset_of_file'],
394 $line
397 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
398 $write_result = @fwrite($GLOBALS['file_handle'], $line);
399 if (! $write_result || ($write_result != strlen($line))) {
400 $GLOBALS['message'] = PMA_Message::error(
401 __('Insufficient space to save the file %s.')
403 $GLOBALS['message']->addParam($save_filename);
404 return false;
406 $time_now = time();
407 if ($time_start >= $time_now + 30) {
408 $time_start = $time_now;
409 header('X-pmaPing: Pong');
410 } // end if
411 } else {
412 // We export as file - output normally
413 echo $line;
415 } else {
416 // We export as html - replace special chars
417 echo htmlspecialchars($line);
420 return true;
421 } // end of the 'PMA_exportOutputHandler()' function
423 // Defines the default <CR><LF> format.
424 // For SQL always use \n as MySQL wants this on all platforms.
425 if (!defined('TESTSUITE')) {
426 if ($what == 'sql') {
427 $crlf = "\n";
428 } else {
429 $crlf = PMA_Util::whichCrlf();
432 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
433 && $type != 'xls';
435 // Do we need to convert charset?
436 $output_charset_conversion = $asfile
437 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
438 && isset($charset_of_file) && $charset_of_file != 'utf-8'
439 && $type != 'xls';
441 // Use on the fly compression?
442 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
443 && $compression == 'gzip';
444 if ($onfly_compression) {
445 $memory_limit = trim(@ini_get('memory_limit'));
446 $memory_limit_num = (int)substr($memory_limit, 0, -1);
447 // 2 MB as default
448 if (empty($memory_limit) || '-1' == $memory_limit) {
449 $memory_limit = 2 * 1024 * 1024;
450 } elseif (strtolower(substr($memory_limit, -1)) == 'm') {
451 $memory_limit = $memory_limit_num * 1024 * 1024;
452 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
453 $memory_limit = $memory_limit_num * 1024;
454 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
455 $memory_limit = $memory_limit_num * 1024 * 1024 * 1024;
456 } else {
457 $memory_limit = (int)$memory_limit;
460 // Some of memory is needed for other things and as threshold.
461 // During export I had allocated (see memory_get_usage function)
462 // approx 1.2MB so this comes from that.
463 if ($memory_limit > 1500000) {
464 $memory_limit -= 1500000;
467 // Some memory is needed for compression, assume 1/3
468 $memory_limit /= 8;
471 // Generate filename and mime type if needed
472 if ($asfile) {
473 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
474 if ($export_type == 'server') {
475 if (isset($remember_template)) {
476 $GLOBALS['PMA_Config']->setUserValue(
477 'pma_server_filename_template',
478 'Export/file_template_server',
479 $filename_template
482 } elseif ($export_type == 'database') {
483 if (isset($remember_template)) {
484 $GLOBALS['PMA_Config']->setUserValue(
485 'pma_db_filename_template',
486 'Export/file_template_database',
487 $filename_template
490 } else {
491 if (isset($remember_template)) {
492 $GLOBALS['PMA_Config']->setUserValue(
493 'pma_table_filename_template',
494 'Export/file_template_table',
495 $filename_template
499 $filename = PMA_Util::expandUserString($filename_template);
500 // remove dots in filename (coming from either the template or already
501 // part of the filename) to avoid a remote code execution vulnerability
502 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
504 // Grab basic dump extension and mime type
505 // Check if the user already added extension;
506 // get the substring where the extension would be if it was included
507 $extension_start_pos = strlen($filename) - strlen(
508 $export_plugin->getProperties()->getExtension()
509 ) - 1;
510 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
511 $required_extension = "." . $export_plugin->getProperties()->getExtension();
512 if (strtolower($user_extension) != $required_extension) {
513 $filename .= $required_extension;
515 $mime_type = $export_plugin->getProperties()->getMimeType();
517 // If dump is going to be compressed, set correct mime_type and add
518 // compression to extension
519 if ($compression == 'gzip') {
520 $filename .= '.gz';
521 $mime_type = 'application/x-gzip';
522 } elseif ($compression == 'zip') {
523 $filename .= '.zip';
524 $mime_type = 'application/zip';
528 // Open file on server if needed
529 if ($save_on_server) {
530 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
531 . preg_replace('@[/\\\\]@', '_', $filename);
532 unset($message);
533 if (file_exists($save_filename)
534 && ((! $quick_export && empty($_REQUEST['onserverover']))
535 || ($quick_export
536 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
538 $message = PMA_Message::error(
540 'File %s already exists on server, '
541 . 'change filename or check overwrite option.'
544 $message->addParam($save_filename);
545 } else {
546 if (is_file($save_filename) && ! is_writable($save_filename)) {
547 $message = PMA_Message::error(
549 'The web server does not have permission '
550 . 'to save the file %s.'
553 $message->addParam($save_filename);
554 } else {
555 if (! $file_handle = @fopen($save_filename, 'w')) {
556 $message = PMA_Message::error(
558 'The web server does not have permission '
559 . 'to save the file %s.'
562 $message->addParam($save_filename);
566 if (isset($message)) {
567 if ($export_type == 'server') {
568 $active_page = 'server_export.php';
569 include 'server_export.php';
570 } elseif ($export_type == 'database') {
571 $active_page = 'db_export.php';
572 include 'db_export.php';
573 } else {
574 $active_page = 'tbl_export.php';
575 include 'tbl_export.php';
577 exit();
582 * Send headers depending on whether the user chose to download a dump file
583 * or not
585 if (! $save_on_server) {
586 if ($asfile) {
587 // Download
588 // (avoid rewriting data containing HTML with anchors and forms;
589 // this was reported to happen under Plesk)
590 @ini_set('url_rewriter.tags', '');
591 $filename = PMA_sanitizeFilename($filename);
593 PMA_downloadHeader($filename, $mime_type);
594 } else {
595 // HTML
596 if ($export_type == 'database') {
597 $num_tables = count($tables);
598 if ($num_tables == 0) {
599 $message = PMA_Message::error(
600 __('No tables found in database.')
602 $active_page = 'db_export.php';
603 include 'db_export.php';
604 exit();
607 $backup_cfgServer = $cfg['Server'];
608 $cfg['Server'] = $backup_cfgServer;
609 unset($backup_cfgServer);
610 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
611 //echo ' <pre>' . "\n";
614 * Displays a back button with all the $_REQUEST data in the URL
615 * (store in a variable to also display after the textarea)
617 $back_button = '<p>[ <a href="';
618 if ($export_type == 'server') {
619 $back_button .= 'server_export.php?' . PMA_URL_getCommon();
620 } elseif ($export_type == 'database') {
621 $back_button .= 'db_export.php?' . PMA_URL_getCommon($db);
622 } else {
623 $back_button .= 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
626 // Convert the multiple select elements from an array to a string
627 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
628 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
629 } elseif ($export_type == 'database'
630 && isset($_REQUEST['table_select'])
632 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
635 foreach ($_REQUEST as $name => $value) {
636 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
638 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
640 echo $back_button;
641 echo '<form name="nofunction">' . "\n";
642 echo '<textarea name="sqldump" cols="50" rows="30" '
643 . 'id="textSQLDUMP" wrap="OFF">' . "\n";
644 } // end download
647 // Fake loop just to allow skip of remain of this code by break, I'd really
648 // need exceptions here :-)
649 do {
651 // Add possibly some comments to export
652 if (! $export_plugin->exportHeader($db)) {
653 break;
656 // Will we need relation & co. setup?
657 $do_relation = isset($GLOBALS[$what . '_relation']);
658 $do_comments = isset($GLOBALS[$what . '_include_comments'])
659 || isset($GLOBALS[$what . '_comments']) ;
660 $do_mime = isset($GLOBALS[$what . '_mime']);
661 if ($do_relation || $do_comments || $do_mime) {
662 $cfgRelation = PMA_getRelationsParam();
664 if ($do_mime) {
665 include_once 'libraries/transformations.lib.php';
668 // Include dates in export?
669 $do_dates = isset($GLOBALS[$what . '_dates']);
671 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
674 * Builds the dump
676 // Gets the number of tables if a dump of a database has been required
677 if ($export_type == 'server') {
678 if (isset($db_select)) {
679 $tmp_select = implode($db_select, '|');
680 $tmp_select = '|' . $tmp_select . '|';
682 // Walk over databases
683 foreach ($GLOBALS['pma']->databases as $current_db) {
684 if (isset($tmp_select)
685 && strpos(' ' . $tmp_select, '|' . $current_db . '|')
687 if (! $export_plugin->exportDBHeader($current_db)) {
688 break 2;
690 if (! $export_plugin->exportDBCreate($current_db)) {
691 break 2;
693 if (method_exists($export_plugin, 'exportRoutines')
694 && strpos($whatStrucOrData, 'structure') !== false
695 && isset($GLOBALS['sql_procedure_function'])
697 $export_plugin->exportRoutines($current_db);
700 $tables = $GLOBALS['dbi']->getTables($current_db);
701 $views = array();
702 foreach ($tables as $table) {
703 // if this is a view, collect it for later;
704 // views must be exported after the tables
705 $is_view = PMA_Table::isView($current_db, $table);
706 if ($is_view) {
707 $views[] = $table;
709 if ($whatStrucOrData == 'structure'
710 || $whatStrucOrData == 'structure_and_data'
712 // for a view, export a stand-in definition of the table
713 // to resolve view dependencies
714 if (! $export_plugin->exportStructure(
715 $current_db, $table, $crlf, $err_url,
716 $is_view ? 'stand_in' : 'create_table', $export_type,
717 $do_relation, $do_comments, $do_mime, $do_dates
718 )) {
719 break 3;
722 // if this is a view or a merge table, don't export data
723 if (($whatStrucOrData == 'data'
724 || $whatStrucOrData == 'structure_and_data')
725 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
727 $local_query = 'SELECT * FROM '
728 . PMA_Util::backquote($current_db)
729 . '.' . PMA_Util::backquote($table);
730 if (! $export_plugin->exportData(
731 $current_db, $table, $crlf, $err_url, $local_query
732 )) {
733 break 3;
736 // now export the triggers (needs to be done after the data
737 // because triggers can modify already imported tables)
738 if ($whatStrucOrData == 'structure'
739 || $whatStrucOrData == 'structure_and_data'
741 if (! $export_plugin->exportStructure(
742 $current_db, $table, $crlf, $err_url,
743 'triggers', $export_type,
744 $do_relation, $do_comments, $do_mime, $do_dates
745 )) {
746 break 2;
750 foreach ($views as $view) {
751 // no data export for a view
752 if ($whatStrucOrData == 'structure'
753 || $whatStrucOrData == 'structure_and_data'
755 if (! $export_plugin->exportStructure(
756 $current_db, $view, $crlf, $err_url,
757 'create_view', $export_type,
758 $do_relation, $do_comments, $do_mime, $do_dates
759 )) {
760 break 3;
764 if (! $export_plugin->exportDBFooter($current_db)) {
765 break 2;
769 } elseif ($export_type == 'database') {
770 if (! $export_plugin->exportDBHeader($db)) {
771 break;
773 if (! $export_plugin->exportDBCreate($db)) {
774 break;
777 if (method_exists($export_plugin, 'exportRoutines')
778 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
779 && isset($GLOBALS['sql_procedure_function'])
781 $export_plugin->exportRoutines($db);
784 $i = 0;
785 $views = array();
787 // $tables contains the choices from the user (via $table_select)
788 foreach ($tables as $table) {
789 // if this is a view, collect it for later;
790 // views must be exported after the tables
791 $is_view = PMA_Table::isView($db, $table);
792 if ($is_view) {
793 $views[] = $table;
795 if ($whatStrucOrData == 'structure'
796 || $whatStrucOrData == 'structure_and_data'
798 // for a view, export a stand-in definition of the table
799 // to resolve view dependencies
800 if (! $export_plugin->exportStructure(
801 $db, $table, $crlf, $err_url,
802 $is_view ? 'stand_in' : 'create_table', $export_type,
803 $do_relation, $do_comments, $do_mime, $do_dates
804 )) {
805 break 2;
808 // if this is a view or a merge table, don't export data
809 if (($whatStrucOrData == 'data'
810 || $whatStrucOrData == 'structure_and_data')
811 && ! ($is_view || PMA_Table::isMerge($db, $table))
813 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
814 . '.' . PMA_Util::backquote($table);
815 if (! $export_plugin->exportData(
816 $db, $table, $crlf, $err_url, $local_query
817 )) {
818 break 2;
821 // now export the triggers (needs to be done after the data because
822 // triggers can modify already imported tables)
823 if ($whatStrucOrData == 'structure'
824 || $whatStrucOrData == 'structure_and_data'
826 if (! $export_plugin->exportStructure(
827 $db, $table, $crlf, $err_url,
828 'triggers', $export_type,
829 $do_relation, $do_comments, $do_mime, $do_dates
830 )) {
831 break 2;
835 foreach ($views as $view) {
836 // no data export for a view
837 if ($whatStrucOrData == 'structure'
838 || $whatStrucOrData == 'structure_and_data'
840 if (! $export_plugin->exportStructure(
841 $db, $view, $crlf, $err_url,
842 'create_view', $export_type,
843 $do_relation, $do_comments, $do_mime, $do_dates
844 )) {
845 break 2;
850 if (! $export_plugin->exportDBFooter($db)) {
851 break;
853 } else {
854 if (! $export_plugin->exportDBHeader($db)) {
855 break;
857 // We export just one table
858 // $allrows comes from the form when "Dump all rows" has been selected
859 if (isset($allrows)
860 && $allrows == '0'
861 && $limit_to > 0
862 && $limit_from >= 0
864 $add_query = ' LIMIT '
865 . (($limit_from > 0) ? $limit_from . ', ' : '')
866 . $limit_to;
867 } else {
868 $add_query = '';
871 $is_view = PMA_Table::isView($db, $table);
872 if ($whatStrucOrData == 'structure'
873 || $whatStrucOrData == 'structure_and_data'
875 if (! $export_plugin->exportStructure(
876 $db, $table, $crlf, $err_url,
877 $is_view ? 'create_view' : 'create_table', $export_type,
878 $do_relation, $do_comments, $do_mime, $do_dates
879 )) {
880 break;
883 // If this is an export of a single view, we have to export data;
884 // for example, a PDF report
885 // if it is a merge table, no data is exported
886 if (($whatStrucOrData == 'data'
887 || $whatStrucOrData == 'structure_and_data')
888 && ! PMA_Table::isMerge($db, $table)
890 if (! empty($sql_query)) {
891 // only preg_replace if needed
892 if (! empty($add_query)) {
893 // remove trailing semicolon before adding a LIMIT
894 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
896 $local_query = $sql_query . $add_query;
897 $GLOBALS['dbi']->selectDb($db);
898 } else {
899 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
900 . '.' . PMA_Util::backquote($table) . $add_query;
902 if (! $export_plugin->exportData(
903 $db, $table, $crlf, $err_url, $local_query
904 )) {
905 break;
908 // now export the triggers (needs to be done after the data because
909 // triggers can modify already imported tables)
910 if ($whatStrucOrData == 'structure'
911 || $whatStrucOrData == 'structure_and_data'
913 if (! $export_plugin->exportStructure(
914 $db, $table, $crlf, $err_url,
915 'triggers', $export_type,
916 $do_relation, $do_comments, $do_mime, $do_dates
917 )) {
918 break 2;
921 if (! $export_plugin->exportDBFooter($db)) {
922 break;
925 if (! $export_plugin->exportFooter()) {
926 break;
929 } while (false);
930 // End of fake loop
932 if ($save_on_server && isset($message)) {
933 if ($export_type == 'server') {
934 $active_page = 'server_export.php';
935 include 'server_export.php';
936 } elseif ($export_type == 'database') {
937 $active_page = 'db_export.php';
938 include 'db_export.php';
939 } else {
940 $active_page = 'tbl_export.php';
941 include 'tbl_export.php';
943 exit();
947 * Send the dump as a file...
949 if (! empty($asfile)) {
950 // Convert the charset if required.
951 if ($output_charset_conversion) {
952 $dump_buffer = PMA_convertString(
953 'utf-8',
954 $GLOBALS['charset_of_file'],
955 $dump_buffer
959 // Do the compression
960 // 1. as a zipped file
961 if ($compression == 'zip') {
962 if (@function_exists('gzcompress')) {
963 $zipfile = new ZipFile();
964 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
965 $dump_buffer = $zipfile->file();
967 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
968 // 3. as a gzipped file
969 // without the optional parameter level because it bugs
970 $dump_buffer = gzencode($dump_buffer);
973 /* If we saved on server, we have to close file now */
974 if ($save_on_server) {
975 $write_result = @fwrite($file_handle, $dump_buffer);
976 fclose($file_handle);
977 if (strlen($dump_buffer) > 0
978 && (! $write_result || ($write_result != strlen($dump_buffer)))
980 $message = new PMA_Message(
981 __('Insufficient space to save the file %s.'),
982 PMA_Message::ERROR,
983 $save_filename
985 } else {
986 $message = new PMA_Message(
987 __('Dump has been saved to file %s.'),
988 PMA_Message::SUCCESS,
989 $save_filename
993 if ($export_type == 'server') {
994 $active_page = 'server_export.php';
995 include_once 'server_export.php';
996 } elseif ($export_type == 'database') {
997 $active_page = 'db_export.php';
998 include_once 'db_export.php';
999 } else {
1000 $active_page = 'tbl_export.php';
1001 include_once 'tbl_export.php';
1003 exit();
1004 } else {
1005 echo $dump_buffer;
1007 } else {
1009 * Displays the dump...
1011 * Close the html tags and add the footers if dump is displayed on screen
1013 echo '</textarea>' . "\n"
1014 . ' </form>' . "\n";
1015 echo $back_button;
1017 echo "\n";
1018 echo '</div>' . "\n";
1019 echo "\n";
1021 <script type="text/javascript">
1022 //<![CDATA[
1023 var $body = $("body");
1024 $("#textSQLDUMP")
1025 .width($body.width() - 50)
1026 .height($body.height() - 100);
1027 //]]>
1028 </script>
1029 <?php
1030 } // end if