Modifiying the Blacklist IP plugin to create the whitelisted domain plugin

This commit is contained in:
Panthro
2012-09-09 20:06:57 -03:00
parent 621c359ad4
commit a9cdb85b5c
2 changed files with 57 additions and 56 deletions

View File

@ -1,22 +1,20 @@
YourlsBlacklistIPs YourlsWhiteListDomains
Plugin for Yourls allowing to blacklist IPs Plugin for Yourls allowing only whitelisted domains
This plugin is intended to be used with YOURLS (cf. http://yourls.org) This plugin is intended to be used with YOURLS (cf. http://yourls.org)
It has been tested on YOURLS v1.5 and v1.5.1 It has been tested on YOURLS v1.5 and v1.5.1
Current version is 1.1, updated on 09/03/2012 Current version is 1.0
Contact : Ludo at Ludo.Boggio+GitHub@GMail.com Contact : panthro.rafael@gmail.com
INSTALL : INSTALL :
- In /user/plugins, create a new folder named BlackListIP - In /user/plugins, create a new folder named whitelist-domains
- In this new directory, copy the plugin.php file from this repository - In this new directory, copy the plugin.php file from this repository
- Go to the Plugins administration page and activate the plugin - Go to the Plugins administration page and activate the plugin
You will see in the admin section a new admin page where you can add the IP addresses you want to blacklist. You will see in the admin section a new admin page where you can add the domain addresses to the whitelist.
Please enter one IP address per line. Other syntax should provide unexpected behaviours. Enter one domain in each line, please, remove the www when adding a new domain, the code works on both.
Roadmap contains the possibility to use regexp to specify the IP addresses. No, no date provided :)

View File

@ -1,69 +1,73 @@
<?php <?php
/* /*
Plugin Name: BlackListIP Plugin Name: WhiteListDomains
Plugin URI: https://github.com/LudoBoggio/YourlsBlackListIPs Plugin URI: https://github.com/Panthro/YourlsWhitelistDomains
Description: Plugin which block blacklisted IPs Description: Plugin which allow only whitelisted domains, forked from: https://github.com/LudoBoggio/YourlsBlacklistIPs
Version: 1.1 Version: 1.0
Author: Ludo Author: Panthro
Author URI: http://ludo.boggio.fr Author URI: http://passeionaweb.com.br
*/ */
// No direct call // No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die(); if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Hook the custom function into the 'pre_check_ip_flood' event // Hook the custom function into the 'pre_check_domain_flood' event
yourls_add_action( 'pre_check_ip_flood', 'ludo_blacklist_ip_root' ); yourls_add_filter( 'shunt_add_new_link', 'panthro_whitelist_domain_root' );
// Hook the admin page into the 'plugins_loaded' event // Hook the admin page into the 'plugins_loaded' event
yourls_add_action( 'plugins_loaded', 'ludo_blacklist_ip_add_page' ); yourls_add_action( 'plugins_loaded', 'panthro_whitelist_domain_add_page' );
// Get blacklisted IPs from YOURLS options feature and compare with current IP address // Get whitelisted domains from YOURLS options feature and compare with current domain address
function ludo_blacklist_ip_root ( $args ) { function panthro_whitelist_domain_root ( $bol, $url ) {
$parse = parse_url($url);
$ip = $args[0]; $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
$liste_ip = yourls_get_option ('ludo_blacklist_ip_liste'); $return = false;
if ( !$liste_ip ) { $domain_list = yourls_get_option ('panthro_whitelist_domain_list');
$liste_ip_display = unserialize ( $liste_ip ); if ( $domain_list ) {
$domain_list_display = unserialize ( $domain_list );
if (in_array($ip, $liste_ip_display)) { //if (!in_array($domain, $domain_list_display)) {
// yourls_die ( "Your IP has been blacklisted.", "Black list",403); if (strpos($domain_list_display,$domain) === false) {
echo "<center>Your IP has been blacklisted.</center>"; $return['status'] = 'fail';
die(); $return['code'] = 'error:domain-not-allowed';
$return['message'] = 'This domain is not allowed';
$return['errorCode'] = '400';
} }
} }
return $return;
} }
// Add admin page // Add admin page
function ludo_blacklist_ip_add_page () { function panthro_whitelist_domain_add_page () {
yourls_register_plugin_page( 'ludo_blacklist_ip', 'Blacklist IPs', 'ludo_blacklist_ip_do_page' ); yourls_register_plugin_page( 'panthro_whitelist_domain', 'Whitelist domains', 'panthro_whitelist_domain_do_page' );
} }
// Display admin page // Display admin page
function ludo_blacklist_ip_do_page () { function panthro_whitelist_domain_do_page () {
if( isset( $_POST['action'] ) && $_POST['action'] == 'blacklist_ip' ) { if( isset( $_POST['action'] ) && $_POST['action'] == 'whitelist_domain' ) {
ludo_blacklist_ip_process (); panthro_whitelist_domain_process ();
} else { } else {
ludo_blacklist_ip_form (); panthro_whitelist_domain_form ();
} }
} }
// Display form to administrate blacklisted IPs list // Display form to administrate whitelisted domains list
function ludo_blacklist_ip_form () { function panthro_whitelist_domain_form () {
$nonce = yourls_create_nonce( 'blacklist_ip' ) ; $nonce = yourls_create_nonce( 'whitelist_domain' ) ;
$liste_ip = yourls_get_option ('ludo_blacklist_ip_liste','Enter IP addresses here, one per line'); $domain_list = yourls_get_option ('panthro_whitelist_domain_list','Enter domain addresses here, one per line');
if ($liste_ip != 'Enter IP addresses here, one per line' ) if ($domain_list != 'Enter domain addresses here, one per line' ){
$liste_ip_display = implode ( "\n" , unserialize ( $liste_ip ) ); $domain_list_display = unserialize ( $domain_list );
}else{
$domain_list_display = $domain_list;
}
echo <<<HTML echo <<<HTML
<h2> BlackList IPs</h2> <h2> WhiteList domains</h2>
<form method="post"> <form method="post">
<input type="hidden" name="action" value="blacklist_ip" /> <input type="hidden" name="action" value="whitelist_domain" />
<input type="hidden" name="nonce" value="$nonce" /> <input type="hidden" name="nonce" value="$nonce" />
<p>Blacklist following IPs <p>Whitelist following domains
<textarea cols="30" rows="5" name="blacklist_form"> <textarea cols="30" rows="5" name="whitelist_form">$domain_list_display</textarea>
$liste_ip_display
</textarea>
</p> </p>
<p><input type="submit" value="Save" /></p> <p><input type="submit" value="Save" /></p>
@ -71,14 +75,13 @@ function ludo_blacklist_ip_form () {
HTML; HTML;
} }
// Update blacklisted IPs list // Update whitelisted domains list
function ludo_blacklist_ip_process () { function panthro_whitelist_domain_process () {
// Check nonce // Check nonce
yourls_verify_nonce( 'blacklist_ip' ) ; yourls_verify_nonce( 'whitelist_domain' ) ;
// Update list // Update list
$sent_list = serialize ( explode ( '\n' , $_POST['blacklist_form'] ) ); $sent_list = serialize($_POST['whitelist_form']);
yourls_update_option ( $sent_list , 'ludo_blacklist_ip_liste' ); yourls_update_option ( 'panthro_whitelist_domain_list',$sent_list );
echo "Black list updated" ; echo "White list updated" ;
} }
?> ?>