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 $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']);
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
76 'htmlword_structure_or_data',
80 'mediawiki_structure_or_data',
82 'pdf_structure_or_data',
83 'odt_structure_or_data',
89 'codegen_structure_or_data',
95 'excel_structure_or_data',
96 'yaml_structure_or_data',
98 'ods_structure_or_data',
100 'json_structure_or_data',
103 'xml_structure_or_data',
105 'xml_export_functions',
106 'xml_export_procedures',
108 'xml_export_triggers',
110 'xml_export_contents',
111 'texytext_structure_or_data',
114 'phparray_structure_or_data',
115 'sql_include_comments',
116 'sql_header_comment',
120 'sql_use_transaction',
123 'sql_structure_or_data',
124 'sql_create_database',
126 'sql_procedure_function',
129 'sql_create_trigger',
131 'sql_auto_increment',
138 'sql_max_query_size',
139 'sql_hex_for_binary',
142 'sql_views_as_tables',
151 'csv_structure_or_data',
152 // csv_replace should have been here but we use it directly from $_POST
154 'latex_structure_or_data',
155 'latex_structure_caption',
156 'latex_structure_continued_caption',
157 'latex_structure_label',
162 'latex_data_caption',
163 'latex_data_continued_caption',
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(
187 'libraries/classes/Plugins/Export/',
189 'export_type' => $export_type,
190 'single_table' => isset($single_table)
195 if (empty($export_plugin)) {
196 Core
::fatalError(__('Bad type!'));
200 * valid compression methods
202 $compression_methods = array(
208 * init and variable checking
210 $compression = false;
212 $save_on_server = false;
213 $buffer_needed = false;
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;
228 $quick_export = false;
231 if ($_POST['output_format'] == 'astext') {
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']))
253 $onserver = $_POST['quick_export_onserver'];
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;
283 } elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
284 $err_url = 'tbl_export.php' . Url
::getCommon(
286 'db' => $db, 'table' => $table
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);
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
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') {
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
350 if (empty($remember_template)) {
351 $remember_template = '';
353 list($filename, $mime_type) = Export
::getFilenameAndMimetype(
354 $export_type, $remember_template, $export_plugin, $compression,
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);
373 * Send headers depending on whether the user chose to download a dump file
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);
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';
397 list($html, $back_button, $refreshButton) = Export
::getHtmlForDisplayedExportHeader(
398 $export_type, $db, $table
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 :-)
412 $dump_buffer_len = 0;
414 // Add possibly some comments to export
415 if (! $export_plugin->exportHeader()) {
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'];
436 if ($export_type == 'server') {
437 if (! isset($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");
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,
466 Export
::unlockTables();
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
476 // We export just one table
477 // $allrows comes from the form when "Dump all rows" has been selected
478 if (! isset($allrows)) {
481 if (! isset($limit_to)) {
484 if (! isset($limit_from)) {
487 if (isset($lock_tables)) {
489 Export
::lockTables($db, array($table), "READ");
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,
497 Export
::unlockTables();
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()) {
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);
526 // Convert the charset if required.
527 if ($output_charset_conversion) {
528 $dump_buffer = Encoding
::convertString(
535 // Compression needed?
537 if (! empty($separate_files)) {
538 $dump_buffer = Export
::compress(
539 $dump_buffer_objects, $compression, $filename
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);