Total Complexity | 40 |
Total Lines | 407 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like FilterTrait 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 FilterTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | trait FilterTrait |
||
23 | { |
||
24 | /** |
||
25 | * Enable or disable the filters. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $filterStatus = [ |
||
30 | /** |
||
31 | * Check how many pageviews an user made in a short period time. |
||
32 | * For example, limit an user can only view 30 pages in 60 minutes. |
||
33 | */ |
||
34 | 'frequency' => true, |
||
35 | |||
36 | /** |
||
37 | * If an user checks any internal link on your website, the user's |
||
38 | * browser will generate HTTP_REFERER information. |
||
39 | * When a user view many pages without HTTP_REFERER information meaning |
||
40 | * that the user MUST be a web crawler. |
||
41 | */ |
||
42 | 'referer' => false, |
||
43 | |||
44 | /** |
||
45 | * Most of web crawlers do not render JavaScript, they only get the |
||
46 | * content they want, so we can check whether the cookie can be created |
||
47 | * by JavaScript or not. |
||
48 | */ |
||
49 | 'cookie' => false, |
||
50 | |||
51 | /** |
||
52 | * Every unique user should only has a unique session, but if a user |
||
53 | * creates different sessions every connection... meaning that the |
||
54 | * user's browser doesn't support cookie. |
||
55 | * It is almost impossible that modern browsers not support cookie, |
||
56 | * therefore the user MUST be a web crawler. |
||
57 | */ |
||
58 | 'session' => false, |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * The status for Filters to reset. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $filterResetStatus = [ |
||
67 | 's' => false, // second. |
||
68 | 'm' => false, // minute. |
||
69 | 'h' => false, // hour. |
||
70 | 'd' => false, // day. |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Detect and analyze an user's behavior. |
||
75 | * |
||
76 | * @return int The response code. |
||
77 | */ |
||
78 | protected function filter(): int |
||
79 | { |
||
80 | $now = time(); |
||
81 | $isFlagged = false; |
||
82 | |||
83 | // Fetch an IP data from Shieldon log table. |
||
84 | $ipDetail = $this->driver->get($this->ip, 'filter'); |
||
85 | |||
86 | $ipDetail = $this->driver->parseData($ipDetail, 'filter'); |
||
87 | $logData = $ipDetail; |
||
88 | |||
89 | // Counting user pageviews. |
||
90 | foreach (array_keys($this->filterResetStatus) as $unit) { |
||
91 | |||
92 | // Each time unit will increase by 1. |
||
93 | $logData['pageviews_' . $unit] = $ipDetail['pageviews_' . $unit] + 1; |
||
94 | $logData['first_time_' . $unit] = $ipDetail['first_time_' . $unit]; |
||
95 | } |
||
96 | |||
97 | $logData['first_time_flag'] = $ipDetail['first_time_flag']; |
||
98 | |||
99 | if (!empty($ipDetail['ip'])) { |
||
100 | $logData['ip'] = $this->ip; |
||
101 | $logData['session'] = get_session()->get('id'); |
||
102 | $logData['hostname'] = $this->rdns; |
||
103 | $logData['last_time'] = $now; |
||
104 | |||
105 | // Filter: HTTP referrer information. |
||
106 | $filterReferer = $this->filterReferer($logData, $ipDetail, $isFlagged); |
||
107 | $isFlagged = $filterReferer['is_flagged']; |
||
108 | $logData = $filterReferer['log_data']; |
||
109 | |||
110 | if ($filterReferer['is_reject']) { |
||
111 | return self::RESPONSE_TEMPORARILY_DENY; |
||
|
|||
112 | } |
||
113 | |||
114 | // Filter: Session. |
||
115 | $filterSession = $this->filterSession($logData, $ipDetail, $isFlagged); |
||
116 | $isFlagged = $filterSession['is_flagged']; |
||
117 | $logData = $filterSession['log_data']; |
||
118 | |||
119 | if ($filterSession['is_reject']) { |
||
120 | return self::RESPONSE_TEMPORARILY_DENY; |
||
121 | } |
||
122 | |||
123 | // Filter: JavaScript produced cookie. |
||
124 | $filterCookie = $this->filterCookie($logData, $ipDetail, $isFlagged); |
||
125 | $isFlagged = $filterCookie['is_flagged']; |
||
126 | $logData = $filterCookie['log_data']; |
||
127 | |||
128 | if ($filterCookie['is_reject']) { |
||
129 | return self::RESPONSE_TEMPORARILY_DENY; |
||
130 | } |
||
131 | |||
132 | // Filter: frequency. |
||
133 | $filterFrequency = $this->filterFrequency($logData, $ipDetail, $isFlagged); |
||
134 | $isFlagged = $filterFrequency['is_flagged']; |
||
135 | $logData = $filterFrequency['log_data']; |
||
136 | |||
137 | if ($filterFrequency['is_reject']) { |
||
138 | return self::RESPONSE_TEMPORARILY_DENY; |
||
139 | } |
||
140 | |||
141 | // Is fagged as unusual beavior? Count the first time. |
||
142 | if ($isFlagged) { |
||
143 | $logData['first_time_flag'] = (!empty($logData['first_time_flag'])) ? $logData['first_time_flag'] : $now; |
||
144 | } |
||
145 | |||
146 | // Reset the flagged factor check. |
||
147 | if (!empty($ipDetail['first_time_flag'])) { |
||
148 | if ($now - $ipDetail['first_time_flag'] >= $this->properties['time_reset_limit']) { |
||
149 | $logData['flag_multi_session'] = 0; |
||
150 | $logData['flag_empty_referer'] = 0; |
||
151 | $logData['flag_js_cookie'] = 0; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $this->driver->save($this->ip, $logData, 'filter'); |
||
156 | |||
157 | } else { |
||
158 | |||
159 | // If $ipDetail[ip] is empty. |
||
160 | // It means that the user is first time visiting our webiste. |
||
161 | $this->InitializeFirstTimeFilter($logData); |
||
162 | } |
||
163 | |||
164 | return self::RESPONSE_ALLOW; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * When the user is first time visiting our webiste. |
||
169 | * Initialize the log data. |
||
170 | * |
||
171 | * @param array $logData The user's log data. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | protected function InitializeFirstTimeFilter($logData) |
||
176 | { |
||
177 | $now = time(); |
||
178 | |||
179 | $logData['ip'] = $this->ip; |
||
180 | $logData['session'] = get_session()->get('id'); |
||
181 | $logData['hostname'] = $this->rdns; |
||
182 | $logData['last_time'] = $now; |
||
183 | |||
184 | foreach (array_keys($this->filterResetStatus) as $unit) { |
||
185 | $logData['first_time_' . $unit] = $now; |
||
186 | } |
||
187 | |||
188 | $this->driver->save($this->ip, $logData, 'filter'); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Filter - Referer. |
||
193 | * |
||
194 | * @param array $logData IP data from Shieldon log table. |
||
195 | * @param array $ipData The IP log data. |
||
196 | * @param bool $isFlagged Is flagged as unusual behavior or not. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function filterReferer(array $logData, array $ipDetail, bool $isFlagged): array |
||
201 | { |
||
202 | $isReject = false; |
||
203 | |||
204 | if ($this->filterStatus['referer']) { |
||
205 | |||
206 | if ($logData['last_time'] - $ipDetail['last_time'] > $this->properties['interval_check_referer']) { |
||
207 | |||
208 | // Get values from data table. We will count it and save it back to data table. |
||
209 | // If an user is already in your website, it is impossible no referer when he views other pages. |
||
210 | $logData['flag_empty_referer'] = $ipDetail['flag_empty_referer'] ?? 0; |
||
211 | |||
212 | if (empty(get_request()->getHeaderLine('referer'))) { |
||
213 | $logData['flag_empty_referer']++; |
||
214 | $isFlagged = true; |
||
215 | } |
||
216 | |||
217 | // Ban this IP if they reached the limit. |
||
218 | if ($logData['flag_empty_referer'] > $this->properties['limit_unusual_behavior']['referer']) { |
||
219 | $this->action( |
||
220 | self::ACTION_TEMPORARILY_DENY, |
||
221 | self::REASON_EMPTY_REFERER |
||
222 | ); |
||
223 | $isReject = true; |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | return [ |
||
229 | 'is_flagged' => $isFlagged, |
||
230 | 'is_reject' => $isReject, |
||
231 | 'log_data' => $logData, |
||
232 | ]; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Filter - Session |
||
237 | * |
||
238 | * @param array $logData IP data from Shieldon log table. |
||
239 | * @param array $ipData The IP log data. |
||
240 | * @param bool $isFlagged Is flagged as unusual behavior or not. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | protected function filterSession(array $logData, array $ipDetail, bool $isFlagged): array |
||
279 | ]; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Filter - Cookie |
||
284 | * |
||
285 | * @param array $logData IP data from Shieldon log table. |
||
286 | * @param array $ipData The IP log data. |
||
287 | * @param bool $isFlagged Is flagged as unusual behavior or not. |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function filterCookie(array $logData, array $ipDetail, bool $isFlagged): array |
||
345 | ]; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Filter - Frequency |
||
350 | * |
||
351 | * @param array $logData IP data from Shieldon log table. |
||
352 | * @param array $ipData The IP log data. |
||
353 | * @param bool $isFlagged Is flagged as unusual behavior or not. |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | protected function filterFrequency(array $logData, array $ipDetail, bool $isFlagged): array |
||
429 | ]; |
||
430 | } |
||
432 |