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',
138 'csv_structure_or_data',
139 // csv_replace should have been here but we use it directly from $_POST
141 'latex_structure_or_data',
142 'latex_structure_caption',
143 'latex_structure_continued_caption',
144 'latex_structure_label',
149 'latex_data_caption',
150 'latex_data_continued_caption',
156 foreach ($post_params as $one_post_param) {
157 if (isset($_POST[$one_post_param])) {
158 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
162 $table = $GLOBALS['table'];
163 // sanitize this parameter which will be used below in a file inclusion
164 $what = PMA_securePath($_POST['what']);
166 PMA_Util
::checkParameters(array('what', 'export_type'));
168 // export class instance, not array of properties, as before
169 $export_plugin = PMA_getPlugin(
172 'libraries/plugins/export/',
174 'export_type' => $export_type,
175 'single_table' => isset($single_table)
179 // Backward compatibility
183 if (! isset($export_plugin)) {
184 PMA_fatalError(__('Bad type!'));
187 // Avoid warning from PHP Analyzer
188 if (is_null($export_plugin)) {
189 $export_plugin = new stdClass();
193 * valid compression methods
195 $compression_methods = array(
201 * init and variable checking
203 $compression = false;
205 $save_on_server = false;
206 $buffer_needed = false;
213 // Is it a quick or custom export?
214 if ($_REQUEST['quick_or_custom'] == 'quick') {
215 $quick_export = true;
217 $quick_export = false;
220 if ($_REQUEST['output_format'] == 'astext') {
224 if (in_array($_REQUEST['compression'], $compression_methods)) {
225 $compression = $_REQUEST['compression'];
226 $buffer_needed = true;
228 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
229 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
232 $onserver = $_REQUEST['quick_export_onserver'];
234 $onserver = $_REQUEST['onserver'];
236 // Will we save dump on server?
237 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
241 // Generate error url and check for needed variables
242 /** @var PMA_String $pmaString */
243 $pmaString = $GLOBALS['PMA_String'];
244 if ($export_type == 'server') {
245 $err_url = 'server_export.php' . PMA_URL_getCommon();
246 } elseif ($export_type == 'database'
247 && /*overload*/mb_strlen($db)
249 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
250 // Check if we have something to export
251 if (isset($table_select)) {
252 $tables = $table_select;
256 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
257 && /*overload*/mb_strlen($table)
259 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
261 'db' => $db, 'table' => $table
265 PMA_fatalError(__('Bad parameters!'));
268 // Merge SQL Query aliases with Export aliases from
269 // export page, Export page aliases are given more
270 // preference over SQL Query aliases.
271 if (!empty($_REQUEST['aliases'])) {
272 $aliases = PMA_mergeAliases(
273 PMA_SQP_getAliasesFromQuery($sql_query, $db),
276 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
278 $aliases = PMA_SQP_getAliasesFromQuery($sql_query, $db);
282 * Increase time limit for script execution and initializes some variables
284 @set_time_limit
($cfg['ExecTimeLimit']);
285 if (! empty($cfg['MemoryLimit'])) {
286 @ini_set
('memory_limit', $cfg['MemoryLimit']);
288 register_shutdown_function('PMA_shutdownDuringExport');
289 // Start with empty buffer
291 $dump_buffer_len = 0;
293 // We send fake headers to avoid browser timeout when buffering
294 $time_start = time();
296 // Defines the default <CR><LF> format.
297 // For SQL always use \n as MySQL wants this on all platforms.
298 if ($what == 'sql') {
301 $crlf = PMA_Util
::whichCrlf();
304 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
307 // Do we need to convert charset?
308 $output_charset_conversion = $asfile
309 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
310 && isset($charset_of_file) && $charset_of_file != 'utf-8'
313 // Use on the fly compression?
314 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
315 && $compression == 'gzip';
316 if ($GLOBALS['onfly_compression']) {
317 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
320 // Generate filename and mime type if needed
322 if (empty($remember_template)) {
323 $remember_template = '';
325 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
326 $export_type, $remember_template, $export_plugin, $compression,
333 // Open file on server if needed
334 if ($save_on_server) {
335 list($save_filename, $message, $file_handle) = PMA_openExportFile(
336 $filename, $quick_export
339 // problem opening export file on server?
340 if (! empty($message)) {
341 PMA_showExportPage($db, $table, $export_type);
345 * Send headers depending on whether the user chose to download a dump file
350 // (avoid rewriting data containing HTML with anchors and forms;
351 // this was reported to happen under Plesk)
352 @ini_set
('url_rewriter.tags', '');
353 $filename = PMA_sanitizeFilename($filename);
355 PMA_downloadHeader($filename, $mime_type);
358 if ($export_type == 'database') {
359 $num_tables = count($tables);
360 if ($num_tables == 0) {
361 $message = PMA_Message
::error(
362 __('No tables found in database.')
364 $active_page = 'db_export.php';
365 include 'db_export.php';
369 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
370 $export_type, $db, $table
377 // Fake loop just to allow skip of remain of this code by break, I'd really
378 // need exceptions here :-)
381 // Add possibly some comments to export
382 if (! $export_plugin->exportHeader($db)) {
386 // Will we need relation & co. setup?
387 $do_relation = isset($GLOBALS[$what . '_relation']);
388 $do_comments = isset($GLOBALS[$what . '_include_comments'])
389 ||
isset($GLOBALS[$what . '_comments']) ;
390 $do_mime = isset($GLOBALS[$what . '_mime']);
391 if ($do_relation ||
$do_comments ||
$do_mime) {
392 $cfgRelation = PMA_getRelationsParam();
395 include_once 'libraries/transformations.lib.php';
398 // Include dates in export?
399 $do_dates = isset($GLOBALS[$what . '_dates']);
401 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
406 if ($export_type == 'server') {
407 if (! isset($db_select)) {
411 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
412 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
415 } elseif ($export_type == 'database') {
416 if (isset($lock_tables)) {
417 PMA_lockTables($db, $tables, "READ");
420 $db, $tables, $whatStrucOrData, $export_plugin, $crlf,
421 $err_url, $export_type, $do_relation, $do_comments,
422 $do_mime, $do_dates, $aliases
425 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
431 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
432 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
437 // We export just one table
438 // $allrows comes from the form when "Dump all rows" has been selected
439 if (! isset($allrows)) {
442 if (! isset($limit_to)) {
445 if (! isset($limit_from)) {
448 if (isset($lock_tables)) {
450 PMA_lockTables($db, array($table), "READ");
452 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
453 $err_url, $export_type, $do_relation, $do_comments,
454 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
458 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
464 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
465 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
466 $allrows, $limit_to, $limit_from, $sql_query, $aliases
470 if (! $export_plugin->exportFooter()) {
477 if ($save_on_server && ! empty($message)) {
478 PMA_showExportPage($db, $table, $export_type);
482 * Send the dump as a file...
484 if (! empty($asfile)) {
485 // Convert the charset if required.
486 if ($output_charset_conversion) {
487 $dump_buffer = PMA_convertString(
489 $GLOBALS['charset_of_file'],
494 // Compression needed?
497 = PMA_compressExport($dump_buffer, $compression, $filename);
500 /* If we saved on server, we have to close file now */
501 if ($save_on_server) {
502 $message = PMA_closeExportFile(
503 $file_handle, $dump_buffer, $save_filename
505 PMA_showExportPage($db, $table, $export_type);
510 echo PMA_getHtmlForDisplayedExportFooter($back_button);