Translated using Weblate (Bengali)
[phpmyadmin.git] / export.php
blob17ec404776545f5344c3c0404241bd113d95d05b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\Encoding;
9 use PMA\libraries\plugins\ExportPlugin;
10 use PMA\libraries\URL;
11 use PMA\libraries\Sanitize;
13 /**
14 * Get the variables sent or posted to this script and a core script
16 /**
17 * If we are sending the export file (as opposed to just displaying it
18 * as text), we have to bypass the usual PMA\libraries\Response mechanism
20 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
21 define('PMA_BYPASS_GET_INSTANCE', 1);
23 include_once 'libraries/common.inc.php';
24 include_once 'libraries/plugin_interface.lib.php';
25 include_once 'libraries/export.lib.php';
27 //check if it's the GET request to check export time out
28 if (isset($_GET['check_time_out'])) {
29 if (isset($_SESSION['pma_export_error'])) {
30 $err = $_SESSION['pma_export_error'];
31 unset($_SESSION['pma_export_error']);
32 echo "timeout";
33 } else {
34 echo "success";
36 exit;
38 /**
39 * Sets globals from $_POST
41 * - Please keep the parameters in order of their appearance in the form
42 * - Some of these parameters are not used, as the code below directly
43 * verifies from the superglobal $_POST or $_REQUEST
44 * TODO: this should be removed to avoid passing user input to GLOBALS
45 * without checking
47 $post_params = array(
48 'db',
49 'table',
50 'what',
51 'single_table',
52 'export_type',
53 'export_method',
54 'quick_or_custom',
55 'db_select',
56 'table_select',
57 'table_structure',
58 'table_data',
59 'limit_to',
60 'limit_from',
61 'allrows',
62 'lock_tables',
63 'output_format',
64 'filename_template',
65 'maxsize',
66 'remember_template',
67 'charset',
68 'compression',
69 'as_separate_files',
70 'knjenc',
71 'xkana',
72 'htmlword_structure_or_data',
73 'htmlword_null',
74 'htmlword_columns',
75 'mediawiki_headers',
76 'mediawiki_structure_or_data',
77 'mediawiki_caption',
78 'pdf_structure_or_data',
79 'odt_structure_or_data',
80 'odt_relation',
81 'odt_comments',
82 'odt_mime',
83 'odt_columns',
84 'odt_null',
85 'codegen_structure_or_data',
86 'codegen_format',
87 'excel_null',
88 'excel_removeCRLF',
89 'excel_columns',
90 'excel_edition',
91 'excel_structure_or_data',
92 'yaml_structure_or_data',
93 'ods_null',
94 'ods_structure_or_data',
95 'ods_columns',
96 'json_structure_or_data',
97 'json_pretty_print',
98 'xml_structure_or_data',
99 'xml_export_events',
100 'xml_export_functions',
101 'xml_export_procedures',
102 'xml_export_tables',
103 'xml_export_triggers',
104 'xml_export_views',
105 'xml_export_contents',
106 'texytext_structure_or_data',
107 'texytext_columns',
108 'texytext_null',
109 'phparray_structure_or_data',
110 'sql_include_comments',
111 'sql_header_comment',
112 'sql_dates',
113 'sql_relation',
114 'sql_mime',
115 'sql_use_transaction',
116 'sql_disable_fk',
117 'sql_compatibility',
118 'sql_structure_or_data',
119 'sql_create_database',
120 'sql_drop_table',
121 'sql_procedure_function',
122 'sql_create_table',
123 'sql_create_view',
124 'sql_create_trigger',
125 'sql_if_not_exists',
126 'sql_auto_increment',
127 'sql_backquotes',
128 'sql_truncate',
129 'sql_delayed',
130 'sql_ignore',
131 'sql_type',
132 'sql_insert_syntax',
133 'sql_max_query_size',
134 'sql_hex_for_binary',
135 'sql_utc_time',
136 'sql_drop_database',
137 'sql_views_as_tables',
138 'sql_metadata',
139 'csv_separator',
140 'csv_enclosed',
141 'csv_escaped',
142 'csv_terminated',
143 'csv_null',
144 'csv_removeCRLF',
145 'csv_columns',
146 'csv_structure_or_data',
147 // csv_replace should have been here but we use it directly from $_POST
148 'latex_caption',
149 'latex_structure_or_data',
150 'latex_structure_caption',
151 'latex_structure_continued_caption',
152 'latex_structure_label',
153 'latex_relation',
154 'latex_comments',
155 'latex_mime',
156 'latex_columns',
157 'latex_data_caption',
158 'latex_data_continued_caption',
159 'latex_data_label',
160 'latex_null',
161 'aliases'
164 foreach ($post_params as $one_post_param) {
165 if (isset($_POST[$one_post_param])) {
166 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
170 $table = $GLOBALS['table'];
172 PMA\libraries\Util::checkParameters(array('what', 'export_type'));
174 // sanitize this parameter which will be used below in a file inclusion
175 $what = PMA_securePath($_POST['what']);
177 // export class instance, not array of properties, as before
178 /* @var $export_plugin ExportPlugin */
179 $export_plugin = PMA_getPlugin(
180 "export",
181 $what,
182 'libraries/plugins/export/',
183 array(
184 'export_type' => $export_type,
185 'single_table' => isset($single_table)
189 // Check export type
190 if (empty($export_plugin)) {
191 PMA_fatalError(__('Bad type!'));
195 * valid compression methods
197 $compression_methods = array(
198 'zip',
199 'gzip'
203 * init and variable checking
205 $compression = false;
206 $onserver = false;
207 $save_on_server = false;
208 $buffer_needed = false;
209 $back_button = '';
210 $save_filename = '';
211 $file_handle = '';
212 $err_url = '';
213 $filename = '';
214 $separate_files = '';
216 // Is it a quick or custom export?
217 if (isset($_REQUEST['quick_or_custom'])
218 && $_REQUEST['quick_or_custom'] == 'quick'
220 $quick_export = true;
221 } else {
222 $quick_export = false;
225 if ($_REQUEST['output_format'] == 'astext') {
226 $asfile = false;
227 } else {
228 $asfile = true;
229 if (isset($_REQUEST['as_separate_files'])
230 && ! empty($_REQUEST['as_separate_files'])
232 if (isset($_REQUEST['compression'])
233 && ! empty($_REQUEST['compression'])
234 && $_REQUEST['compression'] == 'zip'
236 $separate_files = $_REQUEST['as_separate_files'];
239 if (in_array($_REQUEST['compression'], $compression_methods)) {
240 $compression = $_REQUEST['compression'];
241 $buffer_needed = true;
243 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
244 || (! $quick_export && ! empty($_REQUEST['onserver']))
246 if ($quick_export) {
247 $onserver = $_REQUEST['quick_export_onserver'];
248 } else {
249 $onserver = $_REQUEST['onserver'];
251 // Will we save dump on server?
252 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
256 // Generate error url and check for needed variables
257 if ($export_type == 'server') {
258 $err_url = 'server_export.php' . URL::getCommon();
259 } elseif ($export_type == 'database' && strlen($db) > 0) {
260 $err_url = 'db_export.php' . URL::getCommon(array('db' => $db));
261 // Check if we have something to export
262 if (isset($table_select)) {
263 $tables = $table_select;
264 } else {
265 $tables = array();
267 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
268 $err_url = 'tbl_export.php' . URL::getCommon(
269 array(
270 'db' => $db, 'table' => $table
273 } else {
274 PMA_fatalError(__('Bad parameters!'));
277 // Merge SQL Query aliases with Export aliases from
278 // export page, Export page aliases are given more
279 // preference over SQL Query aliases.
280 $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
281 $aliases = array();
282 if ((!empty($parser->statements[0]))
283 && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
285 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
287 if (!empty($_REQUEST['aliases'])) {
288 $aliases = PMA_mergeAliases($aliases, $_REQUEST['aliases']);
289 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
293 * Increase time limit for script execution and initializes some variables
295 @set_time_limit($cfg['ExecTimeLimit']);
296 if (! empty($cfg['MemoryLimit'])) {
297 @ini_set('memory_limit', $cfg['MemoryLimit']);
299 register_shutdown_function('PMA_shutdownDuringExport');
300 // Start with empty buffer
301 $dump_buffer = '';
302 $dump_buffer_len = 0;
304 // Array of dump_buffers - used in separate file exports
305 $dump_buffer_objects = array();
307 // We send fake headers to avoid browser timeout when buffering
308 $time_start = time();
310 // Defines the default <CR><LF> format.
311 // For SQL always use \n as MySQL wants this on all platforms.
312 if ($what == 'sql') {
313 $crlf = "\n";
314 } else {
315 $crlf = PHP_EOL;
318 $output_kanji_conversion = Encoding::canConvertKanji();
320 // Do we need to convert charset?
321 $output_charset_conversion = $asfile
322 && Encoding::isSupported()
323 && isset($charset) && $charset != 'utf-8';
325 // Use on the fly compression?
326 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
327 && $compression == 'gzip';
328 if ($GLOBALS['onfly_compression']) {
329 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
332 // Generate filename and mime type if needed
333 if ($asfile) {
334 if (empty($remember_template)) {
335 $remember_template = '';
337 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
338 $export_type, $remember_template, $export_plugin, $compression,
339 $filename_template
341 } else {
342 $mime_type = '';
345 // Open file on server if needed
346 if ($save_on_server) {
347 list($save_filename, $message, $file_handle) = PMA_openExportFile(
348 $filename, $quick_export
351 // problem opening export file on server?
352 if (! empty($message)) {
353 PMA_showExportPage($db, $table, $export_type);
355 } else {
357 * Send headers depending on whether the user chose to download a dump file
358 * or not
360 if ($asfile) {
361 // Download
362 // (avoid rewriting data containing HTML with anchors and forms;
363 // this was reported to happen under Plesk)
364 @ini_set('url_rewriter.tags', '');
365 $filename = Sanitize::sanitizeFilename($filename);
367 PMA_downloadHeader($filename, $mime_type);
368 } else {
369 // HTML
370 if ($export_type == 'database') {
371 $num_tables = count($tables);
372 if ($num_tables == 0) {
373 $message = PMA\libraries\Message::error(
374 __('No tables found in database.')
376 $active_page = 'db_export.php';
377 include 'db_export.php';
378 exit();
381 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
382 $export_type, $db, $table
384 echo $html;
385 unset($html);
386 } // end download
389 // Fake loop just to allow skip of remain of this code by break, I'd really
390 // need exceptions here :-)
391 do {
392 // Re - initialize
393 $dump_buffer = '';
394 $dump_buffer_len = 0;
396 // Add possibly some comments to export
397 if (! $export_plugin->exportHeader()) {
398 break;
401 // Will we need relation & co. setup?
402 $do_relation = isset($GLOBALS[$what . '_relation']);
403 $do_comments = isset($GLOBALS[$what . '_include_comments'])
404 || isset($GLOBALS[$what . '_comments']);
405 $do_mime = isset($GLOBALS[$what . '_mime']);
406 if ($do_relation || $do_comments || $do_mime) {
407 $cfgRelation = PMA_getRelationsParam();
409 if ($do_mime) {
410 include_once 'libraries/transformations.lib.php';
413 // Include dates in export?
414 $do_dates = isset($GLOBALS[$what . '_dates']);
416 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
419 * Builds the dump
421 if ($export_type == 'server') {
422 if (! isset($db_select)) {
423 $db_select = '';
425 PMA_exportServer(
426 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
427 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
428 $aliases, $separate_files
430 } elseif ($export_type == 'database') {
431 if (!isset($table_structure) || !is_array($table_structure)) {
432 $table_structure = array();
434 if (!isset($table_data) || !is_array($table_data)) {
435 $table_data = array();
437 if (!empty($_REQUEST['structure_or_data_forced'])) {
438 $table_structure = $tables;
439 $table_data = $tables;
441 if (isset($lock_tables)) {
442 PMA_lockTables($db, $tables, "READ");
443 try {
444 PMA_exportDatabase(
445 $db, $tables, $whatStrucOrData, $table_structure,
446 $table_data, $export_plugin, $crlf, $err_url, $export_type,
447 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
448 $separate_files
450 } finally {
451 PMA_unlockTables();
453 } else {
454 PMA_exportDatabase(
455 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
456 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
457 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
460 } else {
461 // We export just one table
462 // $allrows comes from the form when "Dump all rows" has been selected
463 if (! isset($allrows)) {
464 $allrows = '';
466 if (! isset($limit_to)) {
467 $limit_to = 0;
469 if (! isset($limit_from)) {
470 $limit_from = 0;
472 if (isset($lock_tables)) {
473 try {
474 PMA_lockTables($db, array($table), "READ");
475 PMA_exportTable(
476 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
477 $err_url, $export_type, $do_relation, $do_comments,
478 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
479 $sql_query, $aliases
481 } finally {
482 PMA_unlockTables();
484 } else {
485 PMA_exportTable(
486 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
487 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
488 $allrows, $limit_to, $limit_from, $sql_query, $aliases
492 if (! $export_plugin->exportFooter()) {
493 break;
496 } while (false);
497 // End of fake loop
499 if ($save_on_server && ! empty($message)) {
500 PMA_showExportPage($db, $table, $export_type);
504 * Send the dump as a file...
506 if (empty($asfile)) {
507 echo PMA_getHtmlForDisplayedExportFooter($back_button);
508 return;
509 } // end if
511 // Convert the charset if required.
512 if ($output_charset_conversion) {
513 $dump_buffer = Encoding::convertString(
514 'utf-8',
515 $GLOBALS['charset'],
516 $dump_buffer
520 // Compression needed?
521 if ($compression) {
522 if (! empty($separate_files)) {
523 $dump_buffer = PMA_compressExport(
524 $dump_buffer_objects, $compression, $filename
526 } else {
527 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
532 /* If we saved on server, we have to close file now */
533 if ($save_on_server) {
534 $message = PMA_closeExportFile(
535 $file_handle, $dump_buffer, $save_filename
537 PMA_showExportPage($db, $table, $export_type);
538 } else {
539 echo $dump_buffer;