Code style again
Altered variable names from camel to snake case, renamed class files from *.class.php to class-*.php, added some comments.
This commit is contained in:
@ -10,7 +10,7 @@
|
||||
*/
|
||||
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist admin configuration.
|
||||
@ -29,22 +29,26 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
public static function update_options( $options = null ) {
|
||||
if ( isset( $options ) && current_user_can( 'manage_options' ) ) {
|
||||
// Sanitize URLs and remove empty inputs.
|
||||
$givenReferer = $options['referer']['blacklist'];
|
||||
$given_referer = $options['referer']['blacklist'];
|
||||
if ( 0 === $options['referer']['regexp'] ) {
|
||||
$sanitizedReferer = self::sanitizeURLs( $givenReferer );
|
||||
$sanitized_referer = self::sanitizeURLs( $given_referer );
|
||||
} else {
|
||||
$sanitizedReferer = $givenReferer;
|
||||
$sanitized_referer = $given_referer;
|
||||
}
|
||||
|
||||
// Sanitize IPs and Subnets and remove empty inputs.
|
||||
$givenIP = $options['ip']['blacklist'];
|
||||
$sanitizedIP = self::sanitizeIPs( $givenIP );
|
||||
$given_ip = $options['ip']['blacklist'];
|
||||
$sanitized_ip = self::sanitizeIPs( $given_ip );
|
||||
|
||||
// Abort on errors.
|
||||
if ( ! empty( array_diff( array_keys( $givenReferer ), array_keys( $sanitizedReferer ) ) ) ) {
|
||||
return array( 'referer' => $sanitizedReferer );
|
||||
} elseif ( ! empty( array_diff( $givenIP, $sanitizedIP ) ) ) {
|
||||
return array( 'ip' => array_diff( $givenIP, $sanitizedIP ) );
|
||||
if ( ! empty( array_diff( array_keys( $given_referer ), array_keys( $sanitized_referer ) ) ) ) {
|
||||
return array(
|
||||
'referer' => $sanitized_referer,
|
||||
);
|
||||
} elseif ( ! empty( array_diff( $given_ip, $sanitized_ip ) ) ) {
|
||||
return array(
|
||||
'ip' => array_diff( $given_ip, $sanitized_ip ),
|
||||
);
|
||||
}
|
||||
|
||||
// Update database on success.
|
||||
@ -150,58 +154,60 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
}
|
||||
|
||||
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
|
||||
$cleanRef = ( 1 === self::$_options['referer']['cron'] );
|
||||
$cleanTrg = ( 1 === self::$_options['target']['cron'] );
|
||||
$clean_ref = ( 1 === self::$_options['referer']['cron'] );
|
||||
$clean_trg = ( 1 === self::$_options['target']['cron'] );
|
||||
} else {
|
||||
$cleanRef = true;
|
||||
$cleanTrg = true;
|
||||
$clean_ref = true;
|
||||
$clean_trg = true;
|
||||
}
|
||||
|
||||
if ( $cleanRef ) {
|
||||
if ( $clean_ref ) {
|
||||
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) {
|
||||
// Merge given regular expressions into one.
|
||||
$refererRegexp = implode( '|', array_keys( self::$_options['referer']['blacklist'] ) );
|
||||
$referer_regexp = implode( '|', array_keys( self::$_options['referer']['blacklist'] ) );
|
||||
} else {
|
||||
// Sanitize URLs.
|
||||
$referer = self::sanitizeURLs( self::$_options['referer']['blacklist'] );
|
||||
|
||||
// Build filter regexp.
|
||||
$refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
|
||||
$referer_regexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $cleanTrg ) {
|
||||
if ( $clean_trg ) {
|
||||
if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) {
|
||||
// Merge given regular expressions into one.
|
||||
$targetRegexp = implode( '|', array_keys( self::$_options['target']['blacklist'] ) );
|
||||
$target_regexp = implode( '|', array_keys( self::$_options['target']['blacklist'] ) );
|
||||
} else {
|
||||
// Build filter regexp.
|
||||
$targetRegexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) );
|
||||
$target_regexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $refererRegexp ) || ! empty( $targetRegexp ) ) {
|
||||
if ( ! empty( $referer_regexp ) || ! empty( $target_regexp ) ) {
|
||||
global $wpdb;
|
||||
|
||||
// Execute filter on database.
|
||||
if ( ! empty( $refererRegexp ) ) {
|
||||
// @codingStandardsIgnoreStart These statements prouce warnings, rework in future release (TODO).
|
||||
if ( ! empty( $referer_regexp ) ) {
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM `$wpdb->statify` WHERE "
|
||||
. ( ( 1 === self::$_options['referer']['regexp'] ) ? ' BINARY ' : '' )
|
||||
. 'referrer REGEXP %s', $refererRegexp
|
||||
. 'referrer REGEXP %s', $referer_regexp
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( ! empty( $targetRegexp ) ) {
|
||||
if ( ! empty( $target_regexp ) ) {
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM `$wpdb->statify` WHERE "
|
||||
. ( ( 1 === self::$_options['target']['regexp'] ) ? ' BINARY ' : '' )
|
||||
. 'target REGEXP %s', $targetRegexp
|
||||
. 'target REGEXP %s', $target_regexp
|
||||
)
|
||||
);
|
||||
}
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
// Optimize DB.
|
||||
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
|
@ -10,7 +10,7 @@
|
||||
*/
|
||||
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist system configuration.
|
||||
@ -32,7 +32,8 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
if ( function_exists( 'get_sites' ) ) {
|
||||
$sites = get_sites();
|
||||
} elseif ( function_exists( 'wp_get_sites' ) ) {
|
||||
$sites = wp_get_sites(); // Legacy support for WP < 4.6.
|
||||
// @codingStandardsIgnoreLine Legacy support for WP < 4.6.
|
||||
$sites = wp_get_sites();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@ -67,7 +68,8 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
if ( function_exists( 'get_sites' ) ) {
|
||||
$sites = get_sites();
|
||||
} elseif ( function_exists( 'wp_get_sites' ) ) {
|
||||
$sites = wp_get_sites(); // Legacy support for WP < 4.6.
|
||||
// @codingStandardsIgnoreLine Legacy support for WP < 4.6.
|
||||
$sites = wp_get_sites();
|
||||
} else {
|
||||
return;
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
*/
|
||||
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist.
|
||||
@ -58,7 +58,7 @@ class StatifyBlacklist {
|
||||
*/
|
||||
public function __construct() {
|
||||
// Skip on autosave or AJAX.
|
||||
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) or ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
|
||||
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -223,7 +223,8 @@ class StatifyBlacklist {
|
||||
|
||||
// IP blacklist (since 1.4.0).
|
||||
if ( isset( self::$_options['ip']['active'] ) && 0 !== self::$_options['ip']['active'] ) {
|
||||
if ( false !== ( $ip = self::get_ip() ) ) {
|
||||
$ip = self::get_ip();
|
||||
if ( false !== ( $ip ) ) {
|
||||
foreach ( self::$_options['ip']['blacklist'] as $net ) {
|
||||
if ( self::cidr_match( $ip, $net ) ) {
|
||||
return true;
|
||||
@ -247,14 +248,15 @@ class StatifyBlacklist {
|
||||
*/
|
||||
private static function get_ip() {
|
||||
foreach (
|
||||
|
||||
/*
|
||||
* There are more fields, that could possibly be checked, but we only consider the most common for now:
|
||||
* HTTP_CLIENT_IP, HTTP_X_REAL_IP, HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED,
|
||||
* HTTP_X_CLUSTER_CLIENT_IP, HTTP_FORWARDED_FOR, HTTP_FORWARDED, REMOTE_ADDR
|
||||
*/
|
||||
array(
|
||||
// 'HTTP_CLIENT_IP',
|
||||
'HTTP_X_REAL_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
// 'HTTP_X_FORWARDED',
|
||||
// 'HTTP_X_CLUSTER_CLIENT_IP',
|
||||
// 'HTTP_FORWARDED_FOR',
|
||||
// 'HTTP_FORWARDED',
|
||||
'REMOTE_ADDR',
|
||||
) as $k
|
||||
) {
|
||||
@ -303,18 +305,19 @@ class StatifyBlacklist {
|
||||
$mask = 128;
|
||||
}
|
||||
|
||||
$bytesAddr = unpack( 'n*', inet_pton( $base ) );
|
||||
$bytesTest = unpack( 'n*', inet_pton( $ip ) );
|
||||
$bytes_addr = unpack( 'n*', inet_pton( $base ) );
|
||||
$bytes_est = unpack( 'n*', inet_pton( $ip ) );
|
||||
|
||||
if ( ! $bytesAddr || ! $bytesTest ) {
|
||||
if ( ! $bytes_addr || ! $bytes_est ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ( $i = 1, $ceil = ceil( $mask / 16 ); $i <= $ceil; ++ $i ) {
|
||||
$ceil = ceil( $mask / 16 );
|
||||
for ( $i = 1; $i <= $ceil; ++ $i ) {
|
||||
$left = $mask - 16 * ( $i - 1 );
|
||||
$left = ( $left <= 16 ) ? $left : 16;
|
||||
$maskB = ~( 0xffff >> $left ) & 0xffff;
|
||||
if ( ( $bytesAddr[ $i ] & $maskB ) !== ( $bytesTest[ $i ] & $maskB ) ) {
|
||||
$mask_b = ~( 0xffff >> $left ) & 0xffff;
|
||||
if ( ( $bytes_addr[ $i ] & $mask_b ) !== ( $bytes_est[ $i ] & $mask_b ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -341,6 +344,6 @@ class StatifyBlacklist {
|
||||
}
|
||||
|
||||
return ( 0 === substr_compare( sprintf( '%032b', ip2long( $ip ) ), sprintf( '%032b', ip2long( $base ) ), 0, $mask ) );
|
||||
}
|
||||
} // End if().
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user