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');
11 PMA_checkParameters(array('what'));
13 // What type of export are we doing?
14 if ($what == 'excel') {
20 // Get the functions specific to the export type
21 require('./libraries/export/' . PMA_securePath($type) . '.php');
24 if ($export_type == 'server') {
25 $err_url = 'server_export.php?' . PMA_generate_common_url();
26 } elseif ($export_type == 'database') {
27 $err_url = 'db_details_export.php?' . PMA_generate_common_url($db);
29 $err_url = 'tbl_properties_export.php?' . PMA_generate_common_url($db, $table);
33 * Increase time limit for script execution and initializes some variables
35 @set_time_limit
($cfg['ExecTimeLimit']);
37 // Start with empty buffer
41 // We send fake headers to avoid browser timeout when buffering
46 * Output handler for all exports, if needed buffering, it stores data into
47 * $dump_buffer, otherwise it prints thems out.
49 * @param string the insert statement
51 * @return bool Whether output suceeded
53 function PMA_exportOutputHandler($line)
55 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
57 // Kanji encoding convert feature
58 if ($GLOBALS['output_kanji_conversion']) {
59 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ?
$GLOBALS['xkana'] : '');
61 // If we have to buffer data, we will perform everything at once at the end
62 if ($GLOBALS['buffer_needed']) {
64 $dump_buffer .= $line;
65 if ($GLOBALS['onfly_compression']) {
67 $dump_buffer_len +
= strlen($line);
69 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
70 if ($GLOBALS['output_charset_conversion']) {
71 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
74 if ($GLOBALS['compression'] == 'bzip' && @function_exists
('bzcompress')) {
75 $dump_buffer = bzcompress($dump_buffer);
78 else if ($GLOBALS['compression'] == 'gzip' && @function_exists
('gzencode')) {
79 // without the optional parameter level because it bug
80 $dump_buffer = gzencode($dump_buffer);
82 if ($GLOBALS['save_on_server']) {
83 $write_result = @fwrite
($GLOBALS['file_handle'], $dump_buffer);
84 if (!$write_result ||
($write_result != strlen($dump_buffer))) {
85 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
86 $GLOBALS['show_error_header'] = TRUE;
97 if ($time_start >= $time_now +
30) {
98 $time_start = $time_now;
99 header('X-pmaPing: Pong');
103 if ($GLOBALS['asfile']) {
104 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
105 $write_result = @fwrite
($GLOBALS['file_handle'], $line);
106 if (!$write_result ||
($write_result != strlen($line))) {
107 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
108 $GLOBALS['show_error_header'] = TRUE;
112 if ($time_start >= $time_now +
30) {
113 $time_start = $time_now;
114 header('X-pmaPing: Pong');
117 // We export as file - output normally
118 if ($GLOBALS['output_charset_conversion']) {
119 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
124 // We export as html - replace special chars
125 echo htmlspecialchars($line);
129 } // end of the 'PMA_exportOutputHandler()' function
131 // Will we save dump on server?
132 $save_on_server = isset($cfg['SaveDir']) && !empty($cfg['SaveDir']) && !empty($onserver);
134 // Ensure compressed formats are associated with the download feature
135 if (empty($asfile)) {
136 if ($save_on_server) {
138 } elseif (isset($compression) && ($compression == 'zip' |
$compression == 'gzip' |
$compression == 'bzip')) {
147 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
148 if ($what == 'sql') {
151 $crlf = PMA_whichCrlf();
154 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
156 // Do we need to convert charset?
157 $output_charset_conversion = $asfile &&
158 $cfg['AllowAnywhereRecoding'] && $allow_recoding
159 && isset($charset_of_file) && $charset_of_file != $charset
162 // Set whether we will need buffering
163 $buffer_needed = isset($compression) && ($compression == 'zip' |
$compression == 'gzip' |
$compression == 'bzip');
165 // Use on fly compression?
166 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && isset($compression) && ($compression == 'gzip' |
$compression == 'bzip');
167 if ($onfly_compression) {
168 $memory_limit = trim(@ini_get
('memory_limit'));
170 if (empty($memory_limit)) $memory_limit = 2 * 1024 * 1024;
172 if (strtolower(substr($memory_limit, -1)) == 'm') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
173 elseif (strtolower(substr($memory_limit, -1)) == 'k') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
174 elseif (strtolower(substr($memory_limit, -1)) == 'g') $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
175 else $memory_limit = (int)$memory_limit;
177 // Some of memory is needed for other thins and as treshold.
178 // Nijel: During export I had allocated (see memory_get_usage function)
179 // approx 1.2MB so this comes from that.
180 if ($memory_limit > 1500000) $memory_limit -= 1500000;
182 // Some memory is needed for compression, assume 1/3
183 $memory_limit *= 2/3;
186 // Generate filename and mime type if needed
188 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
189 if ($export_type == 'server') {
190 if (isset($remember_template)) {
191 setcookie('pma_server_filename_template', $filename_template , 0, $GLOBALS['cookie_path'], '' , $GLOBALS['is_https']);
193 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template));
194 } elseif ($export_type == 'database') {
195 if (isset($remember_template)) {
196 setcookie('pma_db_filename_template', $filename_template , 0, $GLOBALS['cookie_path'], '' , $GLOBALS['is_https']);
198 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)));
200 if (isset($remember_template)) {
201 setcookie('pma_table_filename_template', $filename_template , 0, $GLOBALS['cookie_path'], '' , $GLOBALS['is_https']);
203 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))));
206 // convert filename to iso-8859-1, it is safer
207 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
208 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
210 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
213 // Generate basic dump extension
214 if ($type == 'csv') {
216 $mime_type = 'text/comma-separated-values';
217 } else if ($type == 'htmlexcel') {
219 $mime_type = 'application/vnd.ms-excel';
220 } else if ($type == 'htmlword') {
222 $mime_type = 'application/vnd.ms-word';
223 } else if ($type == 'xls') {
225 $mime_type = 'application/vnd.ms-excel';
226 } else if ($type == 'xml') {
228 $mime_type = 'text/xml';
229 } else if ($type == 'latex') {
231 $mime_type = 'application/x-tex';
234 // text/x-sql is correct MIME type, however safari ignores further
235 // Content-Disposition header, so we must force it to download it this
237 $mime_type = PMA_USR_BROWSER_AGENT
== 'SAFARI'
238 ?
'application/octet-stream'
242 // If dump is going to be compressed, set correct encoding or mime_type and add
243 // compression to extension
244 $content_encoding = '';
245 if (isset($compression) && $compression == 'bzip') {
247 // browsers don't like this:
248 //$content_encoding = 'x-bzip2';
249 $mime_type = 'application/x-bzip2';
250 } else if (isset($compression) && $compression == 'gzip') {
252 // Needed to avoid recompression by server modules like mod_gzip.
253 // It seems necessary to check about zlib.output_compression
254 // to avoid compressing twice
255 if (!@ini_get
('zlib.output_compression')) {
256 $content_encoding = 'x-gzip';
257 $mime_type = 'application/x-gzip';
259 } else if (isset($compression) && $compression == 'zip') {
261 $mime_type = 'application/zip';
265 // Open file on server if needed
266 if ($save_on_server) {
267 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@','_',$filename);
269 if (file_exists($save_filename) && empty($onserverover)) {
270 $message = sprintf($strFileAlreadyExists, htmlspecialchars($save_filename));
271 $GLOBALS['show_error_header'] = TRUE;
273 if (is_file($save_filename) && !is_writable($save_filename)) {
274 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
275 $GLOBALS['show_error_header'] = TRUE;
277 if (!$file_handle = @fopen
($save_filename, 'w')) {
278 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
279 $GLOBALS['show_error_header'] = TRUE;
283 if (isset($message)) {
284 $js_to_run = 'functions.js';
285 require_once('./header.inc.php');
286 if ($export_type == 'server') {
287 $active_page = 'server_export.php';
288 require('./server_export.php');
289 } elseif ($export_type == 'database') {
290 $active_page = 'db_details_export.php';
291 require('./db_details_export.php');
293 $active_page = 'tbl_properties_export.php';
294 require('./tbl_properties_export.php');
301 * Send headers depending on whether the user chose to download a dump file
304 if (!$save_on_server) {
307 if (!empty($content_encoding)) {
308 header('Content-Encoding: ' . $content_encoding);
310 header('Content-Type: ' . $mime_type);
311 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
312 // lem9: Tested behavior of
314 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
318 header('Content-Disposition: attachment; filename="' . $filename . '"');
319 if (PMA_USR_BROWSER_AGENT
== 'IE') {
320 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
321 header('Pragma: public');
323 header('Pragma: no-cache');
327 $backup_cfgServer = $cfg['Server'];
328 require_once('./header.inc.php');
329 $cfg['Server'] = $backup_cfgServer;
330 unset($backup_cfgServer);
331 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
332 //echo ' <pre>' . "\n";
333 echo ' <form name="nofunction">' . "\n"
334 // remove auto-select for now: there is no way to select
335 // only a part of the text; anyway, it should obey
336 // $cfg['TextareaAutoSelect']
337 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
338 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
342 // Check if we have something to export
343 if ($export_type == 'database') {
344 $tables = PMA_DBI_get_tables($db);
345 $num_tables = count($tables);
346 if ($num_tables == 0) {
347 $message = $strNoTablesFound;
348 $js_to_run = 'functions.js';
349 require_once('./header.inc.php');
350 if ($export_type == 'server') {
351 $active_page = 'server_export.php';
352 require('./server_export.php');
353 } elseif ($export_type == 'database') {
354 $active_page = 'db_details_export.php';
355 require('./db_details_export.php');
357 $active_page = 'tbl_properties_export.php';
358 require('./tbl_properties_export.php');
364 // Fake loop just to allow skip of remain of this code by break, I'd really
365 // need exceptions here :-)
368 // Add possibly some comments to export
369 if (!PMA_exportHeader()) break;
371 // Will we need relation & co. setup?
372 $do_relation = isset($GLOBALS[$what . '_relation']);
373 $do_comments = isset($GLOBALS[$what . '_comments']);
374 $do_mime = isset($GLOBALS[$what . '_mime']);
375 if ($do_relation ||
$do_comments ||
$do_mime) {
376 require_once('./libraries/relation.lib.php');
377 $cfgRelation = PMA_getRelationsParam();
380 require_once('./libraries/transformations.lib.php');
383 // Include dates in export?
384 $do_dates = isset($GLOBALS[$what . '_dates']);
389 // Gets the number of tables if a dump of a database has been required
390 if ($export_type == 'server') {
392 * Gets the databases list - if it has not been built yet
394 if ($server > 0 && empty($dblist)) {
395 PMA_availableDatabases();
398 if (isset($db_select)) {
399 $tmp_select = implode($db_select, '|');
400 $tmp_select = '|' . $tmp_select . '|';
402 // Walk over databases
403 foreach ($dblist AS $current_db) {
404 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
405 ||
!isset($tmp_select)) {
406 if (!PMA_exportDBHeader($current_db))
408 if (!PMA_exportDBCreate($current_db))
410 $tables = PMA_DBI_get_tables($current_db);
411 foreach ($tables as $table) {
412 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
413 if (isset($GLOBALS[$what . '_structure'])) {
414 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates))
417 if (isset($GLOBALS[$what . '_data'])) {
418 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query))
422 if (!PMA_exportDBFooter($current_db))
426 } elseif ($export_type == 'database') {
427 if (!PMA_exportDBHeader($db))
430 if (isset($table_select)) {
431 $tmp_select = implode($table_select, '|');
432 $tmp_select = '|' . $tmp_select . '|';
435 foreach ($tables as $table) {
436 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
437 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
438 ||
!isset($tmp_select)) {
440 if (isset($GLOBALS[$what . '_structure'])) {
441 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates))
444 if (isset($GLOBALS[$what . '_data'])) {
445 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query))
450 if (!PMA_exportDBFooter($db))
453 if (!PMA_exportDBHeader($db))
455 // We export just one table
457 if ($limit_to > 0 && $limit_from >= 0) {
458 $add_query = ' LIMIT '
459 . (($limit_from > 0) ?
$limit_from . ', ' : '')
465 if (!empty($sql_query)) {
466 $local_query = $sql_query . $add_query;
467 PMA_DBI_select_db($db);
469 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
472 if (isset($GLOBALS[$what . '_structure'])) {
473 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates))
476 if (isset($GLOBALS[$what . '_data'])) {
477 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query))
480 if (!PMA_exportDBFooter($db))
483 if (!PMA_exportFooter()) break;
488 if ($save_on_server && isset($message)) {
489 $js_to_run = 'functions.js';
490 require_once('./header.inc.php');
491 if ($export_type == 'server') {
492 $active_page = 'server_export.php';
493 require('./server_export.php');
494 } elseif ($export_type == 'database') {
495 $active_page = 'db_details_export.php';
496 require('./db_details_export.php');
498 $active_page = 'tbl_properties_export.php';
499 require('./tbl_properties_export.php');
505 * Send the dump as a file...
507 if (!empty($asfile)) {
508 // Convert the charset if required.
509 if ($output_charset_conversion) {
510 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
513 // Do the compression
514 // 1. as a gzipped file
515 if (isset($compression) && $compression == 'zip') {
516 if (@function_exists
('gzcompress')) {
517 $zipfile = new zipfile();
518 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
519 $dump_buffer = $zipfile -> file();
522 // 2. as a bzipped file
523 else if (isset($compression) && $compression == 'bzip') {
524 if (@function_exists
('bzcompress')) {
525 $dump_buffer = bzcompress($dump_buffer);
526 if ($dump_buffer === -8) {
527 require_once('./header.inc.php');
528 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
529 require_once('./footer.inc.php');
533 // 3. as a gzipped file
534 else if (isset($compression) && $compression == 'gzip') {
535 if (@function_exists
('gzencode')) {
536 // without the optional parameter level because it bug
537 $dump_buffer = gzencode($dump_buffer);
541 /* If ve saved on server, we have to close file now */
542 if ($save_on_server) {
543 $write_result = @fwrite
($file_handle, $dump_buffer);
544 fclose($file_handle);
545 if (strlen($dump_buffer) !=0 && (!$write_result ||
($write_result != strlen($dump_buffer)))) {
546 $message = sprintf($strNoSpace, htmlspecialchars($save_filename));
548 $message = sprintf($strDumpSaved, htmlspecialchars($save_filename));
551 $js_to_run = 'functions.js';
552 require_once('./header.inc.php');
553 if ($export_type == 'server') {
554 $active_page = 'server_export.php';
555 require_once('./server_export.php');
556 } elseif ($export_type == 'database') {
557 $active_page = 'db_details_export.php';
558 require_once('./db_details_export.php');
560 $active_page = 'tbl_properties_export.php';
561 require_once('./tbl_properties_export.php');
569 * Displays the dump...
573 * Close the html tags and add the footers in dump is displayed on screen
575 //echo ' </pre>' . "\n";
576 echo '</textarea>' . "\n"
578 echo '</div>' . "\n";
580 ?
><script language
="JavaScript" type
="text/javascript">
582 var bodyWidth
=null; var bodyHeight
=null;
583 if (document
.getElementById('textSQLDUMP')) {
584 bodyWidth
= self
.innerWidth
;
585 bodyHeight
= self
.innerHeight
;
586 if(!bodyWidth
&& !bodyHeight
){
587 if (document
.compatMode
&& document
.compatMode
== "BackCompat") {
588 bodyWidth
= document
.body
.clientWidth
;
589 bodyHeight
= document
.body
.clientHeight
;
590 } else if (document
.compatMode
&& document
.compatMode
== "CSS1Compat") {
591 bodyWidth
= document
.documentElement
.clientWidth
;
592 bodyHeight
= document
.documentElement
.clientHeight
;
595 document
.getElementById('textSQLDUMP').style
.width
=(bodyWidth
-50) +
'px';
596 document
.getElementById('textSQLDUMP').style
.height
=(bodyHeight
-100) +
'px';
601 require_once('./footer.inc.php');