unused code
[phpmyadmin/crack.git] / export.php
blob00639e22a3b088cc629abd261e0d4b24a319357e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @todo too much die here, or?
5 * @package phpMyAdmin
6 */
8 /**
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'));
17 // Scan plugins
18 $export_list = PMA_getPlugins('./libraries/export/', array('export_type' => $export_type, 'single_table' => isset($single_table)));
20 // Backward compatbility
21 $type = $what;
23 // Check export type
24 if (!isset($export_list[$type])) {
25 die('Bad type!');
28 /**
29 * valid compression methods
31 $compression_methods = array(
32 'zip',
33 'gzip',
34 'bzip',
37 /**
38 * init and variable checking
40 $compression = false;
41 $onserver = false;
42 $save_on_server = false;
43 $buffer_needed = false;
44 if (empty($_REQUEST['asfile'])) {
45 $asfile = false;
46 } else {
47 $asfile = true;
48 if (in_array($_REQUEST['compression'], $compression_methods)) {
49 $compression = $_REQUEST['compression'];
50 $buffer_needed = true;
52 if (!empty($_REQUEST['onserver'])) {
53 $onserver = $_REQUEST['onserver'];
54 // Will we save dump on server?
55 $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
59 // Does export require to be into file?
60 if (isset($export_list[$type]['force_file']) && ! $asfile) {
61 $message = PMA_Message::error(__('Selected export type has to be saved in file!'));
62 require_once './libraries/header.inc.php';
63 if ($export_type == 'server') {
64 $active_page = 'server_export.php';
65 require './server_export.php';
66 } elseif ($export_type == 'database') {
67 $active_page = 'db_export.php';
68 require './db_export.php';
69 } else {
70 $active_page = 'tbl_export.php';
71 require './tbl_export.php';
73 exit();
76 // Generate error url and check for needed variables
77 if ($export_type == 'server') {
78 $err_url = 'server_export.php?' . PMA_generate_common_url();
79 } elseif ($export_type == 'database' && strlen($db)) {
80 $err_url = 'db_export.php?' . PMA_generate_common_url($db);
81 // Check if we have something to export
82 if (isset($table_select)) {
83 $tables = $table_select;
84 } else {
85 $tables = array();
87 } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
88 $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
89 } else {
90 die('Bad parameters!');
93 // Get the functions specific to the export type
94 require './libraries/export/' . PMA_securePath($type) . '.php';
96 /**
97 * Increase time limit for script execution and initializes some variables
99 @set_time_limit($cfg['ExecTimeLimit']);
100 if (!empty($cfg['MemoryLimit'])) {
101 @ini_set('memory_limit', $cfg['MemoryLimit']);
104 // Start with empty buffer
105 $dump_buffer = '';
106 $dump_buffer_len = 0;
108 // We send fake headers to avoid browser timeout when buffering
109 $time_start = time();
113 * Output handler for all exports, if needed buffering, it stores data into
114 * $dump_buffer, otherwise it prints thems out.
116 * @param string the insert statement
118 * @return bool Whether output suceeded
120 function PMA_exportOutputHandler($line)
122 global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
124 // Kanji encoding convert feature
125 if ($GLOBALS['output_kanji_conversion']) {
126 $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : '');
128 // If we have to buffer data, we will perform everything at once at the end
129 if ($GLOBALS['buffer_needed']) {
131 $dump_buffer .= $line;
132 if ($GLOBALS['onfly_compression']) {
134 $dump_buffer_len += strlen($line);
136 if ($dump_buffer_len > $GLOBALS['memory_limit']) {
137 if ($GLOBALS['output_charset_conversion']) {
138 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
140 // as bzipped
141 if ($GLOBALS['compression'] == 'bzip' && @function_exists('bzcompress')) {
142 $dump_buffer = bzcompress($dump_buffer);
144 // as a gzipped file
145 elseif ($GLOBALS['compression'] == 'gzip' && @function_exists('gzencode')) {
146 // without the optional parameter level because it bug
147 $dump_buffer = gzencode($dump_buffer);
149 if ($GLOBALS['save_on_server']) {
150 $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
151 if (!$write_result || ($write_result != strlen($dump_buffer))) {
152 $GLOBALS['message'] = PMA_Message::error(__('Insufficient space to save the file %s.'));
153 $GLOBALS['message']->addParam($save_filename);
154 return false;
156 } else {
157 echo $dump_buffer;
159 $dump_buffer = '';
160 $dump_buffer_len = 0;
162 } else {
163 $time_now = time();
164 if ($time_start >= $time_now + 30) {
165 $time_start = $time_now;
166 header('X-pmaPing: Pong');
167 } // end if
169 } else {
170 if ($GLOBALS['asfile']) {
171 if ($GLOBALS['output_charset_conversion']) {
172 $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line);
174 if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
175 $write_result = @fwrite($GLOBALS['file_handle'], $line);
176 if (!$write_result || ($write_result != strlen($line))) {
177 $GLOBALS['message'] = PMA_Message::error(__('Insufficient space to save the file %s.'));
178 $GLOBALS['message']->addParam($save_filename);
179 return false;
181 $time_now = time();
182 if ($time_start >= $time_now + 30) {
183 $time_start = $time_now;
184 header('X-pmaPing: Pong');
185 } // end if
186 } else {
187 // We export as file - output normally
188 echo $line;
190 } else {
191 // We export as html - replace special chars
192 echo htmlspecialchars($line);
195 return true;
196 } // end of the 'PMA_exportOutputHandler()' function
198 // Defines the default <CR><LF> format. For SQL always use \n as MySQL wants this on all platforms.
199 if ($what == 'sql') {
200 $crlf = "\n";
201 } else {
202 $crlf = PMA_whichCrlf();
205 $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
207 // Do we need to convert charset?
208 $output_charset_conversion = $asfile && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
209 && isset($charset_of_file) && $charset_of_file != $charset
210 && $type != 'xls';
212 // Use on the fly compression?
213 $onfly_compression = $GLOBALS['cfg']['CompressOnFly'] && ($compression == 'gzip' || $compression == 'bzip');
214 if ($onfly_compression) {
215 $memory_limit = trim(@ini_get('memory_limit'));
216 // 2 MB as default
217 if (empty($memory_limit)) {
218 $memory_limit = 2 * 1024 * 1024;
221 if (strtolower(substr($memory_limit, -1)) == 'm') {
222 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
223 } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
224 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
225 } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
226 $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
227 } else {
228 $memory_limit = (int)$memory_limit;
231 // Some of memory is needed for other thins and as treshold.
232 // Nijel: During export I had allocated (see memory_get_usage function)
233 // approx 1.2MB so this comes from that.
234 if ($memory_limit > 1500000) {
235 $memory_limit -= 1500000;
238 // Some memory is needed for compression, assume 1/3
239 $memory_limit /= 8;
242 // Generate filename and mime type if needed
243 if ($asfile) {
244 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
245 if ($export_type == 'server') {
246 if (isset($remember_template)) {
247 $GLOBALS['PMA_Config']->setCookie('pma_server_filename_template', $filename_template);
249 } elseif ($export_type == 'database') {
250 if (isset($remember_template)) {
251 $GLOBALS['PMA_Config']->setCookie('pma_db_filename_template', $filename_template);
253 } else {
254 if (isset($remember_template)) {
255 $GLOBALS['PMA_Config']->setCookie('pma_table_filename_template', $filename_template);
258 $filename = PMA_expandUserString($filename_template);
260 // convert filename to iso-8859-1, it is safer
261 $filename = PMA_convert_string($charset, 'iso-8859-1', $filename);
263 // Grab basic dump extension and mime type
264 $filename .= '.' . $export_list[$type]['extension'];
265 $mime_type = $export_list[$type]['mime_type'];
267 // If dump is going to be compressed, set correct mime_type and add
268 // compression to extension
269 if ($compression == 'bzip') {
270 $filename .= '.bz2';
271 $mime_type = 'application/x-bzip2';
272 } elseif ($compression == 'gzip') {
273 $filename .= '.gz';
274 $mime_type = 'application/x-gzip';
275 } elseif ($compression == 'zip') {
276 $filename .= '.zip';
277 $mime_type = 'application/zip';
281 // Open file on server if needed
282 if ($save_on_server) {
283 $save_filename = PMA_userDir($cfg['SaveDir']) . preg_replace('@[/\\\\]@', '_', $filename);
284 unset($message);
285 if (file_exists($save_filename) && empty($onserverover)) {
286 $message = PMA_Message::error(__('File %s already exists on server, change filename or check overwrite option.'));
287 $message->addParam($save_filename);
288 } else {
289 if (is_file($save_filename) && !is_writable($save_filename)) {
290 $message = PMA_Message::error(__('The web server does not have permission to save the file %s.'));
291 $message->addParam($save_filename);
292 } else {
293 if (!$file_handle = @fopen($save_filename, 'w')) {
294 $message = PMA_Message::error(__('The web server does not have permission to save the file %s.'));
295 $message->addParam($save_filename);
299 if (isset($message)) {
300 require_once './libraries/header.inc.php';
301 if ($export_type == 'server') {
302 $active_page = 'server_export.php';
303 require './server_export.php';
304 } elseif ($export_type == 'database') {
305 $active_page = 'db_export.php';
306 require './db_export.php';
307 } else {
308 $active_page = 'tbl_export.php';
309 require './tbl_export.php';
311 exit();
316 * Send headers depending on whether the user chose to download a dump file
317 * or not
319 if (!$save_on_server) {
320 if ($asfile) {
321 // Download
322 // (avoid rewriting data containing HTML with anchors and forms;
323 // this was reported to happen under Plesk)
324 @ini_set('url_rewriter.tags','');
326 header('Content-Type: ' . $mime_type);
327 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
328 // Tested behavior of
329 // IE 5.50.4807.2300
330 // IE 6.0.2800.1106 (small glitch, asks twice when I click Open)
331 // IE 6.0.2900.2180
332 // Firefox 1.0.6
333 // in http and https
334 header('Content-Disposition: attachment; filename="' . $filename . '"');
335 if (PMA_USR_BROWSER_AGENT == 'IE') {
336 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
337 header('Pragma: public');
338 } else {
339 header('Pragma: no-cache');
340 // test case: exporting a database into a .gz file with Safari
341 // would produce files not having the current time
342 // (added this header for Safari but should not harm other browsers)
343 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
345 } else {
346 // HTML
347 if ($export_type == 'database') {
348 $num_tables = count($tables);
349 if ($num_tables == 0) {
350 $message = PMA_Message::error(__('No tables found in database.'));
351 require_once './libraries/header.inc.php';
352 $active_page = 'db_export.php';
353 require './db_export.php';
354 exit();
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";
369 } // end download
372 // Fake loop just to allow skip of remain of this code by break, I'd really
373 // need exceptions here :-)
374 do {
376 // Add possibly some comments to export
377 if (!PMA_exportHeader()) {
378 break;
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 $cfgRelation = PMA_getRelationsParam();
388 if ($do_mime) {
389 require_once './libraries/transformations.lib.php';
392 // Include dates in export?
393 $do_dates = isset($GLOBALS[$what . '_dates']);
396 * Builds the dump
398 // Gets the number of tables if a dump of a database has been required
399 if ($export_type == 'server') {
400 if (isset($db_select)) {
401 $tmp_select = implode($db_select, '|');
402 $tmp_select = '|' . $tmp_select . '|';
404 // Walk over databases
405 foreach ($GLOBALS['pma']->databases as $current_db) {
406 if ((isset($tmp_select) && strpos(' ' . $tmp_select, '|' . $current_db . '|'))
407 || !isset($tmp_select)) {
408 if (!PMA_exportDBHeader($current_db)) {
409 break 2;
411 if (!PMA_exportDBCreate($current_db)) {
412 break 2;
414 $tables = PMA_DBI_get_tables($current_db);
415 $views = array();
416 foreach ($tables as $table) {
417 // if this is a view, collect it for later; views must be exported
418 // after the tables
419 $is_view = PMA_Table::isView($current_db, $table);
420 if ($is_view) {
421 $views[] = $table;
423 if (isset($GLOBALS[$what . '_structure'])) {
424 // for a view, export a stand-in definition of the table
425 // to resolve view dependencies
426 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)) {
427 break 3;
430 // if this is a view or a merge table, don't export data
431 if (isset($GLOBALS[$what . '_data']) && !($is_view || PMA_Table::isMerge($current_db, $table))) {
432 $local_query = 'SELECT * FROM ' . PMA_backquote($current_db) . '.' . PMA_backquote($table);
433 if (!PMA_exportData($current_db, $table, $crlf, $err_url, $local_query)) {
434 break 3;
437 // now export the triggers (needs to be done after the data because
438 // triggers can modify already imported tables)
439 if (isset($GLOBALS[$what . '_structure'])) {
440 if (!PMA_exportStructure($current_db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
441 break 2;
445 foreach($views as $view) {
446 // no data export for a view
447 if (isset($GLOBALS[$what . '_structure'])) {
448 if (!PMA_exportStructure($current_db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
449 break 3;
453 if (!PMA_exportDBFooter($current_db)) {
454 break 2;
458 } elseif ($export_type == 'database') {
459 if (!PMA_exportDBHeader($db)) {
460 break;
462 $i = 0;
463 $views = array();
464 // $tables contains the choices from the user (via $table_select)
465 foreach ($tables as $table) {
466 // if this is a view, collect it for later; views must be exported after
467 // the tables
468 $is_view = PMA_Table::isView($db, $table);
469 if ($is_view) {
470 $views[] = $table;
472 if (isset($GLOBALS[$what . '_structure'])) {
473 // for a view, export a stand-in definition of the table
474 // to resolve view dependencies
475 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ? 'stand_in' : 'create_table', $export_type)) {
476 break 2;
479 // if this is a view or a merge table, don't export data
480 if (isset($GLOBALS[$what . '_data']) && !($is_view || PMA_Table::isMerge($db, $table))) {
481 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
482 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
483 break 2;
486 // now export the triggers (needs to be done after the data because
487 // triggers can modify already imported tables)
488 if (isset($GLOBALS[$what . '_structure'])) {
489 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
490 break 2;
494 foreach ($views as $view) {
495 // no data export for a view
496 if (isset($GLOBALS[$what . '_structure'])) {
497 if (!PMA_exportStructure($db, $view, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'create_view', $export_type)) {
498 break 2;
503 if (!PMA_exportDBFooter($db)) {
504 break;
506 } else {
507 if (!PMA_exportDBHeader($db)) {
508 break;
510 // We export just one table
511 // $allrows comes from the form when "Dump all rows" has been selected
512 if ($allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
513 $add_query = ' LIMIT '
514 . (($limit_from > 0) ? $limit_from . ', ' : '')
515 . $limit_to;
516 } else {
517 $add_query = '';
520 $is_view = PMA_Table::isView($db, $table);
521 if (isset($GLOBALS[$what . '_structure'])) {
522 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, $is_view ? 'create_view' : 'create_table', $export_type)) {
523 break;
526 // If this is an export of a single view, we have to export data;
527 // for example, a PDF report
528 // if it is a merge table, no data is exported
529 if (isset($GLOBALS[$what . '_data']) && ! PMA_Table::isMerge($db, $table)) {
530 if (!empty($sql_query)) {
531 // only preg_replace if needed
532 if (!empty($add_query)) {
533 // remove trailing semicolon before adding a LIMIT
534 $sql_query = preg_replace('%;\s*$%', '', $sql_query);
536 $local_query = $sql_query . $add_query;
537 PMA_DBI_select_db($db);
538 } else {
539 $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
541 if (!PMA_exportData($db, $table, $crlf, $err_url, $local_query)) {
542 break;
545 // now export the triggers (needs to be done after the data because
546 // triggers can modify already imported tables)
547 if (isset($GLOBALS[$what . '_structure'])) {
548 if (!PMA_exportStructure($db, $table, $crlf, $err_url, $do_relation, $do_comments, $do_mime, $do_dates, 'triggers', $export_type)) {
549 break 2;
552 if (!PMA_exportDBFooter($db)) {
553 break;
556 if (!PMA_exportFooter()) {
557 break;
560 } while (false);
561 // End of fake loop
563 if ($save_on_server && isset($message)) {
564 require_once './libraries/header.inc.php';
565 if ($export_type == 'server') {
566 $active_page = 'server_export.php';
567 require './server_export.php';
568 } elseif ($export_type == 'database') {
569 $active_page = 'db_export.php';
570 require './db_export.php';
571 } else {
572 $active_page = 'tbl_export.php';
573 require './tbl_export.php';
575 exit();
579 * Send the dump as a file...
581 if (!empty($asfile)) {
582 // Convert the charset if required.
583 if ($output_charset_conversion) {
584 $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer);
587 // Do the compression
588 // 1. as a zipped file
589 if ($compression == 'zip') {
590 if (@function_exists('gzcompress')) {
591 $zipfile = new zipfile();
592 $zipfile -> addFile($dump_buffer, substr($filename, 0, -4));
593 $dump_buffer = $zipfile -> file();
596 // 2. as a bzipped file
597 elseif ($compression == 'bzip') {
598 if (@function_exists('bzcompress')) {
599 $dump_buffer = bzcompress($dump_buffer);
602 // 3. as a gzipped file
603 elseif ($compression == 'gzip') {
604 if (@function_exists('gzencode') && !@ini_get('zlib.output_compression')) {
605 // without the optional parameter level because it bug
606 $dump_buffer = gzencode($dump_buffer);
610 /* If ve saved on server, we have to close file now */
611 if ($save_on_server) {
612 $write_result = @fwrite($file_handle, $dump_buffer);
613 fclose($file_handle);
614 if (strlen($dump_buffer) !=0 && (!$write_result || ($write_result != strlen($dump_buffer)))) {
615 $message = new PMA_Message(__('Insufficient space to save the file %s.'), PMA_Message::ERROR, $save_filename);
616 } else {
617 $message = new PMA_Message(__('Dump has been saved to file %s.'), PMA_Message::SUCCESS, $save_filename);
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_export.php';
626 require_once './db_export.php';
627 } else {
628 $active_page = 'tbl_export.php';
629 require_once './tbl_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">
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 './libraries/footer.inc.php';
671 } // end if