Translated using Weblate (Italian)
[phpmyadmin.git] / export.php
blob5e9765990b00d183f6d3edfa4dbf44f768044700
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get the variables sent or posted to this script and a core script
12 if (!defined('TESTSUITE')) {
13 /**
14 * If we are sending the export file (as opposed to just displaying it
15 * as text), we have to bypass the usual PMA_Response mechanism
17 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
18 define('PMA_BYPASS_GET_INSTANCE', 1);
20 include_once 'libraries/common.inc.php';
21 include_once 'libraries/zip.lib.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 'single_table',
47 'export_type',
48 'export_method',
49 'quick_or_custom',
50 'db_select',
51 'table_select',
52 'limit_to',
53 'limit_from',
54 'allrows',
55 'output_format',
56 'filename_template',
57 'maxsize',
58 'remember_template',
59 'charset_of_file',
60 'compression',
61 'knjenc',
62 'xkana',
63 'htmlword_structure_or_data',
64 'htmlword_null',
65 'htmlword_columns',
66 'mediawiki_headers',
67 'mediawiki_structure_or_data',
68 'mediawiki_caption',
69 'pdf_structure_or_data',
70 'odt_structure_or_data',
71 'odt_relation',
72 'odt_comments',
73 'odt_mime',
74 'odt_columns',
75 'odt_null',
76 'codegen_structure_or_data',
77 'codegen_format',
78 'excel_null',
79 'excel_removeCRLF',
80 'excel_columns',
81 'excel_edition',
82 'excel_structure_or_data',
83 'yaml_structure_or_data',
84 'ods_null',
85 'ods_structure_or_data',
86 'ods_columns',
87 'json_structure_or_data',
88 'xml_structure_or_data',
89 'xml_export_events',
90 'xml_export_functions',
91 'xml_export_procedures',
92 'xml_export_tables',
93 'xml_export_triggers',
94 'xml_export_views',
95 'xml_export_contents',
96 'texytext_structure_or_data',
97 'texytext_columns',
98 'texytext_null',
99 'phparray_structure_or_data',
100 'sql_include_comments',
101 'sql_header_comment',
102 'sql_dates',
103 'sql_relation',
104 'sql_mime',
105 'sql_use_transaction',
106 'sql_disable_fk',
107 'sql_compatibility',
108 'sql_structure_or_data',
109 'sql_create_database',
110 'sql_drop_table',
111 'sql_procedure_function',
112 'sql_create_table_statements',
113 'sql_create_table',
114 'sql_create_view',
115 'sql_create_trigger',
116 'sql_if_not_exists',
117 'sql_auto_increment',
118 'sql_backquotes',
119 'sql_truncate',
120 'sql_delayed',
121 'sql_ignore',
122 'sql_type',
123 'sql_insert_syntax',
124 'sql_max_query_size',
125 'sql_hex_for_binary',
126 'sql_utc_time',
127 'sql_drop_database',
128 'sql_views_as_tables',
129 'csv_separator',
130 'csv_enclosed',
131 'csv_escaped',
132 'csv_terminated',
133 'csv_null',
134 'csv_removeCRLF',
135 'csv_columns',
136 'csv_structure_or_data',
137 // csv_replace should have been here but we use it directly from $_POST
138 'latex_caption',
139 'latex_structure_or_data',
140 'latex_structure_caption',
141 'latex_structure_continued_caption',
142 'latex_structure_label',
143 'latex_relation',
144 'latex_comments',
145 'latex_mime',
146 'latex_columns',
147 'latex_data_caption',
148 'latex_data_continued_caption',
149 'latex_data_label',
150 'latex_null',
151 'aliases'
154 foreach ($post_params as $one_post_param) {
155 if (isset($_POST[$one_post_param])) {
156 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
160 $table = $GLOBALS['table'];
161 // sanitize this parameter which will be used below in a file inclusion
162 $what = PMA_securePath($_POST['what']);
164 PMA_Util::checkParameters(array('what', 'export_type'));
166 // export class instance, not array of properties, as before
167 $export_plugin = PMA_getPlugin(
168 "export",
169 $what,
170 'libraries/plugins/export/',
171 array(
172 'export_type' => $export_type,
173 'single_table' => isset($single_table)
177 // Backward compatibility
178 $type = $what;
180 // Check export type
181 if (! isset($export_plugin)) {
182 PMA_fatalError(__('Bad type!'));
185 // Avoid warning from PHP Analyzer
186 if (is_null($export_plugin)) {
187 $export_plugin = new stdClass();
191 * valid compression methods
193 $compression_methods = array(
194 'zip',
195 'gzip'
199 * init and variable checking
201 $compression = false;
202 $onserver = false;
203 $save_on_server = false;
204 $buffer_needed = false;
205 $back_button = '';
206 $save_filename = '';
207 $file_handle = '';
208 $err_url = '';
209 $filename = '';
211 // Is it a quick or custom export?
212 if ($_REQUEST['quick_or_custom'] == 'quick') {
213 $quick_export = true;
214 } else {
215 $quick_export = false;
218 if ($_REQUEST['output_format'] == 'astext') {
219 $asfile = false;
220 } else {
221 $asfile = true;
222 if (in_array($_REQUEST['compression'], $compression_methods)) {
223 $compression = $_REQUEST['compression'];
224 $buffer_needed = true;
226 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
227 || (! $quick_export && ! empty($_REQUEST['onserver']))
229 if ($quick_export) {
230 $onserver = $_REQUEST['quick_export_onserver'];
231 } else {
232 $onserver = $_REQUEST['onserver'];
234 // Will we save dump on server?
235 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
239 // Generate error url and check for needed variables
240 /** @var PMA_String $pmaString */
241 $pmaString = $GLOBALS['PMA_String'];
242 if ($export_type == 'server') {
243 $err_url = 'server_export.php' . PMA_URL_getCommon();
244 } elseif ($export_type == 'database'
245 && /*overload*/mb_strlen($db)
247 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
248 // Check if we have something to export
249 if (isset($table_select)) {
250 $tables = $table_select;
251 } else {
252 $tables = array();
254 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
255 && /*overload*/mb_strlen($table)
257 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
258 array(
259 'db' => $db, 'table' => $table
262 } else {
263 PMA_fatalError(__('Bad parameters!'));
266 // Merge SQL Query aliases with Export aliases from
267 // export page, Export page aliases are given more
268 // preference over SQL Query aliases.
269 if (!empty($_REQUEST['aliases'])) {
270 $aliases = PMA_mergeAliases(
271 PMA_SQP_getAliasesFromQuery($sql_query, $db),
272 $_REQUEST['aliases']
274 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
275 } else {
276 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
280 * Increase time limit for script execution and initializes some variables
282 @set_time_limit($cfg['ExecTimeLimit']);
283 if (! empty($cfg['MemoryLimit'])) {
284 @ini_set('memory_limit', $cfg['MemoryLimit']);
286 register_shutdown_function('PMA_shutdownDuringExport');
287 // Start with empty buffer
288 $dump_buffer = '';
289 $dump_buffer_len = 0;
291 // We send fake headers to avoid browser timeout when buffering
292 $time_start = time();
294 // Defines the default <CR><LF> format.
295 // For SQL always use \n as MySQL wants this on all platforms.
296 if ($what == 'sql') {
297 $crlf = "\n";
298 } else {
299 $crlf = PMA_Util::whichCrlf();
302 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
303 && $type != 'xls';
305 // Do we need to convert charset?
306 $output_charset_conversion = $asfile
307 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
308 && isset($charset_of_file) && $charset_of_file != 'utf-8'
309 && $type != 'xls';
311 // Use on the fly compression?
312 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
313 && $compression == 'gzip';
314 if ($GLOBALS['onfly_compression']) {
315 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
318 // Generate filename and mime type if needed
319 if ($asfile) {
320 if (empty($remember_template)) {
321 $remember_template = '';
323 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
324 $export_type, $remember_template, $export_plugin, $compression,
325 $filename_template
327 } else {
328 $mime_type = '';
331 // Open file on server if needed
332 if ($save_on_server) {
333 list($save_filename, $message, $file_handle) = PMA_openExportFile(
334 $filename, $quick_export
337 // problem opening export file on server?
338 if (! empty($message)) {
339 PMA_showExportPage($db, $table, $export_type);
341 } else {
343 * Send headers depending on whether the user chose to download a dump file
344 * or not
346 if ($asfile) {
347 // Download
348 // (avoid rewriting data containing HTML with anchors and forms;
349 // this was reported to happen under Plesk)
350 @ini_set('url_rewriter.tags', '');
351 $filename = PMA_sanitizeFilename($filename);
353 PMA_downloadHeader($filename, $mime_type);
354 } else {
355 // HTML
356 if ($export_type == 'database') {
357 $num_tables = count($tables);
358 if ($num_tables == 0) {
359 $message = PMA_Message::error(
360 __('No tables found in database.')
362 $active_page = 'db_export.php';
363 include 'db_export.php';
364 exit();
367 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
368 $export_type, $db, $table
370 echo $html;
371 unset($html);
372 } // end download
375 // Fake loop just to allow skip of remain of this code by break, I'd really
376 // need exceptions here :-)
377 do {
379 // Add possibly some comments to export
380 if (! $export_plugin->exportHeader($db)) {
381 break;
384 // Will we need relation & co. setup?
385 $do_relation = isset($GLOBALS[$what . '_relation']);
386 $do_comments = isset($GLOBALS[$what . '_include_comments'])
387 || isset($GLOBALS[$what . '_comments']) ;
388 $do_mime = isset($GLOBALS[$what . '_mime']);
389 if ($do_relation || $do_comments || $do_mime) {
390 $cfgRelation = PMA_getRelationsParam();
392 if ($do_mime) {
393 include_once 'libraries/transformations.lib.php';
396 // Include dates in export?
397 $do_dates = isset($GLOBALS[$what . '_dates']);
399 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
402 * Builds the dump
404 if ($export_type == 'server') {
405 if (! isset($db_select)) {
406 $db_select = '';
408 PMA_exportServer(
409 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
410 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
411 $aliases
413 } elseif ($export_type == 'database') {
414 PMA_exportDatabase(
415 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
416 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
417 $aliases
419 } else {
420 // We export just one table
421 // $allrows comes from the form when "Dump all rows" has been selected
422 if (! isset($allrows)) {
423 $allrows = '';
425 if (! isset($limit_to)) {
426 $limit_to = 0;
428 if (! isset($limit_from)) {
429 $limit_from = 0;
431 PMA_exportTable(
432 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
433 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
434 $allrows, $limit_to, $limit_from, $sql_query, $aliases
437 if (! $export_plugin->exportFooter()) {
438 break;
441 } while (false);
442 // End of fake loop
444 if ($save_on_server && ! empty($message)) {
445 PMA_showExportPage($db, $table, $export_type);
449 * Send the dump as a file...
451 if (! empty($asfile)) {
452 // Convert the charset if required.
453 if ($output_charset_conversion) {
454 $dump_buffer = PMA_convertString(
455 'utf-8',
456 $GLOBALS['charset_of_file'],
457 $dump_buffer
461 // Compression needed?
462 if ($compression) {
463 $dump_buffer
464 = PMA_compressExport($dump_buffer, $compression, $filename);
467 /* If we saved on server, we have to close file now */
468 if ($save_on_server) {
469 $message = PMA_closeExportFile(
470 $file_handle, $dump_buffer, $save_filename
472 PMA_showExportPage($db, $table, $export_type);
473 } else {
474 echo $dump_buffer;
476 } else {
477 echo PMA_getHtmlForDisplayedExportFooter($back_button);
478 } // end if