Circle::rule()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 76
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 57
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 76
ccs 57
cts 57
cp 1
rs 8.3377
cc 6
nc 6
nop 0
crap 6

How to fix   Long Method   

Long Method

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:

1
<?php
2
// phpcs:disable Generic.Files.LineLength
3
/**
4
 * This file is part of the Shieldon package.
5
 *
6
 * (c) Terry L. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * php version 7.1.0
12
 *
13
 * @category  Web-security
14
 * @package   Shieldon
15
 * @author    Terry Lin <[email protected]>
16
 * @copyright 2019 terrylinooo
17
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
18
 * @link      https://github.com/terrylinooo/shieldon
19
 * @see       https://shieldon.io
20
 */
21
22
declare(strict_types=1);
23
24
namespace Shieldon\Firewall\Panel;
25
26
use Psr\Http\Message\ResponseInterface;
27
use Shieldon\Firewall\Panel\BaseController;
28
use Shieldon\Firewall\Kernel\Enum;
29
use ReflectionObject;
30
use function Shieldon\Firewall\__;
31
use function Shieldon\Firewall\get_request;
32
use function gethostbyaddr;
33
use function time;
34
35
/**
36
 * The data circle controller.
37
 */
38
class Circle extends BaseController
39
{
40
    /**
41
     *   Public methods       | Desctiotion
42
     *  ----------------------|---------------------------------------------
43
     *   rule                 | The page for rule table.
44
     *   filter               | The page for filter table.
45
     *   session              | The page for session table.
46
     *  ----------------------|---------------------------------------------
47
     */
48
49
    /**
50 3
     * Constructor
51
     */
52 3
    public function __construct()
53
    {
54
        parent::__construct();
55
    }
56
57
    /**
58
     * Rule table for current cycle.
59
     *
60 1
     * @return ResponseInterface
61
     */
62 1
    public function rule(): ResponseInterface
63
    {
64 1
        $postParams = get_request()->getParsedBody();
65
66 1
        if ($this->checkPostParamsExist('ip', 'action')) {
67 1
            $ip = $postParams['ip'];
68
            $action = $postParams['action'];
69 1
70
            $actionCode = [];
71 1
72 1
            $actionCode['temporarily_ban'] = Enum::ACTION_TEMPORARILY_DENY;
73 1
            $actionCode['permanently_ban'] = Enum::ACTION_DENY;
74
            $actionCode['allow'] = Enum::ACTION_ALLOW;
75
76 1
            switch ($action) {
77 1
                case 'temporarily_ban':
78 1
                case 'permanently_ban':
79 1
                case 'allow':
80
                    $logData = [];
81 1
82 1
                    $logData['log_ip']     = $ip;
83 1
                    $logData['ip_resolve'] = gethostbyaddr($ip);
84 1
                    $logData['time']       = time();
85 1
                    $logData['type']       = $actionCode[$action];
86
                    $logData['reason']     = Enum::REASON_MANUAL_BAN_DENIED;
87 1
88 1
                    $this->kernel->driver->save($ip, $logData, 'rule');
89
                    break;
90 1
91 1
                case 'remove':
92 1
                    $this->kernel->driver->delete($ip, 'rule');
93
                    break;
94
            }
95
        }
96 1
97 1
        $reasons = [
98 1
            Enum::REASON_MANUAL_BAN_DENIED              => __('panel', 'reason_manual_ban', 'Manually added by administrator'),
99 1
            Enum::REASON_IS_SEARCH_ENGINE_ALLOWED       => __('panel', 'reason_is_search_engine', 'Search engine bot'),
100 1
            Enum::REASON_IS_GOOGLE_ALLOWED              => __('panel', 'reason_is_google', 'Google bot'),
101 1
            Enum::REASON_IS_BING_ALLOWED                => __('panel', 'reason_is_bing', 'Bing bot'),
102 1
            Enum::REASON_IS_YAHOO_ALLOWED               => __('panel', 'reason_is_yahoo', 'Yahoo bot'),
103 1
            Enum::REASON_TOO_MANY_SESSIONS_DENIED       => __('panel', 'reason_too_many_sessions', 'Too many sessions'),
104 1
            Enum::REASON_TOO_MANY_ACCESSE_DENIED        => __('panel', 'reason_too_many_accesses', 'Too many accesses'),
105 1
            Enum::REASON_EMPTY_JS_COOKIE_DENIED         => __('panel', 'reason_empty_js_cookie', 'Unable to create JS cookies'),
106 1
            Enum::REASON_EMPTY_REFERER_DENIED           => __('panel', 'reason_empty_referer', 'Empty referrer'),
107 1
            Enum::REASON_REACH_DAILY_LIMIT_DENIED       => __('panel', 'reason_reached_limit_day', 'Daily limit reached'),
108 1
            Enum::REASON_REACH_HOURLY_LIMIT_DENIED      => __('panel', 'reason_reached_limit_hour', 'Hourly limit reached'),
109 1
            Enum::REASON_REACH_MINUTELY_LIMIT_DENIED    => __('panel', 'reason_reached_limit_minute', 'Minute limit reached'),
110 1
            Enum::REASON_REACH_SECONDLY_LIMIT_DENIED    => __('panel', 'reason_reached_limit_second', 'Second limit reached'),
111 1
            Enum::REASON_INVALID_IP_DENIED              => __('panel', 'reason_invalid_ip', 'Invalid IP address.'),
112 1
            Enum::REASON_DENY_IP_DENIED                 => __('panel', 'reason_deny_ip', 'Denied by IP component.'),
113 1
            Enum::REASON_ALLOW_IP_DENIED                => __('panel', 'reason_allow_ip', 'Allowed by IP component.'),
114 1
            Enum::REASON_COMPONENT_IP_DENIED            => __('panel', 'reason_component_ip', 'Denied by IP component.'),
115 1
            Enum::REASON_COMPONENT_RDNS_DENIED          => __('panel', 'reason_component_rdns', 'Denied by RDNS component.'),
116 1
            Enum::REASON_COMPONENT_HEADER_DENIED        => __('panel', 'reason_component_header', 'Denied by Header component.'),
117 1
            Enum::REASON_COMPONENT_USERAGENT_DENIED     => __('panel', 'reason_component_useragent', 'Denied by User Agent component.'),
118 1
            Enum::REASON_COMPONENT_TRUSTED_ROBOT_DENIED => __('panel', 'reason_component_trusted_robot', 'Identified as a fake search engine.'),
119
        ];
120 1
121 1
        $types = [
122 1
            Enum::ACTION_DENY             => 'DENY',
123 1
            Enum::ACTION_ALLOW            => 'ALLOW',
124 1
            Enum::ACTION_TEMPORARILY_DENY => 'CAPTCHA',
125
        ];
126 1
127
        $data = [];
128 1
129
        $data['rule_list'] = $this->kernel->driver->getAll('rule');
130 1
131 1
        $data['reason_mapping'] = $reasons;
132
        $data['type_mapping'] = $types;
133 1
134 1
        $data['title'] = __('panel', 'menu_data_circle', 'Data Circle') . ' - ';
135
        $data['title'] .= __('panel', 'title_circle_rule', 'Rule Table');
136 1
137
        return $this->renderPage('panel/table_rules', $data);
138
    }
139
140
    /**
141
     * IP filter table for current cycle.
142
     *
143
     * @return ResponseInterface
144 1
     */
145
    public function filter(): ResponseInterface
146 1
    {
147
        $data = [];
148 1
149
        $data['ip_log_list'] = $this->kernel->driver->getAll('filter');
150 1
151 1
        $data['title'] = __('panel', 'menu_data_circle', 'Data Circle') . ' - ';
152
        $data['title'] .= __('panel', 'title_circle_filter', 'Filter Table');
153 1
154
        return $this->renderPage('panel/table_filter_logs', $data);
155
    }
156
157
    /**
158
     * Session table for current cycle.
159
     *
160
     * @return ResponseInterface
161 1
     */
162
    public function session(): ResponseInterface
163 1
    {
164
        $data = [];
165 1
166
        $data['session_list'] = $this->kernel->driver->getAll('session');
167 1
168 1
        $data['is_session_limit']     = false;
169 1
        $data['session_limit_count']  = 0;
170 1
        $data['session_limit_period'] = 0;
171 1
        $data['online_count']         = 0;
172
        $data['expires']              = 0;
173 1
174 1
        $reflection = new ReflectionObject($this->kernel);
175 1
        $t = $reflection->getProperty('sessionLimit');
176 1
        $t->setAccessible(true);
177
        $sessionLimit = $t->getValue($this->kernel);
178 1
179 1
        $isLimitSession = false;
180 1
        $limitCount = 0;
181
        $limitPeriod = 0;
182 1
183 1
        if (!empty($sessionLimit['count'])) {
184 1
            $isLimitSession = true;
185 1
            $limitCount = $sessionLimit['count'];
186
            $limitPeriod = $sessionLimit['period'];
187
        }
188 1
189 1
        $data['is_session_limit']     = $isLimitSession;
190 1
        $data['session_limit_count']  = $limitCount;
191 1
        $data['session_limit_period'] = $limitPeriod;
192 1
        $data['online_count']         = count($data['session_list']);
193
        $data['expires']              = (int) $data['session_limit_period'] * 60;
194 1
195 1
        $data['title'] = __('panel', 'menu_data_circle', 'Data Circle') . ' - ';
196
        $data['title'] .= __('panel', 'title_circle_session', 'Session Table');
197 1
198
        return $this->renderPage('panel/table_sessions', $data);
199
    }
200
}
201