Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / export.php
blob7462543a846c323db57631394d258fa36f5b5e71
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 /**
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
44 * TODO: this should be removed to avoid passing user input to GLOBALS
45 * without checking
47 $post_params = array(
48 'db',
49 'table',
50 'what',
51 'single_table',
52 'export_type',
53 'export_method',
54 'quick_or_custom',
55 'db_select',
56 'table_select',
57 'table_structure',
58 'table_data',
59 'limit_to',
60 'limit_from',
61 'allrows',
62 'lock_tables',
63 'output_format',
64 'filename_template',
65 'maxsize',
66 'remember_template',
67 'charset',
68 'compression',
69 'as_separate_files',
70 'knjenc',
71 'xkana',
72 'htmlword_structure_or_data',
73 'htmlword_null',
74 'htmlword_columns',
75 'mediawiki_headers',
76 'mediawiki_structure_or_data',
77 'mediawiki_caption',
78 'pdf_structure_or_data',
79 'odt_structure_or_data',
80 'odt_relation',
81 'odt_comments',
82 'odt_mime',
83 'odt_columns',
84 'odt_null',
85 'codegen_structure_or_data',
86 'codegen_format',
87 'excel_null',
88 'excel_removeCRLF',
89 'excel_columns',
90 'excel_edition',
91 'excel_structure_or_data',
92 'yaml_structure_or_data',
93 'ods_null',
94 'ods_structure_or_data',
95 'ods_columns',
96 'json_structure_or_data',
97 'json_pretty_print',
98 'xml_structure_or_data',
99 'xml_export_events',
100 'xml_export_functions',
101 'xml_export_procedures',
102 'xml_export_tables',
103 'xml_export_triggers',
104 'xml_export_views',
105 'xml_export_contents',
106 'texytext_structure_or_data',
107 'texytext_columns',
108 'texytext_null',
109 'phparray_structure_or_data',
110 'sql_include_comments',
111 'sql_header_comment',
112 'sql_dates',
113 'sql_relation',
114 'sql_mime',
115 'sql_use_transaction',
116 'sql_disable_fk',
117 'sql_compatibility',
118 'sql_structure_or_data',
119 'sql_create_database',
120 'sql_drop_table',
121 'sql_procedure_function',
122 'sql_create_table',
123 'sql_create_view',
124 'sql_create_trigger',
125 'sql_if_not_exists',
126 'sql_auto_increment',
127 'sql_backquotes',
128 'sql_truncate',
129 'sql_delayed',
130 'sql_ignore',
131 'sql_type',
132 'sql_insert_syntax',
133 'sql_max_query_size',
134 'sql_hex_for_binary',
135 'sql_utc_time',
136 'sql_drop_database',
137 'sql_views_as_tables',
138 'sql_metadata',
139 'csv_separator',
140 'csv_enclosed',
141 'csv_escaped',
142 'csv_terminated',
143 'csv_null',
144 'csv_removeCRLF',
145 'csv_columns',
146 'csv_structure_or_data',
147 // csv_replace should have been here but we use it directly from $_POST
148 'latex_caption',
149 'latex_structure_or_data',
150 'latex_structure_caption',
151 'latex_structure_continued_caption',
152 'latex_structure_label',
153 'latex_relation',
154 'latex_comments',
155 'latex_mime',
156 'latex_columns',
157 'latex_data_caption',
158 'latex_data_continued_caption',
159 'latex_data_label',
160 'latex_null',
161 'aliases'
164 foreach ($post_params as $one_post_param) {
165 if (isset($_POST[$one_post_param])) {
166 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
170 $table = $GLOBALS['table'];
172 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
174 // sanitize this parameter which will be used below in a file inclusion
175 $what = PMA_securePath($_POST['what']);
177 // export class instance, not array of properties, as before
178 /* @var $export_plugin ExportPlugin */
179 $export_plugin = PMA_getPlugin(
180 "export",
181 $what,
182 'libraries/plugins/export/',
183 array(
184 'export_type' => $export_type,
185 'single_table' => isset($single_table)
189 // Backward compatibility
190 $type = $what;
192 // Check export type
193 if (empty($export_plugin)) {
194 PMA_fatalError(__('Bad type!'));
198 * valid compression methods
200 $compression_methods = array(
201 'zip',
202 'gzip'
206 * init and variable checking
208 $compression = false;
209 $onserver = false;
210 $save_on_server = false;
211 $buffer_needed = false;
212 $back_button = '';
213 $save_filename = '';
214 $file_handle = '';
215 $err_url = '';
216 $filename = '';
217 $separate_files = '';
219 // Is it a quick or custom export?
220 if (isset($_REQUEST['quick_or_custom'])
221 && $_REQUEST['quick_or_custom'] == 'quick'
223 $quick_export = true;
224 } else {
225 $quick_export = false;
228 if ($_REQUEST['output_format'] == 'astext') {
229 $asfile = false;
230 } else {
231 $asfile = true;
232 if (isset($_REQUEST['as_separate_files'])
233 && ! empty($_REQUEST['as_separate_files'])
235 if (isset($_REQUEST['compression'])
236 && ! empty($_REQUEST['compression'])
237 && $_REQUEST['compression'] == 'zip'
239 $separate_files = $_REQUEST['as_separate_files'];
242 if (in_array($_REQUEST['compression'], $compression_methods)) {
243 $compression = $_REQUEST['compression'];
244 $buffer_needed = true;
246 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
247 || (! $quick_export && ! empty($_REQUEST['onserver']))
249 if ($quick_export) {
250 $onserver = $_REQUEST['quick_export_onserver'];
251 } else {
252 $onserver = $_REQUEST['onserver'];
254 // Will we save dump on server?
255 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
259 // Generate error url and check for needed variables
260 if ($export_type == 'server') {
261 $err_url = 'server_export.php' . URL::getCommon();
262 } elseif ($export_type == 'database'
263 && mb_strlen($db)
265 $err_url = 'db_export.php' . URL::getCommon(array('db' => $db));
266 // Check if we have something to export
267 if (isset($table_select)) {
268 $tables = $table_select;
269 } else {
270 $tables = array();
272 } elseif ($export_type == 'table' && mb_strlen($db)
273 && mb_strlen($table)
275 $err_url = 'tbl_export.php' . URL::getCommon(
276 array(
277 'db' => $db, 'table' => $table
280 } else {
281 PMA_fatalError(__('Bad parameters!'));
284 // Merge SQL Query aliases with Export aliases from
285 // export page, Export page aliases are given more
286 // preference over SQL Query aliases.
287 $parser = new SqlParser\Parser($sql_query);
288 $aliases = array();
289 if ((!empty($parser->statements[0]))
290 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
292 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
294 if (!empty($_REQUEST['aliases'])) {
295 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
296 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
300 * Increase time limit for script execution and initializes some variables
302 @set_time_limit($cfg['ExecTimeLimit']);
303 if (! empty($cfg['MemoryLimit'])) {
304 @ini_set('memory_limit', $cfg['MemoryLimit']);
306 register_shutdown_function('PMA_shutdownDuringExport');
307 // Start with empty buffer
308 $dump_buffer = '';
309 $dump_buffer_len = 0;
311 // Array of dump_buffers - used in separate file exports
312 $dump_buffer_objects = array();
314 // We send fake headers to avoid browser timeout when buffering
315 $time_start = time();
317 // Defines the default <CR><LF> format.
318 // For SQL always use \n as MySQL wants this on all platforms.
319 if ($what == 'sql') {
320 $crlf = "\n";
321 } else {
322 $crlf = PMA\libraries\Util::whichCrlf();
325 $output_kanji_conversion = Encoding::canConvertKanji() && $type != 'xls';
327 // Do we need to convert charset?
328 $output_charset_conversion = $asfile
329 && Encoding::isSupported()
330 && isset($charset) && $charset != 'utf-8'
331 && $type != 'xls';
333 // Use on the fly compression?
334 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
335 && $compression == 'gzip';
336 if ($GLOBALS['onfly_compression']) {
337 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
340 // Generate filename and mime type if needed
341 if ($asfile) {
342 if (empty($remember_template)) {
343 $remember_template = '';
345 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
346 $export_type, $remember_template, $export_plugin, $compression,
347 $filename_template
349 } else {
350 $mime_type = '';
353 // Open file on server if needed
354 if ($save_on_server) {
355 list($save_filename, $message, $file_handle) = PMA_openExportFile(
356 $filename, $quick_export
359 // problem opening export file on server?
360 if (! empty($message)) {
361 PMA_showExportPage($db, $table, $export_type);
363 } else {
365 * Send headers depending on whether the user chose to download a dump file
366 * or not
368 if ($asfile) {
369 // Download
370 // (avoid rewriting data containing HTML with anchors and forms;
371 // this was reported to happen under Plesk)
372 @ini_set('url_rewriter.tags', '');
373 $filename = Sanitize::sanitizeFilename($filename);
375 PMA_downloadHeader($filename, $mime_type);
376 } else {
377 // HTML
378 if ($export_type == 'database') {
379 $num_tables = count($tables);
380 if ($num_tables == 0) {
381 $message = PMA\libraries\Message::error(
382 __('No tables found in database.')
384 $active_page = 'db_export.php';
385 include 'db_export.php';
386 exit();
389 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
390 $export_type, $db, $table
392 echo $html;
393 unset($html);
394 } // end download
397 // Fake loop just to allow skip of remain of this code by break, I'd really
398 // need exceptions here :-)
399 do {
400 // Re - initialize
401 $dump_buffer = '';
402 $dump_buffer_len = 0;
404 // Add possibly some comments to export
405 if (! $export_plugin->exportHeader()) {
406 break;
409 // Will we need relation & co. setup?
410 $do_relation = isset($GLOBALS[$what . '_relation']);
411 $do_comments = isset($GLOBALS[$what . '_include_comments'])
412 || isset($GLOBALS[$what . '_comments']);
413 $do_mime = isset($GLOBALS[$what . '_mime']);
414 if ($do_relation || $do_comments || $do_mime) {
415 $cfgRelation = PMA_getRelationsParam();
417 if ($do_mime) {
418 include_once 'libraries/transformations.lib.php';
421 // Include dates in export?
422 $do_dates = isset($GLOBALS[$what . '_dates']);
424 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
427 * Builds the dump
429 if ($export_type == 'server') {
430 if (! isset($db_select)) {
431 $db_select = '';
433 PMA_exportServer(
434 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
435 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
436 $aliases, $separate_files
438 } elseif ($export_type == 'database') {
439 if (!isset($table_structure) || !is_array($table_structure)) {
440 $table_structure = array();
442 if (!isset($table_data) || !is_array($table_data)) {
443 $table_data = array();
445 if (!empty($_REQUEST['structure_or_data_forced'])) {
446 $table_structure = $tables;
447 $table_data = $tables;
449 if (isset($lock_tables)) {
450 PMA_lockTables($db, $tables, "READ");
451 try {
452 PMA_exportDatabase(
453 $db, $tables, $whatStrucOrData, $table_structure,
454 $table_data, $export_plugin, $crlf, $err_url, $export_type,
455 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
456 $separate_files
458 } finally {
459 PMA_unlockTables();
461 } else {
462 PMA_exportDatabase(
463 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
464 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
465 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
468 } else {
469 // We export just one table
470 // $allrows comes from the form when "Dump all rows" has been selected
471 if (! isset($allrows)) {
472 $allrows = '';
474 if (! isset($limit_to)) {
475 $limit_to = 0;
477 if (! isset($limit_from)) {
478 $limit_from = 0;
480 if (isset($lock_tables)) {
481 try {
482 PMA_lockTables($db, array($table), "READ");
483 PMA_exportTable(
484 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
485 $err_url, $export_type, $do_relation, $do_comments,
486 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
487 $sql_query, $aliases
489 } finally {
490 PMA_unlockTables();
492 } else {
493 PMA_exportTable(
494 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
495 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
496 $allrows, $limit_to, $limit_from, $sql_query, $aliases
500 if (! $export_plugin->exportFooter()) {
501 break;
504 } while (false);
505 // End of fake loop
507 if ($save_on_server && ! empty($message)) {
508 PMA_showExportPage($db, $table, $export_type);
512 * Send the dump as a file...
514 if (empty($asfile)) {
515 echo PMA_getHtmlForDisplayedExportFooter($back_button);
516 return;
517 } // end if
519 // Convert the charset if required.
520 if ($output_charset_conversion) {
521 $dump_buffer = Encoding::convertString(
522 'utf-8',
523 $GLOBALS['charset'],
524 $dump_buffer
528 // Compression needed?
529 if ($compression) {
530 if (! empty($separate_files)) {
531 $dump_buffer = PMA_compressExport(
532 $dump_buffer_objects, $compression, $filename
534 } else {
535 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
540 /* If we saved on server, we have to close file now */
541 if ($save_on_server) {
542 $message = PMA_closeExportFile(
543 $file_handle, $dump_buffer, $save_filename
545 PMA_showExportPage($db, $table, $export_type);
546 } else {
547 echo $dump_buffer;