PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.8.7
SiteGuard WP Plugin v1.8.7
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-login-history.php
siteguard / classes Last commit date
siteguard-admin-filter.php 1 month ago siteguard-base.php 1 month ago siteguard-captcha.php 1 month ago siteguard-config.php 11 months ago siteguard-disable-author-query.php 3 weeks ago siteguard-disable-pingback.php 1 month ago siteguard-disable-xmlrpc.php 1 month ago siteguard-htaccess.php 3 weeks ago siteguard-login-alert.php 1 month ago siteguard-login-history.php 1 month ago siteguard-login-lock.php 1 month ago siteguard-rename-login.php 1 week ago siteguard-updates-notify.php 1 month ago siteguard-waf-exclude-rule.php 1 month ago
siteguard-login-history.php
206 lines
1 <?php
2
3 class SiteGuard_LoginHistory extends SiteGuard_Base {
4
5 function __construct() {
6 define( 'SITEGUARD_TABLE_HISTORY', 'siteguard_history' );
7 add_action( 'wp_login', array( $this, 'handler_wp_login' ), 1, 2 );
8 add_action( 'wp_login_failed', array( $this, 'handler_wp_login_failed' ), 30 );
9 add_action( 'xmlrpc_call', array( $this, 'handler_xmlrpc_call' ), 10, 1 );
10 }
11 function init() {
12 global $wpdb;
13 // operation
14 // 0: Login failure
15 // 1: Login success
16 // 2: Fail once
17 // 3: Login lock
18 // type
19 // 0: login page
20 // 1: xmlrpc
21 $table_name = $wpdb->prefix . SITEGUARD_TABLE_HISTORY;
22 $sql = "CREATE TABLE $table_name (
23 id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
24 login_name VARCHAR(40) NOT NULL DEFAULT '',
25 ip_address VARCHAR(40) NOT NULL DEFAULT '',
26 operation INT NOT NULL DEFAULT 0,
27 time datetime,
28 type INT NOT NULL DEFAULT 0,
29 UNIQUE KEY id (id)
30 )
31 CHARACTER SET 'utf8';";
32 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
33 dbDelta( $sql );
34 }
35 function get_type() {
36 $type = SITEGUARD_LOGIN_TYPE_NORMAL;
37 if ( basename( $_SERVER['SCRIPT_NAME'] ) == 'xmlrpc.php' ) {
38 $type = SITEGUARD_LOGIN_TYPE_XMLRPC;
39 }
40 return $type;
41 }
42 function check_operation( $operation ) {
43 $items = array( SITEGUARD_LOGIN_SUCCESS, SITEGUARD_LOGIN_FAILED, SITEGUARD_LOGIN_FAIL_ONCE, SITEGUARD_LOGIN_LOCKED );
44 if ( in_array( $operation, $items ) ) {
45 return true;
46 }
47 return false;
48 }
49 function check_type( $type ) {
50 $items = array( SITEGUARD_LOGIN_TYPE_NORMAL, SITEGUARD_LOGIN_TYPE_XMLRPC );
51 if ( in_array( $type, $items ) ) {
52 return true;
53 }
54 return false;
55 }
56 function handler_wp_login( $login, $current_user ) {
57
58 if ( '' == $current_user->user_login ) {
59 return;
60 }
61 $this->add_operation( SITEGUARD_LOGIN_SUCCESS, $current_user->user_login, $this->get_type() );
62 }
63 function handler_wp_login_failed( $username ) {
64 global $siteguard_loginlock;
65 $this->add_operation( $siteguard_loginlock->get_status(), $username, $this->get_type() );
66 }
67 function handler_xmlrpc_call( $method ) {
68 $current_user = wp_get_current_user();
69 if ( '' == $current_user->user_login ) {
70 return;
71 }
72 $this->add_operation( SITEGUARD_LOGIN_SUCCESS, $current_user->user_login, SITEGUARD_LOGIN_TYPE_XMLRPC );
73 }
74 function is_exist( $user, $operation, $after_sec, $less_sec ) {
75 global $wpdb;
76
77 if ( $after_sec > $less_sec ) {
78 return false;
79 }
80
81 $table_name = $wpdb->prefix . SITEGUARD_TABLE_HISTORY;
82 $ip_address = $this->get_ip();
83 $now = current_time( 'mysql' );
84 $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table_name WHERE ip_address = %s AND login_name = %s AND operation = %d AND time BETWEEN %s - INTERVAL %d SECOND AND %s - INTERVAL %d SECOND; ", $ip_address, $user, $operation, $now, $less_sec, $now, $after_sec ) );
85 if ( null == $id ) {
86 return false;
87 }
88 return true;
89 }
90 function add_operation( $operation, $user_login, $type = SITEGUARD_LOGIN_TYPE_NORMAL ) {
91 global $wpdb;
92
93 if ( '' != $user_login ) {
94 $user = $user_login;
95 } else {
96 $current_user = wp_get_current_user();
97 $user = $current_user->user_login;
98 }
99 $table_name = $wpdb->prefix . SITEGUARD_TABLE_HISTORY;
100
101 $wpdb->query( 'START TRANSACTION' );
102 // delete old event
103 $id = $wpdb->get_var( "SELECT id FROM $table_name ORDER BY id DESC LIMIT 9999,1;", 0, 0 );
104 if ( null != $id ) {
105 $wpdb->query( $wpdb->prepare( "DELETE FROM $table_name WHERE id <= %d;", $id ) );
106 }
107 $ip_address = $this->get_ip();
108 $data = array(
109 'operation' => $operation,
110 'login_name' => $user,
111 'ip_address' => $ip_address,
112 'time' => current_time( 'mysql' ),
113 'type' => $type,
114 );
115 $wpdb->insert( $table_name, $data );
116
117 $wpdb->query( 'COMMIT' );
118 }
119 static function convert_operation( $operation ) {
120 $result = '';
121 switch ( $operation ) {
122 case SITEGUARD_LOGIN_FAILED:
123 $result = esc_html__( 'Failed', 'siteguard' );
124 break;
125 case SITEGUARD_LOGIN_SUCCESS:
126 $result = esc_html__( 'Success', 'siteguard' );
127 break;
128 case SITEGUARD_LOGIN_FAIL_ONCE:
129 $result = esc_html__( 'Fail Once', 'siteguard' );
130 break;
131 case SITEGUARD_LOGIN_LOCKED:
132 $result = esc_html__( 'Locked', 'siteguard' );
133 break;
134 default:
135 $result = esc_html__( 'Unknown', 'siteguard' );
136 }
137 return $result;
138 }
139 static function convert_type( $type ) {
140 $result = '';
141 switch ( $type ) {
142 case SITEGUARD_LOGIN_TYPE_NORMAL:
143 $result = esc_html__( 'Login Page', 'siteguard' );
144 break;
145 case SITEGUARD_LOGIN_TYPE_XMLRPC:
146 $result = esc_html__( 'XML-RPC', 'siteguard' );
147 break;
148 default:
149 $result = esc_html__( 'Unknown', 'siteguard' );
150 }
151 return $result;
152 }
153 function get_history( $operation, $login_name, $ip_address, $type, $login_name_not, $ip_address_not ) {
154 global $wpdb;
155 $where = '';
156 $values = array();
157 if ( true === $this->check_operation( $operation ) ) {
158 $where = 'operation = %d';
159 array_push( $values, $operation );
160 }
161 if ( ! empty( $login_name ) ) {
162 if ( ! empty( $where ) ) {
163 $where .= ' and ';
164 }
165 if ( true === $login_name_not ) {
166 $where .= 'login_name <> %s';
167 } else {
168 $where .= 'login_name = %s';
169 }
170 array_push( $values, $login_name );
171 }
172 if ( ! empty( $ip_address ) ) {
173 if ( ! empty( $where ) ) {
174 $where .= ' and ';
175 }
176 if ( true === $ip_address_not ) {
177 $where .= 'ip_address <> %s';
178 } else {
179 $where .= 'ip_address = %s';
180 }
181 array_push( $values, $ip_address );
182 }
183 if ( true === $this->check_type( $type ) ) {
184 if ( ! empty( $where ) ) {
185 $where .= ' and ';
186 }
187 $where .= 'type = %d';
188 array_push( $values, $type );
189 }
190 if ( ! empty( $where ) ) {
191 $where = 'WHERE ' . $where;
192 } else {
193 $where = 'WHERE operation >= %d';
194 array_push( $values, '0' );
195 }
196 $table_name = $wpdb->prefix . SITEGUARD_TABLE_HISTORY;
197 $prepare = array();
198 $prepare[] = "SELECT id, operation, login_name, ip_address, time, type FROM $table_name $where";
199 foreach ( $values as $v ) {
200 $prepare[] = $v;
201 }
202 $results = $wpdb->get_results( call_user_func_array( array( $wpdb, 'prepare' ), $prepare ), ARRAY_A );
203 return $results;
204 }
205 }
206