2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
8 use PMA\libraries\plugins\ExportPlugin
;
11 * Get the variables sent or posted to this script and a core script
13 if (!defined('TESTSUITE')) {
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']);
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
68 'htmlword_structure_or_data',
72 'mediawiki_structure_or_data',
74 'pdf_structure_or_data',
75 'odt_structure_or_data',
81 'codegen_structure_or_data',
87 'excel_structure_or_data',
88 'yaml_structure_or_data',
90 'ods_structure_or_data',
92 'json_structure_or_data',
94 'xml_structure_or_data',
96 'xml_export_functions',
97 'xml_export_procedures',
99 'xml_export_triggers',
101 'xml_export_contents',
102 'texytext_structure_or_data',
105 'phparray_structure_or_data',
106 'sql_include_comments',
107 'sql_header_comment',
111 'sql_use_transaction',
114 'sql_structure_or_data',
115 'sql_create_database',
117 'sql_procedure_function',
120 'sql_create_trigger',
122 'sql_auto_increment',
129 'sql_max_query_size',
130 'sql_hex_for_binary',
133 'sql_views_as_tables',
142 'csv_structure_or_data',
143 // csv_replace should have been here but we use it directly from $_POST
145 'latex_structure_or_data',
146 'latex_structure_caption',
147 'latex_structure_continued_caption',
148 'latex_structure_label',
153 'latex_data_caption',
154 'latex_data_continued_caption',
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'];
168 PMA\libraries\Util
::checkParameters(array('what', 'export_type'));
170 // sanitize this parameter which will be used below in a file inclusion
171 $what = PMA_securePath($_POST['what']);
173 // export class instance, not array of properties, as before
174 /* @var $export_plugin ExportPlugin */
175 $export_plugin = PMA_getPlugin(
178 'libraries/plugins/export/',
180 'export_type' => $export_type,
181 'single_table' => isset($single_table)
185 // Backward compatibility
189 if (empty($export_plugin)) {
190 PMA_fatalError(__('Bad type!'));
194 * valid compression methods
196 $compression_methods = array(
202 * init and variable checking
204 $compression = false;
206 $save_on_server = false;
207 $buffer_needed = false;
213 $separate_files = '';
215 // Is it a quick or custom export?
216 if (isset($_REQUEST['quick_or_custom'])
217 && $_REQUEST['quick_or_custom'] == 'quick'
219 $quick_export = true;
221 $quick_export = false;
224 if ($_REQUEST['output_format'] == 'astext') {
228 if (isset($_REQUEST['as_separate_files'])
229 && ! empty($_REQUEST['as_separate_files'])
231 if (isset($_REQUEST['compression'])
232 && ! empty($_REQUEST['compression'])
233 && $_REQUEST['compression'] == 'zip'
235 $separate_files = $_REQUEST['as_separate_files'];
238 if (in_array($_REQUEST['compression'], $compression_methods)) {
239 $compression = $_REQUEST['compression'];
240 $buffer_needed = true;
242 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
243 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
246 $onserver = $_REQUEST['quick_export_onserver'];
248 $onserver = $_REQUEST['onserver'];
250 // Will we save dump on server?
251 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
255 // Generate error url and check for needed variables
256 if ($export_type == 'server') {
257 $err_url = 'server_export.php' . PMA_URL_getCommon();
258 } elseif ($export_type == 'database'
261 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
262 // Check if we have something to export
263 if (isset($table_select)) {
264 $tables = $table_select;
268 } elseif ($export_type == 'table' && mb_strlen($db)
271 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
273 'db' => $db, 'table' => $table
277 PMA_fatalError(__('Bad parameters!'));
280 // Merge SQL Query aliases with Export aliases from
281 // export page, Export page aliases are given more
282 // preference over SQL Query aliases.
283 $parser = new SqlParser\
Parser($sql_query);
285 if ((!empty($parser->statements
[0]))
286 && ($parser->statements
[0] instanceof SqlParser\Statements\SelectStatement
)
288 $aliases = SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
290 if (!empty($_REQUEST['aliases'])) {
291 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
292 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
296 * Increase time limit for script execution and initializes some variables
298 @set_time_limit
($cfg['ExecTimeLimit']);
299 if (! empty($cfg['MemoryLimit'])) {
300 @ini_set
('memory_limit', $cfg['MemoryLimit']);
302 register_shutdown_function('PMA_shutdownDuringExport');
303 // Start with empty buffer
305 $dump_buffer_len = 0;
307 // Array of dump_buffers - used in separate file exports
308 $dump_buffer_objects = array();
310 // We send fake headers to avoid browser timeout when buffering
311 $time_start = time();
313 // Defines the default <CR><LF> format.
314 // For SQL always use \n as MySQL wants this on all platforms.
315 if ($what == 'sql') {
318 $crlf = PMA\libraries\Util
::whichCrlf();
321 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
324 // Do we need to convert charset?
325 $output_charset_conversion = $asfile
326 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
327 && isset($charset) && $charset != 'utf-8'
330 // Use on the fly compression?
331 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
332 && $compression == 'gzip';
333 if ($GLOBALS['onfly_compression']) {
334 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
337 // Generate filename and mime type if needed
339 if (empty($remember_template)) {
340 $remember_template = '';
342 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
343 $export_type, $remember_template, $export_plugin, $compression,
350 // Open file on server if needed
351 if ($save_on_server) {
352 list($save_filename, $message, $file_handle) = PMA_openExportFile(
353 $filename, $quick_export
356 // problem opening export file on server?
357 if (! empty($message)) {
358 PMA_showExportPage($db, $table, $export_type);
362 * Send headers depending on whether the user chose to download a dump file
367 // (avoid rewriting data containing HTML with anchors and forms;
368 // this was reported to happen under Plesk)
369 @ini_set
('url_rewriter.tags', '');
370 $filename = PMA_sanitizeFilename($filename);
372 PMA_downloadHeader($filename, $mime_type);
375 if ($export_type == 'database') {
376 $num_tables = count($tables);
377 if ($num_tables == 0) {
378 $message = PMA\libraries\Message
::error(
379 __('No tables found in database.')
381 $active_page = 'db_export.php';
382 include 'db_export.php';
386 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
387 $export_type, $db, $table
394 // Fake loop just to allow skip of remain of this code by break, I'd really
395 // need exceptions here :-)
399 $dump_buffer_len = 0;
401 // Add possibly some comments to export
402 if (! $export_plugin->exportHeader()) {
406 // Will we need relation & co. setup?
407 $do_relation = isset($GLOBALS[$what . '_relation']);
408 $do_comments = isset($GLOBALS[$what . '_include_comments'])
409 ||
isset($GLOBALS[$what . '_comments']);
410 $do_mime = isset($GLOBALS[$what . '_mime']);
411 if ($do_relation ||
$do_comments ||
$do_mime) {
412 $cfgRelation = PMA_getRelationsParam();
415 include_once 'libraries/transformations.lib.php';
418 // Include dates in export?
419 $do_dates = isset($GLOBALS[$what . '_dates']);
421 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
426 if ($export_type == 'server') {
427 if (! isset($db_select)) {
431 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
432 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
433 $aliases, $separate_files
435 } elseif ($export_type == 'database') {
436 if (!isset($table_structure) ||
!is_array($table_structure)) {
437 $table_structure = array();
439 if (!isset($table_data) ||
!is_array($table_data)) {
440 $table_data = array();
442 if (!empty($_REQUEST['structure_or_data_forced'])) {
443 $table_structure = $tables;
444 $table_data = $tables;
446 if (isset($lock_tables)) {
447 PMA_lockTables($db, $tables, "READ");
450 $db, $tables, $whatStrucOrData, $table_structure,
451 $table_data, $export_plugin, $crlf, $err_url, $export_type,
452 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
460 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
461 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
462 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
466 // We export just one table
467 // $allrows comes from the form when "Dump all rows" has been selected
468 if (! isset($allrows)) {
471 if (! isset($limit_to)) {
474 if (! isset($limit_from)) {
477 if (isset($lock_tables)) {
479 PMA_lockTables($db, array($table), "READ");
481 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
482 $err_url, $export_type, $do_relation, $do_comments,
483 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
491 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
492 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
493 $allrows, $limit_to, $limit_from, $sql_query, $aliases
497 if (! $export_plugin->exportFooter()) {
504 if ($save_on_server && ! empty($message)) {
505 PMA_showExportPage($db, $table, $export_type);
509 * Send the dump as a file...
511 if (empty($asfile)) {
512 echo PMA_getHtmlForDisplayedExportFooter($back_button);
516 // Convert the charset if required.
517 if ($output_charset_conversion) {
518 $dump_buffer = PMA_convertString(
525 // Compression needed?
527 if (! empty($separate_files)) {
528 $dump_buffer = PMA_compressExport(
529 $dump_buffer_objects, $compression, $filename
532 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
537 /* If we saved on server, we have to close file now */
538 if ($save_on_server) {
539 $message = PMA_closeExportFile(
540 $file_handle, $dump_buffer, $save_filename
542 PMA_showExportPage($db, $table, $export_type);