Translated using Weblate (Slovenian)
[phpmyadmin.git] / export.php
blobc93383338d8f0c99a92fb600c82956296cacda96
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';
25 $response = Response::getInstance();
26 $header = $response->getHeader();
27 $scripts = $header->getScripts();
28 $scripts->addFile('export_output.js');
30 //check if it's the GET request to check export time out
31 if (isset($_GET['check_time_out'])) {
32 if (isset($_SESSION['pma_export_error'])) {
33 $err = $_SESSION['pma_export_error'];
34 unset($_SESSION['pma_export_error']);
35 echo "timeout";
36 } else {
37 echo "success";
39 exit;
42 /**
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
49 * without checking
51 $post_params = array(
52 'db',
53 'table',
54 'what',
55 'single_table',
56 'export_type',
57 'export_method',
58 'quick_or_custom',
59 'db_select',
60 'table_select',
61 'table_structure',
62 'table_data',
63 'limit_to',
64 'limit_from',
65 'allrows',
66 'lock_tables',
67 'output_format',
68 'filename_template',
69 'maxsize',
70 'remember_template',
71 'charset',
72 'compression',
73 'as_separate_files',
74 'knjenc',
75 'xkana',
76 'htmlword_structure_or_data',
77 'htmlword_null',
78 'htmlword_columns',
79 'mediawiki_headers',
80 'mediawiki_structure_or_data',
81 'mediawiki_caption',
82 'pdf_structure_or_data',
83 'odt_structure_or_data',
84 'odt_relation',
85 'odt_comments',
86 'odt_mime',
87 'odt_columns',
88 'odt_null',
89 'codegen_structure_or_data',
90 'codegen_format',
91 'excel_null',
92 'excel_removeCRLF',
93 'excel_columns',
94 'excel_edition',
95 'excel_structure_or_data',
96 'yaml_structure_or_data',
97 'ods_null',
98 'ods_structure_or_data',
99 'ods_columns',
100 'json_structure_or_data',
101 'json_pretty_print',
102 'json_unicode',
103 'xml_structure_or_data',
104 'xml_export_events',
105 'xml_export_functions',
106 'xml_export_procedures',
107 'xml_export_tables',
108 'xml_export_triggers',
109 'xml_export_views',
110 'xml_export_contents',
111 'texytext_structure_or_data',
112 'texytext_columns',
113 'texytext_null',
114 'phparray_structure_or_data',
115 'sql_include_comments',
116 'sql_header_comment',
117 'sql_dates',
118 'sql_relation',
119 'sql_mime',
120 'sql_use_transaction',
121 'sql_disable_fk',
122 'sql_compatibility',
123 'sql_structure_or_data',
124 'sql_create_database',
125 'sql_drop_table',
126 'sql_procedure_function',
127 'sql_create_table',
128 'sql_create_view',
129 'sql_create_trigger',
130 'sql_if_not_exists',
131 'sql_auto_increment',
132 'sql_backquotes',
133 'sql_truncate',
134 'sql_delayed',
135 'sql_ignore',
136 'sql_type',
137 'sql_insert_syntax',
138 'sql_max_query_size',
139 'sql_hex_for_binary',
140 'sql_utc_time',
141 'sql_drop_database',
142 'sql_views_as_tables',
143 'sql_metadata',
144 'csv_separator',
145 'csv_enclosed',
146 'csv_escaped',
147 'csv_terminated',
148 'csv_null',
149 'csv_removeCRLF',
150 'csv_columns',
151 'csv_structure_or_data',
152 // csv_replace should have been here but we use it directly from $_POST
153 'latex_caption',
154 'latex_structure_or_data',
155 'latex_structure_caption',
156 'latex_structure_continued_caption',
157 'latex_structure_label',
158 'latex_relation',
159 'latex_comments',
160 'latex_mime',
161 'latex_columns',
162 'latex_data_caption',
163 'latex_data_continued_caption',
164 'latex_data_label',
165 'latex_null',
166 'aliases'
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 = Plugins::getPlugin(
185 "export",
186 $what,
187 'libraries/classes/Plugins/Export/',
188 array(
189 'export_type' => $export_type,
190 'single_table' => isset($single_table)
194 // Check export type
195 if (empty($export_plugin)) {
196 Core::fatalError(__('Bad type!'));
200 * valid compression methods
202 $compression_methods = array(
203 'zip',
204 'gzip'
208 * init and variable checking
210 $compression = false;
211 $onserver = false;
212 $save_on_server = false;
213 $buffer_needed = false;
214 $back_button = '';
215 $refreshButton = '';
216 $save_filename = '';
217 $file_handle = '';
218 $err_url = '';
219 $filename = '';
220 $separate_files = '';
222 // Is it a quick or custom export?
223 if (isset($_POST['quick_or_custom'])
224 && $_POST['quick_or_custom'] == 'quick'
226 $quick_export = true;
227 } else {
228 $quick_export = false;
231 if ($_POST['output_format'] == 'astext') {
232 $asfile = false;
233 } else {
234 $asfile = true;
235 if (isset($_POST['as_separate_files'])
236 && ! empty($_POST['as_separate_files'])
238 if (isset($_POST['compression'])
239 && ! empty($_POST['compression'])
240 && $_POST['compression'] == 'zip'
242 $separate_files = $_POST['as_separate_files'];
245 if (in_array($_POST['compression'], $compression_methods)) {
246 $compression = $_POST['compression'];
247 $buffer_needed = true;
249 if (($quick_export && ! empty($_POST['quick_export_onserver']))
250 || (! $quick_export && ! empty($_POST['onserver']))
252 if ($quick_export) {
253 $onserver = $_POST['quick_export_onserver'];
254 } else {
255 $onserver = $_POST['onserver'];
257 // Will we save dump on server?
258 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
263 * If we are sending the export file (as opposed to just displaying it
264 * as text), we have to bypass the usual PhpMyAdmin\Response mechanism
266 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit' && ! $save_on_server) {
267 $response->disable();
268 //Disable all active buffers (see: ob_get_status(true) at this point)
269 while (@ob_end_clean());
272 // Generate error url and check for needed variables
273 if ($export_type == 'server') {
274 $err_url = 'server_export.php' . Url::getCommon();
275 } elseif ($export_type == 'database' && strlen($db) > 0) {
276 $err_url = 'db_export.php' . Url::getCommon(array('db' => $db));
277 // Check if we have something to export
278 if (isset($table_select)) {
279 $tables = $table_select;
280 } else {
281 $tables = array();
283 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
284 $err_url = 'tbl_export.php' . Url::getCommon(
285 array(
286 'db' => $db, 'table' => $table
289 } else {
290 Core::fatalError(__('Bad parameters!'));
293 // Merge SQL Query aliases with Export aliases from
294 // export page, Export page aliases are given more
295 // preference over SQL Query aliases.
296 $parser = new \PhpMyAdmin\SqlParser\Parser($sql_query);
297 $aliases = array();
298 if ((!empty($parser->statements[0]))
299 && ($parser->statements[0] instanceof \PhpMyAdmin\SqlParser\Statements\SelectStatement)
301 $aliases = \PhpMyAdmin\SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
303 if (!empty($_POST['aliases'])) {
304 $aliases = Export::mergeAliases($aliases, $_POST['aliases']);
305 $_SESSION['tmpval']['aliases'] = $_POST['aliases'];
309 * Increase time limit for script execution and initializes some variables
311 Util::setTimeLimit();
312 if (! empty($cfg['MemoryLimit'])) {
313 ini_set('memory_limit', $cfg['MemoryLimit']);
315 register_shutdown_function('PhpMyAdmin\Export::shutdown');
316 // Start with empty buffer
317 $dump_buffer = '';
318 $dump_buffer_len = 0;
320 // Array of dump_buffers - used in separate file exports
321 $dump_buffer_objects = array();
323 // We send fake headers to avoid browser timeout when buffering
324 $time_start = time();
326 // Defines the default <CR><LF> format.
327 // For SQL always use \n as MySQL wants this on all platforms.
328 if ($what == 'sql') {
329 $crlf = "\n";
330 } else {
331 $crlf = PHP_EOL;
334 $output_kanji_conversion = Encoding::canConvertKanji();
336 // Do we need to convert charset?
337 $output_charset_conversion = $asfile
338 && Encoding::isSupported()
339 && isset($charset) && $charset != 'utf-8';
341 // Use on the fly compression?
342 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
343 && $compression == 'gzip';
344 if ($GLOBALS['onfly_compression']) {
345 $GLOBALS['memory_limit'] = Export::getMemoryLimit();
348 // Generate filename and mime type if needed
349 if ($asfile) {
350 if (empty($remember_template)) {
351 $remember_template = '';
353 list($filename, $mime_type) = Export::getFilenameAndMimetype(
354 $export_type, $remember_template, $export_plugin, $compression,
355 $filename_template
357 } else {
358 $mime_type = '';
361 // Open file on server if needed
362 if ($save_on_server) {
363 list($save_filename, $message, $file_handle) = Export::openFile(
364 $filename, $quick_export
367 // problem opening export file on server?
368 if (! empty($message)) {
369 Export::showPage($db, $table, $export_type);
371 } else {
373 * Send headers depending on whether the user chose to download a dump file
374 * or not
376 if ($asfile) {
377 // Download
378 // (avoid rewriting data containing HTML with anchors and forms;
379 // this was reported to happen under Plesk)
380 ini_set('url_rewriter.tags', '');
381 $filename = Sanitize::sanitizeFilename($filename);
383 Core::downloadHeader($filename, $mime_type);
384 } else {
385 // HTML
386 if ($export_type == 'database') {
387 $num_tables = count($tables);
388 if ($num_tables == 0) {
389 $message = PhpMyAdmin\Message::error(
390 __('No tables found in database.')
392 $active_page = 'db_export.php';
393 include 'db_export.php';
394 exit();
397 list($html, $back_button, $refreshButton) = Export::getHtmlForDisplayedExportHeader(
398 $export_type, $db, $table
400 echo $html;
401 unset($html);
402 } // end download
405 $relation = new Relation();
407 // Fake loop just to allow skip of remain of this code by break, I'd really
408 // need exceptions here :-)
409 do {
410 // Re - initialize
411 $dump_buffer = '';
412 $dump_buffer_len = 0;
414 // Add possibly some comments to export
415 if (! $export_plugin->exportHeader()) {
416 break;
419 // Will we need relation & co. setup?
420 $do_relation = isset($GLOBALS[$what . '_relation']);
421 $do_comments = isset($GLOBALS[$what . '_include_comments'])
422 || isset($GLOBALS[$what . '_comments']);
423 $do_mime = isset($GLOBALS[$what . '_mime']);
424 if ($do_relation || $do_comments || $do_mime) {
425 $cfgRelation = $relation->getRelationsParam();
428 // Include dates in export?
429 $do_dates = isset($GLOBALS[$what . '_dates']);
431 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
434 * Builds the dump
436 if ($export_type == 'server') {
437 if (! isset($db_select)) {
438 $db_select = '';
440 Export::exportServer(
441 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
442 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
443 $aliases, $separate_files
445 } elseif ($export_type == 'database') {
446 if (!isset($table_structure) || !is_array($table_structure)) {
447 $table_structure = array();
449 if (!isset($table_data) || !is_array($table_data)) {
450 $table_data = array();
452 if (!empty($_POST['structure_or_data_forced'])) {
453 $table_structure = $tables;
454 $table_data = $tables;
456 if (isset($lock_tables)) {
457 Export::lockTables($db, $tables, "READ");
458 try {
459 Export::exportDatabase(
460 $db, $tables, $whatStrucOrData, $table_structure,
461 $table_data, $export_plugin, $crlf, $err_url, $export_type,
462 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
463 $separate_files
465 } finally {
466 Export::unlockTables();
468 } else {
469 Export::exportDatabase(
470 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
471 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
472 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
475 } else {
476 // We export just one table
477 // $allrows comes from the form when "Dump all rows" has been selected
478 if (! isset($allrows)) {
479 $allrows = '';
481 if (! isset($limit_to)) {
482 $limit_to = 0;
484 if (! isset($limit_from)) {
485 $limit_from = 0;
487 if (isset($lock_tables)) {
488 try {
489 Export::lockTables($db, array($table), "READ");
490 Export::exportTable(
491 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
492 $err_url, $export_type, $do_relation, $do_comments,
493 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
494 $sql_query, $aliases
496 } finally {
497 Export::unlockTables();
499 } else {
500 Export::exportTable(
501 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
502 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
503 $allrows, $limit_to, $limit_from, $sql_query, $aliases
507 if (! $export_plugin->exportFooter()) {
508 break;
511 } while (false);
512 // End of fake loop
514 if ($save_on_server && ! empty($message)) {
515 Export::showPage($db, $table, $export_type);
519 * Send the dump as a file...
521 if (empty($asfile)) {
522 echo Export::getHtmlForDisplayedExportFooter($back_button, $refreshButton);
523 return;
524 } // end if
526 // Convert the charset if required.
527 if ($output_charset_conversion) {
528 $dump_buffer = Encoding::convertString(
529 'utf-8',
530 $GLOBALS['charset'],
531 $dump_buffer
535 // Compression needed?
536 if ($compression) {
537 if (! empty($separate_files)) {
538 $dump_buffer = Export::compress(
539 $dump_buffer_objects, $compression, $filename
541 } else {
542 $dump_buffer = Export::compress($dump_buffer, $compression, $filename);
547 /* If we saved on server, we have to close file now */
548 if ($save_on_server) {
549 $message = Export::closeFile(
550 $file_handle, $dump_buffer, $save_filename
552 Export::showPage($db, $table, $export_type);
553 } else {
554 echo $dump_buffer;