1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @subpackage System module |
5
|
|
|
* @category modules |
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2015, Nazar Mokrynskyi |
8
|
|
|
* @license MIT License, see license.txt |
9
|
|
|
*/ |
10
|
|
|
namespace cs\modules\System\api\Controller\admin; |
11
|
|
|
use |
12
|
|
|
cs\Config, |
13
|
|
|
cs\Core, |
14
|
|
|
cs\ExitException, |
15
|
|
|
cs\Language, |
16
|
|
|
cs\Page; |
17
|
|
|
trait databases { |
18
|
|
|
/** |
19
|
|
|
* Get array of databases |
20
|
|
|
*/ |
21
|
|
|
static function admin_databases_get () { |
22
|
|
|
$Config = Config::instance(); |
23
|
|
|
$Core = Core::instance(); |
24
|
|
|
$databases = $Config->db; |
25
|
|
|
$databases[0] = array_merge( |
26
|
|
|
isset($databases[0]) ? $databases[0] : [], |
27
|
|
|
[ |
28
|
|
|
'host' => $Core->db_host, |
29
|
|
|
'type' => $Core->db_type, |
30
|
|
|
'prefix' => $Core->db_prefix, |
31
|
|
|
'name' => $Core->db_name, |
32
|
|
|
'user' => '', |
33
|
|
|
'charset' => $Core->db_charset, |
34
|
|
|
'mirrors' => [] |
35
|
|
|
] |
36
|
|
|
); |
37
|
|
|
foreach ($databases as $i => &$db) { |
38
|
|
|
$db['index'] = $i; |
39
|
|
|
foreach ($db['mirrors'] as $j => &$mirror) { |
40
|
|
|
$mirror['index'] = $j; |
41
|
|
|
} |
42
|
|
|
unset($j, $mirror); |
43
|
|
|
$db['mirrors'] = array_values($db['mirrors']); |
44
|
|
|
} |
45
|
|
|
unset($i, $db); |
46
|
|
|
Page::instance()->json(array_values($databases)); |
47
|
|
|
} |
48
|
|
|
/** |
49
|
|
|
* Update database or database mirror settings |
50
|
|
|
* |
51
|
|
|
* @param int[] $route_ids |
52
|
|
|
* |
53
|
|
|
* @throws ExitException |
54
|
|
|
*/ |
55
|
|
|
static function admin_databases_patch ($route_ids) { |
56
|
|
|
if ( |
57
|
|
|
!isset($route_ids[0], $_POST['host'], $_POST['type'], $_POST['prefix'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['charset']) || |
58
|
|
|
!strlen($_POST['host']) || |
59
|
|
|
!in_array($_POST['type'], static::admin_databases_get_engines()) |
60
|
|
|
) { |
61
|
|
|
throw new ExitException(400); |
62
|
|
|
} |
63
|
|
|
$Config = Config::instance(); |
64
|
|
|
$databases = &$Config->db; |
65
|
|
|
$database_index = $route_ids[0]; |
66
|
|
|
if (!isset($databases[$database_index])) { |
67
|
|
|
throw new ExitException(404); |
68
|
|
|
} |
69
|
|
|
$database = &$databases[$database_index]; |
70
|
|
|
// Maybe, we are changing database mirror |
71
|
|
|
if (isset($route_ids[1])) { |
72
|
|
|
if (!isset($database['mirrors'][$route_ids[1]])) { |
73
|
|
|
throw new ExitException(404); |
74
|
|
|
} |
75
|
|
|
$database = &$database['mirrors'][$route_ids[1]]; |
76
|
|
|
} elseif ($database_index == 0) { |
77
|
|
|
throw new ExitException(400); |
78
|
|
|
} |
79
|
|
|
$database['host'] = $_POST['host']; |
80
|
|
|
$database['type'] = $_POST['type']; |
81
|
|
|
$database['prefix'] = $_POST['prefix']; |
82
|
|
|
$database['name'] = $_POST['name']; |
83
|
|
|
$database['user'] = $_POST['user']; |
84
|
|
|
$database['password'] = $_POST['password']; |
85
|
|
|
$database['charset'] = $_POST['charset']; |
86
|
|
|
if (!$Config->save()) { |
87
|
|
|
throw new ExitException(500); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
/** |
91
|
|
|
* Create database or database mirror |
92
|
|
|
* |
93
|
|
|
* @param int[] $route_ids |
94
|
|
|
* |
95
|
|
|
* @throws ExitException |
96
|
|
|
*/ |
97
|
|
|
static function admin_databases_post ($route_ids) { |
98
|
|
|
if ( |
99
|
|
|
!isset($_POST['mirror'], $_POST['host'], $_POST['type'], $_POST['prefix'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['charset']) || |
100
|
|
|
!strlen($_POST['host']) || |
101
|
|
|
!in_array($_POST['type'], static::admin_databases_get_engines()) |
102
|
|
|
) { |
103
|
|
|
throw new ExitException(400); |
104
|
|
|
} |
105
|
|
|
$Config = Config::instance(); |
106
|
|
|
$databases = &$Config->db; |
107
|
|
|
// Maybe, we are adding database mirror |
108
|
|
|
if (isset($route_ids[0])) { |
109
|
|
|
if (!isset($databases[$route_ids[0]])) { |
110
|
|
|
throw new ExitException(404); |
111
|
|
|
} |
112
|
|
|
$databases = &$databases[$route_ids[0]]['mirrors']; |
113
|
|
|
} |
114
|
|
|
$databases[] = [ |
115
|
|
|
'mirror' => $_POST['mirror'], |
116
|
|
|
'host' => $_POST['host'], |
117
|
|
|
'type' => $_POST['type'], |
118
|
|
|
'prefix' => $_POST['prefix'], |
119
|
|
|
'name' => $_POST['name'], |
120
|
|
|
'user' => $_POST['user'], |
121
|
|
|
'password' => $_POST['password'], |
122
|
|
|
'charset' => $_POST['charset'] |
123
|
|
|
]; |
124
|
|
|
if (!$Config->save()) { |
125
|
|
|
throw new ExitException(500); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
/** |
129
|
|
|
* Delete database or database mirror |
130
|
|
|
* |
131
|
|
|
* @param int[] $route_ids |
132
|
|
|
* |
133
|
|
|
* @throws ExitException |
134
|
|
|
*/ |
135
|
|
|
static function admin_databases_delete ($route_ids) { |
136
|
|
|
if (!isset($route_ids[0])) { |
137
|
|
|
throw new ExitException(400); |
138
|
|
|
} |
139
|
|
|
$Config = Config::instance(); |
140
|
|
|
$databases = &$Config->db; |
141
|
|
|
$database_index = $route_ids[0]; |
142
|
|
|
if (!isset($databases[$database_index])) { |
143
|
|
|
throw new ExitException(404); |
144
|
|
|
} |
145
|
|
|
// Maybe, we are deleting database mirror |
146
|
|
|
if (isset($route_ids[1])) { |
147
|
|
|
if (!isset($databases[$database_index]['mirrors'][$route_ids[1]])) { |
148
|
|
|
throw new ExitException(404); |
149
|
|
|
} |
150
|
|
|
unset($databases[$database_index]['mirrors'][$route_ids[1]]); |
151
|
|
|
} elseif ($database_index == 0) { |
152
|
|
|
throw new ExitException(400); |
153
|
|
|
} else { |
154
|
|
|
static::admin_databases_delete_check_usages($database_index); |
155
|
|
|
unset($databases[$database_index]); |
156
|
|
|
} |
157
|
|
|
if (!$Config->save()) { |
158
|
|
|
throw new ExitException(500); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
protected static function admin_databases_delete_check_usages ($database_index) { |
162
|
|
|
$Config = Config::instance(); |
163
|
|
|
$used_by = []; |
164
|
|
|
foreach ($Config->components['modules'] as $module => $module_data) { |
165
|
|
|
if (isset($module_data['db']) && is_array($module_data['db'])) { |
166
|
|
|
foreach ($module_data['db'] as $index) { |
167
|
|
|
if ($index == $database_index) { |
168
|
|
|
$used_by[] = $module; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
if ($used_by) { |
174
|
|
|
throw new ExitException( |
175
|
|
|
Language::instance()->db_used_by_modules.': '.implode(', ', $used_by), |
176
|
|
|
409 |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
/** |
181
|
|
|
* Get array of available database engines |
182
|
|
|
*/ |
183
|
|
|
static function admin_databases_engines () { |
184
|
|
|
Page::instance()->json( |
185
|
|
|
static::admin_databases_get_engines() |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
/** |
189
|
|
|
* @return string[] |
190
|
|
|
*/ |
191
|
|
|
protected static function admin_databases_get_engines () { |
192
|
|
|
return _mb_substr(get_files_list(ENGINES.'/DB', '/^[^_].*?\.php$/i', 'f'), 0, -4); |
193
|
|
|
} |
194
|
|
|
/** |
195
|
|
|
* Test database connection |
196
|
|
|
* |
197
|
|
|
* @throws ExitException |
198
|
|
|
*/ |
199
|
|
|
static function admin_databases_test () { |
200
|
|
|
$engines = static::admin_databases_get_engines(); |
201
|
|
|
if ( |
202
|
|
|
!isset($_POST['type'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['host'], $_POST['charset']) || |
203
|
|
|
!in_array($_POST['type'], $engines, true) |
204
|
|
|
) { |
205
|
|
|
throw new ExitException(400); |
206
|
|
|
} |
207
|
|
|
errors_off(); |
208
|
|
|
$engine_class = "\\cs\\DB\\$_POST[type]"; |
209
|
|
|
/** |
210
|
|
|
* @var \cs\DB\_Abstract $connection |
211
|
|
|
*/ |
212
|
|
|
$connection = new $engine_class( |
213
|
|
|
$_POST['name'], |
214
|
|
|
$_POST['user'], |
215
|
|
|
$_POST['password'], |
216
|
|
|
$_POST['host'], |
217
|
|
|
$_POST['charset'] |
218
|
|
|
); |
219
|
|
|
errors_on(); |
220
|
|
|
if (!$connection->connected()) { |
221
|
|
|
throw new ExitException(500); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
/** |
225
|
|
|
* Get database settings |
226
|
|
|
*/ |
227
|
|
|
static function admin_databases_get_settings () { |
228
|
|
|
$Config = Config::instance(); |
229
|
|
|
Page::instance()->json( |
230
|
|
|
[ |
231
|
|
|
'db_balance' => $Config->core['db_balance'], |
232
|
|
|
'db_mirror_mode' => $Config->core['db_mirror_mode'], |
233
|
|
|
'simple_admin_mode' => $Config->core['simple_admin_mode'], |
234
|
|
|
'applied' => $Config->cancel_available() |
235
|
|
|
] |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
/** |
239
|
|
|
* Apply database settings |
240
|
|
|
* |
241
|
|
|
* @throws ExitException |
242
|
|
|
*/ |
243
|
|
|
static function admin_databases_apply_settings () { |
244
|
|
|
static::admin_databases_settings_common(); |
245
|
|
|
if (!Config::instance()->apply()) { |
246
|
|
|
throw new ExitException(500); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
/** |
250
|
|
|
* @throws ExitException |
251
|
|
|
*/ |
252
|
|
|
protected static function admin_databases_settings_common () { |
253
|
|
|
if (!isset( |
254
|
|
|
$_POST['db_balance'], |
255
|
|
|
$_POST['db_mirror_mode'] |
256
|
|
|
) |
257
|
|
|
) { |
258
|
|
|
throw new ExitException(400); |
259
|
|
|
} |
260
|
|
|
$Config = Config::instance(); |
261
|
|
|
$Config->core['db_balance'] = (int)(bool)$_POST['db_balance']; |
262
|
|
|
$Config->core['db_mirror_mode'] = (int)(bool)$_POST['db_mirror_mode']; |
263
|
|
|
} |
264
|
|
|
/** |
265
|
|
|
* Save database settings |
266
|
|
|
* |
267
|
|
|
* @throws ExitException |
268
|
|
|
*/ |
269
|
|
|
static function admin_databases_save_settings () { |
270
|
|
|
static::admin_databases_settings_common(); |
271
|
|
|
if (!Config::instance()->save()) { |
272
|
|
|
throw new ExitException(500); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
/** |
276
|
|
|
* Cancel database settings |
277
|
|
|
* |
278
|
|
|
* @throws ExitException |
279
|
|
|
*/ |
280
|
|
|
static function admin_databases_cancel_settings () { |
281
|
|
|
if (!Config::instance()->cancel()) { |
282
|
|
|
throw new ExitException(500); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|