Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Protector 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Protector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Protector |
||
23 | { |
||
24 | var $mydirname; |
||
|
|||
25 | |||
26 | var $_conn = null; |
||
27 | |||
28 | var $_conf = array(); |
||
29 | |||
30 | var $_conf_serialized = ''; |
||
31 | |||
32 | var $_bad_globals = array(); |
||
33 | |||
34 | var $message = ''; |
||
35 | |||
36 | var $warning = false; |
||
37 | |||
38 | var $error = false; |
||
39 | |||
40 | var $_doubtful_requests = array(); |
||
41 | |||
42 | var $_bigumbrella_doubtfuls = array(); |
||
43 | |||
44 | var $_dblayertrap_doubtfuls = array(); |
||
45 | |||
46 | var $_dblayertrap_doubtful_needles = array( |
||
47 | 'information_schema', 'select', "'", '"', |
||
48 | ); |
||
49 | |||
50 | var $_logged = false; |
||
51 | |||
52 | var $_done_badext = false; |
||
53 | |||
54 | var $_done_intval = false; |
||
55 | |||
56 | var $_done_dotdot = false; |
||
57 | |||
58 | var $_done_nullbyte = false; |
||
59 | |||
60 | var $_done_contami = false; |
||
61 | |||
62 | var $_done_isocom = false; |
||
63 | |||
64 | var $_done_union = false; |
||
65 | |||
66 | var $_done_dos = false; |
||
67 | |||
68 | var $_safe_badext = true; |
||
69 | |||
70 | var $_safe_contami = true; |
||
71 | |||
72 | var $_safe_isocom = true; |
||
73 | |||
74 | var $_safe_union = true; |
||
75 | |||
76 | var $_spamcount_uri = 0; |
||
77 | |||
78 | var $_should_be_banned_time0 = false; |
||
79 | |||
80 | var $_should_be_banned = false; |
||
81 | |||
82 | var $_dos_stage = null; |
||
83 | |||
84 | var $ip_matched_info = null; |
||
85 | |||
86 | var $last_error_type = 'UNKNOWN'; |
||
87 | |||
88 | // Constructor |
||
89 | function __construct() |
||
90 | { |
||
91 | $this->mydirname = 'protector'; |
||
92 | |||
93 | // Preferences from configs/cache |
||
94 | $this->_conf_serialized = @file_get_contents($this->get_filepath4confighcache()); |
||
95 | $this->_conf = @unserialize($this->_conf_serialized); |
||
96 | if (empty($this->_conf)) { |
||
97 | $this->_conf = array(); |
||
98 | } |
||
99 | |||
100 | if (!empty($this->_conf['global_disabled'])) { |
||
101 | return true; |
||
102 | } |
||
103 | |||
104 | // die if PHP_SELF XSS found (disabled in 2.53) |
||
105 | // if( preg_match( '/[<>\'";\n ]/' , @$_SERVER['PHP_SELF'] ) ) { |
||
106 | // $this->message .= "Invalid PHP_SELF '{$_SERVER['PHP_SELF']}' found.\n" ; |
||
107 | // $this->output_log( 'PHP_SELF XSS' ) ; |
||
108 | // die( 'invalid PHP_SELF' ) ; |
||
109 | // } |
||
110 | |||
111 | // sanitize against PHP_SELF/PATH_INFO XSS (disabled in 3.33) |
||
112 | // $_SERVER['PHP_SELF'] = strtr( @$_SERVER['PHP_SELF'] , array( '<' => '%3C' , '>' => '%3E' , "'" => '%27' , '"' => '%22' ) ) ; |
||
113 | // if( ! empty( $_SERVER['PATH_INFO'] ) ) $_SERVER['PATH_INFO'] = strtr( @$_SERVER['PATH_INFO'] , array( '<' => '%3C' , '>' => '%3E' , "'" => '%27' , '"' => '%22' ) ) ; |
||
114 | |||
115 | $this->_bad_globals = array( |
||
116 | 'GLOBALS', '_SESSION', 'HTTP_SESSION_VARS', '_GET', 'HTTP_GET_VARS', '_POST', 'HTTP_POST_VARS', '_COOKIE', |
||
117 | 'HTTP_COOKIE_VARS', '_SERVER', 'HTTP_SERVER_VARS', '_REQUEST', '_ENV', '_FILES', 'xoopsDB', 'xoopsUser', |
||
118 | 'xoopsUserId', 'xoopsUserGroups', 'xoopsUserIsAdmin', 'xoopsConfig', 'xoopsOption', 'xoopsModule', |
||
119 | 'xoopsModuleConfig' |
||
120 | ); |
||
121 | |||
122 | $this->_initial_recursive($_GET, 'G'); |
||
123 | $this->_initial_recursive($_POST, 'P'); |
||
124 | $this->_initial_recursive($_COOKIE, 'C'); |
||
125 | return true; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $key |
||
130 | */ |
||
131 | function _initial_recursive($val, $key) |
||
132 | { |
||
133 | if (is_array($val)) { |
||
134 | foreach ($val as $subkey => $subval) { |
||
135 | // check bad globals |
||
136 | View Code Duplication | if (in_array($subkey, $this->_bad_globals, true)) { |
|
137 | $this->message .= "Attempt to inject '$subkey' was found.\n"; |
||
138 | $this->_safe_contami = false; |
||
139 | $this->last_error_type = 'CONTAMI'; |
||
140 | } |
||
141 | $this->_initial_recursive($subval, $key . '_' . base64_encode($subkey)); |
||
142 | } |
||
143 | } else { |
||
144 | // check nullbyte attack |
||
145 | if (@$this->_conf['san_nullbyte'] && strstr($val, chr(0))) { |
||
146 | $val = str_replace(chr(0), ' ', $val); |
||
147 | $this->replace_doubtful($key, $val); |
||
148 | $this->message .= "Injecting Null-byte '$val' found.\n"; |
||
149 | $this->output_log('NullByte', 0, false, 32); |
||
150 | // $this->purge() ; |
||
151 | } |
||
152 | |||
153 | // register as doubtful requests against SQL Injections |
||
154 | if (preg_match('?[\s\'"`/]?', $val)) { |
||
155 | $this->_doubtful_requests["$key"] = $val; |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | static public function &getInstance() |
||
161 | { |
||
162 | static $instance; |
||
163 | if (!isset($instance)) { |
||
164 | $instance = new Protector(); |
||
165 | } |
||
166 | return $instance; |
||
167 | } |
||
168 | |||
169 | function updateConfFromDb() |
||
170 | { |
||
171 | if (empty($this->_conn)) { |
||
172 | return false; |
||
173 | } |
||
174 | |||
175 | $result = @mysql_query("SELECT conf_name,conf_value FROM " . \XoopsBaseConfig::get('db-prefix') . "_config WHERE conf_title like '" . "_MI_PROTECTOR%'", $this->_conn); |
||
176 | if (!$result || mysql_num_rows($result) < 5) { |
||
177 | return false; |
||
178 | } |
||
179 | $db_conf = array(); |
||
180 | while (list($key, $val) = mysql_fetch_row($result)) { |
||
181 | $db_conf[$key] = $val; |
||
182 | } |
||
183 | $db_conf_serialized = serialize($db_conf); |
||
184 | |||
185 | // update config cache |
||
186 | if ($db_conf_serialized != $this->_conf_serialized) { |
||
187 | $fp = fopen($this->get_filepath4confighcache(), 'w'); |
||
188 | fwrite($fp, $db_conf_serialized); |
||
189 | fclose($fp); |
||
190 | $this->_conf = $db_conf; |
||
191 | } |
||
192 | return true; |
||
193 | } |
||
194 | |||
195 | function setConn($conn) |
||
199 | |||
200 | function getConf() |
||
204 | |||
205 | function purge($redirect_to_top = false) |
||
206 | { |
||
207 | // clear all session values |
||
208 | if (isset($_SESSION)) { |
||
209 | foreach ($_SESSION as $key => $val) { |
||
210 | $_SESSION[$key] = ''; |
||
211 | if (isset($GLOBALS[$key])) { |
||
212 | $GLOBALS[$key] = ''; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | if (!headers_sent()) { |
||
218 | // clear typical session id of PHP |
||
219 | setcookie('PHPSESSID', '', time() - 3600, '/', '', 0); |
||
220 | if (isset($_COOKIE[session_name()])) { |
||
221 | setcookie(session_name(), '', time() - 3600, '/', '', 0); |
||
222 | } |
||
223 | |||
224 | // clear autologin cookie |
||
225 | $xoops_cookie_path = defined('XOOPS_COOKIE_PATH') ? XOOPS_COOKIE_PATH : preg_replace('?http://[^/]+(/.*)$?', "$1", XOOPS_URL); |
||
226 | if ($xoops_cookie_path == \XoopsBaseConfig::get('url')) { |
||
227 | $xoops_cookie_path = '/'; |
||
228 | } |
||
229 | setcookie('autologin_uname', '', time() - 3600, $xoops_cookie_path, '', 0); |
||
230 | setcookie('autologin_pass', '', time() - 3600, $xoops_cookie_path, '', 0); |
||
231 | } |
||
232 | |||
233 | if ($redirect_to_top) { |
||
234 | header('Location: ' . \XoopsBaseConfig::get('url') . '/'); |
||
235 | exit; |
||
236 | } else { |
||
237 | $ret = $this->call_filter('prepurge_exit'); |
||
238 | if ($ret == false) { |
||
239 | die('Protector detects attacking actions'); |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | function output_log($type = 'UNKNOWN', $uid = 0, $unique_check = false, $level = 1) |
||
245 | { |
||
246 | if ($this->_logged) { |
||
247 | return true; |
||
248 | } |
||
249 | |||
250 | if (!($this->_conf['log_level'] & $level)) { |
||
251 | return true; |
||
252 | } |
||
253 | |||
254 | if (empty($this->_conn)) { |
||
255 | $this->_conn = @mysql_connect(\XoopsBaseConfig::get('db-host'), \XoopsBaseConfig::get('db-user'), \XoopsBaseConfig::get('db-pass')); |
||
256 | if (!$this->_conn) { |
||
257 | die('db connection failed.'); |
||
258 | } |
||
259 | if (!mysql_select_db(\XoopsBaseConfig::get('db-name'), $this->_conn)) { |
||
260 | die('db selection failed.'); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | $ip = @$_SERVER['REMOTE_ADDR']; |
||
265 | $agent = @$_SERVER['HTTP_USER_AGENT']; |
||
266 | |||
267 | if ($unique_check) { |
||
268 | $result = mysql_query('SELECT ip,type FROM ' . \XoopsBaseConfig::get('db-prefix') . '_' . $this->mydirname . '_log ORDER BY timestamp DESC LIMIT 1', $this->_conn); |
||
269 | list($last_ip, $last_type) = mysql_fetch_row($result); |
||
270 | if ($last_ip == $ip && $last_type == $type) { |
||
271 | $this->_logged = true; |
||
272 | return true; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | mysql_query("INSERT INTO " . XOOPS_DB_PREFIX . "_" . $this->mydirname . "_log SET ip='" . addslashes($ip) . "',agent='" . addslashes($agent) . "',type='" . addslashes($type) . "',description='" . addslashes($this->message) . "',uid='" . (int)($uid) . "',timestamp=NOW()", $this->_conn); |
||
277 | $this->_logged = true; |
||
278 | return true; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param integer $expire |
||
283 | */ |
||
284 | View Code Duplication | function write_file_bwlimit($expire) |
|
285 | { |
||
286 | $expire = min((int)($expire), time() + 300); |
||
287 | |||
288 | $fp = @fopen($this->get_filepath4bwlimit(), 'w'); |
||
289 | if ($fp) { |
||
290 | @flock($fp, LOCK_EX); |
||
291 | fwrite($fp, $expire . "\n"); |
||
292 | @flock($fp, LOCK_UN); |
||
293 | fclose($fp); |
||
294 | return true; |
||
295 | } else { |
||
296 | return false; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | function get_bwlimit() |
||
301 | { |
||
302 | list($expire) = @file(Protector::get_filepath4bwlimit()); |
||
303 | $expire = min((int)($expire), time() + 300); |
||
304 | |||
305 | return $expire; |
||
306 | } |
||
307 | |||
308 | function get_filepath4bwlimit() |
||
312 | |||
313 | View Code Duplication | function write_file_badips($bad_ips) |
|
314 | { |
||
315 | asort($bad_ips); |
||
316 | |||
317 | $fp = @fopen($this->get_filepath4badips(), 'w'); |
||
318 | if ($fp) { |
||
319 | @flock($fp, LOCK_EX); |
||
320 | fwrite($fp, serialize($bad_ips) . "\n"); |
||
321 | @flock($fp, LOCK_UN); |
||
322 | fclose($fp); |
||
323 | return true; |
||
324 | } else { |
||
325 | return false; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | function register_bad_ips($jailed_time = 0, $ip = null) |
||
343 | |||
344 | function get_bad_ips($with_jailed_time = false) |
||
345 | { |
||
346 | list($bad_ips_serialized) = @file(Protector::get_filepath4badips()); |
||
347 | $bad_ips = empty($bad_ips_serialized) ? array() : @unserialize($bad_ips_serialized); |
||
348 | if (!is_array($bad_ips) || isset($bad_ips[0])) { |
||
349 | $bad_ips = array(); |
||
350 | } |
||
351 | |||
352 | // expire jailed_time |
||
353 | $pos = 0; |
||
354 | foreach ($bad_ips as $bad_ip => $jailed_time) { |
||
355 | if ($jailed_time >= time()) { |
||
356 | break; |
||
357 | } |
||
358 | ++$pos; |
||
359 | } |
||
360 | $bad_ips = array_slice($bad_ips, $pos); |
||
361 | |||
362 | if ($with_jailed_time) { |
||
363 | return $bad_ips; |
||
364 | } else { |
||
365 | return array_keys($bad_ips); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | function get_filepath4badips() |
||
373 | |||
374 | function get_group1_ips($with_info = false) |
||
388 | |||
389 | function get_filepath4group1ips() |
||
393 | |||
394 | function get_filepath4confighcache() |
||
398 | |||
399 | function ip_match($ips) |
||
400 | { |
||
401 | foreach ($ips as $ip => $info) { |
||
402 | if ($ip) { |
||
403 | switch (substr($ip, -1)) { |
||
404 | case '.' : |
||
405 | // foward match |
||
406 | if (substr(@$_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip) { |
||
407 | $this->ip_matched_info = $info; |
||
408 | return true; |
||
409 | } |
||
410 | break; |
||
411 | case '0' : |
||
412 | case '1' : |
||
413 | case '2' : |
||
414 | case '3' : |
||
415 | case '4' : |
||
416 | case '5' : |
||
417 | case '6' : |
||
418 | case '7' : |
||
419 | case '8' : |
||
420 | View Code Duplication | case '9' : |
|
421 | // full match |
||
422 | if (@$_SERVER['REMOTE_ADDR'] == $ip) { |
||
423 | $this->ip_matched_info = $info; |
||
424 | return true; |
||
425 | } |
||
426 | break; |
||
427 | View Code Duplication | default : |
|
428 | // perl regex |
||
429 | if (@preg_match($ip, @$_SERVER['REMOTE_ADDR'])) { |
||
430 | $this->ip_matched_info = $info; |
||
431 | return true; |
||
432 | } |
||
433 | break; |
||
434 | } |
||
435 | } |
||
436 | } |
||
437 | $this->ip_matched_info = null; |
||
438 | return false; |
||
439 | } |
||
440 | |||
441 | function deny_by_htaccess($ip = null) |
||
442 | { |
||
443 | if (empty($ip)) { |
||
444 | $ip = @$_SERVER['REMOTE_ADDR']; |
||
445 | } |
||
446 | if (empty($ip)) { |
||
447 | return false; |
||
448 | } |
||
449 | if (!function_exists('file_get_contents')) { |
||
450 | return false; |
||
451 | } |
||
452 | |||
453 | $target_htaccess = \XoopsBaseConfig::get('root-path') . '/.htaccess'; |
||
454 | $backup_htaccess = \XoopsBaseConfig::get('root-path') . '/uploads/.htaccess.bak'; |
||
455 | |||
456 | $ht_body = file_get_contents($target_htaccess); |
||
457 | |||
458 | // make backup as uploads/.htaccess.bak automatically |
||
459 | if ($ht_body && !XoopsLoad::fileExists($backup_htaccess)) { |
||
460 | $fw = fopen($backup_htaccess, "w"); |
||
461 | fwrite($fw, $ht_body); |
||
462 | fclose($fw); |
||
463 | } |
||
464 | |||
465 | // if .htaccess is broken, restore from backup |
||
466 | if (!$ht_body && XoopsLoad::fileExists($backup_htaccess)) { |
||
467 | $ht_body = file_get_contents($backup_htaccess); |
||
468 | } |
||
469 | |||
470 | // new .htaccess |
||
471 | if ($ht_body === false) { |
||
472 | $ht_body = ''; |
||
473 | } |
||
474 | |||
475 | if (preg_match("/^(.*)#PROTECTOR#\s+(DENY FROM .*)\n#PROTECTOR#\n(.*)$/si", $ht_body, $regs)) { |
||
476 | if (substr($regs[2], -strlen($ip)) == $ip) { |
||
477 | return true; |
||
478 | } |
||
479 | $new_ht_body = $regs[1] . "#PROTECTOR#\n" . $regs[2] . " $ip\n#PROTECTOR#\n" . $regs[3]; |
||
480 | } else { |
||
481 | $new_ht_body = "#PROTECTOR#\nDENY FROM $ip\n#PROTECTOR#\n" . $ht_body; |
||
482 | } |
||
483 | |||
484 | // error_log( "$new_ht_body\n" , 3 , "/tmp/error_log" ) ; |
||
485 | |||
486 | $fw = fopen($target_htaccess, "w"); |
||
487 | @flock($fw, LOCK_EX); |
||
488 | fwrite($fw, $new_ht_body); |
||
489 | @flock($fw, LOCK_UN); |
||
490 | fclose($fw); |
||
491 | |||
492 | return true; |
||
493 | } |
||
494 | |||
495 | function getDblayertrapDoubtfuls() |
||
499 | |||
500 | function _dblayertrap_check_recursive($val) |
||
501 | { |
||
502 | if (is_array($val)) { |
||
503 | foreach ($val as $subval) { |
||
504 | $this->_dblayertrap_check_recursive($subval); |
||
505 | } |
||
506 | } else { |
||
507 | if (strlen($val) < 6) { |
||
508 | return; |
||
509 | } |
||
510 | $val = get_magic_quotes_gpc() ? stripslashes($val) : $val; |
||
511 | foreach ($this->_dblayertrap_doubtful_needles as $needle) { |
||
512 | if (stristr($val, $needle)) { |
||
513 | $this->_dblayertrap_doubtfuls[] = $val; |
||
514 | } |
||
515 | } |
||
516 | } |
||
517 | } |
||
518 | |||
519 | function dblayertrap_init($force_override = false) |
||
520 | { |
||
521 | if (!empty($GLOBALS['xoopsOption']['nocommon']) || defined('_LEGACY_PREVENT_EXEC_COMMON_') || defined('_LEGACY_PREVENT_LOAD_CORE_')) { |
||
522 | return; |
||
523 | } // skip |
||
524 | |||
525 | $this->_dblayertrap_doubtfuls = array(); |
||
526 | $this->_dblayertrap_check_recursive($_GET); |
||
527 | $this->_dblayertrap_check_recursive($_POST); |
||
528 | $this->_dblayertrap_check_recursive($_COOKIE); |
||
529 | if (empty($this->_conf['dblayertrap_wo_server'])) { |
||
530 | $this->_dblayertrap_check_recursive($_SERVER); |
||
531 | } |
||
532 | |||
533 | if (!empty($this->_dblayertrap_doubtfuls) || $force_override) { |
||
534 | @define('XOOPS_DB_ALTERNATIVE', 'ProtectorMysqlDatabase'); |
||
535 | require_once dirname(__DIR__) . '/class/ProtectorMysqlDatabase.class.php'; |
||
536 | } |
||
537 | } |
||
538 | |||
539 | function _bigumbrella_check_recursive($val) |
||
551 | |||
552 | function bigumbrella_init() |
||
553 | { |
||
554 | $this->_bigumbrella_doubtfuls = array(); |
||
555 | $this->_bigumbrella_check_recursive($_GET); |
||
556 | $this->_bigumbrella_check_recursive(@$_SERVER['PHP_SELF']); |
||
557 | |||
558 | if (!empty($this->_bigumbrella_doubtfuls)) { |
||
559 | ob_start(array($this, 'bigumbrella_outputcheck')); |
||
560 | } |
||
561 | } |
||
562 | |||
563 | function bigumbrella_outputcheck($s) |
||
564 | { |
||
565 | if (defined('BIGUMBRELLA_DISABLED')) { |
||
566 | return $s; |
||
588 | |||
589 | function intval_allrequestsendid() |
||
629 | |||
630 | function eliminate_dotdot() |
||
689 | |||
690 | function &get_ref_from_base64index(&$current, $indexes) |
||
701 | |||
702 | function replace_doubtful($key, $val) |
||
736 | |||
737 | function check_uploaded_files() |
||
803 | |||
804 | function check_contami_systemglobals() |
||
819 | |||
820 | function check_sql_isolatedcommentin($sanitize = true) |
||
844 | |||
845 | function check_sql_union($sanitize = true) |
||
867 | |||
868 | function stopforumspam($uid) |
||
933 | |||
934 | function check_dos_attack($uid = 0, $can_ban = false) |
||
1075 | |||
1076 | // |
||
1077 | function check_brute_force() |
||
1119 | |||
1120 | function _spam_check_point_recursive($val) |
||
1146 | |||
1147 | /** |
||
1148 | * @param integer $points4deny |
||
1149 | */ |
||
1150 | function spam_check($points4deny, $uid) |
||
1164 | |||
1165 | function disable_features() |
||
1256 | |||
1257 | /** |
||
1258 | * @param string $type |
||
1259 | */ |
||
1260 | function call_filter($type, $dying_message = '') |
||
1271 | } |
||
1272 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.