2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
8 use PMA\libraries\Encoding
;
9 use PMA\libraries\plugins\ExportPlugin
;
10 use PMA\libraries\URL
;
11 use PMA\libraries\Sanitize
;
14 * Get the variables sent or posted to this script and a core script
17 * If we are sending the export file (as opposed to just displaying it
18 * as text), we have to bypass the usual PMA\libraries\Response mechanism
20 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
21 define('PMA_BYPASS_GET_INSTANCE', 1);
23 include_once 'libraries/common.inc.php';
24 include_once 'libraries/plugin_interface.lib.php';
25 include_once 'libraries/export.lib.php';
27 //check if it's the GET request to check export time out
28 if (isset($_GET['check_time_out'])) {
29 if (isset($_SESSION['pma_export_error'])) {
30 $err = $_SESSION['pma_export_error'];
31 unset($_SESSION['pma_export_error']);
39 * Sets globals from $_POST
41 * - Please keep the parameters in order of their appearance in the form
42 * - Some of these parameters are not used, as the code below directly
43 * verifies from the superglobal $_POST or $_REQUEST
44 * TODO: this should be removed to avoid passing user input to GLOBALS
72 'htmlword_structure_or_data',
76 'mediawiki_structure_or_data',
78 'pdf_structure_or_data',
79 'odt_structure_or_data',
85 'codegen_structure_or_data',
91 'excel_structure_or_data',
92 'yaml_structure_or_data',
94 'ods_structure_or_data',
96 'json_structure_or_data',
98 'xml_structure_or_data',
100 'xml_export_functions',
101 'xml_export_procedures',
103 'xml_export_triggers',
105 'xml_export_contents',
106 'texytext_structure_or_data',
109 'phparray_structure_or_data',
110 'sql_include_comments',
111 'sql_header_comment',
115 'sql_use_transaction',
118 'sql_structure_or_data',
119 'sql_create_database',
121 'sql_procedure_function',
124 'sql_create_trigger',
126 'sql_auto_increment',
133 'sql_max_query_size',
134 'sql_hex_for_binary',
137 'sql_views_as_tables',
146 'csv_structure_or_data',
147 // csv_replace should have been here but we use it directly from $_POST
149 'latex_structure_or_data',
150 'latex_structure_caption',
151 'latex_structure_continued_caption',
152 'latex_structure_label',
157 'latex_data_caption',
158 'latex_data_continued_caption',
164 foreach ($post_params as $one_post_param) {
165 if (isset($_POST[$one_post_param])) {
166 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
170 $table = $GLOBALS['table'];
172 PMA\libraries\Util
::checkParameters(array('what', 'export_type'));
174 // sanitize this parameter which will be used below in a file inclusion
175 $what = PMA_securePath($_POST['what']);
177 // export class instance, not array of properties, as before
178 /* @var $export_plugin ExportPlugin */
179 $export_plugin = PMA_getPlugin(
182 'libraries/plugins/export/',
184 'export_type' => $export_type,
185 'single_table' => isset($single_table)
189 // Backward compatibility
193 if (empty($export_plugin)) {
194 PMA_fatalError(__('Bad type!'));
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 (isset($_REQUEST['quick_or_custom'])
221 && $_REQUEST['quick_or_custom'] == 'quick'
223 $quick_export = true;
225 $quick_export = false;
228 if ($_REQUEST['output_format'] == 'astext') {
232 if (isset($_REQUEST['as_separate_files'])
233 && ! empty($_REQUEST['as_separate_files'])
235 if (isset($_REQUEST['compression'])
236 && ! empty($_REQUEST['compression'])
237 && $_REQUEST['compression'] == 'zip'
239 $separate_files = $_REQUEST['as_separate_files'];
242 if (in_array($_REQUEST['compression'], $compression_methods)) {
243 $compression = $_REQUEST['compression'];
244 $buffer_needed = true;
246 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
247 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
250 $onserver = $_REQUEST['quick_export_onserver'];
252 $onserver = $_REQUEST['onserver'];
254 // Will we save dump on server?
255 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
259 // Generate error url and check for needed variables
260 if ($export_type == 'server') {
261 $err_url = 'server_export.php' . URL
::getCommon();
262 } elseif ($export_type == 'database' && strlen($db) > 0) {
263 $err_url = 'db_export.php' . URL
::getCommon(array('db' => $db));
264 // Check if we have something to export
265 if (isset($table_select)) {
266 $tables = $table_select;
270 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
271 $err_url = 'tbl_export.php' . 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 = Encoding
::canConvertKanji() && $type != 'xls';
323 // Do we need to convert charset?
324 $output_charset_conversion = $asfile
325 && Encoding
::isSupported()
326 && isset($charset) && $charset != 'utf-8'
329 // Use on the fly compression?
330 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
331 && $compression == 'gzip';
332 if ($GLOBALS['onfly_compression']) {
333 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
336 // Generate filename and mime type if needed
338 if (empty($remember_template)) {
339 $remember_template = '';
341 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
342 $export_type, $remember_template, $export_plugin, $compression,
349 // Open file on server if needed
350 if ($save_on_server) {
351 list($save_filename, $message, $file_handle) = PMA_openExportFile(
352 $filename, $quick_export
355 // problem opening export file on server?
356 if (! empty($message)) {
357 PMA_showExportPage($db, $table, $export_type);
361 * Send headers depending on whether the user chose to download a dump file
366 // (avoid rewriting data containing HTML with anchors and forms;
367 // this was reported to happen under Plesk)
368 @ini_set
('url_rewriter.tags', '');
369 $filename = Sanitize
::sanitizeFilename($filename);
371 PMA_downloadHeader($filename, $mime_type);
374 if ($export_type == 'database') {
375 $num_tables = count($tables);
376 if ($num_tables == 0) {
377 $message = PMA\libraries\Message
::error(
378 __('No tables found in database.')
380 $active_page = 'db_export.php';
381 include 'db_export.php';
385 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
386 $export_type, $db, $table
393 // Fake loop just to allow skip of remain of this code by break, I'd really
394 // need exceptions here :-)
398 $dump_buffer_len = 0;
400 // Add possibly some comments to export
401 if (! $export_plugin->exportHeader()) {
405 // Will we need relation & co. setup?
406 $do_relation = isset($GLOBALS[$what . '_relation']);
407 $do_comments = isset($GLOBALS[$what . '_include_comments'])
408 ||
isset($GLOBALS[$what . '_comments']);
409 $do_mime = isset($GLOBALS[$what . '_mime']);
410 if ($do_relation ||
$do_comments ||
$do_mime) {
411 $cfgRelation = PMA_getRelationsParam();
414 include_once 'libraries/transformations.lib.php';
417 // Include dates in export?
418 $do_dates = isset($GLOBALS[$what . '_dates']);
420 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
425 if ($export_type == 'server') {
426 if (! isset($db_select)) {
430 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
431 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
432 $aliases, $separate_files
434 } elseif ($export_type == 'database') {
435 if (!isset($table_structure) ||
!is_array($table_structure)) {
436 $table_structure = array();
438 if (!isset($table_data) ||
!is_array($table_data)) {
439 $table_data = array();
441 if (!empty($_REQUEST['structure_or_data_forced'])) {
442 $table_structure = $tables;
443 $table_data = $tables;
445 if (isset($lock_tables)) {
446 PMA_lockTables($db, $tables, "READ");
449 $db, $tables, $whatStrucOrData, $table_structure,
450 $table_data, $export_plugin, $crlf, $err_url, $export_type,
451 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
459 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
460 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
461 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
465 // We export just one table
466 // $allrows comes from the form when "Dump all rows" has been selected
467 if (! isset($allrows)) {
470 if (! isset($limit_to)) {
473 if (! isset($limit_from)) {
476 if (isset($lock_tables)) {
478 PMA_lockTables($db, array($table), "READ");
480 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
481 $err_url, $export_type, $do_relation, $do_comments,
482 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
490 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
491 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
492 $allrows, $limit_to, $limit_from, $sql_query, $aliases
496 if (! $export_plugin->exportFooter()) {
503 if ($save_on_server && ! empty($message)) {
504 PMA_showExportPage($db, $table, $export_type);
508 * Send the dump as a file...
510 if (empty($asfile)) {
511 echo PMA_getHtmlForDisplayedExportFooter($back_button);
515 // Convert the charset if required.
516 if ($output_charset_conversion) {
517 $dump_buffer = Encoding
::convertString(
524 // Compression needed?
526 if (! empty($separate_files)) {
527 $dump_buffer = PMA_compressExport(
528 $dump_buffer_objects, $compression, $filename
531 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
536 /* If we saved on server, we have to close file now */
537 if ($save_on_server) {
538 $message = PMA_closeExportFile(
539 $file_handle, $dump_buffer, $save_filename
541 PMA_showExportPage($db, $table, $export_type);