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