Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / export.php
blob75fc590d59aaf2ce8de6c37e3271386ade185d19
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 'single_table',
47 'export_type',
48 'export_method',
49 'quick_or_custom',
50 'db_select',
51 'table_select',
52 'table_structure',
53 'table_data',
54 'limit_to',
55 'limit_from',
56 'allrows',
57 'lock_tables',
58 'output_format',
59 'filename_template',
60 'maxsize',
61 'remember_template',
62 'charset',
63 'compression',
64 'as_separate_files',
65 'knjenc',
66 'xkana',
67 'htmlword_structure_or_data',
68 'htmlword_null',
69 'htmlword_columns',
70 'mediawiki_headers',
71 'mediawiki_structure_or_data',
72 'mediawiki_caption',
73 'pdf_structure_or_data',
74 'odt_structure_or_data',
75 'odt_relation',
76 'odt_comments',
77 'odt_mime',
78 'odt_columns',
79 'odt_null',
80 'codegen_structure_or_data',
81 'codegen_format',
82 'excel_null',
83 'excel_removeCRLF',
84 'excel_columns',
85 'excel_edition',
86 'excel_structure_or_data',
87 'yaml_structure_or_data',
88 'ods_null',
89 'ods_structure_or_data',
90 'ods_columns',
91 'json_structure_or_data',
92 'json_pretty_print',
93 'xml_structure_or_data',
94 'xml_export_events',
95 'xml_export_functions',
96 'xml_export_procedures',
97 'xml_export_tables',
98 'xml_export_triggers',
99 'xml_export_views',
100 'xml_export_contents',
101 'texytext_structure_or_data',
102 'texytext_columns',
103 'texytext_null',
104 'phparray_structure_or_data',
105 'sql_include_comments',
106 'sql_header_comment',
107 'sql_dates',
108 'sql_relation',
109 'sql_mime',
110 'sql_use_transaction',
111 'sql_disable_fk',
112 'sql_compatibility',
113 'sql_structure_or_data',
114 'sql_create_database',
115 'sql_drop_table',
116 'sql_procedure_function',
117 'sql_create_table',
118 'sql_create_view',
119 'sql_create_trigger',
120 'sql_if_not_exists',
121 'sql_auto_increment',
122 'sql_backquotes',
123 'sql_truncate',
124 'sql_delayed',
125 'sql_ignore',
126 'sql_type',
127 'sql_insert_syntax',
128 'sql_max_query_size',
129 'sql_hex_for_binary',
130 'sql_utc_time',
131 'sql_drop_database',
132 'sql_views_as_tables',
133 'sql_metadata',
134 'csv_separator',
135 'csv_enclosed',
136 'csv_escaped',
137 'csv_terminated',
138 'csv_null',
139 'csv_removeCRLF',
140 'csv_columns',
141 'csv_structure_or_data',
142 // csv_replace should have been here but we use it directly from $_POST
143 'latex_caption',
144 'latex_structure_or_data',
145 'latex_structure_caption',
146 'latex_structure_continued_caption',
147 'latex_structure_label',
148 'latex_relation',
149 'latex_comments',
150 'latex_mime',
151 'latex_columns',
152 'latex_data_caption',
153 'latex_data_continued_caption',
154 'latex_data_label',
155 'latex_null',
156 'aliases'
159 foreach ($post_params as $one_post_param) {
160 if (isset($_POST[$one_post_param])) {
161 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
165 $table = $GLOBALS['table'];
166 // sanitize this parameter which will be used below in a file inclusion
167 $what = PMA_securePath($_POST['what']);
169 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
171 // export class instance, not array of properties, as before
172 /* @var $export_plugin ExportPlugin */
173 $export_plugin = PMA_getPlugin(
174 "export",
175 $what,
176 'libraries/plugins/export/',
177 array(
178 'export_type' => $export_type,
179 'single_table' => isset($single_table)
183 // Backward compatibility
184 $type = $what;
186 // Check export type
187 if (empty($export_plugin)) {
188 PMA_fatalError(__('Bad type!'));
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 = '';
211 $separate_files = '';
213 // Is it a quick or custom export?
214 if (isset($_REQUEST['quick_or_custom'])
215 && $_REQUEST['quick_or_custom'] == 'quick'
217 $quick_export = true;
218 } else {
219 $quick_export = false;
222 if ($_REQUEST['output_format'] == 'astext') {
223 $asfile = false;
224 } else {
225 $asfile = true;
226 if (isset($_REQUEST['as_separate_files'])
227 && ! empty($_REQUEST['as_separate_files'])
229 if (isset($_REQUEST['compression'])
230 && ! empty($_REQUEST['compression'])
231 && $_REQUEST['compression'] == 'zip'
233 $separate_files = $_REQUEST['as_separate_files'];
236 if (in_array($_REQUEST['compression'], $compression_methods)) {
237 $compression = $_REQUEST['compression'];
238 $buffer_needed = true;
240 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
241 || (! $quick_export && ! empty($_REQUEST['onserver']))
243 if ($quick_export) {
244 $onserver = $_REQUEST['quick_export_onserver'];
245 } else {
246 $onserver = $_REQUEST['onserver'];
248 // Will we save dump on server?
249 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
253 // Generate error url and check for needed variables
254 if ($export_type == 'server') {
255 $err_url = 'server_export.php' . PMA_URL_getCommon();
256 } elseif ($export_type == 'database'
257 && mb_strlen($db)
259 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
260 // Check if we have something to export
261 if (isset($table_select)) {
262 $tables = $table_select;
263 } else {
264 $tables = array();
266 } elseif ($export_type == 'table' && mb_strlen($db)
267 && mb_strlen($table)
269 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
270 array(
271 'db' => $db, 'table' => $table
274 } else {
275 PMA_fatalError(__('Bad parameters!'));
278 // Merge SQL Query aliases with Export aliases from
279 // export page, Export page aliases are given more
280 // preference over SQL Query aliases.
281 $parser = new SqlParser\Parser($sql_query);
282 $aliases = array();
283 if ((!empty($parser->statements[0]))
284 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
286 if (!empty($_REQUEST['aliases'])) {
287 $aliases = PMA_mergeAliases(
288 SqlParser\Utils\Misc::getAliases($parser->statements[0], $db),
289 $_REQUEST['aliases']
291 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
292 } else {
293 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
298 * Increase time limit for script execution and initializes some variables
300 @set_time_limit($cfg['ExecTimeLimit']);
301 if (! empty($cfg['MemoryLimit'])) {
302 @ini_set('memory_limit', $cfg['MemoryLimit']);
304 register_shutdown_function('PMA_shutdownDuringExport');
305 // Start with empty buffer
306 $dump_buffer = '';
307 $dump_buffer_len = 0;
309 // Array of dump_buffers - used in separate file exports
310 $dump_buffer_objects = array();
312 // We send fake headers to avoid browser timeout when buffering
313 $time_start = time();
315 // Defines the default <CR><LF> format.
316 // For SQL always use \n as MySQL wants this on all platforms.
317 if ($what == 'sql') {
318 $crlf = "\n";
319 } else {
320 $crlf = PMA\libraries\Util::whichCrlf();
323 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
324 && $type != 'xls';
326 // Do we need to convert charset?
327 $output_charset_conversion = $asfile
328 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
329 && isset($charset) && $charset != 'utf-8'
330 && $type != 'xls';
332 // Use on the fly compression?
333 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
334 && $compression == 'gzip';
335 if ($GLOBALS['onfly_compression']) {
336 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
339 // Generate filename and mime type if needed
340 if ($asfile) {
341 if (empty($remember_template)) {
342 $remember_template = '';
344 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
345 $export_type, $remember_template, $export_plugin, $compression,
346 $filename_template
348 } else {
349 $mime_type = '';
352 // Open file on server if needed
353 if ($save_on_server) {
354 list($save_filename, $message, $file_handle) = PMA_openExportFile(
355 $filename, $quick_export
358 // problem opening export file on server?
359 if (! empty($message)) {
360 PMA_showExportPage($db, $table, $export_type);
362 } else {
364 * Send headers depending on whether the user chose to download a dump file
365 * or not
367 if ($asfile) {
368 // Download
369 // (avoid rewriting data containing HTML with anchors and forms;
370 // this was reported to happen under Plesk)
371 @ini_set('url_rewriter.tags', '');
372 $filename = PMA_sanitizeFilename($filename);
374 PMA_downloadHeader($filename, $mime_type);
375 } else {
376 // HTML
377 if ($export_type == 'database') {
378 $num_tables = count($tables);
379 if ($num_tables == 0) {
380 $message = PMA\libraries\Message::error(
381 __('No tables found in database.')
383 $active_page = 'db_export.php';
384 include 'db_export.php';
385 exit();
388 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
389 $export_type, $db, $table
391 echo $html;
392 unset($html);
393 } // end download
396 // Fake loop just to allow skip of remain of this code by break, I'd really
397 // need exceptions here :-)
398 do {
399 // Re - initialize
400 $dump_buffer = '';
401 $dump_buffer_len = 0;
403 // Add possibly some comments to export
404 if (! $export_plugin->exportHeader()) {
405 break;
408 // Will we need relation & co. setup?
409 $do_relation = isset($GLOBALS[$what . '_relation']);
410 $do_comments = isset($GLOBALS[$what . '_include_comments'])
411 || isset($GLOBALS[$what . '_comments']);
412 $do_mime = isset($GLOBALS[$what . '_mime']);
413 if ($do_relation || $do_comments || $do_mime) {
414 $cfgRelation = PMA_getRelationsParam();
416 if ($do_mime) {
417 include_once 'libraries/transformations.lib.php';
420 // Include dates in export?
421 $do_dates = isset($GLOBALS[$what . '_dates']);
423 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
426 * Builds the dump
428 if ($export_type == 'server') {
429 if (! isset($db_select)) {
430 $db_select = '';
432 PMA_exportServer(
433 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
434 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
435 $aliases, $separate_files
437 } elseif ($export_type == 'database') {
438 if (!isset($table_structure) || !is_array($table_structure)) {
439 $table_structure = array();
441 if (!isset($table_data) || !is_array($table_data)) {
442 $table_data = array();
444 if (!empty($_REQUEST['structure_or_data_forced'])) {
445 $table_structure = $tables;
446 $table_data = $tables;
448 if (isset($lock_tables)) {
449 PMA_lockTables($db, $tables, "READ");
450 try {
451 PMA_exportDatabase(
452 $db, $tables, $whatStrucOrData, $table_structure,
453 $table_data, $export_plugin, $crlf, $err_url, $export_type,
454 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
455 $separate_files
457 PMA_unlockTables();
458 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
459 PMA_unlockTables();
460 throw $e;
462 } else {
463 PMA_exportDatabase(
464 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
465 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
466 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
469 } else {
470 // We export just one table
471 // $allrows comes from the form when "Dump all rows" has been selected
472 if (! isset($allrows)) {
473 $allrows = '';
475 if (! isset($limit_to)) {
476 $limit_to = 0;
478 if (! isset($limit_from)) {
479 $limit_from = 0;
481 if (isset($lock_tables)) {
482 try {
483 PMA_lockTables($db, array($table), "READ");
484 PMA_exportTable(
485 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
486 $err_url, $export_type, $do_relation, $do_comments,
487 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
488 $sql_query, $aliases
490 PMA_unlockTables();
491 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
492 PMA_unlockTables();
493 throw $e;
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;