4.0.9-dev
[phpmyadmin.git] / export.php
blobd81630a7395b0a9f4f540c4776dbc8c00c2b71c3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 /**
10 * If we are sending the export file (as opposed to just displaying it
11 * as text), we have to bypass the usual PMA_Response mechanism
13 if ($_POST['output_format'] == 'sendit') {
14 define('PMA_BYPASS_GET_INSTANCE', 1);
17 /**
18 * Get the variables sent or posted to this script and a core script
20 require_once 'libraries/common.inc.php';
21 require_once 'libraries/zip.lib.php';
22 require_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_generate_common_url();
229 } elseif ($export_type == 'database' && strlen($db)) {
230 $err_url = 'db_export.php?' . PMA_generate_common_url($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_generate_common_url($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_str_conv(
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_convert_string(
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 || ($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_convert_string(
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 ($what == 'sql') {
395 $crlf = "\n";
396 } else {
397 $crlf = PMA_Util::whichCrlf();
400 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
402 // Do we need to convert charset?
403 $output_charset_conversion = $asfile
404 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
405 && isset($charset_of_file) && $charset_of_file != 'utf-8'
406 && $type != 'xls';
408 // Use on the fly compression?
409 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
410 && ($compression == 'gzip' || $compression == 'bzip2');
411 if ($onfly_compression) {
412 $memory_limit = trim(@ini_get('memory_limit'));
413 // 2 MB as default
414 if (empty($memory_limit)) {
415 $memory_limit = 2 * 1024 * 1024;
418 if (strtolower(substr($memory_limit, -1)) == 'm') {
419 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
420 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
421 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
422 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
423 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
424 } else {
425 $memory_limit = (int)$memory_limit;
428 // Some of memory is needed for other thins and as treshold.
429 // Nijel: 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(
510 __('File %s already exists on server, change filename or check overwrite option.')
512 $message->addParam($save_filename);
513 } else {
514 if (is_file($save_filename) && ! is_writable($save_filename)) {
515 $message = PMA_Message::error(
516 __('The web server does not have permission to save the file %s.')
518 $message->addParam($save_filename);
519 } else {
520 if (! $file_handle = @fopen($save_filename, 'w')) {
521 $message = PMA_Message::error(
522 __('The web server does not have permission to save the file %s.')
524 $message->addParam($save_filename);
528 if (isset($message)) {
529 if ($export_type == 'server') {
530 $active_page = 'server_export.php';
531 include 'server_export.php';
532 } elseif ($export_type == 'database') {
533 $active_page = 'db_export.php';
534 include 'db_export.php';
535 } else {
536 $active_page = 'tbl_export.php';
537 include 'tbl_export.php';
539 exit();
544 * Send headers depending on whether the user chose to download a dump file
545 * or not
547 if (! $save_on_server) {
548 if ($asfile) {
549 // Download
550 // (avoid rewriting data containing HTML with anchors and forms;
551 // this was reported to happen under Plesk)
552 @ini_set('url_rewriter.tags', '');
553 $filename = PMA_sanitizeFilename($filename);
555 PMA_downloadHeader($filename, $mime_type);
556 } else {
557 // HTML
558 if ($export_type == 'database') {
559 $num_tables = count($tables);
560 if ($num_tables == 0) {
561 $message = PMA_Message::error(__('No tables found in database.'));
562 $active_page = 'db_export.php';
563 include 'db_export.php';
564 exit();
567 $backup_cfgServer = $cfg['Server'];
568 $cfg['Server'] = $backup_cfgServer;
569 unset($backup_cfgServer);
570 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
571 //echo ' <pre>' . "\n";
574 * Displays a back button with all the $_REQUEST data in the URL
575 * (store in a variable to also display after the textarea)
577 $back_button = '<p>[ <a href="';
578 if ($export_type == 'server') {
579 $back_button .= 'server_export.php?' . PMA_generate_common_url();
580 } elseif ($export_type == 'database') {
581 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
582 } else {
583 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
586 // Convert the multiple select elements from an array to a string
587 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
588 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
589 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
590 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
593 foreach ($_REQUEST as $name => $value) {
594 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
596 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
598 echo $back_button;
599 echo ' <form name="nofunction">' . "\n"
600 // remove auto-select for now: there is no way to select
601 // only a part of the text; anyway, it should obey
602 // $cfg['TextareaAutoSelect']
603 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
604 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
605 } // end download
608 // Fake loop just to allow skip of remain of this code by break, I'd really
609 // need exceptions here :-)
610 do {
612 // Add possibly some comments to export
613 if (! $export_plugin->exportHeader($db)) {
614 break;
617 // Will we need relation & co. setup?
618 $do_relation = isset($GLOBALS[$what . '_relation']);
619 $do_comments = isset($GLOBALS[$what . '_include_comments']);
620 $do_mime = isset($GLOBALS[$what . '_mime']);
621 if ($do_relation || $do_comments || $do_mime) {
622 $cfgRelation = PMA_getRelationsParam();
624 if ($do_mime) {
625 include_once 'libraries/transformations.lib.php';
628 // Include dates in export?
629 $do_dates = isset($GLOBALS[$what . '_dates']);
632 * Builds the dump
634 // Gets the number of tables if a dump of a database has been required
635 if ($export_type == 'server') {
636 if (isset($db_select)) {
637 $tmp_select = implode($db_select, '|');
638 $tmp_select = '|' . $tmp_select . '|';
640 // Walk over databases
641 foreach ($GLOBALS['pma']->databases as $current_db) {
642 if (isset($tmp_select)
643 && strpos(' ' . $tmp_select, '|' . $current_db . '|')
645 if (! $export_plugin->exportDBHeader($current_db)) {
646 break 2;
648 if (! $export_plugin->exportDBCreate($current_db)) {
649 break 2;
651 if (method_exists($export_plugin, 'exportRoutines')
652 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
653 && isset($GLOBALS['sql_procedure_function'])
655 $export_plugin->exportRoutines($current_db);
658 $tables = PMA_DBI_get_tables($current_db);
659 $views = array();
660 foreach ($tables as $table) {
661 // if this is a view, collect it for later;
662 // views must be exported after the tables
663 $is_view = PMA_Table::isView($current_db, $table);
664 if ($is_view) {
665 $views[] = $table;
667 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
668 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
670 // for a view, export a stand-in definition of the table
671 // to resolve view dependencies
672 if (! $export_plugin->exportStructure(
673 $current_db, $table, $crlf, $err_url,
674 $is_view ? 'stand_in' : 'create_table', $export_type,
675 $do_relation, $do_comments, $do_mime, $do_dates
676 )) {
677 break 3;
680 // if this is a view or a merge table, don't export data
681 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
682 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
683 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
685 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
686 . '.' . PMA_Util::backquote($table);
687 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
688 break 3;
691 // now export the triggers (needs to be done after the data
692 // because triggers can modify already imported tables)
693 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
694 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
696 if (! $export_plugin->exportStructure(
697 $current_db, $table, $crlf, $err_url,
698 'triggers', $export_type,
699 $do_relation, $do_comments, $do_mime, $do_dates
700 )) {
701 break 2;
705 foreach ($views as $view) {
706 // no data export for a view
707 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
708 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
710 if (! $export_plugin->exportStructure(
711 $current_db, $view, $crlf, $err_url,
712 'create_view', $export_type,
713 $do_relation, $do_comments, $do_mime, $do_dates
714 )) {
715 break 3;
719 if (! $export_plugin->exportDBFooter($current_db)) {
720 break 2;
724 } elseif ($export_type == 'database') {
725 if (! $export_plugin->exportDBHeader($db)) {
726 break;
728 if (! $export_plugin->exportDBCreate($db)) {
729 break;
732 if (method_exists($export_plugin, 'exportRoutines')
733 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
734 && isset($GLOBALS['sql_procedure_function'])
736 $export_plugin->exportRoutines($db);
739 $i = 0;
740 $views = array();
741 // $tables contains the choices from the user (via $table_select)
742 foreach ($tables as $table) {
743 // if this is a view, collect it for later; views must be exported after
744 // the tables
745 $is_view = PMA_Table::isView($db, $table);
746 if ($is_view) {
747 $views[] = $table;
749 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
750 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
752 // for a view, export a stand-in definition of the table
753 // to resolve view dependencies
754 if (! $export_plugin->exportStructure(
755 $db, $table, $crlf, $err_url,
756 $is_view ? 'stand_in' : 'create_table', $export_type,
757 $do_relation, $do_comments, $do_mime, $do_dates
758 )) {
759 break 2;
762 // if this is a view or a merge table, don't export data
763 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
764 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
765 && ! ($is_view || PMA_Table::isMerge($db, $table))
767 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
768 . '.' . PMA_Util::backquote($table);
769 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
770 break 2;
773 // now export the triggers (needs to be done after the data because
774 // triggers can modify already imported tables)
775 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
776 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
778 if (! $export_plugin->exportStructure(
779 $db, $table, $crlf, $err_url,
780 'triggers', $export_type,
781 $do_relation, $do_comments, $do_mime, $do_dates
782 )) {
783 break 2;
787 foreach ($views as $view) {
788 // no data export for a view
789 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
790 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
792 if (! $export_plugin->exportStructure(
793 $db, $view, $crlf, $err_url,
794 'create_view', $export_type,
795 $do_relation, $do_comments, $do_mime, $do_dates
796 )) {
797 break 2;
802 if (! $export_plugin->exportDBFooter($db)) {
803 break;
805 } else {
806 if (! $export_plugin->exportDBHeader($db)) {
807 break;
809 // We export just one table
810 // $allrows comes from the form when "Dump all rows" has been selected
811 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
812 $add_query = ' LIMIT '
813 . (($limit_from > 0) ? $limit_from . ', ' : '')
814 . $limit_to;
815 } else {
816 $add_query = '';
819 $is_view = PMA_Table::isView($db, $table);
820 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
821 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
823 if (! $export_plugin->exportStructure(
824 $db, $table, $crlf, $err_url,
825 $is_view ? 'create_view' : 'create_table', $export_type,
826 $do_relation, $do_comments, $do_mime, $do_dates
827 )) {
828 break;
831 // If this is an export of a single view, we have to export data;
832 // for example, a PDF report
833 // if it is a merge table, no data is exported
834 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
835 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
836 && ! PMA_Table::isMerge($db, $table)
838 if (! empty($sql_query)) {
839 // only preg_replace if needed
840 if (! empty($add_query)) {
841 // remove trailing semicolon before adding a LIMIT
842 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
844 $local_query = $sql_query . $add_query;
845 PMA_DBI_select_db($db);
846 } else {
847 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
848 . '.' . PMA_Util::backquote($table) . $add_query;
850 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
851 break;
854 // now export the triggers (needs to be done after the data because
855 // triggers can modify already imported tables)
856 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
857 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
859 if (! $export_plugin->exportStructure(
860 $db, $table, $crlf, $err_url,
861 'triggers', $export_type,
862 $do_relation, $do_comments, $do_mime, $do_dates
863 )) {
864 break 2;
867 if (! $export_plugin->exportDBFooter($db)) {
868 break;
871 if (! $export_plugin->exportFooter()) {
872 break;
875 } while (false);
876 // End of fake loop
878 if ($save_on_server && isset($message)) {
879 if ($export_type == 'server') {
880 $active_page = 'server_export.php';
881 include 'server_export.php';
882 } elseif ($export_type == 'database') {
883 $active_page = 'db_export.php';
884 include 'db_export.php';
885 } else {
886 $active_page = 'tbl_export.php';
887 include 'tbl_export.php';
889 exit();
893 * Send the dump as a file...
895 if (! empty($asfile)) {
896 // Convert the charset if required.
897 if ($output_charset_conversion) {
898 $dump_buffer = PMA_convert_string(
899 'utf-8',
900 $GLOBALS['charset_of_file'],
901 $dump_buffer
905 // Do the compression
906 // 1. as a zipped file
907 if ($compression == 'zip') {
908 if (@function_exists('gzcompress')) {
909 $zipfile = new ZipFile();
910 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
911 $dump_buffer = $zipfile->file();
913 } elseif ($compression == 'bzip2') {
914 // 2. as a bzipped file
915 if (@function_exists('bzcompress')) {
916 $dump_buffer = bzcompress($dump_buffer);
918 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
919 // 3. as a gzipped file
920 // without the optional parameter level because it bugs
921 $dump_buffer = gzencode($dump_buffer);
924 /* If we saved on server, we have to close file now */
925 if ($save_on_server) {
926 $write_result = @fwrite($file_handle, $dump_buffer);
927 fclose($file_handle);
928 if (strlen($dump_buffer) > 0
929 && (! $write_result || ($write_result != strlen($dump_buffer)))
931 $message = new PMA_Message(
932 __('Insufficient space to save the file %s.'),
933 PMA_Message::ERROR,
934 $save_filename
936 } else {
937 $message = new PMA_Message(
938 __('Dump has been saved to file %s.'),
939 PMA_Message::SUCCESS,
940 $save_filename
944 if ($export_type == 'server') {
945 $active_page = 'server_export.php';
946 include_once 'server_export.php';
947 } elseif ($export_type == 'database') {
948 $active_page = 'db_export.php';
949 include_once 'db_export.php';
950 } else {
951 $active_page = 'tbl_export.php';
952 include_once 'tbl_export.php';
954 exit();
955 } else {
956 echo $dump_buffer;
958 } else {
960 * Displays the dump...
962 * Close the html tags and add the footers if dump is displayed on screen
964 echo '</textarea>' . "\n"
965 . ' </form>' . "\n";
966 echo $back_button;
968 echo "\n";
969 echo '</div>' . "\n";
970 echo "\n";
972 <script type="text/javascript">
973 //<![CDATA[
974 var $body = $("body");
975 $("#textSQLDUMP")
976 .width($body.width() - 50)
977 .height($body.height() - 100);
978 //]]>
979 </script>
980 <?php
981 } // end if