Translated using Weblate (French)
[phpmyadmin.git] / export.php
blob479804553463d8187c0516b4e6ab90448d377efa
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Encoding;
13 use PhpMyAdmin\Export;
14 use PhpMyAdmin\Plugins;
15 use PhpMyAdmin\Plugins\ExportPlugin;
16 use PhpMyAdmin\Relation;
17 use PhpMyAdmin\Response;
18 use PhpMyAdmin\Sanitize;
19 use PhpMyAdmin\SqlParser\Parser;
20 use PhpMyAdmin\SqlParser\Statements\SelectStatement;
21 use PhpMyAdmin\SqlParser\Utils\Misc;
22 use PhpMyAdmin\Url;
23 use PhpMyAdmin\Util;
25 if (! defined('ROOT_PATH')) {
26 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
29 global $db, $sql_query;
31 include_once ROOT_PATH . 'libraries/common.inc.php';
33 /** @var Response $response */
34 $response = $containerBuilder->get(Response::class);
36 /** @var DatabaseInterface $dbi */
37 $dbi = $containerBuilder->get(DatabaseInterface::class);
39 $header = $response->getHeader();
40 $scripts = $header->getScripts();
41 $scripts->addFile('export_output.js');
43 /** @var Export $export */
44 $export = $containerBuilder->get('export');
46 //check if it's the GET request to check export time out
47 if (isset($_GET['check_time_out'])) {
48 if (isset($_SESSION['pma_export_error'])) {
49 $err = $_SESSION['pma_export_error'];
50 unset($_SESSION['pma_export_error']);
51 echo "timeout";
52 } else {
53 echo "success";
55 exit;
58 /**
59 * Sets globals from $_POST
61 * - Please keep the parameters in order of their appearance in the form
62 * - Some of these parameters are not used, as the code below directly
63 * verifies from the superglobal $_POST or $_REQUEST
64 * TODO: this should be removed to avoid passing user input to GLOBALS
65 * without checking
67 $post_params = [
68 'db',
69 'table',
70 'what',
71 'single_table',
72 'export_type',
73 'export_method',
74 'quick_or_custom',
75 'db_select',
76 'table_select',
77 'table_structure',
78 'table_data',
79 'limit_to',
80 'limit_from',
81 'allrows',
82 'lock_tables',
83 'output_format',
84 'filename_template',
85 'maxsize',
86 'remember_template',
87 'charset',
88 'compression',
89 'as_separate_files',
90 'knjenc',
91 'xkana',
92 'htmlword_structure_or_data',
93 'htmlword_null',
94 'htmlword_columns',
95 'mediawiki_headers',
96 'mediawiki_structure_or_data',
97 'mediawiki_caption',
98 'pdf_structure_or_data',
99 'odt_structure_or_data',
100 'odt_relation',
101 'odt_comments',
102 'odt_mime',
103 'odt_columns',
104 'odt_null',
105 'codegen_structure_or_data',
106 'codegen_format',
107 'excel_null',
108 'excel_removeCRLF',
109 'excel_columns',
110 'excel_edition',
111 'excel_structure_or_data',
112 'yaml_structure_or_data',
113 'ods_null',
114 'ods_structure_or_data',
115 'ods_columns',
116 'json_structure_or_data',
117 'json_pretty_print',
118 'json_unicode',
119 'xml_structure_or_data',
120 'xml_export_events',
121 'xml_export_functions',
122 'xml_export_procedures',
123 'xml_export_tables',
124 'xml_export_triggers',
125 'xml_export_views',
126 'xml_export_contents',
127 'texytext_structure_or_data',
128 'texytext_columns',
129 'texytext_null',
130 'phparray_structure_or_data',
131 'sql_include_comments',
132 'sql_header_comment',
133 'sql_dates',
134 'sql_relation',
135 'sql_mime',
136 'sql_use_transaction',
137 'sql_disable_fk',
138 'sql_compatibility',
139 'sql_structure_or_data',
140 'sql_create_database',
141 'sql_drop_table',
142 'sql_procedure_function',
143 'sql_create_table',
144 'sql_create_view',
145 'sql_create_trigger',
146 'sql_view_current_user',
147 'sql_if_not_exists',
148 'sql_or_replace_view',
149 'sql_auto_increment',
150 'sql_backquotes',
151 'sql_truncate',
152 'sql_delayed',
153 'sql_ignore',
154 'sql_type',
155 'sql_insert_syntax',
156 'sql_max_query_size',
157 'sql_hex_for_binary',
158 'sql_utc_time',
159 'sql_drop_database',
160 'sql_views_as_tables',
161 'sql_metadata',
162 'csv_separator',
163 'csv_enclosed',
164 'csv_escaped',
165 'csv_terminated',
166 'csv_null',
167 'csv_removeCRLF',
168 'csv_columns',
169 'csv_structure_or_data',
170 // csv_replace should have been here but we use it directly from $_POST
171 'latex_caption',
172 'latex_structure_or_data',
173 'latex_structure_caption',
174 'latex_structure_continued_caption',
175 'latex_structure_label',
176 'latex_relation',
177 'latex_comments',
178 'latex_mime',
179 'latex_columns',
180 'latex_data_caption',
181 'latex_data_continued_caption',
182 'latex_data_label',
183 'latex_null',
184 'aliases',
187 foreach ($post_params as $one_post_param) {
188 if (isset($_POST[$one_post_param])) {
189 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
193 $table = $GLOBALS['table'];
195 PhpMyAdmin\Util::checkParameters(['what', 'export_type']);
197 // sanitize this parameter which will be used below in a file inclusion
198 $what = Core::securePath($_POST['what']);
200 // export class instance, not array of properties, as before
201 /** @var ExportPlugin $export_plugin */
202 $export_plugin = Plugins::getPlugin(
203 "export",
204 $what,
205 'libraries/classes/Plugins/Export/',
207 'export_type' => $export_type,
208 'single_table' => isset($single_table),
212 // Check export type
213 if (empty($export_plugin)) {
214 Core::fatalError(__('Bad type!'));
218 * valid compression methods
220 $compression_methods = [
221 'zip',
222 'gzip',
226 * init and variable checking
228 $compression = '';
229 $onserver = false;
230 $save_on_server = false;
231 $buffer_needed = false;
232 $back_button = '';
233 $refreshButton = '';
234 $save_filename = '';
235 $file_handle = '';
236 $err_url = '';
237 $filename = '';
238 $separate_files = '';
240 // Is it a quick or custom export?
241 if (isset($_POST['quick_or_custom'])
242 && $_POST['quick_or_custom'] == 'quick'
244 $quick_export = true;
245 } else {
246 $quick_export = false;
249 if ($_POST['output_format'] == 'astext') {
250 $asfile = false;
251 } else {
252 $asfile = true;
253 if (isset($_POST['as_separate_files'])
254 && ! empty($_POST['as_separate_files'])
256 if (isset($_POST['compression'])
257 && ! empty($_POST['compression'])
258 && $_POST['compression'] == 'zip'
260 $separate_files = $_POST['as_separate_files'];
263 if (in_array($_POST['compression'], $compression_methods)) {
264 $compression = $_POST['compression'];
265 $buffer_needed = true;
267 if (($quick_export && ! empty($_POST['quick_export_onserver']))
268 || (! $quick_export && ! empty($_POST['onserver']))
270 if ($quick_export) {
271 $onserver = $_POST['quick_export_onserver'];
272 } else {
273 $onserver = $_POST['onserver'];
275 // Will we save dump on server?
276 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
281 * If we are sending the export file (as opposed to just displaying it
282 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
284 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit' && ! $save_on_server) {
285 $response->disable();
286 //Disable all active buffers (see: ob_get_status(true) at this point)
287 do {
288 $hasBuffer = @ob_end_clean();
289 } while ($hasBuffer);
292 $tables = [];
293 // Generate error url and check for needed variables
294 if ($export_type == 'server') {
295 $err_url = 'server_export.php' . Url::getCommon();
296 } elseif ($export_type == 'database' && strlen($db) > 0) {
297 $err_url = 'db_export.php' . Url::getCommon(['db' => $db]);
298 // Check if we have something to export
299 if (isset($table_select)) {
300 $tables = $table_select;
301 } else {
302 $tables = [];
304 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
305 $err_url = 'tbl_export.php' . Url::getCommon(
307 'db' => $db,
308 'table' => $table,
311 } else {
312 Core::fatalError(__('Bad parameters!'));
315 // Merge SQL Query aliases with Export aliases from
316 // export page, Export page aliases are given more
317 // preference over SQL Query aliases.
318 $parser = new Parser($sql_query);
319 $aliases = [];
320 if (! empty($parser->statements[0])
321 && ($parser->statements[0] instanceof SelectStatement)
323 $aliases = Misc::getAliases($parser->statements[0], $db);
325 if (! empty($_POST['aliases'])) {
326 $aliases = $export->mergeAliases($aliases, $_POST['aliases']);
327 $_SESSION['tmpval']['aliases'] = $_POST['aliases'];
331 * Increase time limit for script execution and initializes some variables
333 Util::setTimeLimit();
334 if (! empty($cfg['MemoryLimit'])) {
335 ini_set('memory_limit', $cfg['MemoryLimit']);
337 register_shutdown_function([$export, 'shutdown']);
338 // Start with empty buffer
339 $dump_buffer = '';
340 $dump_buffer_len = 0;
342 // Array of dump_buffers - used in separate file exports
343 $dump_buffer_objects = [];
345 // We send fake headers to avoid browser timeout when buffering
346 $time_start = time();
348 // Defines the default <CR><LF> format.
349 // For SQL always use \n as MySQL wants this on all platforms.
350 if ($what == 'sql') {
351 $crlf = "\n";
352 } else {
353 $crlf = PHP_EOL;
356 $output_kanji_conversion = Encoding::canConvertKanji();
358 // Do we need to convert charset?
359 $output_charset_conversion = $asfile
360 && Encoding::isSupported()
361 && isset($charset) && $charset != 'utf-8';
363 // Use on the fly compression?
364 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
365 && $compression == 'gzip';
366 if ($GLOBALS['onfly_compression']) {
367 $GLOBALS['memory_limit'] = $export->getMemoryLimit();
370 // Generate filename and mime type if needed
371 if ($asfile) {
372 if (empty($remember_template)) {
373 $remember_template = '';
375 list($filename, $mime_type) = $export->getFilenameAndMimetype(
376 $export_type,
377 $remember_template,
378 $export_plugin,
379 $compression,
380 $filename_template
382 } else {
383 $mime_type = '';
386 // Open file on server if needed
387 if ($save_on_server) {
388 list($save_filename, $message, $file_handle) = $export->openFile(
389 $filename,
390 $quick_export
393 // problem opening export file on server?
394 if (! empty($message)) {
395 $export->showPage($db, $table, $export_type);
397 } else {
399 * Send headers depending on whether the user chose to download a dump file
400 * or not
402 if ($asfile) {
403 // Download
404 // (avoid rewriting data containing HTML with anchors and forms;
405 // this was reported to happen under Plesk)
406 ini_set('url_rewriter.tags', '');
407 $filename = Sanitize::sanitizeFilename($filename);
409 Core::downloadHeader($filename, $mime_type);
410 } else {
411 // HTML
412 if ($export_type == 'database') {
413 $num_tables = count($tables);
414 if ($num_tables === 0) {
415 $message = PhpMyAdmin\Message::error(
416 __('No tables found in database.')
418 $active_page = 'db_export.php';
419 include ROOT_PATH . 'db_export.php';
420 exit;
423 list($html, $back_button, $refreshButton) = $export->getHtmlForDisplayedExportHeader(
424 $export_type,
425 $db,
426 $table
428 echo $html;
429 unset($html);
430 } // end download
433 /** @var Relation $relation */
434 $relation = $containerBuilder->get('relation');
436 // Fake loop just to allow skip of remain of this code by break, I'd really
437 // need exceptions here :-)
438 do {
439 // Re - initialize
440 $dump_buffer = '';
441 $dump_buffer_len = 0;
443 // Add possibly some comments to export
444 if (! $export_plugin->exportHeader()) {
445 break;
448 // Will we need relation & co. setup?
449 $do_relation = isset($GLOBALS[$what . '_relation']);
450 $do_comments = isset($GLOBALS[$what . '_include_comments'])
451 || isset($GLOBALS[$what . '_comments']);
452 $do_mime = isset($GLOBALS[$what . '_mime']);
453 if ($do_relation || $do_comments || $do_mime) {
454 $cfgRelation = $relation->getRelationsParam();
457 // Include dates in export?
458 $do_dates = isset($GLOBALS[$what . '_dates']);
460 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
463 * Builds the dump
465 if ($export_type == 'server') {
466 if (! isset($db_select)) {
467 $db_select = '';
469 $export->exportServer(
470 $db_select,
471 $whatStrucOrData,
472 $export_plugin,
473 $crlf,
474 $err_url,
475 $export_type,
476 $do_relation,
477 $do_comments,
478 $do_mime,
479 $do_dates,
480 $aliases,
481 $separate_files
483 } elseif ($export_type == 'database') {
484 if (! isset($table_structure) || ! is_array($table_structure)) {
485 $table_structure = [];
487 if (! isset($table_data) || ! is_array($table_data)) {
488 $table_data = [];
490 if (! empty($_POST['structure_or_data_forced'])) {
491 $table_structure = $tables;
492 $table_data = $tables;
494 if (isset($lock_tables)) {
495 $export->lockTables($db, $tables, "READ");
496 try {
497 $export->exportDatabase(
498 $db,
499 $tables,
500 $whatStrucOrData,
501 $table_structure,
502 $table_data,
503 $export_plugin,
504 $crlf,
505 $err_url,
506 $export_type,
507 $do_relation,
508 $do_comments,
509 $do_mime,
510 $do_dates,
511 $aliases,
512 $separate_files
514 } finally {
515 $export->unlockTables();
517 } else {
518 $export->exportDatabase(
519 $db,
520 $tables,
521 $whatStrucOrData,
522 $table_structure,
523 $table_data,
524 $export_plugin,
525 $crlf,
526 $err_url,
527 $export_type,
528 $do_relation,
529 $do_comments,
530 $do_mime,
531 $do_dates,
532 $aliases,
533 $separate_files
536 } else {
537 // We export just one table
538 // $allrows comes from the form when "Dump all rows" has been selected
539 if (! isset($allrows)) {
540 $allrows = '';
542 if (! isset($limit_to)) {
543 $limit_to = '0';
545 if (! isset($limit_from)) {
546 $limit_from = '0';
548 if (isset($lock_tables)) {
549 try {
550 $export->lockTables($db, [$table], "READ");
551 $export->exportTable(
552 $db,
553 $table,
554 $whatStrucOrData,
555 $export_plugin,
556 $crlf,
557 $err_url,
558 $export_type,
559 $do_relation,
560 $do_comments,
561 $do_mime,
562 $do_dates,
563 $allrows,
564 $limit_to,
565 $limit_from,
566 $sql_query,
567 $aliases
569 } finally {
570 $export->unlockTables();
572 } else {
573 $export->exportTable(
574 $db,
575 $table,
576 $whatStrucOrData,
577 $export_plugin,
578 $crlf,
579 $err_url,
580 $export_type,
581 $do_relation,
582 $do_comments,
583 $do_mime,
584 $do_dates,
585 $allrows,
586 $limit_to,
587 $limit_from,
588 $sql_query,
589 $aliases
593 if (! $export_plugin->exportFooter()) {
594 break;
596 } while (false);
597 // End of fake loop
599 if ($save_on_server && ! empty($message)) {
600 $export->showPage($db, $table, $export_type);
604 * Send the dump as a file...
606 if (empty($asfile)) {
607 echo $export->getHtmlForDisplayedExportFooter($back_button, $refreshButton);
608 return;
609 } // end if
611 // Convert the charset if required.
612 if ($output_charset_conversion) {
613 $dump_buffer = Encoding::convertString(
614 'utf-8',
615 $GLOBALS['charset'],
616 $dump_buffer
620 // Compression needed?
621 if ($compression) {
622 if (! empty($separate_files)) {
623 $dump_buffer = $export->compress(
624 $dump_buffer_objects,
625 $compression,
626 $filename
628 } else {
629 $dump_buffer = $export->compress($dump_buffer, $compression, $filename);
633 /* If we saved on server, we have to close file now */
634 if ($save_on_server) {
635 $message = $export->closeFile(
636 $file_handle,
637 $dump_buffer,
638 $save_filename
640 $export->showPage($db, $table, $export_type);
641 } else {
642 echo $dump_buffer;