1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin 6.1.3 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Database; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use PHPPgAdmin\Traits\HelperTrait; |
11
|
|
|
use Slim\Container; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @file |
15
|
|
|
* Class to represent a database connection |
16
|
|
|
* |
17
|
|
|
* Id: Connection.php,v 1.15 2008/02/18 21:42:47 ioguix Exp $ |
18
|
|
|
*/ |
19
|
|
|
class Connection |
20
|
|
|
{ |
21
|
|
|
use HelperTrait; |
22
|
|
|
|
23
|
|
|
public $conn; |
24
|
|
|
|
25
|
|
|
public $platform = 'UNKNOWN'; |
26
|
|
|
|
27
|
|
|
protected $container; |
28
|
|
|
|
29
|
|
|
protected $server_info; |
30
|
|
|
|
31
|
|
|
protected $version_dictionary = [ |
32
|
|
|
'13' => 'Postgres13', |
33
|
|
|
'12' => 'Postgres12', |
34
|
|
|
'11' => 'Postgres11', |
35
|
|
|
'10' => 'Postgres10', |
36
|
|
|
'9.7' => 'Postgres96', |
37
|
|
|
'9.6' => 'Postgres96', |
38
|
|
|
'9.5' => 'Postgres95', |
39
|
|
|
'9.4' => 'Postgres94', |
40
|
|
|
'9.3' => 'Postgres93', |
41
|
|
|
'9.2' => 'Postgres92', |
42
|
|
|
'9.1' => 'Postgres91', |
43
|
|
|
'9.0' => 'Postgres90', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
// The backend platform. Set to UNKNOWN by default. |
47
|
|
|
private $_connection_result; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates a new connection. Will actually make a database connection. |
51
|
|
|
* |
52
|
|
|
* @param array $server_info |
53
|
|
|
* @param string $database database name |
54
|
|
|
* @param Container $container |
55
|
|
|
* @param int $fetchMode Defaults to associative. Override for different behaviour |
56
|
|
|
*/ |
57
|
|
|
public function __construct($server_info, $database, $container, $fetchMode = \ADODB_FETCH_ASSOC) |
58
|
|
|
{ |
59
|
|
|
$host = $server_info['host']; |
60
|
|
|
$port = $server_info['port']; |
61
|
|
|
$sslmode = $server_info['sslmode']; |
62
|
|
|
$user = $server_info['username']; |
63
|
|
|
$password = $server_info['password']; |
64
|
|
|
|
65
|
|
|
$this->server_info = $server_info; |
66
|
|
|
|
67
|
|
|
$this->container = $container; |
68
|
|
|
|
69
|
|
|
$this->conn = \ADONewConnection('postgres9'); |
70
|
|
|
//$this->conn->debug = true; |
71
|
|
|
$this->conn->setFetchMode($fetchMode); |
72
|
|
|
|
73
|
|
|
// Ignore host if null |
74
|
|
|
if (null === $host || '' === $host) { |
75
|
|
|
if (null !== $port && '' !== $port) { |
76
|
|
|
$pghost = ':' . $port; |
77
|
|
|
} else { |
78
|
|
|
$pghost = ''; |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
$pghost = \sprintf( |
82
|
|
|
'%s:%s', |
83
|
|
|
$host, |
84
|
|
|
$port |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Add sslmode to $pghost as needed |
89
|
|
|
if (('disable' === $sslmode) || ('allow' === $sslmode) || ('prefer' === $sslmode) || ('require' === $sslmode)) { |
90
|
|
|
$pghost .= ':' . $sslmode; |
91
|
|
|
} elseif ('legacy' === $sslmode) { |
92
|
|
|
$pghost .= ' requiressl=1'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$this->conn->connect($pghost, $user, $password, $database); |
97
|
|
|
//$this->prtrace($this->conn); |
98
|
|
|
} catch (Exception $e) { |
99
|
|
|
dump($e); |
100
|
|
|
$this->prtrace($e->getMessage(), $e->getTrace()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getConnectionResult() |
105
|
|
|
{ |
106
|
|
|
return $this->_connection_result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets the name of the correct database driver to use. As a side effect, |
111
|
|
|
* sets the platform. |
112
|
|
|
* |
113
|
|
|
* @param string $description A description of the database and version (returns by reference) |
114
|
|
|
* |
115
|
|
|
* @return string The driver. e.g. Postgres96 |
116
|
|
|
*/ |
117
|
|
|
public function getDriver(&$description) |
118
|
|
|
{ |
119
|
|
|
$version = null; |
120
|
|
|
|
121
|
|
|
if ($this->conn->_connectionID) { |
122
|
|
|
$v = \pg_version($this->conn->_connectionID); |
123
|
|
|
|
124
|
|
|
//\PhpConsole\Handler::getInstance()->debug($v, 'pg_version'); |
125
|
|
|
|
126
|
|
|
if (isset($v['server'])) { |
127
|
|
|
$version = $v['server']; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
// If we didn't manage to get the version without a query, query... |
131
|
|
|
if (!isset($version)) { |
132
|
|
|
$adodb = new ADOdbBase($this->conn, $this->container, $this->server_info); |
133
|
|
|
|
134
|
|
|
$sql = 'SELECT VERSION() AS version'; |
135
|
|
|
$field = $adodb->selectField($sql, 'version'); |
136
|
|
|
|
137
|
|
|
// Check the platform, if it's mingw, set it |
138
|
|
|
if (\preg_match('/ mingw /i', $field)) { |
139
|
|
|
$this->platform = 'MINGW'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$params = \explode(' ', $field); |
143
|
|
|
|
144
|
|
|
if (!isset($params[1])) { |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$version = $params[1]; // eg. 8.4.4 |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$description = \sprintf( |
152
|
|
|
'PostgreSQL %s', |
153
|
|
|
$version |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$version_parts = \explode('.', $version); |
157
|
|
|
|
158
|
|
|
if ((int) (10 <= $version_parts[0])) { |
159
|
|
|
$major_version = $version_parts[0]; |
160
|
|
|
} else { |
161
|
|
|
$major_version = \implode('.', [$version_parts[0], $version_parts[1]]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
//$this->prtrace(['pg_version' => pg_version($this->conn->_connectionID), 'version' => $version, 'major_version' => $major_version]); |
165
|
|
|
// Detect version and choose appropriate database driver |
166
|
|
|
if (\array_key_exists($major_version, $this->version_dictionary)) { |
167
|
|
|
return $this->version_dictionary[$major_version]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// if major version is less than 9 return null, we don't support it |
171
|
|
|
if (9 > (int) \mb_substr($version, 0, 1)) { |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// If unknown version, then default to latest driver |
176
|
|
|
return 'Postgres'; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the last error in the connection. |
181
|
|
|
* |
182
|
|
|
* @return string Error string |
183
|
|
|
*/ |
184
|
|
|
public function getLastError() |
185
|
|
|
{ |
186
|
|
|
return \pg_last_error($this->conn->_connectionID); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|