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
64 'htmlword_structure_or_data',
68 'mediawiki_structure_or_data',
71 'pdf_structure_or_data',
72 'odt_structure_or_data',
78 'codegen_structure_or_data',
84 'excel_structure_or_data',
85 'yaml_structure_or_data',
87 'ods_structure_or_data',
89 'json_structure_or_data',
90 'xml_structure_or_data',
91 'xml_export_functions',
92 'xml_export_procedures',
94 'xml_export_triggers',
96 'xml_export_contents',
97 'texytext_structure_or_data',
100 'phparray_structure_or_data',
101 'sql_include_comments',
102 'sql_header_comment',
106 'sql_use_transaction',
109 'sql_structure_or_data',
110 'sql_create_database',
112 'sql_procedure_function',
113 'sql_create_table_statements',
116 'sql_create_trigger',
118 'sql_auto_increment',
125 'sql_max_query_size',
126 'sql_hex_for_binary',
129 'sql_views_as_tables',
137 'csv_structure_or_data',
138 // csv_replace should have been here but we use it directly from $_POST
140 'latex_structure_or_data',
141 'latex_structure_caption',
142 'latex_structure_continued_caption',
143 'latex_structure_label',
148 'latex_data_caption',
149 'latex_data_continued_caption',
154 foreach ($post_params as $one_post_param) {
155 if (isset($_POST[$one_post_param])) {
156 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
160 // sanitize this parameter which will be used below in a file inclusion
161 $what = PMA_securePath($what);
163 PMA_Util
::checkParameters(array('what', 'export_type'));
165 // export class instance, not array of properties, as before
166 $export_plugin = PMA_getPlugin(
169 'libraries/plugins/export/',
171 'export_type' => $export_type,
172 'single_table' => isset($single_table)
176 // Backward compatibility
180 if (! isset($export_plugin)) {
181 PMA_fatalError(__('Bad type!'));
185 * valid compression methods
187 $compression_methods = array(
193 * init and variable checking
195 $compression = false;
197 $save_on_server = false;
198 $buffer_needed = false;
200 // Is it a quick or custom export?
201 if ($_REQUEST['quick_or_custom'] == 'quick') {
202 $quick_export = true;
204 $quick_export = false;
207 if ($_REQUEST['output_format'] == 'astext') {
211 if (in_array($_REQUEST['compression'], $compression_methods)) {
212 $compression = $_REQUEST['compression'];
213 $buffer_needed = true;
215 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
216 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
219 $onserver = $_REQUEST['quick_export_onserver'];
221 $onserver = $_REQUEST['onserver'];
223 // Will we save dump on server?
224 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
228 // Generate error url and check for needed variables
229 if ($export_type == 'server') {
230 $err_url = 'server_export.php?' . PMA_URL_getCommon();
231 } elseif ($export_type == 'database' && strlen($db)) {
232 $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
233 // Check if we have something to export
234 if (isset($table_select)) {
235 $tables = $table_select;
239 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
240 $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
242 PMA_fatalError(__('Bad parameters!'));
246 * Increase time limit for script execution and initializes some variables
248 @set_time_limit
($cfg['ExecTimeLimit']);
249 if (! empty($cfg['MemoryLimit'])) {
250 @ini_set
('memory_limit', $cfg['MemoryLimit']);
252 register_shutdown_function('PMA_shutdownDuringExport');
253 // Start with empty buffer
255 $dump_buffer_len = 0;
257 // We send fake headers to avoid browser timeout when buffering
258 $time_start = time();
261 // Defines the default <CR><LF> format.
262 // For SQL always use \n as MySQL wants this on all platforms.
263 if (!defined('TESTSUITE')) {
264 if ($what == 'sql') {
267 $crlf = PMA_Util
::whichCrlf();
270 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
273 // Do we need to convert charset?
274 $output_charset_conversion = $asfile
275 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
276 && isset($charset_of_file) && $charset_of_file != 'utf-8'
279 // Use on the fly compression?
280 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
281 && $compression == 'gzip';
282 if ($GLOBALS['onfly_compression']) {
283 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
286 // Generate filename and mime type if needed
288 if (empty($remember_template)) {
289 $remember_template = '';
291 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
292 $export_type, $remember_template, $export_plugin, $compression,
297 // Open file on server if needed
298 if ($save_on_server) {
299 list($save_filename, $message, $file_handle) = PMA_openExportFile(
300 $filename, $quick_export
303 // problem opening export file on server?
304 if (! empty($message)) {
305 if ($export_type == 'server') {
306 $active_page = 'server_export.php';
307 include 'server_export.php';
308 } elseif ($export_type == 'database') {
309 $active_page = 'db_export.php';
310 include 'db_export.php';
312 $active_page = 'tbl_export.php';
313 include 'tbl_export.php';
320 * Send headers depending on whether the user chose to download a dump file
323 if (! $save_on_server) {
326 // (avoid rewriting data containing HTML with anchors and forms;
327 // this was reported to happen under Plesk)
328 @ini_set
('url_rewriter.tags', '');
329 $filename = PMA_sanitizeFilename($filename);
331 PMA_downloadHeader($filename, $mime_type);
334 if ($export_type == 'database') {
335 $num_tables = count($tables);
336 if ($num_tables == 0) {
337 $message = PMA_Message
::error(
338 __('No tables found in database.')
340 $active_page = 'db_export.php';
341 include 'db_export.php';
345 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
346 $export_type, $db, $table
353 // Fake loop just to allow skip of remain of this code by break, I'd really
354 // need exceptions here :-)
357 // Add possibly some comments to export
358 if (! $export_plugin->exportHeader($db)) {
362 // Will we need relation & co. setup?
363 $do_relation = isset($GLOBALS[$what . '_relation']);
364 $do_comments = isset($GLOBALS[$what . '_include_comments'])
365 ||
isset($GLOBALS[$what . '_comments']) ;
366 $do_mime = isset($GLOBALS[$what . '_mime']);
367 if ($do_relation ||
$do_comments ||
$do_mime) {
368 $cfgRelation = PMA_getRelationsParam();
371 include_once 'libraries/transformations.lib.php';
374 // Include dates in export?
375 $do_dates = isset($GLOBALS[$what . '_dates']);
377 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
382 if ($export_type == 'server') {
383 if (! isset($db_select)) {
387 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
388 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
390 } elseif ($export_type == 'database') {
392 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
393 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
396 // We export just one table
397 // $allrows comes from the form when "Dump all rows" has been selected
398 if (! isset($allrows)) {
401 if (! isset($limit_to)) {
404 if (! isset($limit_from)) {
408 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
409 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
410 $allrows, $limit_to, $limit_from, $sql_query
413 if (! $export_plugin->exportFooter()) {
420 if ($save_on_server && ! empty($message)) {
421 if ($export_type == 'server') {
422 $active_page = 'server_export.php';
423 include 'server_export.php';
424 } elseif ($export_type == 'database') {
425 $active_page = 'db_export.php';
426 include 'db_export.php';
428 $active_page = 'tbl_export.php';
429 include 'tbl_export.php';
435 * Send the dump as a file...
437 if (! empty($asfile)) {
438 // Convert the charset if required.
439 if ($output_charset_conversion) {
440 $dump_buffer = PMA_convertString(
442 $GLOBALS['charset_of_file'],
447 // Compression needed?
450 = PMA_compressExport($dump_buffer, $compression, $filename);
453 /* If we saved on server, we have to close file now */
454 if ($save_on_server) {
455 $message = PMA_closeExportFile(
456 $file_handle, $dump_buffer, $save_filename
458 if ($export_type == 'server') {
459 $active_page = 'server_export.php';
460 include_once 'server_export.php';
461 } elseif ($export_type == 'database') {
462 $active_page = 'db_export.php';
463 include_once 'db_export.php';
465 $active_page = 'tbl_export.php';
466 include_once 'tbl_export.php';
473 echo PMA_getHtmlForDisplayedExportFooter($back_button);