1
|
<?php
|
2
|
/**
|
3
|
* Does an export of a database or a table (via pg_dump)
|
4
|
* to the screen or as a download.
|
5
|
*
|
6
|
* $Id: dbexport.php,v 1.21 2005/05/02 15:47:23 chriskl 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
|
include_once('./libraries/lib.inc.php');
|
15
|
|
16
|
// Check that database dumps are enabled.
|
17
|
if ($misc->isDumpEnabled()) {
|
18
|
|
19
|
// Make it do a download, if necessary
|
20
|
switch($_REQUEST['output']){
|
21
|
case 'show':
|
22
|
header('Content-Type: text/plain');
|
23
|
break;
|
24
|
case 'download':
|
25
|
// Set headers. MSIE is totally broken for SSL downloading, so
|
26
|
// we need to have it download in-place as plain text
|
27
|
if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) {
|
28
|
header('Content-Type: text/plain');
|
29
|
}
|
30
|
else {
|
31
|
header('Content-Type: application/download');
|
32
|
header('Content-Disposition: attachment; filename=dump.sql');
|
33
|
}
|
34
|
break;
|
35
|
case 'gzipped':
|
36
|
// MSIE in SSL mode cannot do this - it should never get to this point
|
37
|
header('Content-Type: application/download');
|
38
|
header('Content-Disposition: attachment; filename=dump.sql.gz');
|
39
|
break;
|
40
|
}
|
41
|
|
42
|
// Set environmental variables that pg_dump uses
|
43
|
$server_info = $misc->getServerInfo();
|
44
|
putenv('PGPASSWORD=' . $server_info['password']);
|
45
|
putenv('PGUSER=' . $server_info['username']);
|
46
|
$hostname = $server_info['host'];
|
47
|
if ($hostname !== null && $hostname != '') {
|
48
|
putenv('PGHOST=' . $hostname);
|
49
|
}
|
50
|
$port = $server_info['port'];
|
51
|
if ($port !== null && $port != '') {
|
52
|
putenv('PGPORT=' . $port);
|
53
|
}
|
54
|
|
55
|
// Are we doing a cluster-wide dump or just a per-database dump
|
56
|
$dumpall = ($_REQUEST['subject'] == 'server');
|
57
|
|
58
|
// Get the path og the pg_dump/pg_dumpall executable
|
59
|
$exe = $misc->escapeShellCmd($server_info[$dumpall ? 'pg_dumpall_path' : 'pg_dump_path']);
|
60
|
|
61
|
// Build command for executing pg_dump. '-i' means ignore version differences.
|
62
|
$cmd = $exe . " -i";
|
63
|
|
64
|
|
65
|
// Check for a specified table/view
|
66
|
switch ($_REQUEST['subject']) {
|
67
|
case 'table':
|
68
|
case 'view':
|
69
|
// Obtain the pg_dump version number
|
70
|
$version = array();
|
71
|
preg_match("/(\d+(?:\.\d+)?)(?:\.\d+)?.*$/", exec($exe . " --version"), $version);
|
72
|
|
73
|
// If we are 7.4 or higher, assume they are using 7.4 pg_dump and
|
74
|
// set dump schema as well. Also, mixed case dumping has been fixed
|
75
|
// then..
|
76
|
if (((float) $version[1]) >= 7.4) {
|
77
|
$cmd .= " -t " . $misc->escapeShellArg($_REQUEST[$_REQUEST['subject']]);
|
78
|
// Even though they're using a schema-enabled pg_dump, the backend database
|
79
|
// may not support schemas.
|
80
|
if ($data->hasSchemas()) {
|
81
|
$cmd .= " -n " . $misc->escapeShellArg($_REQUEST['schema']);
|
82
|
}
|
83
|
}
|
84
|
else {
|
85
|
// This is an annoying hack needed to work around a bug in dumping
|
86
|
// mixed case tables in pg_dump prior to 7.4
|
87
|
$cmd .= " -t " . $misc->escapeShellArg('"' . $_REQUEST[$_REQUEST['subject']] . '"');
|
88
|
}
|
89
|
}
|
90
|
|
91
|
// Check for GZIP compression specified
|
92
|
if ($_REQUEST['output'] == 'gzipped' && !$dumpall) {
|
93
|
$cmd .= " -Z 9";
|
94
|
}
|
95
|
|
96
|
switch ($_REQUEST['what']) {
|
97
|
case 'dataonly':
|
98
|
$cmd .= ' -a';
|
99
|
if ($_REQUEST['d_format'] == 'sql') $cmd .= ' -d';
|
100
|
elseif (isset($_REQUEST['d_oids'])) $cmd .= ' -o';
|
101
|
break;
|
102
|
case 'structureonly':
|
103
|
$cmd .= ' -s';
|
104
|
if (isset($_REQUEST['s_clean'])) $cmd .= ' -c';
|
105
|
break;
|
106
|
case 'structureanddata':
|
107
|
if ($_REQUEST['sd_format'] == 'sql') $cmd .= ' -d';
|
108
|
elseif (isset($_REQUEST['sd_oids'])) $cmd .= ' -o';
|
109
|
if (isset($_REQUEST['sd_clean'])) $cmd .= ' -c';
|
110
|
break;
|
111
|
}
|
112
|
|
113
|
if (!$dumpall) {
|
114
|
putenv('PGDATABASE=' . $_REQUEST['database']);
|
115
|
}
|
116
|
|
117
|
// Execute command and return the output to the screen
|
118
|
passthru($cmd);
|
119
|
}
|
120
|
|
121
|
?>
|