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',
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',
90 'xml_structure_or_data',
92 'xml_export_functions',
93 'xml_export_procedures',
95 'xml_export_triggers',
97 'xml_export_contents',
98 'texytext_structure_or_data',
101 'phparray_structure_or_data',
102 'sql_include_comments',
103 'sql_header_comment',
107 'sql_use_transaction',
110 'sql_structure_or_data',
111 'sql_create_database',
113 'sql_procedure_function',
114 'sql_create_table_statements',
117 'sql_create_trigger',
119 'sql_auto_increment',
126 'sql_max_query_size',
127 'sql_hex_for_binary',
130 'sql_views_as_tables',
139 'csv_structure_or_data',
140 // csv_replace should have been here but we use it directly from $_POST
142 'latex_structure_or_data',
143 'latex_structure_caption',
144 'latex_structure_continued_caption',
145 'latex_structure_label',
150 'latex_data_caption',
151 'latex_data_continued_caption',
157 foreach ($post_params as $one_post_param) {
158 if (isset($_POST[$one_post_param])) {
159 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
163 $table = $GLOBALS['table'];
164 // sanitize this parameter which will be used below in a file inclusion
165 $what = PMA_securePath($_POST['what']);
167 PMA_Util
::checkParameters(array('what', 'export_type'));
169 // export class instance, not array of properties, as before
170 $export_plugin = PMA_getPlugin(
173 'libraries/plugins/export/',
175 'export_type' => $export_type,
176 'single_table' => isset($single_table)
180 // Backward compatibility
184 if (! isset($export_plugin)) {
185 PMA_fatalError(__('Bad type!'));
188 // Avoid warning from PHP Analyzer
189 if (is_null($export_plugin)) {
190 $export_plugin = new stdClass();
194 * valid compression methods
196 $compression_methods = array(
202 * init and variable checking
204 $compression = false;
206 $save_on_server = false;
207 $buffer_needed = false;
214 // Is it a quick or custom export?
215 if ($_REQUEST['quick_or_custom'] == 'quick') {
216 $quick_export = true;
218 $quick_export = false;
221 if ($_REQUEST['output_format'] == 'astext') {
225 if (in_array($_REQUEST['compression'], $compression_methods)) {
226 $compression = $_REQUEST['compression'];
227 $buffer_needed = true;
229 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
230 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
233 $onserver = $_REQUEST['quick_export_onserver'];
235 $onserver = $_REQUEST['onserver'];
237 // Will we save dump on server?
238 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
242 // Generate error url and check for needed variables
243 /** @var PMA_String $pmaString */
244 $pmaString = $GLOBALS['PMA_String'];
245 if ($export_type == 'server') {
246 $err_url = 'server_export.php' . PMA_URL_getCommon();
247 } elseif ($export_type == 'database'
248 && /*overload*/mb_strlen($db)
250 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
251 // Check if we have something to export
252 if (isset($table_select)) {
253 $tables = $table_select;
257 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
258 && /*overload*/mb_strlen($table)
260 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
262 'db' => $db, 'table' => $table
266 PMA_fatalError(__('Bad parameters!'));
269 // Merge SQL Query aliases with Export aliases from
270 // export page, Export page aliases are given more
271 // preference over SQL Query aliases.
272 if (!empty($_REQUEST['aliases'])) {
273 $aliases = PMA_mergeAliases(
274 PMA_SQP_getAliasesFromQuery($sql_query, $db),
277 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
279 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
283 * Increase time limit for script execution and initializes some variables
285 @set_time_limit
($cfg['ExecTimeLimit']);
286 if (! empty($cfg['MemoryLimit'])) {
287 @ini_set
('memory_limit', $cfg['MemoryLimit']);
289 register_shutdown_function('PMA_shutdownDuringExport');
290 // Start with empty buffer
292 $dump_buffer_len = 0;
294 // We send fake headers to avoid browser timeout when buffering
295 $time_start = time();
297 // Defines the default <CR><LF> format.
298 // For SQL always use \n as MySQL wants this on all platforms.
299 if ($what == 'sql') {
302 $crlf = PMA_Util
::whichCrlf();
305 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
308 // Do we need to convert charset?
309 $output_charset_conversion = $asfile
310 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
311 && isset($charset_of_file) && $charset_of_file != 'utf-8'
314 // Use on the fly compression?
315 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
316 && $compression == 'gzip';
317 if ($GLOBALS['onfly_compression']) {
318 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
321 // Generate filename and mime type if needed
323 if (empty($remember_template)) {
324 $remember_template = '';
326 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
327 $export_type, $remember_template, $export_plugin, $compression,
334 // Open file on server if needed
335 if ($save_on_server) {
336 list($save_filename, $message, $file_handle) = PMA_openExportFile(
337 $filename, $quick_export
340 // problem opening export file on server?
341 if (! empty($message)) {
342 PMA_showExportPage($db, $table, $export_type);
346 * Send headers depending on whether the user chose to download a dump file
351 // (avoid rewriting data containing HTML with anchors and forms;
352 // this was reported to happen under Plesk)
353 @ini_set
('url_rewriter.tags', '');
354 $filename = PMA_sanitizeFilename($filename);
356 PMA_downloadHeader($filename, $mime_type);
359 if ($export_type == 'database') {
360 $num_tables = count($tables);
361 if ($num_tables == 0) {
362 $message = PMA_Message
::error(
363 __('No tables found in database.')
365 $active_page = 'db_export.php';
366 include 'db_export.php';
370 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
371 $export_type, $db, $table
378 // Fake loop just to allow skip of remain of this code by break, I'd really
379 // need exceptions here :-)
382 // Add possibly some comments to export
383 if (! $export_plugin->exportHeader($db)) {
387 // Will we need relation & co. setup?
388 $do_relation = isset($GLOBALS[$what . '_relation']);
389 $do_comments = isset($GLOBALS[$what . '_include_comments'])
390 ||
isset($GLOBALS[$what . '_comments']) ;
391 $do_mime = isset($GLOBALS[$what . '_mime']);
392 if ($do_relation ||
$do_comments ||
$do_mime) {
393 $cfgRelation = PMA_getRelationsParam();
396 include_once 'libraries/transformations.lib.php';
399 // Include dates in export?
400 $do_dates = isset($GLOBALS[$what . '_dates']);
402 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
407 if ($export_type == 'server') {
408 if (! isset($db_select)) {
412 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
413 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
416 } elseif ($export_type == 'database') {
417 if (isset($lock_tables)) {
418 PMA_lockTables($db, $tables, "READ");
421 $db, $tables, $whatStrucOrData, $export_plugin, $crlf,
422 $err_url, $export_type, $do_relation, $do_comments,
423 $do_mime, $do_dates, $aliases
426 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
432 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
433 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
438 // We export just one table
439 // $allrows comes from the form when "Dump all rows" has been selected
440 if (! isset($allrows)) {
443 if (! isset($limit_to)) {
446 if (! isset($limit_from)) {
449 if (isset($lock_tables)) {
451 PMA_lockTables($db, array($table), "READ");
453 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
454 $err_url, $export_type, $do_relation, $do_comments,
455 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
459 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
465 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
466 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
467 $allrows, $limit_to, $limit_from, $sql_query, $aliases
471 if (! $export_plugin->exportFooter()) {
478 if ($save_on_server && ! empty($message)) {
479 PMA_showExportPage($db, $table, $export_type);
483 * Send the dump as a file...
485 if (! empty($asfile)) {
486 // Convert the charset if required.
487 if ($output_charset_conversion) {
488 $dump_buffer = PMA_convertString(
490 $GLOBALS['charset_of_file'],
495 // Compression needed?
498 = PMA_compressExport($dump_buffer, $compression, $filename);
501 /* If we saved on server, we have to close file now */
502 if ($save_on_server) {
503 $message = PMA_closeExportFile(
504 $file_handle, $dump_buffer, $save_filename
506 PMA_showExportPage($db, $table, $export_type);
511 echo PMA_getHtmlForDisplayedExportFooter($back_button);