2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * @todo too much die here, or?
9 * Get the variables sent or posted to this script and a core script
11 require_once './libraries/common.inc.php';
12 require_once './libraries/zip.lib.php';
13 require_once './libraries/plugin_interface.lib.php';
15 PMA_checkParameters(array('what', 'export_type'));
18 $export_list = PMA_getPlugins('./libraries/export/', array('export_type' => $export_type, 'single_table' => isset($single_table)));
20 // Backward compatbility
24 if (!isset($export_list[$type])) {
29 * valid compression methods
31 $compression_methods = array(
38 * init and variable checking
42 $save_on_server = false;
43 $buffer_needed = false;
45 // Is it a quick or custom export?
46 if($_REQUEST['quick_or_custom'] == 'quick') {
49 $quick_export = false;
52 if ($_REQUEST['output_format'] == 'astext') {
56 if (in_array($_REQUEST['compression'], $compression_methods)) {
57 $compression = $_REQUEST['compression'];
58 $buffer_needed = true;
60 if (($quick_export && !empty($_REQUEST['quick_export_onserver'])) ||
(!$quick_export && !empty($_REQUEST['onserver']))) {
62 $onserver = $_REQUEST['quick_export_onserver'];
64 $onserver = $_REQUEST['onserver'];
66 // Will we save dump on server?
67 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
71 // Does export require to be into file?
72 if (isset($export_list[$type]['force_file']) && ! $asfile) {
73 $message = PMA_Message
::error(__('Selected export type has to be saved in file!'));
74 require_once './libraries/header.inc.php';
75 if ($export_type == 'server') {
76 $active_page = 'server_export.php';
77 require './server_export.php';
78 } elseif ($export_type == 'database') {
79 $active_page = 'db_export.php';
80 require './db_export.php';
82 $active_page = 'tbl_export.php';
83 require './tbl_export.php';
88 // Generate error url and check for needed variables
89 if ($export_type == 'server') {
90 $err_url = 'server_export.php?' . PMA_generate_common_url();
91 } elseif ($export_type == 'database' && strlen($db)) {
92 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
93 // Check if we have something to export
94 if (isset($table_select)) {
95 $tables = $table_select;
99 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
100 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
102 die('Bad parameters!');
105 // Get the functions specific to the export type
106 require './libraries/export/' . PMA_securePath($type) . '.php';
109 * Increase time limit for script execution and initializes some variables
111 @set_time_limit
($cfg['ExecTimeLimit']);
112 if (!empty($cfg['MemoryLimit'])) {
113 @ini_set
('memory_limit', $cfg['MemoryLimit']);
116 // Start with empty buffer
118 $dump_buffer_len = 0;
120 // We send fake headers to avoid browser timeout when buffering
121 $time_start = time();
125 * Output handler for all exports, if needed buffering, it stores data into
126 * $dump_buffer, otherwise it prints thems out.
128 * @param string the insert statement
130 * @return bool Whether output suceeded
132 function PMA_exportOutputHandler($line)
134 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
136 // Kanji encoding convert feature
137 if ($GLOBALS['output_kanji_conversion']) {
138 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
140 // If we have to buffer data, we will perform everything at once at the end
141 if ($GLOBALS['buffer_needed']) {
143 $dump_buffer .= $line;
144 if ($GLOBALS['onfly_compression']) {
146 $dump_buffer_len +
= strlen($line);
148 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
149 if ($GLOBALS['output_charset_conversion']) {
150 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
153 if ($GLOBALS['compression'] == 'bzip' && @function_exists
('bzcompress')) {
154 $dump_buffer = bzcompress($dump_buffer);
157 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
158 // without the optional parameter level because it bug
159 $dump_buffer = gzencode($dump_buffer);
161 if ($GLOBALS['save_on_server']) {
162 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
163 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
164 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
165 $GLOBALS['message']->addParam($save_filename);
172 $dump_buffer_len = 0;
176 if ($time_start >= $time_now +
30) {
177 $time_start = $time_now;
178 header('X-pmaPing: Pong');
182 if ($GLOBALS['asfile']) {
183 if ($GLOBALS['output_charset_conversion']) {
184 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
186 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
187 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
188 if (!$write_result ||
($write_result != strlen($line))) {
189 $GLOBALS['message'] = PMA_Message
::error(__('Insufficient space to save the file %s.'));
190 $GLOBALS['message']->addParam($save_filename);
194 if ($time_start >= $time_now +
30) {
195 $time_start = $time_now;
196 header('X-pmaPing: Pong');
199 // We export as file - output normally
203 // We export as html - replace special chars
204 echo htmlspecialchars($line);
208 } // end of the 'PMA_exportOutputHandler()' function
210 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
211 if ($what == 'sql') {
214 $crlf = PMA_whichCrlf();
217 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
219 // Do we need to convert charset?
220 $output_charset_conversion = $asfile && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
221 && isset($charset_of_file) && $charset_of_file != $charset
224 // Use on the fly compression?
225 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' ||
$compression == 'bzip');
226 if ($onfly_compression) {
227 $memory_limit = trim(@ini_get
('memory_limit'));
229 if (empty($memory_limit)) {
230 $memory_limit = 2 * 1024 * 1024;
233 if (strtolower(substr($memory_limit, -1)) == 'm') {
234 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
235 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
236 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
237 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
238 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
240 $memory_limit = (int)$memory_limit;
243 // Some of memory is needed for other thins and as treshold.
244 // Nijel: During export I had allocated (see memory_get_usage function)
245 // approx 1.2MB so this comes from that.
246 if ($memory_limit > 1500000) {
247 $memory_limit -= 1500000;
250 // Some memory is needed for compression, assume 1/3
254 // Generate filename and mime type if needed
256 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
257 if ($export_type == 'server') {
258 if (isset($remember_template)) {
259 $GLOBALS['PMA_Config']->setCookie('pma_server_filename_template', $filename_template);
261 } elseif ($export_type == 'database') {
262 if (isset($remember_template)) {
263 $GLOBALS['PMA_Config']->setCookie('pma_db_filename_template', $filename_template);
266 if (isset($remember_template)) {
267 $GLOBALS['PMA_Config']->setCookie('pma_table_filename_template', $filename_template);
270 $filename = PMA_expandUserString($filename_template);
272 // convert filename to iso-8859-1, it is safer
273 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
275 // Grab basic dump extension and mime type
276 $filename .= '.' . $export_list[$type]['extension'];
277 $mime_type = $export_list[$type]['mime_type'];
279 // If dump is going to be compressed, set correct mime_type and add
280 // compression to extension
281 if ($compression == 'bzip') {
283 $mime_type = 'application/x-bzip2';
284 } elseif ($compression == 'gzip') {
286 $mime_type = 'application/x-gzip';
287 } elseif ($compression == 'zip') {
289 $mime_type = 'application/zip';
293 // Open file on server if needed
294 if ($save_on_server) {
295 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
297 if (file_exists($save_filename) && ((!$quick_export && empty($onserverover)) ||
($quick_export && $_REQUEST['quick_export_onserverover'] != 'saveitover'))) {
298 $message = PMA_Message
::error(__('File %s already exists on server, change filename or check overwrite option.'));
299 $message->addParam($save_filename);
301 if (is_file($save_filename) && !is_writable($save_filename)) {
302 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
303 $message->addParam($save_filename);
305 if (!$file_handle = @fopen
($save_filename, 'w')) {
306 $message = PMA_Message
::error(__('The web server does not have permission to save the file %s.'));
307 $message->addParam($save_filename);
311 if (isset($message)) {
312 require_once './libraries/header.inc.php';
313 if ($export_type == 'server') {
314 $active_page = 'server_export.php';
315 require './server_export.php';
316 } elseif ($export_type == 'database') {
317 $active_page = 'db_export.php';
318 require './db_export.php';
320 $active_page = 'tbl_export.php';
321 require './tbl_export.php';
328 * Send headers depending on whether the user chose to download a dump file
331 if (!$save_on_server) {
334 // (avoid rewriting data containing HTML with anchors and forms;
335 // this was reported to happen under Plesk)
336 @ini_set
('url_rewriter.tags','');
338 header('Content-Type: ' . $mime_type);
339 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
340 // Tested behavior of
342 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
346 header('Content-Disposition: attachment; filename="' . $filename . '"');
347 if (PMA_USR_BROWSER_AGENT
== 'IE') {
348 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
349 header('Pragma: public');
351 header('Pragma: no-cache');
352 // test case: exporting a database into a .gz file with Safari
353 // would produce files not having the current time
354 // (added this header for Safari but should not harm other browsers)
355 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
359 if ($export_type == 'database') {
360 $num_tables = count($tables);
361 if ($num_tables == 0) {
362 $message = PMA_Message
::error(__('No tables found in database.'));
363 require_once './libraries/header.inc.php';
364 $active_page = 'db_export.php';
365 require './db_export.php';
369 $backup_cfgServer = $cfg['Server'];
370 require_once './libraries/header.inc.php';
371 $cfg['Server'] = $backup_cfgServer;
372 unset($backup_cfgServer);
373 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
374 //echo ' <pre>' . "\n";
375 echo ' <form name="nofunction">' . "\n"
376 // remove auto-select for now: there is no way to select
377 // only a part of the text; anyway, it should obey
378 // $cfg['TextareaAutoSelect']
379 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
380 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
384 // Fake loop just to allow skip of remain of this code by break, I'd really
385 // need exceptions here :-)
388 // Add possibly some comments to export
389 if (!PMA_exportHeader()) {
393 // Will we need relation & co. setup?
394 $do_relation = isset($GLOBALS[$what . '_relation']);
395 $do_comments = isset($GLOBALS[$what . '_include_comments']);
396 $do_mime = isset($GLOBALS[$what . '_mime']);
397 if ($do_relation ||
$do_comments ||
$do_mime) {
398 $cfgRelation = PMA_getRelationsParam();
401 require_once './libraries/transformations.lib.php';
404 // Include dates in export?
405 $do_dates = isset($GLOBALS[$what . '_dates']);
410 // Gets the number of tables if a dump of a database has been required
411 if ($export_type == 'server') {
412 if (isset($db_select)) {
413 $tmp_select = implode($db_select, '|');
414 $tmp_select = '|' . $tmp_select . '|';
416 // Walk over databases
417 foreach ($GLOBALS['pma']->databases
as $current_db) {
418 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
419 ||
!isset($tmp_select)) {
420 if (!PMA_exportDBHeader($current_db)) {
423 if (!PMA_exportDBCreate($current_db)) {
426 $tables = PMA_DBI_get_tables($current_db);
428 foreach ($tables as $table) {
429 // if this is a view, collect it for later; views must be exported
431 $is_view = PMA_Table
::isView($current_db, $table);
435 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
436 // for a view, export a stand-in definition of the table
437 // to resolve view dependencies
438 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
442 // if this is a view or a merge table, don't export data
443 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($current_db, $table))) {
444 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
445 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
449 // now export the triggers (needs to be done after the data because
450 // triggers can modify already imported tables)
451 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
452 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
457 foreach($views as $view) {
458 // no data export for a view
459 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
460 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
465 if (!PMA_exportDBFooter($current_db)) {
470 } elseif ($export_type == 'database') {
471 if (!PMA_exportDBHeader($db)) {
476 // $tables contains the choices from the user (via $table_select)
477 foreach ($tables as $table) {
478 // if this is a view, collect it for later; views must be exported after
480 $is_view = PMA_Table
::isView($db, $table);
484 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
485 // for a view, export a stand-in definition of the table
486 // to resolve view dependencies
487 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'stand_in' : 'create_table', $export_type)) {
491 // if this is a view or a merge table, don't export data
492 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && !($is_view || PMA_Table
::isMerge($db, $table))) {
493 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
494 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
498 // now export the triggers (needs to be done after the data because
499 // triggers can modify already imported tables)
500 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
501 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
506 foreach ($views as $view) {
507 // no data export for a view
508 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
509 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
515 if (!PMA_exportDBFooter($db)) {
519 if (!PMA_exportDBHeader($db)) {
522 // We export just one table
523 // $allrows comes from the form when "Dump all rows" has been selected
524 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
525 $add_query = ' LIMIT '
526 . (($limit_from > 0) ?
$limit_from . ', ' : '')
532 $is_view = PMA_Table
::isView($db, $table);
533 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
534 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ?
'create_view' : 'create_table', $export_type)) {
538 // If this is an export of a single view, we have to export data;
539 // for example, a PDF report
540 // if it is a merge table, no data is exported
541 if (($GLOBALS[$what . '_structure_or_data'] == 'data' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') && ! PMA_Table
::isMerge($db, $table)) {
542 if (!empty($sql_query)) {
543 // only preg_replace if needed
544 if (!empty($add_query)) {
545 // remove trailing semicolon before adding a LIMIT
546 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
548 $local_query = $sql_query . $add_query;
549 PMA_DBI_select_db($db);
551 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
553 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
557 // now export the triggers (needs to be done after the data because
558 // triggers can modify already imported tables)
559 if ($GLOBALS[$what . '_structure_or_data'] == 'structure' ||
$GLOBALS[$what . '_structure_or_data'] == 'structure_and_data') {
560 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
564 if (!PMA_exportDBFooter($db)) {
568 if (!PMA_exportFooter()) {
575 if ($save_on_server && isset($message)) {
576 require_once './libraries/header.inc.php';
577 if ($export_type == 'server') {
578 $active_page = 'server_export.php';
579 require './server_export.php';
580 } elseif ($export_type == 'database') {
581 $active_page = 'db_export.php';
582 require './db_export.php';
584 $active_page = 'tbl_export.php';
585 require './tbl_export.php';
591 * Send the dump as a file...
593 if (!empty($asfile)) {
594 // Convert the charset if required.
595 if ($output_charset_conversion) {
596 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
599 // Do the compression
600 // 1. as a zipped file
601 if ($compression == 'zip') {
602 if (@function_exists
('gzcompress')) {
603 $zipfile = new zipfile();
604 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
605 $dump_buffer = $zipfile -> file();
608 // 2. as a bzipped file
609 elseif ($compression == 'bzip') {
610 if (@function_exists
('bzcompress')) {
611 $dump_buffer = bzcompress($dump_buffer);
614 // 3. as a gzipped file
615 elseif ($compression == 'gzip') {
616 if (@function_exists
('gzencode') && !@ini_get
('zlib.output_compression')) {
617 // without the optional parameter level because it bug
618 $dump_buffer = gzencode($dump_buffer);
622 /* If ve saved on server, we have to close file now */
623 if ($save_on_server) {
624 $write_result = @fwrite
($file_handle, $dump_buffer);
625 fclose($file_handle);
626 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
627 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message
::ERROR
, $save_filename);
629 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message
::SUCCESS
, $save_filename);
632 require_once './libraries/header.inc.php';
633 if ($export_type == 'server') {
634 $active_page = 'server_export.php';
635 require_once './server_export.php';
636 } elseif ($export_type == 'database') {
637 $active_page = 'db_export.php';
638 require_once './db_export.php';
640 $active_page = 'tbl_export.php';
641 require_once './tbl_export.php';
649 * Displays the dump...
653 * Close the html tags and add the footers in dump is displayed on screen
655 //echo ' </pre>' . "\n";
656 echo '</textarea>' . "\n"
658 echo '</div>' . "\n";
661 <script type
="text/javascript">
663 var bodyWidth
=null; var bodyHeight
=null;
664 if (document
.getElementById('textSQLDUMP')) {
665 bodyWidth
= self
.innerWidth
;
666 bodyHeight
= self
.innerHeight
;
667 if (!bodyWidth
&& !bodyHeight
) {
668 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
669 bodyWidth
= document
.body
.clientWidth
;
670 bodyHeight
= document
.body
.clientHeight
;
671 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
672 bodyWidth
= document
.documentElement
.clientWidth
;
673 bodyHeight
= document
.documentElement
.clientHeight
;
676 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
677 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
682 require './libraries/footer.inc.php';