Translated using Weblate (Indonesian)
[phpmyadmin.git] / export.php
blobc121f499012ed0d1ce8e874e4199ebf1693ce68f
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\Plugins\ExportPlugin;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Sanitize;
14 use PhpMyAdmin\Url;
15 use PhpMyAdmin\Util;
17 /**
18 * Get the variables sent or posted to this script and a core script
20 /**
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']);
36 echo "timeout";
37 } else {
38 echo "success";
40 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 = PMA_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 $save_filename = '';
216 $file_handle = '';
217 $err_url = '';
218 $filename = '';
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;
226 } else {
227 $quick_export = false;
230 if ($_REQUEST['output_format'] == 'astext') {
231 $asfile = false;
232 } else {
233 $asfile = true;
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']))
251 if ($quick_export) {
252 $onserver = $_REQUEST['quick_export_onserver'];
253 } else {
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;
269 } else {
270 $tables = array();
272 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
273 $err_url = 'tbl_export.php' . Url::getCommon(
274 array(
275 'db' => $db, 'table' => $table
278 } else {
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);
286 $aliases = array();
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
306 $dump_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') {
318 $crlf = "\n";
319 } else {
320 $crlf = PHP_EOL;
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
338 if ($asfile) {
339 if (empty($remember_template)) {
340 $remember_template = '';
342 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
343 $export_type, $remember_template, $export_plugin, $compression,
344 $filename_template
346 } else {
347 $mime_type = '';
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);
360 } else {
362 * Send headers depending on whether the user chose to download a dump file
363 * or not
365 if ($asfile) {
366 // Download
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);
373 } else {
374 // HTML
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';
383 exit();
386 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
387 $export_type, $db, $table
389 echo $html;
390 unset($html);
391 } // end download
394 // Fake loop just to allow skip of remain of this code by break, I'd really
395 // need exceptions here :-)
396 do {
397 // Re - initialize
398 $dump_buffer = '';
399 $dump_buffer_len = 0;
401 // Add possibly some comments to export
402 if (! $export_plugin->exportHeader()) {
403 break;
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'];
421 * Builds the dump
423 if ($export_type == 'server') {
424 if (! isset($db_select)) {
425 $db_select = '';
427 PMA_exportServer(
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");
445 try {
446 PMA_exportDatabase(
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,
450 $separate_files
452 } finally {
453 PMA_unlockTables();
455 } else {
456 PMA_exportDatabase(
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
462 } else {
463 // We export just one table
464 // $allrows comes from the form when "Dump all rows" has been selected
465 if (! isset($allrows)) {
466 $allrows = '';
468 if (! isset($limit_to)) {
469 $limit_to = 0;
471 if (! isset($limit_from)) {
472 $limit_from = 0;
474 if (isset($lock_tables)) {
475 try {
476 PMA_lockTables($db, array($table), "READ");
477 PMA_exportTable(
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,
481 $sql_query, $aliases
483 } finally {
484 PMA_unlockTables();
486 } else {
487 PMA_exportTable(
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()) {
495 break;
498 } while (false);
499 // End of fake loop
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);
510 return;
511 } // end if
513 // Convert the charset if required.
514 if ($output_charset_conversion) {
515 $dump_buffer = Encoding::convertString(
516 'utf-8',
517 $GLOBALS['charset'],
518 $dump_buffer
522 // Compression needed?
523 if ($compression) {
524 if (! empty($separate_files)) {
525 $dump_buffer = PMA_compressExport(
526 $dump_buffer_objects, $compression, $filename
528 } else {
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);
540 } else {
541 echo $dump_buffer;