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
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_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']) && $_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 /** @var PMA_String $pmaString */
255 $pmaString = $GLOBALS['PMA_String'];
256 if ($export_type == 'server') {
257 $err_url = 'server_export.php' . PMA_URL_getCommon();
258 } elseif ($export_type == 'database'
259 && /*overload*/mb_strlen($db)
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' && /*overload*/mb_strlen($db)
269 && /*overload*/mb_strlen($table)
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 if (!empty($_REQUEST['aliases'])) {
289 $aliases = PMA_mergeAliases(
290 SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db),
293 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
295 $aliases = SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
300 * Increase time limit for script execution and initializes some variables
302 @set_time_limit
($cfg['ExecTimeLimit']);
303 if (! empty($cfg['MemoryLimit'])) {
304 @ini_set
('memory_limit', $cfg['MemoryLimit']);
306 register_shutdown_function('PMA_shutdownDuringExport');
307 // Start with empty buffer
309 $dump_buffer_len = 0;
311 // Array of dump_buffers - used in separate file exports
312 $dump_buffer_objects = array();
314 // We send fake headers to avoid browser timeout when buffering
315 $time_start = time();
317 // Defines the default <CR><LF> format.
318 // For SQL always use \n as MySQL wants this on all platforms.
319 if ($what == 'sql') {
322 $crlf = PMA_Util
::whichCrlf();
325 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
328 // Do we need to convert charset?
329 $output_charset_conversion = $asfile
330 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
331 && isset($charset) && $charset != 'utf-8'
334 // Use on the fly compression?
335 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
336 && $compression == 'gzip';
337 if ($GLOBALS['onfly_compression']) {
338 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
341 // Generate filename and mime type if needed
343 if (empty($remember_template)) {
344 $remember_template = '';
346 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
347 $export_type, $remember_template, $export_plugin, $compression,
354 // Open file on server if needed
355 if ($save_on_server) {
356 list($save_filename, $message, $file_handle) = PMA_openExportFile(
357 $filename, $quick_export
360 // problem opening export file on server?
361 if (! empty($message)) {
362 PMA_showExportPage($db, $table, $export_type);
366 * Send headers depending on whether the user chose to download a dump file
371 // (avoid rewriting data containing HTML with anchors and forms;
372 // this was reported to happen under Plesk)
373 @ini_set
('url_rewriter.tags', '');
374 $filename = PMA_sanitizeFilename($filename);
376 PMA_downloadHeader($filename, $mime_type);
379 if ($export_type == 'database') {
380 $num_tables = count($tables);
381 if ($num_tables == 0) {
382 $message = PMA_Message
::error(
383 __('No tables found in database.')
385 $active_page = 'db_export.php';
386 include 'db_export.php';
390 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
391 $export_type, $db, $table
398 // Fake loop just to allow skip of remain of this code by break, I'd really
399 // need exceptions here :-)
403 $dump_buffer_len = 0;
405 // Add possibly some comments to export
406 if (! $export_plugin->exportHeader()) {
410 // Will we need relation & co. setup?
411 $do_relation = isset($GLOBALS[$what . '_relation']);
412 $do_comments = isset($GLOBALS[$what . '_include_comments'])
413 ||
isset($GLOBALS[$what . '_comments']) ;
414 $do_mime = isset($GLOBALS[$what . '_mime']);
415 if ($do_relation ||
$do_comments ||
$do_mime) {
416 $cfgRelation = PMA_getRelationsParam();
419 include_once 'libraries/transformations.lib.php';
422 // Include dates in export?
423 $do_dates = isset($GLOBALS[$what . '_dates']);
425 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
430 if ($export_type == 'server') {
431 if (! isset($db_select)) {
435 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
436 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
437 $aliases, $separate_files
439 } elseif ($export_type == 'database') {
440 if (!isset($table_structure) ||
!is_array($table_structure)) {
441 $table_structure = array();
443 if (!isset($table_data) ||
!is_array($table_data)) {
444 $table_data = array();
446 if (!empty($_REQUEST['structure_or_data_forced'])) {
447 $table_structure = $tables;
448 $table_data = $tables;
450 if (isset($lock_tables)) {
451 PMA_lockTables($db, $tables, "READ");
454 $db, $tables, $whatStrucOrData, $table_structure,
455 $table_data, $export_plugin, $crlf, $err_url, $export_type,
456 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
460 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
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,
493 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
499 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
500 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
501 $allrows, $limit_to, $limit_from, $sql_query, $aliases
505 if (! $export_plugin->exportFooter()) {
512 if ($save_on_server && ! empty($message)) {
513 PMA_showExportPage($db, $table, $export_type);
517 * Send the dump as a file...
519 if (empty($asfile)) {
520 echo PMA_getHtmlForDisplayedExportFooter($back_button);
524 // Convert the charset if required.
525 if ($output_charset_conversion) {
526 $dump_buffer = PMA_convertString(
533 // Compression needed?
535 if (! empty($separate_files)) {
536 $dump_buffer = PMA_compressExport(
537 $dump_buffer_objects, $compression, $filename
540 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
545 /* If we saved on server, we have to close file now */
546 if ($save_on_server) {
547 $message = PMA_closeExportFile(
548 $file_handle, $dump_buffer, $save_filename
550 PMA_showExportPage($db, $table, $export_type);