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
16 if (!defined('TESTSUITE')) {
18 * If we are sending the export file (as opposed to just displaying it
19 * as text), we have to bypass the usual PMA\libraries\Response mechanism
21 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
22 define('PMA_BYPASS_GET_INSTANCE', 1);
24 include_once 'libraries/common.inc.php';
25 include_once 'libraries/plugin_interface.lib.php';
26 include_once 'libraries/export.lib.php';
28 //check if it's the GET request to check export time out
29 if (isset($_GET['check_time_out'])) {
30 if (isset($_SESSION['pma_export_error'])) {
31 $err = $_SESSION['pma_export_error'];
32 unset($_SESSION['pma_export_error']);
40 * Sets globals from $_POST
42 * - Please keep the parameters in order of their appearance in the form
43 * - Some of these parameters are not used, as the code below directly
44 * verifies from the superglobal $_POST or $_REQUEST
71 'htmlword_structure_or_data',
75 'mediawiki_structure_or_data',
77 'pdf_structure_or_data',
78 'odt_structure_or_data',
84 'codegen_structure_or_data',
90 'excel_structure_or_data',
91 'yaml_structure_or_data',
93 'ods_structure_or_data',
95 'json_structure_or_data',
97 'xml_structure_or_data',
99 'xml_export_functions',
100 'xml_export_procedures',
102 'xml_export_triggers',
104 'xml_export_contents',
105 'texytext_structure_or_data',
108 'phparray_structure_or_data',
109 'sql_include_comments',
110 'sql_header_comment',
114 'sql_use_transaction',
117 'sql_structure_or_data',
118 'sql_create_database',
120 'sql_procedure_function',
123 'sql_create_trigger',
125 'sql_auto_increment',
132 'sql_max_query_size',
133 'sql_hex_for_binary',
136 'sql_views_as_tables',
145 'csv_structure_or_data',
146 // csv_replace should have been here but we use it directly from $_POST
148 'latex_structure_or_data',
149 'latex_structure_caption',
150 'latex_structure_continued_caption',
151 'latex_structure_label',
156 'latex_data_caption',
157 'latex_data_continued_caption',
163 foreach ($post_params as $one_post_param) {
164 if (isset($_POST[$one_post_param])) {
165 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
169 $table = $GLOBALS['table'];
171 PMA\libraries\Util
::checkParameters(array('what', 'export_type'));
173 // sanitize this parameter which will be used below in a file inclusion
174 $what = PMA_securePath($_POST['what']);
176 // export class instance, not array of properties, as before
177 /* @var $export_plugin ExportPlugin */
178 $export_plugin = PMA_getPlugin(
181 'libraries/plugins/export/',
183 'export_type' => $export_type,
184 'single_table' => isset($single_table)
188 // Backward compatibility
192 if (empty($export_plugin)) {
193 PMA_fatalError(__('Bad type!'));
197 * valid compression methods
199 $compression_methods = array(
205 * init and variable checking
207 $compression = false;
209 $save_on_server = false;
210 $buffer_needed = false;
216 $separate_files = '';
218 // Is it a quick or custom export?
219 if (isset($_REQUEST['quick_or_custom'])
220 && $_REQUEST['quick_or_custom'] == 'quick'
222 $quick_export = true;
224 $quick_export = false;
227 if ($_REQUEST['output_format'] == 'astext') {
231 if (isset($_REQUEST['as_separate_files'])
232 && ! empty($_REQUEST['as_separate_files'])
234 if (isset($_REQUEST['compression'])
235 && ! empty($_REQUEST['compression'])
236 && $_REQUEST['compression'] == 'zip'
238 $separate_files = $_REQUEST['as_separate_files'];
241 if (in_array($_REQUEST['compression'], $compression_methods)) {
242 $compression = $_REQUEST['compression'];
243 $buffer_needed = true;
245 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
246 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
249 $onserver = $_REQUEST['quick_export_onserver'];
251 $onserver = $_REQUEST['onserver'];
253 // Will we save dump on server?
254 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
258 // Generate error url and check for needed variables
259 if ($export_type == 'server') {
260 $err_url = 'server_export.php' . URL
::getCommon();
261 } elseif ($export_type == 'database'
264 $err_url = 'db_export.php' . URL
::getCommon(array('db' => $db));
265 // Check if we have something to export
266 if (isset($table_select)) {
267 $tables = $table_select;
271 } elseif ($export_type == 'table' && mb_strlen($db)
274 $err_url = 'tbl_export.php' . URL
::getCommon(
276 'db' => $db, 'table' => $table
280 PMA_fatalError(__('Bad parameters!'));
283 // Merge SQL Query aliases with Export aliases from
284 // export page, Export page aliases are given more
285 // preference over SQL Query aliases.
286 $parser = new SqlParser\
Parser($sql_query);
288 if ((!empty($parser->statements
[0]))
289 && ($parser->statements
[0] instanceof SqlParser\Statements\SelectStatement
)
291 $aliases = SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
293 if (!empty($_REQUEST['aliases'])) {
294 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
295 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
299 * Increase time limit for script execution and initializes some variables
301 @set_time_limit
($cfg['ExecTimeLimit']);
302 if (! empty($cfg['MemoryLimit'])) {
303 @ini_set
('memory_limit', $cfg['MemoryLimit']);
305 register_shutdown_function('PMA_shutdownDuringExport');
306 // Start with empty buffer
308 $dump_buffer_len = 0;
310 // Array of dump_buffers - used in separate file exports
311 $dump_buffer_objects = array();
313 // We send fake headers to avoid browser timeout when buffering
314 $time_start = time();
316 // Defines the default <CR><LF> format.
317 // For SQL always use \n as MySQL wants this on all platforms.
318 if ($what == 'sql') {
321 $crlf = PMA\libraries\Util
::whichCrlf();
324 $output_kanji_conversion = Encoding
::canConvertKanji() && $type != 'xls';
326 // Do we need to convert charset?
327 $output_charset_conversion = $asfile
328 && Encoding
::isSupported()
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 = Sanitize
::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,
462 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
463 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
464 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
468 // We export just one table
469 // $allrows comes from the form when "Dump all rows" has been selected
470 if (! isset($allrows)) {
473 if (! isset($limit_to)) {
476 if (! isset($limit_from)) {
479 if (isset($lock_tables)) {
481 PMA_lockTables($db, array($table), "READ");
483 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
484 $err_url, $export_type, $do_relation, $do_comments,
485 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
493 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
494 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
495 $allrows, $limit_to, $limit_from, $sql_query, $aliases
499 if (! $export_plugin->exportFooter()) {
506 if ($save_on_server && ! empty($message)) {
507 PMA_showExportPage($db, $table, $export_type);
511 * Send the dump as a file...
513 if (empty($asfile)) {
514 echo PMA_getHtmlForDisplayedExportFooter($back_button);
518 // Convert the charset if required.
519 if ($output_charset_conversion) {
520 $dump_buffer = Encoding
::convertString(
527 // Compression needed?
529 if (! empty($separate_files)) {
530 $dump_buffer = PMA_compressExport(
531 $dump_buffer_objects, $compression, $filename
534 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
539 /* If we saved on server, we have to close file now */
540 if ($save_on_server) {
541 $message = PMA_closeExportFile(
542 $file_handle, $dump_buffer, $save_filename
544 PMA_showExportPage($db, $table, $export_type);