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