Translated using Weblate (Estonian)
[phpmyadmin.git] / export.php
blobbf142267c6c5754a8e420ddcd4a3ecb4d595af3e
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\Encoding;
12 use PhpMyAdmin\Export;
13 use PhpMyAdmin\Plugins;
14 use PhpMyAdmin\Plugins\ExportPlugin;
15 use PhpMyAdmin\Relation;
16 use PhpMyAdmin\Sanitize;
17 use PhpMyAdmin\Url;
18 use PhpMyAdmin\Util;
19 use PhpMyAdmin\Response;
21 /**
22 * Get the variables sent or posted to this script and a core script
24 include_once 'libraries/common.inc.php';
25 /**
26 * If we are sending the export file (as opposed to just displaying it
27 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
29 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
30 $response = Response::getInstance();
31 $response->disable();
34 $response = Response::getInstance();
35 $header = $response->getHeader();
36 $scripts = $header->getScripts();
37 $scripts->addFile('export_output.js');
39 $export = new Export();
41 //check if it's the GET request to check export time out
42 if (isset($_GET['check_time_out'])) {
43 if (isset($_SESSION['pma_export_error'])) {
44 $err = $_SESSION['pma_export_error'];
45 unset($_SESSION['pma_export_error']);
46 echo "timeout";
47 } else {
48 echo "success";
50 exit;
52 /**
53 * Sets globals from $_POST
55 * - Please keep the parameters in order of their appearance in the form
56 * - Some of these parameters are not used, as the code below directly
57 * verifies from the superglobal $_POST or $_REQUEST
58 * TODO: this should be removed to avoid passing user input to GLOBALS
59 * without checking
61 $post_params = [
62 'db',
63 'table',
64 'what',
65 'single_table',
66 'export_type',
67 'export_method',
68 'quick_or_custom',
69 'db_select',
70 'table_select',
71 'table_structure',
72 'table_data',
73 'limit_to',
74 'limit_from',
75 'allrows',
76 'lock_tables',
77 'output_format',
78 'filename_template',
79 'maxsize',
80 'remember_template',
81 'charset',
82 'compression',
83 'as_separate_files',
84 'knjenc',
85 'xkana',
86 'htmlword_structure_or_data',
87 'htmlword_null',
88 'htmlword_columns',
89 'mediawiki_headers',
90 'mediawiki_structure_or_data',
91 'mediawiki_caption',
92 'pdf_structure_or_data',
93 'odt_structure_or_data',
94 'odt_relation',
95 'odt_comments',
96 'odt_mime',
97 'odt_columns',
98 'odt_null',
99 'codegen_structure_or_data',
100 'codegen_format',
101 'excel_null',
102 'excel_removeCRLF',
103 'excel_columns',
104 'excel_edition',
105 'excel_structure_or_data',
106 'yaml_structure_or_data',
107 'ods_null',
108 'ods_structure_or_data',
109 'ods_columns',
110 'json_structure_or_data',
111 'json_pretty_print',
112 'json_unicode',
113 'xml_structure_or_data',
114 'xml_export_events',
115 'xml_export_functions',
116 'xml_export_procedures',
117 'xml_export_tables',
118 'xml_export_triggers',
119 'xml_export_views',
120 'xml_export_contents',
121 'texytext_structure_or_data',
122 'texytext_columns',
123 'texytext_null',
124 'phparray_structure_or_data',
125 'sql_include_comments',
126 'sql_header_comment',
127 'sql_dates',
128 'sql_relation',
129 'sql_mime',
130 'sql_use_transaction',
131 'sql_disable_fk',
132 'sql_compatibility',
133 'sql_structure_or_data',
134 'sql_create_database',
135 'sql_drop_table',
136 'sql_procedure_function',
137 'sql_create_table',
138 'sql_create_view',
139 'sql_create_trigger',
140 'sql_if_not_exists',
141 'sql_auto_increment',
142 'sql_backquotes',
143 'sql_truncate',
144 'sql_delayed',
145 'sql_ignore',
146 'sql_type',
147 'sql_insert_syntax',
148 'sql_max_query_size',
149 'sql_hex_for_binary',
150 'sql_utc_time',
151 'sql_drop_database',
152 'sql_views_as_tables',
153 'sql_metadata',
154 'csv_separator',
155 'csv_enclosed',
156 'csv_escaped',
157 'csv_terminated',
158 'csv_null',
159 'csv_removeCRLF',
160 'csv_columns',
161 'csv_structure_or_data',
162 // csv_replace should have been here but we use it directly from $_POST
163 'latex_caption',
164 'latex_structure_or_data',
165 'latex_structure_caption',
166 'latex_structure_continued_caption',
167 'latex_structure_label',
168 'latex_relation',
169 'latex_comments',
170 'latex_mime',
171 'latex_columns',
172 'latex_data_caption',
173 'latex_data_continued_caption',
174 'latex_data_label',
175 'latex_null',
176 'aliases'
179 foreach ($post_params as $one_post_param) {
180 if (isset($_POST[$one_post_param])) {
181 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
185 $table = $GLOBALS['table'];
187 PhpMyAdmin\Util::checkParameters(['what', 'export_type']);
189 // sanitize this parameter which will be used below in a file inclusion
190 $what = Core::securePath($_POST['what']);
192 // export class instance, not array of properties, as before
193 /* @var $export_plugin ExportPlugin */
194 $export_plugin = Plugins::getPlugin(
195 "export",
196 $what,
197 'libraries/classes/Plugins/Export/',
199 'export_type' => $export_type,
200 'single_table' => isset($single_table)
204 // Check export type
205 if (empty($export_plugin)) {
206 Core::fatalError(__('Bad type!'));
210 * valid compression methods
212 $compression_methods = [
213 'zip',
214 'gzip'
218 * init and variable checking
220 $compression = '';
221 $onserver = false;
222 $save_on_server = false;
223 $buffer_needed = false;
224 $back_button = '';
225 $refreshButton = '';
226 $save_filename = '';
227 $file_handle = '';
228 $err_url = '';
229 $filename = '';
230 $separate_files = '';
232 // Is it a quick or custom export?
233 if (isset($_REQUEST['quick_or_custom'])
234 && $_REQUEST['quick_or_custom'] == 'quick'
236 $quick_export = true;
237 } else {
238 $quick_export = false;
241 if ($_REQUEST['output_format'] == 'astext') {
242 $asfile = false;
243 } else {
244 $asfile = true;
245 if (isset($_REQUEST['as_separate_files'])
246 && ! empty($_REQUEST['as_separate_files'])
248 if (isset($_REQUEST['compression'])
249 && ! empty($_REQUEST['compression'])
250 && $_REQUEST['compression'] == 'zip'
252 $separate_files = $_REQUEST['as_separate_files'];
255 if (in_array($_REQUEST['compression'], $compression_methods)) {
256 $compression = $_REQUEST['compression'];
257 $buffer_needed = true;
259 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
260 || (! $quick_export && ! empty($_REQUEST['onserver']))
262 if ($quick_export) {
263 $onserver = $_REQUEST['quick_export_onserver'];
264 } else {
265 $onserver = $_REQUEST['onserver'];
267 // Will we save dump on server?
268 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
271 $tables = [];
272 // Generate error url and check for needed variables
273 if ($export_type == 'server') {
274 $err_url = 'server_export.php' . Url::getCommon();
275 } elseif ($export_type == 'database' && strlen($db) > 0) {
276 $err_url = 'db_export.php' . Url::getCommon(['db' => $db]);
277 // Check if we have something to export
278 if (isset($table_select)) {
279 $tables = $table_select;
280 } else {
281 $tables = [];
283 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
284 $err_url = 'tbl_export.php' . Url::getCommon(
286 'db' => $db, 'table' => $table
289 } else {
290 Core::fatalError(__('Bad parameters!'));
293 // Merge SQL Query aliases with Export aliases from
294 // export page, Export page aliases are given more
295 // preference over SQL Query aliases.
296 $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
297 $aliases = [];
298 if ((!empty($parser->statements[0]))
299 && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
301 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
303 if (!empty($_REQUEST['aliases'])) {
304 $aliases = $export->mergeAliases($aliases, $_REQUEST['aliases']);
305 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
309 * Increase time limit for script execution and initializes some variables
311 Util::setTimeLimit();
312 if (! empty($cfg['MemoryLimit'])) {
313 ini_set('memory_limit', $cfg['MemoryLimit']);
315 register_shutdown_function([$export, 'shutdown']);
316 // Start with empty buffer
317 $dump_buffer = '';
318 $dump_buffer_len = 0;
320 // Array of dump_buffers - used in separate file exports
321 $dump_buffer_objects = [];
323 // We send fake headers to avoid browser timeout when buffering
324 $time_start = time();
326 // Defines the default <CR><LF> format.
327 // For SQL always use \n as MySQL wants this on all platforms.
328 if ($what == 'sql') {
329 $crlf = "\n";
330 } else {
331 $crlf = PHP_EOL;
334 $output_kanji_conversion = Encoding::canConvertKanji();
336 // Do we need to convert charset?
337 $output_charset_conversion = $asfile
338 && Encoding::isSupported()
339 && isset($charset) && $charset != 'utf-8';
341 // Use on the fly compression?
342 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
343 && $compression == 'gzip';
344 if ($GLOBALS['onfly_compression']) {
345 $GLOBALS['memory_limit'] = $export->getMemoryLimit();
348 // Generate filename and mime type if needed
349 if ($asfile) {
350 if (empty($remember_template)) {
351 $remember_template = '';
353 list($filename, $mime_type) = $export->getFilenameAndMimetype(
354 $export_type,
355 $remember_template,
356 $export_plugin,
357 $compression,
358 $filename_template
360 } else {
361 $mime_type = '';
364 // Open file on server if needed
365 if ($save_on_server) {
366 list($save_filename, $message, $file_handle) = $export->openFile(
367 $filename,
368 $quick_export
371 // problem opening export file on server?
372 if (! empty($message)) {
373 $export->showPage($db, $table, $export_type);
375 } else {
377 * Send headers depending on whether the user chose to download a dump file
378 * or not
380 if ($asfile) {
381 // Download
382 // (avoid rewriting data containing HTML with anchors and forms;
383 // this was reported to happen under Plesk)
384 ini_set('url_rewriter.tags', '');
385 $filename = Sanitize::sanitizeFilename($filename);
387 Core::downloadHeader($filename, $mime_type);
388 } else {
389 // HTML
390 if ($export_type == 'database') {
391 $num_tables = count($tables);
392 if ($num_tables == 0) {
393 $message = PhpMyAdmin\Message::error(
394 __('No tables found in database.')
396 $active_page = 'db_export.php';
397 include 'db_export.php';
398 exit();
401 list($html, $back_button, $refreshButton) = $export->getHtmlForDisplayedExportHeader(
402 $export_type,
403 $db,
404 $table
406 echo $html;
407 unset($html);
408 } // end download
411 $relation = new Relation();
413 // Fake loop just to allow skip of remain of this code by break, I'd really
414 // need exceptions here :-)
415 do {
416 // Re - initialize
417 $dump_buffer = '';
418 $dump_buffer_len = 0;
420 // Add possibly some comments to export
421 if (! $export_plugin->exportHeader()) {
422 break;
425 // Will we need relation & co. setup?
426 $do_relation = isset($GLOBALS[$what . '_relation']);
427 $do_comments = isset($GLOBALS[$what . '_include_comments'])
428 || isset($GLOBALS[$what . '_comments']);
429 $do_mime = isset($GLOBALS[$what . '_mime']);
430 if ($do_relation || $do_comments || $do_mime) {
431 $cfgRelation = $relation->getRelationsParam();
434 // Include dates in export?
435 $do_dates = isset($GLOBALS[$what . '_dates']);
437 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
440 * Builds the dump
442 if ($export_type == 'server') {
443 if (! isset($db_select)) {
444 $db_select = '';
446 $export->exportServer(
447 $db_select,
448 $whatStrucOrData,
449 $export_plugin,
450 $crlf,
451 $err_url,
452 $export_type,
453 $do_relation,
454 $do_comments,
455 $do_mime,
456 $do_dates,
457 $aliases,
458 $separate_files
460 } elseif ($export_type == 'database') {
461 if (!isset($table_structure) || !is_array($table_structure)) {
462 $table_structure = [];
464 if (!isset($table_data) || !is_array($table_data)) {
465 $table_data = [];
467 if (!empty($_REQUEST['structure_or_data_forced'])) {
468 $table_structure = $tables;
469 $table_data = $tables;
471 if (isset($lock_tables)) {
472 $export->lockTables($db, $tables, "READ");
473 try {
474 $export->exportDatabase(
475 $db,
476 $tables,
477 $whatStrucOrData,
478 $table_structure,
479 $table_data,
480 $export_plugin,
481 $crlf,
482 $err_url,
483 $export_type,
484 $do_relation,
485 $do_comments,
486 $do_mime,
487 $do_dates,
488 $aliases,
489 $separate_files
491 } finally {
492 $export->unlockTables();
494 } else {
495 $export->exportDatabase(
496 $db,
497 $tables,
498 $whatStrucOrData,
499 $table_structure,
500 $table_data,
501 $export_plugin,
502 $crlf,
503 $err_url,
504 $export_type,
505 $do_relation,
506 $do_comments,
507 $do_mime,
508 $do_dates,
509 $aliases,
510 $separate_files
513 } else {
514 // We export just one table
515 // $allrows comes from the form when "Dump all rows" has been selected
516 if (! isset($allrows)) {
517 $allrows = '';
519 if (! isset($limit_to)) {
520 $limit_to = '0';
522 if (! isset($limit_from)) {
523 $limit_from = '0';
525 if (isset($lock_tables)) {
526 try {
527 $export->lockTables($db, [$table], "READ");
528 $export->exportTable(
529 $db,
530 $table,
531 $whatStrucOrData,
532 $export_plugin,
533 $crlf,
534 $err_url,
535 $export_type,
536 $do_relation,
537 $do_comments,
538 $do_mime,
539 $do_dates,
540 $allrows,
541 $limit_to,
542 $limit_from,
543 $sql_query,
544 $aliases
546 } finally {
547 $export->unlockTables();
549 } else {
550 $export->exportTable(
551 $db,
552 $table,
553 $whatStrucOrData,
554 $export_plugin,
555 $crlf,
556 $err_url,
557 $export_type,
558 $do_relation,
559 $do_comments,
560 $do_mime,
561 $do_dates,
562 $allrows,
563 $limit_to,
564 $limit_from,
565 $sql_query,
566 $aliases
570 if (! $export_plugin->exportFooter()) {
571 break;
573 } while (false);
574 // End of fake loop
576 if ($save_on_server && ! empty($message)) {
577 $export->showPage($db, $table, $export_type);
581 * Send the dump as a file...
583 if (empty($asfile)) {
584 echo $export->getHtmlForDisplayedExportFooter($back_button, $refreshButton);
585 return;
586 } // end if
588 // Convert the charset if required.
589 if ($output_charset_conversion) {
590 $dump_buffer = Encoding::convertString(
591 'utf-8',
592 $GLOBALS['charset'],
593 $dump_buffer
597 // Compression needed?
598 if ($compression) {
599 if (! empty($separate_files)) {
600 $dump_buffer = $export->compress(
601 $dump_buffer_objects,
602 $compression,
603 $filename
605 } else {
606 $dump_buffer = $export->compress($dump_buffer, $compression, $filename);
610 /* If we saved on server, we have to close file now */
611 if ($save_on_server) {
612 $message = $export->closeFile(
613 $file_handle,
614 $dump_buffer,
615 $save_filename
617 $export->showPage($db, $table, $export_type);
618 } else {
619 echo $dump_buffer;