|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Storage\Database; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class DBUtil con utilidades de la BD |
|
29
|
|
|
* |
|
30
|
|
|
* @package SP\Storage |
|
31
|
|
|
*/ |
|
32
|
|
|
final class DatabaseUtil |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var array Tablas de la BBDD |
|
36
|
|
|
*/ |
|
37
|
|
|
public static $tables = [ |
|
38
|
|
|
'Client', |
|
39
|
|
|
'Category', |
|
40
|
|
|
'Tag', |
|
41
|
|
|
'UserGroup', |
|
42
|
|
|
'UserProfile', |
|
43
|
|
|
'User', |
|
44
|
|
|
'Account', |
|
45
|
|
|
'AccountToFavorite', |
|
46
|
|
|
'AccountFile', |
|
47
|
|
|
'AccountToUserGroup', |
|
48
|
|
|
'AccountHistory', |
|
49
|
|
|
'AccountToTag', |
|
50
|
|
|
'AccountToUser', |
|
51
|
|
|
'AuthToken', |
|
52
|
|
|
'Config', |
|
53
|
|
|
'CustomFieldType', |
|
54
|
|
|
'CustomFieldDefinition', |
|
55
|
|
|
'CustomFieldData', |
|
56
|
|
|
'EventLog', |
|
57
|
|
|
'PublicLink', |
|
58
|
|
|
'UserPassRecover', |
|
59
|
|
|
'UserToUserGroup', |
|
60
|
|
|
'Plugin', |
|
61
|
|
|
'Notification', |
|
62
|
|
|
'account_data_v', |
|
63
|
|
|
'account_search_v' |
|
64
|
|
|
]; |
|
65
|
|
|
/** |
|
66
|
|
|
* @var DBStorageInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
private $DBStorage; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* DatabaseUtil constructor. |
|
72
|
|
|
* |
|
73
|
|
|
* @param DBStorageInterface $DBStorage |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct(DBStorageInterface $DBStorage) |
|
76
|
|
|
{ |
|
77
|
|
|
|
|
78
|
|
|
$this->DBStorage = $DBStorage; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Comprobar que la base de datos existe. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $dbName |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function checkDatabaseTables($dbName) |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
|
|
$tables = implode(',', array_map(function ($value) { |
|
92
|
|
|
return '\'' . $value . '\''; |
|
93
|
|
|
}, self::$tables)); |
|
94
|
|
|
|
|
95
|
|
|
$query = /** @lang SQL */ |
|
96
|
|
|
'SELECT COUNT(*) |
|
97
|
|
|
FROM information_schema.tables |
|
98
|
|
|
WHERE table_schema = \'' . $dbName . '\' |
|
99
|
|
|
AND `table_name` IN (' . $tables . ')'; |
|
100
|
|
|
|
|
101
|
|
|
$numTables = (int)$this->DBStorage->getConnection()->query($query)->fetchColumn(); |
|
102
|
|
|
|
|
103
|
|
|
return $numTables === count(self::$tables); |
|
104
|
|
|
} catch (\Exception $e) { |
|
105
|
|
|
processException($e); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
public function checkDatabaseConnection() |
|
115
|
|
|
{ |
|
116
|
|
|
try { |
|
117
|
|
|
$this->DBStorage->getConnection(); |
|
118
|
|
|
|
|
119
|
|
|
return true; |
|
120
|
|
|
} catch (\Exception $e) { |
|
121
|
|
|
processException($e); |
|
122
|
|
|
|
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Obtener la información del servidor de base de datos |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getDBinfo() |
|
133
|
|
|
{ |
|
134
|
|
|
$dbinfo = []; |
|
135
|
|
|
|
|
136
|
|
|
try { |
|
137
|
|
|
$db = $this->DBStorage->getConnection(); |
|
138
|
|
|
|
|
139
|
|
|
$attributes = [ |
|
140
|
|
|
'SERVER_VERSION', |
|
141
|
|
|
'CLIENT_VERSION', |
|
142
|
|
|
'SERVER_INFO', |
|
143
|
|
|
'CONNECTION_STATUS', |
|
144
|
|
|
]; |
|
145
|
|
|
|
|
146
|
|
|
foreach ($attributes as $val) { |
|
147
|
|
|
$dbinfo[$val] = $db->getAttribute(constant('PDO::ATTR_' . $val)); |
|
148
|
|
|
} |
|
149
|
|
|
} catch (\Exception $e) { |
|
150
|
|
|
processException($e); |
|
151
|
|
|
|
|
152
|
|
|
logger($e->getMessage()); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $dbinfo; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Escapar una cadena de texto con funciones de mysqli. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $str string con la cadena a escapar |
|
162
|
|
|
* |
|
163
|
|
|
* @return string con la cadena escapada |
|
164
|
|
|
*/ |
|
165
|
|
|
public function escape($str) |
|
166
|
|
|
{ |
|
167
|
|
|
try { |
|
168
|
|
|
return $this->DBStorage->getConnection()->quote(trim($str)); |
|
169
|
|
|
} catch (\Exception $e) { |
|
170
|
|
|
processException($e); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $str; |
|
174
|
|
|
} |
|
175
|
|
|
} |