2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
8 use PMA\libraries\plugins\ExportPlugin
;
10 use PMA\libraries\Sanitize
;
13 * Get the variables sent or posted to this script and a core script
15 if (!defined('TESTSUITE')) {
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']);
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
70 'htmlword_structure_or_data',
74 'mediawiki_structure_or_data',
76 'pdf_structure_or_data',
77 'odt_structure_or_data',
83 'codegen_structure_or_data',
89 'excel_structure_or_data',
90 'yaml_structure_or_data',
92 'ods_structure_or_data',
94 'json_structure_or_data',
96 'xml_structure_or_data',
98 'xml_export_functions',
99 'xml_export_procedures',
101 'xml_export_triggers',
103 'xml_export_contents',
104 'texytext_structure_or_data',
107 'phparray_structure_or_data',
108 'sql_include_comments',
109 'sql_header_comment',
113 'sql_use_transaction',
116 'sql_structure_or_data',
117 'sql_create_database',
119 'sql_procedure_function',
122 'sql_create_trigger',
124 'sql_auto_increment',
131 'sql_max_query_size',
132 'sql_hex_for_binary',
135 'sql_views_as_tables',
144 'csv_structure_or_data',
145 // csv_replace should have been here but we use it directly from $_POST
147 'latex_structure_or_data',
148 'latex_structure_caption',
149 'latex_structure_continued_caption',
150 'latex_structure_label',
155 'latex_data_caption',
156 'latex_data_continued_caption',
162 foreach ($post_params as $one_post_param) {
163 if (isset($_POST[$one_post_param])) {
164 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
168 $table = $GLOBALS['table'];
170 PMA\libraries\Util
::checkParameters(array('what', 'export_type'));
172 // sanitize this parameter which will be used below in a file inclusion
173 $what = PMA_securePath($_POST['what']);
175 // export class instance, not array of properties, as before
176 /* @var $export_plugin ExportPlugin */
177 $export_plugin = PMA_getPlugin(
180 'libraries/plugins/export/',
182 'export_type' => $export_type,
183 'single_table' => isset($single_table)
187 // Backward compatibility
191 if (empty($export_plugin)) {
192 PMA_fatalError(__('Bad type!'));
196 * valid compression methods
198 $compression_methods = array(
204 * init and variable checking
206 $compression = false;
208 $save_on_server = false;
209 $buffer_needed = false;
215 $separate_files = '';
217 // Is it a quick or custom export?
218 if (isset($_REQUEST['quick_or_custom'])
219 && $_REQUEST['quick_or_custom'] == 'quick'
221 $quick_export = true;
223 $quick_export = false;
226 if ($_REQUEST['output_format'] == 'astext') {
230 if (isset($_REQUEST['as_separate_files'])
231 && ! empty($_REQUEST['as_separate_files'])
233 if (isset($_REQUEST['compression'])
234 && ! empty($_REQUEST['compression'])
235 && $_REQUEST['compression'] == 'zip'
237 $separate_files = $_REQUEST['as_separate_files'];
240 if (in_array($_REQUEST['compression'], $compression_methods)) {
241 $compression = $_REQUEST['compression'];
242 $buffer_needed = true;
244 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
245 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
248 $onserver = $_REQUEST['quick_export_onserver'];
250 $onserver = $_REQUEST['onserver'];
252 // Will we save dump on server?
253 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
257 // Generate error url and check for needed variables
258 if ($export_type == 'server') {
259 $err_url = 'server_export.php' . URL
::getCommon();
260 } elseif ($export_type == 'database'
263 $err_url = 'db_export.php' . URL
::getCommon(array('db' => $db));
264 // Check if we have something to export
265 if (isset($table_select)) {
266 $tables = $table_select;
270 } elseif ($export_type == 'table' && mb_strlen($db)
273 $err_url = 'tbl_export.php' . URL
::getCommon(
275 'db' => $db, 'table' => $table
279 PMA_fatalError(__('Bad parameters!'));
282 // Merge SQL Query aliases with Export aliases from
283 // export page, Export page aliases are given more
284 // preference over SQL Query aliases.
285 $parser = new SqlParser\
Parser($sql_query);
287 if ((!empty($parser->statements
[0]))
288 && ($parser->statements
[0] instanceof SqlParser\Statements\SelectStatement
)
290 if (!empty($_REQUEST['aliases'])) {
291 $aliases = PMA_mergeAliases(
292 SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db),
295 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
297 $aliases = SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
302 * Increase time limit for script execution and initializes some variables
304 @set_time_limit
($cfg['ExecTimeLimit']);
305 if (! empty($cfg['MemoryLimit'])) {
306 @ini_set
('memory_limit', $cfg['MemoryLimit']);
308 register_shutdown_function('PMA_shutdownDuringExport');
309 // Start with empty buffer
311 $dump_buffer_len = 0;
313 // Array of dump_buffers - used in separate file exports
314 $dump_buffer_objects = array();
316 // We send fake headers to avoid browser timeout when buffering
317 $time_start = time();
319 // Defines the default <CR><LF> format.
320 // For SQL always use \n as MySQL wants this on all platforms.
321 if ($what == 'sql') {
324 $crlf = PMA\libraries\Util
::whichCrlf();
327 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
330 // Do we need to convert charset?
331 $output_charset_conversion = $asfile
332 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
333 && isset($charset) && $charset != 'utf-8'
336 // Use on the fly compression?
337 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
338 && $compression == 'gzip';
339 if ($GLOBALS['onfly_compression']) {
340 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
343 // Generate filename and mime type if needed
345 if (empty($remember_template)) {
346 $remember_template = '';
348 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
349 $export_type, $remember_template, $export_plugin, $compression,
356 // Open file on server if needed
357 if ($save_on_server) {
358 list($save_filename, $message, $file_handle) = PMA_openExportFile(
359 $filename, $quick_export
362 // problem opening export file on server?
363 if (! empty($message)) {
364 PMA_showExportPage($db, $table, $export_type);
368 * Send headers depending on whether the user chose to download a dump file
373 // (avoid rewriting data containing HTML with anchors and forms;
374 // this was reported to happen under Plesk)
375 @ini_set
('url_rewriter.tags', '');
376 $filename = Sanitize
::sanitizeFilename($filename);
378 PMA_downloadHeader($filename, $mime_type);
381 if ($export_type == 'database') {
382 $num_tables = count($tables);
383 if ($num_tables == 0) {
384 $message = PMA\libraries\Message
::error(
385 __('No tables found in database.')
387 $active_page = 'db_export.php';
388 include 'db_export.php';
392 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
393 $export_type, $db, $table
400 // Fake loop just to allow skip of remain of this code by break, I'd really
401 // need exceptions here :-)
405 $dump_buffer_len = 0;
407 // Add possibly some comments to export
408 if (! $export_plugin->exportHeader()) {
412 // Will we need relation & co. setup?
413 $do_relation = isset($GLOBALS[$what . '_relation']);
414 $do_comments = isset($GLOBALS[$what . '_include_comments'])
415 ||
isset($GLOBALS[$what . '_comments']);
416 $do_mime = isset($GLOBALS[$what . '_mime']);
417 if ($do_relation ||
$do_comments ||
$do_mime) {
418 $cfgRelation = PMA_getRelationsParam();
421 include_once 'libraries/transformations.lib.php';
424 // Include dates in export?
425 $do_dates = isset($GLOBALS[$what . '_dates']);
427 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
432 if ($export_type == 'server') {
433 if (! isset($db_select)) {
437 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
438 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
439 $aliases, $separate_files
441 } elseif ($export_type == 'database') {
442 if (!isset($table_structure) ||
!is_array($table_structure)) {
443 $table_structure = array();
445 if (!isset($table_data) ||
!is_array($table_data)) {
446 $table_data = array();
448 if (!empty($_REQUEST['structure_or_data_forced'])) {
449 $table_structure = $tables;
450 $table_data = $tables;
452 if (isset($lock_tables)) {
453 PMA_lockTables($db, $tables, "READ");
456 $db, $tables, $whatStrucOrData, $table_structure,
457 $table_data, $export_plugin, $crlf, $err_url, $export_type,
458 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
466 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
467 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
468 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
472 // We export just one table
473 // $allrows comes from the form when "Dump all rows" has been selected
474 if (! isset($allrows)) {
477 if (! isset($limit_to)) {
480 if (! isset($limit_from)) {
483 if (isset($lock_tables)) {
485 PMA_lockTables($db, array($table), "READ");
487 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
488 $err_url, $export_type, $do_relation, $do_comments,
489 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
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()) {
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);
522 // Convert the charset if required.
523 if ($output_charset_conversion) {
524 $dump_buffer = PMA_convertString(
531 // Compression needed?
533 if (! empty($separate_files)) {
534 $dump_buffer = PMA_compressExport(
535 $dump_buffer_objects, $compression, $filename
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);