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
67 'htmlword_structure_or_data',
71 'mediawiki_structure_or_data',
73 'pdf_structure_or_data',
74 'odt_structure_or_data',
80 'codegen_structure_or_data',
86 'excel_structure_or_data',
87 'yaml_structure_or_data',
89 'ods_structure_or_data',
91 'json_structure_or_data',
93 'xml_structure_or_data',
95 'xml_export_functions',
96 'xml_export_procedures',
98 'xml_export_triggers',
100 'xml_export_contents',
101 'texytext_structure_or_data',
104 'phparray_structure_or_data',
105 'sql_include_comments',
106 'sql_header_comment',
110 'sql_use_transaction',
113 'sql_structure_or_data',
114 'sql_create_database',
116 'sql_procedure_function',
119 'sql_create_trigger',
121 'sql_auto_increment',
128 'sql_max_query_size',
129 'sql_hex_for_binary',
132 'sql_views_as_tables',
141 'csv_structure_or_data',
142 // csv_replace should have been here but we use it directly from $_POST
144 'latex_structure_or_data',
145 'latex_structure_caption',
146 'latex_structure_continued_caption',
147 'latex_structure_label',
152 'latex_data_caption',
153 'latex_data_continued_caption',
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(
176 'libraries/plugins/export/',
178 'export_type' => $export_type,
179 'single_table' => isset($single_table)
183 // Backward compatibility
187 if (empty($export_plugin)) {
188 PMA_fatalError(__('Bad type!'));
192 * valid compression methods
194 $compression_methods = array(
200 * init and variable checking
202 $compression = false;
204 $save_on_server = false;
205 $buffer_needed = false;
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;
219 $quick_export = false;
222 if ($_REQUEST['output_format'] == 'astext') {
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']))
244 $onserver = $_REQUEST['quick_export_onserver'];
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'
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;
266 } elseif ($export_type == 'table' && mb_strlen($db)
269 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
271 'db' => $db, 'table' => $table
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);
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),
291 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
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
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') {
320 $crlf = PMA\libraries\Util
::whichCrlf();
323 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
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'
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
341 if (empty($remember_template)) {
342 $remember_template = '';
344 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
345 $export_type, $remember_template, $export_plugin, $compression,
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);
364 * Send headers depending on whether the user chose to download a dump file
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);
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';
388 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
389 $export_type, $db, $table
396 // Fake loop just to allow skip of remain of this code by break, I'd really
397 // need exceptions here :-)
401 $dump_buffer_len = 0;
403 // Add possibly some comments to export
404 if (! $export_plugin->exportHeader()) {
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();
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'];
428 if ($export_type == 'server') {
429 if (! isset($db_select)) {
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");
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,
458 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
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
470 // We export just one table
471 // $allrows comes from the form when "Dump all rows" has been selected
472 if (! isset($allrows)) {
475 if (! isset($limit_to)) {
478 if (! isset($limit_from)) {
481 if (isset($lock_tables)) {
483 PMA_lockTables($db, array($table), "READ");
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,
491 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
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);