Translated using Weblate (Slovenian)
[phpmyadmin.git] / export.php
blob912b2f9b4d26b3078176d7cf257ec00aa0a18574
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\plugins\ExportPlugin;
9 use PMA\libraries\URL;
10 use PMA\libraries\Sanitize;
12 /**
13 * Get the variables sent or posted to this script and a core script
15 if (!defined('TESTSUITE')) {
16 /**
17 * If we are sending the export file (as opposed to just displaying it
18 * as text), we have to bypass the usual PMA\libraries\Response mechanism
20 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
21 define('PMA_BYPASS_GET_INSTANCE', 1);
23 include_once 'libraries/common.inc.php';
24 include_once 'libraries/plugin_interface.lib.php';
25 include_once 'libraries/export.lib.php';
27 //check if it's the GET request to check export time out
28 if (isset($_GET['check_time_out'])) {
29 if (isset($_SESSION['pma_export_error'])) {
30 $err = $_SESSION['pma_export_error'];
31 unset($_SESSION['pma_export_error']);
32 echo "timeout";
33 } else {
34 echo "success";
36 exit;
38 /**
39 * Sets globals from $_POST
41 * - Please keep the parameters in order of their appearance in the form
42 * - Some of these parameters are not used, as the code below directly
43 * verifies from the superglobal $_POST or $_REQUEST
45 $post_params = array(
46 'db',
47 'table',
48 'what',
49 'single_table',
50 'export_type',
51 'export_method',
52 'quick_or_custom',
53 'db_select',
54 'table_select',
55 'table_structure',
56 'table_data',
57 'limit_to',
58 'limit_from',
59 'allrows',
60 'lock_tables',
61 'output_format',
62 'filename_template',
63 'maxsize',
64 'remember_template',
65 'charset',
66 'compression',
67 'as_separate_files',
68 'knjenc',
69 'xkana',
70 'htmlword_structure_or_data',
71 'htmlword_null',
72 'htmlword_columns',
73 'mediawiki_headers',
74 'mediawiki_structure_or_data',
75 'mediawiki_caption',
76 'pdf_structure_or_data',
77 'odt_structure_or_data',
78 'odt_relation',
79 'odt_comments',
80 'odt_mime',
81 'odt_columns',
82 'odt_null',
83 'codegen_structure_or_data',
84 'codegen_format',
85 'excel_null',
86 'excel_removeCRLF',
87 'excel_columns',
88 'excel_edition',
89 'excel_structure_or_data',
90 'yaml_structure_or_data',
91 'ods_null',
92 'ods_structure_or_data',
93 'ods_columns',
94 'json_structure_or_data',
95 'json_pretty_print',
96 'xml_structure_or_data',
97 'xml_export_events',
98 'xml_export_functions',
99 'xml_export_procedures',
100 'xml_export_tables',
101 'xml_export_triggers',
102 'xml_export_views',
103 'xml_export_contents',
104 'texytext_structure_or_data',
105 'texytext_columns',
106 'texytext_null',
107 'phparray_structure_or_data',
108 'sql_include_comments',
109 'sql_header_comment',
110 'sql_dates',
111 'sql_relation',
112 'sql_mime',
113 'sql_use_transaction',
114 'sql_disable_fk',
115 'sql_compatibility',
116 'sql_structure_or_data',
117 'sql_create_database',
118 'sql_drop_table',
119 'sql_procedure_function',
120 'sql_create_table',
121 'sql_create_view',
122 'sql_create_trigger',
123 'sql_if_not_exists',
124 'sql_auto_increment',
125 'sql_backquotes',
126 'sql_truncate',
127 'sql_delayed',
128 'sql_ignore',
129 'sql_type',
130 'sql_insert_syntax',
131 'sql_max_query_size',
132 'sql_hex_for_binary',
133 'sql_utc_time',
134 'sql_drop_database',
135 'sql_views_as_tables',
136 'sql_metadata',
137 'csv_separator',
138 'csv_enclosed',
139 'csv_escaped',
140 'csv_terminated',
141 'csv_null',
142 'csv_removeCRLF',
143 'csv_columns',
144 'csv_structure_or_data',
145 // csv_replace should have been here but we use it directly from $_POST
146 'latex_caption',
147 'latex_structure_or_data',
148 'latex_structure_caption',
149 'latex_structure_continued_caption',
150 'latex_structure_label',
151 'latex_relation',
152 'latex_comments',
153 'latex_mime',
154 'latex_columns',
155 'latex_data_caption',
156 'latex_data_continued_caption',
157 'latex_data_label',
158 'latex_null',
159 'aliases'
162 foreach ($post_params as $one_post_param) {
163 if (isset($_POST[$one_post_param])) {
164 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
168 $table = $GLOBALS['table'];
170 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
172 // sanitize this parameter which will be used below in a file inclusion
173 $what = PMA_securePath($_POST['what']);
175 // export class instance, not array of properties, as before
176 /* @var $export_plugin ExportPlugin */
177 $export_plugin = PMA_getPlugin(
178 "export",
179 $what,
180 'libraries/plugins/export/',
181 array(
182 'export_type' => $export_type,
183 'single_table' => isset($single_table)
187 // Backward compatibility
188 $type = $what;
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'
261 && mb_strlen($db)
263 $err_url = 'db_export.php' . URL::getCommon(array('db' => $db));
264 // Check if we have something to export
265 if (isset($table_select)) {
266 $tables = $table_select;
267 } else {
268 $tables = array();
270 } elseif ($export_type == 'table' && mb_strlen($db)
271 && mb_strlen($table)
273 $err_url = 'tbl_export.php' . URL::getCommon(
274 array(
275 'db' => $db, 'table' => $table
278 } else {
279 PMA_fatalError(__('Bad parameters!'));
282 // Merge SQL Query aliases with Export aliases from
283 // export page, Export page aliases are given more
284 // preference over SQL Query aliases.
285 $parser = new SqlParser\Parser($sql_query);
286 $aliases = array();
287 if ((!empty($parser->statements[0]))
288 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
290 if (!empty($_REQUEST['aliases'])) {
291 $aliases = PMA_mergeAliases(
292 SqlParser\Utils\Misc::getAliases($parser->statements[0], $db),
293 $_REQUEST['aliases']
295 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
296 } else {
297 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
302 * Increase time limit for script execution and initializes some variables
304 @set_time_limit($cfg['ExecTimeLimit']);
305 if (! empty($cfg['MemoryLimit'])) {
306 @ini_set('memory_limit', $cfg['MemoryLimit']);
308 register_shutdown_function('PMA_shutdownDuringExport');
309 // Start with empty buffer
310 $dump_buffer = '';
311 $dump_buffer_len = 0;
313 // Array of dump_buffers - used in separate file exports
314 $dump_buffer_objects = array();
316 // We send fake headers to avoid browser timeout when buffering
317 $time_start = time();
319 // Defines the default <CR><LF> format.
320 // For SQL always use \n as MySQL wants this on all platforms.
321 if ($what == 'sql') {
322 $crlf = "\n";
323 } else {
324 $crlf = PMA\libraries\Util::whichCrlf();
327 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
328 && $type != 'xls';
330 // Do we need to convert charset?
331 $output_charset_conversion = $asfile
332 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
333 && isset($charset) && $charset != 'utf-8'
334 && $type != 'xls';
336 // Use on the fly compression?
337 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
338 && $compression == 'gzip';
339 if ($GLOBALS['onfly_compression']) {
340 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
343 // Generate filename and mime type if needed
344 if ($asfile) {
345 if (empty($remember_template)) {
346 $remember_template = '';
348 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
349 $export_type, $remember_template, $export_plugin, $compression,
350 $filename_template
352 } else {
353 $mime_type = '';
356 // Open file on server if needed
357 if ($save_on_server) {
358 list($save_filename, $message, $file_handle) = PMA_openExportFile(
359 $filename, $quick_export
362 // problem opening export file on server?
363 if (! empty($message)) {
364 PMA_showExportPage($db, $table, $export_type);
366 } else {
368 * Send headers depending on whether the user chose to download a dump file
369 * or not
371 if ($asfile) {
372 // Download
373 // (avoid rewriting data containing HTML with anchors and forms;
374 // this was reported to happen under Plesk)
375 @ini_set('url_rewriter.tags', '');
376 $filename = Sanitize::sanitizeFilename($filename);
378 PMA_downloadHeader($filename, $mime_type);
379 } else {
380 // HTML
381 if ($export_type == 'database') {
382 $num_tables = count($tables);
383 if ($num_tables == 0) {
384 $message = PMA\libraries\Message::error(
385 __('No tables found in database.')
387 $active_page = 'db_export.php';
388 include 'db_export.php';
389 exit();
392 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
393 $export_type, $db, $table
395 echo $html;
396 unset($html);
397 } // end download
400 // Fake loop just to allow skip of remain of this code by break, I'd really
401 // need exceptions here :-)
402 do {
403 // Re - initialize
404 $dump_buffer = '';
405 $dump_buffer_len = 0;
407 // Add possibly some comments to export
408 if (! $export_plugin->exportHeader()) {
409 break;
412 // Will we need relation & co. setup?
413 $do_relation = isset($GLOBALS[$what . '_relation']);
414 $do_comments = isset($GLOBALS[$what . '_include_comments'])
415 || isset($GLOBALS[$what . '_comments']);
416 $do_mime = isset($GLOBALS[$what . '_mime']);
417 if ($do_relation || $do_comments || $do_mime) {
418 $cfgRelation = PMA_getRelationsParam();
420 if ($do_mime) {
421 include_once 'libraries/transformations.lib.php';
424 // Include dates in export?
425 $do_dates = isset($GLOBALS[$what . '_dates']);
427 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
430 * Builds the dump
432 if ($export_type == 'server') {
433 if (! isset($db_select)) {
434 $db_select = '';
436 PMA_exportServer(
437 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
438 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
439 $aliases, $separate_files
441 } elseif ($export_type == 'database') {
442 if (!isset($table_structure) || !is_array($table_structure)) {
443 $table_structure = array();
445 if (!isset($table_data) || !is_array($table_data)) {
446 $table_data = array();
448 if (!empty($_REQUEST['structure_or_data_forced'])) {
449 $table_structure = $tables;
450 $table_data = $tables;
452 if (isset($lock_tables)) {
453 PMA_lockTables($db, $tables, "READ");
454 try {
455 PMA_exportDatabase(
456 $db, $tables, $whatStrucOrData, $table_structure,
457 $table_data, $export_plugin, $crlf, $err_url, $export_type,
458 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
459 $separate_files
461 } finally {
462 PMA_unlockTables();
464 } else {
465 PMA_exportDatabase(
466 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
467 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
468 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
471 } else {
472 // We export just one table
473 // $allrows comes from the form when "Dump all rows" has been selected
474 if (! isset($allrows)) {
475 $allrows = '';
477 if (! isset($limit_to)) {
478 $limit_to = 0;
480 if (! isset($limit_from)) {
481 $limit_from = 0;
483 if (isset($lock_tables)) {
484 try {
485 PMA_lockTables($db, array($table), "READ");
486 PMA_exportTable(
487 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
488 $err_url, $export_type, $do_relation, $do_comments,
489 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
490 $sql_query, $aliases
492 } finally {
493 PMA_unlockTables();
495 } else {
496 PMA_exportTable(
497 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
498 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
499 $allrows, $limit_to, $limit_from, $sql_query, $aliases
503 if (! $export_plugin->exportFooter()) {
504 break;
507 } while (false);
508 // End of fake loop
510 if ($save_on_server && ! empty($message)) {
511 PMA_showExportPage($db, $table, $export_type);
515 * Send the dump as a file...
517 if (empty($asfile)) {
518 echo PMA_getHtmlForDisplayedExportFooter($back_button);
519 return;
520 } // end if
522 // Convert the charset if required.
523 if ($output_charset_conversion) {
524 $dump_buffer = PMA_convertString(
525 'utf-8',
526 $GLOBALS['charset'],
527 $dump_buffer
531 // Compression needed?
532 if ($compression) {
533 if (! empty($separate_files)) {
534 $dump_buffer = PMA_compressExport(
535 $dump_buffer_objects, $compression, $filename
537 } else {
538 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
543 /* If we saved on server, we have to close file now */
544 if ($save_on_server) {
545 $message = PMA_closeExportFile(
546 $file_handle, $dump_buffer, $save_filename
548 PMA_showExportPage($db, $table, $export_type);
549 } else {
550 echo $dump_buffer;