Translated using Weblate (Hungarian)
[phpmyadmin.git] / export.php
blob76bf32461a5bd9cf01e48410dc6a6fd3e2c500fb
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\Sanitize;
13 /**
14 * Get the variables sent or posted to this script and a core script
16 if (!defined('TESTSUITE')) {
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
46 $post_params = array(
47 'db',
48 'table',
49 'what',
50 'single_table',
51 'export_type',
52 'export_method',
53 'quick_or_custom',
54 'db_select',
55 'table_select',
56 'table_structure',
57 'table_data',
58 'limit_to',
59 'limit_from',
60 'allrows',
61 'lock_tables',
62 'output_format',
63 'filename_template',
64 'maxsize',
65 'remember_template',
66 'charset',
67 'compression',
68 'as_separate_files',
69 'knjenc',
70 'xkana',
71 'htmlword_structure_or_data',
72 'htmlword_null',
73 'htmlword_columns',
74 'mediawiki_headers',
75 'mediawiki_structure_or_data',
76 'mediawiki_caption',
77 'pdf_structure_or_data',
78 'odt_structure_or_data',
79 'odt_relation',
80 'odt_comments',
81 'odt_mime',
82 'odt_columns',
83 'odt_null',
84 'codegen_structure_or_data',
85 'codegen_format',
86 'excel_null',
87 'excel_removeCRLF',
88 'excel_columns',
89 'excel_edition',
90 'excel_structure_or_data',
91 'yaml_structure_or_data',
92 'ods_null',
93 'ods_structure_or_data',
94 'ods_columns',
95 'json_structure_or_data',
96 'json_pretty_print',
97 'xml_structure_or_data',
98 'xml_export_events',
99 'xml_export_functions',
100 'xml_export_procedures',
101 'xml_export_tables',
102 'xml_export_triggers',
103 'xml_export_views',
104 'xml_export_contents',
105 'texytext_structure_or_data',
106 'texytext_columns',
107 'texytext_null',
108 'phparray_structure_or_data',
109 'sql_include_comments',
110 'sql_header_comment',
111 'sql_dates',
112 'sql_relation',
113 'sql_mime',
114 'sql_use_transaction',
115 'sql_disable_fk',
116 'sql_compatibility',
117 'sql_structure_or_data',
118 'sql_create_database',
119 'sql_drop_table',
120 'sql_procedure_function',
121 'sql_create_table',
122 'sql_create_view',
123 'sql_create_trigger',
124 'sql_if_not_exists',
125 'sql_auto_increment',
126 'sql_backquotes',
127 'sql_truncate',
128 'sql_delayed',
129 'sql_ignore',
130 'sql_type',
131 'sql_insert_syntax',
132 'sql_max_query_size',
133 'sql_hex_for_binary',
134 'sql_utc_time',
135 'sql_drop_database',
136 'sql_views_as_tables',
137 'sql_metadata',
138 'csv_separator',
139 'csv_enclosed',
140 'csv_escaped',
141 'csv_terminated',
142 'csv_null',
143 'csv_removeCRLF',
144 'csv_columns',
145 'csv_structure_or_data',
146 // csv_replace should have been here but we use it directly from $_POST
147 'latex_caption',
148 'latex_structure_or_data',
149 'latex_structure_caption',
150 'latex_structure_continued_caption',
151 'latex_structure_label',
152 'latex_relation',
153 'latex_comments',
154 'latex_mime',
155 'latex_columns',
156 'latex_data_caption',
157 'latex_data_continued_caption',
158 'latex_data_label',
159 'latex_null',
160 'aliases'
163 foreach ($post_params as $one_post_param) {
164 if (isset($_POST[$one_post_param])) {
165 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
169 $table = $GLOBALS['table'];
171 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
173 // sanitize this parameter which will be used below in a file inclusion
174 $what = PMA_securePath($_POST['what']);
176 // export class instance, not array of properties, as before
177 /* @var $export_plugin ExportPlugin */
178 $export_plugin = PMA_getPlugin(
179 "export",
180 $what,
181 'libraries/plugins/export/',
182 array(
183 'export_type' => $export_type,
184 'single_table' => isset($single_table)
188 // Backward compatibility
189 $type = $what;
191 // Check export type
192 if (empty($export_plugin)) {
193 PMA_fatalError(__('Bad type!'));
197 * valid compression methods
199 $compression_methods = array(
200 'zip',
201 'gzip'
205 * init and variable checking
207 $compression = false;
208 $onserver = false;
209 $save_on_server = false;
210 $buffer_needed = false;
211 $back_button = '';
212 $save_filename = '';
213 $file_handle = '';
214 $err_url = '';
215 $filename = '';
216 $separate_files = '';
218 // Is it a quick or custom export?
219 if (isset($_REQUEST['quick_or_custom'])
220 && $_REQUEST['quick_or_custom'] == 'quick'
222 $quick_export = true;
223 } else {
224 $quick_export = false;
227 if ($_REQUEST['output_format'] == 'astext') {
228 $asfile = false;
229 } else {
230 $asfile = true;
231 if (isset($_REQUEST['as_separate_files'])
232 && ! empty($_REQUEST['as_separate_files'])
234 if (isset($_REQUEST['compression'])
235 && ! empty($_REQUEST['compression'])
236 && $_REQUEST['compression'] == 'zip'
238 $separate_files = $_REQUEST['as_separate_files'];
241 if (in_array($_REQUEST['compression'], $compression_methods)) {
242 $compression = $_REQUEST['compression'];
243 $buffer_needed = true;
245 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
246 || (! $quick_export && ! empty($_REQUEST['onserver']))
248 if ($quick_export) {
249 $onserver = $_REQUEST['quick_export_onserver'];
250 } else {
251 $onserver = $_REQUEST['onserver'];
253 // Will we save dump on server?
254 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
258 // Generate error url and check for needed variables
259 if ($export_type == 'server') {
260 $err_url = 'server_export.php' . URL::getCommon();
261 } elseif ($export_type == 'database'
262 && mb_strlen($db)
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' && mb_strlen($db)
272 && mb_strlen($table)
274 $err_url = 'tbl_export.php' . URL::getCommon(
275 array(
276 'db' => $db, 'table' => $table
279 } else {
280 PMA_fatalError(__('Bad parameters!'));
283 // Merge SQL Query aliases with Export aliases from
284 // export page, Export page aliases are given more
285 // preference over SQL Query aliases.
286 $parser = new SqlParser\Parser($sql_query);
287 $aliases = array();
288 if ((!empty($parser->statements[0]))
289 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
291 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
293 if (!empty($_REQUEST['aliases'])) {
294 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
295 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
299 * Increase time limit for script execution and initializes some variables
301 @set_time_limit($cfg['ExecTimeLimit']);
302 if (! empty($cfg['MemoryLimit'])) {
303 @ini_set('memory_limit', $cfg['MemoryLimit']);
305 register_shutdown_function('PMA_shutdownDuringExport');
306 // Start with empty buffer
307 $dump_buffer = '';
308 $dump_buffer_len = 0;
310 // Array of dump_buffers - used in separate file exports
311 $dump_buffer_objects = array();
313 // We send fake headers to avoid browser timeout when buffering
314 $time_start = time();
316 // Defines the default <CR><LF> format.
317 // For SQL always use \n as MySQL wants this on all platforms.
318 if ($what == 'sql') {
319 $crlf = "\n";
320 } else {
321 $crlf = PMA\libraries\Util::whichCrlf();
324 $output_kanji_conversion = Encoding::canConvertKanji() && $type != 'xls';
326 // Do we need to convert charset?
327 $output_charset_conversion = $asfile
328 && Encoding::isSupported()
329 && isset($charset) && $charset != 'utf-8'
330 && $type != 'xls';
332 // Use on the fly compression?
333 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
334 && $compression == 'gzip';
335 if ($GLOBALS['onfly_compression']) {
336 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
339 // Generate filename and mime type if needed
340 if ($asfile) {
341 if (empty($remember_template)) {
342 $remember_template = '';
344 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
345 $export_type, $remember_template, $export_plugin, $compression,
346 $filename_template
348 } else {
349 $mime_type = '';
352 // Open file on server if needed
353 if ($save_on_server) {
354 list($save_filename, $message, $file_handle) = PMA_openExportFile(
355 $filename, $quick_export
358 // problem opening export file on server?
359 if (! empty($message)) {
360 PMA_showExportPage($db, $table, $export_type);
362 } else {
364 * Send headers depending on whether the user chose to download a dump file
365 * or not
367 if ($asfile) {
368 // Download
369 // (avoid rewriting data containing HTML with anchors and forms;
370 // this was reported to happen under Plesk)
371 @ini_set('url_rewriter.tags', '');
372 $filename = Sanitize::sanitizeFilename($filename);
374 PMA_downloadHeader($filename, $mime_type);
375 } else {
376 // HTML
377 if ($export_type == 'database') {
378 $num_tables = count($tables);
379 if ($num_tables == 0) {
380 $message = PMA\libraries\Message::error(
381 __('No tables found in database.')
383 $active_page = 'db_export.php';
384 include 'db_export.php';
385 exit();
388 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
389 $export_type, $db, $table
391 echo $html;
392 unset($html);
393 } // end download
396 // Fake loop just to allow skip of remain of this code by break, I'd really
397 // need exceptions here :-)
398 do {
399 // Re - initialize
400 $dump_buffer = '';
401 $dump_buffer_len = 0;
403 // Add possibly some comments to export
404 if (! $export_plugin->exportHeader()) {
405 break;
408 // Will we need relation & co. setup?
409 $do_relation = isset($GLOBALS[$what . '_relation']);
410 $do_comments = isset($GLOBALS[$what . '_include_comments'])
411 || isset($GLOBALS[$what . '_comments']);
412 $do_mime = isset($GLOBALS[$what . '_mime']);
413 if ($do_relation || $do_comments || $do_mime) {
414 $cfgRelation = PMA_getRelationsParam();
416 if ($do_mime) {
417 include_once 'libraries/transformations.lib.php';
420 // Include dates in export?
421 $do_dates = isset($GLOBALS[$what . '_dates']);
423 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
426 * Builds the dump
428 if ($export_type == 'server') {
429 if (! isset($db_select)) {
430 $db_select = '';
432 PMA_exportServer(
433 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
434 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
435 $aliases, $separate_files
437 } elseif ($export_type == 'database') {
438 if (!isset($table_structure) || !is_array($table_structure)) {
439 $table_structure = array();
441 if (!isset($table_data) || !is_array($table_data)) {
442 $table_data = array();
444 if (!empty($_REQUEST['structure_or_data_forced'])) {
445 $table_structure = $tables;
446 $table_data = $tables;
448 if (isset($lock_tables)) {
449 PMA_lockTables($db, $tables, "READ");
450 try {
451 PMA_exportDatabase(
452 $db, $tables, $whatStrucOrData, $table_structure,
453 $table_data, $export_plugin, $crlf, $err_url, $export_type,
454 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
455 $separate_files
457 } finally {
458 PMA_unlockTables();
460 } else {
461 PMA_exportDatabase(
462 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
463 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
464 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
467 } else {
468 // We export just one table
469 // $allrows comes from the form when "Dump all rows" has been selected
470 if (! isset($allrows)) {
471 $allrows = '';
473 if (! isset($limit_to)) {
474 $limit_to = 0;
476 if (! isset($limit_from)) {
477 $limit_from = 0;
479 if (isset($lock_tables)) {
480 try {
481 PMA_lockTables($db, array($table), "READ");
482 PMA_exportTable(
483 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
484 $err_url, $export_type, $do_relation, $do_comments,
485 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
486 $sql_query, $aliases
488 } finally {
489 PMA_unlockTables();
491 } else {
492 PMA_exportTable(
493 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
494 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
495 $allrows, $limit_to, $limit_from, $sql_query, $aliases
499 if (! $export_plugin->exportFooter()) {
500 break;
503 } while (false);
504 // End of fake loop
506 if ($save_on_server && ! empty($message)) {
507 PMA_showExportPage($db, $table, $export_type);
511 * Send the dump as a file...
513 if (empty($asfile)) {
514 echo PMA_getHtmlForDisplayedExportFooter($back_button);
515 return;
516 } // end if
518 // Convert the charset if required.
519 if ($output_charset_conversion) {
520 $dump_buffer = Encoding::convertString(
521 'utf-8',
522 $GLOBALS['charset'],
523 $dump_buffer
527 // Compression needed?
528 if ($compression) {
529 if (! empty($separate_files)) {
530 $dump_buffer = PMA_compressExport(
531 $dump_buffer_objects, $compression, $filename
533 } else {
534 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
539 /* If we saved on server, we have to close file now */
540 if ($save_on_server) {
541 $message = PMA_closeExportFile(
542 $file_handle, $dump_buffer, $save_filename
544 PMA_showExportPage($db, $table, $export_type);
545 } else {
546 echo $dump_buffer;