2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
10 * Get the variables sent or posted to this script and a core script
12 if (!defined('TESTSUITE')) {
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']);
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
63 'htmlword_structure_or_data',
67 'mediawiki_structure_or_data',
69 'pdf_structure_or_data',
70 'odt_structure_or_data',
76 'codegen_structure_or_data',
82 'excel_structure_or_data',
83 'yaml_structure_or_data',
85 'ods_structure_or_data',
87 'json_structure_or_data',
88 'xml_structure_or_data',
90 'xml_export_functions',
91 'xml_export_procedures',
93 'xml_export_triggers',
95 'xml_export_contents',
96 'texytext_structure_or_data',
99 'phparray_structure_or_data',
100 'sql_include_comments',
101 'sql_header_comment',
105 'sql_use_transaction',
108 'sql_structure_or_data',
109 'sql_create_database',
111 'sql_procedure_function',
112 'sql_create_table_statements',
115 'sql_create_trigger',
117 'sql_auto_increment',
124 'sql_max_query_size',
125 'sql_hex_for_binary',
128 'sql_views_as_tables',
136 'csv_structure_or_data',
137 // csv_replace should have been here but we use it directly from $_POST
139 'latex_structure_or_data',
140 'latex_structure_caption',
141 'latex_structure_continued_caption',
142 'latex_structure_label',
147 'latex_data_caption',
148 'latex_data_continued_caption',
154 foreach ($post_params as $one_post_param) {
155 if (isset($_POST[$one_post_param])) {
156 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
160 $table = $GLOBALS['table'];
161 // sanitize this parameter which will be used below in a file inclusion
162 $what = PMA_securePath($_POST['what']);
164 PMA_Util
::checkParameters(array('what', 'export_type'));
166 // export class instance, not array of properties, as before
167 $export_plugin = PMA_getPlugin(
170 'libraries/plugins/export/',
172 'export_type' => $export_type,
173 'single_table' => isset($single_table)
177 // Backward compatibility
181 if (! isset($export_plugin)) {
182 PMA_fatalError(__('Bad type!'));
185 // Avoid warning from PHP Analyzer
186 if (is_null($export_plugin)) {
187 $export_plugin = new stdClass();
191 * valid compression methods
193 $compression_methods = array(
199 * init and variable checking
201 $compression = false;
203 $save_on_server = false;
204 $buffer_needed = false;
211 // Is it a quick or custom export?
212 if ($_REQUEST['quick_or_custom'] == 'quick') {
213 $quick_export = true;
215 $quick_export = false;
218 if ($_REQUEST['output_format'] == 'astext') {
222 if (in_array($_REQUEST['compression'], $compression_methods)) {
223 $compression = $_REQUEST['compression'];
224 $buffer_needed = true;
226 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
227 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
230 $onserver = $_REQUEST['quick_export_onserver'];
232 $onserver = $_REQUEST['onserver'];
234 // Will we save dump on server?
235 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
239 // Generate error url and check for needed variables
240 /** @var PMA_String $pmaString */
241 $pmaString = $GLOBALS['PMA_String'];
242 if ($export_type == 'server') {
243 $err_url = 'server_export.php' . PMA_URL_getCommon();
244 } elseif ($export_type == 'database'
245 && /*overload*/mb_strlen($db)
247 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
248 // Check if we have something to export
249 if (isset($table_select)) {
250 $tables = $table_select;
254 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
255 && /*overload*/mb_strlen($table)
257 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
259 'db' => $db, 'table' => $table
263 PMA_fatalError(__('Bad parameters!'));
266 // Merge SQL Query aliases with Export aliases from
267 // export page, Export page aliases are given more
268 // preference over SQL Query aliases.
269 if (!empty($_REQUEST['aliases'])) {
270 $aliases = PMA_mergeAliases(
271 PMA_SQP_getAliasesFromQuery($sql_query, $db),
274 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
276 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
280 * Increase time limit for script execution and initializes some variables
282 @set_time_limit
($cfg['ExecTimeLimit']);
283 if (! empty($cfg['MemoryLimit'])) {
284 @ini_set
('memory_limit', $cfg['MemoryLimit']);
286 register_shutdown_function('PMA_shutdownDuringExport');
287 // Start with empty buffer
289 $dump_buffer_len = 0;
291 // We send fake headers to avoid browser timeout when buffering
292 $time_start = time();
294 // Defines the default <CR><LF> format.
295 // For SQL always use \n as MySQL wants this on all platforms.
296 if ($what == 'sql') {
299 $crlf = PMA_Util
::whichCrlf();
302 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
305 // Do we need to convert charset?
306 $output_charset_conversion = $asfile
307 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
308 && isset($charset_of_file) && $charset_of_file != 'utf-8'
311 // Use on the fly compression?
312 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
313 && $compression == 'gzip';
314 if ($GLOBALS['onfly_compression']) {
315 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
318 // Generate filename and mime type if needed
320 if (empty($remember_template)) {
321 $remember_template = '';
323 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
324 $export_type, $remember_template, $export_plugin, $compression,
331 // Open file on server if needed
332 if ($save_on_server) {
333 list($save_filename, $message, $file_handle) = PMA_openExportFile(
334 $filename, $quick_export
337 // problem opening export file on server?
338 if (! empty($message)) {
339 PMA_showExportPage($db, $table, $export_type);
343 * Send headers depending on whether the user chose to download a dump file
348 // (avoid rewriting data containing HTML with anchors and forms;
349 // this was reported to happen under Plesk)
350 @ini_set
('url_rewriter.tags', '');
351 $filename = PMA_sanitizeFilename($filename);
353 PMA_downloadHeader($filename, $mime_type);
356 if ($export_type == 'database') {
357 $num_tables = count($tables);
358 if ($num_tables == 0) {
359 $message = PMA_Message
::error(
360 __('No tables found in database.')
362 $active_page = 'db_export.php';
363 include 'db_export.php';
367 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
368 $export_type, $db, $table
375 // Fake loop just to allow skip of remain of this code by break, I'd really
376 // need exceptions here :-)
379 // Add possibly some comments to export
380 if (! $export_plugin->exportHeader($db)) {
384 // Will we need relation & co. setup?
385 $do_relation = isset($GLOBALS[$what . '_relation']);
386 $do_comments = isset($GLOBALS[$what . '_include_comments'])
387 ||
isset($GLOBALS[$what . '_comments']) ;
388 $do_mime = isset($GLOBALS[$what . '_mime']);
389 if ($do_relation ||
$do_comments ||
$do_mime) {
390 $cfgRelation = PMA_getRelationsParam();
393 include_once 'libraries/transformations.lib.php';
396 // Include dates in export?
397 $do_dates = isset($GLOBALS[$what . '_dates']);
399 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
404 if ($export_type == 'server') {
405 if (! isset($db_select)) {
409 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
410 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
413 } elseif ($export_type == 'database') {
415 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
416 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
420 // We export just one table
421 // $allrows comes from the form when "Dump all rows" has been selected
422 if (! isset($allrows)) {
425 if (! isset($limit_to)) {
428 if (! isset($limit_from)) {
432 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
433 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
434 $allrows, $limit_to, $limit_from, $sql_query, $aliases
437 if (! $export_plugin->exportFooter()) {
444 if ($save_on_server && ! empty($message)) {
445 PMA_showExportPage($db, $table, $export_type);
449 * Send the dump as a file...
451 if (! empty($asfile)) {
452 // Convert the charset if required.
453 if ($output_charset_conversion) {
454 $dump_buffer = PMA_convertString(
456 $GLOBALS['charset_of_file'],
461 // Compression needed?
464 = PMA_compressExport($dump_buffer, $compression, $filename);
467 /* If we saved on server, we have to close file now */
468 if ($save_on_server) {
469 $message = PMA_closeExportFile(
470 $file_handle, $dump_buffer, $save_filename
472 PMA_showExportPage($db, $table, $export_type);
477 echo PMA_getHtmlForDisplayedExportFooter($back_button);