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\Util
;
12 use PMA\libraries\Sanitize
;
15 * Get the variables sent or posted to this script and a core script
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
45 * TODO: this should be removed to avoid passing user input to GLOBALS
73 'htmlword_structure_or_data',
77 'mediawiki_structure_or_data',
79 'pdf_structure_or_data',
80 'odt_structure_or_data',
86 'codegen_structure_or_data',
92 'excel_structure_or_data',
93 'yaml_structure_or_data',
95 'ods_structure_or_data',
97 'json_structure_or_data',
99 'xml_structure_or_data',
101 'xml_export_functions',
102 'xml_export_procedures',
104 'xml_export_triggers',
106 'xml_export_contents',
107 'texytext_structure_or_data',
110 'phparray_structure_or_data',
111 'sql_include_comments',
112 'sql_header_comment',
116 'sql_use_transaction',
119 'sql_structure_or_data',
120 'sql_create_database',
122 'sql_procedure_function',
125 'sql_create_trigger',
127 'sql_auto_increment',
134 'sql_max_query_size',
135 'sql_hex_for_binary',
138 'sql_views_as_tables',
147 'csv_structure_or_data',
148 // csv_replace should have been here but we use it directly from $_POST
150 'latex_structure_or_data',
151 'latex_structure_caption',
152 'latex_structure_continued_caption',
153 'latex_structure_label',
158 'latex_data_caption',
159 'latex_data_continued_caption',
165 foreach ($post_params as $one_post_param) {
166 if (isset($_POST[$one_post_param])) {
167 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
171 $table = $GLOBALS['table'];
173 PMA\libraries\Util
::checkParameters(array('what', 'export_type'));
175 // sanitize this parameter which will be used below in a file inclusion
176 $what = PMA_securePath($_POST['what']);
178 // export class instance, not array of properties, as before
179 /* @var $export_plugin ExportPlugin */
180 $export_plugin = PMA_getPlugin(
183 'libraries/plugins/export/',
185 'export_type' => $export_type,
186 'single_table' => isset($single_table)
191 if (empty($export_plugin)) {
192 PMA_fatalError(__('Bad type!'));
196 * valid compression methods
198 $compression_methods = array(
204 * init and variable checking
206 $compression = false;
208 $save_on_server = false;
209 $buffer_needed = false;
215 $separate_files = '';
217 // Is it a quick or custom export?
218 if (isset($_REQUEST['quick_or_custom'])
219 && $_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 if ($export_type == 'server') {
259 $err_url = 'server_export.php' . URL
::getCommon();
260 } elseif ($export_type == 'database' && strlen($db) > 0) {
261 $err_url = 'db_export.php' . 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' && strlen($db) > 0 && strlen($table) > 0) {
269 $err_url = 'tbl_export.php' . URL
::getCommon(
271 'db' => $db, 'table' => $table
275 PMA_fatalError(__('Bad parameters!'));
278 // Merge SQL Query aliases with Export aliases from
279 // export page, Export page aliases are given more
280 // preference over SQL Query aliases.
281 $parser = new \PhpMyAdmin\SqlParser\
Parser($sql_query);
283 if ((!empty($parser->statements
[0]))
284 && ($parser->statements
[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement
)
286 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
288 if (!empty($_REQUEST['aliases'])) {
289 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
290 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
294 * Increase time limit for script execution and initializes some variables
296 Util
::setTimeLimit();
297 if (! empty($cfg['MemoryLimit'])) {
298 @ini_set
('memory_limit', $cfg['MemoryLimit']);
300 register_shutdown_function('PMA_shutdownDuringExport');
301 // Start with empty buffer
303 $dump_buffer_len = 0;
305 // Array of dump_buffers - used in separate file exports
306 $dump_buffer_objects = array();
308 // We send fake headers to avoid browser timeout when buffering
309 $time_start = time();
311 // Defines the default <CR><LF> format.
312 // For SQL always use \n as MySQL wants this on all platforms.
313 if ($what == 'sql') {
319 $output_kanji_conversion = Encoding
::canConvertKanji();
321 // Do we need to convert charset?
322 $output_charset_conversion = $asfile
323 && Encoding
::isSupported()
324 && isset($charset) && $charset != 'utf-8';
326 // Use on the fly compression?
327 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
328 && $compression == 'gzip';
329 if ($GLOBALS['onfly_compression']) {
330 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
333 // Generate filename and mime type if needed
335 if (empty($remember_template)) {
336 $remember_template = '';
338 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
339 $export_type, $remember_template, $export_plugin, $compression,
346 // Open file on server if needed
347 if ($save_on_server) {
348 list($save_filename, $message, $file_handle) = PMA_openExportFile(
349 $filename, $quick_export
352 // problem opening export file on server?
353 if (! empty($message)) {
354 PMA_showExportPage($db, $table, $export_type);
358 * Send headers depending on whether the user chose to download a dump file
363 // (avoid rewriting data containing HTML with anchors and forms;
364 // this was reported to happen under Plesk)
365 @ini_set
('url_rewriter.tags', '');
366 $filename = Sanitize
::sanitizeFilename($filename);
368 PMA_downloadHeader($filename, $mime_type);
371 if ($export_type == 'database') {
372 $num_tables = count($tables);
373 if ($num_tables == 0) {
374 $message = PMA\libraries\Message
::error(
375 __('No tables found in database.')
377 $active_page = 'db_export.php';
378 include 'db_export.php';
382 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
383 $export_type, $db, $table
390 // Fake loop just to allow skip of remain of this code by break, I'd really
391 // need exceptions here :-)
395 $dump_buffer_len = 0;
397 // Add possibly some comments to export
398 if (! $export_plugin->exportHeader()) {
402 // Will we need relation & co. setup?
403 $do_relation = isset($GLOBALS[$what . '_relation']);
404 $do_comments = isset($GLOBALS[$what . '_include_comments'])
405 ||
isset($GLOBALS[$what . '_comments']);
406 $do_mime = isset($GLOBALS[$what . '_mime']);
407 if ($do_relation ||
$do_comments ||
$do_mime) {
408 $cfgRelation = PMA_getRelationsParam();
411 include_once 'libraries/transformations.lib.php';
414 // Include dates in export?
415 $do_dates = isset($GLOBALS[$what . '_dates']);
417 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
422 if ($export_type == 'server') {
423 if (! isset($db_select)) {
427 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
428 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
429 $aliases, $separate_files
431 } elseif ($export_type == 'database') {
432 if (!isset($table_structure) ||
!is_array($table_structure)) {
433 $table_structure = array();
435 if (!isset($table_data) ||
!is_array($table_data)) {
436 $table_data = array();
438 if (!empty($_REQUEST['structure_or_data_forced'])) {
439 $table_structure = $tables;
440 $table_data = $tables;
442 if (isset($lock_tables)) {
443 PMA_lockTables($db, $tables, "READ");
446 $db, $tables, $whatStrucOrData, $table_structure,
447 $table_data, $export_plugin, $crlf, $err_url, $export_type,
448 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
456 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
457 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
458 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
462 // We export just one table
463 // $allrows comes from the form when "Dump all rows" has been selected
464 if (! isset($allrows)) {
467 if (! isset($limit_to)) {
470 if (! isset($limit_from)) {
473 if (isset($lock_tables)) {
475 PMA_lockTables($db, array($table), "READ");
477 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
478 $err_url, $export_type, $do_relation, $do_comments,
479 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
487 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
488 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
489 $allrows, $limit_to, $limit_from, $sql_query, $aliases
493 if (! $export_plugin->exportFooter()) {
500 if ($save_on_server && ! empty($message)) {
501 PMA_showExportPage($db, $table, $export_type);
505 * Send the dump as a file...
507 if (empty($asfile)) {
508 echo PMA_getHtmlForDisplayedExportFooter($back_button);
512 // Convert the charset if required.
513 if ($output_charset_conversion) {
514 $dump_buffer = Encoding
::convertString(
521 // Compression needed?
523 if (! empty($separate_files)) {
524 $dump_buffer = PMA_compressExport(
525 $dump_buffer_objects, $compression, $filename
528 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
533 /* If we saved on server, we have to close file now */
534 if ($save_on_server) {
535 $message = PMA_closeExportFile(
536 $file_handle, $dump_buffer, $save_filename
538 PMA_showExportPage($db, $table, $export_type);