PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.6.0
SiteGuard WP Plugin v1.6.0
1.8.9 1.8.8 1.8.7 1.8.6 1.8.6-beta1 1.8.6-beta2 1.8.4 1.8.5 1.8.3 1.8.2 1.8.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.4.3 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.11 1.7.12 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.0-beta1 1.8.0-beta2 1.8.0-beta3 1.8.0-beta4
siteguard / classes / siteguard-admin-filter.php
siteguard / classes Last commit date
siteguard-admin-filter.php 6 years ago siteguard-base.php 5 years ago siteguard-captcha.php 8 years ago siteguard-config.php 10 years ago siteguard-disable-author-query.php 5 years ago siteguard-disable-pingback.php 10 years ago siteguard-disable-xmlrpc.php 6 years ago siteguard-htaccess.php 5 years ago siteguard-login-alert.php 8 years ago siteguard-login-history.php 8 years ago siteguard-login-lock.php 8 years ago siteguard-rename-login.php 6 years ago siteguard-updates-notify.php 8 years ago siteguard-waf-exclude-rule.php 6 years ago
siteguard-admin-filter.php
165 lines
1 <?php
2
3 class SiteGuard_AdminFilter extends SiteGuard_Base {
4 public static $htaccess_mark = '#==== SITEGUARD_ADMIN_FILTER_SETTINGS';
5
6 function __construct( ) {
7 define( 'SITEGUARD_TABLE_LOGIN', 'siteguard_login' );
8 add_action( 'wp_login', array( $this, 'handler_wp_login' ), 1, 2 );
9 }
10 static function get_mark( ) {
11 return SiteGuard_AdminFilter::$htaccess_mark;
12 }
13 function init( ) {
14 global $wpdb, $siteguard_config;
15 $table_name = $wpdb->prefix . SITEGUARD_TABLE_LOGIN;
16 $sql = 'CREATE TABLE ' . $table_name . " (
17 ip_address varchar(40) NOT NULL DEFAULT '',
18 status INT NOT NULL DEFAULT 0,
19 count INT NOT NULL DEFAULT 0,
20 last_login_time datetime,
21 UNIQUE KEY ip_address (ip_address)
22 )
23 CHARACTER SET 'utf8';";
24 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
25 dbDelta( $sql );
26 $siteguard_config->set( 'admin_filter_exclude_path', 'css,images,admin-ajax.php,load-styles.php,site-health.php' );
27 $siteguard_config->set( 'admin_filter_enable', '0' );
28 $siteguard_config->update( );
29 }
30 function handler_wp_login( $login, $current_user ) {
31 global $siteguard_htaccess, $siteguard_config;
32
33 if ( '' == $current_user->user_login ) {
34 return;
35 }
36 if ( 1 == $siteguard_config->get( 'admin_filter_enable' ) ) {
37 $this->feature_on( $this->get_ip( ) );
38 }
39 }
40 function cvt_exclude( $exclude ) {
41 return str_replace( ',', '|', $exclude );
42 }
43 function cvt_status_for_1_2_5( $ip_address ) {
44 global $wpdb;
45 $table_name = $wpdb->prefix . SITEGUARD_TABLE_LOGIN;
46 $wpdb->update( $table_name, array( 'status' => 0 ), array( 'ip_address' => $ip_address ) );
47 }
48 function get_ip_mode( ) {
49 global $siteguard_config;
50 if ( ! is_object( $siteguard_config ) ) {
51 $siteguard_config = new SiteGuard_Config( );
52 }
53 $ip_mode = $siteguard_config->get( 'ip_mode' );
54 if ( ! in_array( $ip_mode, SiteGuard_Base::$ip_mode_items ) ) {
55 $ip_mode = '0';
56 }
57 $ip_mode_num = intval( $ip_mode );
58
59 return $ip_mode_num;
60 }
61 function get_rewrite_postfix( $ip_mode ) {
62 $postfix = '';
63 switch ( $ip_mode ) {
64 case 2:
65 $postfix = '\s*,\s*[^,]+';
66 break;
67 case 3:
68 $postfix = '(\s*,\s*[^,]+){2}';
69 break;
70 default:
71 $postfix = '';
72 }
73 return $postfix;
74 }
75 function get_rewrite_pre_cond( $ip_mode ) {
76 if ( 0 === $ip_mode ) {
77 return '';
78 }
79 $postfix = $this->get_rewrite_postfix( $ip_mode );
80 $result = ' RewriteCond %{HTTP:X-Forwarded-For} [^,]+' . $postfix . "$\n";
81 return $result;
82 }
83 function get_rewrite_cond( $ip, $ip_mode ) {
84 if ( 0 === $ip_mode ) {
85 return ' RewriteCond %{REMOTE_ADDR} !^' . str_replace( '.', '\.', $ip ) . "$\n";
86 }
87 $postfix = $this->get_rewrite_postfix( $ip_mode );
88 return ' RewriteCond %{HTTP:X-Forwarded-For} !' . str_replace( '.', '\.', $ip ) . $postfix . "$\n";
89 }
90 function update_settings( $ip_address ) {
91 global $wpdb, $siteguard_config;
92 $htaccess_str = '';
93 $table_name = $wpdb->prefix . SITEGUARD_TABLE_LOGIN;
94 $exclude_paths = preg_split( '/,/', $siteguard_config->get( 'admin_filter_exclude_path' ) );
95
96 $now_str = current_time( 'mysql' );
97 $now_bin = strtotime( $now_str );
98
99 $wpdb->query( 'START TRANSACTION' );
100 $wpdb->query( $wpdb->prepare( "DELETE FROM $table_name WHERE status = %d AND last_login_time < SYSDATE() - INTERVAL 1 DAY;", SITEGUARD_LOGIN_SUCCESS ) );
101 $data = array(
102 'ip_address' => $ip_address,
103 'status' => SITEGUARD_LOGIN_SUCCESS,
104 'count' => 0,
105 'last_login_time' => $now_str,
106 );
107 $result = $wpdb->get_row( $wpdb->prepare( "SELECT status from $table_name WHERE ip_address = %s", $ip_address ) );
108 if ( null == $result ) {
109 $wpdb->insert( $table_name, $data );
110 } else {
111 $wpdb->update( $table_name, $data, array( 'ip_address' => $ip_address ) );
112 }
113 $parse_url = parse_url( site_url( ) );
114 if ( false === $parse_url ) {
115 $base = '/';
116 } else {
117 if ( isset( $parse_url['path'] ) ) {
118 $base = $parse_url['path'] . '/';
119 } else {
120 $base = '/';
121 }
122 }
123 $htaccess_str .= "<IfModule mod_rewrite.c>\n";
124 $htaccess_str .= " RewriteEngine on\n";
125 $htaccess_str .= " RewriteBase $base\n";
126 $htaccess_str .= " RewriteRule ^404-siteguard - [L]\n";
127 foreach ( $exclude_paths as $path ) {
128 $htaccess_str .= ' RewriteRule ^wp-admin/' . trim( str_replace( '.', '\.', $path ) ) . " - [L]\n";
129 }
130 $ip_mode = $this->get_ip_mode( );
131 $htaccess_str .= $this->get_rewrite_pre_cond( $ip_mode );
132 $results = $wpdb->get_col( $wpdb->prepare( "SELECT ip_address FROM $table_name WHERE status = %d;", SITEGUARD_LOGIN_SUCCESS ) );
133 if ( $results ) {
134 foreach ( $results as $ip ) {
135 $htaccess_str .= $this->get_rewrite_cond( $ip, $ip_mode );
136 }
137 }
138 $server_ip = $this->get_server_ip( );
139 if ( false !== $server_ip ) {
140 $htaccess_str .= $this->get_rewrite_cond( $server_ip, 0 );
141 }
142 $htaccess_str .= $this->get_rewrite_cond( '127.0.0.1', 0 );
143 $htaccess_str .= $this->get_rewrite_cond( '::1', 0 );
144 $htaccess_str .= " RewriteRule ^wp-admin 404-siteguard [L]\n";
145 $htaccess_str .= "</IfModule>\n";
146
147 $wpdb->query( 'COMMIT' );
148
149 return $htaccess_str;
150 }
151 function feature_on( $ip_address ) {
152 global $siteguard_htaccess, $siteguard_config;
153 if ( false === SiteGuard_Htaccess::check_permission( ) ) {
154 return false;
155 }
156 $mark = $this->get_mark( );
157 $data = $this->update_settings( $ip_address );
158 return $siteguard_htaccess->update_settings( $mark, $data );
159 }
160 static function feature_off( ) {
161 $mark = SiteGuard_AdminFilter::get_mark( );
162 return SiteGuard_Htaccess::clear_settings( $mark );
163 }
164 }
165