Translated using Weblate (Italian)
[phpmyadmin.git] / export.php
blob785972b7ddbe7bb4f9b2d86e3cc0cc1a7a6541b5
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;
10 /**
11 * Get the variables sent or posted to this script and a core script
13 if (!defined('TESTSUITE')) {
14 /**
15 * If we are sending the export file (as opposed to just displaying it
16 * as text), we have to bypass the usual PMA\libraries\Response mechanism
18 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
19 define('PMA_BYPASS_GET_INSTANCE', 1);
21 include_once 'libraries/common.inc.php';
22 include_once 'libraries/plugin_interface.lib.php';
23 include_once 'libraries/export.lib.php';
25 //check if it's the GET request to check export time out
26 if (isset($_GET['check_time_out'])) {
27 if (isset($_SESSION['pma_export_error'])) {
28 $err = $_SESSION['pma_export_error'];
29 unset($_SESSION['pma_export_error']);
30 echo "timeout";
31 } else {
32 echo "success";
34 exit;
36 /**
37 * Sets globals from $_POST
39 * - Please keep the parameters in order of their appearance in the form
40 * - Some of these parameters are not used, as the code below directly
41 * verifies from the superglobal $_POST or $_REQUEST
43 $post_params = array(
44 'db',
45 'table',
46 'what',
47 'single_table',
48 'export_type',
49 'export_method',
50 'quick_or_custom',
51 'db_select',
52 'table_select',
53 'table_structure',
54 'table_data',
55 'limit_to',
56 'limit_from',
57 'allrows',
58 'lock_tables',
59 'output_format',
60 'filename_template',
61 'maxsize',
62 'remember_template',
63 'charset',
64 'compression',
65 'as_separate_files',
66 'knjenc',
67 'xkana',
68 'htmlword_structure_or_data',
69 'htmlword_null',
70 'htmlword_columns',
71 'mediawiki_headers',
72 'mediawiki_structure_or_data',
73 'mediawiki_caption',
74 'pdf_structure_or_data',
75 'odt_structure_or_data',
76 'odt_relation',
77 'odt_comments',
78 'odt_mime',
79 'odt_columns',
80 'odt_null',
81 'codegen_structure_or_data',
82 'codegen_format',
83 'excel_null',
84 'excel_removeCRLF',
85 'excel_columns',
86 'excel_edition',
87 'excel_structure_or_data',
88 'yaml_structure_or_data',
89 'ods_null',
90 'ods_structure_or_data',
91 'ods_columns',
92 'json_structure_or_data',
93 'json_pretty_print',
94 'xml_structure_or_data',
95 'xml_export_events',
96 'xml_export_functions',
97 'xml_export_procedures',
98 'xml_export_tables',
99 'xml_export_triggers',
100 'xml_export_views',
101 'xml_export_contents',
102 'texytext_structure_or_data',
103 'texytext_columns',
104 'texytext_null',
105 'phparray_structure_or_data',
106 'sql_include_comments',
107 'sql_header_comment',
108 'sql_dates',
109 'sql_relation',
110 'sql_mime',
111 'sql_use_transaction',
112 'sql_disable_fk',
113 'sql_compatibility',
114 'sql_structure_or_data',
115 'sql_create_database',
116 'sql_drop_table',
117 'sql_procedure_function',
118 'sql_create_table',
119 'sql_create_view',
120 'sql_create_trigger',
121 'sql_if_not_exists',
122 'sql_auto_increment',
123 'sql_backquotes',
124 'sql_truncate',
125 'sql_delayed',
126 'sql_ignore',
127 'sql_type',
128 'sql_insert_syntax',
129 'sql_max_query_size',
130 'sql_hex_for_binary',
131 'sql_utc_time',
132 'sql_drop_database',
133 'sql_views_as_tables',
134 'sql_metadata',
135 'csv_separator',
136 'csv_enclosed',
137 'csv_escaped',
138 'csv_terminated',
139 'csv_null',
140 'csv_removeCRLF',
141 'csv_columns',
142 'csv_structure_or_data',
143 // csv_replace should have been here but we use it directly from $_POST
144 'latex_caption',
145 'latex_structure_or_data',
146 'latex_structure_caption',
147 'latex_structure_continued_caption',
148 'latex_structure_label',
149 'latex_relation',
150 'latex_comments',
151 'latex_mime',
152 'latex_columns',
153 'latex_data_caption',
154 'latex_data_continued_caption',
155 'latex_data_label',
156 'latex_null',
157 'aliases'
160 foreach ($post_params as $one_post_param) {
161 if (isset($_POST[$one_post_param])) {
162 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
166 $table = $GLOBALS['table'];
168 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
170 // sanitize this parameter which will be used below in a file inclusion
171 $what = PMA_securePath($_POST['what']);
173 // export class instance, not array of properties, as before
174 /* @var $export_plugin ExportPlugin */
175 $export_plugin = PMA_getPlugin(
176 "export",
177 $what,
178 'libraries/plugins/export/',
179 array(
180 'export_type' => $export_type,
181 'single_table' => isset($single_table)
185 // Backward compatibility
186 $type = $what;
188 // Check export type
189 if (empty($export_plugin)) {
190 PMA_fatalError(__('Bad type!'));
194 * valid compression methods
196 $compression_methods = array(
197 'zip',
198 'gzip'
202 * init and variable checking
204 $compression = false;
205 $onserver = false;
206 $save_on_server = false;
207 $buffer_needed = false;
208 $back_button = '';
209 $save_filename = '';
210 $file_handle = '';
211 $err_url = '';
212 $filename = '';
213 $separate_files = '';
215 // Is it a quick or custom export?
216 if (isset($_REQUEST['quick_or_custom'])
217 && $_REQUEST['quick_or_custom'] == 'quick'
219 $quick_export = true;
220 } else {
221 $quick_export = false;
224 if ($_REQUEST['output_format'] == 'astext') {
225 $asfile = false;
226 } else {
227 $asfile = true;
228 if (isset($_REQUEST['as_separate_files'])
229 && ! empty($_REQUEST['as_separate_files'])
231 if (isset($_REQUEST['compression'])
232 && ! empty($_REQUEST['compression'])
233 && $_REQUEST['compression'] == 'zip'
235 $separate_files = $_REQUEST['as_separate_files'];
238 if (in_array($_REQUEST['compression'], $compression_methods)) {
239 $compression = $_REQUEST['compression'];
240 $buffer_needed = true;
242 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
243 || (! $quick_export && ! empty($_REQUEST['onserver']))
245 if ($quick_export) {
246 $onserver = $_REQUEST['quick_export_onserver'];
247 } else {
248 $onserver = $_REQUEST['onserver'];
250 // Will we save dump on server?
251 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
255 // Generate error url and check for needed variables
256 if ($export_type == 'server') {
257 $err_url = 'server_export.php' . PMA_URL_getCommon();
258 } elseif ($export_type == 'database'
259 && mb_strlen($db)
261 $err_url = 'db_export.php' . PMA_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' && mb_strlen($db)
269 && mb_strlen($table)
271 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
272 array(
273 'db' => $db, 'table' => $table
276 } else {
277 PMA_fatalError(__('Bad parameters!'));
280 // Merge SQL Query aliases with Export aliases from
281 // export page, Export page aliases are given more
282 // preference over SQL Query aliases.
283 $parser = new SqlParser\Parser($sql_query);
284 $aliases = array();
285 if ((!empty($parser->statements[0]))
286 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
288 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
290 if (!empty($_REQUEST['aliases'])) {
291 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
292 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
296 * Increase time limit for script execution and initializes some variables
298 @set_time_limit($cfg['ExecTimeLimit']);
299 if (! empty($cfg['MemoryLimit'])) {
300 @ini_set('memory_limit', $cfg['MemoryLimit']);
302 register_shutdown_function('PMA_shutdownDuringExport');
303 // Start with empty buffer
304 $dump_buffer = '';
305 $dump_buffer_len = 0;
307 // Array of dump_buffers - used in separate file exports
308 $dump_buffer_objects = array();
310 // We send fake headers to avoid browser timeout when buffering
311 $time_start = time();
313 // Defines the default <CR><LF> format.
314 // For SQL always use \n as MySQL wants this on all platforms.
315 if ($what == 'sql') {
316 $crlf = "\n";
317 } else {
318 $crlf = PMA\libraries\Util::whichCrlf();
321 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
322 && $type != 'xls';
324 // Do we need to convert charset?
325 $output_charset_conversion = $asfile
326 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
327 && isset($charset) && $charset != 'utf-8'
328 && $type != 'xls';
330 // Use on the fly compression?
331 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
332 && $compression == 'gzip';
333 if ($GLOBALS['onfly_compression']) {
334 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
337 // Generate filename and mime type if needed
338 if ($asfile) {
339 if (empty($remember_template)) {
340 $remember_template = '';
342 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
343 $export_type, $remember_template, $export_plugin, $compression,
344 $filename_template
346 } else {
347 $mime_type = '';
350 // Open file on server if needed
351 if ($save_on_server) {
352 list($save_filename, $message, $file_handle) = PMA_openExportFile(
353 $filename, $quick_export
356 // problem opening export file on server?
357 if (! empty($message)) {
358 PMA_showExportPage($db, $table, $export_type);
360 } else {
362 * Send headers depending on whether the user chose to download a dump file
363 * or not
365 if ($asfile) {
366 // Download
367 // (avoid rewriting data containing HTML with anchors and forms;
368 // this was reported to happen under Plesk)
369 @ini_set('url_rewriter.tags', '');
370 $filename = PMA_sanitizeFilename($filename);
372 PMA_downloadHeader($filename, $mime_type);
373 } else {
374 // HTML
375 if ($export_type == 'database') {
376 $num_tables = count($tables);
377 if ($num_tables == 0) {
378 $message = PMA\libraries\Message::error(
379 __('No tables found in database.')
381 $active_page = 'db_export.php';
382 include 'db_export.php';
383 exit();
386 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
387 $export_type, $db, $table
389 echo $html;
390 unset($html);
391 } // end download
394 // Fake loop just to allow skip of remain of this code by break, I'd really
395 // need exceptions here :-)
396 do {
397 // Re - initialize
398 $dump_buffer = '';
399 $dump_buffer_len = 0;
401 // Add possibly some comments to export
402 if (! $export_plugin->exportHeader()) {
403 break;
406 // Will we need relation & co. setup?
407 $do_relation = isset($GLOBALS[$what . '_relation']);
408 $do_comments = isset($GLOBALS[$what . '_include_comments'])
409 || isset($GLOBALS[$what . '_comments']);
410 $do_mime = isset($GLOBALS[$what . '_mime']);
411 if ($do_relation || $do_comments || $do_mime) {
412 $cfgRelation = PMA_getRelationsParam();
414 if ($do_mime) {
415 include_once 'libraries/transformations.lib.php';
418 // Include dates in export?
419 $do_dates = isset($GLOBALS[$what . '_dates']);
421 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
424 * Builds the dump
426 if ($export_type == 'server') {
427 if (! isset($db_select)) {
428 $db_select = '';
430 PMA_exportServer(
431 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
432 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
433 $aliases, $separate_files
435 } elseif ($export_type == 'database') {
436 if (!isset($table_structure) || !is_array($table_structure)) {
437 $table_structure = array();
439 if (!isset($table_data) || !is_array($table_data)) {
440 $table_data = array();
442 if (!empty($_REQUEST['structure_or_data_forced'])) {
443 $table_structure = $tables;
444 $table_data = $tables;
446 if (isset($lock_tables)) {
447 PMA_lockTables($db, $tables, "READ");
448 try {
449 PMA_exportDatabase(
450 $db, $tables, $whatStrucOrData, $table_structure,
451 $table_data, $export_plugin, $crlf, $err_url, $export_type,
452 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
453 $separate_files
455 } finally {
456 PMA_unlockTables();
458 } else {
459 PMA_exportDatabase(
460 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
461 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
462 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
465 } else {
466 // We export just one table
467 // $allrows comes from the form when "Dump all rows" has been selected
468 if (! isset($allrows)) {
469 $allrows = '';
471 if (! isset($limit_to)) {
472 $limit_to = 0;
474 if (! isset($limit_from)) {
475 $limit_from = 0;
477 if (isset($lock_tables)) {
478 try {
479 PMA_lockTables($db, array($table), "READ");
480 PMA_exportTable(
481 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
482 $err_url, $export_type, $do_relation, $do_comments,
483 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
484 $sql_query, $aliases
486 } finally {
487 PMA_unlockTables();
489 } else {
490 PMA_exportTable(
491 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
492 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
493 $allrows, $limit_to, $limit_from, $sql_query, $aliases
497 if (! $export_plugin->exportFooter()) {
498 break;
501 } while (false);
502 // End of fake loop
504 if ($save_on_server && ! empty($message)) {
505 PMA_showExportPage($db, $table, $export_type);
509 * Send the dump as a file...
511 if (empty($asfile)) {
512 echo PMA_getHtmlForDisplayedExportFooter($back_button);
513 return;
514 } // end if
516 // Convert the charset if required.
517 if ($output_charset_conversion) {
518 $dump_buffer = PMA_convertString(
519 'utf-8',
520 $GLOBALS['charset'],
521 $dump_buffer
525 // Compression needed?
526 if ($compression) {
527 if (! empty($separate_files)) {
528 $dump_buffer = PMA_compressExport(
529 $dump_buffer_objects, $compression, $filename
531 } else {
532 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
537 /* If we saved on server, we have to close file now */
538 if ($save_on_server) {
539 $message = PMA_closeExportFile(
540 $file_handle, $dump_buffer, $save_filename
542 PMA_showExportPage($db, $table, $export_type);
543 } else {
544 echo $dump_buffer;