1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin 6.1.3 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Controller; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Base controller class. |
11
|
|
|
*/ |
12
|
|
|
class DbexportController extends BaseController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Default method to render the controller according to the action parameter. |
16
|
|
|
*/ |
17
|
|
|
public function render() |
18
|
|
|
{ |
19
|
|
|
$data = $this->misc->getDatabaseAccessor(); |
20
|
|
|
|
21
|
|
|
// Prevent timeouts on large exports |
22
|
|
|
\set_time_limit(0); |
23
|
|
|
|
24
|
|
|
$response = $this |
25
|
|
|
->container |
26
|
|
|
->response; |
27
|
|
|
|
28
|
|
|
// Include application functions |
29
|
|
|
$f_schema = $f_object = ''; |
30
|
|
|
$this->setNoOutput(true); |
31
|
|
|
|
32
|
|
|
\ini_set('memory_limit', '768M'); |
33
|
|
|
|
34
|
|
|
// Are we doing a cluster-wide dump or just a per-database dump |
35
|
|
|
$dumpall = ('server' === $_REQUEST['subject']); |
36
|
|
|
|
37
|
|
|
// Check that database dumps are enabled. |
38
|
|
|
if (!$this->misc->isDumpEnabled($dumpall)) { |
39
|
|
|
return $response; |
40
|
|
|
} |
41
|
|
|
$server_info = $this->misc->getServerInfo(); |
42
|
|
|
|
43
|
|
|
// Get the path of the pg_dump/pg_dumpall executable |
44
|
|
|
$exe = $this->misc->escapeShellCmd($server_info[$dumpall ? 'pg_dumpall_path' : 'pg_dump_path']); |
45
|
|
|
|
46
|
|
|
// Obtain the pg_dump version number and check if the path is good |
47
|
|
|
$version = []; |
48
|
|
|
\preg_match('/(\\d+(?:\\.\\d+)?)(?:\\.\\d+)?.*$/', \exec($exe . ' --version'), $version); |
49
|
|
|
|
50
|
|
|
if (empty($version)) { |
51
|
|
|
$this->prtrace('$exe', $exe, 'version', $version[1]); |
52
|
|
|
|
53
|
|
|
if ($dumpall) { |
54
|
|
|
\printf($this->lang['strbadpgdumpallpath'], $server_info['pg_dumpall_path']); |
55
|
|
|
} else { |
56
|
|
|
\printf($this->lang['strbadpgdumppath'], $server_info['pg_dump_path']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$response = $response |
63
|
|
|
->withHeader('Controller', $this->controller_name); |
64
|
|
|
|
65
|
|
|
$this->prtrace('REQUEST[output]', $_REQUEST['output']); |
66
|
|
|
// Make it do a download, if necessary |
67
|
|
|
switch ($_REQUEST['output']) { |
68
|
|
|
case 'show': |
69
|
|
|
\header('Content-Type: text/plain'); |
70
|
|
|
$response = $response |
71
|
|
|
->withHeader('Content-type', 'text/plain'); |
72
|
|
|
|
73
|
|
|
break; |
74
|
|
|
case 'download': |
75
|
|
|
// Set headers. MSIE is totally broken for SSL downloading, so |
76
|
|
|
// we need to have it download in-place as plain text |
77
|
|
|
if (\mb_strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) { |
78
|
|
|
\header('Content-Type: text/plain'); |
79
|
|
|
$response = $response |
80
|
|
|
->withHeader('Content-type', 'text/plain'); |
81
|
|
|
} else { |
82
|
|
|
$response = $response |
83
|
|
|
->withHeader('Content-type', 'application/download') |
84
|
|
|
->withHeader('Content-Disposition', 'attachment; filename=dump.sql'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
break; |
88
|
|
|
case 'gzipped': |
89
|
|
|
// MSIE in SSL mode cannot do this - it should never get to this point |
90
|
|
|
$response = $response |
91
|
|
|
->withHeader('Content-type', 'application/download') |
92
|
|
|
->withHeader('Content-Disposition', 'attachment; filename=dump.sql.gz'); |
93
|
|
|
|
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Set environmental variables that pg_dump uses |
98
|
|
|
\putenv('PGPASSWORD=' . $server_info['password']); |
99
|
|
|
\putenv('PGUSER=' . $server_info['username']); |
100
|
|
|
$hostname = $server_info['host']; |
101
|
|
|
|
102
|
|
|
if (null !== $hostname && '' !== $hostname) { |
103
|
|
|
\putenv('PGHOST=' . $hostname); |
104
|
|
|
} |
105
|
|
|
$port = $server_info['port']; |
106
|
|
|
|
107
|
|
|
if (null !== $port && '' !== $port) { |
108
|
|
|
\putenv('PGPORT=' . $port); |
109
|
|
|
} |
110
|
|
|
$cmd = $exe; |
111
|
|
|
// Build command for executing pg_dump. '-i' means ignore version differences. |
112
|
|
|
// deprecated |
113
|
|
|
/*if (((float) $version[1]) < 9.5) { |
114
|
|
|
$this->prtrace('version', $version); |
115
|
|
|
|
116
|
|
|
$cmd = $exe . ' -i'; |
117
|
|
|
} else { |
118
|
|
|
$cmd = $exe; |
119
|
|
|
}*/ |
120
|
|
|
|
121
|
|
|
// we are PG 7.4+, so we always have a schema |
122
|
|
|
if (isset($_REQUEST['schema'])) { |
123
|
|
|
$f_schema = $_REQUEST['schema']; |
124
|
|
|
$data->fieldClean($f_schema); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Check for a specified table/view |
128
|
|
|
switch ($_REQUEST['subject']) { |
129
|
|
|
case 'schema': |
130
|
|
|
// This currently works for 8.2+ (due to the orthoganl -t -n issue introduced then) |
131
|
|
|
$cmd .= ' -n ' . $this->misc->escapeShellArg("\"{$f_schema}\""); |
132
|
|
|
|
133
|
|
|
break; |
134
|
|
|
case 'table': |
135
|
|
|
case 'view': |
136
|
|
|
case 'matview': |
137
|
|
|
$f_object = $_REQUEST[$_REQUEST['subject']]; |
138
|
|
|
$this->prtrace('f_object', $f_object); |
139
|
|
|
$data->fieldClean($f_object); |
140
|
|
|
|
141
|
|
|
// Starting in 8.2, -n and -t are orthagonal, so we now schema qualify |
142
|
|
|
// the table name in the -t argument and quote both identifiers |
143
|
|
|
if (8.2 <= ((float) $version[1])) { |
144
|
|
|
$cmd .= ' -t ' . $this->misc->escapeShellArg("\"{$f_schema}\".\"{$f_object}\""); |
145
|
|
|
} else { |
146
|
|
|
// If we are 7.4 or higher, assume they are using 7.4 pg_dump and |
147
|
|
|
// set dump schema as well. Also, mixed case dumping has been fixed |
148
|
|
|
// then.. |
149
|
|
|
$cmd .= ' -t ' . $this->misc->escapeShellArg($f_object) |
150
|
|
|
. ' -n ' . $this->misc->escapeShellArg($f_schema); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Check for GZIP compression specified |
155
|
|
|
if ('gzipped' === $_REQUEST['output'] && !$dumpall) { |
156
|
|
|
$cmd .= ' -Z 9'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
switch ($_REQUEST['what']) { |
160
|
|
|
case 'dataonly': |
161
|
|
|
$cmd .= ' -a'; |
162
|
|
|
|
163
|
|
|
if ('sql' === $_REQUEST['d_format']) { |
164
|
|
|
$cmd .= ' --inserts'; |
165
|
|
|
} elseif (isset($_REQUEST['d_oids'])) { |
166
|
|
|
$cmd .= ' -o'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
break; |
170
|
|
|
case 'structureonly': |
171
|
|
|
$cmd .= ' -s'; |
172
|
|
|
|
173
|
|
|
if (isset($_REQUEST['s_clean'])) { |
174
|
|
|
$cmd .= ' -c'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
break; |
178
|
|
|
case 'structureanddata': |
179
|
|
|
if ('sql' === $_REQUEST['sd_format']) { |
180
|
|
|
$cmd .= ' --inserts'; |
181
|
|
|
} elseif (isset($_REQUEST['sd_oids'])) { |
182
|
|
|
$cmd .= ' -o'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (isset($_REQUEST['sd_clean'])) { |
186
|
|
|
$cmd .= ' -c'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
break; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (!$dumpall) { |
193
|
|
|
\putenv('PGDATABASE=' . $_REQUEST['database']); |
194
|
|
|
} else { |
195
|
|
|
//$cmd .= ' --no-role-password'; |
196
|
|
|
\putenv('PGDATABASE'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/*$this->prtrace( |
200
|
|
|
'ENV VARS', |
201
|
|
|
[ |
202
|
|
|
'PGUSER' => \getenv('PGUSER'), |
203
|
|
|
'PGPASSWORD' => \getenv('PGPASSWORD'), |
204
|
|
|
'PGHOST' => \getenv('PGHOST'), |
205
|
|
|
'PGPORT' => \getenv('PGPORT'), |
206
|
|
|
'PGDATABASE' => \getenv('PGDATABASE'), |
207
|
|
|
] |
208
|
|
|
);*/ |
209
|
|
|
|
210
|
|
|
// Execute command and return the output to the screen |
211
|
|
|
\passthru($cmd); |
212
|
|
|
|
213
|
|
|
return $response; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|