| Total Complexity | 67 |
| Total Lines | 419 |
| 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 |
||
| 16 | class FirewallController extends BaseController |
||
| 17 | { |
||
| 18 | |||
| 19 | |||
| 20 | /** |
||
| 21 | * Построение таблицы доступа к ресурсам системы |
||
| 22 | */ |
||
| 23 | public function indexAction(): void |
||
| 24 | { |
||
| 25 | $calculator = new Cidr(); |
||
| 26 | $localAddresses = []; |
||
| 27 | $localAddresses[] = '0.0.0.0/0'; |
||
| 28 | $conditions = 'disabled=0 AND internet=0'; // Нам нужны только локальные включенные сети |
||
| 29 | $networkInterfaces = LanInterfaces::find($conditions); |
||
| 30 | foreach ($networkInterfaces as $interface) { |
||
| 31 | if (empty($interface->ipaddr)) { |
||
| 32 | continue; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (strpos($interface->subnet, '.') === false) { |
||
| 36 | $localAddresses[] = $calculator->cidr2network( |
||
| 37 | $interface->ipaddr, |
||
| 38 | $interface->subnet |
||
| 39 | ) . '/' . $interface->subnet; |
||
| 40 | } else { |
||
| 41 | $cidr = $calculator->netmask2cidr($interface->subnet); |
||
| 42 | $localAddresses[] = $calculator->cidr2network($interface->ipaddr, $cidr) . '/' . $cidr; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | $defaultRules = FirewallRules::getDefaultRules(); |
||
| 47 | $networksTable = []; |
||
| 48 | $networkFilters = NetworkFilters::find(); |
||
| 49 | $networkFiltersStoredInDB = ($networkFilters->count() > 0); |
||
| 50 | foreach ($networkFilters as $filter) { |
||
| 51 | $networksTable[$filter->id]['id'] = $filter->id; |
||
| 52 | $networksTable[$filter->id]['description'] = $filter->description; |
||
| 53 | |||
| 54 | $permitParts = explode('/', $filter->permit); |
||
| 55 | |||
| 56 | if (strpos($permitParts[1], '.') === false) { |
||
| 57 | $networksTable[$filter->id]['network'] = $calculator->cidr2network( |
||
| 58 | $permitParts[0], |
||
| 59 | $permitParts[1] |
||
| 60 | ) . '/' . $permitParts[1]; |
||
| 61 | } else { |
||
| 62 | $cidr = $calculator->netmask2cidr($permitParts[1]); |
||
| 63 | $networksTable[$filter->id]['network'] = $calculator->cidr2network( |
||
| 64 | $permitParts[0], |
||
| 65 | $cidr |
||
| 66 | ) . '/' . $cidr; |
||
| 67 | } |
||
| 68 | $networksTable[$filter->id]['permanent'] = false; |
||
| 69 | |||
| 70 | |||
| 71 | // Заполним значениями по умолчанию |
||
| 72 | foreach ($defaultRules as $key => $value) { |
||
| 73 | $networksTable[$filter->id]['category'][$key] = [ |
||
| 74 | 'name' => empty($value['shortName']) ? $key : $value['shortName'], |
||
| 75 | 'action' => $value['action'], |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Заполним сохраненными ранее значениями |
||
| 80 | $firewallRules = $filter->FirewallRules; |
||
| 81 | foreach ($firewallRules as $rule) { |
||
| 82 | $networksTable[$filter->id]['category'][$rule->category]['action'] = $rule->action; |
||
| 83 | if ( ! array_key_exists('name', $networksTable[$filter->id]['category'][$rule->category])) { |
||
| 84 | $networksTable[$filter->id]['category'][$rule->category]['name'] = $rule->category; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // Добавиим фильтры по умолчанию, если они еще не добавлены. |
||
| 90 | foreach ($localAddresses as $localAddress) { |
||
| 91 | $existsPersistentRecord = false; |
||
| 92 | foreach ($networksTable as $key => $value) { |
||
| 93 | if ($value['network'] === $localAddress) { |
||
| 94 | $networksTable[$key]['permanent'] = true; |
||
| 95 | $existsPersistentRecord = true; |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | if ( ! $existsPersistentRecord) { |
||
| 100 | foreach ($defaultRules as $key => $value) { |
||
| 101 | $networksTableNewRecord['category'][$key] = [ |
||
| 102 | 'name' => $key, |
||
| 103 | 'action' => $networkFiltersStoredInDB ? 'block' : $value['action'], |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | $networksTableNewRecord['id'] = ''; |
||
| 107 | $networksTableNewRecord['permanent'] = true; |
||
| 108 | $networksTableNewRecord['network'] = $localAddress; |
||
| 109 | if ($localAddress === '0.0.0.0/0') { |
||
| 110 | $networksTableNewRecord['description'] = $this->translation->_('fw_AllNetworksRule'); |
||
| 111 | } else { |
||
| 112 | $networksTableNewRecord['description'] = $this->translation->_('fw_LocalNetworksRule'); |
||
| 113 | } |
||
| 114 | $networksTable[] = $networksTableNewRecord; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | usort($networksTable, [__CLASS__, 'sortArrayByNetwork']); |
||
| 119 | |||
| 120 | $this->view->rulesTable = $networksTable; |
||
| 121 | $this->view->PBXFirewallEnabled = PbxSettings::getValueByKey('PBXFirewallEnabled'); |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Форма редактирования карточки сетевого фильтра |
||
| 127 | * |
||
| 128 | * @param string $networkId |
||
| 129 | */ |
||
| 130 | public function modifyAction(string $networkId = ''): void |
||
| 131 | { |
||
| 132 | $networkFilter = NetworkFilters::findFirstById($networkId); |
||
| 133 | $firewallRules = FirewallRules::getDefaultRules(); |
||
| 134 | $data = $this->request->getPost(); |
||
| 135 | if ($networkFilter === null) { |
||
| 136 | $networkFilter = new NetworkFilters(); |
||
| 137 | $networkFilter->permit = empty($data['permit']) ? '0.0.0.0/0' : $data['permit']; |
||
| 138 | } else { |
||
| 139 | // Заполним сохраненными ранее значениями |
||
| 140 | foreach ($networkFilter->FirewallRules as $rule) { |
||
| 141 | $firewallRules[$rule->category]['action'] = $rule->action; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | $permitParts = explode('/', $networkFilter->permit); |
||
| 145 | |||
| 146 | $this->view->form = new FirewallEditForm( |
||
| 147 | $networkFilter, |
||
| 148 | ['network' => $permitParts[0], 'subnet' => $permitParts[1]] |
||
| 149 | ); |
||
| 150 | $this->view->firewallRules = $firewallRules; |
||
| 151 | $this->view->represent = $networkFilter->getRepresent(); |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Проверка на доступность номера |
||
| 157 | */ |
||
| 158 | public function saveAction(): void |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Заполним параметры записи Network Filter |
||
| 202 | * |
||
| 203 | * @param \MikoPBX\Common\Models\NetworkFilters $filterRecord |
||
| 204 | * @param array $data массив полей из POST запроса |
||
| 205 | * |
||
| 206 | * @return bool update result |
||
| 207 | */ |
||
| 208 | private function updateNetworkFilters(NetworkFilters $filterRecord, array $data): bool |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Updates firewall rules |
||
| 250 | * |
||
| 251 | * @param array $data POST parameters array |
||
| 252 | * |
||
| 253 | * @return bool update result |
||
| 254 | */ |
||
| 255 | private function updateFirewallRules(array $data): bool |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Удаление правил настройки firewall |
||
| 324 | * |
||
| 325 | * @param string $networkId |
||
| 326 | */ |
||
| 327 | public function deleteAction(string $networkId = '') |
||
| 328 | { |
||
| 329 | $this->db->begin(); |
||
| 330 | $filterRecord = NetworkFilters::findFirstById($networkId); |
||
| 331 | |||
| 332 | $errors = null; |
||
| 333 | if ($filterRecord !== null && ! $filterRecord->delete()) { |
||
| 334 | $errors = $filterRecord->getMessages(); |
||
| 335 | } |
||
| 336 | |||
| 337 | if ($errors) { |
||
| 338 | $this->flash->warning(implode('<br>', $errors)); |
||
| 339 | $this->db->rollback(); |
||
| 340 | } else { |
||
| 341 | $this->db->commit(); |
||
| 342 | } |
||
| 343 | |||
| 344 | $this->forward('firewall/index'); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Включение firewall |
||
| 349 | */ |
||
| 350 | public function enableAction(): void |
||
| 351 | { |
||
| 352 | $fail2BanEnabled = PbxSettings::findFirstByKey('PBXFail2BanEnabled'); |
||
| 353 | if ($fail2BanEnabled === null) { |
||
| 354 | $fail2BanEnabled = new PbxSettings(); |
||
| 355 | $fail2BanEnabled->key = 'PBXFail2BanEnabled'; |
||
| 356 | } |
||
| 357 | $fail2BanEnabled->value = '1'; |
||
| 358 | if ($fail2BanEnabled->save() === false) { |
||
| 359 | $errors = $fail2BanEnabled->getMessages(); |
||
| 360 | $this->flash->warning(implode('<br>', $errors)); |
||
| 361 | $this->view->success = false; |
||
| 362 | |||
| 363 | return; |
||
| 364 | } |
||
| 365 | |||
| 366 | $firewallEnabled = PbxSettings::findFirstByKey('PBXFirewallEnabled'); |
||
| 367 | if ($firewallEnabled === null) { |
||
| 368 | $firewallEnabled = new PbxSettings(); |
||
| 369 | $firewallEnabled->key = 'PBXFail2BanEnabled'; |
||
| 370 | } |
||
| 371 | $firewallEnabled->value = '1'; |
||
| 372 | if ($firewallEnabled->save() === false) { |
||
| 373 | $errors = $firewallEnabled->getMessages(); |
||
| 374 | $this->flash->warning(implode('<br>', $errors)); |
||
| 375 | $this->view->success = false; |
||
| 376 | |||
| 377 | return; |
||
| 378 | } |
||
| 379 | $this->view->success = true; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Выключение firewall |
||
| 384 | */ |
||
| 385 | public function disableAction(): void |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Метод сортировки, локальная сеть и 0 сеть всегда сверху списка должны быть |
||
| 419 | * |
||
| 420 | * @param $a |
||
| 421 | * @param $b |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | private function sortArrayByNetwork($a, $b): bool |
||
| 435 | } |
||
| 436 | } |