Translated using Weblate (Ukrainian)
[phpmyadmin.git] / export.php
blob7124602fb8d69195a342e08172c4f010e0b252d5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\Encoding;
9 use PMA\libraries\plugins\ExportPlugin;
10 use PMA\libraries\URL;
11 use PMA\libraries\Util;
12 use PMA\libraries\Sanitize;
14 /**
15 * Get the variables sent or posted to this script and a core script
17 /**
18 * If we are sending the export file (as opposed to just displaying it
19 * as text), we have to bypass the usual PMA\libraries\Response mechanism
21 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
22 define('PMA_BYPASS_GET_INSTANCE', 1);
24 include_once 'libraries/common.inc.php';
25 include_once 'libraries/plugin_interface.lib.php';
26 include_once 'libraries/export.lib.php';
28 //check if it's the GET request to check export time out
29 if (isset($_GET['check_time_out'])) {
30 if (isset($_SESSION['pma_export_error'])) {
31 $err = $_SESSION['pma_export_error'];
32 unset($_SESSION['pma_export_error']);
33 echo "timeout";
34 } else {
35 echo "success";
37 exit;
39 /**
40 * Sets globals from $_POST
42 * - Please keep the parameters in order of their appearance in the form
43 * - Some of these parameters are not used, as the code below directly
44 * verifies from the superglobal $_POST or $_REQUEST
45 * TODO: this should be removed to avoid passing user input to GLOBALS
46 * without checking
48 $post_params = array(
49 'db',
50 'table',
51 'what',
52 'single_table',
53 'export_type',
54 'export_method',
55 'quick_or_custom',
56 'db_select',
57 'table_select',
58 'table_structure',
59 'table_data',
60 'limit_to',
61 'limit_from',
62 'allrows',
63 'lock_tables',
64 'output_format',
65 'filename_template',
66 'maxsize',
67 'remember_template',
68 'charset',
69 'compression',
70 'as_separate_files',
71 'knjenc',
72 'xkana',
73 'htmlword_structure_or_data',
74 'htmlword_null',
75 'htmlword_columns',
76 'mediawiki_headers',
77 'mediawiki_structure_or_data',
78 'mediawiki_caption',
79 'pdf_structure_or_data',
80 'odt_structure_or_data',
81 'odt_relation',
82 'odt_comments',
83 'odt_mime',
84 'odt_columns',
85 'odt_null',
86 'codegen_structure_or_data',
87 'codegen_format',
88 'excel_null',
89 'excel_removeCRLF',
90 'excel_columns',
91 'excel_edition',
92 'excel_structure_or_data',
93 'yaml_structure_or_data',
94 'ods_null',
95 'ods_structure_or_data',
96 'ods_columns',
97 'json_structure_or_data',
98 'json_pretty_print',
99 'xml_structure_or_data',
100 'xml_export_events',
101 'xml_export_functions',
102 'xml_export_procedures',
103 'xml_export_tables',
104 'xml_export_triggers',
105 'xml_export_views',
106 'xml_export_contents',
107 'texytext_structure_or_data',
108 'texytext_columns',
109 'texytext_null',
110 'phparray_structure_or_data',
111 'sql_include_comments',
112 'sql_header_comment',
113 'sql_dates',
114 'sql_relation',
115 'sql_mime',
116 'sql_use_transaction',
117 'sql_disable_fk',
118 'sql_compatibility',
119 'sql_structure_or_data',
120 'sql_create_database',
121 'sql_drop_table',
122 'sql_procedure_function',
123 'sql_create_table',
124 'sql_create_view',
125 'sql_create_trigger',
126 'sql_if_not_exists',
127 'sql_auto_increment',
128 'sql_backquotes',
129 'sql_truncate',
130 'sql_delayed',
131 'sql_ignore',
132 'sql_type',
133 'sql_insert_syntax',
134 'sql_max_query_size',
135 'sql_hex_for_binary',
136 'sql_utc_time',
137 'sql_drop_database',
138 'sql_views_as_tables',
139 'sql_metadata',
140 'csv_separator',
141 'csv_enclosed',
142 'csv_escaped',
143 'csv_terminated',
144 'csv_null',
145 'csv_removeCRLF',
146 'csv_columns',
147 'csv_structure_or_data',
148 // csv_replace should have been here but we use it directly from $_POST
149 'latex_caption',
150 'latex_structure_or_data',
151 'latex_structure_caption',
152 'latex_structure_continued_caption',
153 'latex_structure_label',
154 'latex_relation',
155 'latex_comments',
156 'latex_mime',
157 'latex_columns',
158 'latex_data_caption',
159 'latex_data_continued_caption',
160 'latex_data_label',
161 'latex_null',
162 'aliases'
165 foreach ($post_params as $one_post_param) {
166 if (isset($_POST[$one_post_param])) {
167 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
171 $table = $GLOBALS['table'];
173 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
175 // sanitize this parameter which will be used below in a file inclusion
176 $what = PMA_securePath($_POST['what']);
178 // export class instance, not array of properties, as before
179 /* @var $export_plugin ExportPlugin */
180 $export_plugin = PMA_getPlugin(
181 "export",
182 $what,
183 'libraries/plugins/export/',
184 array(
185 'export_type' => $export_type,
186 'single_table' => isset($single_table)
190 // Check export type
191 if (empty($export_plugin)) {
192 PMA_fatalError(__('Bad type!'));
196 * valid compression methods
198 $compression_methods = array(
199 'zip',
200 'gzip'
204 * init and variable checking
206 $compression = false;
207 $onserver = false;
208 $save_on_server = false;
209 $buffer_needed = false;
210 $back_button = '';
211 $save_filename = '';
212 $file_handle = '';
213 $err_url = '';
214 $filename = '';
215 $separate_files = '';
217 // Is it a quick or custom export?
218 if (isset($_REQUEST['quick_or_custom'])
219 && $_REQUEST['quick_or_custom'] == 'quick'
221 $quick_export = true;
222 } else {
223 $quick_export = false;
226 if ($_REQUEST['output_format'] == 'astext') {
227 $asfile = false;
228 } else {
229 $asfile = true;
230 if (isset($_REQUEST['as_separate_files'])
231 && ! empty($_REQUEST['as_separate_files'])
233 if (isset($_REQUEST['compression'])
234 && ! empty($_REQUEST['compression'])
235 && $_REQUEST['compression'] == 'zip'
237 $separate_files = $_REQUEST['as_separate_files'];
240 if (in_array($_REQUEST['compression'], $compression_methods)) {
241 $compression = $_REQUEST['compression'];
242 $buffer_needed = true;
244 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
245 || (! $quick_export && ! empty($_REQUEST['onserver']))
247 if ($quick_export) {
248 $onserver = $_REQUEST['quick_export_onserver'];
249 } else {
250 $onserver = $_REQUEST['onserver'];
252 // Will we save dump on server?
253 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
257 // Generate error url and check for needed variables
258 if ($export_type == 'server') {
259 $err_url = 'server_export.php' . URL::getCommon();
260 } elseif ($export_type == 'database' && strlen($db) > 0) {
261 $err_url = 'db_export.php' . URL::getCommon(array('db' => $db));
262 // Check if we have something to export
263 if (isset($table_select)) {
264 $tables = $table_select;
265 } else {
266 $tables = array();
268 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
269 $err_url = 'tbl_export.php' . URL::getCommon(
270 array(
271 'db' => $db, 'table' => $table
274 } else {
275 PMA_fatalError(__('Bad parameters!'));
278 // Merge SQL Query aliases with Export aliases from
279 // export page, Export page aliases are given more
280 // preference over SQL Query aliases.
281 $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
282 $aliases = array();
283 if ((!empty($parser->statements[0]))
284 && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
286 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
288 if (!empty($_REQUEST['aliases'])) {
289 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
290 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
294 * Increase time limit for script execution and initializes some variables
296 Util::setTimeLimit();
297 if (! empty($cfg['MemoryLimit'])) {
298 @ini_set('memory_limit', $cfg['MemoryLimit']);
300 register_shutdown_function('PMA_shutdownDuringExport');
301 // Start with empty buffer
302 $dump_buffer = '';
303 $dump_buffer_len = 0;
305 // Array of dump_buffers - used in separate file exports
306 $dump_buffer_objects = array();
308 // We send fake headers to avoid browser timeout when buffering
309 $time_start = time();
311 // Defines the default <CR><LF> format.
312 // For SQL always use \n as MySQL wants this on all platforms.
313 if ($what == 'sql') {
314 $crlf = "\n";
315 } else {
316 $crlf = PHP_EOL;
319 $output_kanji_conversion = Encoding::canConvertKanji();
321 // Do we need to convert charset?
322 $output_charset_conversion = $asfile
323 && Encoding::isSupported()
324 && isset($charset) && $charset != 'utf-8';
326 // Use on the fly compression?
327 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
328 && $compression == 'gzip';
329 if ($GLOBALS['onfly_compression']) {
330 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
333 // Generate filename and mime type if needed
334 if ($asfile) {
335 if (empty($remember_template)) {
336 $remember_template = '';
338 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
339 $export_type, $remember_template, $export_plugin, $compression,
340 $filename_template
342 } else {
343 $mime_type = '';
346 // Open file on server if needed
347 if ($save_on_server) {
348 list($save_filename, $message, $file_handle) = PMA_openExportFile(
349 $filename, $quick_export
352 // problem opening export file on server?
353 if (! empty($message)) {
354 PMA_showExportPage($db, $table, $export_type);
356 } else {
358 * Send headers depending on whether the user chose to download a dump file
359 * or not
361 if ($asfile) {
362 // Download
363 // (avoid rewriting data containing HTML with anchors and forms;
364 // this was reported to happen under Plesk)
365 @ini_set('url_rewriter.tags', '');
366 $filename = Sanitize::sanitizeFilename($filename);
368 PMA_downloadHeader($filename, $mime_type);
369 } else {
370 // HTML
371 if ($export_type == 'database') {
372 $num_tables = count($tables);
373 if ($num_tables == 0) {
374 $message = PMA\libraries\Message::error(
375 __('No tables found in database.')
377 $active_page = 'db_export.php';
378 include 'db_export.php';
379 exit();
382 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
383 $export_type, $db, $table
385 echo $html;
386 unset($html);
387 } // end download
390 // Fake loop just to allow skip of remain of this code by break, I'd really
391 // need exceptions here :-)
392 do {
393 // Re - initialize
394 $dump_buffer = '';
395 $dump_buffer_len = 0;
397 // Add possibly some comments to export
398 if (! $export_plugin->exportHeader()) {
399 break;
402 // Will we need relation & co. setup?
403 $do_relation = isset($GLOBALS[$what . '_relation']);
404 $do_comments = isset($GLOBALS[$what . '_include_comments'])
405 || isset($GLOBALS[$what . '_comments']);
406 $do_mime = isset($GLOBALS[$what . '_mime']);
407 if ($do_relation || $do_comments || $do_mime) {
408 $cfgRelation = PMA_getRelationsParam();
410 if ($do_mime) {
411 include_once 'libraries/transformations.lib.php';
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;