Continue refactoring db tracking
[phpmyadmin.git] / export.php
blob70c304f62d07a483018bb2d3b4041be0f5d7cd75
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 '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_statements',
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'];
167 // sanitize this parameter which will be used below in a file inclusion
168 $what = PMA_securePath($_POST['what']);
170 PMA_Util::checkParameters(array('what', 'export_type'));
172 // export class instance, not array of properties, as before
173 /* @var $export_plugin ExportPlugin */
174 $export_plugin = PMA_getPlugin(
175 "export",
176 $what,
177 'libraries/plugins/export/',
178 array(
179 'export_type' => $export_type,
180 'single_table' => isset($single_table)
184 // Backward compatibility
185 $type = $what;
187 // Check export type
188 if (empty($export_plugin)) {
189 PMA_fatalError(__('Bad type!'));
193 * valid compression methods
195 $compression_methods = array(
196 'zip',
197 'gzip'
201 * init and variable checking
203 $compression = false;
204 $onserver = false;
205 $save_on_server = false;
206 $buffer_needed = false;
207 $back_button = '';
208 $save_filename = '';
209 $file_handle = '';
210 $err_url = '';
211 $filename = '';
212 $separate_files = '';
214 // Is it a quick or custom export?
215 if ($_REQUEST['quick_or_custom'] == 'quick') {
216 $quick_export = true;
217 } else {
218 $quick_export = false;
221 if ($_REQUEST['output_format'] == 'astext') {
222 $asfile = false;
223 } else {
224 $asfile = true;
225 if (isset($_REQUEST['as_separate_files'])
226 && ! empty($_REQUEST['as_separate_files'])
228 if (isset($_REQUEST['compression'])
229 && ! empty($_REQUEST['compression'])
230 && $_REQUEST['compression'] == 'zip'
232 $separate_files = $_REQUEST['as_separate_files'];
235 if (in_array($_REQUEST['compression'], $compression_methods)) {
236 $compression = $_REQUEST['compression'];
237 $buffer_needed = true;
239 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
240 || (! $quick_export && ! empty($_REQUEST['onserver']))
242 if ($quick_export) {
243 $onserver = $_REQUEST['quick_export_onserver'];
244 } else {
245 $onserver = $_REQUEST['onserver'];
247 // Will we save dump on server?
248 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
252 // Generate error url and check for needed variables
253 /** @var PMA_String $pmaString */
254 $pmaString = $GLOBALS['PMA_String'];
255 if ($export_type == 'server') {
256 $err_url = 'server_export.php' . PMA_URL_getCommon();
257 } elseif ($export_type == 'database'
258 && /*overload*/mb_strlen($db)
260 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
261 // Check if we have something to export
262 if (isset($table_select)) {
263 $tables = $table_select;
264 } else {
265 $tables = array();
267 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
268 && /*overload*/mb_strlen($table)
270 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
271 array(
272 'db' => $db, 'table' => $table
275 } else {
276 PMA_fatalError(__('Bad parameters!'));
279 // Merge SQL Query aliases with Export aliases from
280 // export page, Export page aliases are given more
281 // preference over SQL Query aliases.
282 $parser = new SqlParser\Parser($sql_query);
283 $aliases = array();
284 if ((!empty($parser->statements[0]))
285 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
287 if (!empty($_REQUEST['aliases'])) {
288 $aliases = PMA_mergeAliases(
289 SqlParser\Utils\Misc::getAliases($parser->statements[0], $db),
290 $_REQUEST['aliases']
292 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
293 } else {
294 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
299 * Increase time limit for script execution and initializes some variables
301 @set_time_limit($cfg['ExecTimeLimit']);
302 if (! empty($cfg['MemoryLimit'])) {
303 @ini_set('memory_limit', $cfg['MemoryLimit']);
305 register_shutdown_function('PMA_shutdownDuringExport');
306 // Start with empty buffer
307 $dump_buffer = '';
308 $dump_buffer_len = 0;
310 // Array of dump_buffers - used in separate file exports
311 $dump_buffer_objects = array();
313 // We send fake headers to avoid browser timeout when buffering
314 $time_start = time();
316 // Defines the default <CR><LF> format.
317 // For SQL always use \n as MySQL wants this on all platforms.
318 if ($what == 'sql') {
319 $crlf = "\n";
320 } else {
321 $crlf = PMA_Util::whichCrlf();
324 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
325 && $type != 'xls';
327 // Do we need to convert charset?
328 $output_charset_conversion = $asfile
329 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
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 = PMA_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_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 {
401 // Add possibly some comments to export
402 if (! $export_plugin->exportHeader()) {
403 break;
405 // Re - initialize
406 $dump_buffer = '';
407 $dump_buffer_len = 0;
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 PMA_unlockTables();
459 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
460 PMA_unlockTables();
461 throw $e;
463 } else {
464 PMA_exportDatabase(
465 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
466 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
467 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
470 } else {
471 // We export just one table
472 // $allrows comes from the form when "Dump all rows" has been selected
473 if (! isset($allrows)) {
474 $allrows = '';
476 if (! isset($limit_to)) {
477 $limit_to = 0;
479 if (! isset($limit_from)) {
480 $limit_from = 0;
482 if (isset($lock_tables)) {
483 try {
484 PMA_lockTables($db, array($table), "READ");
485 PMA_exportTable(
486 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
487 $err_url, $export_type, $do_relation, $do_comments,
488 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
489 $sql_query, $aliases
491 PMA_unlockTables();
492 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
493 PMA_unlockTables();
494 throw $e;
496 } else {
497 PMA_exportTable(
498 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
499 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
500 $allrows, $limit_to, $limit_from, $sql_query, $aliases
504 if (! $export_plugin->exportFooter()) {
505 break;
508 } while (false);
509 // End of fake loop
511 if ($save_on_server && ! empty($message)) {
512 PMA_showExportPage($db, $table, $export_type);
516 * Send the dump as a file...
518 if (empty($asfile)) {
519 echo PMA_getHtmlForDisplayedExportFooter($back_button);
520 return;
521 } // end if
523 // Convert the charset if required.
524 if ($output_charset_conversion) {
525 $dump_buffer = PMA_convertString(
526 'utf-8',
527 $GLOBALS['charset'],
528 $dump_buffer
532 // Compression needed?
533 if ($compression) {
534 if (! empty($separate_files)) {
535 $dump_buffer = PMA_compressExport(
536 $dump_buffer_objects, $compression, $filename
538 } else {
539 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
544 /* If we saved on server, we have to close file now */
545 if ($save_on_server) {
546 $message = PMA_closeExportFile(
547 $file_handle, $dump_buffer, $save_filename
549 PMA_showExportPage($db, $table, $export_type);
550 } else {
551 echo $dump_buffer;