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
67 'htmlword_structure_or_data',
71 'mediawiki_structure_or_data',
73 'pdf_structure_or_data',
74 'odt_structure_or_data',
80 'codegen_structure_or_data',
86 'excel_structure_or_data',
87 'yaml_structure_or_data',
89 'ods_structure_or_data',
91 'json_structure_or_data',
93 'xml_structure_or_data',
95 'xml_export_functions',
96 'xml_export_procedures',
98 'xml_export_triggers',
100 'xml_export_contents',
101 'texytext_structure_or_data',
104 'phparray_structure_or_data',
105 'sql_include_comments',
106 'sql_header_comment',
110 'sql_use_transaction',
113 'sql_structure_or_data',
114 'sql_create_database',
116 'sql_procedure_function',
117 'sql_create_table_statements',
120 'sql_create_trigger',
122 'sql_auto_increment',
129 'sql_max_query_size',
130 'sql_hex_for_binary',
133 'sql_views_as_tables',
142 'csv_structure_or_data',
143 // csv_replace should have been here but we use it directly from $_POST
145 'latex_structure_or_data',
146 'latex_structure_caption',
147 'latex_structure_continued_caption',
148 'latex_structure_label',
153 'latex_data_caption',
154 'latex_data_continued_caption',
160 foreach ($post_params as $one_post_param) {
161 if (isset($_POST[$one_post_param])) {
162 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
166 $table = $GLOBALS['table'];
167 // sanitize this parameter which will be used below in a file inclusion
168 $what = PMA_securePath($_POST['what']);
170 PMA_Util
::checkParameters(array('what', 'export_type'));
172 // export class instance, not array of properties, as before
173 /* @var $export_plugin ExportPlugin */
174 $export_plugin = PMA_getPlugin(
177 'libraries/plugins/export/',
179 'export_type' => $export_type,
180 'single_table' => isset($single_table)
184 // Backward compatibility
188 if (empty($export_plugin)) {
189 PMA_fatalError(__('Bad type!'));
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;
212 $separate_files = '';
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 (isset($_REQUEST['as_separate_files'])
226 && ! empty($_REQUEST['as_separate_files'])
228 if (isset($_REQUEST['compression'])
229 && ! empty($_REQUEST['compression'])
230 && $_REQUEST['compression'] == 'zip'
232 $separate_files = $_REQUEST['as_separate_files'];
235 if (in_array($_REQUEST['compression'], $compression_methods)) {
236 $compression = $_REQUEST['compression'];
237 $buffer_needed = true;
239 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
240 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
243 $onserver = $_REQUEST['quick_export_onserver'];
245 $onserver = $_REQUEST['onserver'];
247 // Will we save dump on server?
248 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
252 // Generate error url and check for needed variables
253 /** @var PMA_String $pmaString */
254 $pmaString = $GLOBALS['PMA_String'];
255 if ($export_type == 'server') {
256 $err_url = 'server_export.php' . PMA_URL_getCommon();
257 } elseif ($export_type == 'database'
258 && /*overload*/mb_strlen($db)
260 $err_url = 'db_export.php' . PMA_URL_getCommon(array('db' => $db));
261 // Check if we have something to export
262 if (isset($table_select)) {
263 $tables = $table_select;
267 } elseif ($export_type == 'table' && /*overload*/mb_strlen($db)
268 && /*overload*/mb_strlen($table)
270 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
272 'db' => $db, 'table' => $table
276 PMA_fatalError(__('Bad parameters!'));
279 // Merge SQL Query aliases with Export aliases from
280 // export page, Export page aliases are given more
281 // preference over SQL Query aliases.
282 $parser = new SqlParser\
Parser($sql_query);
284 if ((!empty($parser->statements
[0]))
285 && ($parser->statements
[0] instanceof SqlParser\Statements\SelectStatement
)
287 if (!empty($_REQUEST['aliases'])) {
288 $aliases = PMA_mergeAliases(
289 SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db),
292 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
294 $aliases = SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
299 * Increase time limit for script execution and initializes some variables
301 @set_time_limit
($cfg['ExecTimeLimit']);
302 if (! empty($cfg['MemoryLimit'])) {
303 @ini_set
('memory_limit', $cfg['MemoryLimit']);
305 register_shutdown_function('PMA_shutdownDuringExport');
306 // Start with empty buffer
308 $dump_buffer_len = 0;
310 // Array of dump_buffers - used in separate file exports
311 $dump_buffer_objects = array();
313 // We send fake headers to avoid browser timeout when buffering
314 $time_start = time();
316 // Defines the default <CR><LF> format.
317 // For SQL always use \n as MySQL wants this on all platforms.
318 if ($what == 'sql') {
321 $crlf = PMA_Util
::whichCrlf();
324 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
327 // Do we need to convert charset?
328 $output_charset_conversion = $asfile
329 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
330 && isset($charset) && $charset != 'utf-8'
333 // Use on the fly compression?
334 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
335 && $compression == 'gzip';
336 if ($GLOBALS['onfly_compression']) {
337 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
340 // Generate filename and mime type if needed
342 if (empty($remember_template)) {
343 $remember_template = '';
345 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
346 $export_type, $remember_template, $export_plugin, $compression,
353 // Open file on server if needed
354 if ($save_on_server) {
355 list($save_filename, $message, $file_handle) = PMA_openExportFile(
356 $filename, $quick_export
359 // problem opening export file on server?
360 if (! empty($message)) {
361 PMA_showExportPage($db, $table, $export_type);
365 * Send headers depending on whether the user chose to download a dump file
370 // (avoid rewriting data containing HTML with anchors and forms;
371 // this was reported to happen under Plesk)
372 @ini_set
('url_rewriter.tags', '');
373 $filename = PMA_sanitizeFilename($filename);
375 PMA_downloadHeader($filename, $mime_type);
378 if ($export_type == 'database') {
379 $num_tables = count($tables);
380 if ($num_tables == 0) {
381 $message = PMA_Message
::error(
382 __('No tables found in database.')
384 $active_page = 'db_export.php';
385 include 'db_export.php';
389 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
390 $export_type, $db, $table
397 // Fake loop just to allow skip of remain of this code by break, I'd really
398 // need exceptions here :-)
401 // Add possibly some comments to export
402 if (! $export_plugin->exportHeader()) {
407 $dump_buffer_len = 0;
409 // Will we need relation & co. setup?
410 $do_relation = isset($GLOBALS[$what . '_relation']);
411 $do_comments = isset($GLOBALS[$what . '_include_comments'])
412 ||
isset($GLOBALS[$what . '_comments']) ;
413 $do_mime = isset($GLOBALS[$what . '_mime']);
414 if ($do_relation ||
$do_comments ||
$do_mime) {
415 $cfgRelation = PMA_getRelationsParam();
418 include_once 'libraries/transformations.lib.php';
421 // Include dates in export?
422 $do_dates = isset($GLOBALS[$what . '_dates']);
424 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
429 if ($export_type == 'server') {
430 if (! isset($db_select)) {
434 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
435 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
436 $aliases, $separate_files
438 } elseif ($export_type == 'database') {
439 if (!isset($table_structure) ||
!is_array($table_structure)) {
440 $table_structure = array();
442 if (!isset($table_data) ||
!is_array($table_data)) {
443 $table_data = array();
445 if (!empty($_REQUEST['structure_or_data_forced'])) {
446 $table_structure = $tables;
447 $table_data = $tables;
449 if (isset($lock_tables)) {
450 PMA_lockTables($db, $tables, "READ");
453 $db, $tables, $whatStrucOrData, $table_structure,
454 $table_data, $export_plugin, $crlf, $err_url, $export_type,
455 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
459 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
465 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
466 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
467 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
471 // We export just one table
472 // $allrows comes from the form when "Dump all rows" has been selected
473 if (! isset($allrows)) {
476 if (! isset($limit_to)) {
479 if (! isset($limit_from)) {
482 if (isset($lock_tables)) {
484 PMA_lockTables($db, array($table), "READ");
486 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
487 $err_url, $export_type, $do_relation, $do_comments,
488 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
492 } catch (Exception
$e) { // TODO use finally when PHP version is 5.5
498 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
499 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
500 $allrows, $limit_to, $limit_from, $sql_query, $aliases
504 if (! $export_plugin->exportFooter()) {
511 if ($save_on_server && ! empty($message)) {
512 PMA_showExportPage($db, $table, $export_type);
516 * Send the dump as a file...
518 if (empty($asfile)) {
519 echo PMA_getHtmlForDisplayedExportFooter($back_button);
523 // Convert the charset if required.
524 if ($output_charset_conversion) {
525 $dump_buffer = PMA_convertString(
532 // Compression needed?
534 if (! empty($separate_files)) {
535 $dump_buffer = PMA_compressExport(
536 $dump_buffer_objects, $compression, $filename
539 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
544 /* If we saved on server, we have to close file now */
545 if ($save_on_server) {
546 $message = PMA_closeExportFile(
547 $file_handle, $dump_buffer, $save_filename
549 PMA_showExportPage($db, $table, $export_type);