-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin.php
118 lines (98 loc) · 3.83 KB
/
plugin.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
<?php
/*
Plugin Name: BlackListIP
Plugin URI: https://github.com/LudoBoggio/YourlsBlackListIPs
Description: Plugin which block blacklisted IPs
Version: 1.3
Author: Ludo
Author URI: http://ludovic.boggio.fr
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
include "ludo_blacklist_ip_Check_IP_Module.php";
// Hook the custom function into the 'pre_check_ip_flood' event
yourls_add_action( 'pre_check_ip_flood', 'ludo_blacklist_ip_root' );
// Hook the admin page into the 'plugins_loaded' event
yourls_add_action( 'plugins_loaded', 'ludo_blacklist_ip_add_page' );
// Get blacklisted IPs from YOURLS options feature and compare with current IP address
function ludo_blacklist_ip_root ( $args ) {
$IP = $args[0];
$Intervalle_IP = yourls_get_option ('ludo_blacklist_ip_liste');
$Intervalle_IP = ( $Intervalle_IP ) ? ( unserialize ( $Intervalle_IP ) ):((array)NULL);
foreach ( $Intervalle_IP as $value ) {
$IPs = explode ( "-" , $value );
if ( $IP >= $IPs[0] AND $IP <= $IPs[1]) {
// yourls_die ( "Your IP has been blacklisted.", "Black list",403);
echo "<center>Your IP has been blacklisted.</center>";
die();
}
}
}
// Add admin page
function ludo_blacklist_ip_add_page () {
yourls_register_plugin_page( 'ludo_blacklist_ip', 'Blacklist IPs', 'ludo_blacklist_ip_do_page' );
}
// Display admin page
function ludo_blacklist_ip_do_page () {
if( isset( $_POST['action'] ) && $_POST['action'] == 'blacklist_ip' ) {
ludo_blacklist_ip_process ();
} else {
ludo_blacklist_ip_form ();
}
}
// Display form to administrate blacklisted IPs list
function ludo_blacklist_ip_form () {
$nonce = yourls_create_nonce( 'blacklist_ip' ) ;
$liste_ip = yourls_get_option ('ludo_blacklist_ip_liste','Enter IP addresses here, one entry per line');
if ($liste_ip != 'Enter IP addresses here, one entry per line' )
$liste_ip_display = implode ( "\r\n" , unserialize ( $liste_ip ) );
else
$liste_ip_display=$liste_ip;
echo <<<HTML
<h2>BlackList IPs</h2>
<form method="post">
<input type="hidden" name="action" value="blacklist_ip" />
<input type="hidden" name="nonce" value="$nonce" />
<p>Blacklist following IPs (one range or IP per line, no wildcards allowed) :</p>
<p><textarea cols="50" rows="10" name="blacklist_form">$liste_ip_display</textarea></p>
<p><input type="submit" value="Save" /></p>
<p>I suggest to add here IPs that you saw adding bulk URL. It is your own responsibility to check the use of the IPs you block. WARNING : erroneous entries may create unexpected behaviours, please double-check before validation.</p>
<p>Examples :
<ul>
<li>10.0.0.1/24 : blacklist from 10.0.0.0 to 10.0.0.255 (CIDR notation).</li>
<li>192.168.1.2/255.255.255.128 : blacklist from 192.168.1.0 to 192.168.0.128.</li>
<li>192.168.1.12-192.168.1.59 : blacklist from 192.168.1.12 to 192.168.1.59.</li>
<li>192.168.0.0 : blacklist from 192.168.0.0 to 192.168.255.255</li>
<li>10.0.0.58 : blacklist only 10.0.0.58 IP address.</li>
</ul>
</p>
</form>
HTML;
}
// Update blacklisted IPs list
function ludo_blacklist_ip_process () {
// Check nonce
yourls_verify_nonce( 'blacklist_ip' ) ;
// Check if the answer is correct.
$IP_Form = explode ( "\r\n" , $_POST['blacklist_form'] ) ;
if (! is_array ($IP_Form) ) {
echo "Bad answer, Blacklist not updated";
die ();
}
$boucle = 0;
foreach ($IP_Form as $value) {
$Retour = ludo_blacklist_ip_Analyze_IP ( $value ) ;
if ( $Retour != "NULL" ) {
$IPList[$boucle++] = $Retour ;
}
}
// Update list
yourls_update_option ( 'ludo_blacklist_ip_liste', serialize ( $IPList ) );
echo "Black list updated. New blacklist is " ;
if ( count ( $IPList ) == 0 )
echo "empty.";
else {
echo ":<BR />";
foreach ($IPList as $value) echo $value."<BR />";
}
}