Translated using Weblate (Norwegian Bokmål)
[phpmyadmin.git] / export.php
blob376bd020c2a4aab600e17c49e6d1ff585b42f50a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Encoding;
11 use PhpMyAdmin\Export;
12 use PhpMyAdmin\Plugins;
13 use PhpMyAdmin\Plugins\ExportPlugin;
14 use PhpMyAdmin\Relation;
15 use PhpMyAdmin\Sanitize;
16 use PhpMyAdmin\Url;
17 use PhpMyAdmin\Util;
18 use PhpMyAdmin\Response;
20 /**
21 * Get the variables sent or posted to this script and a core script
23 include_once 'libraries/common.inc.php';
24 /**
25 * If we are sending the export file (as opposed to just displaying it
26 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
28 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
29 $response = Response::getInstance();
30 $response->disable();
33 $response = Response::getInstance();
34 $header = $response->getHeader();
35 $scripts = $header->getScripts();
36 $scripts->addFile('export_output.js');
38 //check if it's the GET request to check export time out
39 if (isset($_GET['check_time_out'])) {
40 if (isset($_SESSION['pma_export_error'])) {
41 $err = $_SESSION['pma_export_error'];
42 unset($_SESSION['pma_export_error']);
43 echo "timeout";
44 } else {
45 echo "success";
47 exit;
49 /**
50 * Sets globals from $_POST
52 * - Please keep the parameters in order of their appearance in the form
53 * - Some of these parameters are not used, as the code below directly
54 * verifies from the superglobal $_POST or $_REQUEST
55 * TODO: this should be removed to avoid passing user input to GLOBALS
56 * without checking
58 $post_params = array(
59 'db',
60 'table',
61 'what',
62 'single_table',
63 'export_type',
64 'export_method',
65 'quick_or_custom',
66 'db_select',
67 'table_select',
68 'table_structure',
69 'table_data',
70 'limit_to',
71 'limit_from',
72 'allrows',
73 'lock_tables',
74 'output_format',
75 'filename_template',
76 'maxsize',
77 'remember_template',
78 'charset',
79 'compression',
80 'as_separate_files',
81 'knjenc',
82 'xkana',
83 'htmlword_structure_or_data',
84 'htmlword_null',
85 'htmlword_columns',
86 'mediawiki_headers',
87 'mediawiki_structure_or_data',
88 'mediawiki_caption',
89 'pdf_structure_or_data',
90 'odt_structure_or_data',
91 'odt_relation',
92 'odt_comments',
93 'odt_mime',
94 'odt_columns',
95 'odt_null',
96 'codegen_structure_or_data',
97 'codegen_format',
98 'excel_null',
99 'excel_removeCRLF',
100 'excel_columns',
101 'excel_edition',
102 'excel_structure_or_data',
103 'yaml_structure_or_data',
104 'ods_null',
105 'ods_structure_or_data',
106 'ods_columns',
107 'json_structure_or_data',
108 'json_pretty_print',
109 'json_unicode',
110 'xml_structure_or_data',
111 'xml_export_events',
112 'xml_export_functions',
113 'xml_export_procedures',
114 'xml_export_tables',
115 'xml_export_triggers',
116 'xml_export_views',
117 'xml_export_contents',
118 'texytext_structure_or_data',
119 'texytext_columns',
120 'texytext_null',
121 'phparray_structure_or_data',
122 'sql_include_comments',
123 'sql_header_comment',
124 'sql_dates',
125 'sql_relation',
126 'sql_mime',
127 'sql_use_transaction',
128 'sql_disable_fk',
129 'sql_compatibility',
130 'sql_structure_or_data',
131 'sql_create_database',
132 'sql_drop_table',
133 'sql_procedure_function',
134 'sql_create_table',
135 'sql_create_view',
136 'sql_create_trigger',
137 'sql_if_not_exists',
138 'sql_auto_increment',
139 'sql_backquotes',
140 'sql_truncate',
141 'sql_delayed',
142 'sql_ignore',
143 'sql_type',
144 'sql_insert_syntax',
145 'sql_max_query_size',
146 'sql_hex_for_binary',
147 'sql_utc_time',
148 'sql_drop_database',
149 'sql_views_as_tables',
150 'sql_metadata',
151 'csv_separator',
152 'csv_enclosed',
153 'csv_escaped',
154 'csv_terminated',
155 'csv_null',
156 'csv_removeCRLF',
157 'csv_columns',
158 'csv_structure_or_data',
159 // csv_replace should have been here but we use it directly from $_POST
160 'latex_caption',
161 'latex_structure_or_data',
162 'latex_structure_caption',
163 'latex_structure_continued_caption',
164 'latex_structure_label',
165 'latex_relation',
166 'latex_comments',
167 'latex_mime',
168 'latex_columns',
169 'latex_data_caption',
170 'latex_data_continued_caption',
171 'latex_data_label',
172 'latex_null',
173 'aliases'
176 foreach ($post_params as $one_post_param) {
177 if (isset($_POST[$one_post_param])) {
178 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
182 $table = $GLOBALS['table'];
184 PhpMyAdmin\Util::checkParameters(array('what', 'export_type'));
186 // sanitize this parameter which will be used below in a file inclusion
187 $what = Core::securePath($_POST['what']);
189 // export class instance, not array of properties, as before
190 /* @var $export_plugin ExportPlugin */
191 $export_plugin = Plugins::getPlugin(
192 "export",
193 $what,
194 'libraries/classes/Plugins/Export/',
195 array(
196 'export_type' => $export_type,
197 'single_table' => isset($single_table)
201 // Check export type
202 if (empty($export_plugin)) {
203 Core::fatalError(__('Bad type!'));
207 * valid compression methods
209 $compression_methods = array(
210 'zip',
211 'gzip'
215 * init and variable checking
217 $compression = false;
218 $onserver = false;
219 $save_on_server = false;
220 $buffer_needed = false;
221 $back_button = '';
222 $refreshButton = '';
223 $save_filename = '';
224 $file_handle = '';
225 $err_url = '';
226 $filename = '';
227 $separate_files = '';
229 // Is it a quick or custom export?
230 if (isset($_REQUEST['quick_or_custom'])
231 && $_REQUEST['quick_or_custom'] == 'quick'
233 $quick_export = true;
234 } else {
235 $quick_export = false;
238 if ($_REQUEST['output_format'] == 'astext') {
239 $asfile = false;
240 } else {
241 $asfile = true;
242 if (isset($_REQUEST['as_separate_files'])
243 && ! empty($_REQUEST['as_separate_files'])
245 if (isset($_REQUEST['compression'])
246 && ! empty($_REQUEST['compression'])
247 && $_REQUEST['compression'] == 'zip'
249 $separate_files = $_REQUEST['as_separate_files'];
252 if (in_array($_REQUEST['compression'], $compression_methods)) {
253 $compression = $_REQUEST['compression'];
254 $buffer_needed = true;
256 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
257 || (! $quick_export && ! empty($_REQUEST['onserver']))
259 if ($quick_export) {
260 $onserver = $_REQUEST['quick_export_onserver'];
261 } else {
262 $onserver = $_REQUEST['onserver'];
264 // Will we save dump on server?
265 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
269 // Generate error url and check for needed variables
270 if ($export_type == 'server') {
271 $err_url = 'server_export.php' . Url::getCommon();
272 } elseif ($export_type == 'database' && strlen($db) > 0) {
273 $err_url = 'db_export.php' . Url::getCommon(array('db' => $db));
274 // Check if we have something to export
275 if (isset($table_select)) {
276 $tables = $table_select;
277 } else {
278 $tables = array();
280 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
281 $err_url = 'tbl_export.php' . Url::getCommon(
282 array(
283 'db' => $db, 'table' => $table
286 } else {
287 Core::fatalError(__('Bad parameters!'));
290 // Merge SQL Query aliases with Export aliases from
291 // export page, Export page aliases are given more
292 // preference over SQL Query aliases.
293 $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
294 $aliases = array();
295 if ((!empty($parser->statements[0]))
296 && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
298 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
300 if (!empty($_REQUEST['aliases'])) {
301 $aliases = Export::mergeAliases($aliases, $_REQUEST['aliases']);
302 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
306 * Increase time limit for script execution and initializes some variables
308 Util::setTimeLimit();
309 if (! empty($cfg['MemoryLimit'])) {
310 ini_set('memory_limit', $cfg['MemoryLimit']);
312 register_shutdown_function('PhpMyAdmin\Export::shutdown');
313 // Start with empty buffer
314 $dump_buffer = '';
315 $dump_buffer_len = 0;
317 // Array of dump_buffers - used in separate file exports
318 $dump_buffer_objects = array();
320 // We send fake headers to avoid browser timeout when buffering
321 $time_start = time();
323 // Defines the default <CR><LF> format.
324 // For SQL always use \n as MySQL wants this on all platforms.
325 if ($what == 'sql') {
326 $crlf = "\n";
327 } else {
328 $crlf = PHP_EOL;
331 $output_kanji_conversion = Encoding::canConvertKanji();
333 // Do we need to convert charset?
334 $output_charset_conversion = $asfile
335 && Encoding::isSupported()
336 && isset($charset) && $charset != 'utf-8';
338 // Use on the fly compression?
339 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
340 && $compression == 'gzip';
341 if ($GLOBALS['onfly_compression']) {
342 $GLOBALS['memory_limit'] = Export::getMemoryLimit();
345 // Generate filename and mime type if needed
346 if ($asfile) {
347 if (empty($remember_template)) {
348 $remember_template = '';
350 list($filename, $mime_type) = Export::getFilenameAndMimetype(
351 $export_type, $remember_template, $export_plugin, $compression,
352 $filename_template
354 } else {
355 $mime_type = '';
358 // Open file on server if needed
359 if ($save_on_server) {
360 list($save_filename, $message, $file_handle) = Export::openFile(
361 $filename, $quick_export
364 // problem opening export file on server?
365 if (! empty($message)) {
366 Export::showPage($db, $table, $export_type);
368 } else {
370 * Send headers depending on whether the user chose to download a dump file
371 * or not
373 if ($asfile) {
374 // Download
375 // (avoid rewriting data containing HTML with anchors and forms;
376 // this was reported to happen under Plesk)
377 ini_set('url_rewriter.tags', '');
378 $filename = Sanitize::sanitizeFilename($filename);
380 Core::downloadHeader($filename, $mime_type);
381 } else {
382 // HTML
383 if ($export_type == 'database') {
384 $num_tables = count($tables);
385 if ($num_tables == 0) {
386 $message = PhpMyAdmin\Message::error(
387 __('No tables found in database.')
389 $active_page = 'db_export.php';
390 include 'db_export.php';
391 exit();
394 list($html, $back_button, $refreshButton) = Export::getHtmlForDisplayedExportHeader(
395 $export_type, $db, $table
397 echo $html;
398 unset($html);
399 } // end download
402 $relation = new Relation();
404 // Fake loop just to allow skip of remain of this code by break, I'd really
405 // need exceptions here :-)
406 do {
407 // Re - initialize
408 $dump_buffer = '';
409 $dump_buffer_len = 0;
411 // Add possibly some comments to export
412 if (! $export_plugin->exportHeader()) {
413 break;
416 // Will we need relation & co. setup?
417 $do_relation = isset($GLOBALS[$what . '_relation']);
418 $do_comments = isset($GLOBALS[$what . '_include_comments'])
419 || isset($GLOBALS[$what . '_comments']);
420 $do_mime = isset($GLOBALS[$what . '_mime']);
421 if ($do_relation || $do_comments || $do_mime) {
422 $cfgRelation = $relation->getRelationsParam();
425 // Include dates in export?
426 $do_dates = isset($GLOBALS[$what . '_dates']);
428 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
431 * Builds the dump
433 if ($export_type == 'server') {
434 if (! isset($db_select)) {
435 $db_select = '';
437 Export::exportServer(
438 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
439 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
440 $aliases, $separate_files
442 } elseif ($export_type == 'database') {
443 if (!isset($table_structure) || !is_array($table_structure)) {
444 $table_structure = array();
446 if (!isset($table_data) || !is_array($table_data)) {
447 $table_data = array();
449 if (!empty($_REQUEST['structure_or_data_forced'])) {
450 $table_structure = $tables;
451 $table_data = $tables;
453 if (isset($lock_tables)) {
454 Export::lockTables($db, $tables, "READ");
455 try {
456 Export::exportDatabase(
457 $db, $tables, $whatStrucOrData, $table_structure,
458 $table_data, $export_plugin, $crlf, $err_url, $export_type,
459 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
460 $separate_files
462 } finally {
463 Export::unlockTables();
465 } else {
466 Export::exportDatabase(
467 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
468 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
469 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
472 } else {
473 // We export just one table
474 // $allrows comes from the form when "Dump all rows" has been selected
475 if (! isset($allrows)) {
476 $allrows = '';
478 if (! isset($limit_to)) {
479 $limit_to = 0;
481 if (! isset($limit_from)) {
482 $limit_from = 0;
484 if (isset($lock_tables)) {
485 try {
486 Export::lockTables($db, array($table), "READ");
487 Export::exportTable(
488 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
489 $err_url, $export_type, $do_relation, $do_comments,
490 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
491 $sql_query, $aliases
493 } finally {
494 Export::unlockTables();
496 } else {
497 Export::exportTable(
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()) {
505 break;
508 } while (false);
509 // End of fake loop
511 if ($save_on_server && ! empty($message)) {
512 Export::showPage($db, $table, $export_type);
516 * Send the dump as a file...
518 if (empty($asfile)) {
519 echo Export::getHtmlForDisplayedExportFooter($back_button, $refreshButton);
520 return;
521 } // end if
523 // Convert the charset if required.
524 if ($output_charset_conversion) {
525 $dump_buffer = Encoding::convertString(
526 'utf-8',
527 $GLOBALS['charset'],
528 $dump_buffer
532 // Compression needed?
533 if ($compression) {
534 if (! empty($separate_files)) {
535 $dump_buffer = Export::compress(
536 $dump_buffer_objects, $compression, $filename
538 } else {
539 $dump_buffer = Export::compress($dump_buffer, $compression, $filename);
544 /* If we saved on server, we have to close file now */
545 if ($save_on_server) {
546 $message = Export::closeFile(
547 $file_handle, $dump_buffer, $save_filename
549 Export::showPage($db, $table, $export_type);
550 } else {
551 echo $dump_buffer;