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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Validator;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Setup\Model\Installer;
use Magento\Setup\Module\ConnectionFactory;
/**
* Class DbValidator - validates DB related settings
*/
class DbValidator
{
/**
* Db prefix max length
*/
const DB_PREFIX_LENGTH = 5;
/**
* DB connection factory
*
* @var ConnectionFactory
*/
private $connectionFactory;
/**
* Constructor
*
* @param ConnectionFactory $connectionFactory
*/
public function __construct(ConnectionFactory $connectionFactory)
{
$this->connectionFactory = $connectionFactory;
}
/**
* Check if database table prefix is valid
*
* @param string $prefix
* @return boolean
* @throws \InvalidArgumentException
*/
public function checkDatabaseTablePrefix($prefix)
{
//The table prefix should contain only letters (a-z), numbers (0-9) or underscores (_);
// the first character should be a letter.
if ($prefix !== '' && !preg_match('/^([a-zA-Z])([[:alnum:]_]+)$/', $prefix)) {
throw new \InvalidArgumentException(
'Please correct the table prefix format, should contain only numbers, letters or underscores.'
.' The first character should be a letter.'
);
}
if (strlen($prefix) > self::DB_PREFIX_LENGTH) {
throw new \InvalidArgumentException(
'Table prefix length can\'t be more than ' . self::DB_PREFIX_LENGTH . ' characters.'
);
}
return true;
}
/**
* Checks Database Connection
*
* @param string $dbName
* @param string $dbHost
* @param string $dbUser
* @param string $dbPass
* @return boolean
* @throws \Magento\Setup\Exception
*/
public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '')
{
// establish connection to information_schema view to retrieve information about user and table privileges
$connection = $this->connectionFactory->create([
ConfigOptionsListConstants::KEY_NAME => 'information_schema',
ConfigOptionsListConstants::KEY_HOST => $dbHost,
ConfigOptionsListConstants::KEY_USER => $dbUser,
ConfigOptionsListConstants::KEY_PASSWORD => $dbPass,
ConfigOptionsListConstants::KEY_ACTIVE => true,
]);
if (!$connection) {
throw new \Magento\Setup\Exception('Database connection failure.');
}
$mysqlVersion = $connection->fetchOne('SELECT version()');
if ($mysqlVersion) {
if (preg_match('/^([0-9\.]+)/', $mysqlVersion, $matches)) {
if (isset($matches[1]) && !empty($matches[1])) {
if (version_compare($matches[1], Installer::MYSQL_VERSION_REQUIRED) < 0) {
throw new \Magento\Setup\Exception(
'Sorry, but we support MySQL version ' . Installer::MYSQL_VERSION_REQUIRED . ' or later.'
);
}
}
}
}
return $this->checkDatabaseName($connection, $dbName) && $this->checkDatabasePrivileges($connection, $dbName);
}
/**
* Checks if specified database exists and visible to current user
*
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param string $dbName
* @return bool
* @throws \Magento\Setup\Exception
*/
private function checkDatabaseName(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $dbName)
{
$query = "SHOW DATABASES";
$accessibleDbs = $connection->query($query)->fetchAll(\PDO::FETCH_COLUMN, 0);
foreach ($accessibleDbs as $accessibleDbName) {
if ($dbName == $accessibleDbName) {
return true;
}
}
throw new \Magento\Setup\Exception(
"Database '{$dbName}' does not exist "
."or specified database server user does not have privileges to access this database."
);
}
/**
* Checks database privileges
*
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param string $dbName
* @return bool
* @throws \Magento\Setup\Exception
*/
private function checkDatabasePrivileges(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $dbName)
{
$requiredPrivileges = [
'SELECT',
'INSERT',
'UPDATE',
'DELETE',
'CREATE',
'DROP',
'INDEX',
'ALTER',
'CREATE TEMPORARY TABLES',
'LOCK TABLES',
'EXECUTE',
'CREATE VIEW',
'SHOW VIEW',
'CREATE ROUTINE',
'ALTER ROUTINE',
'TRIGGER'
];
// check global privileges
$userPrivilegesQuery = "SELECT PRIVILEGE_TYPE FROM USER_PRIVILEGES "
. "WHERE REPLACE(GRANTEE, '\'', '') = current_user()";
$grantInfo = $connection->query($userPrivilegesQuery)->fetchAll(\PDO::FETCH_NUM);
if (empty(array_diff($requiredPrivileges, $this->parseGrantInfo($grantInfo)))) {
return true;
}
// check table privileges
$schemaPrivilegesQuery = "SELECT PRIVILEGE_TYPE FROM SCHEMA_PRIVILEGES " .
"WHERE '$dbName' LIKE TABLE_SCHEMA AND REPLACE(GRANTEE, '\'', '') = current_user()";
$grantInfo = $connection->query($schemaPrivilegesQuery)->fetchAll(\PDO::FETCH_NUM);
if (empty(array_diff($requiredPrivileges, $this->parseGrantInfo($grantInfo)))) {
return true;
}
$errorMessage = 'Database user does not have enough privileges. Please make sure '
. implode(', ', $requiredPrivileges) . " privileges are granted to table '{$dbName}'.";
throw new \Magento\Setup\Exception($errorMessage);
}
/**
* Parses query result
*
* @param array $grantInfo
* @return array
*/
private function parseGrantInfo(array $grantInfo)
{
$result = [];
foreach ($grantInfo as $grantRow) {
$result[] = $grantRow[0];
}
return $result;
}
}