2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Main export handling code
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
;
18 use PhpMyAdmin\Response
;
21 * Get the variables sent or posted to this script and a core script
23 include_once 'libraries/common.inc.php';
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();
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']);
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
83 'htmlword_structure_or_data',
87 'mediawiki_structure_or_data',
89 'pdf_structure_or_data',
90 'odt_structure_or_data',
96 'codegen_structure_or_data',
102 'excel_structure_or_data',
103 'yaml_structure_or_data',
105 'ods_structure_or_data',
107 'json_structure_or_data',
110 'xml_structure_or_data',
112 'xml_export_functions',
113 'xml_export_procedures',
115 'xml_export_triggers',
117 'xml_export_contents',
118 'texytext_structure_or_data',
121 'phparray_structure_or_data',
122 'sql_include_comments',
123 'sql_header_comment',
127 'sql_use_transaction',
130 'sql_structure_or_data',
131 'sql_create_database',
133 'sql_procedure_function',
136 'sql_create_trigger',
138 'sql_auto_increment',
145 'sql_max_query_size',
146 'sql_hex_for_binary',
149 'sql_views_as_tables',
158 'csv_structure_or_data',
159 // csv_replace should have been here but we use it directly from $_POST
161 'latex_structure_or_data',
162 'latex_structure_caption',
163 'latex_structure_continued_caption',
164 'latex_structure_label',
169 'latex_data_caption',
170 'latex_data_continued_caption',
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(
194 'libraries/classes/Plugins/Export/',
196 'export_type' => $export_type,
197 'single_table' => isset($single_table)
202 if (empty($export_plugin)) {
203 Core
::fatalError(__('Bad type!'));
207 * valid compression methods
209 $compression_methods = array(
215 * init and variable checking
217 $compression = false;
219 $save_on_server = false;
220 $buffer_needed = false;
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;
235 $quick_export = false;
238 if ($_REQUEST['output_format'] == 'astext') {
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']))
260 $onserver = $_REQUEST['quick_export_onserver'];
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;
280 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
281 $err_url = 'tbl_export.php' . Url
::getCommon(
283 'db' => $db, 'table' => $table
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);
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
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') {
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
347 if (empty($remember_template)) {
348 $remember_template = '';
350 list($filename, $mime_type) = Export
::getFilenameAndMimetype(
351 $export_type, $remember_template, $export_plugin, $compression,
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);
370 * Send headers depending on whether the user chose to download a dump file
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);
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';
394 list($html, $back_button, $refreshButton) = Export
::getHtmlForDisplayedExportHeader(
395 $export_type, $db, $table
402 // Fake loop just to allow skip of remain of this code by break, I'd really
403 // need exceptions here :-)
407 $dump_buffer_len = 0;
409 // Add possibly some comments to export
410 if (! $export_plugin->exportHeader()) {
414 // Will we need relation & co. setup?
415 $do_relation = isset($GLOBALS[$what . '_relation']);
416 $do_comments = isset($GLOBALS[$what . '_include_comments'])
417 ||
isset($GLOBALS[$what . '_comments']);
418 $do_mime = isset($GLOBALS[$what . '_mime']);
419 if ($do_relation ||
$do_comments ||
$do_mime) {
420 $cfgRelation = Relation
::getRelationsParam();
423 // Include dates in export?
424 $do_dates = isset($GLOBALS[$what . '_dates']);
426 $whatStrucOrData = $GLOBALS[$what . '_structure_or_data'];
431 if ($export_type == 'server') {
432 if (! isset($db_select)) {
435 Export
::exportServer(
436 $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url,
437 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
438 $aliases, $separate_files
440 } elseif ($export_type == 'database') {
441 if (!isset($table_structure) ||
!is_array($table_structure)) {
442 $table_structure = array();
444 if (!isset($table_data) ||
!is_array($table_data)) {
445 $table_data = array();
447 if (!empty($_REQUEST['structure_or_data_forced'])) {
448 $table_structure = $tables;
449 $table_data = $tables;
451 if (isset($lock_tables)) {
452 Export
::lockTables($db, $tables, "READ");
454 Export
::exportDatabase(
455 $db, $tables, $whatStrucOrData, $table_structure,
456 $table_data, $export_plugin, $crlf, $err_url, $export_type,
457 $do_relation, $do_comments, $do_mime, $do_dates, $aliases,
461 Export
::unlockTables();
464 Export
::exportDatabase(
465 $db, $tables, $whatStrucOrData, $table_structure, $table_data,
466 $export_plugin, $crlf, $err_url, $export_type, $do_relation,
467 $do_comments, $do_mime, $do_dates, $aliases, $separate_files
471 // We export just one table
472 // $allrows comes from the form when "Dump all rows" has been selected
473 if (! isset($allrows)) {
476 if (! isset($limit_to)) {
479 if (! isset($limit_from)) {
482 if (isset($lock_tables)) {
484 Export
::lockTables($db, array($table), "READ");
486 $db, $table, $whatStrucOrData, $export_plugin, $crlf,
487 $err_url, $export_type, $do_relation, $do_comments,
488 $do_mime, $do_dates, $allrows, $limit_to, $limit_from,
492 Export
::unlockTables();
496 $db, $table, $whatStrucOrData, $export_plugin, $crlf, $err_url,
497 $export_type, $do_relation, $do_comments, $do_mime, $do_dates,
498 $allrows, $limit_to, $limit_from, $sql_query, $aliases
502 if (! $export_plugin->exportFooter()) {
509 if ($save_on_server && ! empty($message)) {
510 Export
::showPage($db, $table, $export_type);
514 * Send the dump as a file...
516 if (empty($asfile)) {
517 echo Export
::getHtmlForDisplayedExportFooter($back_button, $refreshButton);
521 // Convert the charset if required.
522 if ($output_charset_conversion) {
523 $dump_buffer = Encoding
::convertString(
530 // Compression needed?
532 if (! empty($separate_files)) {
533 $dump_buffer = Export
::compress(
534 $dump_buffer_objects, $compression, $filename
537 $dump_buffer = Export
::compress($dump_buffer, $compression, $filename);
542 /* If we saved on server, we have to close file now */
543 if ($save_on_server) {
544 $message = Export
::closeFile(
545 $file_handle, $dump_buffer, $save_filename
547 Export
::showPage($db, $table, $export_type);