Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Connection.php « classes « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a27a147043d1a853235431b38faa5da0dcc5cd5d (plain)
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
216
217
<?php

/**
 * PHPPgAdmin 6.1.3
 */

namespace PHPPgAdmin;

use ADODB_pdo;
use ADODB_postgres9;
use PHPPgAdmin\Traits\HelperTrait;

/**
 * @file
 * Class to represent a database connection
 *
 * Id: Connection.php,v 1.15 2008/02/18 21:42:47 ioguix Exp $
 */
class Connection
{
    use HelperTrait;

    public $conn;

    public $platform = 'UNKNOWN';

    /**
     * @var string
     */
    public $driver;

    protected $container;

    protected $server_info;

    protected $version_dictionary = [
        '13' => 'Postgres13',
        '12' => 'Postgres12',
        '11' => 'Postgres11',
        '10' => 'Postgres10',
        '9.7' => 'Postgres96',
        '9.6' => 'Postgres96',
        '9.5' => 'Postgres95',
        '9.4' => 'Postgres94',
        '9.3' => 'Postgres93',
        '9.2' => 'Postgres92',
        '9.1' => 'Postgres91',
        '9.0' => 'Postgres90',
    ];

    /**
     * @var string
     */
    private $pgVersion;

    /**
     * @var string
     */
    private $_captured_error;

    private $adodb_driver = 'postgres9';

    // The backend platform.  Set to UNKNOWN by default.
    private $_connection_result;

    /**
     * Creates a new connection.  Will actually make a database connection.
     *
     * @param array          $server_info
     * @param string         $database    database name
     * @param ContainerUtils $container
     * @param int            $fetchMode   Defaults to associative.  Override for different behaviour
     */
    public function __construct($server_info, $database, $container, $fetchMode = ADODB_FETCH_ASSOC)
    {
        $host = $server_info['host'];
        $port = $server_info['port'];
        $sslmode = $server_info['sslmode'];
        $user = $server_info['username'];
        $password = $server_info['password'];

        $this->server_info = $server_info;

        $this->container = $container;

        $this->conn = 'pdo' === $this->adodb_driver ?
                $this->getPDOConnection($host, $port, $sslmode, $database, $user, $password, $fetchMode) :
                $this->getPG9Connection($host, $port, $sslmode, $database, $user, $password, $fetchMode);
        $this->conn->setFetchMode($fetchMode);
        //$this->prtrace($this->conn);
    }

    public function getVersion(): string
    {
        return $this->pgVersion;
    }

    /**
     * Gets the name of the correct database driver to use.  As a side effect,
     * sets the platform.
     *
     * @param string $description A description of the database and version (returns by reference)
     *
     * @return string The driver. e.g. Postgres96
     */
    public function getDriver(&$description)
    {
        if (!$this->conn->IsConnected()) {
            return null;
        }
        $serverInfo = $this->conn->ServerInfo();
        dump($serverInfo);
        $this->pgVersion = $serverInfo['version'];
        $description = \sprintf(
            'PostgreSQL %s',
            $this->pgVersion
        );

        $version_parts = \explode('.', $this->pgVersion);

        if ((int) (10 <= $version_parts[0])) {
            $major_version = $version_parts[0];
        } else {
            $major_version = \implode('.', [$version_parts[0], $version_parts[1]]);
        }

        // if major version is less than 9 return null, we don't support it
        if (9 > (float) $major_version) {
            $this->driver = null;

            return null;
        }

        $this->driver = 'Postgres';
        //$this->prtrace(['pg_version' => pg_version($this->conn->_connectionID), 'version' => $version, 'major_version' => $major_version]);
        // Detect version and choose appropriate database driver
        if (\array_key_exists($major_version, $this->version_dictionary)) {
            $this->driver = $this->version_dictionary[$major_version];
        }

        // If unknown version, then default to latest driver
        return $this->driver;
    }

    /**
     * Get the last error in the connection.
     *
     * @return string Error string
     */
    public function getLastError()
    {
        return $this->conn->ErrorMsg();
    }

    private function getPG9Connection(
        string $host,
        int $port,
        string $sslmode,
        ?string $database,
        ?string $user,
        ?string $password,
        int $fetchMode = \ADODB_FETCH_ASSOC
    ): ADODB_postgres9 {
        $this->conn = ADONewConnection('postgres9');
        $this->conn->setFetchMode($fetchMode);
        // Ignore host if null
        if (null === $host || '' === $host) {
            if (null !== $port && '' !== $port) {
                $pghost = ':' . $port;
            } else {
                $pghost = '';
            }
        } else {
            $pghost = \sprintf(
                '%s:%s',
                $host,
                $port
            );
        }

        // Add sslmode to $pghost as needed
        if (('disable' === $sslmode) || ('allow' === $sslmode) || ('prefer' === $sslmode) || ('require' === $sslmode)) {
            $pghost .= ':' . $sslmode;
        } elseif ('legacy' === $sslmode) {
            $pghost .= ' requiressl=1';
        }
        \ob_start();
        $this->_connection_result = $this->conn->connect($pghost, $user, $password, $database);

        $this->_captured_error = \ob_get_clean();

        return $this->conn;
    }

    private function getPDOConnection(
        string $host,
        int $port,
        string $sslmode,
        ?string $database,
        ?string $user,
        ?string $password,
        int $fetchMode = \ADODB_FETCH_ASSOC
    ): ADODB_pdo {
        $this->conn = ADONewConnection('pdo');
        $this->conn->setFetchMode($fetchMode);
        $dsnString = \sprintf(
            'pgsql:host=%s;port=%d;dbname=%s;sslmode=%s;application_name=PHPPgAdmin6',
            $host,
            $port,
            $database,
            $sslmode
        );
        $this->conn->connect($dsnString, $user, $password);

        return $this->conn;
    }
}