2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
10 use PhpMyAdmin\Encoding
;
11 use PhpMyAdmin\Plugins\ExportPlugin
;
12 use PhpMyAdmin\Relation
;
13 use PhpMyAdmin\Sanitize
;
18 * Get the variables sent or posted to this script and a core script
21 * If we are sending the export file (as opposed to just displaying it
22 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
24 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
25 define('PMA_BYPASS_GET_INSTANCE', 1);
27 include_once 'libraries/common.inc.php';
28 include_once 'libraries/plugin_interface.lib.php';
29 include_once 'libraries/export.lib.php';
31 //check if it's the GET request to check export time out
32 if (isset($_GET['check_time_out'])) {
33 if (isset($_SESSION['pma_export_error'])) {
34 $err = $_SESSION['pma_export_error'];
35 unset($_SESSION['pma_export_error']);
43 * Sets globals from $_POST
45 * - Please keep the parameters in order of their appearance in the form
46 * - Some of these parameters are not used, as the code below directly
47 * verifies from the superglobal $_POST or $_REQUEST
48 * TODO: this should be removed to avoid passing user input to GLOBALS
76 'htmlword_structure_or_data',
80 'mediawiki_structure_or_data',
82 'pdf_structure_or_data',
83 'odt_structure_or_data',
89 'codegen_structure_or_data',
95 'excel_structure_or_data',
96 'yaml_structure_or_data',
98 'ods_structure_or_data',
100 'json_structure_or_data',
103 'xml_structure_or_data',
105 'xml_export_functions',
106 'xml_export_procedures',
108 'xml_export_triggers',
110 'xml_export_contents',
111 'texytext_structure_or_data',
114 'phparray_structure_or_data',
115 'sql_include_comments',
116 'sql_header_comment',
120 'sql_use_transaction',
123 'sql_structure_or_data',
124 'sql_create_database',
126 'sql_procedure_function',
129 'sql_create_trigger',
131 'sql_auto_increment',
138 'sql_max_query_size',
139 'sql_hex_for_binary',
142 'sql_views_as_tables',
151 'csv_structure_or_data',
152 // csv_replace should have been here but we use it directly from $_POST
154 'latex_structure_or_data',
155 'latex_structure_caption',
156 'latex_structure_continued_caption',
157 'latex_structure_label',
162 'latex_data_caption',
163 'latex_data_continued_caption',
169 foreach ($post_params as $one_post_param) {
170 if (isset($_POST[$one_post_param])) {
171 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
175 $table = $GLOBALS['table'];
177 PhpMyAdmin\Util
::checkParameters(array('what', 'export_type'));
179 // sanitize this parameter which will be used below in a file inclusion
180 $what = Core
::securePath($_POST['what']);
182 // export class instance, not array of properties, as before
183 /* @var $export_plugin ExportPlugin */
184 $export_plugin = PMA_getPlugin(
187 'libraries/classes/Plugins/Export/',
189 'export_type' => $export_type,
190 'single_table' => isset($single_table)
195 if (empty($export_plugin)) {
196 Core
::fatalError(__('Bad type!'));
200 * valid compression methods
202 $compression_methods = array(
208 * init and variable checking
210 $compression = false;
212 $save_on_server = false;
213 $buffer_needed = false;
219 $separate_files = '';
221 // Is it a quick or custom export?
222 if (isset($_REQUEST['quick_or_custom'])
223 && $_REQUEST['quick_or_custom'] == 'quick'
225 $quick_export = true;
227 $quick_export = false;
230 if ($_REQUEST['output_format'] == 'astext') {
234 if (isset($_REQUEST['as_separate_files'])
235 && ! empty($_REQUEST['as_separate_files'])
237 if (isset($_REQUEST['compression'])
238 && ! empty($_REQUEST['compression'])
239 && $_REQUEST['compression'] == 'zip'
241 $separate_files = $_REQUEST['as_separate_files'];
244 if (in_array($_REQUEST['compression'], $compression_methods)) {
245 $compression = $_REQUEST['compression'];
246 $buffer_needed = true;
248 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
249 ||
(! $quick_export && ! empty($_REQUEST['onserver']))
252 $onserver = $_REQUEST['quick_export_onserver'];
254 $onserver = $_REQUEST['onserver'];
256 // Will we save dump on server?
257 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
261 // Generate error url and check for needed variables
262 if ($export_type == 'server') {
263 $err_url = 'server_export.php' . Url
::getCommon();
264 } elseif ($export_type == 'database' && strlen($db) > 0) {
265 $err_url = 'db_export.php' . Url
::getCommon(array('db' => $db));
266 // Check if we have something to export
267 if (isset($table_select)) {
268 $tables = $table_select;
272 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
273 $err_url = 'tbl_export.php' . Url
::getCommon(
275 'db' => $db, 'table' => $table
279 Core
::fatalError(__('Bad parameters!'));
282 // Merge SQL Query aliases with Export aliases from
283 // export page, Export page aliases are given more
284 // preference over SQL Query aliases.
285 $parser = new \PhpMyAdmin\SqlParser\
Parser($sql_query);
287 if ((!empty($parser->statements
[0]))
288 && ($parser->statements
[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement
)
290 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc
::getAliases($parser->statements
[0], $db);
292 if (!empty($_REQUEST['aliases'])) {
293 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
294 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
298 * Increase time limit for script execution and initializes some variables
300 Util
::setTimeLimit();
301 if (! empty($cfg['MemoryLimit'])) {
302 @ini_set
('memory_limit', $cfg['MemoryLimit']);
304 register_shutdown_function('PMA_shutdownDuringExport');
305 // Start with empty buffer
307 $dump_buffer_len = 0;
309 // Array of dump_buffers - used in separate file exports
310 $dump_buffer_objects = array();
312 // We send fake headers to avoid browser timeout when buffering
313 $time_start = time();
315 // Defines the default <CR><LF> format.
316 // For SQL always use \n as MySQL wants this on all platforms.
317 if ($what == 'sql') {
323 $output_kanji_conversion = Encoding
::canConvertKanji();
325 // Do we need to convert charset?
326 $output_charset_conversion = $asfile
327 && Encoding
::isSupported()
328 && isset($charset) && $charset != 'utf-8';
330 // Use on the fly compression?
331 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
332 && $compression == 'gzip';
333 if ($GLOBALS['onfly_compression']) {
334 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
337 // Generate filename and mime type if needed
339 if (empty($remember_template)) {
340 $remember_template = '';
342 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
343 $export_type, $remember_template, $export_plugin, $compression,
350 // Open file on server if needed
351 if ($save_on_server) {
352 list($save_filename, $message, $file_handle) = PMA_openExportFile(
353 $filename, $quick_export
356 // problem opening export file on server?
357 if (! empty($message)) {
358 PMA_showExportPage($db, $table, $export_type);
362 * Send headers depending on whether the user chose to download a dump file
367 // (avoid rewriting data containing HTML with anchors and forms;
368 // this was reported to happen under Plesk)
369 @ini_set
('url_rewriter.tags', '');
370 $filename = Sanitize
::sanitizeFilename($filename);
372 Core
::downloadHeader($filename, $mime_type);
375 if ($export_type == 'database') {
376 $num_tables = count($tables);
377 if ($num_tables == 0) {
378 $message = PhpMyAdmin\Message
::error(
379 __('No tables found in database.')
381 $active_page = 'db_export.php';
382 include 'db_export.php';
386 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
387 $export_type, $db, $table
394 // Fake loop just to allow skip of remain of this code by break, I'd really
395 // need exceptions here :-)
399 $dump_buffer_len = 0;
401 // Add possibly some comments to export
402 if (! $export_plugin->exportHeader()) {
406 // Will we need relation & co. setup?
407 $do_relation = isset($GLOBALS[$what . '_relation']);
408 $do_comments = isset($GLOBALS[$what . '_include_comments'])
409 ||
isset($GLOBALS[$what . '_comments']);
410 $do_mime = isset($GLOBALS[$what . '_mime']);
411 if ($do_relation ||
$do_comments ||
$do_mime) {
412 $cfgRelation = Relation
::getRelationsParam();
415 // Include dates in export?
416 $do_dates = isset($GLOBALS[$what . '_dates']);
418 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
423 if ($export_type == 'server') {
424 if (! isset($db_select)) {
428 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
429 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
430 $aliases, $separate_files
432 } elseif ($export_type == 'database') {
433 if (!isset($table_structure) ||
!is_array($table_structure)) {
434 $table_structure = array();
436 if (!isset($table_data) ||
!is_array($table_data)) {
437 $table_data = array();
439 if (!empty($_REQUEST['structure_or_data_forced'])) {
440 $table_structure = $tables;
441 $table_data = $tables;
443 if (isset($lock_tables)) {
444 PMA_lockTables($db, $tables, "READ");
447 $db, $tables, $whatStrucOrData, $table_structure,
448 $table_data, $export_plugin, $crlf, $err_url, $export_type,
449 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
457 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
458 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
459 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
463 // We export just one table
464 // $allrows comes from the form when "Dump all rows" has been selected
465 if (! isset($allrows)) {
468 if (! isset($limit_to)) {
471 if (! isset($limit_from)) {
474 if (isset($lock_tables)) {
476 PMA_lockTables($db, array($table), "READ");
478 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
479 $err_url, $export_type, $do_relation, $do_comments,
480 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
488 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
489 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
490 $allrows, $limit_to, $limit_from, $sql_query, $aliases
494 if (! $export_plugin->exportFooter()) {
501 if ($save_on_server && ! empty($message)) {
502 PMA_showExportPage($db, $table, $export_type);
506 * Send the dump as a file...
508 if (empty($asfile)) {
509 echo PMA_getHtmlForDisplayedExportFooter($back_button);
513 // Convert the charset if required.
514 if ($output_charset_conversion) {
515 $dump_buffer = Encoding
::convertString(
522 // Compression needed?
524 if (! empty($separate_files)) {
525 $dump_buffer = PMA_compressExport(
526 $dump_buffer_objects, $compression, $filename
529 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
534 /* If we saved on server, we have to close file now */
535 if ($save_on_server) {
536 $message = PMA_closeExportFile(
537 $file_handle, $dump_buffer, $save_filename
539 PMA_showExportPage($db, $table, $export_type);