Connection   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
c 0
b 0
f 0
dl 0
loc 185
rs 10
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 34 5
A getPDOConnection() 0 15 1
B getPG9Connection() 0 34 10
A getLastError() 0 3 1
A __construct() 0 16 2
A getVersion() 0 3 1
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin;
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
class Connection
16
{
17
    use \PHPPgAdmin\Traits\HelperTrait;
18
19
    public $conn;
20
21
    public $platform = 'UNKNOWN';
22
23
    /**
24
     * @var string
25
     */
26
    public $driver;
27
28
    protected $container;
29
30
    protected $server_info;
31
32
    protected $version_dictionary = [
33
        '13' => 'Postgres13',
34
        '12' => 'Postgres12',
35
        '11' => 'Postgres11',
36
        '10' => 'Postgres10',
37
        '9.7' => 'Postgres96',
38
        '9.6' => 'Postgres96',
39
        '9.5' => 'Postgres95',
40
        '9.4' => 'Postgres94',
41
        '9.3' => 'Postgres93',
42
        '9.2' => 'Postgres92',
43
        '9.1' => 'Postgres91',
44
        '9.0' => 'Postgres90',
45
    ];
46
47
    /**
48
     * @var string
49
     */
50
    private $pgVersion;
51
52
    /**
53
     * @var string
54
     */
55
    private $_captured_error;
56
57
    private $adodb_driver = 'postgres9';
58
59
    // The backend platform.  Set to UNKNOWN by default.
60
    private $_connection_result;
61
62
    /**
63
     * Creates a new connection.  Will actually make a database connection.
64
     *
65
     * @param array                      $server_info
66
     * @param string                     $database    database name
67
     * @param \PHPPgAdmin\ContainerUtils $container
68
     * @param int                        $fetchMode   Defaults to associative.  Override for different behaviour
69
     */
70
    public function __construct($server_info, $database, $container, $fetchMode = ADODB_FETCH_ASSOC)
71
    {
72
        $host = $server_info['host'];
73
        $port = $server_info['port'];
74
        $sslmode = $server_info['sslmode'];
75
        $user = $server_info['username'];
76
        $password = $server_info['password'];
77
78
        $this->server_info = $server_info;
79
80
        $this->container = $container;
81
82
        $this->conn = 'pdo' === $this->adodb_driver ?
0 ignored issues
show
introduced by
The condition 'pdo' === $this->adodb_driver is always false.
Loading history...
83
                $this->getPDOConnection($host, $port, $sslmode, $database, $user, $password, $fetchMode) :
84
                $this->getPG9Connection($host, $port, $sslmode, $database, $user, $password, $fetchMode);
85
        $this->conn->setFetchMode($fetchMode);
0 ignored issues
show
Bug introduced by
$fetchMode of type integer is incompatible with the type The expected by parameter $mode of ADOConnection::SetFetchMode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $this->conn->setFetchMode(/** @scrutinizer ignore-type */ $fetchMode);
Loading history...
86
87
        //$this->prtrace($this->conn);
88
    }
89
90
    public function getVersion(): string
91
    {
92
        return $this->pgVersion;
93
    }
94
95
    /**
96
     * Gets the name of the correct database driver to use.  As a side effect,
97
     * sets the platform.
98
     *
99
     * @param string $description A description of the database and version (returns by reference)
100
     *
101
     * @return string The driver. e.g. Postgres96
102
     */
103
    public function getDriver(&$description)
104
    {
105
        if (!$this->conn->IsConnected()) {
106
            return null;
107
        }
108
        $serverInfo = $this->conn->ServerInfo();
109
        $this->dump($serverInfo);
110
        $this->pgVersion = $serverInfo['version'];
111
        $description = "PostgreSQL {$this->pgVersion}";
112
113
        $version_parts = \explode('.', $this->pgVersion);
114
115
        if ((int) (10 <= $version_parts[0])) {
116
            $major_version = $version_parts[0];
117
        } else {
118
            $major_version = \implode('.', [$version_parts[0], $version_parts[1]]);
119
        }
120
121
        // if major version is less than 9 return null, we don't support it
122
        if (9 > (float) $major_version) {
123
            $this->driver = null;
124
125
            return null;
126
        }
127
128
        $this->driver = 'Postgres';
129
        //$this->prtrace(['pg_version' => pg_version($this->conn->_connectionID), 'version' => $version, 'major_version' => $major_version]);
130
        // Detect version and choose appropriate database driver
131
        if (\array_key_exists($major_version, $this->version_dictionary)) {
132
            $this->driver = $this->version_dictionary[$major_version];
133
        }
134
135
        // If unknown version, then default to latest driver
136
        return $this->driver;
137
    }
138
139
    /**
140
     * Get the last error in the connection.
141
     *
142
     * @return string Error string
143
     */
144
    public function getLastError()
145
    {
146
        return $this->conn->ErrorMsg();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->conn->ErrorMsg() also could return the type true which is incompatible with the documented return type string.
Loading history...
147
    }
148
149
    private function getPG9Connection(
150
        string $host,
151
        int $port,
152
        string $sslmode,
153
        ?string $database,
154
        ?string $user,
155
        ?string $password,
156
        int $fetchMode = \ADODB_FETCH_ASSOC
157
    ): \ADODB_postgres9 {
158
        $this->conn = ADONewConnection('postgres9');
159
        $this->conn->setFetchMode($fetchMode);
160
        // Ignore host if null
161
        if (null === $host || '' === $host) {
162
            if (null !== $port && '' !== $port) {
0 ignored issues
show
introduced by
The condition '' !== $port is always true.
Loading history...
163
                $pghost = ':' . $port;
164
            } else {
165
                $pghost = '';
166
            }
167
        } else {
168
            $pghost = "{$host}:{$port}";
169
        }
170
171
        // Add sslmode to $pghost as needed
172
        if (('disable' === $sslmode) || ('allow' === $sslmode) || ('prefer' === $sslmode) || ('require' === $sslmode)) {
173
            $pghost .= ':' . $sslmode;
174
        } elseif ('legacy' === $sslmode) {
175
            $pghost .= ' requiressl=1';
176
        }
177
        \ob_start();
178
        $this->_connection_result = $this->conn->connect($pghost, $user, $password, $database);
179
180
        $this->_captured_error = \ob_get_clean();
181
182
        return $this->conn;
183
    }
184
185
    private function getPDOConnection(
186
        string $host,
187
        int $port,
188
        string $sslmode,
189
        ?string $database,
190
        ?string $user,
191
        ?string $password,
192
        int $fetchMode = \ADODB_FETCH_ASSOC
193
    ): \ADODB_pdo {
194
        $this->conn = ADONewConnection('pdo');
195
        $this->conn->setFetchMode($fetchMode);
196
        $dsnString = \sprintf('pgsql:host=%s;port=%d;dbname=%s;sslmode=%s;application_name=PHPPgAdmin6', $host, $port, $database, $sslmode);
197
        $this->conn->connect($dsnString, $user, $password);
198
199
        return $this->conn;
200
    }
201
}
202