Passed
Push — master ( 172cf1...660559 )
by
unknown
17:32
created

PostgreSql   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 197
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPostgreSqlVersion() 0 15 2
A checkLibpqVersion() 0 28 3
A checkDefaultDatabaseServerCharset() 0 18 2
A getStatus() 0 14 2
A checkDefaultDatabaseCharset() 0 23 2
A isValidDatabaseName() 0 3 2
A checkDatabaseName() 0 13 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Core\Database\Connection;
20
use TYPO3\CMS\Core\Database\ConnectionPool;
21
use TYPO3\CMS\Core\Messaging\FlashMessage;
22
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Check database configuration status for PostgreSQL
27
 *
28
 * This class is a hardcoded requirement check for the database server.
29
 *
30
 * The status messages and title *must not* include HTML, use plain
31
 * text only. The return values of this class are not bound to HTML
32
 * and can be used in different scopes (eg. as json array).
33
 *
34
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
35
 */
36
class PostgreSql extends AbstractPlatform
37
{
38
    /**
39
     * Minimum supported PostgreSQL Server version
40
     *
41
     * @var string
42
     */
43
    protected $minimumPostgreSQLVerion = '9.2';
44
45
    /**
46
     * Minimum supported libpq version
47
     * @var string
48
     */
49
    protected $minimumLibPQVersion = '9.0';
50
51
    /**
52
     * Charset of the database that should be fulfilled
53
     * @var array
54
     */
55
    protected $databaseCharsetToCheck = [
56
        'utf8',
57
    ];
58
59
    /**
60
     * Charset of the database server that should be fulfilled
61
     * @var array
62
     */
63
    protected $databaseServerCharsetToCheck = [
64
        'utf8',
65
    ];
66
67
    /**
68
     * Get all status information as array with status objects
69
     *
70
     * @return FlashMessageQueue
71
     * @throws \Doctrine\DBAL\DBALException
72
     * @throws \InvalidArgumentException
73
     */
74
    public function getStatus(): FlashMessageQueue
75
    {
76
        $defaultConnection = GeneralUtility::makeInstance(ConnectionPool::class)
77
            ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
78
        if (strpos($defaultConnection->getServerVersion(), 'PostgreSQL') !== 0) {
79
            return $this->messageQueue;
80
        }
81
82
        $this->checkPostgreSqlVersion($defaultConnection);
83
        $this->checkLibpqVersion();
84
        $this->checkDefaultDatabaseCharset($defaultConnection);
85
        $this->checkDefaultDatabaseServerCharset($defaultConnection);
86
        $this->checkDatabaseName($defaultConnection);
87
        return $this->messageQueue;
88
    }
89
90
    /**
91
     * Check minimum PostgreSQL version
92
     *
93
     * @param Connection $connection to the database to be checked
94
     */
95
    protected function checkPostgreSqlVersion(Connection $connection)
96
    {
97
        preg_match('/PostgreSQL ((\d+\.)*(\d+\.)*\d+)/', $connection->getServerVersion(), $match);
98
        $currentPostgreSqlVersion = $match[1];
99
        if (version_compare($currentPostgreSqlVersion, $this->minimumPostgreSQLVerion, '<')) {
100
            $this->messageQueue->enqueue(new FlashMessage(
101
                'Your PostgreSQL version ' . $currentPostgreSqlVersion . ' is not supported. TYPO3 CMS does not run'
102
                . ' with this version. The minimum supported PostgreSQL version is ' . $this->minimumPostgreSQLVerion,
103
                'PostgreSQL Server version is unsupported',
104
                FlashMessage::ERROR
105
            ));
106
        } else {
107
            $this->messageQueue->enqueue(new FlashMessage(
108
                '',
109
                'PostgreSQL Server version is supported'
110
            ));
111
        }
112
    }
113
114
    /**
115
     * Check the version of ligpq within the PostgreSQL driver
116
     */
117
    protected function checkLibpqVersion()
118
    {
119
        if (!defined('PGSQL_LIBPQ_VERSION_STR')) {
120
            $this->messageQueue->enqueue(new FlashMessage(
121
                'It is not possible to retrieve your PostgreSQL libpq version. Please check the version'
122
                . ' in the "phpinfo" area of the "System environment" module in the install tool manually.'
123
                . ' This should be found in section "pdo_pgsql".'
124
                . ' You should have at least the following version of  PostgreSQL libpq installed: '
125
                . $this->minimumLibPQVersion,
126
                'PostgreSQL libpq version cannot be determined',
127
                FlashMessage::WARNING
128
            ));
129
        } else {
130
            preg_match('/PostgreSQL ((\d+\.)*(\d+\.)*\d+)/', \PGSQL_LIBPQ_VERSION_STR, $match);
131
            $currentPostgreSqlLibpqVersion = $match[1];
132
133
            if (version_compare($currentPostgreSqlLibpqVersion, $this->minimumLibPQVersion, '<')) {
134
                $this->messageQueue->enqueue(new FlashMessage(
135
                    'Your PostgreSQL libpq version "' . $currentPostgreSqlLibpqVersion . '" is unsupported.'
136
                    . ' TYPO3 CMS does not run with this version. The minimum supported libpq version is '
137
                    . $this->minimumLibPQVersion,
138
                    'PostgreSQL libpq version is unsupported',
139
                    FlashMessage::ERROR
140
                ));
141
            } else {
142
                $this->messageQueue->enqueue(new FlashMessage(
143
                    '',
144
                    'PostgreSQL libpq version is supported'
145
                ));
146
            }
147
        }
148
    }
149
150
    /**
151
     * Checks the character set of the database and reports an error if it is not utf-8.
152
     *
153
     * @param Connection $connection to the database to be checked
154
     */
155
    public function checkDefaultDatabaseCharset(Connection $connection): void
156
    {
157
        $defaultDatabaseCharset = $connection->executeQuery(
158
            'SELECT pg_catalog.pg_encoding_to_char(pg_database.encoding) from pg_database where datname = ?',
159
            [$connection->getDatabase()],
160
            [\PDO::PARAM_STR]
161
        )
162
            ->fetch();
163
164
        if (!in_array(strtolower($defaultDatabaseCharset['pg_encoding_to_char']), $this->databaseCharsetToCheck, true)) {
165
            $this->messageQueue->enqueue(new FlashMessage(
166
                sprintf(
167
                    'Checking database character set failed, got key "%s" instead of "%s"',
168
                    $defaultDatabaseCharset,
169
                    implode(' or ', $this->databaseCharsetToCheck)
170
                ),
171
                'PostgreSQL database character set check failed',
172
                FlashMessage::ERROR
173
            ));
174
        } else {
175
            $this->messageQueue->enqueue(new FlashMessage(
176
                '',
177
                sprintf('PostgreSQL database uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
178
            ));
179
        }
180
    }
181
182
    /**
183
     * Checks the character set of the database server and reports an info if it is not utf-8.
184
     *
185
     * @param Connection $connection to the database to be checked
186
     */
187
    public function checkDefaultDatabaseServerCharset(Connection $connection): void
188
    {
189
        $defaultServerCharset = $connection->executeQuery('SHOW SERVER_ENCODING')->fetch();
190
191
        if (!in_array(strtolower($defaultServerCharset['server_encoding']), $this->databaseCharsetToCheck, true)) {
192
            $this->messageQueue->enqueue(new FlashMessage(
193
                sprintf(
194
                    'Checking server character set failed, got key "%s" instead of "%s"',
195
                    $defaultServerCharset,
196
                    implode(' or ', $this->databaseServerCharsetToCheck)
197
                ),
198
                'PostgreSQL database character set check failed',
199
                FlashMessage::INFO
200
            ));
201
        } else {
202
            $this->messageQueue->enqueue(new FlashMessage(
203
                '',
204
                sprintf('PostgreSQL server default uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
205
            ));
206
        }
207
    }
208
209
    /**
210
     * Validate the database name
211
     *
212
     * @param string $databaseName
213
     * @return bool
214
     */
215
    public static function isValidDatabaseName(string $databaseName): bool
216
    {
217
        return strlen($databaseName) <= static::SCHEMA_NAME_MAX_LENGTH && preg_match('/^(?!pg_)[a-zA-Z0-9\$_]*$/', $databaseName);
218
    }
219
220
    protected function checkDatabaseName(Connection $connection): void
221
    {
222
        if (static::isValidDatabaseName($connection->getDatabase())) {
223
            return;
224
        }
225
226
        $this->messageQueue->enqueue(
227
            new FlashMessage(
228
                'The given database name must not be longer than ' . static::SCHEMA_NAME_MAX_LENGTH . ' characters'
229
                . ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)'
230
                . ' and underscores (_) and does not start with "pg_".',
231
                'Database name not valid',
232
                FlashMessage::ERROR
233
            )
234
        );
235
    }
236
}
237