Guest User

Untitled

a guest
Jul 16th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. <?php
  2. /**
  3. * Does an export of a database, schema, or table (via pg_dump)
  4. * to the screen or as a download.
  5. *
  6. * $Id: dbexport.php,v 1.22 2007/03/25 03:15:09 xzilla Exp $
  7. */
  8.  
  9. // Prevent timeouts on large exports (non-safe mode only)
  10. if (!ini_get('safe_mode')) set_time_limit(0);
  11.  
  12. // Include application functions
  13. $_no_output = true;
  14. $f_schema = $f_object = '';
  15. include_once('./libraries/lib.inc.php');
  16.  
  17. // Are we doing a cluster-wide dump or just a per-database dump
  18. $dumpall = ($_REQUEST['subject'] == 'server');
  19.  
  20. // Check that database dumps are enabled.
  21. if ($misc->isDumpEnabled($dumpall)) {
  22.  
  23. $server_info = $misc->getServerInfo();
  24.  
  25. // Get the path of the pg_dump/pg_dumpall executable
  26. $exe = $misc->escapeShellCmd($server_info[$dumpall ? 'pg_dumpall_path' : 'pg_dump_path']);
  27.  
  28. // Obtain the pg_dump version number and check if the path is good
  29. $version = array();
  30. preg_match("/(\d+(?:\.\d+)?)(?:\.\d+)?.*$/", exec($exe . " --version"), $version);
  31.  
  32. if (empty($version)) {
  33. if ($dumpall)
  34. printf($lang['strbadpgdumpallpath'], $server_info['pg_dumpall_path']);
  35. else
  36. printf($lang['strbadpgdumppath'], $server_info['pg_dump_path']);
  37. exit;
  38. }
  39.  
  40. // Make it do a download, if necessary
  41. switch($_REQUEST['output']){
  42. case 'show':
  43. header('Content-Type: text/plain');
  44. break;
  45. case 'download':
  46. // Set headers. MSIE is totally broken for SSL downloading, so
  47. // we need to have it download in-place as plain text
  48. if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) {
  49. header('Content-Type: text/plain');
  50. }
  51. else {
  52. header('Content-Type: application/download');
  53. header('Content-Disposition: attachment; filename=dump.sql');
  54. }
  55. break;
  56. case 'gzipped':
  57. // MSIE in SSL mode cannot do this - it should never get to this point
  58. header('Content-Type: application/download');
  59. header('Content-Disposition: attachment; filename=dump.sql.gz');
  60. break;
  61. }
  62.  
  63. // Set environmental variables that pg_dump uses
  64. putenv('PGPASSWORD=' . $server_info['password']);
  65. putenv('PGUSER=' . $server_info['username']);
  66. $hostname = $server_info['host'];
  67. if ($hostname !== null && $hostname != '') {
  68. putenv('PGHOST=' . $hostname);
  69. }
  70. $port = $server_info['port'];
  71. if ($port !== null && $port != '') {
  72. putenv('PGPORT=' . $port);
  73. }
  74.  
  75. // Build command for executing pg_dump. '-i' means ignore version differences.
  76. $cmd = $exe . " -i";
  77.  
  78. // we are PG 7.4+, so we always have a schema
  79. if (isset($_REQUEST['schema'])) {
  80. $f_schema = $_REQUEST['schema'];
  81. $data->fieldClean($f_schema);
  82. }
  83.  
  84. // Check for a specified table/view
  85. switch ($_REQUEST['subject']) {
  86. case 'schema':
  87. // This currently works for 8.2+ (due to the orthoganl -t -n issue introduced then)
  88. $cmd .= " -n " . $misc->escapeShellArg("\"{$f_schema}\"");
  89. break;
  90. case 'table':
  91. case 'view':
  92. $f_object = $_REQUEST[$_REQUEST['subject']];
  93. $data->fieldClean($f_object);
  94.  
  95. // Starting in 8.2, -n and -t are orthagonal, so we now schema qualify
  96. // the table name in the -t argument and quote both identifiers
  97. if ( ((float) $version[1]) >= 8.2 ) {
  98. $cmd .= " -t " . $misc->escapeShellArg("\"{$f_schema}\".\"{$f_object}\"");
  99. }
  100. else {
  101. // If we are 7.4 or higher, assume they are using 7.4 pg_dump and
  102. // set dump schema as well. Also, mixed case dumping has been fixed
  103. // then..
  104. $cmd .= " -t " . $misc->escapeShellArg($f_object)
  105. . " -n " . $misc->escapeShellArg($f_schema);
  106. }
  107. }
  108.  
  109. // Check for GZIP compression specified
  110. if ($_REQUEST['output'] == 'gzipped' && !$dumpall) {
  111. $cmd .= " -Z 9";
  112. }
  113.  
  114. switch ($_REQUEST['what']) {
  115. case 'dataonly':
  116. $cmd .= ' -a';
  117. if ($_REQUEST['d_format'] == 'sql') $cmd .= ' --inserts';
  118. elseif (isset($_REQUEST['d_oids'])) $cmd .= ' -o';
  119. break;
  120. case 'structureonly':
  121. $cmd .= ' -s';
  122. if (isset($_REQUEST['s_clean'])) $cmd .= ' -c';
  123. break;
  124. case 'structureanddata':
  125. if ($_REQUEST['sd_format'] == 'sql') $cmd .= ' --inserts';
  126. elseif (isset($_REQUEST['sd_oids'])) $cmd .= ' -o';
  127. if (isset($_REQUEST['sd_clean'])) $cmd .= ' -c';
  128. break;
  129. }
  130.  
  131. if (!$dumpall) {
  132. putenv('PGDATABASE=' . $_REQUEST['database']);
  133. }
  134.  
  135. // Execute command and return the output to the screen
  136. passthru($cmd);
  137. }
  138.  
  139. ?>
Add Comment
Please, Sign In to add comment