Total Complexity | 68 |
Total Lines | 429 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FirewallController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FirewallController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class FirewallController extends BaseController |
||
28 | { |
||
29 | /** |
||
30 | * Prepares index page |
||
31 | */ |
||
32 | public function indexAction(): void |
||
131 | } |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Prepares forms to edit firewall rules |
||
136 | * |
||
137 | * @param string $networkId |
||
138 | */ |
||
139 | public function modifyAction(string $networkId = ''): void |
||
161 | } |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Save request from the form |
||
166 | */ |
||
167 | public function saveAction(): void |
||
168 | { |
||
169 | if (! $this->request->isPost()) { |
||
170 | return; |
||
171 | } |
||
172 | |||
173 | $this->db->begin(); |
||
174 | $data = $this->request->getPost(); |
||
175 | $networkId = $this->request->getPost('id'); |
||
176 | // Update network filters Network Filter |
||
177 | $filterRecordId = $this->updateNetworkFilters($networkId, $data); |
||
178 | if (empty($filterRecordId)) { |
||
179 | $this->view->success = false; |
||
180 | $this->db->rollback(); |
||
181 | |||
182 | return; |
||
183 | } |
||
184 | |||
185 | // If it was new entity we will reload page with new ID |
||
186 | if (empty($data['id'])) { |
||
187 | $this->view->reload = "firewall/modify/$filterRecordId"; |
||
188 | } |
||
189 | |||
190 | // Update firewall rules Firewall |
||
191 | $data['id'] = $filterRecordId; |
||
192 | if (! $this->updateFirewallRules($data)) { |
||
193 | $this->view->success = false; |
||
194 | $this->db->rollback(); |
||
195 | |||
196 | return; |
||
197 | } |
||
198 | |||
199 | $this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
||
200 | $this->view->success = true; |
||
201 | $this->db->commit(); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Fills Network Filter record |
||
206 | * |
||
207 | * @param string $networkId |
||
208 | * @param array $data POST parameters array |
||
209 | * |
||
210 | * @return string update result |
||
211 | */ |
||
212 | private function updateNetworkFilters(string $networkId, array $data): string |
||
213 | { |
||
214 | $filterRecord = NetworkFilters::findFirstById($networkId); |
||
215 | if ($filterRecord === null) { |
||
216 | $filterRecord = new NetworkFilters(); |
||
217 | } |
||
218 | |||
219 | $calculator = new Cidr(); |
||
220 | // Fills Network Filter record |
||
221 | foreach ($filterRecord as $name => $value) { |
||
222 | switch ($name) { |
||
223 | case 'permit': |
||
224 | $filterRecord->$name = $calculator->cidr2network( |
||
225 | $data['network'], |
||
226 | intval($data['subnet']) |
||
227 | ) . '/' . $data['subnet']; |
||
228 | break; |
||
229 | case 'deny': |
||
230 | $filterRecord->$name = '0.0.0.0/0'; |
||
231 | break; |
||
232 | case 'local_network': |
||
233 | case 'newer_block_ip': |
||
234 | if (array_key_exists($name, $data) && $data[$name] === 'on') { |
||
235 | $filterRecord->$name = 1; |
||
236 | } else { |
||
237 | $filterRecord->$name = 0; |
||
238 | } |
||
239 | break; |
||
240 | default: |
||
241 | if (array_key_exists($name, $data)) { |
||
242 | $filterRecord->$name = $data[$name]; |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | if ($filterRecord->save() === false) { |
||
248 | $errors = $filterRecord->getMessages(); |
||
249 | $this->flash->error(implode('<br>', $errors)); |
||
250 | |||
251 | return ''; |
||
252 | } |
||
253 | |||
254 | return $filterRecord->toArray()['id']; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Updates firewall rules |
||
259 | * |
||
260 | * @param array $data POST parameters array |
||
261 | * |
||
262 | * @return bool update result |
||
263 | */ |
||
264 | private function updateFirewallRules(array $data): bool |
||
265 | { |
||
266 | // Get default rules |
||
267 | $defaultRules = FirewallRules::getDefaultRules(); |
||
268 | $countDefaultRules = 0; |
||
269 | foreach ($defaultRules as $key => $value) { |
||
270 | foreach ($value['rules'] as $rule) { |
||
271 | $countDefaultRules++; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | // Delete outdated records |
||
276 | $parameters = [ |
||
277 | 'conditions' => 'networkfilterid=:networkfilterid:', |
||
278 | 'bind' => [ |
||
279 | 'networkfilterid' => $data['id'], |
||
280 | ], |
||
281 | ]; |
||
282 | $firewallRules = FirewallRules::find($parameters); |
||
283 | $currentRulesCount = $firewallRules->count(); |
||
284 | |||
285 | $needUpdateFirewallRules = false; |
||
286 | while ($countDefaultRules < $currentRulesCount) { |
||
287 | $firewallRules->next(); |
||
288 | if ($firewallRules->current()->delete() === false) { |
||
289 | $errors = $firewallRules->getMessages(); |
||
290 | $this->flash->error(implode('<br>', $errors)); |
||
291 | $this->view->success = false; |
||
292 | |||
293 | return false; |
||
294 | } |
||
295 | $currentRulesCount--; |
||
296 | $needUpdateFirewallRules = true; |
||
297 | } |
||
298 | |||
299 | if ($needUpdateFirewallRules) { |
||
300 | $firewallRules = FirewallRules::find($parameters); |
||
301 | } |
||
302 | $rowId = 0; |
||
303 | foreach ($defaultRules as $key => $value) { |
||
304 | foreach ($value['rules'] as $rule) { |
||
305 | if ($firewallRules->offsetExists($rowId)) { |
||
306 | $newRule = $firewallRules->offsetGet($rowId); |
||
307 | } else { |
||
308 | $newRule = new FirewallRules(); |
||
309 | } |
||
310 | $newRule->networkfilterid = $data['id']; |
||
311 | $newRule->protocol = $rule['protocol']; |
||
312 | $newRule->portfrom = $rule['portfrom']; |
||
313 | $newRule->portto = $rule['portto']; |
||
314 | $newRule->category = $key; |
||
315 | $newRule->portFromKey = $rule['portFromKey']; |
||
316 | $newRule->portToKey = $rule['portToKey']; |
||
317 | |||
318 | if (array_key_exists('rule_' . $key, $data) && $data['rule_' . $key]) { |
||
319 | $newRule->action = $data['rule_' . $key] === 'on' ? 'allow' : 'block'; |
||
320 | } else { |
||
321 | $newRule->action = 'block'; |
||
322 | } |
||
323 | $newRule->description = "$newRule->action connection from network: {$data['network']} / {$data['subnet']}"; |
||
324 | |||
325 | if ($newRule->save() === false) { |
||
326 | $errors = $newRule->getMessages(); |
||
327 | $this->flash->error(implode('<br>', $errors)); |
||
328 | $this->view->success = false; |
||
329 | |||
330 | return false; |
||
331 | } |
||
332 | $rowId++; |
||
333 | } |
||
334 | } |
||
335 | |||
336 | return true; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Deletes NetworkFilters record |
||
341 | * |
||
342 | * @param string $networkId |
||
343 | */ |
||
344 | public function deleteAction(string $networkId = ''): void |
||
345 | { |
||
346 | $this->db->begin(); |
||
347 | $filterRecord = NetworkFilters::findFirstById($networkId); |
||
348 | |||
349 | $errors = null; |
||
350 | if ($filterRecord !== null && ! $filterRecord->delete()) { |
||
351 | $errors = $filterRecord->getMessages(); |
||
352 | } |
||
353 | |||
354 | if ($errors) { |
||
355 | $this->flash->warning(implode('<br>', $errors)); |
||
356 | $this->db->rollback(); |
||
357 | } else { |
||
358 | $this->db->commit(); |
||
359 | } |
||
360 | |||
361 | $this->forward('firewall/index'); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Enables Fail2Ban and Firewall |
||
366 | */ |
||
367 | public function enableAction(): void |
||
368 | { |
||
369 | $fail2BanEnabled = PbxSettings::findFirstByKey(PbxSettings::PBX_FAIL2BAN_ENABLED); |
||
370 | if ($fail2BanEnabled === null) { |
||
371 | $fail2BanEnabled = new PbxSettings(); |
||
372 | $fail2BanEnabled->key = PbxSettings::PBX_FAIL2BAN_ENABLED; |
||
373 | } |
||
374 | $fail2BanEnabled->value = '1'; |
||
375 | if ($fail2BanEnabled->save() === false) { |
||
376 | $errors = $fail2BanEnabled->getMessages(); |
||
377 | $this->flash->warning(implode('<br>', $errors)); |
||
378 | $this->view->success = false; |
||
379 | |||
380 | return; |
||
381 | } |
||
382 | |||
383 | $firewallEnabled = PbxSettings::findFirstByKey(PbxSettings::PBX_FIREWALL_ENABLED); |
||
384 | if ($firewallEnabled === null) { |
||
385 | $firewallEnabled = new PbxSettings(); |
||
386 | $firewallEnabled->key = PbxSettings::PBX_FAIL2BAN_ENABLED; |
||
387 | } |
||
388 | $firewallEnabled->value = '1'; |
||
389 | if ($firewallEnabled->save() === false) { |
||
390 | $errors = $firewallEnabled->getMessages(); |
||
391 | $this->flash->warning(implode('<br>', $errors)); |
||
392 | $this->view->success = false; |
||
393 | |||
394 | return; |
||
395 | } |
||
396 | $this->view->success = true; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Disables Fail2Ban and Firewall |
||
401 | */ |
||
402 | public function disableAction(): void |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Compare two network entries for sorting |
||
436 | * |
||
437 | * @param array $a First network entry |
||
438 | * @param array $b Second network entry |
||
439 | * @return int Returns -1 if $a should be placed before $b, |
||
440 | * 1 if $a should be placed after $b, |
||
441 | * 0 if they are considered equal |
||
442 | */ |
||
443 | private function sortArrayByNetwork(array $a, array $b): int |
||
458 |