Translated using Weblate (Slovenian)
[phpmyadmin.git] / export.php
blob248633f2c072dc0b98d28ee5e9e4e686b71d779c
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 'limit_to',
53 'limit_from',
54 'allrows',
55 'output_format',
56 'filename_template',
57 'maxsize',
58 'remember_template',
59 'charset_of_file',
60 'compression',
61 'what',
62 'knjenc',
63 'xkana',
64 'htmlword_structure_or_data',
65 'htmlword_null',
66 'htmlword_columns',
67 'mediawiki_structure_or_data',
68 'mediawiki_caption',
69 'pdf_report_title',
70 'pdf_structure_or_data',
71 'odt_structure_or_data',
72 'odt_relation',
73 'odt_comments',
74 'odt_mime',
75 'odt_columns',
76 'odt_null',
77 'codegen_structure_or_data',
78 'codegen_format',
79 'excel_null',
80 'excel_removeCRLF',
81 'excel_columns',
82 'excel_edition',
83 'excel_structure_or_data',
84 'yaml_structure_or_data',
85 'ods_null',
86 'ods_structure_or_data',
87 'ods_columns',
88 'json_structure_or_data',
89 'xml_structure_or_data',
90 'xml_export_functions',
91 'xml_export_procedures',
92 'xml_export_tables',
93 'xml_export_triggers',
94 'xml_export_views',
95 'xml_export_contents',
96 'texytext_structure_or_data',
97 'texytext_columns',
98 'texytext_null',
99 'phparray_structure_or_data',
100 'sql_include_comments',
101 'sql_header_comment',
102 'sql_dates',
103 'sql_relation',
104 'sql_mime',
105 'sql_use_transaction',
106 'sql_disable_fk',
107 'sql_compatibility',
108 'sql_structure_or_data',
109 'sql_create_database',
110 'sql_drop_table',
111 'sql_procedure_function',
112 'sql_create_table_statements',
113 'sql_create_table',
114 'sql_create_view',
115 'sql_create_trigger',
116 'sql_if_not_exists',
117 'sql_auto_increment',
118 'sql_backquotes',
119 'sql_truncate',
120 'sql_delayed',
121 'sql_ignore',
122 'sql_type',
123 'sql_insert_syntax',
124 'sql_max_query_size',
125 'sql_hex_for_blob',
126 'sql_utc_time',
127 'sql_drop_database',
128 'csv_separator',
129 'csv_enclosed',
130 'csv_escaped',
131 'csv_terminated',
132 'csv_null',
133 'csv_removeCRLF',
134 'csv_columns',
135 'csv_structure_or_data',
136 // csv_replace should have been here but we use it directly from $_POST
137 'latex_caption',
138 'latex_structure_or_data',
139 'latex_structure_caption',
140 'latex_structure_continued_caption',
141 'latex_structure_label',
142 'latex_relation',
143 'latex_comments',
144 'latex_mime',
145 'latex_columns',
146 'latex_data_caption',
147 'latex_data_continued_caption',
148 'latex_data_label',
149 'latex_null'
152 foreach ($post_params as $one_post_param) {
153 if (isset($_POST[$one_post_param])) {
154 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
158 // sanitize this parameter which will be used below in a file inclusion
159 $what = PMA_securePath($what);
161 PMA_Util::checkParameters(array('what', 'export_type'));
163 // export class instance, not array of properties, as before
164 $export_plugin = PMA_getPlugin(
165 "export",
166 $what,
167 'libraries/plugins/export/',
168 array(
169 'export_type' => $export_type,
170 'single_table' => isset($single_table)
174 // Backward compatibility
175 $type = $what;
177 // Check export type
178 if (! isset($export_plugin)) {
179 PMA_fatalError(__('Bad type!'));
183 * valid compression methods
185 $compression_methods = array(
186 'zip',
187 'gzip'
191 * init and variable checking
193 $compression = false;
194 $onserver = false;
195 $save_on_server = false;
196 $buffer_needed = false;
198 // Is it a quick or custom export?
199 if ($_REQUEST['quick_or_custom'] == 'quick') {
200 $quick_export = true;
201 } else {
202 $quick_export = false;
205 if ($_REQUEST['output_format'] == 'astext') {
206 $asfile = false;
207 } else {
208 $asfile = true;
209 if (in_array($_REQUEST['compression'], $compression_methods)) {
210 $compression = $_REQUEST['compression'];
211 $buffer_needed = true;
213 if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
214 || (! $quick_export && ! empty($_REQUEST['onserver']))
216 if ($quick_export) {
217 $onserver = $_REQUEST['quick_export_onserver'];
218 } else {
219 $onserver = $_REQUEST['onserver'];
221 // Will we save dump on server?
222 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
226 // Generate error url and check for needed variables
227 if ($export_type == 'server') {
228 $err_url = 'server_export.php?' . PMA_URL_getCommon();
229 } elseif ($export_type == 'database' && strlen($db)) {
230 $err_url = 'db_export.php?' . PMA_URL_getCommon($db);
231 // Check if we have something to export
232 if (isset($table_select)) {
233 $tables = $table_select;
234 } else {
235 $tables = array();
237 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
238 $err_url = 'tbl_export.php?' . PMA_URL_getCommon($db, $table);
239 } else {
240 PMA_fatalError(__('Bad parameters!'));
244 * Increase time limit for script execution and initializes some variables
246 @set_time_limit($cfg['ExecTimeLimit']);
247 if (! empty($cfg['MemoryLimit'])) {
248 @ini_set('memory_limit', $cfg['MemoryLimit']);
250 register_shutdown_function('PMA_shutdownDuringExport');
251 // Start with empty buffer
252 $dump_buffer = '';
253 $dump_buffer_len = 0;
255 // We send fake headers to avoid browser timeout when buffering
256 $time_start = time();
258 // Defines the default <CR><LF> format.
259 // For SQL always use \n as MySQL wants this on all platforms.
260 if ($what == 'sql') {
261 $crlf = "\n";
262 } else {
263 $crlf = PMA_Util::whichCrlf();
266 $output_kanji_conversion = function_exists('PMA_Kanji_strConv')
267 && $type != 'xls';
269 // Do we need to convert charset?
270 $output_charset_conversion = $asfile
271 && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
272 && isset($charset_of_file) && $charset_of_file != 'utf-8'
273 && $type != 'xls';
275 // Use on the fly compression?
276 $GLOBALS['onfly_compression'] = $GLOBALS['cfg']['CompressOnFly']
277 && $compression == 'gzip';
278 if ($GLOBALS['onfly_compression']) {
279 $GLOBALS['memory_limit'] = PMA_getMemoryLimitForExport();
282 // Generate filename and mime type if needed
283 if ($asfile) {
284 if (empty($remember_template)) {
285 $remember_template = '';
287 list($filename, $mime_type) = PMA_getExportFilenameAndMimetype(
288 $export_type, $remember_template, $export_plugin, $compression,
289 $filename_template
293 // Open file on server if needed
294 if ($save_on_server) {
295 list($save_filename, $message, $file_handle) = PMA_openExportFile(
296 $filename, $quick_export
299 // problem opening export file on server?
300 if (! empty($message)) {
301 PMA_showExportPage($export_type);
303 } else {
305 * Send headers depending on whether the user chose to download a dump file
306 * or not
308 if ($asfile) {
309 // Download
310 // (avoid rewriting data containing HTML with anchors and forms;
311 // this was reported to happen under Plesk)
312 @ini_set('url_rewriter.tags', '');
313 $filename = PMA_sanitizeFilename($filename);
315 PMA_downloadHeader($filename, $mime_type);
316 } else {
317 // HTML
318 if ($export_type == 'database') {
319 $num_tables = count($tables);
320 if ($num_tables == 0) {
321 $message = PMA_Message::error(
322 __('No tables found in database.')
324 $active_page = 'db_export.php';
325 include 'db_export.php';
326 exit();
329 list($html, $back_button) = PMA_getHtmlForDisplayedExportHeader(
330 $export_type, $db, $table
332 echo $html;
333 unset($html);
334 } // end download
337 // Fake loop just to allow skip of remain of this code by break, I'd really
338 // need exceptions here :-)
339 do {
341 // Add possibly some comments to export
342 if (! $export_plugin->exportHeader($db)) {
343 break;
346 // Will we need relation & co. setup?
347 $do_relation = isset($GLOBALS[$what . '_relation']);
348 $do_comments = isset($GLOBALS[$what . '_include_comments'])
349 || isset($GLOBALS[$what . '_comments']) ;
350 $do_mime = isset($GLOBALS[$what . '_mime']);
351 if ($do_relation || $do_comments || $do_mime) {
352 $cfgRelation = PMA_getRelationsParam();
354 if ($do_mime) {
355 include_once 'libraries/transformations.lib.php';
358 // Include dates in export?
359 $do_dates = isset($GLOBALS[$what . '_dates']);
361 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
364 * Builds the dump
366 if ($export_type == 'server') {
367 if (! isset($db_select)) {
368 $db_select = '';
370 PMA_exportServer(
371 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
372 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
374 } elseif ($export_type == 'database') {
375 PMA_exportDatabase(
376 $db, $tables, $whatStrucOrData, $export_plugin, $crlf, $err_url,
377 $export_type, $do_relation, $do_comments, $do_mime, $do_dates
379 } else {
380 // We export just one table
381 // $allrows comes from the form when "Dump all rows" has been selected
382 if (! isset($allrows)) {
383 $allrows = '';
385 if (! isset($limit_to)) {
386 $limit_to = 0;
388 if (! isset($limit_from)) {
389 $limit_from = 0;
391 PMA_exportTable(
392 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
393 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
394 $allrows, $limit_to, $limit_from, $sql_query
397 if (! $export_plugin->exportFooter()) {
398 break;
401 } while (false);
402 // End of fake loop
404 if ($save_on_server && ! empty($message)) {
405 PMA_showExportPage($export_type);
409 * Send the dump as a file...
411 if (! empty($asfile)) {
412 // Convert the charset if required.
413 if ($output_charset_conversion) {
414 $dump_buffer = PMA_convertString(
415 'utf-8',
416 $GLOBALS['charset_of_file'],
417 $dump_buffer
421 // Compression needed?
422 if ($compression) {
423 $dump_buffer
424 = PMA_compressExport($dump_buffer, $compression, $filename);
427 /* If we saved on server, we have to close file now */
428 if ($save_on_server) {
429 $message = PMA_closeExportFile(
430 $file_handle, $dump_buffer, $save_filename
432 PMA_showExportPage($export_type);
433 } else {
434 echo $dump_buffer;
436 } else {
437 echo PMA_getHtmlForDisplayedExportFooter($back_button);
438 } // end if