Update po files
[phpmyadmin.git] / export.php
blobedaf43269072ac11ce5ec865f6511fbd2e0ad54d
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_columns',
58 'excel_edition',
59 'excel_structure_or_data',
60 'yaml_structure_or_data',
61 'ods_null',
62 'ods_structure_or_data',
63 'ods_columns',
64 'json_structure_or_data',
65 'xml_structure_or_data',
66 'xml_export_functions',
67 'xml_export_procedures',
68 'xml_export_tables',
69 'xml_export_triggers',
70 'xml_export_views',
71 'xml_export_contents',
72 'texytext_structure_or_data',
73 'texytext_columns',
74 'texytext_null',
75 'phparray_structure_or_data',
76 'sql_include_comments',
77 'sql_header_comment',
78 'sql_dates',
79 'sql_relation',
80 'sql_mime',
81 'sql_use_transaction',
82 'sql_disable_fk',
83 'sql_compatibility',
84 'sql_structure_or_data',
85 'sql_drop_table',
86 'sql_procedure_function',
87 'sql_create_table_statements',
88 'sql_if_not_exists',
89 'sql_auto_increment',
90 'sql_backquotes',
91 'sql_truncate',
92 'sql_delayed',
93 'sql_ignore',
94 'sql_type',
95 'sql_insert_syntax',
96 'sql_max_query_size',
97 'sql_hex_for_blob',
98 'sql_utc_time',
99 'csv_separator',
100 'csv_enclosed',
101 'csv_escaped',
102 'csv_terminated',
103 'csv_null',
104 'csv_columns',
105 'csv_structure_or_data',
106 // csv_replace should have been here but we use it directly from $_POST
107 'latex_caption',
108 'latex_structure_or_data',
109 'latex_structure_caption',
110 'latex_structure_continued_caption',
111 'latex_structure_label',
112 'latex_relation',
113 'latex_comments',
114 'latex_mime',
115 'latex_columns',
116 'latex_data_caption',
117 'latex_data_continued_caption',
118 'latex_data_label',
119 'latex_null'
122 foreach ($post_params as $one_post_param) {
123 if (isset($_POST[$one_post_param])) {
124 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
128 // sanitize this parameter which will be used below in a file inclusion
129 $what = PMA_securePath($what);
131 PMA_Util::checkParameters(array('what', 'export_type'));
133 // export class instance, not array of properties, as before
134 $export_plugin = PMA_getPlugin(
135 "export",
136 $what,
137 'libraries/plugins/export/',
138 array(
139 'export_type' => $export_type,
140 'single_table' => isset($single_table)
144 // Backward compatbility
145 $type = $what;
147 // Check export type
148 if (! isset($export_plugin)) {
149 PMA_fatalError(__('Bad type!'));
153 * valid compression methods
155 $compression_methods = array(
156 'zip',
157 'gzip',
158 'bzip2',
162 * init and variable checking
164 $compression = false;
165 $onserver = false;
166 $save_on_server = false;
167 $buffer_needed = false;
169 // Is it a quick or custom export?
170 if ($_REQUEST['quick_or_custom'] == 'quick') {
171 $quick_export = true;
172 } else {
173 $quick_export = false;
176 if ($_REQUEST['output_format'] == 'astext') {
177 $asfile = false;
178 } else {
179 $asfile = true;
180 if (in_array($_REQUEST['compression'], $compression_methods)) {
181 $compression = $_REQUEST['compression'];
182 $buffer_needed = true;
184 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
185 || (! $quick_export && ! empty($_REQUEST['onserver']))
187 if ($quick_export) {
188 $onserver = $_REQUEST['quick_export_onserver'];
189 } else {
190 $onserver = $_REQUEST['onserver'];
192 // Will we save dump on server?
193 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
197 // Does export require to be into file?
198 if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
199 $message = PMA_Message::error(
200 __('Selected export type has to be saved in file!')
202 if ($export_type == 'server') {
203 $active_page = 'server_export.php';
204 include 'server_export.php';
205 } elseif ($export_type == 'database') {
206 $active_page = 'db_export.php';
207 include 'db_export.php';
208 } else {
209 $active_page = 'tbl_export.php';
210 include 'tbl_export.php';
212 exit();
215 // Generate error url and check for needed variables
216 if ($export_type == 'server') {
217 $err_url = 'server_export.php?' . PMA_generate_common_url();
218 } elseif ($export_type == 'database' && strlen($db)) {
219 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
220 // Check if we have something to export
221 if (isset($table_select)) {
222 $tables = $table_select;
223 } else {
224 $tables = array();
226 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
227 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
228 } else {
229 PMA_fatalError(__('Bad parameters!'));
233 * Increase time limit for script execution and initializes some variables
235 @set_time_limit($cfg['ExecTimeLimit']);
236 if (! empty($cfg['MemoryLimit'])) {
237 @ini_set('memory_limit', $cfg['MemoryLimit']);
240 // Start with empty buffer
241 $dump_buffer = '';
242 $dump_buffer_len = 0;
244 // We send fake headers to avoid browser timeout when buffering
245 $time_start = time();
249 * Detect ob_gzhandler
251 * @return bool
253 function PMA_isGzHandlerEnabled()
255 return in_array('ob_gzhandler', ob_list_handlers());
259 * Detect whether gzencode is needed; it might not be needed if
260 * the server is already compressing by itself
262 * @return bool Whether gzencode is needed
264 function PMA_gzencodeNeeded()
266 if (@function_exists('gzencode')
267 && ! @ini_get('zlib.output_compression')
268 // Here, we detect Apache's mod_deflate so we bet that
269 // this module is active for this instance of phpMyAdmin
270 // and therefore, will gzip encode the content
271 && ! (function_exists('apache_get_modules')
272 && in_array('mod_deflate', apache_get_modules()))
273 && ! PMA_isGzHandlerEnabled()
275 return true;
276 } else {
277 return false;
282 * Output handler for all exports, if needed buffering, it stores data into
283 * $dump_buffer, otherwise it prints thems out.
285 * @param string $line the insert statement
287 * @return bool Whether output succeeded
289 function PMA_exportOutputHandler($line)
291 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
293 // Kanji encoding convert feature
294 if ($GLOBALS['output_kanji_conversion']) {
295 $line = PMA_kanji_str_conv(
296 $line,
297 $GLOBALS['knjenc'],
298 isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
301 // If we have to buffer data, we will perform everything at once at the end
302 if ($GLOBALS['buffer_needed']) {
304 $dump_buffer .= $line;
305 if ($GLOBALS['onfly_compression']) {
307 $dump_buffer_len += strlen($line);
309 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
310 if ($GLOBALS['output_charset_conversion']) {
311 $dump_buffer = PMA_convert_string(
312 'utf-8',
313 $GLOBALS['charset_of_file'],
314 $dump_buffer
317 // as bzipped
318 if ($GLOBALS['compression'] == 'bzip2'
319 && @function_exists('bzcompress')
321 $dump_buffer = bzcompress($dump_buffer);
322 } elseif ($GLOBALS['compression'] == 'gzip'
323 && PMA_gzencodeNeeded()
325 // as a gzipped file
326 // without the optional parameter level because it bugs
327 $dump_buffer = gzencode($dump_buffer);
329 if ($GLOBALS['save_on_server']) {
330 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
331 if (! $write_result || ($write_result != strlen($dump_buffer))) {
332 $GLOBALS['message'] = PMA_Message::error(
333 __('Insufficient space to save the file %s.')
335 $GLOBALS['message']->addParam($save_filename);
336 return false;
338 } else {
339 echo $dump_buffer;
341 $dump_buffer = '';
342 $dump_buffer_len = 0;
344 } else {
345 $time_now = time();
346 if ($time_start >= $time_now + 30) {
347 $time_start = $time_now;
348 header('X-pmaPing: Pong');
349 } // end if
351 } else {
352 if ($GLOBALS['asfile']) {
353 if ($GLOBALS['output_charset_conversion']) {
354 $line = PMA_convert_string(
355 'utf-8',
356 $GLOBALS['charset_of_file'],
357 $line
360 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
361 $write_result = @fwrite($GLOBALS['file_handle'], $line);
362 if (! $write_result || ($write_result != strlen($line))) {
363 $GLOBALS['message'] = PMA_Message::error(
364 __('Insufficient space to save the file %s.')
366 $GLOBALS['message']->addParam($save_filename);
367 return false;
369 $time_now = time();
370 if ($time_start >= $time_now + 30) {
371 $time_start = $time_now;
372 header('X-pmaPing: Pong');
373 } // end if
374 } else {
375 // We export as file - output normally
376 echo $line;
378 } else {
379 // We export as html - replace special chars
380 echo htmlspecialchars($line);
383 return true;
384 } // end of the 'PMA_exportOutputHandler()' function
386 // Defines the default <CR><LF> format.
387 // For SQL always use \n as MySQL wants this on all platforms.
388 if ($what == 'sql') {
389 $crlf = "\n";
390 } else {
391 $crlf = PMA_Util::whichCrlf();
394 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
396 // Do we need to convert charset?
397 $output_charset_conversion = $asfile
398 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
399 && isset($charset_of_file) && $charset_of_file != 'utf-8'
400 && $type != 'xls';
402 // Use on the fly compression?
403 $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
404 && ($compression == 'gzip' || $compression == 'bzip2');
405 if ($onfly_compression) {
406 $memory_limit = trim(@ini_get('memory_limit'));
407 // 2 MB as default
408 if (empty($memory_limit)) {
409 $memory_limit = 2 * 1024 * 1024;
412 if (strtolower(substr($memory_limit, -1)) == 'm') {
413 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
414 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
415 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
416 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
417 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
418 } else {
419 $memory_limit = (int)$memory_limit;
422 // Some of memory is needed for other thins and as treshold.
423 // Nijel: During export I had allocated (see memory_get_usage function)
424 // approx 1.2MB so this comes from that.
425 if ($memory_limit > 1500000) {
426 $memory_limit -= 1500000;
429 // Some memory is needed for compression, assume 1/3
430 $memory_limit /= 8;
433 // Generate filename and mime type if needed
434 if ($asfile) {
435 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
436 if ($export_type == 'server') {
437 if (isset($remember_template)) {
438 $GLOBALS['PMA_Config']->setUserValue(
439 'pma_server_filename_template',
440 'Export/file_template_server',
441 $filename_template
444 } elseif ($export_type == 'database') {
445 if (isset($remember_template)) {
446 $GLOBALS['PMA_Config']->setUserValue(
447 'pma_db_filename_template',
448 'Export/file_template_database',
449 $filename_template
452 } else {
453 if (isset($remember_template)) {
454 $GLOBALS['PMA_Config']->setUserValue(
455 'pma_table_filename_template',
456 'Export/file_template_table',
457 $filename_template
461 $filename = PMA_Util::expandUserString($filename_template);
462 // remove dots in filename (coming from either the template or already
463 // part of the filename) to avoid a remote code execution vulnerability
464 $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
466 // Grab basic dump extension and mime type
467 // Check if the user already added extension;
468 // get the substring where the extension would be if it was included
469 $extension_start_pos = strlen($filename) - strlen(
470 $export_plugin->getProperties()->getExtension()
471 ) - 1;
472 $user_extension = substr($filename, $extension_start_pos, strlen($filename));
473 $required_extension = "." . $export_plugin->getProperties()->getExtension();
474 if (strtolower($user_extension) != $required_extension) {
475 $filename .= $required_extension;
477 $mime_type = $export_plugin->getProperties()->getMimeType();
479 // If dump is going to be compressed, set correct mime_type and add
480 // compression to extension
481 if ($compression == 'bzip2') {
482 $filename .= '.bz2';
483 $mime_type = 'application/x-bzip2';
484 } elseif ($compression == 'gzip') {
485 $filename .= '.gz';
486 $mime_type = 'application/x-gzip';
487 } elseif ($compression == 'zip') {
488 $filename .= '.zip';
489 $mime_type = 'application/zip';
493 // Open file on server if needed
494 if ($save_on_server) {
495 $save_filename = PMA_Util::userDir($cfg['SaveDir'])
496 . preg_replace('@[/\\\\]@', '_', $filename);
497 unset($message);
498 if (file_exists($save_filename)
499 && ((! $quick_export && empty($onserverover))
500 || ($quick_export
501 && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
503 $message = PMA_Message::error(
504 __('File %s already exists on server, change filename or check overwrite option.')
506 $message->addParam($save_filename);
507 } else {
508 if (is_file($save_filename) && ! is_writable($save_filename)) {
509 $message = PMA_Message::error(
510 __('The web server does not have permission to save the file %s.')
512 $message->addParam($save_filename);
513 } else {
514 if (! $file_handle = @fopen($save_filename, 'w')) {
515 $message = PMA_Message::error(
516 __('The web server does not have permission to save the file %s.')
518 $message->addParam($save_filename);
522 if (isset($message)) {
523 if ($export_type == 'server') {
524 $active_page = 'server_export.php';
525 include 'server_export.php';
526 } elseif ($export_type == 'database') {
527 $active_page = 'db_export.php';
528 include 'db_export.php';
529 } else {
530 $active_page = 'tbl_export.php';
531 include 'tbl_export.php';
533 exit();
538 * Send headers depending on whether the user chose to download a dump file
539 * or not
541 if (! $save_on_server) {
542 if ($asfile) {
543 // Download
544 // (avoid rewriting data containing HTML with anchors and forms;
545 // this was reported to happen under Plesk)
546 @ini_set('url_rewriter.tags', '');
547 $filename = PMA_sanitizeFilename($filename);
549 PMA_downloadHeader($filename, $mime_type);
550 } else {
551 // HTML
552 if ($export_type == 'database') {
553 $num_tables = count($tables);
554 if ($num_tables == 0) {
555 $message = PMA_Message::error(__('No tables found in database.'));
556 $active_page = 'db_export.php';
557 include 'db_export.php';
558 exit();
561 $backup_cfgServer = $cfg['Server'];
562 $cfg['Server'] = $backup_cfgServer;
563 unset($backup_cfgServer);
564 echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
565 //echo ' <pre>' . "\n";
568 * Displays a back button with all the $_REQUEST data in the URL
569 * (store in a variable to also display after the textarea)
571 $back_button = '<p>[ <a href="';
572 if ($export_type == 'server') {
573 $back_button .= 'server_export.php?' . PMA_generate_common_url();
574 } elseif ($export_type == 'database') {
575 $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
576 } else {
577 $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
580 // Convert the multiple select elements from an array to a string
581 if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
582 $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
583 } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
584 $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
587 foreach ($_REQUEST as $name => $value) {
588 $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
590 $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
592 echo $back_button;
593 echo ' <form name="nofunction">' . "\n"
594 // remove auto-select for now: there is no way to select
595 // only a part of the text; anyway, it should obey
596 // $cfg['TextareaAutoSelect']
597 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
598 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
599 } // end download
602 // Fake loop just to allow skip of remain of this code by break, I'd really
603 // need exceptions here :-)
604 do {
606 // Add possibly some comments to export
607 if (! $export_plugin->exportHeader($db)) {
608 break;
611 // Will we need relation & co. setup?
612 $do_relation = isset($GLOBALS[$what . '_relation']);
613 $do_comments = isset($GLOBALS[$what . '_include_comments']);
614 $do_mime = isset($GLOBALS[$what . '_mime']);
615 if ($do_relation || $do_comments || $do_mime) {
616 $cfgRelation = PMA_getRelationsParam();
618 if ($do_mime) {
619 include_once 'libraries/transformations.lib.php';
622 // Include dates in export?
623 $do_dates = isset($GLOBALS[$what . '_dates']);
626 * Builds the dump
628 // Gets the number of tables if a dump of a database has been required
629 if ($export_type == 'server') {
630 if (isset($db_select)) {
631 $tmp_select = implode($db_select, '|');
632 $tmp_select = '|' . $tmp_select . '|';
634 // Walk over databases
635 foreach ($GLOBALS['pma']->databases as $current_db) {
636 if (isset($tmp_select)
637 && strpos(' ' . $tmp_select, '|' . $current_db . '|')
639 if (! $export_plugin->exportDBHeader($current_db)) {
640 break 2;
642 if (! $export_plugin->exportDBCreate($current_db)) {
643 break 2;
645 if (method_exists($export_plugin, 'exportRoutines')
646 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
647 && isset($GLOBALS['sql_procedure_function'])
649 $export_plugin->exportRoutines($current_db);
652 $tables = PMA_DBI_get_tables($current_db);
653 $views = array();
654 foreach ($tables as $table) {
655 // if this is a view, collect it for later;
656 // views must be exported after the tables
657 $is_view = PMA_Table::isView($current_db, $table);
658 if ($is_view) {
659 $views[] = $table;
661 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
662 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
664 // for a view, export a stand-in definition of the table
665 // to resolve view dependencies
666 if (! $export_plugin->exportStructure(
667 $current_db, $table, $crlf, $err_url,
668 $is_view ? 'stand_in' : 'create_table', $export_type,
669 $do_relation, $do_comments, $do_mime, $do_dates
670 )) {
671 break 3;
674 // if this is a view or a merge table, don't export data
675 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
676 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
677 && ! ($is_view || PMA_Table::isMerge($current_db, $table))
679 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
680 . '.' . PMA_Util::backquote($table);
681 if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
682 break 3;
685 // now export the triggers (needs to be done after the data
686 // because triggers can modify already imported tables)
687 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
688 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
690 if (! $export_plugin->exportStructure(
691 $current_db, $table, $crlf, $err_url,
692 'triggers', $export_type,
693 $do_relation, $do_comments, $do_mime, $do_dates
694 )) {
695 break 2;
699 foreach ($views as $view) {
700 // no data export for a view
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, $view, $crlf, $err_url,
706 'create_view', $export_type,
707 $do_relation, $do_comments, $do_mime, $do_dates
708 )) {
709 break 3;
713 if (! $export_plugin->exportDBFooter($current_db)) {
714 break 2;
718 } elseif ($export_type == 'database') {
719 if (! $export_plugin->exportDBHeader($db)) {
720 break;
722 if (! $export_plugin->exportDBCreate($db)) {
723 break;
726 if (method_exists($export_plugin, 'exportRoutines')
727 && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
728 && isset($GLOBALS['sql_procedure_function'])
730 $export_plugin->exportRoutines($db);
733 $i = 0;
734 $views = array();
735 // $tables contains the choices from the user (via $table_select)
736 foreach ($tables as $table) {
737 // if this is a view, collect it for later; views must be exported after
738 // the tables
739 $is_view = PMA_Table::isView($db, $table);
740 if ($is_view) {
741 $views[] = $table;
743 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
744 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
746 // for a view, export a stand-in definition of the table
747 // to resolve view dependencies
748 if (! $export_plugin->exportStructure(
749 $db, $table, $crlf, $err_url,
750 $is_view ? 'stand_in' : 'create_table', $export_type,
751 $do_relation, $do_comments, $do_mime, $do_dates
752 )) {
753 break 2;
756 // if this is a view or a merge table, don't export data
757 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
758 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
759 && ! ($is_view || PMA_Table::isMerge($db, $table))
761 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
762 . '.' . PMA_Util::backquote($table);
763 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
764 break 2;
767 // now export the triggers (needs to be done after the data because
768 // triggers can modify already imported tables)
769 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
770 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
772 if (! $export_plugin->exportStructure(
773 $db, $table, $crlf, $err_url,
774 'triggers', $export_type,
775 $do_relation, $do_comments, $do_mime, $do_dates
776 )) {
777 break 2;
781 foreach ($views as $view) {
782 // no data export for a view
783 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
784 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
786 if (! $export_plugin->exportStructure(
787 $db, $view, $crlf, $err_url,
788 'create_view', $export_type,
789 $do_relation, $do_comments, $do_mime, $do_dates
790 )) {
791 break 2;
796 if (! $export_plugin->exportDBFooter($db)) {
797 break;
799 } else {
800 if (! $export_plugin->exportDBHeader($db)) {
801 break;
803 // We export just one table
804 // $allrows comes from the form when "Dump all rows" has been selected
805 if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
806 $add_query = ' LIMIT '
807 . (($limit_from > 0) ? $limit_from . ', ' : '')
808 . $limit_to;
809 } else {
810 $add_query = '';
813 $is_view = PMA_Table::isView($db, $table);
814 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
815 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
817 if (! $export_plugin->exportStructure(
818 $db, $table, $crlf, $err_url,
819 $is_view ? 'create_view' : 'create_table', $export_type,
820 $do_relation, $do_comments, $do_mime, $do_dates
821 )) {
822 break;
825 // If this is an export of a single view, we have to export data;
826 // for example, a PDF report
827 // if it is a merge table, no data is exported
828 if (($GLOBALS[$what . '_structure_or_data'] == 'data'
829 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
830 && ! PMA_Table::isMerge($db, $table)
832 if (! empty($sql_query)) {
833 // only preg_replace if needed
834 if (! empty($add_query)) {
835 // remove trailing semicolon before adding a LIMIT
836 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
838 $local_query = $sql_query . $add_query;
839 PMA_DBI_select_db($db);
840 } else {
841 $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
842 . '.' . PMA_Util::backquote($table) . $add_query;
844 if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
845 break;
848 // now export the triggers (needs to be done after the data because
849 // triggers can modify already imported tables)
850 if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
851 || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
853 if (! $export_plugin->exportStructure(
854 $db, $table, $crlf, $err_url,
855 'triggers', $export_type,
856 $do_relation, $do_comments, $do_mime, $do_dates
857 )) {
858 break 2;
861 if (! $export_plugin->exportDBFooter($db)) {
862 break;
865 if (! $export_plugin->exportFooter()) {
866 break;
869 } while (false);
870 // End of fake loop
872 if ($save_on_server && isset($message)) {
873 if ($export_type == 'server') {
874 $active_page = 'server_export.php';
875 include 'server_export.php';
876 } elseif ($export_type == 'database') {
877 $active_page = 'db_export.php';
878 include 'db_export.php';
879 } else {
880 $active_page = 'tbl_export.php';
881 include 'tbl_export.php';
883 exit();
887 * Send the dump as a file...
889 if (! empty($asfile)) {
890 // Convert the charset if required.
891 if ($output_charset_conversion) {
892 $dump_buffer = PMA_convert_string(
893 'utf-8',
894 $GLOBALS['charset_of_file'],
895 $dump_buffer
899 // Do the compression
900 // 1. as a zipped file
901 if ($compression == 'zip') {
902 if (@function_exists('gzcompress')) {
903 $zipfile = new ZipFile();
904 $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
905 $dump_buffer = $zipfile->file();
907 } elseif ($compression == 'bzip2') {
908 // 2. as a bzipped file
909 if (@function_exists('bzcompress')) {
910 $dump_buffer = bzcompress($dump_buffer);
912 } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
913 // 3. as a gzipped file
914 // without the optional parameter level because it bugs
915 $dump_buffer = gzencode($dump_buffer);
918 /* If we saved on server, we have to close file now */
919 if ($save_on_server) {
920 $write_result = @fwrite($file_handle, $dump_buffer);
921 fclose($file_handle);
922 if (strlen($dump_buffer) > 0
923 && (! $write_result || ($write_result != strlen($dump_buffer)))
925 $message = new PMA_Message(
926 __('Insufficient space to save the file %s.'),
927 PMA_Message::ERROR,
928 $save_filename
930 } else {
931 $message = new PMA_Message(
932 __('Dump has been saved to file %s.'),
933 PMA_Message::SUCCESS,
934 $save_filename
938 if ($export_type == 'server') {
939 $active_page = 'server_export.php';
940 include_once 'server_export.php';
941 } elseif ($export_type == 'database') {
942 $active_page = 'db_export.php';
943 include_once 'db_export.php';
944 } else {
945 $active_page = 'tbl_export.php';
946 include_once 'tbl_export.php';
948 exit();
949 } else {
950 PMA_Response::getInstance()->disable();
951 echo $dump_buffer;
953 } else {
955 * Displays the dump...
957 * Close the html tags and add the footers if dump is displayed on screen
959 echo '</textarea>' . "\n"
960 . ' </form>' . "\n";
961 echo $back_button;
963 echo "\n";
964 echo '</div>' . "\n";
965 echo "\n";
967 <script type="text/javascript">
968 //<![CDATA[
969 var $body = $("body");
970 $("#textSQLDUMP")
971 .width($body.width() - 50)
972 .height($body.height() - 100);
973 //]]>
974 </script>
975 <?php
976 } // end if