2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
8 declare(strict_types
=1);
11 use PhpMyAdmin\DatabaseInterface
;
12 use PhpMyAdmin\Encoding
;
13 use PhpMyAdmin\Export
;
14 use PhpMyAdmin\Plugins
;
15 use PhpMyAdmin\Plugins\ExportPlugin
;
16 use PhpMyAdmin\Relation
;
17 use PhpMyAdmin\Response
;
18 use PhpMyAdmin\Sanitize
;
19 use PhpMyAdmin\SqlParser\Parser
;
20 use PhpMyAdmin\SqlParser\Statements\SelectStatement
;
21 use PhpMyAdmin\SqlParser\Utils\Misc
;
25 if (! defined('ROOT_PATH')) {
26 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
29 global $db, $sql_query;
31 include_once ROOT_PATH
. 'libraries/common.inc.php';
33 /** @var Response $response */
34 $response = $containerBuilder->get(Response
::class);
36 /** @var DatabaseInterface $dbi */
37 $dbi = $containerBuilder->get(DatabaseInterface
::class);
39 $header = $response->getHeader();
40 $scripts = $header->getScripts();
41 $scripts->addFile('export_output.js');
43 /** @var Export $export */
44 $export = $containerBuilder->get('export');
46 //check if it's the GET request to check export time out
47 if (isset($_GET['check_time_out'])) {
48 if (isset($_SESSION['pma_export_error'])) {
49 $err = $_SESSION['pma_export_error'];
50 unset($_SESSION['pma_export_error']);
59 * Sets globals from $_POST
61 * - Please keep the parameters in order of their appearance in the form
62 * - Some of these parameters are not used, as the code below directly
63 * verifies from the superglobal $_POST or $_REQUEST
64 * TODO: this should be removed to avoid passing user input to GLOBALS
92 'htmlword_structure_or_data',
96 'mediawiki_structure_or_data',
98 'pdf_structure_or_data',
99 'odt_structure_or_data',
105 'codegen_structure_or_data',
111 'excel_structure_or_data',
112 'yaml_structure_or_data',
114 'ods_structure_or_data',
116 'json_structure_or_data',
119 'xml_structure_or_data',
121 'xml_export_functions',
122 'xml_export_procedures',
124 'xml_export_triggers',
126 'xml_export_contents',
127 'texytext_structure_or_data',
130 'phparray_structure_or_data',
131 'sql_include_comments',
132 'sql_header_comment',
136 'sql_use_transaction',
139 'sql_structure_or_data',
140 'sql_create_database',
142 'sql_procedure_function',
145 'sql_create_trigger',
146 'sql_view_current_user',
148 'sql_or_replace_view',
149 'sql_auto_increment',
156 'sql_max_query_size',
157 'sql_hex_for_binary',
160 'sql_views_as_tables',
169 'csv_structure_or_data',
170 // csv_replace should have been here but we use it directly from $_POST
172 'latex_structure_or_data',
173 'latex_structure_caption',
174 'latex_structure_continued_caption',
175 'latex_structure_label',
180 'latex_data_caption',
181 'latex_data_continued_caption',
187 foreach ($post_params as $one_post_param) {
188 if (isset($_POST[$one_post_param])) {
189 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
193 $table = $GLOBALS['table'];
195 PhpMyAdmin\Util
::checkParameters(['what', 'export_type']);
197 // sanitize this parameter which will be used below in a file inclusion
198 $what = Core
::securePath($_POST['what']);
200 // export class instance, not array of properties, as before
201 /** @var ExportPlugin $export_plugin */
202 $export_plugin = Plugins
::getPlugin(
205 'libraries/classes/Plugins/Export/',
207 'export_type' => $export_type,
208 'single_table' => isset($single_table),
213 if (empty($export_plugin)) {
214 Core
::fatalError(__('Bad type!'));
218 * valid compression methods
220 $compression_methods = [
226 * init and variable checking
230 $save_on_server = false;
231 $buffer_needed = false;
238 $separate_files = '';
240 // Is it a quick or custom export?
241 if (isset($_POST['quick_or_custom'])
242 && $_POST['quick_or_custom'] == 'quick'
244 $quick_export = true;
246 $quick_export = false;
249 if ($_POST['output_format'] == 'astext') {
253 if (isset($_POST['as_separate_files'])
254 && ! empty($_POST['as_separate_files'])
256 if (isset($_POST['compression'])
257 && ! empty($_POST['compression'])
258 && $_POST['compression'] == 'zip'
260 $separate_files = $_POST['as_separate_files'];
263 if (in_array($_POST['compression'], $compression_methods)) {
264 $compression = $_POST['compression'];
265 $buffer_needed = true;
267 if (($quick_export && ! empty($_POST['quick_export_onserver']))
268 ||
(! $quick_export && ! empty($_POST['onserver']))
271 $onserver = $_POST['quick_export_onserver'];
273 $onserver = $_POST['onserver'];
275 // Will we save dump on server?
276 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
281 * If we are sending the export file (as opposed to just displaying it
282 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
284 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit' && ! $save_on_server) {
285 $response->disable();
286 //Disable all active buffers (see: ob_get_status(true) at this point)
288 $hasBuffer = @ob_end_clean
();
289 } while ($hasBuffer);
293 // Generate error url and check for needed variables
294 if ($export_type == 'server') {
295 $err_url = 'server_export.php' . Url
::getCommon();
296 } elseif ($export_type == 'database' && strlen($db) > 0) {
297 $err_url = 'db_export.php' . Url
::getCommon(['db' => $db]);
298 // Check if we have something to export
299 if (isset($table_select)) {
300 $tables = $table_select;
304 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
305 $err_url = 'tbl_export.php' . Url
::getCommon(
312 Core
::fatalError(__('Bad parameters!'));
315 // Merge SQL Query aliases with Export aliases from
316 // export page, Export page aliases are given more
317 // preference over SQL Query aliases.
318 $parser = new Parser($sql_query);
320 if (! empty($parser->statements
[0])
321 && ($parser->statements
[0] instanceof SelectStatement
)
323 $aliases = Misc
::getAliases($parser->statements
[0], $db);
325 if (! empty($_POST['aliases'])) {
326 $aliases = $export->mergeAliases($aliases, $_POST['aliases']);
327 $_SESSION['tmpval']['aliases'] = $_POST['aliases'];
331 * Increase time limit for script execution and initializes some variables
333 Util
::setTimeLimit();
334 if (! empty($cfg['MemoryLimit'])) {
335 ini_set('memory_limit', $cfg['MemoryLimit']);
337 register_shutdown_function([$export, 'shutdown']);
338 // Start with empty buffer
340 $dump_buffer_len = 0;
342 // Array of dump_buffers - used in separate file exports
343 $dump_buffer_objects = [];
345 // We send fake headers to avoid browser timeout when buffering
346 $time_start = time();
348 // Defines the default <CR><LF> format.
349 // For SQL always use \n as MySQL wants this on all platforms.
350 if ($what == 'sql') {
356 $output_kanji_conversion = Encoding
::canConvertKanji();
358 // Do we need to convert charset?
359 $output_charset_conversion = $asfile
360 && Encoding
::isSupported()
361 && isset($charset) && $charset != 'utf-8';
363 // Use on the fly compression?
364 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
365 && $compression == 'gzip';
366 if ($GLOBALS['onfly_compression']) {
367 $GLOBALS['memory_limit'] = $export->getMemoryLimit();
370 // Generate filename and mime type if needed
372 if (empty($remember_template)) {
373 $remember_template = '';
375 list($filename, $mime_type) = $export->getFilenameAndMimetype(
386 // Open file on server if needed
387 if ($save_on_server) {
388 list($save_filename, $message, $file_handle) = $export->openFile(
393 // problem opening export file on server?
394 if (! empty($message)) {
395 $export->showPage($db, $table, $export_type);
399 * Send headers depending on whether the user chose to download a dump file
404 // (avoid rewriting data containing HTML with anchors and forms;
405 // this was reported to happen under Plesk)
406 ini_set('url_rewriter.tags', '');
407 $filename = Sanitize
::sanitizeFilename($filename);
409 Core
::downloadHeader($filename, $mime_type);
412 if ($export_type == 'database') {
413 $num_tables = count($tables);
414 if ($num_tables == 0) {
415 $message = PhpMyAdmin\Message
::error(
416 __('No tables found in database.')
418 $active_page = 'db_export.php';
419 include ROOT_PATH
. 'db_export.php';
423 list($html, $back_button, $refreshButton) = $export->getHtmlForDisplayedExportHeader(
433 /** @var Relation $relation */
434 $relation = $containerBuilder->get('relation');
436 // Fake loop just to allow skip of remain of this code by break, I'd really
437 // need exceptions here :-)
441 $dump_buffer_len = 0;
443 // Add possibly some comments to export
444 if (! $export_plugin->exportHeader()) {
448 // Will we need relation & co. setup?
449 $do_relation = isset($GLOBALS[$what . '_relation']);
450 $do_comments = isset($GLOBALS[$what . '_include_comments'])
451 ||
isset($GLOBALS[$what . '_comments']);
452 $do_mime = isset($GLOBALS[$what . '_mime']);
453 if ($do_relation ||
$do_comments ||
$do_mime) {
454 $cfgRelation = $relation->getRelationsParam();
457 // Include dates in export?
458 $do_dates = isset($GLOBALS[$what . '_dates']);
460 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
465 if ($export_type == 'server') {
466 if (! isset($db_select)) {
469 $export->exportServer(
483 } elseif ($export_type == 'database') {
484 if (! isset($table_structure) ||
! is_array($table_structure)) {
485 $table_structure = [];
487 if (! isset($table_data) ||
! is_array($table_data)) {
490 if (! empty($_POST['structure_or_data_forced'])) {
491 $table_structure = $tables;
492 $table_data = $tables;
494 if (isset($lock_tables)) {
495 $export->lockTables($db, $tables, "READ");
497 $export->exportDatabase(
515 $export->unlockTables();
518 $export->exportDatabase(
537 // We export just one table
538 // $allrows comes from the form when "Dump all rows" has been selected
539 if (! isset($allrows)) {
542 if (! isset($limit_to)) {
545 if (! isset($limit_from)) {
548 if (isset($lock_tables)) {
550 $export->lockTables($db, [$table], "READ");
551 $export->exportTable(
570 $export->unlockTables();
573 $export->exportTable(
593 if (! $export_plugin->exportFooter()) {
599 if ($save_on_server && ! empty($message)) {
600 $export->showPage($db, $table, $export_type);
604 * Send the dump as a file...
606 if (empty($asfile)) {
607 echo $export->getHtmlForDisplayedExportFooter($back_button, $refreshButton);
611 // Convert the charset if required.
612 if ($output_charset_conversion) {
613 $dump_buffer = Encoding
::convertString(
620 // Compression needed?
622 if (! empty($separate_files)) {
623 $dump_buffer = $export->compress(
624 $dump_buffer_objects,
629 $dump_buffer = $export->compress($dump_buffer, $compression, $filename);
633 /* If we saved on server, we have to close file now */
634 if ($save_on_server) {
635 $message = $export->closeFile(
640 $export->showPage($db, $table, $export_type);