Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / export.php
blobddcd25b31a511b48fbb57d0e720dbc89db6b501f
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 ($_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 /**
25 * Sets globals from $_POST
27 * - Please keep the parameters in order of their appearance in the form
28 * - Some of these parameters are not used, as the code below directly
29 * verifies from the superglobal $_POST or $_REQUEST
31 $post_params = array(
32 'db',
33 'table',
34 'single_table',
35 'export_type',
36 'export_method',
37 'quick_or_custom',
38 'db_select',
39 'table_select',
40 'limit_to',
41 'limit_from',
42 'allrows',
43 'output_format',
44 'filename_template',
45 'remember_template',
46 'charset_of_file',
47 'compression',
48 'what',
49 'htmlword_structure_or_data',
50 'htmlword_null',
51 'htmlword_columns',
52 'mediawiki_structure_or_data',
53 'mediawiki_caption',
54 'pdf_report_title',
55 'pdf_structure_or_data',
56 'odt_structure_or_data',
57 'odt_relation',
58 'odt_comments',
59 'odt_mime',
60 'odt_columns',
61 'odt_null',
62 'codegen_structure_or_data',
63 'codegen_format',
64 'excel_null',
65 'excel_removeCRLF',
66 'excel_columns',
67 'excel_edition',
68 'excel_structure_or_data',
69 'yaml_structure_or_data',
70 'ods_null',
71 'ods_structure_or_data',
72 'ods_columns',
73 'json_structure_or_data',
74 'xml_structure_or_data',
75 'xml_export_functions',
76 'xml_export_procedures',
77 'xml_export_tables',
78 'xml_export_triggers',
79 'xml_export_views',
80 'xml_export_contents',
81 'texytext_structure_or_data',
82 'texytext_columns',
83 'texytext_null',
84 'phparray_structure_or_data',
85 'sql_include_comments',
86 'sql_header_comment',
87 'sql_dates',
88 'sql_relation',
89 'sql_mime',
90 'sql_use_transaction',
91 'sql_disable_fk',
92 'sql_compatibility',
93 'sql_structure_or_data',
94 'sql_create_database',
95 'sql_drop_table',
96 'sql_procedure_function',
97 'sql_create_table_statements',
98 'sql_if_not_exists',
99 'sql_auto_increment',
100 'sql_backquotes',
101 'sql_truncate',
102 'sql_delayed',
103 'sql_ignore',
104 'sql_type',
105 'sql_insert_syntax',
106 'sql_max_query_size',
107 'sql_hex_for_blob',
108 'sql_utc_time',
109 'csv_separator',
110 'csv_enclosed',
111 'csv_escaped',
112 'csv_terminated',
113 'csv_null',
114 'csv_removeCRLF',
115 'csv_columns',
116 'csv_structure_or_data',
117 // csv_replace should have been here but we use it directly from $_POST
118 'latex_caption',
119 'latex_structure_or_data',
120 'latex_structure_caption',
121 'latex_structure_continued_caption',
122 'latex_structure_label',
123 'latex_relation',
124 'latex_comments',
125 'latex_mime',
126 'latex_columns',
127 'latex_data_caption',
128 'latex_data_continued_caption',
129 'latex_data_label',
130 'latex_null'
133 foreach ($post_params as $one_post_param) {
134 if (isset($_POST[$one_post_param])) {
135 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
139 // sanitize this parameter which will be used below in a file inclusion
140 $what = PMA_securePath($what);
142 PMA_Util::checkParameters(array('what', 'export_type'));
144 // export class instance, not array of properties, as before
145 $export_plugin = PMA_getPlugin(
146 "export",
147 $what,
148 'libraries/plugins/export/',
149 array(
150 'export_type' => $export_type,
151 'single_table' => isset($single_table)
155 // Backward compatbility
156 $type = $what;
158 // Check export type
159 if (! isset($export_plugin)) {
160 PMA_fatalError(__('Bad type!'));
164 * valid compression methods
166 $compression_methods = array(
167 'zip',
168 'gzip',
169 'bzip2',
173 * init and variable checking
175 $compression = false;
176 $onserver = false;
177 $save_on_server = false;
178 $buffer_needed = false;
180 // Is it a quick or custom export?
181 if ($_REQUEST['quick_or_custom'] == 'quick') {
182 $quick_export = true;
183 } else {
184 $quick_export = false;
187 if ($_REQUEST['output_format'] == 'astext') {
188 $asfile = false;
189 } else {
190 $asfile = true;
191 if (in_array($_REQUEST['compression'], $compression_methods)) {
192 $compression = $_REQUEST['compression'];
193 $buffer_needed = true;
195 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
196 || (! $quick_export && ! empty($_REQUEST['onserver']))
198 if ($quick_export) {
199 $onserver = $_REQUEST['quick_export_onserver'];
200 } else {
201 $onserver = $_REQUEST['onserver'];
203 // Will we save dump on server?
204 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
208 // Does export require to be into file?
209 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
210 $message = PMA_Message::error(
211 __('Selected export type has to be saved in file!')
213 if ($export_type == 'server') {
214 $active_page = 'server_export.php';
215 include 'server_export.php';
216 } elseif ($export_type == 'database') {
217 $active_page = 'db_export.php';
218 include 'db_export.php';
219 } else {
220 $active_page = 'tbl_export.php';
221 include 'tbl_export.php';
223 exit();
226 // Generate error url and check for needed variables
227 if ($export_type == 'server') {
228 $err_url = 'server_export.php?' . PMA_URL_getCommon();
229 } elseif ($export_type == 'database' && strlen($db)) {
230 $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
231 // Check if we have something to export
232 if (isset($table_select)) {
233 $tables = $table_select;
234 } else {
235 $tables = array();
237 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
238 $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
239 } else {
240 PMA_fatalError(__('Bad parameters!'));
244 * Increase time limit for script execution and initializes some variables
246 @set_time_limit($cfg['ExecTimeLimit']);
247 if (! empty($cfg['MemoryLimit'])) {
248 @ini_set('memory_limit', $cfg['MemoryLimit']);
251 // Start with empty buffer
252 $dump_buffer = '';
253 $dump_buffer_len = 0;
255 // We send fake headers to avoid browser timeout when buffering
256 $time_start = time();
260 * Detect ob_gzhandler
262 * @return bool
264 function PMA_isGzHandlerEnabled()
266 return in_array('ob_gzhandler', ob_list_handlers());
270 * Detect whether gzencode is needed; it might not be needed if
271 * the server is already compressing by itself
273 * @return bool Whether gzencode is needed
275 function PMA_gzencodeNeeded()
277 if (@function_exists('gzencode')
278 && ! @ini_get('zlib.output_compression')
279 && ! PMA_isGzHandlerEnabled()
281 return true;
282 } else {
283 return false;
288 * Output handler for all exports, if needed buffering, it stores data into
289 * $dump_buffer, otherwise it prints thems out.
291 * @param string $line the insert statement
293 * @return bool Whether output succeeded
295 function PMA_exportOutputHandler($line)
297 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
299 // Kanji encoding convert feature
300 if ($GLOBALS['output_kanji_conversion']) {
301 $line = PMA_Kanji_strConv(
302 $line,
303 $GLOBALS['knjenc'],
304 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
307 // If we have to buffer data, we will perform everything at once at the end
308 if ($GLOBALS['buffer_needed']) {
310 $dump_buffer .= $line;
311 if ($GLOBALS['onfly_compression']) {
313 $dump_buffer_len += strlen($line);
315 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
316 if ($GLOBALS['output_charset_conversion']) {
317 $dump_buffer = PMA_convertString(
318 'utf-8',
319 $GLOBALS['charset_of_file'],
320 $dump_buffer
323 // as bzipped
324 if ($GLOBALS['compression'] == 'bzip2'
325 && @function_exists('bzcompress')
327 $dump_buffer = bzcompress($dump_buffer);
328 } elseif ($GLOBALS['compression'] == 'gzip'
329 && PMA_gzencodeNeeded()
331 // as a gzipped file
332 // without the optional parameter level because it bugs
333 $dump_buffer = gzencode($dump_buffer);
335 if ($GLOBALS['save_on_server']) {
336 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
337 if ($write_result != strlen($dump_buffer)) {
338 $GLOBALS['message'] = PMA_Message::error(
339 __('Insufficient space to save the file %s.')
341 $GLOBALS['message']->addParam($save_filename);
342 return false;
344 } else {
345 echo $dump_buffer;
347 $dump_buffer = '';
348 $dump_buffer_len = 0;
350 } else {
351 $time_now = time();
352 if ($time_start >= $time_now + 30) {
353 $time_start = $time_now;
354 header('X-pmaPing: Pong');
355 } // end if
357 } else {
358 if ($GLOBALS['asfile']) {
359 if ($GLOBALS['output_charset_conversion']) {
360 $line = PMA_convertString(
361 'utf-8',
362 $GLOBALS['charset_of_file'],
363 $line
366 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
367 $write_result = @fwrite($GLOBALS['file_handle'], $line);
368 if (! $write_result || ($write_result != strlen($line))) {
369 $GLOBALS['message'] = PMA_Message::error(
370 __('Insufficient space to save the file %s.')
372 $GLOBALS['message']->addParam($save_filename);
373 return false;
375 $time_now = time();
376 if ($time_start >= $time_now + 30) {
377 $time_start = $time_now;
378 header('X-pmaPing: Pong');
379 } // end if
380 } else {
381 // We export as file - output normally
382 echo $line;
384 } else {
385 // We export as html - replace special chars
386 echo htmlspecialchars($line);
389 return true;
390 } // end of the 'PMA_exportOutputHandler()' function
392 // Defines the default <CR><LF> format.
393 // For SQL always use \n as MySQL wants this on all platforms.
394 if (!defined('TESTSUITE')) {
395 if ($what == 'sql') {
396 $crlf = "\n";
397 } else {
398 $crlf = PMA_Util::whichCrlf();
401 $output_kanji_conversion = function_exists('PMA_Kanji_strConv') && $type != 'xls';
403 // Do we need to convert charset?
404 $output_charset_conversion = $asfile
405 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
406 && isset($charset_of_file) && $charset_of_file != 'utf-8'
407 && $type != 'xls';
409 // Use on the fly compression?
410 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
411 && ($compression == 'gzip' || $compression == 'bzip2');
412 if ($onfly_compression) {
413 $memory_limit = trim(@ini_get('memory_limit'));
414 $memory_limit_num = (int)substr($memory_limit, 0, -1);
415 // 2 MB as default
416 if (empty($memory_limit)) {
417 $memory_limit = 2 * 1024 * 1024;
418 } elseif (strtolower(substr($memory_limit, -1)) == 'm') {
419 $memory_limit = $memory_limit_num * 1024 * 1024;
420 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
421 $memory_limit = $memory_limit_num * 1024;
422 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
423 $memory_limit = $memory_limit_num * 1024 * 1024 * 1024;
424 } else {
425 $memory_limit = (int)$memory_limit;
428 // Some of memory is needed for other things and as threshold.
429 // During export I had allocated (see memory_get_usage function)
430 // approx 1.2MB so this comes from that.
431 if ($memory_limit > 1500000) {
432 $memory_limit -= 1500000;
435 // Some memory is needed for compression, assume 1/3
436 $memory_limit /= 8;
439 // Generate filename and mime type if needed
440 if ($asfile) {
441 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
442 if ($export_type == 'server') {
443 if (isset($remember_template)) {
444 $GLOBALS['PMA_Config']->setUserValue(
445 'pma_server_filename_template',
446 'Export/file_template_server',
447 $filename_template
450 } elseif ($export_type == 'database') {
451 if (isset($remember_template)) {
452 $GLOBALS['PMA_Config']->setUserValue(
453 'pma_db_filename_template',
454 'Export/file_template_database',
455 $filename_template
458 } else {
459 if (isset($remember_template)) {
460 $GLOBALS['PMA_Config']->setUserValue(
461 'pma_table_filename_template',
462 'Export/file_template_table',
463 $filename_template
467 $filename = PMA_Util::expandUserString($filename_template);
468 // remove dots in filename (coming from either the template or already
469 // part of the filename) to avoid a remote code execution vulnerability
470 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
472 // Grab basic dump extension and mime type
473 // Check if the user already added extension;
474 // get the substring where the extension would be if it was included
475 $extension_start_pos = strlen($filename) - strlen(
476 $export_plugin->getProperties()->getExtension()
477 ) - 1;
478 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
479 $required_extension = "." . $export_plugin->getProperties()->getExtension();
480 if (strtolower($user_extension) != $required_extension) {
481 $filename .= $required_extension;
483 $mime_type = $export_plugin->getProperties()->getMimeType();
485 // If dump is going to be compressed, set correct mime_type and add
486 // compression to extension
487 if ($compression == 'bzip2') {
488 $filename .= '.bz2';
489 $mime_type = 'application/x-bzip2';
490 } elseif ($compression == 'gzip') {
491 $filename .= '.gz';
492 $mime_type = 'application/x-gzip';
493 } elseif ($compression == 'zip') {
494 $filename .= '.zip';
495 $mime_type = 'application/zip';
499 // Open file on server if needed
500 if ($save_on_server) {
501 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
502 . preg_replace('@[/\\\\]@', '_', $filename);
503 unset($message);
504 if (file_exists($save_filename)
505 && ((! $quick_export && empty($_REQUEST['onserverover']))
506 || ($quick_export
507 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
509 $message = PMA_Message::error(
511 'File %s already exists on server, '
512 . 'change filename or check overwrite option.'
515 $message->addParam($save_filename);
516 } else {
517 if (is_file($save_filename) && ! is_writable($save_filename)) {
518 $message = PMA_Message::error(
520 'The web server does not have permission '
521 . 'to save the file %s.'
524 $message->addParam($save_filename);
525 } else {
526 if (! $file_handle = @fopen($save_filename, 'w')) {
527 $message = PMA_Message::error(
529 'The web server does not have permission '
530 . 'to save the file %s.'
533 $message->addParam($save_filename);
537 if (isset($message)) {
538 if ($export_type == 'server') {
539 $active_page = 'server_export.php';
540 include 'server_export.php';
541 } elseif ($export_type == 'database') {
542 $active_page = 'db_export.php';
543 include 'db_export.php';
544 } else {
545 $active_page = 'tbl_export.php';
546 include 'tbl_export.php';
548 exit();
553 * Send headers depending on whether the user chose to download a dump file
554 * or not
556 if (! $save_on_server) {
557 if ($asfile) {
558 // Download
559 // (avoid rewriting data containing HTML with anchors and forms;
560 // this was reported to happen under Plesk)
561 @ini_set('url_rewriter.tags', '');
562 $filename = PMA_sanitizeFilename($filename);
564 PMA_downloadHeader($filename, $mime_type);
565 } else {
566 // HTML
567 if ($export_type == 'database') {
568 $num_tables = count($tables);
569 if ($num_tables == 0) {
570 $message = PMA_Message::error(
571 __('No tables found in database.')
573 $active_page = 'db_export.php';
574 include 'db_export.php';
575 exit();
578 $backup_cfgServer = $cfg['Server'];
579 $cfg['Server'] = $backup_cfgServer;
580 unset($backup_cfgServer);
581 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
582 //echo ' <pre>' . "\n";
585 * Displays a back button with all the $_REQUEST data in the URL
586 * (store in a variable to also display after the textarea)
588 $back_button = '<p>[ <a href="';
589 if ($export_type == 'server') {
590 $back_button .= 'server_export.php?' . PMA_URL_getCommon();
591 } elseif ($export_type == 'database') {
592 $back_button .= 'db_export.php?' . PMA_URL_getCommon($db);
593 } else {
594 $back_button .= 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
597 // Convert the multiple select elements from an array to a string
598 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
599 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
600 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
601 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
604 foreach ($_REQUEST as $name => $value) {
605 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
607 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
609 echo $back_button;
610 echo '<form name="nofunction">' . "\n";
611 echo '<textarea name="sqldump" cols="50" rows="30" '
612 . 'id="textSQLDUMP" wrap="OFF">' . "\n";
613 } // end download
616 // Fake loop just to allow skip of remain of this code by break, I'd really
617 // need exceptions here :-)
618 do {
620 // Add possibly some comments to export
621 if (! $export_plugin->exportHeader($db)) {
622 break;
625 // Will we need relation & co. setup?
626 $do_relation = isset($GLOBALS[$what . '_relation']);
627 $do_comments = isset($GLOBALS[$what . '_include_comments']);
628 $do_mime = isset($GLOBALS[$what . '_mime']);
629 if ($do_relation || $do_comments || $do_mime) {
630 $cfgRelation = PMA_getRelationsParam();
632 if ($do_mime) {
633 include_once 'libraries/transformations.lib.php';
636 // Include dates in export?
637 $do_dates = isset($GLOBALS[$what . '_dates']);
640 * Builds the dump
642 // Gets the number of tables if a dump of a database has been required
643 if ($export_type == 'server') {
644 if (isset($db_select)) {
645 $tmp_select = implode($db_select, '|');
646 $tmp_select = '|' . $tmp_select . '|';
648 // Walk over databases
649 foreach ($GLOBALS['pma']->databases as $current_db) {
650 if (isset($tmp_select)
651 && strpos(' ' . $tmp_select, '|' . $current_db . '|')
653 if (! $export_plugin->exportDBHeader($current_db)) {
654 break 2;
656 if (! $export_plugin->exportDBCreate($current_db)) {
657 break 2;
659 if (method_exists($export_plugin, 'exportRoutines')
660 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
661 && isset($GLOBALS['sql_procedure_function'])
663 $export_plugin->exportRoutines($current_db);
666 $tables = $GLOBALS['dbi']->getTables($current_db);
667 $views = array();
668 foreach ($tables as $table) {
669 // if this is a view, collect it for later;
670 // views must be exported after the tables
671 $is_view = PMA_Table::isView($current_db, $table);
672 if ($is_view) {
673 $views[] = $table;
675 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
676 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
678 // for a view, export a stand-in definition of the table
679 // to resolve view dependencies
680 if (! $export_plugin->exportStructure(
681 $current_db, $table, $crlf, $err_url,
682 $is_view ? 'stand_in' : 'create_table', $export_type,
683 $do_relation, $do_comments, $do_mime, $do_dates
684 )) {
685 break 3;
688 // if this is a view or a merge table, don't export data
689 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
690 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
691 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
693 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
694 . '.' . PMA_Util::backquote($table);
695 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
696 break 3;
699 // now export the triggers (needs to be done after the data
700 // because triggers can modify already imported tables)
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, $table, $crlf, $err_url,
706 'triggers', $export_type,
707 $do_relation, $do_comments, $do_mime, $do_dates
708 )) {
709 break 2;
713 foreach ($views as $view) {
714 // no data export for a view
715 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
716 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
718 if (! $export_plugin->exportStructure(
719 $current_db, $view, $crlf, $err_url,
720 'create_view', $export_type,
721 $do_relation, $do_comments, $do_mime, $do_dates
722 )) {
723 break 3;
727 if (! $export_plugin->exportDBFooter($current_db)) {
728 break 2;
732 } elseif ($export_type == 'database') {
733 if (! $export_plugin->exportDBHeader($db)) {
734 break;
736 if (! $export_plugin->exportDBCreate($db)) {
737 break;
740 if (method_exists($export_plugin, 'exportRoutines')
741 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
742 && isset($GLOBALS['sql_procedure_function'])
744 $export_plugin->exportRoutines($db);
747 $i = 0;
748 $views = array();
749 // $tables contains the choices from the user (via $table_select)
750 foreach ($tables as $table) {
751 // if this is a view, collect it for later; views must be exported after
752 // the tables
753 $is_view = PMA_Table::isView($db, $table);
754 if ($is_view) {
755 $views[] = $table;
757 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
758 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
760 // for a view, export a stand-in definition of the table
761 // to resolve view dependencies
762 if (! $export_plugin->exportStructure(
763 $db, $table, $crlf, $err_url,
764 $is_view ? 'stand_in' : 'create_table', $export_type,
765 $do_relation, $do_comments, $do_mime, $do_dates
766 )) {
767 break 2;
770 // if this is a view or a merge table, don't export data
771 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
772 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
773 && ! ($is_view || PMA_Table::isMerge($db, $table))
775 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
776 . '.' . PMA_Util::backquote($table);
777 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
778 break 2;
781 // now export the triggers (needs to be done after the data because
782 // triggers can modify already imported tables)
783 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
784 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
786 if (! $export_plugin->exportStructure(
787 $db, $table, $crlf, $err_url,
788 'triggers', $export_type,
789 $do_relation, $do_comments, $do_mime, $do_dates
790 )) {
791 break 2;
795 foreach ($views as $view) {
796 // no data export for a view
797 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
798 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
800 if (! $export_plugin->exportStructure(
801 $db, $view, $crlf, $err_url,
802 'create_view', $export_type,
803 $do_relation, $do_comments, $do_mime, $do_dates
804 )) {
805 break 2;
810 if (! $export_plugin->exportDBFooter($db)) {
811 break;
813 } else {
814 if (! $export_plugin->exportDBHeader($db)) {
815 break;
817 // We export just one table
818 // $allrows comes from the form when "Dump all rows" has been selected
819 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
820 $add_query = ' LIMIT '
821 . (($limit_from > 0) ? $limit_from . ', ' : '')
822 . $limit_to;
823 } else {
824 $add_query = '';
827 $is_view = PMA_Table::isView($db, $table);
828 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
829 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
831 if (! $export_plugin->exportStructure(
832 $db, $table, $crlf, $err_url,
833 $is_view ? 'create_view' : 'create_table', $export_type,
834 $do_relation, $do_comments, $do_mime, $do_dates
835 )) {
836 break;
839 // If this is an export of a single view, we have to export data;
840 // for example, a PDF report
841 // if it is a merge table, no data is exported
842 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
843 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
844 && ! PMA_Table::isMerge($db, $table)
846 if (! empty($sql_query)) {
847 // only preg_replace if needed
848 if (! empty($add_query)) {
849 // remove trailing semicolon before adding a LIMIT
850 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
852 $local_query = $sql_query . $add_query;
853 $GLOBALS['dbi']->selectDb($db);
854 } else {
855 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
856 . '.' . PMA_Util::backquote($table) . $add_query;
858 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
859 break;
862 // now export the triggers (needs to be done after the data because
863 // triggers can modify already imported tables)
864 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
865 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
867 if (! $export_plugin->exportStructure(
868 $db, $table, $crlf, $err_url,
869 'triggers', $export_type,
870 $do_relation, $do_comments, $do_mime, $do_dates
871 )) {
872 break 2;
875 if (! $export_plugin->exportDBFooter($db)) {
876 break;
879 if (! $export_plugin->exportFooter()) {
880 break;
883 } while (false);
884 // End of fake loop
886 if ($save_on_server && isset($message)) {
887 if ($export_type == 'server') {
888 $active_page = 'server_export.php';
889 include 'server_export.php';
890 } elseif ($export_type == 'database') {
891 $active_page = 'db_export.php';
892 include 'db_export.php';
893 } else {
894 $active_page = 'tbl_export.php';
895 include 'tbl_export.php';
897 exit();
901 * Send the dump as a file...
903 if (! empty($asfile)) {
904 // Convert the charset if required.
905 if ($output_charset_conversion) {
906 $dump_buffer = PMA_convertString(
907 'utf-8',
908 $GLOBALS['charset_of_file'],
909 $dump_buffer
913 // Do the compression
914 // 1. as a zipped file
915 if ($compression == 'zip') {
916 if (@function_exists('gzcompress')) {
917 $zipfile = new ZipFile();
918 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
919 $dump_buffer = $zipfile->file();
921 } elseif ($compression == 'bzip2') {
922 // 2. as a bzipped file
923 if (@function_exists('bzcompress')) {
924 $dump_buffer = bzcompress($dump_buffer);
926 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
927 // 3. as a gzipped file
928 // without the optional parameter level because it bugs
929 $dump_buffer = gzencode($dump_buffer);
932 /* If we saved on server, we have to close file now */
933 if ($save_on_server) {
934 $write_result = @fwrite($file_handle, $dump_buffer);
935 fclose($file_handle);
936 if (strlen($dump_buffer) > 0
937 && (! $write_result || ($write_result != strlen($dump_buffer)))
939 $message = new PMA_Message(
940 __('Insufficient space to save the file %s.'),
941 PMA_Message::ERROR,
942 $save_filename
944 } else {
945 $message = new PMA_Message(
946 __('Dump has been saved to file %s.'),
947 PMA_Message::SUCCESS,
948 $save_filename
952 if ($export_type == 'server') {
953 $active_page = 'server_export.php';
954 include_once 'server_export.php';
955 } elseif ($export_type == 'database') {
956 $active_page = 'db_export.php';
957 include_once 'db_export.php';
958 } else {
959 $active_page = 'tbl_export.php';
960 include_once 'tbl_export.php';
962 exit();
963 } else {
964 echo $dump_buffer;
966 } else {
968 * Displays the dump...
970 * Close the html tags and add the footers if dump is displayed on screen
972 echo '</textarea>' . "\n"
973 . ' </form>' . "\n";
974 echo $back_button;
976 echo "\n";
977 echo '</div>' . "\n";
978 echo "\n";
980 <script type="text/javascript">
981 //<![CDATA[
982 var $body = $("body");
983 $("#textSQLDUMP")
984 .width($body.width() - 50)
985 .height($body.height() - 100);
986 //]]>
987 </script>
988 <?php
989 } // end if