Translated using Weblate (Ukrainian)
[phpmyadmin.git] / export.php
blob206e84a0dbbd098197ae18eac5608b0115787d99
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 if (ob_get_length() > 0 || ob_get_level() > 0) {
289 $hasBuffer = ob_end_clean();
290 } else {
291 $hasBuffer = false;
293 } while ($hasBuffer);
296 $tables = [];
297 // Generate error url and check for needed variables
298 if ($export_type == 'server') {
299 $err_url = 'server_export.php' . Url::getCommon();
300 } elseif ($export_type == 'database' && strlen($db) > 0) {
301 $err_url = 'db_export.php' . Url::getCommon(['db' => $db]);
302 // Check if we have something to export
303 if (isset($table_select)) {
304 $tables = $table_select;
305 } else {
306 $tables = [];
308 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
309 $err_url = 'tbl_export.php' . Url::getCommon(
311 'db' => $db,
312 'table' => $table,
315 } else {
316 Core::fatalError(__('Bad parameters!'));
319 // Merge SQL Query aliases with Export aliases from
320 // export page, Export page aliases are given more
321 // preference over SQL Query aliases.
322 $parser = new Parser($sql_query);
323 $aliases = [];
324 if (! empty($parser->statements[0])
325 && ($parser->statements[0] instanceof SelectStatement)
327 $aliases = Misc::getAliases($parser->statements[0], $db);
329 if (! empty($_POST['aliases'])) {
330 $aliases = $export->mergeAliases($aliases, $_POST['aliases']);
331 $_SESSION['tmpval']['aliases'] = $_POST['aliases'];
335 * Increase time limit for script execution and initializes some variables
337 Util::setTimeLimit();
338 if (! empty($cfg['MemoryLimit'])) {
339 ini_set('memory_limit', $cfg['MemoryLimit']);
341 register_shutdown_function([$export, 'shutdown']);
342 // Start with empty buffer
343 $dump_buffer = '';
344 $dump_buffer_len = 0;
346 // Array of dump_buffers - used in separate file exports
347 $dump_buffer_objects = [];
349 // We send fake headers to avoid browser timeout when buffering
350 $time_start = time();
352 // Defines the default <CR><LF> format.
353 // For SQL always use \n as MySQL wants this on all platforms.
354 if ($what == 'sql') {
355 $crlf = "\n";
356 } else {
357 $crlf = PHP_EOL;
360 $output_kanji_conversion = Encoding::canConvertKanji();
362 // Do we need to convert charset?
363 $output_charset_conversion = $asfile
364 && Encoding::isSupported()
365 && isset($charset) && $charset != 'utf-8';
367 // Use on the fly compression?
368 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
369 && $compression == 'gzip';
370 if ($GLOBALS['onfly_compression']) {
371 $GLOBALS['memory_limit'] = $export->getMemoryLimit();
374 // Generate filename and mime type if needed
375 if ($asfile) {
376 if (empty($remember_template)) {
377 $remember_template = '';
379 list($filename, $mime_type) = $export->getFilenameAndMimetype(
380 $export_type,
381 $remember_template,
382 $export_plugin,
383 $compression,
384 $filename_template
386 } else {
387 $mime_type = '';
390 // Open file on server if needed
391 if ($save_on_server) {
392 list($save_filename, $message, $file_handle) = $export->openFile(
393 $filename,
394 $quick_export
397 // problem opening export file on server?
398 if (! empty($message)) {
399 $export->showPage($db, $table, $export_type);
401 } else {
403 * Send headers depending on whether the user chose to download a dump file
404 * or not
406 if ($asfile) {
407 // Download
408 // (avoid rewriting data containing HTML with anchors and forms;
409 // this was reported to happen under Plesk)
410 ini_set('url_rewriter.tags', '');
411 $filename = Sanitize::sanitizeFilename($filename);
413 Core::downloadHeader($filename, $mime_type);
414 } else {
415 // HTML
416 if ($export_type == 'database') {
417 $num_tables = count($tables);
418 if ($num_tables === 0) {
419 $message = PhpMyAdmin\Message::error(
420 __('No tables found in database.')
422 $active_page = 'db_export.php';
423 include ROOT_PATH . 'db_export.php';
424 exit;
427 list($html, $back_button, $refreshButton) = $export->getHtmlForDisplayedExportHeader(
428 $export_type,
429 $db,
430 $table
432 echo $html;
433 unset($html);
434 } // end download
437 /** @var Relation $relation */
438 $relation = $containerBuilder->get('relation');
440 // Fake loop just to allow skip of remain of this code by break, I'd really
441 // need exceptions here :-)
442 do {
443 // Re - initialize
444 $dump_buffer = '';
445 $dump_buffer_len = 0;
447 // Add possibly some comments to export
448 if (! $export_plugin->exportHeader()) {
449 break;
452 // Will we need relation & co. setup?
453 $do_relation = isset($GLOBALS[$what . '_relation']);
454 $do_comments = isset($GLOBALS[$what . '_include_comments'])
455 || isset($GLOBALS[$what . '_comments']);
456 $do_mime = isset($GLOBALS[$what . '_mime']);
457 if ($do_relation || $do_comments || $do_mime) {
458 $cfgRelation = $relation->getRelationsParam();
461 // Include dates in export?
462 $do_dates = isset($GLOBALS[$what . '_dates']);
464 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
467 * Builds the dump
469 if ($export_type == 'server') {
470 if (! isset($db_select)) {
471 $db_select = '';
473 $export->exportServer(
474 $db_select,
475 $whatStrucOrData,
476 $export_plugin,
477 $crlf,
478 $err_url,
479 $export_type,
480 $do_relation,
481 $do_comments,
482 $do_mime,
483 $do_dates,
484 $aliases,
485 $separate_files
487 } elseif ($export_type == 'database') {
488 if (! isset($table_structure) || ! is_array($table_structure)) {
489 $table_structure = [];
491 if (! isset($table_data) || ! is_array($table_data)) {
492 $table_data = [];
494 if (! empty($_POST['structure_or_data_forced'])) {
495 $table_structure = $tables;
496 $table_data = $tables;
498 if (isset($lock_tables)) {
499 $export->lockTables($db, $tables, "READ");
500 try {
501 $export->exportDatabase(
502 $db,
503 $tables,
504 $whatStrucOrData,
505 $table_structure,
506 $table_data,
507 $export_plugin,
508 $crlf,
509 $err_url,
510 $export_type,
511 $do_relation,
512 $do_comments,
513 $do_mime,
514 $do_dates,
515 $aliases,
516 $separate_files
518 } finally {
519 $export->unlockTables();
521 } else {
522 $export->exportDatabase(
523 $db,
524 $tables,
525 $whatStrucOrData,
526 $table_structure,
527 $table_data,
528 $export_plugin,
529 $crlf,
530 $err_url,
531 $export_type,
532 $do_relation,
533 $do_comments,
534 $do_mime,
535 $do_dates,
536 $aliases,
537 $separate_files
540 } else {
541 // We export just one table
542 // $allrows comes from the form when "Dump all rows" has been selected
543 if (! isset($allrows)) {
544 $allrows = '';
546 if (! isset($limit_to)) {
547 $limit_to = '0';
549 if (! isset($limit_from)) {
550 $limit_from = '0';
552 if (isset($lock_tables)) {
553 try {
554 $export->lockTables($db, [$table], "READ");
555 $export->exportTable(
556 $db,
557 $table,
558 $whatStrucOrData,
559 $export_plugin,
560 $crlf,
561 $err_url,
562 $export_type,
563 $do_relation,
564 $do_comments,
565 $do_mime,
566 $do_dates,
567 $allrows,
568 $limit_to,
569 $limit_from,
570 $sql_query,
571 $aliases
573 } finally {
574 $export->unlockTables();
576 } else {
577 $export->exportTable(
578 $db,
579 $table,
580 $whatStrucOrData,
581 $export_plugin,
582 $crlf,
583 $err_url,
584 $export_type,
585 $do_relation,
586 $do_comments,
587 $do_mime,
588 $do_dates,
589 $allrows,
590 $limit_to,
591 $limit_from,
592 $sql_query,
593 $aliases
597 if (! $export_plugin->exportFooter()) {
598 break;
600 } while (false);
601 // End of fake loop
603 if ($save_on_server && ! empty($message)) {
604 $export->showPage($db, $table, $export_type);
608 * Send the dump as a file...
610 if (empty($asfile)) {
611 echo $export->getHtmlForDisplayedExportFooter($back_button, $refreshButton);
612 return;
613 } // end if
615 // Convert the charset if required.
616 if ($output_charset_conversion) {
617 $dump_buffer = Encoding::convertString(
618 'utf-8',
619 $GLOBALS['charset'],
620 $dump_buffer
624 // Compression needed?
625 if ($compression) {
626 if (! empty($separate_files)) {
627 $dump_buffer = $export->compress(
628 $dump_buffer_objects,
629 $compression,
630 $filename
632 } else {
633 $dump_buffer = $export->compress($dump_buffer, $compression, $filename);
637 /* If we saved on server, we have to close file now */
638 if ($save_on_server) {
639 $message = $export->closeFile(
640 $file_handle,
641 $dump_buffer,
642 $save_filename
644 $export->showPage($db, $table, $export_type);
645 } else {
646 echo $dump_buffer;