Completed
Push — master ( abcfbd...cc0839 )
by Nazar
04:23 queued 13s
created

databases::admin_databases_patch()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 5.3846
cc 8
eloc 26
nc 8
nop 1
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
			!in_array($_POST['type'], static::admin_databases_get_engines())
59
		) {
60
			throw new ExitException(400);
61
		}
62
		$Config         = Config::instance();
63
		$databases      = &$Config->db;
64
		$database_index = $route_ids[0];
65
		if (!isset($databases[$database_index])) {
66
			throw new ExitException(404);
67
		}
68
		$database = &$databases[$database_index];
69
		// Maybe, we are changing database mirror
70
		if (isset($route_ids[1])) {
71
			if (!isset($database['mirrors'][$route_ids[1]])) {
72
				throw new ExitException(404);
73
			}
74
			$database = &$database['mirrors'][$route_ids[1]];
75
		} elseif ($database_index == 0) {
76
			throw new ExitException(400);
77
		}
78
		$database['host']     = $_POST['host'];
79
		$database['type']     = $_POST['type'];
80
		$database['prefix']   = $_POST['prefix'];
81
		$database['name']     = $_POST['name'];
82
		$database['user']     = $_POST['user'];
83
		$database['password'] = $_POST['password'];
84
		$database['charset']  = $_POST['charset'];
85
		if (!$Config->save()) {
86
			throw new ExitException(500);
87
		}
88
	}
89
	/**
90
	 * Create database or database mirror
91
	 *
92
	 * @param int[] $route_ids
93
	 *
94
	 * @throws ExitException
95
	 */
96
	static function admin_databases_post ($route_ids) {
97
		if (
98
			!isset($_POST['mirror'], $_POST['host'], $_POST['type'], $_POST['prefix'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['charset']) ||
99
			!in_array($_POST['type'], static::admin_databases_get_engines())
100
		) {
101
			throw new ExitException(400);
102
		}
103
		$Config    = Config::instance();
104
		$databases = &$Config->db;
105
		// Maybe, we are adding database mirror
106
		if (isset($route_ids[0])) {
107
			if (!isset($databases[$route_ids[0]])) {
108
				throw new ExitException(404);
109
			}
110
			$databases = &$databases[$route_ids[0]]['mirrors'];
111
		}
112
		$databases[] = [
113
			'mirror'   => $_POST['mirror'],
114
			'host'     => $_POST['host'],
115
			'type'     => $_POST['type'],
116
			'prefix'   => $_POST['prefix'],
117
			'name'     => $_POST['name'],
118
			'user'     => $_POST['user'],
119
			'password' => $_POST['password'],
120
			'charset'  => $_POST['charset']
121
		];
122
		if (!$Config->save()) {
123
			throw new ExitException(500);
124
		}
125
	}
126
	/**
127
	 * Delete database or database mirror
128
	 *
129
	 * @param int[] $route_ids
130
	 *
131
	 * @throws ExitException
132
	 */
133
	static function admin_databases_delete ($route_ids) {
134
		if (!isset($route_ids[0])) {
135
			throw new ExitException(400);
136
		}
137
		$Config         = Config::instance();
138
		$databases      = &$Config->db;
139
		$database_index = $route_ids[0];
140
		if (!isset($databases[$database_index])) {
141
			throw new ExitException(404);
142
		}
143
		// Maybe, we are deleting database mirror
144
		if (isset($route_ids[1])) {
145
			if (!isset($databases[$database_index]['mirrors'][$route_ids[1]])) {
146
				throw new ExitException(404);
147
			}
148
			unset($databases[$database_index]['mirrors'][$route_ids[1]]);
149
		} elseif ($database_index == 0) {
150
			throw new ExitException(400);
151
		} else {
152
			static::admin_databases_delete_check_usages($database_index);
153
			unset($databases[$database_index]);
154
		}
155
		if (!$Config->save()) {
156
			throw new ExitException(500);
157
		}
158
	}
159
	protected static function admin_databases_delete_check_usages ($database_index) {
160
		$Config  = Config::instance();
161
		$used_by = [];
162
		foreach ($Config->components['modules'] as $module => $module_data) {
163
			if (isset($module_data['db']) && is_array($module_data['db'])) {
164
				foreach ($module_data['db'] as $index) {
165
					if ($index == $database_index) {
166
						$used_by[] = $module;
167
					}
168
				}
169
			}
170
		}
171
		if ($used_by) {
172
			throw new ExitException(
173
				Language::instance()->db_used_by_modules.': '.implode(', ', $used_by),
174
				409
175
			);
176
		}
177
	}
178
	/**
179
	 * Get array of available database engines
180
	 */
181
	static function admin_databases_engines () {
182
		Page::instance()->json(
183
			static::admin_databases_get_engines()
184
		);
185
	}
186
	/**
187
	 * @return string[]
188
	 */
189
	protected static function admin_databases_get_engines () {
190
		return _mb_substr(get_files_list(ENGINES.'/DB', '/^[^_].*?\.php$/i', 'f'), 0, -4);
191
	}
192
	/**
193
	 * Test database connection
194
	 *
195
	 * @throws ExitException
196
	 */
197
	static function admin_databases_test () {
198
		$engines = static::admin_databases_get_engines();
199
		if (
200
			!isset($_POST['type'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['host'], $_POST['charset']) ||
201
			!in_array($_POST['type'], $engines, true)
202
		) {
203
			throw new ExitException(400);
204
		}
205
		errors_off();
206
		$engine_class = "\\cs\\DB\\$_POST[type]";
207
		/**
208
		 * @var \cs\DB\_Abstract $connection
209
		 */
210
		$connection = new $engine_class(
211
			$_POST['name'],
212
			$_POST['user'],
213
			$_POST['password'],
214
			$_POST['host'],
215
			$_POST['charset']
216
		);
217
		errors_on();
218
		if (!$connection->connected()) {
219
			throw new ExitException(500);
220
		}
221
	}
222
	/**
223
	 * Get database settings
224
	 */
225
	static function admin_databases_get_settings () {
226
		$Config = Config::instance();
227
		Page::instance()->json(
228
			[
229
				'db_balance'        => $Config->core['db_balance'],
230
				'db_mirror_mode'    => $Config->core['db_mirror_mode'],
231
				'simple_admin_mode' => $Config->core['simple_admin_mode'],
232
				'applied'           => $Config->cancel_available()
233
			]
234
		);
235
	}
236
	/**
237
	 * Apply database settings
238
	 *
239
	 * @throws ExitException
240
	 */
241
	static function admin_databases_apply_settings () {
242
		static::admin_databases_settings_common();
243
		if (!Config::instance()->apply()) {
244
			throw new ExitException(500);
245
		}
246
	}
247
	/**
248
	 * @throws ExitException
249
	 */
250
	protected static function admin_databases_settings_common () {
251
		if (!isset(
252
			$_POST['db_balance'],
253
			$_POST['db_mirror_mode']
254
		)
255
		) {
256
			throw new ExitException(400);
257
		}
258
		$Config                         = Config::instance();
259
		$Config->core['db_balance']     = (int)(bool)$_POST['db_balance'];
260
		$Config->core['db_mirror_mode'] = (int)(bool)$_POST['db_mirror_mode'];
261
	}
262
	/**
263
	 * Save database settings
264
	 *
265
	 * @throws ExitException
266
	 */
267
	static function admin_databases_save_settings () {
268
		static::admin_databases_settings_common();
269
		if (!Config::instance()->save()) {
270
			throw new ExitException(500);
271
		}
272
	}
273
	/**
274
	 * Cancel database settings
275
	 *
276
	 * @throws ExitException
277
	 */
278
	static function admin_databases_cancel_settings () {
279
		if (!Config::instance()->cancel()) {
280
			throw new ExitException(500);
281
		}
282
	}
283
}
284