missing texts for export
[phpmyadmin/last10db.git] / export.php
blobfacdb11ea3c85bc713e4c451c483347ef7a289ea
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Get the variables sent or posted to this script and a core script
7 */
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'));
14 // Scan plugins
15 $export_list = PMA_getPlugins('./libraries/export/', array('export_type' => $export_type, 'single_table' => isset($single_table)));
17 // Backward compatbility
18 $type = $what;
20 // Check export type
21 if (!isset($export_list[$type])) {
22 die('Bad 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_details_export.php';
36 require('./db_details_export.php');
37 } else {
38 $active_page = 'tbl_properties_export.php';
39 require('./tbl_properties_export.php');
41 exit();
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_details_export.php?' . PMA_generate_common_url($db);
49 } elseif ($export_type == 'table' && isset($db) && strlen($db) && isset($table) && strlen($table)) {
50 $err_url = 'tbl_properties_export.php?' . PMA_generate_common_url($db, $table);
51 } else {
52 die('Bad parameters!');
55 // Get the functions specific to the export type
56 require('./libraries/export/' . PMA_securePath($type) . '.php');
58 /**
59 * Increase time limit for script execution and initializes some variables
61 @set_time_limit($cfg['ExecTimeLimit']);
62 if (!empty($cfg['MemoryLimit'])) {
63 @ini_set('memory_limit', $cfg['MemoryLimit']);
66 // Start with empty buffer
67 $dump_buffer = '';
68 $dump_buffer_len = 0;
70 // We send fake headers to avoid browser timeout when buffering
71 $time_start = time();
74 /**
75 * Output handler for all exports, if needed buffering, it stores data into
76 * $dump_buffer, otherwise it prints thems out.
78 * @param string the insert statement
80 * @return bool Whether output suceeded
82 function PMA_exportOutputHandler($line)
84 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
86 // Kanji encoding convert feature
87 if ($GLOBALS['output_kanji_conversion']) {
88 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
90 // If we have to buffer data, we will perform everything at once at the end
91 if ($GLOBALS['buffer_needed']) {
93 $dump_buffer .= $line;
94 if ($GLOBALS['onfly_compression']) {
96 $dump_buffer_len += strlen($line);
98 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
99 if ($GLOBALS['output_charset_conversion']) {
100 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
102 // as bzipped
103 if ($GLOBALS['compression'] == 'bzip' && @function_exists('bzcompress')) {
104 $dump_buffer = bzcompress($dump_buffer);
106 // as a gzipped file
107 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists('gzencode')) {
108 // without the optional parameter level because it bug
109 $dump_buffer = gzencode($dump_buffer);
111 if ($GLOBALS['save_on_server']) {
112 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
113 if (!$write_result || ($write_result != strlen($dump_buffer))) {
114 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
115 $GLOBALS['show_error_header'] = TRUE;
116 return FALSE;
118 } else {
119 echo $dump_buffer;
121 $dump_buffer = '';
122 $dump_buffer_len = 0;
124 } else {
125 $time_now = time();
126 if ($time_start >= $time_now + 30) {
127 $time_start = $time_now;
128 header('X-pmaPing: Pong');
129 } // end if
131 } else {
132 if ($GLOBALS['asfile']) {
133 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
134 $write_result = @fwrite($GLOBALS['file_handle'], $line);
135 if (!$write_result || ($write_result != strlen($line))) {
136 $GLOBALS['message'] = sprintf($GLOBALS['strNoSpace'], htmlspecialchars($save_filename));
137 $GLOBALS['show_error_header'] = TRUE;
138 return FALSE;
140 $time_now = time();
141 if ($time_start >= $time_now + 30) {
142 $time_start = $time_now;
143 header('X-pmaPing: Pong');
144 } // end if
145 } else {
146 // We export as file - output normally
147 if ($GLOBALS['output_charset_conversion']) {
148 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
150 echo $line;
152 } else {
153 // We export as html - replace special chars
154 echo htmlspecialchars($line);
157 return TRUE;
158 } // end of the 'PMA_exportOutputHandler()' function
160 // Will we save dump on server?
161 $save_on_server = isset($cfg['SaveDir']) && !empty($cfg['SaveDir']) && !empty($onserver);
163 // Ensure compressed formats are associated with the download feature
164 if (empty($asfile)) {
165 if ($save_on_server) {
166 $asfile = TRUE;
167 } elseif (isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip')) {
168 $asfile = TRUE;
169 } else {
170 $asfile = FALSE;
172 } else {
173 $asfile = TRUE;
176 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
177 if ($what == 'sql') {
178 $crlf = "\n";
179 } else {
180 $crlf = PMA_whichCrlf();
183 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
185 // Do we need to convert charset?
186 $output_charset_conversion = $asfile &&
187 $cfg['AllowAnywhereRecoding'] && $allow_recoding
188 && isset($charset_of_file) && $charset_of_file != $charset
189 && $type != 'xls';
191 // Set whether we will need buffering
192 $buffer_needed = isset($compression) && ($compression == 'zip' | $compression == 'gzip' | $compression == 'bzip');
194 // Use on fly compression?
195 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && isset($compression) && ($compression == 'gzip' | $compression == 'bzip');
196 if ($onfly_compression) {
197 $memory_limit = trim(@ini_get('memory_limit'));
198 // 2 MB as default
199 if (empty($memory_limit)) {
200 $memory_limit = 2 * 1024 * 1024;
203 if (strtolower(substr($memory_limit, -1)) == 'm') {
204 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
205 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
206 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
207 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
208 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
209 } else {
210 $memory_limit = (int)$memory_limit;
213 // Some of memory is needed for other thins and as treshold.
214 // Nijel: During export I had allocated (see memory_get_usage function)
215 // approx 1.2MB so this comes from that.
216 if ($memory_limit > 1500000) {
217 $memory_limit -= 1500000;
220 // Some memory is needed for compression, assume 1/3
221 $memory_limit *= 2/3;
224 // Generate filename and mime type if needed
225 if ($asfile) {
226 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
227 if ($export_type == 'server') {
228 if (isset($remember_template)) {
229 setcookie('pma_server_filename_template', $filename_template, 0, $GLOBALS['cookie_path'], '', $GLOBALS['is_https']);
231 $filename = str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template));
232 } elseif ($export_type == 'database') {
233 if (isset($remember_template)) {
234 setcookie('pma_db_filename_template', $filename_template, 0, $GLOBALS['cookie_path'], '', $GLOBALS['is_https']);
236 $filename = str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template)));
237 } else {
238 if (isset($remember_template)) {
239 setcookie('pma_table_filename_template', $filename_template, 0, $GLOBALS['cookie_path'], '', $GLOBALS['is_https']);
241 $filename = str_replace('__TABLE__', $table, str_replace('__DB__', $db, str_replace('__SERVER__', $GLOBALS['cfg']['Server']['host'], strftime($filename_template))));
244 // convert filename to iso-8859-1, it is safer
245 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
246 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
247 } else {
248 $filename = PMA_convert_string($convcharset, 'iso-8859-1', $filename);
251 // Grab basic dump extension and mime type
252 $filename .= '.' . $export_list[$type]['extension'];
253 $mime_type = $export_list[$type]['mime_type'];
255 // If dump is going to be compressed, set correct encoding or mime_type and add
256 // compression to extension
257 $content_encoding = '';
258 if (isset($compression) && $compression == 'bzip') {
259 $filename .= '.bz2';
260 // browsers don't like this:
261 //$content_encoding = 'x-bzip2';
262 $mime_type = 'application/x-bzip2';
263 } elseif (isset($compression) && $compression == 'gzip') {
264 $filename .= '.gz';
265 // Needed to avoid recompression by server modules like mod_gzip.
266 // It seems necessary to check about zlib.output_compression
267 // to avoid compressing twice
268 if (!@ini_get('zlib.output_compression')) {
269 $content_encoding = 'x-gzip';
270 $mime_type = 'application/x-gzip';
272 } elseif (isset($compression) && $compression == 'zip') {
273 $filename .= '.zip';
274 $mime_type = 'application/zip';
278 // Open file on server if needed
279 if ($save_on_server) {
280 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
281 unset($message);
282 if (file_exists($save_filename) && empty($onserverover)) {
283 $message = sprintf($strFileAlreadyExists, htmlspecialchars($save_filename));
284 $GLOBALS['show_error_header'] = TRUE;
285 } else {
286 if (is_file($save_filename) && !is_writable($save_filename)) {
287 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
288 $GLOBALS['show_error_header'] = TRUE;
289 } else {
290 if (!$file_handle = @fopen($save_filename, 'w')) {
291 $message = sprintf($strNoPermission, htmlspecialchars($save_filename));
292 $GLOBALS['show_error_header'] = TRUE;
296 if (isset($message)) {
297 $js_to_run = 'functions.js';
298 require_once('./libraries/header.inc.php');
299 if ($export_type == 'server') {
300 $active_page = 'server_export.php';
301 require('./server_export.php');
302 } elseif ($export_type == 'database') {
303 $active_page = 'db_details_export.php';
304 require('./db_details_export.php');
305 } else {
306 $active_page = 'tbl_properties_export.php';
307 require('./tbl_properties_export.php');
309 exit();
314 * Send headers depending on whether the user chose to download a dump file
315 * or not
317 if (!$save_on_server) {
318 if ($asfile ) {
319 // Download
320 if (!empty($content_encoding)) {
321 header('Content-Encoding: ' . $content_encoding);
323 header('Content-Type: ' . $mime_type);
324 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
325 // lem9: Tested behavior of
326 // IE 5.50.4807.2300
327 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
328 // IE 6.0.2900.2180
329 // Firefox 1.0.6
330 // in http and https
331 header('Content-Disposition: attachment; filename="' . $filename . '"');
332 if (PMA_USR_BROWSER_AGENT == 'IE') {
333 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
334 header('Pragma: public');
335 } else {
336 header('Pragma: no-cache');
338 } else {
339 // HTML
340 $backup_cfgServer = $cfg['Server'];
341 require_once('./libraries/header.inc.php');
342 $cfg['Server'] = $backup_cfgServer;
343 unset($backup_cfgServer);
344 echo "\n" . '<div align="' . $cell_align_left . '">' . "\n";
345 //echo ' <pre>' . "\n";
346 echo ' <form name="nofunction">' . "\n"
347 // remove auto-select for now: there is no way to select
348 // only a part of the text; anyway, it should obey
349 // $cfg['TextareaAutoSelect']
350 //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
351 . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
352 } // end download
355 // Check if we have something to export
356 if ($export_type == 'database') {
357 $tables = PMA_DBI_get_tables($db);
358 $num_tables = count($tables);
359 if ($num_tables == 0) {
360 $message = $strNoTablesFound;
361 $js_to_run = 'functions.js';
362 require_once('./libraries/header.inc.php');
363 if ($export_type == 'server') {
364 $active_page = 'server_export.php';
365 require('./server_export.php');
366 } elseif ($export_type == 'database') {
367 $active_page = 'db_details_export.php';
368 require('./db_details_export.php');
369 } else {
370 $active_page = 'tbl_properties_export.php';
371 require('./tbl_properties_export.php');
373 exit();
377 // Fake loop just to allow skip of remain of this code by break, I'd really
378 // need exceptions here :-)
379 do {
381 // Add possibly some comments to export
382 if (!PMA_exportHeader()) {
383 break;
386 // Will we need relation & co. setup?
387 $do_relation = isset($GLOBALS[$what . '_relation']);
388 $do_comments = isset($GLOBALS[$what . '_comments']);
389 $do_mime = isset($GLOBALS[$what . '_mime']);
390 if ($do_relation || $do_comments || $do_mime) {
391 require_once('./libraries/relation.lib.php');
392 $cfgRelation = PMA_getRelationsParam();
394 if ($do_mime) {
395 require_once('./libraries/transformations.lib.php');
398 // Include dates in export?
399 $do_dates = isset($GLOBALS[$what . '_dates']);
402 * Builds the dump
404 // Gets the number of tables if a dump of a database has been required
405 if ($export_type == 'server') {
407 * Gets the databases list - if it has not been built yet
409 if ($server > 0 && empty($dblist)) {
410 PMA_availableDatabases();
413 if (isset($db_select)) {
414 $tmp_select = implode($db_select, '|');
415 $tmp_select = '|' . $tmp_select . '|';
417 // Walk over databases
418 foreach ($dblist AS $current_db) {
419 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
420 || !isset($tmp_select)) {
421 if (!PMA_exportDBHeader($current_db)) {
422 break 2;
424 if (!PMA_exportDBCreate($current_db)) {
425 break 2;
427 $tables = PMA_DBI_get_tables($current_db);
428 $views = array();
429 foreach ($tables as $table) {
430 // if this is a view, collect it for later; views must be exported
431 // after the tables
432 if (PMA_Table::isView($current_db, $table)) {
433 $views[] = $table;
434 continue;
436 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
437 if (isset($GLOBALS[$what . '_structure'])) {
438 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
439 break 3;
442 if (isset($GLOBALS[$what . '_data'])) {
443 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
444 break 3;
448 foreach($views as $view) {
449 // no data export for a view
450 if (isset($GLOBALS[$what . '_structure'])) {
451 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
452 break 3;
456 if (!PMA_exportDBFooter($current_db)) {
457 break 2;
461 } elseif ($export_type == 'database') {
462 if (!PMA_exportDBHeader($db)) {
463 break;
466 if (isset($table_select)) {
467 $tmp_select = implode($table_select, '|');
468 $tmp_select = '|' . $tmp_select . '|';
470 $i = 0;
471 $views = array();
472 foreach ($tables as $table) {
473 // if this is a view, collect it for later; views must be exported after
474 // the tables
475 if (PMA_Table::isView($db, $table)) {
476 $views[] = $table;
477 continue;
479 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
480 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $table . '|'))
481 || !isset($tmp_select)) {
483 if (isset($GLOBALS[$what . '_structure'])) {
484 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
485 break 2;
488 if (isset($GLOBALS[$what . '_data'])) {
489 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
490 break 2;
495 foreach ($views as $view) {
496 // no data export for a view
497 if (isset($GLOBALS[$what . '_structure'])) {
498 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
499 break 2;
504 if (!PMA_exportDBFooter($db)) {
505 break;
507 } else {
508 if (!PMA_exportDBHeader($db)) {
509 break;
511 // We export just one table
513 if ($limit_to > 0 && $limit_from >= 0) {
514 $add_query = ' LIMIT '
515 . (($limit_from > 0) ? $limit_from . ', ' : '')
516 . $limit_to;
517 } else {
518 $add_query = '';
521 if (!empty($sql_query)) {
522 // only preg_replace if needed
523 if (!empty($add_query)) {
524 // remove trailing semicolon before adding a LIMIT
525 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
527 $local_query = $sql_query . $add_query;
528 PMA_DBI_select_db($db);
529 } else {
530 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
533 if (isset($GLOBALS[$what . '_structure'])) {
534 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates)) {
535 break;
538 // I think we have to export data for a single view; for example PDF report
539 //if (isset($GLOBALS[$what . '_data']) && ! PMA_table::isView($db, $table)) {
540 if (isset($GLOBALS[$what . '_data'])) {
541 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
542 break;
545 if (!PMA_exportDBFooter($db)) {
546 break;
549 if (!PMA_exportFooter()) {
550 break;
553 } while (FALSE);
554 // End of fake loop
556 if ($save_on_server && isset($message)) {
557 $js_to_run = 'functions.js';
558 require_once('./libraries/header.inc.php');
559 if ($export_type == 'server') {
560 $active_page = 'server_export.php';
561 require('./server_export.php');
562 } elseif ($export_type == 'database') {
563 $active_page = 'db_details_export.php';
564 require('./db_details_export.php');
565 } else {
566 $active_page = 'tbl_properties_export.php';
567 require('./tbl_properties_export.php');
569 exit();
573 * Send the dump as a file...
575 if (!empty($asfile)) {
576 // Convert the charset if required.
577 if ($output_charset_conversion) {
578 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
581 // Do the compression
582 // 1. as a gzipped file
583 if (isset($compression) && $compression == 'zip') {
584 if (@function_exists('gzcompress')) {
585 $zipfile = new zipfile();
586 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
587 $dump_buffer = $zipfile -> file();
590 // 2. as a bzipped file
591 elseif (isset($compression) && $compression == 'bzip') {
592 if (@function_exists('bzcompress')) {
593 $dump_buffer = bzcompress($dump_buffer);
594 if ($dump_buffer === -8) {
595 require_once('./libraries/header.inc.php');
596 echo sprintf($strBzError, '<a href="http://bugs.php.net/bug.php?id=17300" target="_blank">17300</a>');
597 require_once('./libraries/footer.inc.php');
601 // 3. as a gzipped file
602 elseif (isset($compression) && $compression == 'gzip') {
603 if (@function_exists('gzencode')) {
604 // without the optional parameter level because it bug
605 $dump_buffer = gzencode($dump_buffer);
609 /* If ve saved on server, we have to close file now */
610 if ($save_on_server) {
611 $write_result = @fwrite($file_handle, $dump_buffer);
612 fclose($file_handle);
613 if (strlen($dump_buffer) !=0 && (!$write_result || ($write_result != strlen($dump_buffer)))) {
614 $message = sprintf($strNoSpace, htmlspecialchars($save_filename));
615 } else {
616 $message = sprintf($strDumpSaved, htmlspecialchars($save_filename));
619 $js_to_run = 'functions.js';
620 require_once('./libraries/header.inc.php');
621 if ($export_type == 'server') {
622 $active_page = 'server_export.php';
623 require_once('./server_export.php');
624 } elseif ($export_type == 'database') {
625 $active_page = 'db_details_export.php';
626 require_once('./db_details_export.php');
627 } else {
628 $active_page = 'tbl_properties_export.php';
629 require_once('./tbl_properties_export.php');
631 exit();
632 } else {
633 echo $dump_buffer;
637 * Displays the dump...
639 else {
641 * Close the html tags and add the footers in dump is displayed on screen
643 //echo ' </pre>' . "\n";
644 echo '</textarea>' . "\n"
645 . ' </form>' . "\n";
646 echo '</div>' . "\n";
647 echo "\n";
649 <script type="text/javascript" language="javascript">
650 //<![CDATA[
651 var bodyWidth=null; var bodyHeight=null;
652 if (document.getElementById('textSQLDUMP')) {
653 bodyWidth = self.innerWidth;
654 bodyHeight = self.innerHeight;
655 if (!bodyWidth && !bodyHeight) {
656 if (document.compatMode && document.compatMode == "BackCompat") {
657 bodyWidth = document.body.clientWidth;
658 bodyHeight = document.body.clientHeight;
659 } else if (document.compatMode && document.compatMode == "CSS1Compat") {
660 bodyWidth = document.documentElement.clientWidth;
661 bodyHeight = document.documentElement.clientHeight;
664 document.getElementById('textSQLDUMP').style.width=(bodyWidth-50) + 'px';
665 document.getElementById('textSQLDUMP').style.height=(bodyHeight-100) + 'px';
667 //]]>
668 </script>
669 <?php
670 require_once('./libraries/footer.inc.php');
671 } // end if