Translated using Weblate (German)
[phpmyadmin.git] / export.php
blob9f153c8bca4007778f2a5bbd08ffa561fb781fbb
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Encoding;
11 use PhpMyAdmin\Plugins\ExportPlugin;
12 use PhpMyAdmin\Url;
13 use PhpMyAdmin\Util;
14 use PhpMyAdmin\Sanitize;
16 /**
17 * Get the variables sent or posted to this script and a core script
19 /**
20 * If we are sending the export file (as opposed to just displaying it
21 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
23 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
24 define('PMA_BYPASS_GET_INSTANCE', 1);
26 include_once 'libraries/common.inc.php';
27 include_once 'libraries/plugin_interface.lib.php';
28 include_once 'libraries/export.lib.php';
30 //check if it's the GET request to check export time out
31 if (isset($_GET['check_time_out'])) {
32 if (isset($_SESSION['pma_export_error'])) {
33 $err = $_SESSION['pma_export_error'];
34 unset($_SESSION['pma_export_error']);
35 echo "timeout";
36 } else {
37 echo "success";
39 exit;
41 /**
42 * Sets globals from $_POST
44 * - Please keep the parameters in order of their appearance in the form
45 * - Some of these parameters are not used, as the code below directly
46 * verifies from the superglobal $_POST or $_REQUEST
47 * TODO: this should be removed to avoid passing user input to GLOBALS
48 * without checking
50 $post_params = array(
51 'db',
52 'table',
53 'what',
54 'single_table',
55 'export_type',
56 'export_method',
57 'quick_or_custom',
58 'db_select',
59 'table_select',
60 'table_structure',
61 'table_data',
62 'limit_to',
63 'limit_from',
64 'allrows',
65 'lock_tables',
66 'output_format',
67 'filename_template',
68 'maxsize',
69 'remember_template',
70 'charset',
71 'compression',
72 'as_separate_files',
73 'knjenc',
74 'xkana',
75 'htmlword_structure_or_data',
76 'htmlword_null',
77 'htmlword_columns',
78 'mediawiki_headers',
79 'mediawiki_structure_or_data',
80 'mediawiki_caption',
81 'pdf_structure_or_data',
82 'odt_structure_or_data',
83 'odt_relation',
84 'odt_comments',
85 'odt_mime',
86 'odt_columns',
87 'odt_null',
88 'codegen_structure_or_data',
89 'codegen_format',
90 'excel_null',
91 'excel_removeCRLF',
92 'excel_columns',
93 'excel_edition',
94 'excel_structure_or_data',
95 'yaml_structure_or_data',
96 'ods_null',
97 'ods_structure_or_data',
98 'ods_columns',
99 'json_structure_or_data',
100 'json_pretty_print',
101 'json_unicode',
102 'xml_structure_or_data',
103 'xml_export_events',
104 'xml_export_functions',
105 'xml_export_procedures',
106 'xml_export_tables',
107 'xml_export_triggers',
108 'xml_export_views',
109 'xml_export_contents',
110 'texytext_structure_or_data',
111 'texytext_columns',
112 'texytext_null',
113 'phparray_structure_or_data',
114 'sql_include_comments',
115 'sql_header_comment',
116 'sql_dates',
117 'sql_relation',
118 'sql_mime',
119 'sql_use_transaction',
120 'sql_disable_fk',
121 'sql_compatibility',
122 'sql_structure_or_data',
123 'sql_create_database',
124 'sql_drop_table',
125 'sql_procedure_function',
126 'sql_create_table',
127 'sql_create_view',
128 'sql_create_trigger',
129 'sql_if_not_exists',
130 'sql_auto_increment',
131 'sql_backquotes',
132 'sql_truncate',
133 'sql_delayed',
134 'sql_ignore',
135 'sql_type',
136 'sql_insert_syntax',
137 'sql_max_query_size',
138 'sql_hex_for_binary',
139 'sql_utc_time',
140 'sql_drop_database',
141 'sql_views_as_tables',
142 'sql_metadata',
143 'csv_separator',
144 'csv_enclosed',
145 'csv_escaped',
146 'csv_terminated',
147 'csv_null',
148 'csv_removeCRLF',
149 'csv_columns',
150 'csv_structure_or_data',
151 // csv_replace should have been here but we use it directly from $_POST
152 'latex_caption',
153 'latex_structure_or_data',
154 'latex_structure_caption',
155 'latex_structure_continued_caption',
156 'latex_structure_label',
157 'latex_relation',
158 'latex_comments',
159 'latex_mime',
160 'latex_columns',
161 'latex_data_caption',
162 'latex_data_continued_caption',
163 'latex_data_label',
164 'latex_null',
165 'aliases'
168 foreach ($post_params as $one_post_param) {
169 if (isset($_POST[$one_post_param])) {
170 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
174 $table = $GLOBALS['table'];
176 PhpMyAdmin\Util::checkParameters(array('what', 'export_type'));
178 // sanitize this parameter which will be used below in a file inclusion
179 $what = Core::securePath($_POST['what']);
181 // export class instance, not array of properties, as before
182 /* @var $export_plugin ExportPlugin */
183 $export_plugin = PMA_getPlugin(
184 "export",
185 $what,
186 'libraries/classes/Plugins/Export/',
187 array(
188 'export_type' => $export_type,
189 'single_table' => isset($single_table)
193 // Check export type
194 if (empty($export_plugin)) {
195 Core::fatalError(__('Bad type!'));
199 * valid compression methods
201 $compression_methods = array(
202 'zip',
203 'gzip'
207 * init and variable checking
209 $compression = false;
210 $onserver = false;
211 $save_on_server = false;
212 $buffer_needed = false;
213 $back_button = '';
214 $save_filename = '';
215 $file_handle = '';
216 $err_url = '';
217 $filename = '';
218 $separate_files = '';
220 // Is it a quick or custom export?
221 if (isset($_REQUEST['quick_or_custom'])
222 && $_REQUEST['quick_or_custom'] == 'quick'
224 $quick_export = true;
225 } else {
226 $quick_export = false;
229 if ($_REQUEST['output_format'] == 'astext') {
230 $asfile = false;
231 } else {
232 $asfile = true;
233 if (isset($_REQUEST['as_separate_files'])
234 && ! empty($_REQUEST['as_separate_files'])
236 if (isset($_REQUEST['compression'])
237 && ! empty($_REQUEST['compression'])
238 && $_REQUEST['compression'] == 'zip'
240 $separate_files = $_REQUEST['as_separate_files'];
243 if (in_array($_REQUEST['compression'], $compression_methods)) {
244 $compression = $_REQUEST['compression'];
245 $buffer_needed = true;
247 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
248 || (! $quick_export && ! empty($_REQUEST['onserver']))
250 if ($quick_export) {
251 $onserver = $_REQUEST['quick_export_onserver'];
252 } else {
253 $onserver = $_REQUEST['onserver'];
255 // Will we save dump on server?
256 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
260 // Generate error url and check for needed variables
261 if ($export_type == 'server') {
262 $err_url = 'server_export.php' . Url::getCommon();
263 } elseif ($export_type == 'database' && strlen($db) > 0) {
264 $err_url = 'db_export.php' . Url::getCommon(array('db' => $db));
265 // Check if we have something to export
266 if (isset($table_select)) {
267 $tables = $table_select;
268 } else {
269 $tables = array();
271 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
272 $err_url = 'tbl_export.php' . Url::getCommon(
273 array(
274 'db' => $db, 'table' => $table
277 } else {
278 Core::fatalError(__('Bad parameters!'));
281 // Merge SQL Query aliases with Export aliases from
282 // export page, Export page aliases are given more
283 // preference over SQL Query aliases.
284 $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
285 $aliases = array();
286 if ((!empty($parser->statements[0]))
287 && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
289 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
291 if (!empty($_REQUEST['aliases'])) {
292 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
293 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
297 * Increase time limit for script execution and initializes some variables
299 Util::setTimeLimit();
300 if (! empty($cfg['MemoryLimit'])) {
301 @ini_set('memory_limit', $cfg['MemoryLimit']);
303 register_shutdown_function('PMA_shutdownDuringExport');
304 // Start with empty buffer
305 $dump_buffer = '';
306 $dump_buffer_len = 0;
308 // Array of dump_buffers - used in separate file exports
309 $dump_buffer_objects = array();
311 // We send fake headers to avoid browser timeout when buffering
312 $time_start = time();
314 // Defines the default <CR><LF> format.
315 // For SQL always use \n as MySQL wants this on all platforms.
316 if ($what == 'sql') {
317 $crlf = "\n";
318 } else {
319 $crlf = PHP_EOL;
322 $output_kanji_conversion = Encoding::canConvertKanji();
324 // Do we need to convert charset?
325 $output_charset_conversion = $asfile
326 && Encoding::isSupported()
327 && isset($charset) && $charset != 'utf-8';
329 // Use on the fly compression?
330 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
331 && $compression == 'gzip';
332 if ($GLOBALS['onfly_compression']) {
333 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
336 // Generate filename and mime type if needed
337 if ($asfile) {
338 if (empty($remember_template)) {
339 $remember_template = '';
341 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
342 $export_type, $remember_template, $export_plugin, $compression,
343 $filename_template
345 } else {
346 $mime_type = '';
349 // Open file on server if needed
350 if ($save_on_server) {
351 list($save_filename, $message, $file_handle) = PMA_openExportFile(
352 $filename, $quick_export
355 // problem opening export file on server?
356 if (! empty($message)) {
357 PMA_showExportPage($db, $table, $export_type);
359 } else {
361 * Send headers depending on whether the user chose to download a dump file
362 * or not
364 if ($asfile) {
365 // Download
366 // (avoid rewriting data containing HTML with anchors and forms;
367 // this was reported to happen under Plesk)
368 @ini_set('url_rewriter.tags', '');
369 $filename = Sanitize::sanitizeFilename($filename);
371 Core::downloadHeader($filename, $mime_type);
372 } else {
373 // HTML
374 if ($export_type == 'database') {
375 $num_tables = count($tables);
376 if ($num_tables == 0) {
377 $message = PhpMyAdmin\Message::error(
378 __('No tables found in database.')
380 $active_page = 'db_export.php';
381 include 'db_export.php';
382 exit();
385 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
386 $export_type, $db, $table
388 echo $html;
389 unset($html);
390 } // end download
393 // Fake loop just to allow skip of remain of this code by break, I'd really
394 // need exceptions here :-)
395 do {
396 // Re - initialize
397 $dump_buffer = '';
398 $dump_buffer_len = 0;
400 // Add possibly some comments to export
401 if (! $export_plugin->exportHeader()) {
402 break;
405 // Will we need relation & co. setup?
406 $do_relation = isset($GLOBALS[$what . '_relation']);
407 $do_comments = isset($GLOBALS[$what . '_include_comments'])
408 || isset($GLOBALS[$what . '_comments']);
409 $do_mime = isset($GLOBALS[$what . '_mime']);
410 if ($do_relation || $do_comments || $do_mime) {
411 $cfgRelation = PMA_getRelationsParam();
414 // Include dates in export?
415 $do_dates = isset($GLOBALS[$what . '_dates']);
417 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
420 * Builds the dump
422 if ($export_type == 'server') {
423 if (! isset($db_select)) {
424 $db_select = '';
426 PMA_exportServer(
427 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
428 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
429 $aliases, $separate_files
431 } elseif ($export_type == 'database') {
432 if (!isset($table_structure) || !is_array($table_structure)) {
433 $table_structure = array();
435 if (!isset($table_data) || !is_array($table_data)) {
436 $table_data = array();
438 if (!empty($_REQUEST['structure_or_data_forced'])) {
439 $table_structure = $tables;
440 $table_data = $tables;
442 if (isset($lock_tables)) {
443 PMA_lockTables($db, $tables, "READ");
444 try {
445 PMA_exportDatabase(
446 $db, $tables, $whatStrucOrData, $table_structure,
447 $table_data, $export_plugin, $crlf, $err_url, $export_type,
448 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
449 $separate_files
451 } finally {
452 PMA_unlockTables();
454 } else {
455 PMA_exportDatabase(
456 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
457 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
458 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
461 } else {
462 // We export just one table
463 // $allrows comes from the form when "Dump all rows" has been selected
464 if (! isset($allrows)) {
465 $allrows = '';
467 if (! isset($limit_to)) {
468 $limit_to = 0;
470 if (! isset($limit_from)) {
471 $limit_from = 0;
473 if (isset($lock_tables)) {
474 try {
475 PMA_lockTables($db, array($table), "READ");
476 PMA_exportTable(
477 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
478 $err_url, $export_type, $do_relation, $do_comments,
479 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
480 $sql_query, $aliases
482 } finally {
483 PMA_unlockTables();
485 } else {
486 PMA_exportTable(
487 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
488 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
489 $allrows, $limit_to, $limit_from, $sql_query, $aliases
493 if (! $export_plugin->exportFooter()) {
494 break;
497 } while (false);
498 // End of fake loop
500 if ($save_on_server && ! empty($message)) {
501 PMA_showExportPage($db, $table, $export_type);
505 * Send the dump as a file...
507 if (empty($asfile)) {
508 echo PMA_getHtmlForDisplayedExportFooter($back_button);
509 return;
510 } // end if
512 // Convert the charset if required.
513 if ($output_charset_conversion) {
514 $dump_buffer = Encoding::convertString(
515 'utf-8',
516 $GLOBALS['charset'],
517 $dump_buffer
521 // Compression needed?
522 if ($compression) {
523 if (! empty($separate_files)) {
524 $dump_buffer = PMA_compressExport(
525 $dump_buffer_objects, $compression, $filename
527 } else {
528 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
533 /* If we saved on server, we have to close file now */
534 if ($save_on_server) {
535 $message = PMA_closeExportFile(
536 $file_handle, $dump_buffer, $save_filename
538 PMA_showExportPage($db, $table, $export_type);
539 } else {
540 echo $dump_buffer;