3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Get the variables sent or posted to this script and a core script
8 require_once('./libraries/common.lib.php');
9 require_once('./libraries/zip.lib.php');
10 require_once('./libraries/plugin_interface.lib.php');
12 PMA_checkParameters(array('what', 'export_type'));
15 $export_list = PMA_getPlugins('./libraries/export/', array('export_type' => $export_type, 'single_table' => isset($single_table)));
17 // Backward compatbility
21 if (!isset($export_list[$type])) {
25 // Does export require to be into file?
26 if (isset($export_list[$type]['force_file']) && ! isset($asfile)) {
27 $message = $strExportMustBeFile;
28 $GLOBALS['show_error_header'] = true;
29 $js_to_run = 'functions.js';
30 require_once('./libraries/header.inc.php');
31 if ($export_type == 'server') {
32 $active_page = 'server_export.php';
33 require('./server_export.php');
34 } elseif ($export_type == 'database') {
35 $active_page = 'db_export.php';
36 require('./db_export.php');
38 $active_page = 'tbl_export.php';
39 require('./tbl_export.php');
44 // Generate error url and check for needed variables
45 if ($export_type == 'server') {
46 $err_url = 'server_export.php?' . PMA_generate_common_url();
47 } elseif ($export_type == 'database' && isset($db) && strlen($db)) {
48 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
49 // Check if we have something to export
50 if (isset($table_select)) {
51 $tables = $table_select;
55 } elseif ($export_type == 'table' && isset($db) && strlen($db) && isset($table) && strlen($table)) {
56 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
58 die('Bad parameters!');
61 // Get the functions specific to the export type
62 require('./libraries/export/' . PMA_securePath($type) . '.php');
65 * Increase time limit for script execution and initializes some variables
67 @set_time_limit
($cfg['ExecTimeLimit']);
68 if (!empty($cfg['MemoryLimit'])) {
69 @ini_set
('memory_limit', $cfg['MemoryLimit']);
72 // Start with empty buffer
76 // We send fake headers to avoid browser timeout when buffering
81 * Output handler for all exports, if needed buffering, it stores data into
82 * $dump_buffer, otherwise it prints thems out.
84 * @param string the insert statement
86 * @return bool Whether output suceeded
88 function PMA_exportOutputHandler($line)
90 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
92 // Kanji encoding convert feature
93 if ($GLOBALS['output_kanji_conversion']) {
94 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
96 // If we have to buffer data, we will perform everything at once at the end
97 if ($GLOBALS['buffer_needed']) {
99 $dump_buffer .= $line;
100 if ($GLOBALS['onfly_compression']) {
102 $dump_buffer_len +
= strlen($line);
104 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
105 if ($GLOBALS['output_charset_conversion']) {
106 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
109 if ($GLOBALS['compression'] == 'bzip' && @function_exists
('bzcompress')) {
110 $dump_buffer = bzcompress($dump_buffer);
113 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
114 // without the optional parameter level because it bug
115 $dump_buffer = gzencode($dump_buffer);
117 if ($GLOBALS['save_on_server']) {
118 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
119 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
120 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
121 $GLOBALS['show_error_header'] = TRUE;
128 $dump_buffer_len = 0;
132 if ($time_start >= $time_now +
30) {
133 $time_start = $time_now;
134 header('X-pmaPing: Pong');
138 if ($GLOBALS['asfile']) {
139 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
140 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
141 if (!$write_result ||
($write_result != strlen($line))) {
142 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
143 $GLOBALS['show_error_header'] = TRUE;
147 if ($time_start >= $time_now +
30) {
148 $time_start = $time_now;
149 header('X-pmaPing: Pong');
152 // We export as file - output normally
153 if ($GLOBALS['output_charset_conversion']) {
154 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
159 // We export as html - replace special chars
160 echo htmlspecialchars($line);
164 } // end of the 'PMA_exportOutputHandler()' function
166 // Will we save dump on server?
167 $save_on_server = isset($cfg['SaveDir']) && !empty($cfg['SaveDir']) && !empty($onserver);
169 // Ensure compressed formats are associated with the download feature
170 if (empty($asfile)) {
171 if ($save_on_server) {
173 } elseif (isset($compression) && ($compression == 'zip' |
$compression == 'gzip' |
$compression == 'bzip')) {
182 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
183 if ($what == 'sql') {
186 $crlf = PMA_whichCrlf();
189 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
191 // Do we need to convert charset?
192 $output_charset_conversion = $asfile &&
193 $cfg['AllowAnywhereRecoding'] && $allow_recoding
194 && isset($charset_of_file) && $charset_of_file != $charset
197 // Set whether we will need buffering
198 $buffer_needed = isset($compression) && ($compression == 'zip' |
$compression == 'gzip' |
$compression == 'bzip');
200 // Use on fly compression?
201 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && isset($compression) && ($compression == 'gzip' |
$compression == 'bzip');
202 if ($onfly_compression) {
203 $memory_limit = trim(@ini_get
('memory_limit'));
205 if (empty($memory_limit)) {
206 $memory_limit = 2 * 1024 * 1024;
209 if (strtolower(substr($memory_limit, -1)) == 'm') {
210 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
211 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
212 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
213 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
214 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
216 $memory_limit = (int)$memory_limit;
219 // Some of memory is needed for other thins and as treshold.
220 // Nijel: During export I had allocated (see memory_get_usage function)
221 // approx 1.2MB so this comes from that.
222 if ($memory_limit > 1500000) {
223 $memory_limit -= 1500000;
226 // Some memory is needed for compression, assume 1/3
230 // Generate filename and mime type if needed
232 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
233 if ($export_type == 'server') {
234 if (isset($remember_template)) {
235 PMA_setCookie('pma_server_filename_template', $filename_template);
237 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template));
238 } elseif ($export_type == 'database') {
239 if (isset($remember_template)) {
240 PMA_setCookie('pma_db_filename_template', $filename_template);
242 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)));
244 if (isset($remember_template)) {
245 PMA_setCookie('pma_table_filename_template', $filename_template);
247 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))));
250 // convert filename to iso-8859-1, it is safer
251 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
252 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
254 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
257 // Grab basic dump extension and mime type
258 $filename .= '.' . $export_list[$type]['extension'];
259 $mime_type = $export_list[$type]['mime_type'];
261 // If dump is going to be compressed, set correct encoding or mime_type and add
262 // compression to extension
263 $content_encoding = '';
264 if (isset($compression) && $compression == 'bzip') {
266 // browsers don't like this:
267 //$content_encoding = 'x-bzip2';
268 $mime_type = 'application/x-bzip2';
269 } elseif (isset($compression) && $compression == 'gzip') {
271 // Needed to avoid recompression by server modules like mod_gzip.
272 // It seems necessary to check about zlib.output_compression
273 // to avoid compressing twice
274 if (!@ini_get
('zlib.output_compression')) {
275 $content_encoding = 'x-gzip';
276 $mime_type = 'application/x-gzip';
278 } elseif (isset($compression) && $compression == 'zip') {
280 $mime_type = 'application/zip';
284 // Open file on server if needed
285 if ($save_on_server) {
286 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
288 if (file_exists($save_filename) && empty($onserverover)) {
289 $message = sprintf($strFileAlreadyExists, htmlspecialchars($save_filename));
290 $GLOBALS['show_error_header'] = TRUE;
292 if (is_file($save_filename) && !is_writable($save_filename)) {
293 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
294 $GLOBALS['show_error_header'] = TRUE;
296 if (!$file_handle = @fopen
($save_filename, 'w')) {
297 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
298 $GLOBALS['show_error_header'] = TRUE;
302 if (isset($message)) {
303 $js_to_run = 'functions.js';
304 require_once('./libraries/header.inc.php');
305 if ($export_type == 'server') {
306 $active_page = 'server_export.php';
307 require('./server_export.php');
308 } elseif ($export_type == 'database') {
309 $active_page = 'db_export.php';
310 require('./db_export.php');
312 $active_page = 'tbl_export.php';
313 require('./tbl_export.php');
320 * Send headers depending on whether the user chose to download a dump file
323 if (!$save_on_server) {
326 if (!empty($content_encoding)) {
327 header('Content-Encoding: ' . $content_encoding);
329 header('Content-Type: ' . $mime_type);
330 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
331 // lem9: Tested behavior of
333 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
337 header('Content-Disposition: attachment; filename="' . $filename . '"');
338 if (PMA_USR_BROWSER_AGENT
== 'IE') {
339 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
340 header('Pragma: public');
342 header('Pragma: no-cache');
346 if ($export_type == 'database') {
347 $num_tables = count($tables);
348 if ($num_tables == 0) {
349 $message = $strNoTablesFound;
350 $js_to_run = 'functions.js';
351 require_once('./libraries/header.inc.php');
352 $active_page = 'db_export.php';
353 require('./db_export.php');
357 $backup_cfgServer = $cfg['Server'];
358 require_once('./libraries/header.inc.php');
359 $cfg['Server'] = $backup_cfgServer;
360 unset($backup_cfgServer);
361 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
362 //echo ' <pre>' . "\n";
363 echo ' <form name="nofunction">' . "\n"
364 // remove auto-select for now: there is no way to select
365 // only a part of the text; anyway, it should obey
366 // $cfg['TextareaAutoSelect']
367 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
368 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
372 // Fake loop just to allow skip of remain of this code by break, I'd really
373 // need exceptions here :-)
376 // Add possibly some comments to export
377 if (!PMA_exportHeader()) {
381 // Will we need relation & co. setup?
382 $do_relation = isset($GLOBALS[$what . '_relation']);
383 $do_comments = isset($GLOBALS[$what . '_comments']);
384 $do_mime = isset($GLOBALS[$what . '_mime']);
385 if ($do_relation ||
$do_comments ||
$do_mime) {
386 require_once('./libraries/relation.lib.php');
387 $cfgRelation = PMA_getRelationsParam();
390 require_once('./libraries/transformations.lib.php');
393 // Include dates in export?
394 $do_dates = isset($GLOBALS[$what . '_dates']);
399 // Gets the number of tables if a dump of a database has been required
400 if ($export_type == 'server') {
401 if (isset($db_select)) {
402 $tmp_select = implode($db_select, '|');
403 $tmp_select = '|' . $tmp_select . '|';
405 // Walk over databases
406 foreach ($GLOBALS['PMA_List_Database']->items
as $current_db) {
407 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
408 ||
!isset($tmp_select)) {
409 if (!PMA_exportDBHeader($current_db)) {
412 if (!PMA_exportDBCreate($current_db)) {
415 $tables = PMA_DBI_get_tables($current_db);
417 foreach ($tables as $table) {
418 // if this is a view, collect it for later; views must be exported
420 if (PMA_Table
::isView($current_db, $table)) {
424 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
425 if (isset($GLOBALS[$what . '_structure'])) {
426 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
430 if (isset($GLOBALS[$what . '_data'])) {
431 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
436 foreach($views as $view) {
437 // no data export for a view
438 if (isset($GLOBALS[$what . '_structure'])) {
439 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
444 if (!PMA_exportDBFooter($current_db)) {
449 } elseif ($export_type == 'database') {
450 if (!PMA_exportDBHeader($db)) {
455 // $tables contains the choices from the user (via $table_select)
456 foreach ($tables as $table) {
457 // if this is a view, collect it for later; views must be exported after
459 if (PMA_Table
::isView($db, $table)) {
463 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
464 //if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
465 //|| !isset($tmp_select)) {
467 if (isset($GLOBALS[$what . '_structure'])) {
468 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
472 if (isset($GLOBALS[$what . '_data'])) {
473 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
479 foreach ($views as $view) {
480 // no data export for a view
481 if (isset($GLOBALS[$what . '_structure'])) {
482 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
488 if (!PMA_exportDBFooter($db)) {
492 if (!PMA_exportDBHeader($db)) {
495 // We export just one table
497 if ($limit_to > 0 && $limit_from >= 0) {
498 $add_query = ' LIMIT '
499 . (($limit_from > 0) ?
$limit_from . ', ' : '')
505 if (!empty($sql_query)) {
506 // only preg_replace if needed
507 if (!empty($add_query)) {
508 // remove trailing semicolon before adding a LIMIT
509 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
511 $local_query = $sql_query . $add_query;
512 PMA_DBI_select_db($db);
514 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
517 if (isset($GLOBALS[$what . '_structure'])) {
518 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
522 // I think we have to export data for a single view; for example PDF report
523 //if (isset($GLOBALS[$what . '_data']) && ! PMA_table::isView($db, $table)) {
524 if (isset($GLOBALS[$what . '_data'])) {
525 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
529 if (!PMA_exportDBFooter($db)) {
533 if (!PMA_exportFooter()) {
540 if ($save_on_server && isset($message)) {
541 $js_to_run = 'functions.js';
542 require_once('./libraries/header.inc.php');
543 if ($export_type == 'server') {
544 $active_page = 'server_export.php';
545 require('./server_export.php');
546 } elseif ($export_type == 'database') {
547 $active_page = 'db_export.php';
548 require('./db_export.php');
550 $active_page = 'tbl_export.php';
551 require('./tbl_export.php');
557 * Send the dump as a file...
559 if (!empty($asfile)) {
560 // Convert the charset if required.
561 if ($output_charset_conversion) {
562 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
565 // Do the compression
566 // 1. as a gzipped file
567 if (isset($compression) && $compression == 'zip') {
568 if (@function_exists
('gzcompress')) {
569 $zipfile = new zipfile();
570 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
571 $dump_buffer = $zipfile -> file();
574 // 2. as a bzipped file
575 elseif (isset($compression) && $compression == 'bzip') {
576 if (@function_exists
('bzcompress')) {
577 $dump_buffer = bzcompress($dump_buffer);
578 if ($dump_buffer === -8) {
579 require_once('./libraries/header.inc.php');
580 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
581 require_once('./libraries/footer.inc.php');
585 // 3. as a gzipped file
586 elseif (isset($compression) && $compression == 'gzip') {
587 if (@function_exists
('gzencode')) {
588 // without the optional parameter level because it bug
589 $dump_buffer = gzencode($dump_buffer);
593 /* If ve saved on server, we have to close file now */
594 if ($save_on_server) {
595 $write_result = @fwrite
($file_handle, $dump_buffer);
596 fclose($file_handle);
597 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
598 $message = sprintf($strNoSpace, htmlspecialchars($save_filename));
600 $message = sprintf($strDumpSaved, htmlspecialchars($save_filename));
603 $js_to_run = 'functions.js';
604 require_once('./libraries/header.inc.php');
605 if ($export_type == 'server') {
606 $active_page = 'server_export.php';
607 require_once('./server_export.php');
608 } elseif ($export_type == 'database') {
609 $active_page = 'db_export.php';
610 require_once('./db_export.php');
612 $active_page = 'tbl_export.php';
613 require_once('./tbl_export.php');
621 * Displays the dump...
625 * Close the html tags and add the footers in dump is displayed on screen
627 //echo ' </pre>' . "\n";
628 echo '</textarea>' . "\n"
630 echo '</div>' . "\n";
633 <script type
="text/javascript" language
="javascript">
635 var bodyWidth
=null; var bodyHeight
=null;
636 if (document
.getElementById('textSQLDUMP')) {
637 bodyWidth
= self
.innerWidth
;
638 bodyHeight
= self
.innerHeight
;
639 if (!bodyWidth
&& !bodyHeight
) {
640 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
641 bodyWidth
= document
.body
.clientWidth
;
642 bodyHeight
= document
.body
.clientHeight
;
643 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
644 bodyWidth
= document
.documentElement
.clientWidth
;
645 bodyHeight
= document
.documentElement
.clientHeight
;
648 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
649 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
654 require_once('./libraries/footer.inc.php');