Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / export.php
blob081eddf4b3e395281654241ae5f2194e7e905a82
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_removeCRLF',
58 'excel_columns',
59 'excel_edition',
60 'excel_structure_or_data',
61 'yaml_structure_or_data',
62 'ods_null',
63 'ods_structure_or_data',
64 'ods_columns',
65 'json_structure_or_data',
66 'xml_structure_or_data',
67 'xml_export_functions',
68 'xml_export_procedures',
69 'xml_export_tables',
70 'xml_export_triggers',
71 'xml_export_views',
72 'xml_export_contents',
73 'texytext_structure_or_data',
74 'texytext_columns',
75 'texytext_null',
76 'phparray_structure_or_data',
77 'sql_include_comments',
78 'sql_header_comment',
79 'sql_dates',
80 'sql_relation',
81 'sql_mime',
82 'sql_use_transaction',
83 'sql_disable_fk',
84 'sql_views_as_tables',
85 'sql_compatibility',
86 'sql_structure_or_data',
87 'sql_create_database',
88 'sql_drop_table',
89 'sql_procedure_function',
90 'sql_create_table_statements',
91 'sql_if_not_exists',
92 'sql_auto_increment',
93 'sql_backquotes',
94 'sql_truncate',
95 'sql_delayed',
96 'sql_ignore',
97 'sql_type',
98 'sql_insert_syntax',
99 'sql_max_query_size',
100 'sql_hex_for_blob',
101 'sql_utc_time',
102 'csv_separator',
103 'csv_enclosed',
104 'csv_escaped',
105 'csv_terminated',
106 'csv_null',
107 'csv_removeCRLF',
108 'csv_columns',
109 'csv_structure_or_data',
110 // csv_replace should have been here but we use it directly from $_POST
111 'latex_caption',
112 'latex_structure_or_data',
113 'latex_structure_caption',
114 'latex_structure_continued_caption',
115 'latex_structure_label',
116 'latex_relation',
117 'latex_comments',
118 'latex_mime',
119 'latex_columns',
120 'latex_data_caption',
121 'latex_data_continued_caption',
122 'latex_data_label',
123 'latex_null'
126 foreach ($post_params as $one_post_param) {
127 if (isset($_POST[$one_post_param])) {
128 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
132 // sanitize this parameter which will be used below in a file inclusion
133 $what = PMA_securePath($what);
135 PMA_Util::checkParameters(array('what', 'export_type'));
137 // export class instance, not array of properties, as before
138 $export_plugin = PMA_getPlugin(
139 "export",
140 $what,
141 'libraries/plugins/export/',
142 array(
143 'export_type' => $export_type,
144 'single_table' => isset($single_table)
148 // Backward compatbility
149 $type = $what;
151 // Check export type
152 if (! isset($export_plugin)) {
153 PMA_fatalError(__('Bad type!'));
157 * valid compression methods
159 $compression_methods = array(
160 'zip',
161 'gzip',
162 'bzip2',
166 * init and variable checking
168 $compression = false;
169 $onserver = false;
170 $save_on_server = false;
171 $buffer_needed = false;
173 // Is it a quick or custom export?
174 if ($_REQUEST['quick_or_custom'] == 'quick') {
175 $quick_export = true;
176 } else {
177 $quick_export = false;
180 if ($_REQUEST['output_format'] == 'astext') {
181 $asfile = false;
182 } else {
183 $asfile = true;
184 if (in_array($_REQUEST['compression'], $compression_methods)) {
185 $compression = $_REQUEST['compression'];
186 $buffer_needed = true;
188 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
189 || (! $quick_export && ! empty($_REQUEST['onserver']))
191 if ($quick_export) {
192 $onserver = $_REQUEST['quick_export_onserver'];
193 } else {
194 $onserver = $_REQUEST['onserver'];
196 // Will we save dump on server?
197 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
201 // Does export require to be into file?
202 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
203 $message = PMA_Message::error(
204 __('Selected export type has to be saved in file!')
206 if ($export_type == 'server') {
207 $active_page = 'server_export.php';
208 include 'server_export.php';
209 } elseif ($export_type == 'database') {
210 $active_page = 'db_export.php';
211 include 'db_export.php';
212 } else {
213 $active_page = 'tbl_export.php';
214 include 'tbl_export.php';
216 exit();
219 // Generate error url and check for needed variables
220 if ($export_type == 'server') {
221 $err_url = 'server_export.php?' . PMA_generate_common_url();
222 } elseif ($export_type == 'database' && strlen($db)) {
223 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
224 // Check if we have something to export
225 if (isset($table_select)) {
226 $tables = $table_select;
227 } else {
228 $tables = array();
230 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
231 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
232 } else {
233 PMA_fatalError(__('Bad parameters!'));
237 * Increase time limit for script execution and initializes some variables
239 @set_time_limit($cfg['ExecTimeLimit']);
240 if (! empty($cfg['MemoryLimit'])) {
241 @ini_set('memory_limit', $cfg['MemoryLimit']);
244 // Start with empty buffer
245 $dump_buffer = '';
246 $dump_buffer_len = 0;
248 // We send fake headers to avoid browser timeout when buffering
249 $time_start = time();
253 * Detect ob_gzhandler
255 * @return bool
257 function PMA_isGzHandlerEnabled()
259 return in_array('ob_gzhandler', ob_list_handlers());
263 * Detect whether gzencode is needed; it might not be needed if
264 * the server is already compressing by itself
266 * @return bool Whether gzencode is needed
268 function PMA_gzencodeNeeded()
270 // Here, we detect Apache's mod_deflate so we bet that
271 // this module is active for this instance of phpMyAdmin
272 // and therefore, will gzip encode the content
273 if (@function_exists('gzencode')
274 && ! @ini_get('zlib.output_compression')
275 && ! (function_exists('apache_get_modules')
276 && in_array('mod_deflate', apache_get_modules()))
277 && ! PMA_isGzHandlerEnabled()
279 return true;
280 } else {
281 return false;
286 * Output handler for all exports, if needed buffering, it stores data into
287 * $dump_buffer, otherwise it prints thems out.
289 * @param string $line the insert statement
291 * @return bool Whether output succeeded
293 function PMA_exportOutputHandler($line)
295 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
297 // Kanji encoding convert feature
298 if ($GLOBALS['output_kanji_conversion']) {
299 $line = PMA_Kanji_strConv(
300 $line,
301 $GLOBALS['knjenc'],
302 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
305 // If we have to buffer data, we will perform everything at once at the end
306 if ($GLOBALS['buffer_needed']) {
308 $dump_buffer .= $line;
309 if ($GLOBALS['onfly_compression']) {
311 $dump_buffer_len += strlen($line);
313 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
314 if ($GLOBALS['output_charset_conversion']) {
315 $dump_buffer = PMA_convertString(
316 'utf-8',
317 $GLOBALS['charset_of_file'],
318 $dump_buffer
321 // as bzipped
322 if ($GLOBALS['compression'] == 'bzip2'
323 && @function_exists('bzcompress')
325 $dump_buffer = bzcompress($dump_buffer);
326 } elseif ($GLOBALS['compression'] == 'gzip'
327 && PMA_gzencodeNeeded()
329 // as a gzipped file
330 // without the optional parameter level because it bugs
331 $dump_buffer = gzencode($dump_buffer);
333 if ($GLOBALS['save_on_server']) {
334 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
335 if (! $write_result || ($write_result != strlen($dump_buffer))) {
336 $GLOBALS['message'] = PMA_Message::error(
337 __('Insufficient space to save the file %s.')
339 $GLOBALS['message']->addParam($save_filename);
340 return false;
342 } else {
343 echo $dump_buffer;
345 $dump_buffer = '';
346 $dump_buffer_len = 0;
348 } else {
349 $time_now = time();
350 if ($time_start >= $time_now + 30) {
351 $time_start = $time_now;
352 header('X-pmaPing: Pong');
353 } // end if
355 } else {
356 if ($GLOBALS['asfile']) {
357 if ($GLOBALS['output_charset_conversion']) {
358 $line = PMA_convertString(
359 'utf-8',
360 $GLOBALS['charset_of_file'],
361 $line
364 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
365 $write_result = @fwrite($GLOBALS['file_handle'], $line);
366 if (! $write_result || ($write_result != strlen($line))) {
367 $GLOBALS['message'] = PMA_Message::error(
368 __('Insufficient space to save the file %s.')
370 $GLOBALS['message']->addParam($save_filename);
371 return false;
373 $time_now = time();
374 if ($time_start >= $time_now + 30) {
375 $time_start = $time_now;
376 header('X-pmaPing: Pong');
377 } // end if
378 } else {
379 // We export as file - output normally
380 echo $line;
382 } else {
383 // We export as html - replace special chars
384 echo htmlspecialchars($line);
387 return true;
388 } // end of the 'PMA_exportOutputHandler()' function
390 // Defines the default <CR><LF> format.
391 // For SQL always use \n as MySQL wants this on all platforms.
392 if ($what == 'sql') {
393 $crlf = "\n";
394 } else {
395 $crlf = PMA_Util::whichCrlf();
398 $output_kanji_conversion = function_exists('PMA_Kanji_strConv') && $type != 'xls';
400 // Do we need to convert charset?
401 $output_charset_conversion = $asfile
402 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
403 && isset($charset_of_file) && $charset_of_file != 'utf-8'
404 && $type != 'xls';
406 // Use on the fly compression?
407 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
408 && ($compression == 'gzip' || $compression == 'bzip2');
409 if ($onfly_compression) {
410 $memory_limit = trim(@ini_get('memory_limit'));
411 // 2 MB as default
412 if (empty($memory_limit)) {
413 $memory_limit = 2 * 1024 * 1024;
416 if (strtolower(substr($memory_limit, -1)) == 'm') {
417 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
418 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
419 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
420 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
421 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
422 } else {
423 $memory_limit = (int)$memory_limit;
426 // Some of memory is needed for other things and as threshold.
427 // During export I had allocated (see memory_get_usage function)
428 // approx 1.2MB so this comes from that.
429 if ($memory_limit > 1500000) {
430 $memory_limit -= 1500000;
433 // Some memory is needed for compression, assume 1/3
434 $memory_limit /= 8;
437 // Generate filename and mime type if needed
438 if ($asfile) {
439 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
440 if ($export_type == 'server') {
441 if (isset($remember_template)) {
442 $GLOBALS['PMA_Config']->setUserValue(
443 'pma_server_filename_template',
444 'Export/file_template_server',
445 $filename_template
448 } elseif ($export_type == 'database') {
449 if (isset($remember_template)) {
450 $GLOBALS['PMA_Config']->setUserValue(
451 'pma_db_filename_template',
452 'Export/file_template_database',
453 $filename_template
456 } else {
457 if (isset($remember_template)) {
458 $GLOBALS['PMA_Config']->setUserValue(
459 'pma_table_filename_template',
460 'Export/file_template_table',
461 $filename_template
465 $filename = PMA_Util::expandUserString($filename_template);
466 // remove dots in filename (coming from either the template or already
467 // part of the filename) to avoid a remote code execution vulnerability
468 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
470 // Grab basic dump extension and mime type
471 // Check if the user already added extension;
472 // get the substring where the extension would be if it was included
473 $extension_start_pos = strlen($filename) - strlen(
474 $export_plugin->getProperties()->getExtension()
475 ) - 1;
476 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
477 $required_extension = "." . $export_plugin->getProperties()->getExtension();
478 if (strtolower($user_extension) != $required_extension) {
479 $filename .= $required_extension;
481 $mime_type = $export_plugin->getProperties()->getMimeType();
483 // If dump is going to be compressed, set correct mime_type and add
484 // compression to extension
485 if ($compression == 'bzip2') {
486 $filename .= '.bz2';
487 $mime_type = 'application/x-bzip2';
488 } elseif ($compression == 'gzip') {
489 $filename .= '.gz';
490 $mime_type = 'application/x-gzip';
491 } elseif ($compression == 'zip') {
492 $filename .= '.zip';
493 $mime_type = 'application/zip';
497 // Open file on server if needed
498 if ($save_on_server) {
499 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
500 . preg_replace('@[/\\\\]@', '_', $filename);
501 unset($message);
502 if (file_exists($save_filename)
503 && ((! $quick_export && empty($onserverover))
504 || ($quick_export
505 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
507 $message = PMA_Message::error(
508 __('File %s already exists on server, change filename or check overwrite option.')
510 $message->addParam($save_filename);
511 } else {
512 if (is_file($save_filename) && ! is_writable($save_filename)) {
513 $message = PMA_Message::error(
514 __('The web server does not have permission to save the file %s.')
516 $message->addParam($save_filename);
517 } else {
518 if (! $file_handle = @fopen($save_filename, 'w')) {
519 $message = PMA_Message::error(
520 __('The web server does not have permission to save the file %s.')
522 $message->addParam($save_filename);
526 if (isset($message)) {
527 if ($export_type == 'server') {
528 $active_page = 'server_export.php';
529 include 'server_export.php';
530 } elseif ($export_type == 'database') {
531 $active_page = 'db_export.php';
532 include 'db_export.php';
533 } else {
534 $active_page = 'tbl_export.php';
535 include 'tbl_export.php';
537 exit();
542 * Send headers depending on whether the user chose to download a dump file
543 * or not
545 if (! $save_on_server) {
546 if ($asfile) {
547 // Download
548 // (avoid rewriting data containing HTML with anchors and forms;
549 // this was reported to happen under Plesk)
550 @ini_set('url_rewriter.tags', '');
551 $filename = PMA_sanitizeFilename($filename);
553 PMA_downloadHeader($filename, $mime_type);
554 } else {
555 // HTML
556 if ($export_type == 'database') {
557 $num_tables = count($tables);
558 if ($num_tables == 0) {
559 $message = PMA_Message::error(__('No tables found in database.'));
560 $active_page = 'db_export.php';
561 include 'db_export.php';
562 exit();
565 $backup_cfgServer = $cfg['Server'];
566 $cfg['Server'] = $backup_cfgServer;
567 unset($backup_cfgServer);
568 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
569 //echo ' <pre>' . "\n";
572 * Displays a back button with all the $_REQUEST data in the URL
573 * (store in a variable to also display after the textarea)
575 $back_button = '<p>[ <a href="';
576 if ($export_type == 'server') {
577 $back_button .= 'server_export.php?' . PMA_generate_common_url();
578 } elseif ($export_type == 'database') {
579 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
580 } else {
581 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
584 // Convert the multiple select elements from an array to a string
585 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
586 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
587 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
588 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
591 foreach ($_REQUEST as $name => $value) {
592 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
594 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
596 echo $back_button;
597 echo ' <form name="nofunction">' . "\n"
598 // remove auto-select for now: there is no way to select
599 // only a part of the text; anyway, it should obey
600 // $cfg['TextareaAutoSelect']
601 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
602 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
603 } // end download
606 // Fake loop just to allow skip of remain of this code by break, I'd really
607 // need exceptions here :-)
608 do {
610 // Add possibly some comments to export
611 if (! $export_plugin->exportHeader($db)) {
612 break;
615 // Will we need relation & co. setup?
616 $do_relation = isset($GLOBALS[$what . '_relation']);
617 $do_comments = isset($GLOBALS[$what . '_include_comments']);
618 $do_mime = isset($GLOBALS[$what . '_mime']);
619 if ($do_relation || $do_comments || $do_mime) {
620 $cfgRelation = PMA_getRelationsParam();
622 if ($do_mime) {
623 include_once 'libraries/transformations.lib.php';
626 // Include dates in export?
627 $do_dates = isset($GLOBALS[$what . '_dates']);
630 * Builds the dump
632 // Gets the number of tables if a dump of a database has been required
633 if ($export_type == 'server') {
634 if (isset($db_select)) {
635 $tmp_select = implode($db_select, '|');
636 $tmp_select = '|' . $tmp_select . '|';
638 // Walk over databases
639 foreach ($GLOBALS['pma']->databases as $current_db) {
640 if (isset($tmp_select)
641 && strpos(' ' . $tmp_select, '|' . $current_db . '|')
643 if (! $export_plugin->exportDBHeader($current_db)) {
644 break 2;
646 if (! $export_plugin->exportDBCreate($current_db)) {
647 break 2;
649 if (method_exists($export_plugin, 'exportRoutines')
650 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
651 && isset($GLOBALS['sql_procedure_function'])
653 $export_plugin->exportRoutines($current_db);
656 $tables = $GLOBALS['dbi']->getTables($current_db);
657 $views = array();
658 foreach ($tables as $table) {
659 // if this is a view, collect it for later;
660 // views must be exported after the tables
661 $is_view = PMA_Table::isView($current_db, $table);
662 if ($is_view) {
663 $views[] = $table;
665 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
666 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
668 // for a view, export a stand-in definition of the table
669 // to resolve view dependencies
670 if (! $export_plugin->exportStructure(
671 $current_db, $table, $crlf, $err_url,
672 $is_view ? 'stand_in' : 'create_table', $export_type,
673 $do_relation, $do_comments, $do_mime, $do_dates
674 )) {
675 break 3;
678 // if this is a view or a merge table, don't export data
679 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
680 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
681 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
683 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
684 . '.' . PMA_Util::backquote($table);
685 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
686 break 3;
689 // now export the triggers (needs to be done after the data
690 // because triggers can modify already imported tables)
691 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
692 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
694 if (! $export_plugin->exportStructure(
695 $current_db, $table, $crlf, $err_url,
696 'triggers', $export_type,
697 $do_relation, $do_comments, $do_mime, $do_dates
698 )) {
699 break 2;
703 foreach ($views as $view) {
704 // no data export for a view
705 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
706 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
708 if (! $export_plugin->exportStructure(
709 $current_db, $view, $crlf, $err_url,
710 'create_view', $export_type,
711 $do_relation, $do_comments, $do_mime, $do_dates
712 )) {
713 break 3;
717 if (! $export_plugin->exportDBFooter($current_db)) {
718 break 2;
722 } elseif ($export_type == 'database') {
723 if (! $export_plugin->exportDBHeader($db)) {
724 break;
726 if (! $export_plugin->exportDBCreate($db)) {
727 break;
730 if (method_exists($export_plugin, 'exportRoutines')
731 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
732 && isset($GLOBALS['sql_procedure_function'])
734 $export_plugin->exportRoutines($db);
737 $i = 0;
738 $views = array();
739 // $tables contains the choices from the user (via $table_select)
740 foreach ($tables as $table) {
741 // if this is a view, collect it for later; views must be exported after
742 // the tables
743 $is_view = PMA_Table::isView($db, $table);
744 if ($is_view) {
745 $views[] = $table;
747 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
748 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
750 // for a view, export a stand-in definition of the table
751 // to resolve view dependencies
752 if (! $export_plugin->exportStructure(
753 $db, $table, $crlf, $err_url,
754 $is_view ? 'stand_in' : 'create_table', $export_type,
755 $do_relation, $do_comments, $do_mime, $do_dates
756 )) {
757 break 2;
760 // if this is a view or a merge table, don't export data
761 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
762 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
763 && ! ($is_view || PMA_Table::isMerge($db, $table))
765 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
766 . '.' . PMA_Util::backquote($table);
767 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
768 break 2;
771 // now export the triggers (needs to be done after the data because
772 // triggers can modify already imported tables)
773 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
774 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
776 if (! $export_plugin->exportStructure(
777 $db, $table, $crlf, $err_url,
778 'triggers', $export_type,
779 $do_relation, $do_comments, $do_mime, $do_dates
780 )) {
781 break 2;
785 foreach ($views as $view) {
786 // no data export for a view
787 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
788 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
790 if (! $export_plugin->exportStructure(
791 $db, $view, $crlf, $err_url,
792 'create_view', $export_type,
793 $do_relation, $do_comments, $do_mime, $do_dates
794 )) {
795 break 2;
800 if (! $export_plugin->exportDBFooter($db)) {
801 break;
803 } else {
804 if (! $export_plugin->exportDBHeader($db)) {
805 break;
807 // We export just one table
808 // $allrows comes from the form when "Dump all rows" has been selected
809 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
810 $add_query = ' LIMIT '
811 . (($limit_from > 0) ? $limit_from . ', ' : '')
812 . $limit_to;
813 } else {
814 $add_query = '';
817 $is_view = PMA_Table::isView($db, $table);
818 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
819 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
821 if (! $export_plugin->exportStructure(
822 $db, $table, $crlf, $err_url,
823 $is_view ? 'create_view' : 'create_table', $export_type,
824 $do_relation, $do_comments, $do_mime, $do_dates
825 )) {
826 break;
829 // If this is an export of a single view, we have to export data;
830 // for example, a PDF report
831 // if it is a merge table, no data is exported
832 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
833 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
834 && ! PMA_Table::isMerge($db, $table)
836 if (! empty($sql_query)) {
837 // only preg_replace if needed
838 if (! empty($add_query)) {
839 // remove trailing semicolon before adding a LIMIT
840 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
842 $local_query = $sql_query . $add_query;
843 $GLOBALS['dbi']->selectDb($db);
844 } else {
845 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
846 . '.' . PMA_Util::backquote($table) . $add_query;
848 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
849 break;
852 // now export the triggers (needs to be done after the data because
853 // triggers can modify already imported tables)
854 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
855 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
857 if (! $export_plugin->exportStructure(
858 $db, $table, $crlf, $err_url,
859 'triggers', $export_type,
860 $do_relation, $do_comments, $do_mime, $do_dates
861 )) {
862 break 2;
865 if (! $export_plugin->exportDBFooter($db)) {
866 break;
869 if (! $export_plugin->exportFooter()) {
870 break;
873 } while (false);
874 // End of fake loop
876 if ($save_on_server && isset($message)) {
877 if ($export_type == 'server') {
878 $active_page = 'server_export.php';
879 include 'server_export.php';
880 } elseif ($export_type == 'database') {
881 $active_page = 'db_export.php';
882 include 'db_export.php';
883 } else {
884 $active_page = 'tbl_export.php';
885 include 'tbl_export.php';
887 exit();
891 * Send the dump as a file...
893 if (! empty($asfile)) {
894 // Convert the charset if required.
895 if ($output_charset_conversion) {
896 $dump_buffer = PMA_convertString(
897 'utf-8',
898 $GLOBALS['charset_of_file'],
899 $dump_buffer
903 // Do the compression
904 // 1. as a zipped file
905 if ($compression == 'zip') {
906 if (@function_exists('gzcompress')) {
907 $zipfile = new ZipFile();
908 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
909 $dump_buffer = $zipfile->file();
911 } elseif ($compression == 'bzip2') {
912 // 2. as a bzipped file
913 if (@function_exists('bzcompress')) {
914 $dump_buffer = bzcompress($dump_buffer);
916 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
917 // 3. as a gzipped file
918 // without the optional parameter level because it bugs
919 $dump_buffer = gzencode($dump_buffer);
922 /* If we saved on server, we have to close file now */
923 if ($save_on_server) {
924 $write_result = @fwrite($file_handle, $dump_buffer);
925 fclose($file_handle);
926 if (strlen($dump_buffer) > 0
927 && (! $write_result || ($write_result != strlen($dump_buffer)))
929 $message = new PMA_Message(
930 __('Insufficient space to save the file %s.'),
931 PMA_Message::ERROR,
932 $save_filename
934 } else {
935 $message = new PMA_Message(
936 __('Dump has been saved to file %s.'),
937 PMA_Message::SUCCESS,
938 $save_filename
942 if ($export_type == 'server') {
943 $active_page = 'server_export.php';
944 include_once 'server_export.php';
945 } elseif ($export_type == 'database') {
946 $active_page = 'db_export.php';
947 include_once 'db_export.php';
948 } else {
949 $active_page = 'tbl_export.php';
950 include_once 'tbl_export.php';
952 exit();
953 } else {
954 PMA_Response::getInstance()->disable();
955 echo $dump_buffer;
957 } else {
959 * Displays the dump...
961 * Close the html tags and add the footers if dump is displayed on screen
963 echo '</textarea>' . "\n"
964 . ' </form>' . "\n";
965 echo $back_button;
967 echo "\n";
968 echo '</div>' . "\n";
969 echo "\n";
971 <script type="text/javascript">
972 //<![CDATA[
973 var $body = $("body");
974 $("#textSQLDUMP")
975 .width($body.width() - 50)
976 .height($body.height() - 100);
977 //]]>
978 </script>
979 <?php
980 } // end if