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