Comments and code style reworked

To adhere a little more to the WP Coding Standards, PHPdoc blocks and inline have been reworked. No code has been changed.
This commit is contained in:
2017-07-04 14:56:48 +02:00
parent 3428c6b711
commit 5b1e490ace
6 changed files with 575 additions and 424 deletions

View File

@ -1,36 +1,49 @@
<?php <?php
/**
* Statify Blacklist: StatifyBlacklist class
*
* This file contains the plugin's base class.
*
* @package Statify_Blacklist
* @since 1.0.0
*/
/* Quit */ // Quit.
defined( 'ABSPATH' ) OR exit; defined( 'ABSPATH' ) OR exit;
/** /**
* Statify Blacklist * Statify Blacklist.
* *
* @since 1.0.0 * @since 1.0.0
* @version 1.4.0
*/ */
class StatifyBlacklist { class StatifyBlacklist {
/**
* Plugin major version.
*
* @since 1.4.0
* @var int VERSION_MAIN
*/
const VERSION_MAIN = 1.4; const VERSION_MAIN = 1.4;
/** /**
* Plugin options * Plugin options.
* *
* @var array
* @since 1.0.0 * @since 1.0.0
* @var array $_options
*/ */
public static $_options; public static $_options;
/** /**
* Multisite Status * Multisite Status.
* *
* @var bool
* @since 1.0.0 * @since 1.0.0
* @var bool $multisite
*/ */
public static $multisite; public static $multisite;
/** /**
* Class self initialize * Class self initialize.
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@ -39,50 +52,52 @@ class StatifyBlacklist {
} }
/** /**
* Class constructor * Class constructor.
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function __construct() { public function __construct() {
/* Skip on autosave or AJAX */ // Skip on autosave or AJAX.
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return; return;
} }
/* Plugin options */ // Plugin options.
self::update_options(); self::update_options();
/* Get multisite status */ // Get multisite status.
self::$multisite = ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ); self::$multisite = ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) );
/* Add Filter to statify hook if enabled */ // Add Filter to statify hook if enabled.
if ( self::$_options['referer']['active'] != 0 ) { if ( self::$_options['referer']['active'] != 0 ) {
add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) ); add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) );
} }
/* Admin only filters */ // Admin only filters.
if ( is_admin() ) { if ( is_admin() ) {
/* Load Textdomain (only needed for backend */ // Load Textdomain (only needed for backend.
load_plugin_textdomain( 'statifyblacklist', false, STATIFYBLACKLIST_DIR . '/lang/' ); load_plugin_textdomain( 'statifyblacklist', false, STATIFYBLACKLIST_DIR . '/lang/' );
/* Add actions */ // Add actions.
add_action( 'wpmu_new_blog', array( 'StatifyBlacklist_Install', 'init_site' ) ); add_action( 'wpmu_new_blog', array( 'StatifyBlacklist_Install', 'init_site' ) );
add_action( 'delete_blog', array( 'StatifyBlacklist_System', 'init_site' ) ); add_action( 'delete_blog', array( 'StatifyBlacklist_System', 'init_site' ) );
add_filter( 'plugin_row_meta', array( 'StatifyBlacklist_Admin', 'plugin_meta_link' ), 10, 2 ); add_filter( 'plugin_row_meta', array( 'StatifyBlacklist_Admin', 'plugin_meta_link' ), 10, 2 );
if ( is_multisite() ) { if ( is_multisite() ) {
add_action( 'network_admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) ); add_action( 'network_admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) );
add_filter( 'network_admin_plugin_action_links', array( add_filter(
'network_admin_plugin_action_links', array(
'StatifyBlacklist_Admin', 'StatifyBlacklist_Admin',
'plugin_actions_links' 'plugin_actions_links'
), 10, 2 ); ), 10, 2
);
} else { } else {
add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) ); add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) );
add_filter( 'plugin_action_links', array( 'StatifyBlacklist_Admin', 'plugin_actions_links' ), 10, 2 ); add_filter( 'plugin_action_links', array( 'StatifyBlacklist_Admin', 'plugin_actions_links' ), 10, 2 );
} }
} }
/* CronJob to clean up database */ // CronJob to clean up database.
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
if ( self::$_options['referer']['cron'] == 1 || self::$_options['target']['cron'] == 1 ) { if ( self::$_options['referer']['cron'] == 1 || self::$_options['target']['cron'] == 1 ) {
add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) ); add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) );
@ -91,12 +106,13 @@ class StatifyBlacklist {
} }
/** /**
* Update options * Update options.
*
* @param array $options New options to save
* *
* @since 1.0.0 * @since 1.0.0
* @since 1.2.1 update_options($options = null) Parameter with default value introduced * @since 1.2.1 update_options($options = null) Parameter with default value introduced.
*
* @param array $options New options to save.
*
*/ */
public static function update_options( $options = null ) { public static function update_options( $options = null ) {
self::$_options = wp_parse_args( self::$_options = wp_parse_args(
@ -110,7 +126,7 @@ class StatifyBlacklist {
* *
* @since 1.4.0 * @since 1.4.0
* *
* @return array the options array * @return array The options array.
*/ */
protected static function defaultOptions() { protected static function defaultOptions() {
return array( return array(
@ -137,68 +153,68 @@ class StatifyBlacklist {
/** /**
* Apply the blacklist filter if active * Apply the blacklist filter if active
* *
* @return bool TRUE if referer matches blacklist.
*
* @since 1.0.0 * @since 1.0.0
*
* @return bool TRUE if referer matches blacklist.
*/ */
public static function apply_blacklist_filter() { public static function apply_blacklist_filter() {
/* Referer blacklist */ // Referer blacklist.
if ( isset( self::$_options['referer']['active'] ) && self::$_options['referer']['active'] != 0 ) { if ( isset( self::$_options['referer']['active'] ) && self::$_options['referer']['active'] != 0 ) {
/* Regular Expression filtering since 1.3.0 */ // Regular Expression filtering since 1.3.0.
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) { if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) {
/* Get full referer string */ // Get full referer string.
$referer = ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ); $referer = ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' );
/* Merge given regular expressions into one */ // Merge given regular expressions into one.
$regexp = '/' . implode( "|", array_keys( self::$_options['referer']['blacklist'] ) ) . '/'; $regexp = '/' . implode( "|", array_keys( self::$_options['referer']['blacklist'] ) ) . '/';
if ( self::$_options['referer']['regexp'] == 2 ) { if ( self::$_options['referer']['regexp'] == 2 ) {
$regexp .= 'i'; $regexp .= 'i';
} }
/* Check blacklist (return NULL to continue filtering) */ // Check blacklist (return NULL to continue filtering).
return ( preg_match( $regexp, $referer ) === 1 ) ? true : null; return ( preg_match( $regexp, $referer ) === 1 ) ? true : null;
} else { } else {
/* Extract relevant domain parts */ // Extract relevant domain parts.
$referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) ); $referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) );
/* Get blacklist */ // Get blacklist.
$blacklist = self::$_options['referer']['blacklist']; $blacklist = self::$_options['referer']['blacklist'];
/* Check blacklist */ // Check blacklist.
if ( isset( $blacklist[ $referer ] ) ) { if ( isset( $blacklist[$referer] ) ) {
return true; return true;
} }
} }
} }
/* Target blacklist (since 1.4.0) */ // Target blacklist (since 1.4.0)
if ( isset( self::$_options['target']['active'] ) && self::$_options['target']['active'] != 0 ) { if ( isset( self::$_options['target']['active'] ) && self::$_options['target']['active'] != 0 ) {
/* Regular Expression filtering since 1.3.0 */ // Regular Expression filtering since 1.3.0.
if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) { if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) {
/* Get full referer string */ // Get full referer string.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' ); $target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' );
/* Merge given regular expressions into one */ // Merge given regular expressions into one
$regexp = '/' . implode( "|", array_keys( self::$_options['target']['blacklist'] ) ) . '/'; $regexp = '/' . implode( "|", array_keys( self::$_options['target']['blacklist'] ) ) . '/';
if ( self::$_options['target']['regexp'] == 2 ) { if ( self::$_options['target']['regexp'] == 2 ) {
$regexp .= 'i'; $regexp .= 'i';
} }
/* Check blacklist (return NULL to continue filtering) */ // Check blacklist (return NULL to continue filtering).
return ( preg_match( $regexp, $target ) === 1 ) ? true : null; return ( preg_match( $regexp, $target ) === 1 ) ? true : null;
} else { } else {
/* Extract target page */ // Extract target page.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' ); $target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' );
/* Get blacklist */ // Get blacklist.
$blacklist = self::$_options['target']['blacklist']; $blacklist = self::$_options['target']['blacklist'];
/* Check blacklist */ // Check blacklist.
if ( isset( $blacklist[ $target ] ) ) { if ( isset( $blacklist[$target] ) ) {
return true; return true;
} }
} }
} }
/* IP blacklist (since 1.4.0) */ // IP blacklist (since 1.4.0).
if ( isset ( self::$_options['ip']['active'] ) && self::$_options['ip']['active'] != 0 ) { if ( isset ( self::$_options['ip']['active'] ) && self::$_options['ip']['active'] != 0 ) {
if ( ( $ip = self::getIP() ) !== false ) { if ( ( $ip = self::getIP() ) !== false ) {
foreach ( self::$_options['ip']['blacklist'] as $net ) { foreach ( self::$_options['ip']['blacklist'] as $net ) {
@ -209,34 +225,35 @@ class StatifyBlacklist {
} }
} }
/* Skip and continue (return NULL), if all blacklists are inactive */ // Skip and continue (return NULL), if all blacklists are inactive.
return null; return null;
} }
/** /**
* Helper method to determine the client's IP address. * Helper method to determine the client's IP address.
*
* If a proxy is used, the X-Real-IP or X-Forwarded-For header is checked, otherwise the default remote address. * If a proxy is used, the X-Real-IP or X-Forwarded-For header is checked, otherwise the default remote address.
* For performance reasons only the most common flags are checked. This might be even reduce by user configuration. * For performance reasons only the most common flags are checked. This might be even reduce by user configuration.
* Maybe some community feedback will ease the decision on that. * Maybe some community feedback will ease the decision on that.
* *
* @return string|bool the client's IP address or FALSE, if none could be determined * @return string|bool the client's IP address or FALSE, if none could be determined.
*/ */
private static function getIP() { private static function getIP() {
foreach ( foreach (
array( array(
// 'HTTP_CLIENT_IP', // 'HTTP_CLIENT_IP',
'HTTP_X_REAL_IP', 'HTTP_X_REAL_IP',
'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR',
// 'HTTP_X_FORWARDED', // 'HTTP_X_FORWARDED',
// 'HTTP_X_CLUSTER_CLIENT_IP', // 'HTTP_X_CLUSTER_CLIENT_IP',
// 'HTTP_FORWARDED_FOR', // 'HTTP_FORWARDED_FOR',
// 'HTTP_FORWARDED', // 'HTTP_FORWARDED',
'REMOTE_ADDR' 'REMOTE_ADDR'
) as $k ) as $k
) { ) {
if ( isset( $_SERVER[ $k ] ) ) { if ( isset( $_SERVER[$k] ) ) {
foreach ( explode( ',', $_SERVER[ $k ] ) as $ip ) { foreach ( explode( ',', $_SERVER[$k] ) as $ip ) {
if ( filter_var( $ip, FILTER_VALIDATE_IP ) !== false ) { if ( filter_var( $ip, FILTER_VALIDATE_IP ) !== false ) {
return $ip; return $ip;
} }
@ -250,18 +267,18 @@ class StatifyBlacklist {
/** /**
* Helper function to check if an IP address matches a given subnet. * Helper function to check if an IP address matches a given subnet.
* *
* @param string $ip IP address to check * @param string $ip IP address to check.
* @param string $net IP address or subnet in CIDR notation * @param string $net IP address or subnet in CIDR notation.
* *
* @return bool TRUE, if the given IP addresses matches the given subnet * @return bool TRUE, if the given IP addresses matches the given subnet.
*/ */
private static function cidrMatch( $ip, $net ) { private static function cidrMatch( $ip, $net ) {
if ( substr_count( $net, ':' ) > 1 ) { /* Check for IPv6 */ if ( substr_count( $net, ':' ) > 1 ) { // Check for IPv6.
if ( ! ( ( extension_loaded( 'sockets' ) && defined( 'AF_INET6' ) ) || @inet_pton( '::1' ) ) ) { if ( ! ( ( extension_loaded( 'sockets' ) && defined( 'AF_INET6' ) ) || @inet_pton( '::1' ) ) ) {
return false; return false;
} }
if ( false !== strpos( $net, '/' ) ) { /* Parse CIDR subnet */ if ( false !== strpos( $net, '/' ) ) { // Parse CIDR subnet.
list( $base, $mask ) = explode( '/', $net, 2 ); list( $base, $mask ) = explode( '/', $net, 2 );
if ( $mask < 1 || $mask > 128 ) { if ( $mask < 1 || $mask > 128 ) {
@ -283,18 +300,18 @@ class StatifyBlacklist {
$left = $mask - 16 * ( $i - 1 ); $left = $mask - 16 * ( $i - 1 );
$left = ( $left <= 16 ) ? $left : 16; $left = ( $left <= 16 ) ? $left : 16;
$maskB = ~( 0xffff >> $left ) & 0xffff; $maskB = ~( 0xffff >> $left ) & 0xffff;
if ( ( $bytesAddr[ $i ] & $maskB ) != ( $bytesTest[ $i ] & $maskB ) ) { if ( ( $bytesAddr[$i] & $maskB ) != ( $bytesTest[$i] & $maskB ) ) {
return false; return false;
} }
} }
return true; return true;
} else { /* Check for IPv4 */ } else { // Check for IPv4.
if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) { if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
return false; return false;
} }
if ( false !== strpos( $net, '/' ) ) { /* Parse CIDR subnet */ if ( false !== strpos( $net, '/' ) ) { // Parse CIDR subnet.
list( $base, $mask ) = explode( '/', $net, 2 ); list( $base, $mask ) = explode( '/', $net, 2 );
if ( $mask === '0' ) { if ( $mask === '0' ) {
@ -304,7 +321,7 @@ class StatifyBlacklist {
if ( $mask < 0 || $mask > 32 ) { if ( $mask < 0 || $mask > 32 ) {
return false; return false;
} }
} else { /* Use single address */ } else { // Use single address.
$base = $net; $base = $net;
$mask = 32; $mask = 32;
} }

View File

@ -1,26 +1,34 @@
<?php <?php
/**
* Statify Blacklist: StatifyBlacklist_Admin class
*
* This file contains the derived class for the plugin's administration features.
*
* @package Statify_Blacklist
* @subpackge Admin
* @since 1.0.0
*/
/* Quit */ // Quit.
defined( 'ABSPATH' ) OR exit; defined( 'ABSPATH' ) OR exit;
/** /**
* Statify Blacklist admin configuration * Statify Blacklist admin configuration.
* *
* @since 1.0.0 * @since 1.0.0
* @version 1.4.0
*/ */
class StatifyBlacklist_Admin extends StatifyBlacklist { class StatifyBlacklist_Admin extends StatifyBlacklist {
/** /**
* Update options * Update options.
* *
* @param array $options New options to save * @param array $options New options to save.
* *
* @return array|bool array of sanitized array on errors, FALSE if there were none * @return array|bool array of sanitized array on errors, FALSE if there were none.
* @since 1.1.1 * @since 1.1.1
*/ */
public static function update_options( $options = null ) { public static function update_options( $options = null ) {
if ( isset( $options ) && current_user_can( 'manage_options' ) ) { if ( isset( $options ) && current_user_can( 'manage_options' ) ) {
/* Sanitize URLs and remove empty inputs */ // Sanitize URLs and remove empty inputs.
$givenReferer = $options['referer']['blacklist']; $givenReferer = $options['referer']['blacklist'];
if ( $options['referer']['regexp'] == 0 ) { if ( $options['referer']['regexp'] == 0 ) {
$sanitizedReferer = self::sanitizeURLs( $givenReferer ); $sanitizedReferer = self::sanitizeURLs( $givenReferer );
@ -28,18 +36,18 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$sanitizedReferer = $givenReferer; $sanitizedReferer = $givenReferer;
} }
/* Sanitize IPs and Subnets and remove empty inputs */ // Sanitize IPs and Subnets and remove empty inputs.
$givenIP = $options['ip']['blacklist']; $givenIP = $options['ip']['blacklist'];
$sanitizedIP = self::sanitizeIPs( $givenIP ); $sanitizedIP = self::sanitizeIPs( $givenIP );
/* Abort on errors */ // Abort on errors.
if ( ! empty( array_diff( array_keys( $givenReferer ), array_keys( $sanitizedReferer ) ) ) ) { if ( ! empty( array_diff( array_keys( $givenReferer ), array_keys( $sanitizedReferer ) ) ) ) {
return array( 'referer' => $sanitizedReferer ); return array( 'referer' => $sanitizedReferer );
} elseif ( ! empty( array_diff( $givenIP, $sanitizedIP ) ) ) { } elseif ( ! empty( array_diff( $givenIP, $sanitizedIP ) ) ) {
return array( 'ip' => array_diff( $givenIP, $sanitizedIP ) ); return array( 'ip' => array_diff( $givenIP, $sanitizedIP ) );
} }
/* Update database on success */ // Update database on success.
if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) { if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) {
update_site_option( 'statify-blacklist', $options ); update_site_option( 'statify-blacklist', $options );
} else { } else {
@ -47,33 +55,42 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
} }
} }
/* Refresh options */ // Refresh options.
parent::update_options( $options ); parent::update_options( $options );
return false; return false;
} }
/** /**
* Add configuration page to admin menu * Add configuration page to admin menu.
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function _add_menu_page() { public function _add_menu_page() {
$title = __( 'Statify Blacklist', 'statify-blacklist' ); $title = __( 'Statify Blacklist', 'statify-blacklist' );
if ( self::$multisite ) { if ( self::$multisite ) {
add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'statify-blacklist-settings', array( add_submenu_page(
'StatifyBlacklist_Admin', 'settings.php', $title, $title, 'manage_network_plugins', 'statify-blacklist-settings', array(
'settings_page' 'StatifyBlacklist_Admin',
) ); 'settings_page'
)
);
} else { } else {
add_submenu_page( 'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array( add_submenu_page(
'StatifyBlacklist_Admin', 'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array(
'settings_page' 'StatifyBlacklist_Admin',
) ); 'settings_page'
)
);
} }
} }
/**
* Include the Statify-Blacklist settings page.
*
* @since 1.0.0
*/
public static function settings_page() { public static function settings_page() {
include STATIFYBLACKLIST_DIR . '/views/settings_page.php'; include STATIFYBLACKLIST_DIR . '/views/settings_page.php';
} }
@ -81,12 +98,13 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
/** /**
* Add plugin meta links * Add plugin meta links
* *
* @param array $links Registered links
* @param string $file The filename
*
* @return array Merged links
*
* @since 1.0.0 * @since 1.0.0
*
* @param array $links Registered links.
* @param string $file The filename.
*
* @return array Merged links.
*
*/ */
public static function plugin_meta_link( $links, $file ) { public static function plugin_meta_link( $links, $file ) {
if ( $file == STATIFYBLACKLIST_BASE ) { if ( $file == STATIFYBLACKLIST_BASE ) {
@ -97,14 +115,15 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
} }
/** /**
* Add plugin action links * Add plugin action links.
*
* @param array $links Registered links
* @param string $file The filename
*
* @return array Merged links
* *
* @since 1.0.0 * @since 1.0.0
*
* @param array $links Registered links
* @param string $file The filename
*
* @return array Merged links.
*
*/ */
public static function plugin_actions_links( $links, $file ) { public static function plugin_actions_links( $links, $file ) {
$base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' ); $base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
@ -123,9 +142,11 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* Filter database for cleanup. * Filter database for cleanup.
* *
* @since 1.1.0 * @since 1.1.0
*
* @global wpdb $wpdb WordPress database.
*/ */
public static function cleanup_database() { public static function cleanup_database() {
/* Check user permissions */ // Check user permissions.
if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
die( __( 'Are you sure you want to do this?' ) ); die( __( 'Are you sure you want to do this?' ) );
} }
@ -141,23 +162,23 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
if ( $cleanRef ) { if ( $cleanRef ) {
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) { if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) {
/* Merge given regular expressions into one */ // Merge given regular expressions into one.
$refererRegexp = implode( "|", array_keys( self::$_options['referer']['blacklist'] ) ); $refererRegexp = implode( "|", array_keys( self::$_options['referer']['blacklist'] ) );
} else { } else {
/* Sanitize URLs */ // Sanitize URLs.
$referer = self::sanitizeURLs( self::$_options['referer']['blacklist'] ); $referer = self::sanitizeURLs( self::$_options['referer']['blacklist'] );
/* Build filter regexp */ // Build filter regexp.
$refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); $refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
} }
} }
if ( $cleanTrg ) { if ( $cleanTrg ) {
if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) { if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) {
/* Merge given regular expressions into one */ // Merge given regular expressions into one.
$targetRegexp = implode( "|", array_keys( self::$_options['target']['blacklist'] ) ); $targetRegexp = implode( "|", array_keys( self::$_options['target']['blacklist'] ) );
} else { } else {
/* Build filter regexp */ // Build filter regexp.
$targetRegexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) ); $targetRegexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) );
} }
} }
@ -166,39 +187,44 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
if ( ! empty( $refererRegexp ) || ! empty( $targetRegexp ) ) { if ( ! empty( $refererRegexp ) || ! empty( $targetRegexp ) ) {
global $wpdb; global $wpdb;
/* Execute filter on database */ // Execute filter on database.
if ( ! empty( $refererRegexp ) ) { if ( ! empty( $refererRegexp ) ) {
$wpdb->query( $wpdb->query(
$wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE " $wpdb->prepare(
. ( ( self::$_options['referer']['regexp'] == 1 ) ? " BINARY " : "" ) "DELETE FROM `$wpdb->statify` WHERE "
. "referrer REGEXP %s", $refererRegexp ) . ( ( self::$_options['referer']['regexp'] == 1 ) ? " BINARY " : "" )
. "referrer REGEXP %s", $refererRegexp
)
); );
} }
if ( ! empty( $targetRegexp ) ) { if ( ! empty( $targetRegexp ) ) {
$wpdb->query( $wpdb->query(
$wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE " $wpdb->prepare(
. ( ( self::$_options['target']['regexp'] == 1 ) ? " BINARY " : "" ) "DELETE FROM `$wpdb->statify` WHERE "
. "target REGEXP %s", $targetRegexp ) . ( ( self::$_options['target']['regexp'] == 1 ) ? " BINARY " : "" )
. "target REGEXP %s", $targetRegexp
)
); );
} }
/* Optimize DB */ // Optimize DB.
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" ); $wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
/* Delete transient statify data */ // Delete transient statify data.
delete_transient( 'statify_data' ); delete_transient( 'statify_data' );
} }
} }
/** /**
* Sanitize URLs and remove empty results * Sanitize URLs and remove empty results.
*
* @param array $urls given array of URLs
*
* @return array sanitized array
* *
* @since 1.1.1 * @since 1.1.1
*
* @param array $urls given array of URLs.
*
* @return array sanitized array.
*
*/ */
private static function sanitizeURLs( $urls ) { private static function sanitizeURLs( $urls ) {
return array_flip( return array_flip(
@ -214,20 +240,27 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
} }
/** /**
* Sanitize IP addresses with optional CIDR notation and remove empty results * Sanitize IP addresses with optional CIDR notation and remove empty results.
*
* @param array $ips given array of URLs
*
* @return array sanitized array
* *
* @since 1.4.0 * @since 1.4.0
*
* @param array $ips given array of URLs.
*
* @return array sanitized array.
*
*/ */
private static function sanitizeIPs( $ips ) { private static function sanitizeIPs( $ips ) {
return array_filter( $ips, function ( $ip ) { return array_filter(
return preg_match( '/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' . $ips, function ( $ip ) {
'(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $ip ) || return preg_match(
preg_match( '/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' . '/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' .
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/', $ip ); '(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $ip
} ); ) ||
preg_match(
'/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' .
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/', $ip
);
}
);
} }
} }

View File

@ -1,13 +1,21 @@
<?php <?php
/**
* Statify Blacklist: StatifyBlacklist_Syste, class
*
* This file contains the derived class for the plugin's system operations.
*
* @package Statify_Blacklist
* @subpackge System
* @since 1.0.0
*/
/* Quit */ // Quit.
defined( 'ABSPATH' ) OR exit; defined( 'ABSPATH' ) OR exit;
/** /**
* Statify Blacklist system configuration * Statify Blacklist system configuration.
* *
* @since 1.0.0 * @since 1.0.0
* @version 1.4.0
*/ */
class StatifyBlacklist_System extends StatifyBlacklist { class StatifyBlacklist_System extends StatifyBlacklist {
@ -24,7 +32,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
if ( function_exists( 'get_sites' ) ) { if ( function_exists( 'get_sites' ) ) {
$sites = get_sites(); $sites = get_sites();
} elseif ( function_exists( 'wp_get_sites' ) ) { } elseif ( function_exists( 'wp_get_sites' ) ) {
$sites = wp_get_sites(); /* legacy support for WP < 4.6 */ $sites = wp_get_sites(); // legacy support for WP < 4.6.
} else { } else {
return; return;
} }
@ -59,7 +67,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
if ( function_exists( 'get_sites' ) ) { if ( function_exists( 'get_sites' ) ) {
$sites = get_sites(); $sites = get_sites();
} elseif ( function_exists( 'wp_get_sites' ) ) { } elseif ( function_exists( 'wp_get_sites' ) ) {
$sites = wp_get_sites(); /* legacy support for WP < 4.6 */ $sites = wp_get_sites(); // legacy support for WP < 4.6.
} else { } else {
return; return;
} }
@ -83,9 +91,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
*/ */
public static function upgrade() { public static function upgrade() {
self::update_options(); self::update_options();
/* Check if config array is not associative (pre 1.2.0) */ // Check if config array is not associative (pre 1.2.0).
if ( array_keys( self::$_options['referer'] ) === range( 0, count( self::$_options['referer'] ) - 1 ) ) { if ( array_keys( self::$_options['referer'] ) === range( 0, count( self::$_options['referer'] ) - 1 ) ) {
/* Flip referer array to make domains keys */ // Flip referer array to make domains keys.
$options = self::$_options; $options = self::$_options;
$options['referer'] = array_flip( self::$_options['referer'] ); $options['referer'] = array_flip( self::$_options['referer'] );
if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) { if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) {
@ -95,9 +103,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} }
} }
/* Version not set (pre 1.3.0) or older than 1.4 */ // Version not set (pre 1.3.0) or older than 1.4.
if ( ! isset( self::$_options['version'] ) || self::$_options['version'] < 1.4 ) { if ( ! isset( self::$_options['version'] ) || self::$_options['version'] < 1.4 ) {
/* Upgrade options to new schema */ // Upgrade options to new schema.
$options = array( $options = array(
'referer' => array( 'referer' => array(
'active' => self::$_options['active_referer'], 'active' => self::$_options['active_referer'],
@ -125,9 +133,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
self::update_options(); self::update_options();
} }
/* Version older than current major release */ // Version older than current major release.
if ( self::$_options['version'] < self::VERSION_MAIN ) { if ( self::$_options['version'] < self::VERSION_MAIN ) {
/* Merge default options with current config, assuming only additive changes */ // Merge default options with current config, assuming only additive changes.
$options = array_merge_recursive( self::defaultOptions(), self::$_options ); $options = array_merge_recursive( self::defaultOptions(), self::$_options );
$options['version'] = self::VERSION_MAIN; $options['version'] = self::VERSION_MAIN;
if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) { if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) {

View File

@ -1,41 +1,55 @@
<?php <?php
/* /*
Plugin Name: Statify Blacklist Plugin Name: Statify Blacklist
Description: Extension for the statify plugin to add a customizable blacklists. Plugin URI: https://de.wordpress.org/plugins/statify-blacklist/
Text Domain: statify-blacklist Description: Extension for the Statify plugin to add a customizable blacklists.
Domain Path: /lang Version: 1.4.0
Author: Stefan Kalscheuer Author: Stefan Kalscheuer (@stklcode)
Author URI: https://www.stklcode.de Author URI: https://www.stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist Plugin URI: https://wordpress.org/plugins/statify-blacklist
License: GPLv3 or later Text Domain: statify-blacklist
Version: 1.4.0 Domain Path: /lang
License: GPLv2 or later
Statify Blacklist is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
Statify Blacklist is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Statify Blacklist. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
*/ */
/* Quit */ // Quit
defined( 'ABSPATH' ) OR exit; defined( 'ABSPATH' ) OR exit;
/* Constants */ // Constants
define( 'STATIFYBLACKLIST_FILE', __FILE__ ); define( 'STATIFYBLACKLIST_FILE', __FILE__ );
define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) ); define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) );
define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) ); define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) );
/* System Hooks */ // System Hooks.
add_action( 'plugins_loaded', array( 'StatifyBlacklist', 'instance' ) ); add_action( 'plugins_loaded', array( 'StatifyBlacklist', 'instance' ) );
register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'install' ) ); register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'install' ) );
register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) ); register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) );
/* Upgrade hook */ // Upgrade hook
register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) ); register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) );
/* Autoload */ // Autoload
spl_autoload_register( 'statifyBlacklist_autoload' ); spl_autoload_register( 'statifyBlacklist_autoload' );
/** /**
* Autoloader for StatifyBlacklist classes. * Autoloader for StatifyBlacklist classes.
* *
* @param string $class name of the class to load * @param string $class Name of the class to load.
* *
* @since 1.0.0 * @since 1.0.0
*/ */

View File

@ -1,16 +1,43 @@
<?php <?php
/**
* Statify Blacklist: Unit Test
*
* This is a PHPunit test class for the plugin's functionality
*
* @package Statify_Blacklist
* @subpackage Admin
* @since 1.3.0
*/
/**
* Simulating the ABSPATH constant.
*
* @since 1.3.0
* @var bool ABSPATH
*/
const ABSPATH = false; const ABSPATH = false;
/**
* The StatifyBlacklist base class.
*/
require_once( '../inc/statifyblacklist.class.php' ); require_once( '../inc/statifyblacklist.class.php' );
/**
* The StatifyBlacklist system class.
*/
require_once( '../inc/statifyblacklist_system.class.php' ); require_once( '../inc/statifyblacklist_system.class.php' );
/**
* The StatifyBlacklist admin class.
*/
require_once( '../inc/statifyblacklist_admin.class.php' ); require_once( '../inc/statifyblacklist_admin.class.php' );
/** /**
* Class StatifyBlacklistTest * Class StatifyBlacklistTest.
* *
* PHPUnit test class for StatifyBlacklist * PHPUnit test class for StatifyBlacklist.
* *
* @version 1.4.0 * @since 1.3.0
*/ */
class StatifyBlacklistTest extends PHPUnit_Framework_TestCase { class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
@ -18,47 +45,47 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
* Test simple referer filter. * Test simple referer filter.
*/ */
public function testRefererFilter() { public function testRefererFilter() {
/* Prepare Options: 2 blacklisted domains, disabled */ // Prepare Options: 2 blacklisted domains, disabled
StatifyBlacklist::$_options = array( StatifyBlacklist::$_options = array(
'referer' => array( 'referer' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array( 'blacklist' => array(
'example.com' => 0, 'example.com' => 0,
'example.net' => 1 'example.net' => 1
) )
), ),
'target' => array( 'target' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'ip' => array( 'ip' => array(
'active' => 0, 'active' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'version' => StatifyBlacklist::VERSION_MAIN 'version' => StatifyBlacklist::VERSION_MAIN
); );
/* No multisite */ // No multisite.
StatifyBlacklist::$multisite = false; StatifyBlacklist::$multisite = false;
/* No referer */ // No referer.
unset( $_SERVER['HTTP_REFERER'] ); unset( $_SERVER['HTTP_REFERER'] );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Non-blacklisted referer */ // Non-blacklisted referer.
$_SERVER['HTTP_REFERER'] = 'http://example.org'; $_SERVER['HTTP_REFERER'] = 'http://example.org';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer */ // Blacklisted referer.
$_SERVER['HTTP_REFERER'] = 'http://example.com'; $_SERVER['HTTP_REFERER'] = 'http://example.com';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer with path */ // Blacklisted referer with path.
$_SERVER['HTTP_REFERER'] = 'http://example.net/foo/bar.html'; $_SERVER['HTTP_REFERER'] = 'http://example.net/foo/bar.html';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Activate filter and run tests again */ // Activate filter and run tests again.
StatifyBlacklist::$_options['referer']['active'] = 1; StatifyBlacklist::$_options['referer']['active'] = 1;
unset( $_SERVER['HTTP_REFERER'] ); unset( $_SERVER['HTTP_REFERER'] );
@ -78,53 +105,53 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
* Test referer filter using regular expressions. * Test referer filter using regular expressions.
*/ */
public function testRefererRegexFilter() { public function testRefererRegexFilter() {
/* Prepare Options: 2 regular expressions */ // Prepare Options: 2 regular expressions.
StatifyBlacklist::$_options = array( StatifyBlacklist::$_options = array(
'referer' => array( 'referer' => array(
'active' => 1, 'active' => 1,
'cron' => 0, 'cron' => 0,
'regexp' => 1, 'regexp' => 1,
'blacklist' => array( 'blacklist' => array(
'example.[a-z]+' => 0, 'example.[a-z]+' => 0,
'test' => 1 'test' => 1
) )
), ),
'target' => array( 'target' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'ip' => array( 'ip' => array(
'active' => 0, 'active' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'version' => StatifyBlacklist::VERSION_MAIN 'version' => StatifyBlacklist::VERSION_MAIN
); );
/* No multisite */ // No multisite.
StatifyBlacklist::$multisite = false; StatifyBlacklist::$multisite = false;
/* No referer */ // No referer.
unset( $_SERVER['HTTP_REFERER'] ); unset( $_SERVER['HTTP_REFERER'] );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Non-blacklisted referer */ // Non-blacklisted referer.
$_SERVER['HTTP_REFERER'] = 'http://not.evil'; $_SERVER['HTTP_REFERER'] = 'http://not.evil';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer */ // Blacklisted referer.
$_SERVER['HTTP_REFERER'] = 'http://example.com'; $_SERVER['HTTP_REFERER'] = 'http://example.com';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer with path */ // Blacklisted referer with path.
$_SERVER['HTTP_REFERER'] = 'http://foobar.net/test/me'; $_SERVER['HTTP_REFERER'] = 'http://foobar.net/test/me';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
/* Matching both */ // Matching both.
$_SERVER['HTTP_REFERER'] = 'http://example.net/test/me'; $_SERVER['HTTP_REFERER'] = 'http://example.net/test/me';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
/* Mathinc with wrong case */ // Mathinc with wrong case.
$_SERVER['HTTP_REFERER'] = 'http://eXaMpLe.NeT/tEsT/mE'; $_SERVER['HTTP_REFERER'] = 'http://eXaMpLe.NeT/tEsT/mE';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Set RegExp filter to case insensitive */ // Set RegExp filter to case insensitive.
StatifyBlacklist::$_options['referer']['regexp'] = 2; StatifyBlacklist::$_options['referer']['regexp'] = 2;
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
} }
@ -133,7 +160,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
* Test the upgrade methodology for configuration options. * Test the upgrade methodology for configuration options.
*/ */
public function testUpgrade() { public function testUpgrade() {
/* Create configuration of version 1.3 */ // Create configuration of version 1.3.
$options13 = array( $options13 = array(
'active_referer' => 1, 'active_referer' => 1,
'cron_referer' => 0, 'cron_referer' => 0,
@ -145,28 +172,28 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'version' => 1.3 'version' => 1.3
); );
/* Set options in mock */ // Set options in mock.
update_option( 'statify-blacklist', $options13 ); update_option( 'statify-blacklist', $options13 );
/* Execute upgrade */ // Execute upgrade.
StatifyBlacklist_System::upgrade(); StatifyBlacklist_System::upgrade();
/* Retrieve updated options */ // Retrieve updated options.
$optionsUpdated = get_option( 'statify-blacklist' ); $optionsUpdated = get_option( 'statify-blacklist' );
/* Verify size against default options (no junk left) */ // Verify size against default options (no junk left).
$this->assertEquals( 4, sizeof( $optionsUpdated ) ); $this->assertEquals( 4, sizeof( $optionsUpdated ) );
$this->assertEquals( 4, sizeof( $optionsUpdated['referer'] ) ); $this->assertEquals( 4, sizeof( $optionsUpdated['referer'] ) );
$this->assertEquals( 4, sizeof( $optionsUpdated['target'] ) ); $this->assertEquals( 4, sizeof( $optionsUpdated['target'] ) );
$this->assertEquals( 2, sizeof( $optionsUpdated['ip'] ) ); $this->assertEquals( 2, sizeof( $optionsUpdated['ip'] ) );
/* Verify that original attributes are unchanged */ // Verify that original attributes are unchanged.
$this->assertEquals( $options13['active_referer'], $optionsUpdated['referer']['active'] ); $this->assertEquals( $options13['active_referer'], $optionsUpdated['referer']['active'] );
$this->assertEquals( $options13['cron_referer'], $optionsUpdated['referer']['cron'] ); $this->assertEquals( $options13['cron_referer'], $optionsUpdated['referer']['cron'] );
$this->assertEquals( $options13['referer'], $optionsUpdated['referer']['blacklist'] ); $this->assertEquals( $options13['referer'], $optionsUpdated['referer']['blacklist'] );
$this->assertEquals( $options13['referer_regexp'], $optionsUpdated['referer']['regexp'] ); $this->assertEquals( $options13['referer_regexp'], $optionsUpdated['referer']['regexp'] );
/* Verify that new attributes are present in config and filled with default values (disabled, empty) */ // Verify that new attributes are present in config and filled with default values (disabled, empty).
$this->assertEquals( 0, $optionsUpdated['target']['active'] ); $this->assertEquals( 0, $optionsUpdated['target']['active'] );
$this->assertEquals( 0, $optionsUpdated['target']['cron'] ); $this->assertEquals( 0, $optionsUpdated['target']['cron'] );
$this->assertEquals( 0, $optionsUpdated['target']['regexp'] ); $this->assertEquals( 0, $optionsUpdated['target']['regexp'] );
@ -174,7 +201,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
$this->assertEquals( 0, $optionsUpdated['ip']['active'] ); $this->assertEquals( 0, $optionsUpdated['ip']['active'] );
$this->assertEquals( array(), $optionsUpdated['ip']['blacklist'] ); $this->assertEquals( array(), $optionsUpdated['ip']['blacklist'] );
/* Verify that version number has changed to current release */ // Verify that version number has changed to current release.
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $optionsUpdated['version'] ); $this->assertEquals( StatifyBlacklist::VERSION_MAIN, $optionsUpdated['version'] );
} }
@ -182,63 +209,103 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
* Test CIDR address matching for IP filter (#7) * Test CIDR address matching for IP filter (#7)
*/ */
public function testCidrMatch() { public function testCidrMatch() {
/* IPv4 tests */ // IPv4 tests.
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1', '127.0.0.1' ) ) ); $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1', '127.0.0.1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1', '127.0.0.1/32' ) ) ); $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1', '127.0.0.1/32' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( $this->assertFalse(
'127.0.0.1', invokeStatic(
'127.0.0.1/33' StatifyBlacklist::class, 'cidrMatch', array(
) ) ); '127.0.0.1',
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1/33'
'127.0.0.1', )
'127.0.0.1/-1' )
) ) ); );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( $this->assertFalse(
'192.0.2.123', invokeStatic(
'192.0.2.0/24' StatifyBlacklist::class, 'cidrMatch', array(
) ) ); '127.0.0.1',
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1/-1'
'192.0.3.123', )
'192.0.2.0/24' )
) ) ); );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( $this->assertTrue(
'192.0.2.123', invokeStatic(
'192.0.2.120/29' StatifyBlacklist::class, 'cidrMatch', array(
) ) ); '192.0.2.123',
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '192.0.2.0/24'
'192.0.2.128', )
'192.0.2.120/29' )
) ) ); );
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
'192.0.3.123',
'192.0.2.0/24'
)
)
);
$this->assertTrue(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
'192.0.2.123',
'192.0.2.120/29'
)
)
);
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
'192.0.2.128',
'192.0.2.120/29'
)
)
);
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '10.11.12.13', '10.0.0.0/8' ) ) ); $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '10.11.12.13', '10.0.0.0/8' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( $this->assertFalse(
'10.11.12.345', invokeStatic(
'10.0.0.0/8' StatifyBlacklist::class, 'cidrMatch', array(
) ) ); '10.11.12.345',
'10.0.0.0/8'
)
)
);
/* IPv6 tests */ // IPv6 tests.
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1' ) ) ); $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/128' ) ) ); $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/128' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/129' ) ) ); $this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/129' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/-1' ) ) ); $this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/-1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( $this->assertTrue(
'2001:db8:a0b:12f0:1:2:3:4', invokeStatic(
'2001:db8:a0b:12f0::1/64 ' StatifyBlacklist::class, 'cidrMatch', array(
) ) ); '2001:db8:a0b:12f0:1:2:3:4',
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '2001:db8:a0b:12f0::1/64 '
'2001:db8:a0b:12f0::123:456', )
'2001:db8:a0b:12f0::1/96 ' )
) ) ); );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( $this->assertTrue(
'2001:db8:a0b:12f0::1:132:465', invokeStatic(
'2001:db8:a0b:12f0::1/96 ' StatifyBlacklist::class, 'cidrMatch', array(
) ) ); '2001:db8:a0b:12f0::123:456',
'2001:db8:a0b:12f0::1/96 '
)
)
);
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
'2001:db8:a0b:12f0::1:132:465',
'2001:db8:a0b:12f0::1/96 '
)
)
);
} }
/** /**
* Test sanitization of IP addresses * Test sanitization of IP addresses
*/ */
public function testSanitizeIPs() { public function testSanitizeIPs() {
/* IPv4 tests */ // IPv4 tests.
$valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' ); $valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' );
$invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' ); $invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' );
$result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); $result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) );
@ -246,7 +313,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
$this->assertInternalType( 'array', $result ); $this->assertInternalType( 'array', $result );
$this->assertEquals( $valid, $result ); $this->assertEquals( $valid, $result );
/* IPv6 tests */ // IPv6 tests.
$valid = array( $valid = array(
'2001:db8:a0b:12f0::', '2001:db8:a0b:12f0::',
'2001:db8:a0b:12f0::1', '2001:db8:a0b:12f0::1',
@ -270,22 +337,22 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
* Test IP filter (#7). * Test IP filter (#7).
*/ */
public function testIPFilter() { public function testIPFilter() {
/* Prepare Options: 2 blacklisted IPs, disabled */ // Prepare Options: 2 blacklisted IPs, disabled.
StatifyBlacklist::$_options = array( StatifyBlacklist::$_options = array(
'referer' => array( 'referer' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'target' => array( 'target' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'ip' => array( 'ip' => array(
'active' => 0, 'active' => 0,
'blacklist' => array( 'blacklist' => array(
'192.0.2.123', '192.0.2.123',
'2001:db8:a0b:12f0::1' '2001:db8:a0b:12f0::1'
@ -294,29 +361,29 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'version' => StatifyBlacklist::VERSION_MAIN 'version' => StatifyBlacklist::VERSION_MAIN
); );
/* No multisite */ // No multisite.
StatifyBlacklist::$multisite = false; StatifyBlacklist::$multisite = false;
/* Set matching IP */ // Set matching IP.
$_SERVER['REMOTE_ADDR'] = '192.0.2.123'; $_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Activate filter */ // Activate filter.
StatifyBlacklist::$_options['ip']['active'] = 1; StatifyBlacklist::$_options['ip']['active'] = 1;
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
/* Try matching v6 address */ // Try matching v6 address.
$_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::1'; $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::1';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
/* Non-matching addresses */ // Non-matching addresses.
$_SERVER['REMOTE_ADDR'] = '192.0.2.234'; $_SERVER['REMOTE_ADDR'] = '192.0.2.234';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::2'; $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::2';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Subnet matching */ // Subnet matching.
StatifyBlacklist::$_options['ip']['blacklist'] = array( StatifyBlacklist::$_options['ip']['blacklist'] = array(
'192.0.2.0/25', '192.0.2.0/25',
'2001:db8:a0b:12f0::/96' '2001:db8:a0b:12f0::/96'
); );
$_SERVER['REMOTE_ADDR'] = '192.0.2.123'; $_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REMOTE_ADDR'] = '192.0.2.234'; $_SERVER['REMOTE_ADDR'] = '192.0.2.234';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
@ -325,7 +392,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
$_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0:0:1111::1'; $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0:0:1111::1';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Filter using proxy header */ // Filter using proxy header.
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.0.2.123'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.0.2.123';
@ -340,50 +407,50 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
* Test simple target filter. * Test simple target filter.
*/ */
public function testTargetFilter() { public function testTargetFilter() {
/* Prepare Options: 2 blacklisted domains, disabled */ // Prepare Options: 2 blacklisted domains, disabled.
StatifyBlacklist::$_options = array( StatifyBlacklist::$_options = array(
'referer' => array( 'referer' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'target' => array( 'target' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
'regexp' => 0, 'regexp' => 0,
'blacklist' => array( 'blacklist' => array(
'/excluded/page/' => 0, '/excluded/page/' => 0,
'/?page_id=3' => 1 '/?page_id=3' => 1
) )
), ),
'ip' => array( 'ip' => array(
'active' => 0, 'active' => 0,
'blacklist' => array() 'blacklist' => array()
), ),
'version' => StatifyBlacklist::VERSION_MAIN 'version' => StatifyBlacklist::VERSION_MAIN
); );
/* No multisite */ // No multisite.
StatifyBlacklist::$multisite = false; StatifyBlacklist::$multisite = false;
/* Empty target */ // Empty target.
unset( $_SERVER['REQUEST_URI'] ); unset( $_SERVER['REQUEST_URI'] );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Non-blacklisted targets */ // Non-blacklisted targets.
$_SERVER['REQUEST_URI'] = ''; $_SERVER['REQUEST_URI'] = '';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REQUEST_URI'] = '/'; $_SERVER['REQUEST_URI'] = '/';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REQUEST_URI'] = '/?page_id=1'; $_SERVER['REQUEST_URI'] = '/?page_id=1';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer */ // Blacklisted referer.
$_SERVER['REQUEST_URI'] = '/excluded/page/'; $_SERVER['REQUEST_URI'] = '/excluded/page/';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REQUEST_URI'] = '/?page_id=3'; $_SERVER['REQUEST_URI'] = '/?page_id=3';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Activate filter and run tests again */ // Activate filter and run tests again.
StatifyBlacklist::$_options['target']['active'] = 1; StatifyBlacklist::$_options['target']['active'] = 1;
unset( $_SERVER['REQUEST_URI'] ); unset( $_SERVER['REQUEST_URI'] );
@ -412,9 +479,8 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
} }
} }
/**
* Helper for testing inaccessible static methods /** @ignore */
*/
function invokeStatic( $class, $methodName, $parameters = array() ) { function invokeStatic( $class, $methodName, $parameters = array() ) {
$reflection = new \ReflectionClass( $class ); $reflection = new \ReflectionClass( $class );
$method = $reflection->getMethod( $methodName ); $method = $reflection->getMethod( $methodName );
@ -424,18 +490,19 @@ function invokeStatic( $class, $methodName, $parameters = array() ) {
} }
/* Some mocked functions */ // Some mocked WP functions.
$mock_options = array(); $mock_options = array();
$mock_multisite = false; $mock_multisite = false;
/** @ignore */
function is_multisite() { function is_multisite() {
global $mock_multisite; global $mock_multisite;
return $mock_multisite; return $mock_multisite;
} }
/** @ignore */
function wp_parse_args( $args, $defaults = '' ) { function wp_parse_args( $args, $defaults = '' ) {
if ( is_object( $args ) ) { if ( is_object( $args ) ) {
$r = get_object_vars( $args ); $r = get_object_vars( $args );
@ -452,17 +519,20 @@ function wp_parse_args( $args, $defaults = '' ) {
return $r; return $r;
} }
/** @ignore */
function get_option( $option, $default = false ) { function get_option( $option, $default = false ) {
global $mock_options; global $mock_options;
return isset( $mock_options[ $option ] ) ? $mock_options[ $option ] : $default; return isset( $mock_options[$option] ) ? $mock_options[$option] : $default;
} }
/** @ignore */
function update_option( $option, $value, $autoload = null ) { function update_option( $option, $value, $autoload = null ) {
global $mock_options; global $mock_options;
$mock_options[ $option ] = $value; $mock_options[$option] = $value;
} }
function wp_unslash ( $value ) { /** @ignore */
function wp_unslash( $value ) {
return is_string( $value ) ? stripslashes( $value ) : $value; return is_string( $value ) ? stripslashes( $value ) : $value;
} }

View File

@ -1,44 +1,53 @@
<?php <?php
/**
* Statify Blacklist: Settings View
*
* This file contains the dynamic HTML skeleton for the plugin's settings page.
*
* @package Statify_Blacklist
* @subpackage Admin
* @since 1.0.0
*/
/* Quit */ // Quit.
defined( 'ABSPATH' ) OR exit; defined( 'ABSPATH' ) OR exit;
/* Update plugin options */ // Update plugin options.
if ( ! empty( $_POST['statifyblacklist'] ) ) { if ( ! empty( $_POST['statifyblacklist'] ) ) {
/* Verify nonce */ // Verify nonce.
check_admin_referer( 'statify-blacklist-settings' ); check_admin_referer( 'statify-blacklist-settings' );
/* Check user capabilities */ // Check user capabilities.
if ( ! current_user_can( 'manage_options' ) ) { if ( ! current_user_can( 'manage_options' ) ) {
die( __( 'Are you sure you want to do this?' ) ); die( __( 'Are you sure you want to do this?' ) );
} }
if ( ! empty( $_POST['cleanUp'] ) ) { if ( ! empty( $_POST['cleanUp'] ) ) {
/* CleanUp DB */ // CleanUp DB.
StatifyBlacklist_Admin::cleanup_database(); StatifyBlacklist_Admin::cleanup_database();
} else { } else {
/* Extract referer array */ // Extract referer array.
if ( empty( trim( $_POST['statifyblacklist']['referer']['blacklist'] ) ) ) { if ( empty( trim( $_POST['statifyblacklist']['referer']['blacklist'] ) ) ) {
$referer = array(); $referer = array();
} else { } else {
$referer = explode( "\r\n", $_POST['statifyblacklist']['referer']['blacklist'] ); $referer = explode( "\r\n", $_POST['statifyblacklist']['referer']['blacklist'] );
} }
/* Extract target array */ // Extract target array.
if ( empty( trim( $_POST['statifyblacklist']['target']['blacklist'] ) ) ) { if ( empty( trim( $_POST['statifyblacklist']['target']['blacklist'] ) ) ) {
$target = array(); $target = array();
} else { } else {
$target = explode( "\r\n", str_replace( '\\\\', '\\', $_POST['statifyblacklist']['target']['blacklist'] ) ); $target = explode( "\r\n", str_replace( '\\\\', '\\', $_POST['statifyblacklist']['target']['blacklist'] ) );
} }
/* Extract IP array */ // Extract IP array.
if ( empty( trim( $_POST['statifyblacklist']['ip']['blacklist'] ) ) ) { if ( empty( trim( $_POST['statifyblacklist']['ip']['blacklist'] ) ) ) {
$ip = array(); $ip = array();
} else { } else {
$ip = explode( "\r\n", $_POST['statifyblacklist']['ip']['blacklist'] ); $ip = explode( "\r\n", $_POST['statifyblacklist']['ip']['blacklist'] );
} }
/* Update options (data will be sanitized) */ // Update options (data will be sanitized).
$statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options( $statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options(
array( array(
'referer' => array( 'referer' => array(
@ -61,7 +70,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
) )
); );
/* Generate messages */ // Generate messages.
if ( $statifyBlacklistUpdateResult !== false ) { if ( $statifyBlacklistUpdateResult !== false ) {
if ( array_key_exists( 'referer', $statifyBlacklistUpdateResult ) ) { if ( array_key_exists( 'referer', $statifyBlacklistUpdateResult ) ) {
$statifyBlacklistPostWarning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' ); $statifyBlacklistPostWarning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
@ -76,7 +85,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
?> ?>
<div class="wrap"> <div class="wrap">
<h1><?php _e( 'Statify Blacklist', 'statify-blacklist' ) ?></h1> <h1><?php _e( 'Statify Blacklist', 'statify-blacklist' ) ?></h1>
<?php <?php
if ( is_plugin_inactive( 'statify/statify.php' ) ) { if ( is_plugin_inactive( 'statify/statify.php' ) ) {
print '<div class="notice notice-warning"><p>'; print '<div class="notice notice-warning"><p>';
@ -85,191 +94,191 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
} }
if ( isset( $statifyBlacklistPostWarning ) ) { if ( isset( $statifyBlacklistPostWarning ) ) {
print '<div class="notice notice-warning"><p>' . print '<div class="notice notice-warning"><p>' .
esc_html( $statifyBlacklistPostWarning ); esc_html( $statifyBlacklistPostWarning );
print '<br/>'; print '<br/>';
esc_html_e( 'Settings have not been saved yet.', 'statify-blacklist' ); esc_html_e( 'Settings have not been saved yet.', 'statify-blacklist' );
print '</p></div>'; print '</p></div>';
} }
if ( isset( $statifyBlacklistPostSuccess ) ) { if ( isset( $statifyBlacklistPostSuccess ) ) {
print '<div class="notice notice-success"><p>' . print '<div class="notice notice-success"><p>' .
esc_html( $statifyBlacklistPostSuccess ) . esc_html( $statifyBlacklistPostSuccess ) .
'</p></div>'; '</p></div>';
} }
?> ?>
<form action="" method="post" id="statify-blacklist-settings"> <form action="" method="post" id="statify-blacklist-settings">
<fieldset> <fieldset>
<h2><?php esc_html_e( 'Referer blacklist', 'statify-blacklist' ); ?></h2> <h2><?php esc_html_e( 'Referer blacklist', 'statify-blacklist' ); ?></h2>
<ul style="list-style: none;"> <ul style="list-style: none;">
<li> <li>
<label for="statify-blacklist_active_referer"> <label for="statify-blacklist_active_referer">
<input type="checkbox" name="statifyblacklist[referer][active]" <input type="checkbox" name="statifyblacklist[referer][active]"
id="statifyblacklist_active_referer" id="statifyblacklist_active_referer"
value="1" <?php checked( StatifyBlacklist::$_options['referer']['active'], 1 ); ?> /> value="1" <?php checked( StatifyBlacklist::$_options['referer']['active'], 1 ); ?> />
<?php esc_html_e( 'Activate live fiter', 'statify-blacklist' ); ?> <?php esc_html_e( 'Activate live fiter', 'statify-blacklist' ); ?>
</label> </label>
</li> </li>
<li> <li>
<label for="statify-blacklist_cron_referer"> <label for="statify-blacklist_cron_referer">
<input type="checkbox" name="statifyblacklist[referer][cron]" id="statifyblacklist_cron_referer" <input type="checkbox" name="statifyblacklist[referer][cron]" id="statifyblacklist_cron_referer"
value="1" <?php checked( StatifyBlacklist::$_options['referer']['cron'], 1 ); ?> /> value="1" <?php checked( StatifyBlacklist::$_options['referer']['cron'], 1 ); ?> />
<?php esc_html_e( 'CronJob execution', 'statify-blacklist' ); ?> <?php esc_html_e( 'CronJob execution', 'statify-blacklist' ); ?>
<small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?> <small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?>
) )
</small> </small>
</label> </label>
</li> </li>
<li> <li>
<label for="statify-blacklist_referer_regexp"> <label for="statify-blacklist_referer_regexp">
<?php esc_html_e( 'Use regular expressions', 'statify-blacklist' ); ?>: <?php esc_html_e( 'Use regular expressions', 'statify-blacklist' ); ?>:
<br/> <br />
<select name="statifyblacklist[referer][regexp]" id="statifyblacklist_referer_regexp"> <select name="statifyblacklist[referer][regexp]" id="statifyblacklist_referer_regexp">
<option value="0" <?php selected( StatifyBlacklist::$_options['referer']['regexp'], 0 ); ?>> <option value="0" <?php selected( StatifyBlacklist::$_options['referer']['regexp'], 0 ); ?>>
<?php esc_html_e( 'Disabled', 'statify-blacklist' ); ?> <?php esc_html_e( 'Disabled', 'statify-blacklist' ); ?>
</option> </option>
<option value="1" <?php selected( StatifyBlacklist::$_options['referer']['regexp'], 1 ); ?>> <option value="1" <?php selected( StatifyBlacklist::$_options['referer']['regexp'], 1 ); ?>>
<?php esc_html_e( 'Case-sensitive', 'statify-blacklist' ); ?> <?php esc_html_e( 'Case-sensitive', 'statify-blacklist' ); ?>
</option> </option>
<option value="2" <?php selected( StatifyBlacklist::$_options['referer']['regexp'], 2 ); ?>> <option value="2" <?php selected( StatifyBlacklist::$_options['referer']['regexp'], 2 ); ?>>
<?php esc_html_e( 'Case-insensitive', 'statify-blacklist' ); ?> <?php esc_html_e( 'Case-insensitive', 'statify-blacklist' ); ?>
</option> </option>
</select> </select>
<small> <small>
(<?php esc_html_e( 'Performance slower than standard filter. Recommended for cron or manual execition only.', 'statify-blacklist' ); ?> (<?php esc_html_e( 'Performance slower than standard filter. Recommended for cron or manual execition only.', 'statify-blacklist' ); ?>
) )
</small> </small>
</label> </label>
</li> </li>
<li> <li>
<label for="statify-blacklist_referer"> <label for="statify-blacklist_referer">
<?php esc_html_e( 'Referer blacklist', 'statify-blacklist' ); ?>:<br/> <?php esc_html_e( 'Referer blacklist', 'statify-blacklist' ); ?>:<br />
<textarea cols="40" rows="5" name="statifyblacklist[referer][blacklist]" id="statify-blacklist_referer"><?php <textarea cols="40" rows="5" name="statifyblacklist[referer][blacklist]" id="statify-blacklist_referer"><?php
if ( isset( $statifyBlacklistUpdateResult['referer'] ) ) { if ( isset( $statifyBlacklistUpdateResult['referer'] ) ) {
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['referer'] ) ) ); print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['referer'] ) ) );
} else { } else {
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['referer']['blacklist'] ) ) ); print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['referer']['blacklist'] ) ) );
} }
?></textarea> ?></textarea>
<br/> <br />
<small> <small>
(<?php esc_html_e( 'Add one domain (without subdomains) each line, e.g. example.com', 'statify-blacklist' ); ?> (<?php esc_html_e( 'Add one domain (without subdomains) each line, e.g. example.com', 'statify-blacklist' ); ?>
) )
</small> </small>
</label> </label>
</li> </li>
</ul> </ul>
</fieldset> </fieldset>
<fieldset> <fieldset>
<h2><?php esc_html_e( 'Target blacklist', 'statify-blacklist' ); ?></h2> <h2><?php esc_html_e( 'Target blacklist', 'statify-blacklist' ); ?></h2>
<ul style="list-style: none;"> <ul style="list-style: none;">
<li> <li>
<label for="statify-blacklist_active_target"> <label for="statify-blacklist_active_target">
<input type="checkbox" name="statifyblacklist[target][active]" <input type="checkbox" name="statifyblacklist[target][active]"
id="statifyblacklist_active_target" id="statifyblacklist_active_target"
value="1" <?php checked( StatifyBlacklist::$_options['target']['active'], 1 ); ?> /> value="1" <?php checked( StatifyBlacklist::$_options['target']['active'], 1 ); ?> />
<?php esc_html_e( 'Activate live fiter', 'statify-blacklist' ); ?> <?php esc_html_e( 'Activate live fiter', 'statify-blacklist' ); ?>
</label> </label>
</li> </li>
<li> <li>
<label for="statify-blacklist_cron_target"> <label for="statify-blacklist_cron_target">
<input type="checkbox" name="statifyblacklist[target][cron]" id="statifyblacklist_cron_target" <input type="checkbox" name="statifyblacklist[target][cron]" id="statifyblacklist_cron_target"
value="1" <?php checked( StatifyBlacklist::$_options['target']['cron'], 1 ); ?> /> value="1" <?php checked( StatifyBlacklist::$_options['target']['cron'], 1 ); ?> />
<?php esc_html_e( 'CronJob execution', 'statify-blacklist' ); ?> <?php esc_html_e( 'CronJob execution', 'statify-blacklist' ); ?>
<small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?> <small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?>
) )
</small> </small>
</label> </label>
</li> </li>
<li> <li>
<label for="statify-blacklist_target_regexp"> <label for="statify-blacklist_target_regexp">
<?php esc_html_e( 'Use regular expressions', 'statify-blacklist' ); ?>: <?php esc_html_e( 'Use regular expressions', 'statify-blacklist' ); ?>:
<br/> <br />
<select name="statifyblacklist[target][regexp]" id="statifyblacklist_target_regexp"> <select name="statifyblacklist[target][regexp]" id="statifyblacklist_target_regexp">
<option value="0" <?php selected( StatifyBlacklist::$_options['target']['regexp'], 0 ); ?>> <option value="0" <?php selected( StatifyBlacklist::$_options['target']['regexp'], 0 ); ?>>
<?php esc_html_e( 'Disabled', 'statify-blacklist' ); ?> <?php esc_html_e( 'Disabled', 'statify-blacklist' ); ?>
</option> </option>
<option value="1" <?php selected( StatifyBlacklist::$_options['target']['regexp'], 1 ); ?>> <option value="1" <?php selected( StatifyBlacklist::$_options['target']['regexp'], 1 ); ?>>
<?php esc_html_e( 'Case-sensitive', 'statify-blacklist' ); ?> <?php esc_html_e( 'Case-sensitive', 'statify-blacklist' ); ?>
</option> </option>
<option value="2" <?php selected( StatifyBlacklist::$_options['target']['regexp'], 2 ); ?>> <option value="2" <?php selected( StatifyBlacklist::$_options['target']['regexp'], 2 ); ?>>
<?php esc_html_e( 'Case-insensitive', 'statify-blacklist' ); ?> <?php esc_html_e( 'Case-insensitive', 'statify-blacklist' ); ?>
</option> </option>
</select> </select>
<small> <small>
(<?php esc_html_e( 'Performance slower than standard filter. Recommended for cron or manual execition only.', 'statify-blacklist' ); ?> (<?php esc_html_e( 'Performance slower than standard filter. Recommended for cron or manual execition only.', 'statify-blacklist' ); ?>
) )
</small> </small>
</label> </label>
</li> </li>
<li> <li>
<label for="statify-blacklist_target"> <label for="statify-blacklist_target">
<?php esc_html_e( 'Target blacklist', 'statify-blacklist' ); ?>:<br/> <?php esc_html_e( 'Target blacklist', 'statify-blacklist' ); ?>:<br />
<textarea cols="40" rows="5" name="statifyblacklist[target][blacklist]" id="statify-blacklist_target"><?php <textarea cols="40" rows="5" name="statifyblacklist[target][blacklist]" id="statify-blacklist_target"><?php
if ( isset( $statifyBlacklistUpdateResult['target'] ) ) { if ( isset( $statifyBlacklistUpdateResult['target'] ) ) {
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['target'] ) ) ); print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['target'] ) ) );
} else { } else {
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['target']['blacklist'] ) ) ); print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['target']['blacklist'] ) ) );
} }
?></textarea> ?></textarea>
<br/> <br />
<small> <small>
(<?php esc_html_e( 'Add one target URL each line, e.g.', 'statify-blacklist' ); (<?php esc_html_e( 'Add one target URL each line, e.g.', 'statify-blacklist' );
print ' /, /test/page/, /?page_id=123' ?> print ' /, /test/page/, /?page_id=123' ?>
) )
</small> </small>
</label> </label>
</li> </li>
</ul> </ul>
</fieldset> </fieldset>
<fieldset> <fieldset>
<h2><?php esc_html_e( 'IP blacklist', 'statify-blacklist' ); ?></h2> <h2><?php esc_html_e( 'IP blacklist', 'statify-blacklist' ); ?></h2>
<ul style="list-style: none;"> <ul style="list-style: none;">
<li> <li>
<label for="statify-blacklist_active_ip"> <label for="statify-blacklist_active_ip">
<input type="checkbox" name="statifyblacklist[ip][active]" id="statifyblacklist_active_ip" <input type="checkbox" name="statifyblacklist[ip][active]" id="statifyblacklist_active_ip"
value="1" <?php checked( StatifyBlacklist::$_options['ip']['active'], 1 ); ?> /> value="1" <?php checked( StatifyBlacklist::$_options['ip']['active'], 1 ); ?> />
<?php esc_html_e( 'Activate live fiter', 'statify-blacklist' ); ?> <?php esc_html_e( 'Activate live fiter', 'statify-blacklist' ); ?>
</label> </label>
</li> </li>
<li> <li>
<small> <small>
(<?php esc_html_e( 'Cron execution is not possible for IP filter, because IP addresses are not stored.', 'statify-blacklist' ); ?> (<?php esc_html_e( 'Cron execution is not possible for IP filter, because IP addresses are not stored.', 'statify-blacklist' ); ?>
) )
</small> </small>
</li> </li>
<li> <li>
<label for="statify-blacklist_ip"> <label for="statify-blacklist_ip">
<?php esc_html_e( 'IP blacklist', 'statify-blacklist' ); ?>:<br/> <?php esc_html_e( 'IP blacklist', 'statify-blacklist' ); ?>:<br />
<textarea cols="40" rows="5" name="statifyblacklist[ip][blacklist]" id="statify-blacklist_ip"><?php <textarea cols="40" rows="5" name="statifyblacklist[ip][blacklist]" id="statify-blacklist_ip"><?php
if ( isset( $statifyBlacklistUpdateResult['ip'] ) ) { if ( isset( $statifyBlacklistUpdateResult['ip'] ) ) {
print esc_html( $_POST['statifyblacklist']['ip']['blacklist'] ); print esc_html( $_POST['statifyblacklist']['ip']['blacklist'] );
} else { } else {
print esc_html( implode( "\r\n", StatifyBlacklist::$_options['ip']['blacklist'] ) ); print esc_html( implode( "\r\n", StatifyBlacklist::$_options['ip']['blacklist'] ) );
} }
?></textarea> ?></textarea>
<br/> <br />
<small> <small>
(<?php esc_html_e( 'Add one IP address or range per line, e.g.', 'statify-blacklist' ) ?> (<?php esc_html_e( 'Add one IP address or range per line, e.g.', 'statify-blacklist' ) ?>
127.0.0.1, 127.0.0.1, 192.168.123.0/24, 2001:db8:a0b:12f0::1/64
192.168.123.0/24, 2001:db8:a0b:12f0::1/64 )
) </small>
</small> </label>
</label> </li>
</li> </ul>
</ul> </fieldset>
</fieldset>
<?php wp_nonce_field( 'statify-blacklist-settings' ); ?> <?php wp_nonce_field( 'statify-blacklist-settings' ); ?>
<p class="submit"> <p class="submit">
<input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>"> <input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>">
<hr/> <hr />
<input class="button-secondary" type="submit" name="cleanUp" <input class="button-secondary" type="submit" name="cleanUp"
value="<?php esc_html_e( 'CleanUp Database', 'statify-blacklist' ) ?>" value="<?php esc_html_e( 'CleanUp Database', 'statify-blacklist' ) ?>"
onclick="return confirm('Do you really want to apply filters to database? This cannot be undone.');"> onclick="return confirm('Do you really want to apply filters to database? This cannot be undone.');">
<br/> <br />
<small><?php esc_html_e( 'Applies referer and target filter (even if disabled) to data stored in database.', 'statify-blacklist' ); ?> <b><?php esc_html_e( 'This cannot be undone!', 'statify-blacklist' ); ?></b></small> <small><?php esc_html_e( 'Applies referer and target filter (even if disabled) to data stored in database.', 'statify-blacklist' ); ?>
</p> <em><?php esc_html_e( 'This cannot be undone!', 'statify-blacklist' ); ?></em></small>
</form> </p>
</form>
</div> </div>