Translated using Weblate (Russian)
[phpmyadmin.git] / export.php
blobcbe661bfba7f62564e0be89cbfe0482a035401fd
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Main export handling code
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get the variables sent or posted to this script and a core script
12 if (!defined('TESTSUITE')) {
13 /**
14 * If we are sending the export file (as opposed to just displaying it
15 * as text), we have to bypass the usual PMA_Response mechanism
17 if (isset($_POST['output_format']) && $_POST['output_format'] == 'sendit') {
18 define('PMA_BYPASS_GET_INSTANCE', 1);
20 include_once 'libraries/common.inc.php';
21 include_once 'libraries/zip.lib.php';
22 include_once 'libraries/plugin_interface.lib.php';
23 include_once 'libraries/export.lib.php';
25 //check if it's the GET request to check export time out
26 if (isset($_GET['check_time_out'])) {
27 if (isset($_SESSION['pma_export_error'])) {
28 $err = $_SESSION['pma_export_error'];
29 unset($_SESSION['pma_export_error']);
30 echo "timeout";
31 } else {
32 echo "success";
34 exit;
36 /**
37 * Sets globals from $_POST
39 * - Please keep the parameters in order of their appearance in the form
40 * - Some of these parameters are not used, as the code below directly
41 * verifies from the superglobal $_POST or $_REQUEST
43 $post_params = array(
44 'db',
45 'table',
46 'single_table',
47 'export_type',
48 'export_method',
49 'quick_or_custom',
50 'db_select',
51 'table_select',
52 'table_structure',
53 'table_data',
54 'limit_to',
55 'limit_from',
56 'allrows',
57 'lock_tables',
58 'output_format',
59 'filename_template',
60 'maxsize',
61 'remember_template',
62 'charset',
63 'compression',
64 'as_separate_files',
65 'knjenc',
66 'xkana',
67 'htmlword_structure_or_data',
68 'htmlword_null',
69 'htmlword_columns',
70 'mediawiki_headers',
71 'mediawiki_structure_or_data',
72 'mediawiki_caption',
73 'pdf_structure_or_data',
74 'odt_structure_or_data',
75 'odt_relation',
76 'odt_comments',
77 'odt_mime',
78 'odt_columns',
79 'odt_null',
80 'codegen_structure_or_data',
81 'codegen_format',
82 'excel_null',
83 'excel_removeCRLF',
84 'excel_columns',
85 'excel_edition',
86 'excel_structure_or_data',
87 'yaml_structure_or_data',
88 'ods_null',
89 'ods_structure_or_data',
90 'ods_columns',
91 'json_structure_or_data',
92 'json_pretty_print',
93 'xml_structure_or_data',
94 'xml_export_events',
95 'xml_export_functions',
96 'xml_export_procedures',
97 'xml_export_tables',
98 'xml_export_triggers',
99 'xml_export_views',
100 'xml_export_contents',
101 'texytext_structure_or_data',
102 'texytext_columns',
103 'texytext_null',
104 'phparray_structure_or_data',
105 'sql_include_comments',
106 'sql_header_comment',
107 'sql_dates',
108 'sql_relation',
109 'sql_mime',
110 'sql_use_transaction',
111 'sql_disable_fk',
112 'sql_compatibility',
113 'sql_structure_or_data',
114 'sql_create_database',
115 'sql_drop_table',
116 'sql_procedure_function',
117 'sql_create_table_statements',
118 'sql_create_table',
119 'sql_create_view',
120 'sql_create_trigger',
121 'sql_if_not_exists',
122 'sql_auto_increment',
123 'sql_backquotes',
124 'sql_truncate',
125 'sql_delayed',
126 'sql_ignore',
127 'sql_type',
128 'sql_insert_syntax',
129 'sql_max_query_size',
130 'sql_hex_for_binary',
131 'sql_utc_time',
132 'sql_drop_database',
133 'sql_views_as_tables',
134 'sql_metadata',
135 'csv_separator',
136 'csv_enclosed',
137 'csv_escaped',
138 'csv_terminated',
139 'csv_null',
140 'csv_removeCRLF',
141 'csv_columns',
142 'csv_structure_or_data',
143 // csv_replace should have been here but we use it directly from $_POST
144 'latex_caption',
145 'latex_structure_or_data',
146 'latex_structure_caption',
147 'latex_structure_continued_caption',
148 'latex_structure_label',
149 'latex_relation',
150 'latex_comments',
151 'latex_mime',
152 'latex_columns',
153 'latex_data_caption',
154 'latex_data_continued_caption',
155 'latex_data_label',
156 'latex_null',
157 'aliases'
160 foreach ($post_params as $one_post_param) {
161 if (isset($_POST[$one_post_param])) {
162 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
166 $table = $GLOBALS['table'];
167 // sanitize this parameter which will be used below in a file inclusion
168 $what = PMA_securePath($_POST['what']);
170 PMA_Util::checkParameters(array('what', 'export_type'));
172 // export class instance, not array of properties, as before
173 /* @var $export_plugin ExportPlugin */
174 $export_plugin = PMA_getPlugin(
175 "export",
176 $what,
177 'libraries/plugins/export/',
178 array(
179 'export_type' => $export_type,
180 'single_table' => isset($single_table)
184 // Backward compatibility
185 $type = $what;
187 // Check export type
188 if (! isset($export_plugin)) {
189 PMA_fatalError(__('Bad type!'));
192 // Avoid warning from PHP Analyzer
193 if (is_null($export_plugin)) {
194 $export_plugin = new stdClass();
198 * valid compression methods
200 $compression_methods = array(
201 'zip',
202 'gzip'
206 * init and variable checking
208 $compression = false;
209 $onserver = false;
210 $save_on_server = false;
211 $buffer_needed = false;
212 $back_button = '';
213 $save_filename = '';
214 $file_handle = '';
215 $err_url = '';
216 $filename = '';
217 $separate_files = '';
219 // Is it a quick or custom export?
220 if ($_REQUEST['quick_or_custom'] == 'quick') {
221 $quick_export = true;
222 } else {
223 $quick_export = false;
226 if ($_REQUEST['output_format'] == 'astext') {
227 $asfile = false;
228 } else {
229 $asfile = true;
230 if (isset($_REQUEST['as_separate_files'])
231 && ! empty($_REQUEST['as_separate_files'])
233 if (isset($_REQUEST['compression'])
234 && ! empty($_REQUEST['compression'])
235 && $_REQUEST['compression'] == 'zip'
237 $separate_files = $_REQUEST['as_separate_files'];
240 if (in_array($_REQUEST['compression'], $compression_methods)) {
241 $compression = $_REQUEST['compression'];
242 $buffer_needed = true;
244 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
245 || (! $quick_export && ! empty($_REQUEST['onserver']))
247 if ($quick_export) {
248 $onserver = $_REQUEST['quick_export_onserver'];
249 } else {
250 $onserver = $_REQUEST['onserver'];
252 // Will we save dump on server?
253 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
257 // Generate error url and check for needed variables
258 /** @var PMA_String $pmaString */
259 $pmaString = $GLOBALS['PMA_String'];
260 if ($export_type == 'server') {
261 $err_url = 'server_export.php' . PMA_URL_getCommon();
262 } elseif ($export_type == 'database'
263 && /*overload*/mb_strlen($db)
265 $err_url = 'db_export.php' . PMA_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' && /*overload*/mb_strlen($db)
273 && /*overload*/mb_strlen($table)
275 $err_url = 'tbl_export.php' . PMA_URL_getCommon(
276 array(
277 'db' => $db, 'table' => $table
280 } else {
281 PMA_fatalError(__('Bad parameters!'));
284 // Merge SQL Query aliases with Export aliases from
285 // export page, Export page aliases are given more
286 // preference over SQL Query aliases.
287 $parser = new SqlParser\Parser($sql_query);
288 $aliases = array();
289 if ((!empty($parser->statements[0]))
290 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
292 if (!empty($_REQUEST['aliases'])) {
293 $aliases = PMA_mergeAliases(
294 SqlParser\Utils\Misc::getAliases($parser->statements[0], $db),
295 $_REQUEST['aliases']
297 $_SESSION['tmpval']['aliases'] = $_REQUEST['aliases'];
298 } else {
299 $aliases = SqlParser\Utils\Misc::getAliases($parser->statements[0], $db);
304 * Increase time limit for script execution and initializes some variables
306 @set_time_limit($cfg['ExecTimeLimit']);
307 if (! empty($cfg['MemoryLimit'])) {
308 @ini_set('memory_limit', $cfg['MemoryLimit']);
310 register_shutdown_function('PMA_shutdownDuringExport');
311 // Start with empty buffer
312 $dump_buffer = '';
313 $dump_buffer_len = 0;
315 // Array of dump_buffers - used in separate file exports
316 $dump_buffer_objects = array();
318 // We send fake headers to avoid browser timeout when buffering
319 $time_start = time();
321 // Defines the default <CR><LF> format.
322 // For SQL always use \n as MySQL wants this on all platforms.
323 if ($what == 'sql') {
324 $crlf = "\n";
325 } else {
326 $crlf = PMA_Util::whichCrlf();
329 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
330 && $type != 'xls';
332 // Do we need to convert charset?
333 $output_charset_conversion = $asfile
334 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
335 && isset($charset) && $charset != 'utf-8'
336 && $type != 'xls';
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'] = PMA_getMemoryLimitForExport();
345 // Generate filename and mime type if needed
346 if ($asfile) {
347 if (empty($remember_template)) {
348 $remember_template = '';
350 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
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) = PMA_openExportFile(
361 $filename, $quick_export
364 // problem opening export file on server?
365 if (! empty($message)) {
366 PMA_showExportPage($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 = PMA_sanitizeFilename($filename);
380 PMA_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 = PMA_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) = PMA_getHtmlForDisplayedExportHeader(
395 $export_type, $db, $table
397 echo $html;
398 unset($html);
399 } // end download
402 // Fake loop just to allow skip of remain of this code by break, I'd really
403 // need exceptions here :-)
404 do {
406 if ($separate_files != 'database') {
407 // Add possibly some comments to export
408 if (! $export_plugin->exportHeader()) {
409 break;
413 // Will we need relation & co. setup?
414 $do_relation = isset($GLOBALS[$what . '_relation']);
415 $do_comments = isset($GLOBALS[$what . '_include_comments'])
416 || isset($GLOBALS[$what . '_comments']) ;
417 $do_mime = isset($GLOBALS[$what . '_mime']);
418 if ($do_relation || $do_comments || $do_mime) {
419 $cfgRelation = PMA_getRelationsParam();
421 if ($do_mime) {
422 include_once 'libraries/transformations.lib.php';
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 PMA_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 PMA_lockTables($db, $tables, "READ");
455 try {
456 PMA_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 PMA_unlockTables();
463 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
464 PMA_unlockTables();
465 throw $e;
467 } else {
468 PMA_exportDatabase(
469 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
470 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
471 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
474 } else {
475 // We export just one table
476 // $allrows comes from the form when "Dump all rows" has been selected
477 if (! isset($allrows)) {
478 $allrows = '';
480 if (! isset($limit_to)) {
481 $limit_to = 0;
483 if (! isset($limit_from)) {
484 $limit_from = 0;
486 if (isset($lock_tables)) {
487 try {
488 PMA_lockTables($db, array($table), "READ");
489 PMA_exportTable(
490 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
491 $err_url, $export_type, $do_relation, $do_comments,
492 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
493 $sql_query, $aliases
495 PMA_unlockTables();
496 } catch (Exception $e) { // TODO use finally when PHP version is 5.5
497 PMA_unlockTables();
498 throw $e;
500 } else {
501 PMA_exportTable(
502 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
503 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
504 $allrows, $limit_to, $limit_from, $sql_query, $aliases
508 if (! $export_plugin->exportFooter()) {
509 break;
512 } while (false);
513 // End of fake loop
515 if ($save_on_server && ! empty($message)) {
516 PMA_showExportPage($db, $table, $export_type);
520 * Send the dump as a file...
522 if (empty($asfile)) {
523 echo PMA_getHtmlForDisplayedExportFooter($back_button);
524 return;
525 } // end if
527 // Convert the charset if required.
528 if ($output_charset_conversion) {
529 $dump_buffer = PMA_convertString(
530 'utf-8',
531 $GLOBALS['charset'],
532 $dump_buffer
536 // Compression needed?
537 if ($compression) {
538 if (! empty($separate_files)) {
539 $dump_buffer = PMA_compressExport(
540 $dump_buffer_objects, $compression, $filename
542 } else {
543 $dump_buffer = PMA_compressExport($dump_buffer, $compression, $filename);
548 /* If we saved on server, we have to close file now */
549 if ($save_on_server) {
550 $message = PMA_closeExportFile(
551 $file_handle, $dump_buffer, $save_filename
553 PMA_showExportPage($db, $table, $export_type);
554 } else {
555 echo $dump_buffer;