remove trailing space
[phpmyadmin.git] / export.php
blob264c7e704351e5987847e13804b2e6ef7aac725f
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 || ($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 // 2 MB as default
415 if (empty($memory_limit)) {
416 $memory_limit = 2 * 1024 * 1024;
419 if (strtolower(substr($memory_limit, -1)) == 'm') {
420 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
421 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
422 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
423 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
424 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
425 } else {
426 $memory_limit = (int)$memory_limit;
429 // Some of memory is needed for other things and as threshold.
430 // During export I had allocated (see memory_get_usage function)
431 // approx 1.2MB so this comes from that.
432 if ($memory_limit > 1500000) {
433 $memory_limit -= 1500000;
436 // Some memory is needed for compression, assume 1/3
437 $memory_limit /= 8;
440 // Generate filename and mime type if needed
441 if ($asfile) {
442 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
443 if ($export_type == 'server') {
444 if (isset($remember_template)) {
445 $GLOBALS['PMA_Config']->setUserValue(
446 'pma_server_filename_template',
447 'Export/file_template_server',
448 $filename_template
451 } elseif ($export_type == 'database') {
452 if (isset($remember_template)) {
453 $GLOBALS['PMA_Config']->setUserValue(
454 'pma_db_filename_template',
455 'Export/file_template_database',
456 $filename_template
459 } else {
460 if (isset($remember_template)) {
461 $GLOBALS['PMA_Config']->setUserValue(
462 'pma_table_filename_template',
463 'Export/file_template_table',
464 $filename_template
468 $filename = PMA_Util::expandUserString($filename_template);
469 // remove dots in filename (coming from either the template or already
470 // part of the filename) to avoid a remote code execution vulnerability
471 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
473 // Grab basic dump extension and mime type
474 // Check if the user already added extension;
475 // get the substring where the extension would be if it was included
476 $extension_start_pos = strlen($filename) - strlen(
477 $export_plugin->getProperties()->getExtension()
478 ) - 1;
479 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
480 $required_extension = "." . $export_plugin->getProperties()->getExtension();
481 if (strtolower($user_extension) != $required_extension) {
482 $filename .= $required_extension;
484 $mime_type = $export_plugin->getProperties()->getMimeType();
486 // If dump is going to be compressed, set correct mime_type and add
487 // compression to extension
488 if ($compression == 'bzip2') {
489 $filename .= '.bz2';
490 $mime_type = 'application/x-bzip2';
491 } elseif ($compression == 'gzip') {
492 $filename .= '.gz';
493 $mime_type = 'application/x-gzip';
494 } elseif ($compression == 'zip') {
495 $filename .= '.zip';
496 $mime_type = 'application/zip';
500 // Open file on server if needed
501 if ($save_on_server) {
502 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
503 . preg_replace('@[/\\\\]@', '_', $filename);
504 unset($message);
505 if (file_exists($save_filename)
506 && ((! $quick_export && empty($_REQUEST['onserverover']))
507 || ($quick_export
508 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
510 $message = PMA_Message::error(
511 __('File %s already exists on server, change filename or check overwrite option.')
513 $message->addParam($save_filename);
514 } else {
515 if (is_file($save_filename) && ! is_writable($save_filename)) {
516 $message = PMA_Message::error(
517 __('The web server does not have permission to save the file %s.')
519 $message->addParam($save_filename);
520 } else {
521 if (! $file_handle = @fopen($save_filename, 'w')) {
522 $message = PMA_Message::error(
523 __('The web server does not have permission to save the file %s.')
525 $message->addParam($save_filename);
529 if (isset($message)) {
530 if ($export_type == 'server') {
531 $active_page = 'server_export.php';
532 include 'server_export.php';
533 } elseif ($export_type == 'database') {
534 $active_page = 'db_export.php';
535 include 'db_export.php';
536 } else {
537 $active_page = 'tbl_export.php';
538 include 'tbl_export.php';
540 exit();
545 * Send headers depending on whether the user chose to download a dump file
546 * or not
548 if (! $save_on_server) {
549 if ($asfile) {
550 // Download
551 // (avoid rewriting data containing HTML with anchors and forms;
552 // this was reported to happen under Plesk)
553 @ini_set('url_rewriter.tags', '');
554 $filename = PMA_sanitizeFilename($filename);
556 PMA_downloadHeader($filename, $mime_type);
557 } else {
558 // HTML
559 if ($export_type == 'database') {
560 $num_tables = count($tables);
561 if ($num_tables == 0) {
562 $message = PMA_Message::error(__('No tables found in database.'));
563 $active_page = 'db_export.php';
564 include 'db_export.php';
565 exit();
568 $backup_cfgServer = $cfg['Server'];
569 $cfg['Server'] = $backup_cfgServer;
570 unset($backup_cfgServer);
571 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
572 //echo ' <pre>' . "\n";
575 * Displays a back button with all the $_REQUEST data in the URL
576 * (store in a variable to also display after the textarea)
578 $back_button = '<p>[ <a href="';
579 if ($export_type == 'server') {
580 $back_button .= 'server_export.php?' . PMA_URL_getCommon();
581 } elseif ($export_type == 'database') {
582 $back_button .= 'db_export.php?' . PMA_URL_getCommon($db);
583 } else {
584 $back_button .= 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
587 // Convert the multiple select elements from an array to a string
588 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
589 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
590 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
591 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
594 foreach ($_REQUEST as $name => $value) {
595 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
597 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
599 echo $back_button;
600 echo ' <form name="nofunction">' . "\n"
601 // remove auto-select for now: there is no way to select
602 // only a part of the text; anyway, it should obey
603 // $cfg['TextareaAutoSelect']
604 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
605 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
606 } // end download
609 // Fake loop just to allow skip of remain of this code by break, I'd really
610 // need exceptions here :-)
611 do {
613 // Add possibly some comments to export
614 if (! $export_plugin->exportHeader($db)) {
615 break;
618 // Will we need relation & co. setup?
619 $do_relation = isset($GLOBALS[$what . '_relation']);
620 $do_comments = isset($GLOBALS[$what . '_include_comments']);
621 $do_mime = isset($GLOBALS[$what . '_mime']);
622 if ($do_relation || $do_comments || $do_mime) {
623 $cfgRelation = PMA_getRelationsParam();
625 if ($do_mime) {
626 include_once 'libraries/transformations.lib.php';
629 // Include dates in export?
630 $do_dates = isset($GLOBALS[$what . '_dates']);
633 * Builds the dump
635 // Gets the number of tables if a dump of a database has been required
636 if ($export_type == 'server') {
637 if (isset($db_select)) {
638 $tmp_select = implode($db_select, '|');
639 $tmp_select = '|' . $tmp_select . '|';
641 // Walk over databases
642 foreach ($GLOBALS['pma']->databases as $current_db) {
643 if (isset($tmp_select)
644 && strpos(' ' . $tmp_select, '|' . $current_db . '|')
646 if (! $export_plugin->exportDBHeader($current_db)) {
647 break 2;
649 if (! $export_plugin->exportDBCreate($current_db)) {
650 break 2;
652 if (method_exists($export_plugin, 'exportRoutines')
653 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
654 && isset($GLOBALS['sql_procedure_function'])
656 $export_plugin->exportRoutines($current_db);
659 $tables = $GLOBALS['dbi']->getTables($current_db);
660 $views = array();
661 foreach ($tables as $table) {
662 // if this is a view, collect it for later;
663 // views must be exported after the tables
664 $is_view = PMA_Table::isView($current_db, $table);
665 if ($is_view) {
666 $views[] = $table;
668 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
669 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
671 // for a view, export a stand-in definition of the table
672 // to resolve view dependencies
673 if (! $export_plugin->exportStructure(
674 $current_db, $table, $crlf, $err_url,
675 $is_view ? 'stand_in' : 'create_table', $export_type,
676 $do_relation, $do_comments, $do_mime, $do_dates
677 )) {
678 break 3;
681 // if this is a view or a merge table, don't export data
682 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
683 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
684 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
686 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
687 . '.' . PMA_Util::backquote($table);
688 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
689 break 3;
692 // now export the triggers (needs to be done after the data
693 // because triggers can modify already imported tables)
694 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
695 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
697 if (! $export_plugin->exportStructure(
698 $current_db, $table, $crlf, $err_url,
699 'triggers', $export_type,
700 $do_relation, $do_comments, $do_mime, $do_dates
701 )) {
702 break 2;
706 foreach ($views as $view) {
707 // no data export for a view
708 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
709 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
711 if (! $export_plugin->exportStructure(
712 $current_db, $view, $crlf, $err_url,
713 'create_view', $export_type,
714 $do_relation, $do_comments, $do_mime, $do_dates
715 )) {
716 break 3;
720 if (! $export_plugin->exportDBFooter($current_db)) {
721 break 2;
725 } elseif ($export_type == 'database') {
726 if (! $export_plugin->exportDBHeader($db)) {
727 break;
729 if (! $export_plugin->exportDBCreate($db)) {
730 break;
733 if (method_exists($export_plugin, 'exportRoutines')
734 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
735 && isset($GLOBALS['sql_procedure_function'])
737 $export_plugin->exportRoutines($db);
740 $i = 0;
741 $views = array();
742 // $tables contains the choices from the user (via $table_select)
743 foreach ($tables as $table) {
744 // if this is a view, collect it for later; views must be exported after
745 // the tables
746 $is_view = PMA_Table::isView($db, $table);
747 if ($is_view) {
748 $views[] = $table;
750 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
751 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
753 // for a view, export a stand-in definition of the table
754 // to resolve view dependencies
755 if (! $export_plugin->exportStructure(
756 $db, $table, $crlf, $err_url,
757 $is_view ? 'stand_in' : 'create_table', $export_type,
758 $do_relation, $do_comments, $do_mime, $do_dates
759 )) {
760 break 2;
763 // if this is a view or a merge table, don't export data
764 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
765 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
766 && ! ($is_view || PMA_Table::isMerge($db, $table))
768 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
769 . '.' . PMA_Util::backquote($table);
770 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
771 break 2;
774 // now export the triggers (needs to be done after the data because
775 // triggers can modify already imported tables)
776 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
777 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
779 if (! $export_plugin->exportStructure(
780 $db, $table, $crlf, $err_url,
781 'triggers', $export_type,
782 $do_relation, $do_comments, $do_mime, $do_dates
783 )) {
784 break 2;
788 foreach ($views as $view) {
789 // no data export for a view
790 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
791 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
793 if (! $export_plugin->exportStructure(
794 $db, $view, $crlf, $err_url,
795 'create_view', $export_type,
796 $do_relation, $do_comments, $do_mime, $do_dates
797 )) {
798 break 2;
803 if (! $export_plugin->exportDBFooter($db)) {
804 break;
806 } else {
807 if (! $export_plugin->exportDBHeader($db)) {
808 break;
810 // We export just one table
811 // $allrows comes from the form when "Dump all rows" has been selected
812 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
813 $add_query = ' LIMIT '
814 . (($limit_from > 0) ? $limit_from . ', ' : '')
815 . $limit_to;
816 } else {
817 $add_query = '';
820 $is_view = PMA_Table::isView($db, $table);
821 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
822 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
824 if (! $export_plugin->exportStructure(
825 $db, $table, $crlf, $err_url,
826 $is_view ? 'create_view' : 'create_table', $export_type,
827 $do_relation, $do_comments, $do_mime, $do_dates
828 )) {
829 break;
832 // If this is an export of a single view, we have to export data;
833 // for example, a PDF report
834 // if it is a merge table, no data is exported
835 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
836 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
837 && ! PMA_Table::isMerge($db, $table)
839 if (! empty($sql_query)) {
840 // only preg_replace if needed
841 if (! empty($add_query)) {
842 // remove trailing semicolon before adding a LIMIT
843 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
845 $local_query = $sql_query . $add_query;
846 $GLOBALS['dbi']->selectDb($db);
847 } else {
848 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
849 . '.' . PMA_Util::backquote($table) . $add_query;
851 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
852 break;
855 // now export the triggers (needs to be done after the data because
856 // triggers can modify already imported tables)
857 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
858 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
860 if (! $export_plugin->exportStructure(
861 $db, $table, $crlf, $err_url,
862 'triggers', $export_type,
863 $do_relation, $do_comments, $do_mime, $do_dates
864 )) {
865 break 2;
868 if (! $export_plugin->exportDBFooter($db)) {
869 break;
872 if (! $export_plugin->exportFooter()) {
873 break;
876 } while (false);
877 // End of fake loop
879 if ($save_on_server && isset($message)) {
880 if ($export_type == 'server') {
881 $active_page = 'server_export.php';
882 include 'server_export.php';
883 } elseif ($export_type == 'database') {
884 $active_page = 'db_export.php';
885 include 'db_export.php';
886 } else {
887 $active_page = 'tbl_export.php';
888 include 'tbl_export.php';
890 exit();
894 * Send the dump as a file...
896 if (! empty($asfile)) {
897 // Convert the charset if required.
898 if ($output_charset_conversion) {
899 $dump_buffer = PMA_convertString(
900 'utf-8',
901 $GLOBALS['charset_of_file'],
902 $dump_buffer
906 // Do the compression
907 // 1. as a zipped file
908 if ($compression == 'zip') {
909 if (@function_exists('gzcompress')) {
910 $zipfile = new ZipFile();
911 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
912 $dump_buffer = $zipfile->file();
914 } elseif ($compression == 'bzip2') {
915 // 2. as a bzipped file
916 if (@function_exists('bzcompress')) {
917 $dump_buffer = bzcompress($dump_buffer);
919 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
920 // 3. as a gzipped file
921 // without the optional parameter level because it bugs
922 $dump_buffer = gzencode($dump_buffer);
925 /* If we saved on server, we have to close file now */
926 if ($save_on_server) {
927 $write_result = @fwrite($file_handle, $dump_buffer);
928 fclose($file_handle);
929 if (strlen($dump_buffer) > 0
930 && (! $write_result || ($write_result != strlen($dump_buffer)))
932 $message = new PMA_Message(
933 __('Insufficient space to save the file %s.'),
934 PMA_Message::ERROR,
935 $save_filename
937 } else {
938 $message = new PMA_Message(
939 __('Dump has been saved to file %s.'),
940 PMA_Message::SUCCESS,
941 $save_filename
945 if ($export_type == 'server') {
946 $active_page = 'server_export.php';
947 include_once 'server_export.php';
948 } elseif ($export_type == 'database') {
949 $active_page = 'db_export.php';
950 include_once 'db_export.php';
951 } else {
952 $active_page = 'tbl_export.php';
953 include_once 'tbl_export.php';
955 exit();
956 } else {
957 PMA_Response::getInstance()->disable();
958 echo $dump_buffer;
960 } else {
962 * Displays the dump...
964 * Close the html tags and add the footers if dump is displayed on screen
966 echo '</textarea>' . "\n"
967 . ' </form>' . "\n";
968 echo $back_button;
970 echo "\n";
971 echo '</div>' . "\n";
972 echo "\n";
974 <script type="text/javascript">
975 //<![CDATA[
976 var $body = $("body");
977 $("#textSQLDUMP")
978 .width($body.width() - 50)
979 .height($body.height() - 100);
980 //]]>
981 </script>
982 <?php
983 } // end if