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
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',
117 'sql_create_table_statements',
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'];
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(
177 'libraries/plugins/export/',
179 'export_type' => $export_type,
180 'single_table' => isset($single_table)
184 // Backward compatibility
188 if (! isset($export_plugin)) {
189 PMA_fatalError(__('Bad type!'));
192 // Avoid warning from PHP Analyzer
193 if (is_null($export_plugin)) {
194 $export_plugin = new stdClass();
198 * valid compression methods
200 $compression_methods = array(
206 * init and variable checking
208 $compression = false;
210 $save_on_server = false;
211 $buffer_needed = false;
217 $separate_files = '';
219 // Is it a quick or custom export?
220 if ($_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 /** @var PMA_String $pmaString */
259 $pmaString = $GLOBALS['PMA_String'];
260 if ($export_type == 'server') {
261 $err_url = 'server_export.php' . PMA_URL_getCommon();
262 } elseif ($export_type == 'database'
263 && /*overload*/mb_strlen($db)
265 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
266 // Check if we have something to export
267 if (isset($table_select)) {
268 $tables = $table_select;
272 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
273 && /*overload*/mb_strlen($table)
275 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
277 'db' => $db, 'table' => $table
281 PMA_fatalError(__('Bad parameters!'));
284 // Merge SQL Query aliases with Export aliases from
285 // export page, Export page aliases are given more
286 // preference over SQL Query aliases.
287 $parser = new SqlParser\
Parser($sql_query);
289 if ((!empty($parser->statements
[0]))
290 && ($parser->statements
[0] instanceof SqlParser\Statements\SelectStatement
)
292 if (!empty($_REQUEST['aliases'])) {
293 $aliases = PMA_mergeAliases(
294 SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db),
297 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
299 $aliases = SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
304 * Increase time limit for script execution and initializes some variables
306 @set_time_limit
($cfg['ExecTimeLimit']);
307 if (! empty($cfg['MemoryLimit'])) {
308 @ini_set
('memory_limit', $cfg['MemoryLimit']);
310 register_shutdown_function('PMA_shutdownDuringExport');
311 // Start with empty buffer
313 $dump_buffer_len = 0;
315 // Array of dump_buffers - used in separate file exports
316 $dump_buffer_objects = array();
318 // We send fake headers to avoid browser timeout when buffering
319 $time_start = time();
321 // Defines the default <CR><LF> format.
322 // For SQL always use \n as MySQL wants this on all platforms.
323 if ($what == 'sql') {
326 $crlf = PMA_Util
::whichCrlf();
329 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
332 // Do we need to convert charset?
333 $output_charset_conversion = $asfile
334 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
335 && isset($charset) && $charset != 'utf-8'
338 // Use on the fly compression?
339 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
340 && $compression == 'gzip';
341 if ($GLOBALS['onfly_compression']) {
342 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
345 // Generate filename and mime type if needed
347 if (empty($remember_template)) {
348 $remember_template = '';
350 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
351 $export_type, $remember_template, $export_plugin, $compression,
358 // Open file on server if needed
359 if ($save_on_server) {
360 list($save_filename, $message, $file_handle) = PMA_openExportFile(
361 $filename, $quick_export
364 // problem opening export file on server?
365 if (! empty($message)) {
366 PMA_showExportPage($db, $table, $export_type);
370 * Send headers depending on whether the user chose to download a dump file
375 // (avoid rewriting data containing HTML with anchors and forms;
376 // this was reported to happen under Plesk)
377 @ini_set
('url_rewriter.tags', '');
378 $filename = PMA_sanitizeFilename($filename);
380 PMA_downloadHeader($filename, $mime_type);
383 if ($export_type == 'database') {
384 $num_tables = count($tables);
385 if ($num_tables == 0) {
386 $message = PMA_Message
::error(
387 __('No tables found in database.')
389 $active_page = 'db_export.php';
390 include 'db_export.php';
394 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
395 $export_type, $db, $table
402 // Fake loop just to allow skip of remain of this code by break, I'd really
403 // need exceptions here :-)
406 if ($separate_files != 'database') {
407 // Add possibly some comments to export
408 if (! $export_plugin->exportHeader()) {
413 // Will we need relation & co. setup?
414 $do_relation = isset($GLOBALS[$what . '_relation']);
415 $do_comments = isset($GLOBALS[$what . '_include_comments'])
416 ||
isset($GLOBALS[$what . '_comments']) ;
417 $do_mime = isset($GLOBALS[$what . '_mime']);
418 if ($do_relation ||
$do_comments ||
$do_mime) {
419 $cfgRelation = PMA_getRelationsParam();
422 include_once 'libraries/transformations.lib.php';
425 // Include dates in export?
426 $do_dates = isset($GLOBALS[$what . '_dates']);
428 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
433 if ($export_type == 'server') {
434 if (! isset($db_select)) {
438 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
439 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
440 $aliases, $separate_files
442 } elseif ($export_type == 'database') {
443 if (!isset($table_structure) ||
!is_array($table_structure)) {
444 $table_structure = array();
446 if (!isset($table_data) ||
!is_array($table_data)) {
447 $table_data = array();
449 if (!empty($_REQUEST['structure_or_data_forced'])) {
450 $table_structure = $tables;
451 $table_data = $tables;
453 if (isset($lock_tables)) {
454 PMA_lockTables($db, $tables, "READ");
457 $db, $tables, $whatStrucOrData, $table_structure,
458 $table_data, $export_plugin, $crlf, $err_url, $export_type,
459 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
463 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
469 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
470 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
471 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
475 // We export just one table
476 // $allrows comes from the form when "Dump all rows" has been selected
477 if (! isset($allrows)) {
480 if (! isset($limit_to)) {
483 if (! isset($limit_from)) {
486 if (isset($lock_tables)) {
488 PMA_lockTables($db, array($table), "READ");
490 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
491 $err_url, $export_type, $do_relation, $do_comments,
492 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
496 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
502 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
503 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
504 $allrows, $limit_to, $limit_from, $sql_query, $aliases
508 if (! $export_plugin->exportFooter()) {
515 if ($save_on_server && ! empty($message)) {
516 PMA_showExportPage($db, $table, $export_type);
520 * Send the dump as a file...
522 if (empty($asfile)) {
523 echo PMA_getHtmlForDisplayedExportFooter($back_button);
527 // Convert the charset if required.
528 if ($output_charset_conversion) {
529 $dump_buffer = PMA_convertString(
536 // Compression needed?
538 if (! empty($separate_files)) {
539 $dump_buffer = PMA_compressExport(
540 $dump_buffer_objects, $compression, $filename
543 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
548 /* If we saved on server, we have to close file now */
549 if ($save_on_server) {
550 $message = PMA_closeExportFile(
551 $file_handle, $dump_buffer, $save_filename
553 PMA_showExportPage($db, $table, $export_type);