Translated using Weblate (Dutch)
[phpmyadmin.git] / export.php
blob50ceb500ac616d48ae289e6998cd21bca914c7ad
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 'lock_tables',
56 'output_format',
57 'filename_template',
58 'maxsize',
59 'remember_template',
60 'charset_of_file',
61 'compression',
62 'knjenc',
63 'xkana',
64 'htmlword_structure_or_data',
65 'htmlword_null',
66 'htmlword_columns',
67 'mediawiki_headers',
68 'mediawiki_structure_or_data',
69 'mediawiki_caption',
70 'pdf_structure_or_data',
71 'odt_structure_or_data',
72 'odt_relation',
73 'odt_comments',
74 'odt_mime',
75 'odt_columns',
76 'odt_null',
77 'codegen_structure_or_data',
78 'codegen_format',
79 'excel_null',
80 'excel_removeCRLF',
81 'excel_columns',
82 'excel_edition',
83 'excel_structure_or_data',
84 'yaml_structure_or_data',
85 'ods_null',
86 'ods_structure_or_data',
87 'ods_columns',
88 'json_structure_or_data',
89 'xml_structure_or_data',
90 'xml_export_events',
91 'xml_export_functions',
92 'xml_export_procedures',
93 'xml_export_tables',
94 'xml_export_triggers',
95 'xml_export_views',
96 'xml_export_contents',
97 'texytext_structure_or_data',
98 'texytext_columns',
99 'texytext_null',
100 'phparray_structure_or_data',
101 'sql_include_comments',
102 'sql_header_comment',
103 'sql_dates',
104 'sql_relation',
105 'sql_mime',
106 'sql_use_transaction',
107 'sql_disable_fk',
108 'sql_compatibility',
109 'sql_structure_or_data',
110 'sql_create_database',
111 'sql_drop_table',
112 'sql_procedure_function',
113 'sql_create_table_statements',
114 'sql_create_table',
115 'sql_create_view',
116 'sql_create_trigger',
117 'sql_if_not_exists',
118 'sql_auto_increment',
119 'sql_backquotes',
120 'sql_truncate',
121 'sql_delayed',
122 'sql_ignore',
123 'sql_type',
124 'sql_insert_syntax',
125 'sql_max_query_size',
126 'sql_hex_for_binary',
127 'sql_utc_time',
128 'sql_drop_database',
129 'sql_views_as_tables',
130 'csv_separator',
131 'csv_enclosed',
132 'csv_escaped',
133 'csv_terminated',
134 'csv_null',
135 'csv_removeCRLF',
136 'csv_columns',
137 'csv_structure_or_data',
138 // csv_replace should have been here but we use it directly from $_POST
139 'latex_caption',
140 'latex_structure_or_data',
141 'latex_structure_caption',
142 'latex_structure_continued_caption',
143 'latex_structure_label',
144 'latex_relation',
145 'latex_comments',
146 'latex_mime',
147 'latex_columns',
148 'latex_data_caption',
149 'latex_data_continued_caption',
150 'latex_data_label',
151 'latex_null',
152 'aliases'
155 foreach ($post_params as $one_post_param) {
156 if (isset($_POST[$one_post_param])) {
157 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
161 $table = $GLOBALS['table'];
162 // sanitize this parameter which will be used below in a file inclusion
163 $what = PMA_securePath($_POST['what']);
165 PMA_Util::checkParameters(array('what', 'export_type'));
167 // export class instance, not array of properties, as before
168 $export_plugin = PMA_getPlugin(
169 "export",
170 $what,
171 'libraries/plugins/export/',
172 array(
173 'export_type' => $export_type,
174 'single_table' => isset($single_table)
178 // Backward compatibility
179 $type = $what;
181 // Check export type
182 if (! isset($export_plugin)) {
183 PMA_fatalError(__('Bad type!'));
186 // Avoid warning from PHP Analyzer
187 if (is_null($export_plugin)) {
188 $export_plugin = new stdClass();
192 * valid compression methods
194 $compression_methods = array(
195 'zip',
196 'gzip'
200 * init and variable checking
202 $compression = false;
203 $onserver = false;
204 $save_on_server = false;
205 $buffer_needed = false;
206 $back_button = '';
207 $save_filename = '';
208 $file_handle = '';
209 $err_url = '';
210 $filename = '';
212 // Is it a quick or custom export?
213 if ($_REQUEST['quick_or_custom'] == 'quick') {
214 $quick_export = true;
215 } else {
216 $quick_export = false;
219 if ($_REQUEST['output_format'] == 'astext') {
220 $asfile = false;
221 } else {
222 $asfile = true;
223 if (in_array($_REQUEST['compression'], $compression_methods)) {
224 $compression = $_REQUEST['compression'];
225 $buffer_needed = true;
227 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
228 || (! $quick_export && ! empty($_REQUEST['onserver']))
230 if ($quick_export) {
231 $onserver = $_REQUEST['quick_export_onserver'];
232 } else {
233 $onserver = $_REQUEST['onserver'];
235 // Will we save dump on server?
236 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
240 // Generate error url and check for needed variables
241 /** @var PMA_String $pmaString */
242 $pmaString = $GLOBALS['PMA_String'];
243 if ($export_type == 'server') {
244 $err_url = 'server_export.php' . PMA_URL_getCommon();
245 } elseif ($export_type == 'database'
246 && /*overload*/mb_strlen($db)
248 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
249 // Check if we have something to export
250 if (isset($table_select)) {
251 $tables = $table_select;
252 } else {
253 $tables = array();
255 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
256 && /*overload*/mb_strlen($table)
258 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
259 array(
260 'db' => $db, 'table' => $table
263 } else {
264 PMA_fatalError(__('Bad parameters!'));
267 // Merge SQL Query aliases with Export aliases from
268 // export page, Export page aliases are given more
269 // preference over SQL Query aliases.
270 if (!empty($_REQUEST['aliases'])) {
271 $aliases = PMA_mergeAliases(
272 PMA_SQP_getAliasesFromQuery($sql_query, $db),
273 $_REQUEST['aliases']
275 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
276 } else {
277 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
281 * Increase time limit for script execution and initializes some variables
283 @set_time_limit($cfg['ExecTimeLimit']);
284 if (! empty($cfg['MemoryLimit'])) {
285 @ini_set('memory_limit', $cfg['MemoryLimit']);
287 register_shutdown_function('PMA_shutdownDuringExport');
288 // Start with empty buffer
289 $dump_buffer = '';
290 $dump_buffer_len = 0;
292 // We send fake headers to avoid browser timeout when buffering
293 $time_start = time();
295 // Defines the default <CR><LF> format.
296 // For SQL always use \n as MySQL wants this on all platforms.
297 if ($what == 'sql') {
298 $crlf = "\n";
299 } else {
300 $crlf = PMA_Util::whichCrlf();
303 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
304 && $type != 'xls';
306 // Do we need to convert charset?
307 $output_charset_conversion = $asfile
308 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
309 && isset($charset_of_file) && $charset_of_file != 'utf-8'
310 && $type != 'xls';
312 // Use on the fly compression?
313 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
314 && $compression == 'gzip';
315 if ($GLOBALS['onfly_compression']) {
316 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
319 // Generate filename and mime type if needed
320 if ($asfile) {
321 if (empty($remember_template)) {
322 $remember_template = '';
324 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
325 $export_type, $remember_template, $export_plugin, $compression,
326 $filename_template
328 } else {
329 $mime_type = '';
332 // Open file on server if needed
333 if ($save_on_server) {
334 list($save_filename, $message, $file_handle) = PMA_openExportFile(
335 $filename, $quick_export
338 // problem opening export file on server?
339 if (! empty($message)) {
340 PMA_showExportPage($db, $table, $export_type);
342 } else {
344 * Send headers depending on whether the user chose to download a dump file
345 * or not
347 if ($asfile) {
348 // Download
349 // (avoid rewriting data containing HTML with anchors and forms;
350 // this was reported to happen under Plesk)
351 @ini_set('url_rewriter.tags', '');
352 $filename = PMA_sanitizeFilename($filename);
354 PMA_downloadHeader($filename, $mime_type);
355 } else {
356 // HTML
357 if ($export_type == 'database') {
358 $num_tables = count($tables);
359 if ($num_tables == 0) {
360 $message = PMA_Message::error(
361 __('No tables found in database.')
363 $active_page = 'db_export.php';
364 include 'db_export.php';
365 exit();
368 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
369 $export_type, $db, $table
371 echo $html;
372 unset($html);
373 } // end download
376 // Fake loop just to allow skip of remain of this code by break, I'd really
377 // need exceptions here :-)
378 do {
380 // Add possibly some comments to export
381 if (! $export_plugin->exportHeader($db)) {
382 break;
385 // Will we need relation & co. setup?
386 $do_relation = isset($GLOBALS[$what . '_relation']);
387 $do_comments = isset($GLOBALS[$what . '_include_comments'])
388 || isset($GLOBALS[$what . '_comments']) ;
389 $do_mime = isset($GLOBALS[$what . '_mime']);
390 if ($do_relation || $do_comments || $do_mime) {
391 $cfgRelation = PMA_getRelationsParam();
393 if ($do_mime) {
394 include_once 'libraries/transformations.lib.php';
397 // Include dates in export?
398 $do_dates = isset($GLOBALS[$what . '_dates']);
400 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
403 * Builds the dump
405 if ($export_type == 'server') {
406 if (! isset($db_select)) {
407 $db_select = '';
409 PMA_exportServer(
410 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
411 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
412 $aliases
414 } elseif ($export_type == 'database') {
415 if (isset($lock_tables)) {
416 PMA_lockTables($db, $tables, "READ");
417 try {
418 PMA_exportDatabase(
419 $db, $tables, $whatStrucOrData, $export_plugin, $crlf,
420 $err_url, $export_type, $do_relation, $do_comments,
421 $do_mime, $do_dates, $aliases
423 PMA_unlockTables();
424 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
425 PMA_unlockTables();
426 throw $e;
428 } else {
429 PMA_exportDatabase(
430 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
431 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
432 $aliases
435 } else {
436 // We export just one table
437 // $allrows comes from the form when "Dump all rows" has been selected
438 if (! isset($allrows)) {
439 $allrows = '';
441 if (! isset($limit_to)) {
442 $limit_to = 0;
444 if (! isset($limit_from)) {
445 $limit_from = 0;
447 if (isset($lock_tables)) {
448 try {
449 PMA_lockTables($db, array($table), "READ");
450 PMA_exportTable(
451 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
452 $err_url, $export_type, $do_relation, $do_comments,
453 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
454 $sql_query, $aliases
456 PMA_unlockTables();
457 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
458 PMA_unlockTables();
459 throw $e;
461 } else {
462 PMA_exportTable(
463 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
464 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
465 $allrows, $limit_to, $limit_from, $sql_query, $aliases
469 if (! $export_plugin->exportFooter()) {
470 break;
473 } while (false);
474 // End of fake loop
476 if ($save_on_server && ! empty($message)) {
477 PMA_showExportPage($db, $table, $export_type);
481 * Send the dump as a file...
483 if (! empty($asfile)) {
484 // Convert the charset if required.
485 if ($output_charset_conversion) {
486 $dump_buffer = PMA_convertString(
487 'utf-8',
488 $GLOBALS['charset_of_file'],
489 $dump_buffer
493 // Compression needed?
494 if ($compression) {
495 $dump_buffer
496 = PMA_compressExport($dump_buffer, $compression, $filename);
499 /* If we saved on server, we have to close file now */
500 if ($save_on_server) {
501 $message = PMA_closeExportFile(
502 $file_handle, $dump_buffer, $save_filename
504 PMA_showExportPage($db, $table, $export_type);
505 } else {
506 echo $dump_buffer;
508 } else {
509 echo PMA_getHtmlForDisplayedExportFooter($back_button);
510 } // end if