-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip-blacklist.php
174 lines (140 loc) · 5.03 KB
/
ip-blacklist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
Plugin Name: IP Blacklist
Description: A plugin to record user IP at registration and blacklist certain IPs
Version: 1.0
Author: Dezzyboy | Glibx Inc
*/
global $jal_db_version;
$jal_db_version = '1.0';
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . 'blacklisted_ips';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
username varchar(55) DEFAULT '' NOT NULL,
ip_address varchar(55) DEFAULT '' NOT NULL,
reg_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
device_info text,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( plugin_basename( __DIR__ ) . '/ip-blacklist.php', 'jal_install' );
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
function record_registration_ip($user_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'blacklisted_ips';
$ip_address = get_client_ip();
$username = $_POST['user_login'];
$blacklisted = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE ip_address = %s",
$ip_address
) );
if ( $blacklisted ) {
wp_die('Your IP has been blacklisted.');
} else {
update_user_meta($user_id, 'registration_ip', $ip_address);
$wpdb->insert(
$table_name,
array(
'username' => $username,
'ip_address' => $ip_address
)
);
}
}
add_action('user_register', 'record_registration_ip');
function blacklist_ips_menu() {
add_menu_page(
__( 'Blacklisted IPs', 'textdomain' ),
'Blacklist IPs',
'manage_options',
'blacklist_ips',
'blacklist_ips_admin_page',
'dashicons-shield',
plugins_url( 'glibx-icon.png', __FILE__ ),
6
);
}
add_action( 'admin_menu', 'blacklist_ips_menu' );
function blacklist_ips_admin_page(){
global $wpdb;
// Set number of users per page
$users_per_page = 10;
// Get current page from URL, default to 1 if not present
$current_page = isset($_GET['paged']) ? intval($_GET['paged']) : 1;
// Calculate the SQL offset
$offset = ($current_page - 1) * $users_per_page;
// Get the users for this page
$users = get_users([
'number' => $users_per_page,
'offset' => $offset,
]);
// Get the total number of users
$total_users = count(get_users());
// Calculate total pages
$total_pages = ceil($total_users / $users_per_page);
$table_name = $wpdb->prefix . 'blacklisted_ips';
if (isset($_POST['new_ip'])) {
$wpdb->insert(
$table_name,
array(
'ip_address' => $_POST['new_ip']
)
);
}
echo '<h1>Blacklist IP</h1>';
echo '<form method="post" action="">';
echo '<input type="text" name="new_ip" placeholder="Enter IP to blacklist" required>';
echo '<input type="submit" value="Blacklist IP">';
echo '</form>';
echo '<h2>Registered User IPs</h2>';
echo '<table border="1" cellspacing="0" cellpadding="5">';
echo '<tr><th>Username</th><th>IP Address</th><th>Registration Date</th><th>Device Info</th></tr>';
$users = get_users();
foreach ($users as $user) {
$ip = get_user_meta($user->ID, 'registration_ip', true);
$date = get_user_meta($user->ID, 'registration_date', true);
$device = get_user_meta($user->ID, 'registration_device', true);
echo '<tr><td>' . $user->user_login . '</td><td>' . $ip . '</td><td>' . $date . '</td><td>' . $device . '</td></tr>';
}
echo '</table>';
$results = $wpdb->get_results( "SELECT * FROM $table_name" );
echo '<h2>Blacklisted IPs</h2>';
echo '<table border="1" cellspacing="0" cellpadding="5">';
echo '<tr><th>IP Address</th></tr>';
foreach ( $results as $row ) {
echo '<tr><td>' . $row->ip_address . '</td></tr>';
}
echo '</table>';
// Add pagination links
for ($page = 1; $page <= $total_pages; $page++) {
if ($page == $current_page) {
echo '<strong>' . $page . '</strong>';
} else {
echo '<a href="' . add_query_arg('paged', $page) . '">' . $page . '</a>';
}
}
}