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',
67 'mediawiki_structure_or_data',
70 'pdf_structure_or_data',
71 'odt_structure_or_data',
77 'codegen_structure_or_data',
83 'excel_structure_or_data',
84 'yaml_structure_or_data',
86 'ods_structure_or_data',
88 'json_structure_or_data',
89 'xml_structure_or_data',
90 'xml_export_functions',
91 'xml_export_procedures',
93 'xml_export_triggers',
95 'xml_export_contents',
96 'texytext_structure_or_data',
99 'phparray_structure_or_data',
100 'sql_include_comments',
101 'sql_header_comment',
105 'sql_use_transaction',
108 'sql_structure_or_data',
109 'sql_create_database',
111 'sql_procedure_function',
112 'sql_create_table_statements',
115 'sql_create_trigger',
117 'sql_auto_increment',
124 'sql_max_query_size',
135 'csv_structure_or_data',
136 // csv_replace should have been here but we use it directly from $_POST
138 'latex_structure_or_data',
139 'latex_structure_caption',
140 'latex_structure_continued_caption',
141 'latex_structure_label',
146 'latex_data_caption',
147 'latex_data_continued_caption',
152 foreach ($post_params as $one_post_param) {
153 if (isset($_POST[$one_post_param])) {
154 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
158 // sanitize this parameter which will be used below in a file inclusion
159 $what = PMA_securePath($what);
161 PMA_Util
::checkParameters(array('what', 'export_type'));
163 // export class instance, not array of properties, as before
164 $export_plugin = PMA_getPlugin(
167 'libraries/plugins/export/',
169 'export_type' => $export_type,
170 'single_table' => isset($single_table)
174 // Backward compatibility
178 if (! isset($export_plugin)) {
179 PMA_fatalError(__('Bad type!'));
183 * valid compression methods
185 $compression_methods = array(
191 * init and variable checking
193 $compression = false;
195 $save_on_server = false;
196 $buffer_needed = false;
198 // Is it a quick or custom export?
199 if ($_REQUEST['quick_or_custom'] == 'quick') {
200 $quick_export = true;
202 $quick_export = false;
205 if ($_REQUEST['output_format'] == 'astext') {
209 if (in_array($_REQUEST['compression'], $compression_methods)) {
210 $compression = $_REQUEST['compression'];
211 $buffer_needed = true;
213 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
214 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
217 $onserver = $_REQUEST['quick_export_onserver'];
219 $onserver = $_REQUEST['onserver'];
221 // Will we save dump on server?
222 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
226 // Generate error url and check for needed variables
227 if ($export_type == 'server') {
228 $err_url = 'server_export.php?' . PMA_URL_getCommon();
229 } elseif ($export_type == 'database' && strlen($db)) {
230 $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
231 // Check if we have something to export
232 if (isset($table_select)) {
233 $tables = $table_select;
237 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
238 $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
240 PMA_fatalError(__('Bad parameters!'));
244 * Increase time limit for script execution and initializes some variables
246 @set_time_limit
($cfg['ExecTimeLimit']);
247 if (! empty($cfg['MemoryLimit'])) {
248 @ini_set
('memory_limit', $cfg['MemoryLimit']);
250 register_shutdown_function('PMA_shutdownDuringExport');
251 // Start with empty buffer
253 $dump_buffer_len = 0;
255 // We send fake headers to avoid browser timeout when buffering
256 $time_start = time();
258 // Defines the default <CR><LF> format.
259 // For SQL always use \n as MySQL wants this on all platforms.
260 if ($what == 'sql') {
263 $crlf = PMA_Util
::whichCrlf();
266 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
269 // Do we need to convert charset?
270 $output_charset_conversion = $asfile
271 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
272 && isset($charset_of_file) && $charset_of_file != 'utf-8'
275 // Use on the fly compression?
276 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
277 && $compression == 'gzip';
278 if ($GLOBALS['onfly_compression']) {
279 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
282 // Generate filename and mime type if needed
284 if (empty($remember_template)) {
285 $remember_template = '';
287 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
288 $export_type, $remember_template, $export_plugin, $compression,
293 // Open file on server if needed
294 if ($save_on_server) {
295 list($save_filename, $message, $file_handle) = PMA_openExportFile(
296 $filename, $quick_export
299 // problem opening export file on server?
300 if (! empty($message)) {
301 PMA_showExportPage($export_type);
305 * Send headers depending on whether the user chose to download a dump file
310 // (avoid rewriting data containing HTML with anchors and forms;
311 // this was reported to happen under Plesk)
312 @ini_set
('url_rewriter.tags', '');
313 $filename = PMA_sanitizeFilename($filename);
315 PMA_downloadHeader($filename, $mime_type);
318 if ($export_type == 'database') {
319 $num_tables = count($tables);
320 if ($num_tables == 0) {
321 $message = PMA_Message
::error(
322 __('No tables found in database.')
324 $active_page = 'db_export.php';
325 include 'db_export.php';
329 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
330 $export_type, $db, $table
337 // Fake loop just to allow skip of remain of this code by break, I'd really
338 // need exceptions here :-)
341 // Add possibly some comments to export
342 if (! $export_plugin->exportHeader($db)) {
346 // Will we need relation & co. setup?
347 $do_relation = isset($GLOBALS[$what . '_relation']);
348 $do_comments = isset($GLOBALS[$what . '_include_comments'])
349 ||
isset($GLOBALS[$what . '_comments']) ;
350 $do_mime = isset($GLOBALS[$what . '_mime']);
351 if ($do_relation ||
$do_comments ||
$do_mime) {
352 $cfgRelation = PMA_getRelationsParam();
355 include_once 'libraries/transformations.lib.php';
358 // Include dates in export?
359 $do_dates = isset($GLOBALS[$what . '_dates']);
361 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
366 if ($export_type == 'server') {
367 if (! isset($db_select)) {
371 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
372 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
374 } elseif ($export_type == 'database') {
376 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
377 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
380 // We export just one table
381 // $allrows comes from the form when "Dump all rows" has been selected
382 if (! isset($allrows)) {
385 if (! isset($limit_to)) {
388 if (! isset($limit_from)) {
392 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
393 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
394 $allrows, $limit_to, $limit_from, $sql_query
397 if (! $export_plugin->exportFooter()) {
404 if ($save_on_server && ! empty($message)) {
405 PMA_showExportPage($export_type);
409 * Send the dump as a file...
411 if (! empty($asfile)) {
412 // Convert the charset if required.
413 if ($output_charset_conversion) {
414 $dump_buffer = PMA_convertString(
416 $GLOBALS['charset_of_file'],
421 // Compression needed?
424 = PMA_compressExport($dump_buffer, $compression, $filename);
427 /* If we saved on server, we have to close file now */
428 if ($save_on_server) {
429 $message = PMA_closeExportFile(
430 $file_handle, $dump_buffer, $save_filename
432 PMA_showExportPage($export_type);
437 echo PMA_getHtmlForDisplayedExportFooter($back_button);