1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-RC1. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @file |
11
|
|
|
* Class to hold various commonly used functions |
12
|
|
|
* |
13
|
|
|
* Id: Misc.php,v 1.171 2008/03/17 21:35:48 ioguix Exp $ |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class to hold various commonly used functions. |
18
|
|
|
* |
19
|
|
|
* Release: Misc.php,v 1.171 2008/03/17 21:35:48 ioguix Exp $ |
20
|
|
|
*/ |
21
|
|
|
class Misc |
22
|
|
|
{ |
23
|
|
|
use \PHPPgAdmin\Traits\HelperTrait; |
24
|
|
|
use \PHPPgAdmin\Traits\MiscTrait; |
25
|
|
|
|
26
|
|
|
private $_connection; |
27
|
|
|
private $_no_db_connection = false; |
28
|
|
|
private $_reload_browser = false; |
29
|
|
|
private $_data; |
30
|
|
|
private $_database; |
31
|
|
|
private $_server_id; |
32
|
|
|
private $_server_info; |
33
|
|
|
private $_error_msg = ''; |
34
|
|
|
|
35
|
|
|
public $appLangFiles = []; |
36
|
|
|
public $appName = ''; |
37
|
|
|
public $appVersion = ''; |
38
|
|
|
public $form = ''; |
39
|
|
|
public $href = ''; |
40
|
|
|
public $controller_name = 'Misc'; |
41
|
|
|
public $lang = []; |
42
|
|
|
|
43
|
|
|
protected $container; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \Slim\Container $container The container |
47
|
|
|
*/ |
48
|
|
|
public function __construct(\Slim\Container $container) |
49
|
|
|
{ |
50
|
|
|
$this->container = $container; |
51
|
|
|
|
52
|
|
|
$this->lang = $container->get('lang'); |
53
|
|
|
$this->conf = $container->get('conf'); |
54
|
|
|
|
55
|
|
|
//$this->view = $container->get('view'); |
56
|
|
|
$this->plugin_manager = $container->get('plugin_manager'); |
57
|
|
|
$this->appLangFiles = $container->get('appLangFiles'); |
58
|
|
|
|
59
|
|
|
$this->appName = $container->get('settings')['appName']; |
60
|
|
|
$this->appVersion = $container->get('settings')['appVersion']; |
61
|
|
|
$this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
62
|
|
|
$this->phpMinVer = $container->get('settings')['phpMinVer']; |
63
|
|
|
|
64
|
|
|
$base_version = $container->get('settings')['base_version']; |
65
|
|
|
|
66
|
|
|
//$this->prtrace($base_version); |
67
|
|
|
|
68
|
|
|
// Check for config file version mismatch |
69
|
|
|
if (!isset($this->conf['version']) || $base_version > $this->conf['version']) { |
70
|
|
|
$container->get('utils')->addError($this->lang['strbadconfig']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Check database support is properly compiled in |
74
|
|
|
if (!function_exists('pg_connect')) { |
75
|
|
|
$container->get('utils')->addError($this->lang['strnotloaded']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Check the version of PHP |
79
|
|
|
if (version_compare(PHP_VERSION, $this->phpMinVer, '<')) { |
80
|
|
|
$container->get('utils')->addError(sprintf('Version of PHP not supported. Please upgrade to version %s or later.', $this->phpMinVer)); |
81
|
|
|
} |
82
|
|
|
//$this->dumpAndDie($this); |
83
|
|
|
|
84
|
|
|
$this->getServerId(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function serverToSha() |
88
|
|
|
{ |
89
|
|
|
$request_server = $this->container->requestobj->getParam('server'); |
90
|
|
|
if ($request_server === null) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
$srv_array = explode(':', $request_server); |
94
|
|
|
if (count($srv_array) === 3) { |
95
|
|
|
return sha1($request_server); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $request_server; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getServerId() |
102
|
|
|
{ |
103
|
|
|
if ($this->_server_id) { |
104
|
|
|
return $this->_server_id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$request_server = $this->serverToSha(); |
108
|
|
|
|
109
|
|
|
if (count($this->conf['servers']) === 1) { |
110
|
|
|
$info = $this->conf['servers'][0]; |
111
|
|
|
$this->_server_id = sha1($info['host'] . ':' . $info['port'] . ':' . $info['sslmode']); |
112
|
|
|
} elseif ($request_server !== null) { |
113
|
|
|
$this->_server_id = $request_server; |
114
|
|
|
} elseif (isset($_SESSION['webdbLogin']) && count($_SESSION['webdbLogin']) > 0) { |
115
|
|
|
//$this->prtrace('webdbLogin', $_SESSION['webdbLogin']); |
116
|
|
|
$this->_server_id = array_keys($_SESSION['webdbLogin'])[0]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->_server_id; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sets the view instance property of this class. |
124
|
|
|
* |
125
|
|
|
* @param \Slim\Views\Twig $view view instance |
126
|
|
|
* |
127
|
|
|
* @return \PHPPgAdmin\Misc this class instance |
128
|
|
|
*/ |
129
|
|
|
public function setView(\Slim\Views\Twig $view) |
130
|
|
|
{ |
131
|
|
|
$this->view = $view; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Adds or modifies a key in the $conf instance property of this class. |
138
|
|
|
* |
139
|
|
|
* @param string $key name of the key to set |
140
|
|
|
* @param mixed $value value of the key to set |
141
|
|
|
* |
142
|
|
|
* @return \PHPPgAdmin\Misc this class instance |
143
|
|
|
*/ |
144
|
|
|
public function setConf($key, $value) |
145
|
|
|
{ |
146
|
|
|
$this->conf[$key] = $value; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Gets the value of a config property, or the array of all config properties. |
153
|
|
|
* |
154
|
|
|
* @param null|string $key value of the key to be retrieved. If null, the full array is returnes |
155
|
|
|
* |
156
|
|
|
* @return null|array|string the whole $conf array, the value of $conf[key] or null if said key does not exist |
157
|
|
|
*/ |
158
|
|
|
public function getConf($key = null) |
159
|
|
|
{ |
160
|
|
|
if ($key === null) { |
161
|
|
|
return $this->conf; |
162
|
|
|
} |
163
|
|
|
if (array_key_exists($key, $this->conf)) { |
164
|
|
|
return $this->conf[$key]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Displays link to the context help. |
172
|
|
|
* |
173
|
|
|
* @param string $str the string that the context help is related to (already escaped) |
174
|
|
|
* @param string $help help section identifier |
175
|
|
|
* @param bool $do_print true to echo, false to return |
176
|
|
|
*/ |
177
|
|
|
public function printHelp($str, $help = null, $do_print = true) |
178
|
|
|
{ |
179
|
|
|
if ($help !== null) { |
180
|
|
|
$helplink = $this->getHelpLink($help); |
181
|
|
|
$str .= '<a class="help" href="' . $helplink . '" title="' . $this->lang['strhelp'] . '" target="phppgadminhelp">'; |
182
|
|
|
$str .= $this->lang['strhelpicon'] . '</a>'; |
183
|
|
|
} |
184
|
|
|
if ($do_print) { |
185
|
|
|
echo $str; |
186
|
|
|
} else { |
187
|
|
|
return $str; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gets the help link. |
193
|
|
|
* |
194
|
|
|
* @param string $help The help subject |
195
|
|
|
* |
196
|
|
|
* @return string the help link |
197
|
|
|
*/ |
198
|
|
|
public function getHelpLink($help) |
199
|
|
|
{ |
200
|
|
|
return htmlspecialchars(SUBFOLDER . '/help?help=' . urlencode($help) . '&server=' . urlencode($this->getServerId())); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Internally sets the reload browser property. |
205
|
|
|
* |
206
|
|
|
* @param bool $flag sets internal $_reload_browser var which will be passed to the footer methods |
207
|
|
|
* |
208
|
|
|
* @return \PHPPgAdmin\Misc this class instance |
209
|
|
|
*/ |
210
|
|
|
public function setReloadBrowser($flag) |
211
|
|
|
{ |
212
|
|
|
$this->_reload_browser = (bool) $flag; |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getReloadBrowser() |
218
|
|
|
{ |
219
|
|
|
return $this->_reload_browser; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getContainer() |
223
|
|
|
{ |
224
|
|
|
return $this->container; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Sets $_no_db_connection boolean value, allows to render scripts that do not need an active session. |
229
|
|
|
* |
230
|
|
|
* @param bool $flag true or false to allow unconnected clients to access the view |
231
|
|
|
* |
232
|
|
|
* @return \PHPPgAdmin\Misc this class instance |
233
|
|
|
*/ |
234
|
|
|
public function setNoDBConnection($flag) |
235
|
|
|
{ |
236
|
|
|
$this->_no_db_connection = (bool) $flag; |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Gets member variable $_no_db_connection. |
243
|
|
|
* |
244
|
|
|
* @return bool value of member variable $_no_db_connection |
245
|
|
|
*/ |
246
|
|
|
public function getNoDBConnection() |
247
|
|
|
{ |
248
|
|
|
return $this->_no_db_connection; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Sets the last error message to display afterwards instead of just dying with the error msg. |
253
|
|
|
* |
254
|
|
|
* @param string $msg error message string |
255
|
|
|
* |
256
|
|
|
* @return \PHPPgAdmin\Misc this class instance |
257
|
|
|
*/ |
258
|
|
|
public function setErrorMsg($msg) |
259
|
|
|
{ |
260
|
|
|
$this->_error_msg = $msg; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the error messages stored in member variable $_error_msg. |
267
|
|
|
* |
268
|
|
|
* @return string the error message |
269
|
|
|
*/ |
270
|
|
|
public function getErrorMsg() |
271
|
|
|
{ |
272
|
|
|
return $this->_error_msg; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Creates a database accessor. |
277
|
|
|
* |
278
|
|
|
* @param string $database the name of the database |
279
|
|
|
* @param mixed $server_id the id of the server |
280
|
|
|
* |
281
|
|
|
* @internal mixed $plaform placeholder that will receive the value of the platform |
282
|
|
|
* |
283
|
|
|
* @return \PHPPgAdmin\Database\ADOdbBase the database accessor instance |
284
|
|
|
*/ |
285
|
|
|
public function getDatabaseAccessor($database = '', $server_id = null) |
286
|
|
|
{ |
287
|
|
|
$lang = $this->lang; |
288
|
|
|
|
289
|
|
|
if ($server_id !== null) { |
290
|
|
|
$this->_server_id = $server_id; |
291
|
|
|
} |
292
|
|
|
//$this->prtrace($this->_server_id); |
293
|
|
|
|
294
|
|
|
$server_info = $this->getServerInfo($this->_server_id); |
295
|
|
|
|
296
|
|
|
if ($this->_no_db_connection || !isset($server_info['username'])) { |
297
|
|
|
return null; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if ($this->_data === null) { |
301
|
|
|
try { |
302
|
|
|
$_connection = $this->getConnection($database, $this->_server_id); |
303
|
|
|
} catch (\Exception $e) { |
304
|
|
|
$this->setServerInfo(null, null, $this->_server_id); |
305
|
|
|
$this->setNoDBConnection(true); |
306
|
|
|
$this->setErrorMsg($e->getMessage()); |
307
|
|
|
|
308
|
|
|
return null; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
//$this->prtrace('_connection', $_connection); |
312
|
|
|
if (!$_connection) { |
313
|
|
|
$this->container->utils->addError($lang['strloginfailed']); |
314
|
|
|
$this->setErrorMsg($lang['strloginfailed']); |
315
|
|
|
|
316
|
|
|
return null; |
317
|
|
|
} |
318
|
|
|
// Get the name of the database driver we need to use. |
319
|
|
|
// The description of the server is returned in $platform. |
320
|
|
|
$_type = $_connection->getDriver($platform); |
321
|
|
|
|
322
|
|
|
//$this->prtrace(['type' => $_type, 'platform' => $platform, 'pgVersion' => $_connection->conn->pgVersion]); |
323
|
|
|
|
324
|
|
|
if ($_type === null) { |
325
|
|
|
$errormsg = sprintf($lang['strpostgresqlversionnotsupported'], $this->postgresqlMinVer); |
326
|
|
|
$this->container->utils->addError($errormsg); |
327
|
|
|
$this->setErrorMsg($errormsg); |
328
|
|
|
|
329
|
|
|
return null; |
330
|
|
|
} |
331
|
|
|
$_type = '\PHPPgAdmin\Database\\' . $_type; |
332
|
|
|
|
333
|
|
|
$this->prtrace('driver:', $_type); |
334
|
|
|
|
335
|
|
|
$this->setServerInfo('platform', $platform, $this->_server_id); |
336
|
|
|
$this->setServerInfo('pgVersion', $_connection->conn->pgVersion, $this->_server_id); |
337
|
|
|
|
338
|
|
|
// Create a database wrapper class for easy manipulation of the |
339
|
|
|
// connection. |
340
|
|
|
|
341
|
|
|
$this->_data = new $_type($_connection->conn, $this->container, $server_info); |
342
|
|
|
$this->_data->platform = $_connection->platform; |
343
|
|
|
|
344
|
|
|
//$this->_data->getHelpPages(); |
345
|
|
|
|
346
|
|
|
//$this->prtrace('help_page has ' . count($this->_data->help_page) . ' items'); |
347
|
|
|
|
348
|
|
|
/* we work on UTF-8 only encoding */ |
349
|
|
|
$this->_data->execute("SET client_encoding TO 'UTF-8'"); |
350
|
|
|
|
351
|
|
|
if ($this->_data->hasByteaHexDefault()) { |
352
|
|
|
$this->_data->execute('SET bytea_output TO escape'); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if ($this->_no_db_connection === false && |
357
|
|
|
$this->getDatabase() !== null && |
358
|
|
|
isset($_REQUEST['schema']) |
359
|
|
|
) { |
360
|
|
|
$status = $this->_data->setSchema($_REQUEST['schema']); |
361
|
|
|
|
362
|
|
|
if ($status != 0) { |
363
|
|
|
$this->container->utils->addError($this->lang['strbadschema']); |
364
|
|
|
$this->setErrorMsg($this->lang['strbadschema']); |
365
|
|
|
|
366
|
|
|
return null; |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $this->_data; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function getConnection($database = '', $server_id = null) |
374
|
|
|
{ |
375
|
|
|
$lang = $this->lang; |
376
|
|
|
|
377
|
|
|
if ($this->_connection === null) { |
378
|
|
|
if ($server_id !== null) { |
379
|
|
|
$this->_server_id = $server_id; |
380
|
|
|
} |
381
|
|
|
$server_info = $this->getServerInfo($this->_server_id); |
382
|
|
|
$database_to_use = $this->getDatabase($database); |
383
|
|
|
|
384
|
|
|
// Perform extra security checks if this config option is set |
385
|
|
|
if ($this->conf['extra_login_security']) { |
386
|
|
|
// Disallowed logins if extra_login_security is enabled. |
387
|
|
|
// These must be lowercase. |
388
|
|
|
$bad_usernames = [ |
389
|
|
|
'pgsql' => 'pgsql', |
390
|
|
|
'postgres' => 'postgres', |
391
|
|
|
'root' => 'root', |
392
|
|
|
'administrator' => 'administrator', |
393
|
|
|
]; |
394
|
|
|
|
395
|
|
|
if (isset($server_info['username']) && |
396
|
|
|
array_key_exists(strtolower($server_info['username']), $bad_usernames) |
397
|
|
|
) { |
398
|
|
|
$msg = $lang['strlogindisallowed']; |
399
|
|
|
|
400
|
|
|
throw new \Exception($msg); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if (!isset($server_info['password']) || |
404
|
|
|
$server_info['password'] == '' |
405
|
|
|
) { |
406
|
|
|
$msg = $lang['strlogindisallowed']; |
407
|
|
|
|
408
|
|
|
throw new \Exception($msg); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
try { |
413
|
|
|
// Create the connection object and make the connection |
414
|
|
|
$this->_connection = new \PHPPgAdmin\Database\Connection( |
415
|
|
|
$server_info, |
416
|
|
|
$database_to_use, |
417
|
|
|
$this->container |
418
|
|
|
); |
419
|
|
|
} catch (\PHPPgAdmin\ADOdbException $e) { |
420
|
|
|
throw new \Exception($lang['strloginfailed']); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
return $this->_connection; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Validate and retrieve information on a server. |
429
|
|
|
* If the parameter isn't supplied then the currently |
430
|
|
|
* connected server is returned. |
431
|
|
|
* |
432
|
|
|
* @param string $server_id A server identifier (host:port) |
433
|
|
|
* |
434
|
|
|
* @return array An associative array of server properties |
435
|
|
|
*/ |
436
|
|
|
public function getServerInfo($server_id = null) |
437
|
|
|
{ |
438
|
|
|
if ($server_id !== null) { |
439
|
|
|
$this->_server_id = $server_id; |
440
|
|
|
} elseif ($this->_server_info !== null) { |
441
|
|
|
return $this->_server_info; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
// Check for the server in the logged-in list |
445
|
|
|
if (isset($_SESSION['webdbLogin'][$this->_server_id])) { |
446
|
|
|
$this->_server_info = $_SESSION['webdbLogin'][$this->_server_id]; |
447
|
|
|
|
448
|
|
|
return $this->_server_info; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
// Otherwise, look for it in the conf file |
452
|
|
|
foreach ($this->conf['servers'] as $idx => $info) { |
453
|
|
|
$server_string = $info['host'] . ':' . $info['port'] . ':' . $info['sslmode']; |
454
|
|
|
$server_sha = sha1($server_string); |
455
|
|
|
|
456
|
|
|
if ($this->_server_id === $server_string || |
457
|
|
|
$this->_server_id === $server_sha |
458
|
|
|
) { |
459
|
|
|
if (isset($info['username'])) { |
460
|
|
|
$this->setServerInfo(null, $info, $this->_server_id); |
461
|
|
|
} elseif (isset($_SESSION['sharedUsername'])) { |
462
|
|
|
$info['username'] = $_SESSION['sharedUsername']; |
463
|
|
|
$info['password'] = $_SESSION['sharedPassword']; |
464
|
|
|
$this->setReloadBrowser(true); |
465
|
|
|
$this->setServerInfo(null, $info, $this->_server_id); |
466
|
|
|
} |
467
|
|
|
$this->_server_info = $info; |
468
|
|
|
|
469
|
|
|
return $this->_server_info; |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
if ($server_id === null) { |
474
|
|
|
$this->_server_info = null; |
475
|
|
|
|
476
|
|
|
return $this->_server_info; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
$this->prtrace('Invalid server param'); |
480
|
|
|
$this->_server_info = null; |
481
|
|
|
// Unable to find a matching server, are we being hacked? |
482
|
|
|
return $this->halt($this->lang['strinvalidserverparam']); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Set server information. |
487
|
|
|
* |
488
|
|
|
* @param null|string $key parameter name to set, or null to replace all |
489
|
|
|
* params with the assoc-array in $value |
490
|
|
|
* @param mixed $value the new value, or null to unset the parameter |
491
|
|
|
* @param null|string $server_id the server identifier, or null for current server |
492
|
|
|
*/ |
493
|
|
|
public function setServerInfo($key, $value, $server_id = null) |
494
|
|
|
{ |
495
|
|
|
if ($server_id === null) { |
496
|
|
|
$server_id = $this->container->requestobj->getParam('server'); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
if ($key === null) { |
500
|
|
|
if ($value === null) { |
501
|
|
|
unset($_SESSION['webdbLogin'][$server_id]); |
502
|
|
|
} else { |
503
|
|
|
$_SESSION['webdbLogin'][$server_id] = $value; |
504
|
|
|
} |
505
|
|
|
} else { |
506
|
|
|
if ($value === null) { |
507
|
|
|
unset($_SESSION['webdbLogin'][$server_id][$key]); |
508
|
|
|
} else { |
509
|
|
|
$_SESSION['webdbLogin'][$server_id][$key] = $value; |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
public function getDatabase($database = '') |
515
|
|
|
{ |
516
|
|
|
if ($this->_server_id === null && !isset($_REQUEST['database'])) { |
517
|
|
|
return null; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
$server_info = $this->getServerInfo($this->_server_id); |
521
|
|
|
|
522
|
|
|
if ($this->_server_id !== null && |
523
|
|
|
isset($server_info['useonlydefaultdb']) && |
524
|
|
|
$server_info['useonlydefaultdb'] === true && |
525
|
|
|
isset($server_info['defaultdb']) |
526
|
|
|
) { |
527
|
|
|
$this->_database = $server_info['defaultdb']; |
528
|
|
|
} elseif ($database !== '') { |
529
|
|
|
$this->_database = $database; |
530
|
|
|
} elseif (isset($_REQUEST['database'])) { |
531
|
|
|
// Connect to the current database |
532
|
|
|
$this->_database = $_REQUEST['database']; |
533
|
|
|
} elseif (isset($server_info['defaultdb'])) { |
534
|
|
|
// or if one is not specified then connect to the default database. |
535
|
|
|
$this->_database = $server_info['defaultdb']; |
536
|
|
|
} else { |
537
|
|
|
return null; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
return $this->_database; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Set the current schema. |
545
|
|
|
* |
546
|
|
|
* @param string $schema The schema name |
547
|
|
|
* |
548
|
|
|
* @return int 0 on success |
549
|
|
|
*/ |
550
|
|
|
public function setCurrentSchema($schema) |
551
|
|
|
{ |
552
|
|
|
$data = $this->getDatabaseAccessor(); |
553
|
|
|
|
554
|
|
|
$status = $data->setSchema($schema); |
|
|
|
|
555
|
|
|
if ($status != 0) { |
556
|
|
|
return $status; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
$_REQUEST['schema'] = $schema; |
560
|
|
|
$this->container->offsetSet('schema', $schema); |
561
|
|
|
$this->setHREF(); |
562
|
|
|
|
563
|
|
|
return 0; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Checks if dumps are properly set up. |
568
|
|
|
* |
569
|
|
|
* @param bool $all (optional) True to check pg_dumpall, false to just check pg_dump |
570
|
|
|
* |
571
|
|
|
* @return bool True, dumps are set up, false otherwise |
572
|
|
|
*/ |
573
|
|
|
public function isDumpEnabled($all = false) |
574
|
|
|
{ |
575
|
|
|
$info = $this->getServerInfo(); |
576
|
|
|
|
577
|
|
|
return !empty($info[$all ? 'pg_dumpall_path' : 'pg_dump_path']); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* Sets the href tracking variable. |
582
|
|
|
* |
583
|
|
|
* @return \PHPPgAdmin\Misc this class instance |
584
|
|
|
*/ |
585
|
|
|
public function setHREF() |
586
|
|
|
{ |
587
|
|
|
$this->href = $this->getHREF(); |
588
|
|
|
|
589
|
|
|
return $this; |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* Get a href query string, excluding objects below the given object type (inclusive). |
594
|
|
|
* |
595
|
|
|
* @param null|string $exclude_from |
596
|
|
|
* |
597
|
|
|
* @return string |
598
|
|
|
*/ |
599
|
|
|
public function getHREF($exclude_from = null) |
600
|
|
|
{ |
601
|
|
|
$href = []; |
602
|
|
|
|
603
|
|
|
$server = $this->container->server || isset($_REQUEST['server']) ? $_REQUEST['server'] : null; |
604
|
|
|
$database = $this->container->database || isset($_REQUEST['database']) ? $_REQUEST['database'] : null; |
605
|
|
|
$schema = $this->container->schema || isset($_REQUEST['schema']) ? $_REQUEST['schema'] : null; |
606
|
|
|
|
607
|
|
|
if ($server && $exclude_from !== 'server') { |
608
|
|
|
$href[] = 'server=' . urlencode($server); |
609
|
|
|
} |
610
|
|
|
if ($database && $exclude_from !== 'database') { |
611
|
|
|
$href[] = 'database=' . urlencode($database); |
612
|
|
|
} |
613
|
|
|
if ($schema && $exclude_from !== 'schema') { |
614
|
|
|
$href[] = 'schema=' . urlencode($schema); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
$this->href = htmlentities(implode('&', $href)); |
618
|
|
|
|
619
|
|
|
return $this->href; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Sets the form tracking variable. |
624
|
|
|
*/ |
625
|
|
|
public function setForm() |
626
|
|
|
{ |
627
|
|
|
$form = []; |
628
|
|
|
if ($this->container->server) { |
629
|
|
|
$form[] = '<input type="hidden" name="server" value="' . htmlspecialchars($this->container->server) . '" />'; |
630
|
|
|
} |
631
|
|
|
if ($this->container->database) { |
632
|
|
|
$form[] = '<input type="hidden" name="database" value="' . htmlspecialchars($this->container->database) . '" />'; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
if ($this->container->schema) { |
636
|
|
|
$form[] = '<input type="hidden" name="schema" value="' . htmlspecialchars($this->container->schema) . '" />'; |
637
|
|
|
} |
638
|
|
|
$this->form = implode("\n", $form); |
639
|
|
|
|
640
|
|
|
return $this->form; |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* A function to recursively strip slashes. Used to |
645
|
|
|
* enforce magic_quotes_gpc being off. |
646
|
|
|
* |
647
|
|
|
* @param mixed $var The variable to strip (passed by reference) |
648
|
|
|
*/ |
649
|
|
|
public function stripVar(&$var) |
650
|
|
|
{ |
651
|
|
|
if (is_array($var)) { |
652
|
|
|
foreach ($var as $k => $v) { |
653
|
|
|
$this->stripVar($var[$k]); |
654
|
|
|
|
655
|
|
|
/* magic_quotes_gpc escape keys as well ...*/ |
656
|
|
|
if (is_string($k)) { |
657
|
|
|
$ek = stripslashes($k); |
658
|
|
|
if ($ek !== $k) { |
659
|
|
|
$var[$ek] = $var[$k]; |
660
|
|
|
unset($var[$k]); |
661
|
|
|
} |
662
|
|
|
} |
663
|
|
|
} |
664
|
|
|
} else { |
665
|
|
|
$var = stripslashes($var); |
666
|
|
|
} |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Converts a PHP.INI size variable to bytes. Taken from publically available |
671
|
|
|
* function by Chris DeRose, here: http://www.php.net/manual/en/configuration.directives.php#ini.file-uploads. |
672
|
|
|
* |
673
|
|
|
* @param mixed $strIniSize The PHP.INI variable |
674
|
|
|
* |
675
|
|
|
* @return bool|float|int size in bytes, false on failure |
676
|
|
|
*/ |
677
|
|
|
public function inisizeToBytes($strIniSize) |
678
|
|
|
{ |
679
|
|
|
// This function will take the string value of an ini 'size' parameter, |
680
|
|
|
// and return a double (64-bit float) representing the number of bytes |
681
|
|
|
// that the parameter represents. Or false if $strIniSize is unparseable. |
682
|
|
|
$a_IniParts = []; |
683
|
|
|
|
684
|
|
|
if (!is_string($strIniSize)) { |
685
|
|
|
return false; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
if (!preg_match('/^(\d+)([bkm]*)$/i', $strIniSize, $a_IniParts)) { |
689
|
|
|
return false; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
$nSize = (float) $a_IniParts[1]; |
693
|
|
|
$strUnit = strtolower($a_IniParts[2]); |
694
|
|
|
|
695
|
|
|
switch ($strUnit) { |
696
|
|
|
case 'm': |
697
|
|
|
return $nSize * (float) 1048576; |
698
|
|
|
case 'k': |
699
|
|
|
return $nSize * (float) 1024; |
700
|
|
|
case 'b': |
701
|
|
|
default: |
702
|
|
|
return $nSize; |
703
|
|
|
} |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
public function getRequestVars($subject = '') |
707
|
|
|
{ |
708
|
|
|
$v = []; |
709
|
|
|
if (!empty($subject)) { |
710
|
|
|
$v['subject'] = $subject; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
if ($this->_server_id !== null && $subject != 'root') { |
714
|
|
|
$v['server'] = $this->_server_id; |
715
|
|
|
if ($this->_database !== null && $subject != 'server') { |
716
|
|
|
$v['database'] = $this->_database; |
717
|
|
|
if (isset($_REQUEST['schema']) && $subject != 'database') { |
718
|
|
|
$v['schema'] = $_REQUEST['schema']; |
719
|
|
|
} |
720
|
|
|
} |
721
|
|
|
} |
722
|
|
|
//$this->prtrace($v); |
723
|
|
|
return $v; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
public function icon($icon) |
727
|
|
|
{ |
728
|
|
|
return $this->container->utils->icon($icon); |
729
|
|
|
|
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
/** |
733
|
|
|
* Function to escape command line parameters. |
734
|
|
|
* |
735
|
|
|
* @param string $str The string to escape |
736
|
|
|
* |
737
|
|
|
* @return string The escaped string |
738
|
|
|
*/ |
739
|
|
|
public function escapeShellArg($str) |
740
|
|
|
{ |
741
|
|
|
//$data = $this->getDatabaseAccessor(); |
742
|
|
|
$lang = $this->lang; |
743
|
|
|
|
744
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
745
|
|
|
// Due to annoying PHP bugs, shell arguments cannot be escaped |
746
|
|
|
// (command simply fails), so we cannot allow complex objects |
747
|
|
|
// to be dumped. |
748
|
|
|
if (preg_match('/^[_.[:alnum:]]+$/', $str)) { |
749
|
|
|
return $str; |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
return $this->halt($lang['strcannotdumponwindows']); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
return escapeshellarg($str); |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
/** |
759
|
|
|
* Function to escape command line programs. |
760
|
|
|
* |
761
|
|
|
* @param string $str The string to escape |
762
|
|
|
* |
763
|
|
|
* @return string The escaped string |
764
|
|
|
*/ |
765
|
|
|
public function escapeShellCmd($str) |
766
|
|
|
{ |
767
|
|
|
$data = $this->getDatabaseAccessor(); |
768
|
|
|
|
769
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
770
|
|
|
$data->fieldClean($str); |
771
|
|
|
|
772
|
|
|
return '"' . $str . '"'; |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
return escapeshellcmd($str); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Save the given SQL script in the history |
780
|
|
|
* of the database and server. |
781
|
|
|
* |
782
|
|
|
* @param string $script the SQL script to save |
783
|
|
|
*/ |
784
|
|
|
public function saveScriptHistory($script) |
785
|
|
|
{ |
786
|
|
|
list($usec, $sec) = explode(' ', microtime()); |
787
|
|
|
$time = ((float) $usec + (float) $sec); |
788
|
|
|
|
789
|
|
|
$server = $this->container->server ? $this->container->server : $_REQUEST['server']; |
790
|
|
|
$database = $this->container->database ? $this->container->database : $_REQUEST['database']; |
791
|
|
|
|
792
|
|
|
$_SESSION['history'][$server][$database]["${time}"] = [ |
793
|
|
|
'query' => $script, |
794
|
|
|
'paginate' => !isset($_REQUEST['paginate']) ? 'f' : 't', |
795
|
|
|
'queryid' => $time, |
796
|
|
|
]; |
797
|
|
|
} |
798
|
|
|
} |
799
|
|
|
|