| Conditions | 21 |
| Paths | 2400 |
| Total Lines | 102 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 96 | private function iptablesFormPost(string $type, string $commandLogFile, string $iptablesQueueFile) |
||
| 97 | { |
||
| 98 | $postParams = get_request()->getParsedBody(); |
||
| 99 | |||
| 100 | $con1 = ( |
||
| 101 | isset($postParams['ip']) && |
||
| 102 | filter_var(explode('/', $postParams['ip'])[0], FILTER_VALIDATE_IP) |
||
| 103 | ); |
||
| 104 | |||
| 105 | $con2 = ( |
||
| 106 | isset($postParams['port']) && |
||
| 107 | ( |
||
| 108 | is_numeric($postParams['port']) || |
||
| 109 | $postParams['port'] === 'all' || |
||
| 110 | $postParams['port'] === 'custom' |
||
| 111 | ) |
||
| 112 | ); |
||
| 113 | |||
| 114 | $con3 = ( |
||
| 115 | isset($postParams['subnet']) && |
||
| 116 | ( |
||
| 117 | is_numeric($postParams['subnet']) || |
||
| 118 | $postParams['subnet'] === 'null' |
||
| 119 | ) |
||
| 120 | ); |
||
| 121 | |||
| 122 | $con4 = ( |
||
| 123 | isset($postParams['protocol']) && |
||
| 124 | in_array($postParams['protocol'], ['tcp', 'udp', 'all']) |
||
| 125 | ); |
||
| 126 | |||
| 127 | $con5 = ( |
||
| 128 | isset($postParams['action']) && |
||
| 129 | in_array($postParams['action'], ['allow', 'deny']) |
||
| 130 | ); |
||
| 131 | |||
| 132 | if ($con1 && $con2 && $con3 && $con4 && $con5) { |
||
| 133 | $ip = $postParams['ip']; |
||
| 134 | $port = $postParams['port']; |
||
| 135 | $subnet = $postParams['subnet']; |
||
| 136 | $protocol = $postParams['protocol']; |
||
| 137 | $action = $postParams['action']; |
||
| 138 | $cPort = $postParams['port_custom'] ?? 'all'; |
||
| 139 | |||
| 140 | $isRemoval = false; |
||
| 141 | |||
| 142 | if (isset($postParams['remove']) && $postParams['remove'] === 'yes') { |
||
| 143 | $isRemoval = true; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ('custom' === $port) { |
||
| 147 | $port = $cPort; |
||
| 148 | } |
||
| 149 | |||
| 150 | $ipv = '4'; |
||
| 151 | |||
| 152 | if ('IPv6' === $type) { |
||
| 153 | $ipv = '6'; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The process of add or remove command string from two files: |
||
| 158 | * |
||
| 159 | * (1) The command file - |
||
| 160 | * This file is used on display the commands on the page |
||
| 161 | * Iptables Manager. |
||
| 162 | * (2) The queue file - |
||
| 163 | * This file is a bridge between Shieldon Firewall and Iptalbes. |
||
| 164 | * ipbales_bridge.sh will monitor this file, once commands come, |
||
| 165 | * transforming the commands into Iptables syntax commands and |
||
| 166 | * then execute the Iptables commands. |
||
| 167 | */ |
||
| 168 | if (!$isRemoval) { |
||
| 169 | $originCommandString = "add,$ipv,$ip,$subnet,$port,$protocol,$action"; |
||
| 170 | |||
| 171 | // Delete line from the log file. |
||
| 172 | $fileArr = file($commandLogFile); |
||
| 173 | |||
| 174 | if (is_array($fileArr)) { |
||
| 175 | $keyFound = array_search(trim($originCommandString), $fileArr); |
||
| 176 | unset($fileArr[$keyFound]); |
||
| 177 | |||
| 178 | $t = []; |
||
| 179 | $i = 0; |
||
| 180 | foreach ($fileArr as $f) { |
||
| 181 | $t[$i] = trim($f); |
||
| 182 | $i++; |
||
| 183 | } |
||
| 184 | file_put_contents($commandLogFile, implode(PHP_EOL, $t)); |
||
| 185 | } |
||
| 186 | |||
| 187 | $applyCommand = "delete,$ipv,$ip,$subnet,$port,$protocol,$action"; |
||
| 188 | |||
| 189 | file_put_contents($iptablesQueueFile, $applyCommand . "\n", FILE_APPEND | LOCK_EX); |
||
| 190 | |||
| 191 | // Becase we need system cronjob done, and then the web page will show the actual results. |
||
| 192 | sleep(10); |
||
| 193 | } else { |
||
| 194 | $applyCommand = "add,$ipv,$ip,$subnet,$port,$protocol,$action"; |
||
| 195 | |||
| 196 | file_put_contents($iptablesQueueFile, $applyCommand . "\n", FILE_APPEND | LOCK_EX); |
||
| 197 | sleep(1); |
||
| 198 | } |
||
| 298 |