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:
parent
2e7883bc62
commit
089ed347fa
@ -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().
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
|
||||
<!-- The plugin uses switch_to_blog for multisite handling. -->
|
||||
<exclude name="WordPress.VIP.RestrictedFunctions.switch_to_blog"/>
|
||||
<exclude name="WordPress.VIP.RestrictedFunctions.switch_to_blog_switch_to_blog"/>
|
||||
|
||||
<!-- Direct queries used to clean up statify table. -->
|
||||
<exclude name="WordPress.VIP.DirectDatabaseQuery.DirectQuery"/>
|
||||
|
@ -33,7 +33,7 @@
|
||||
*/
|
||||
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
// Constants.
|
||||
define( 'STATIFYBLACKLIST_FILE', __FILE__ );
|
||||
@ -67,10 +67,10 @@ function statify_blacklist_autoload( $class ) {
|
||||
'StatifyBlacklist_System',
|
||||
);
|
||||
|
||||
if ( in_array( $class, $plugin_classes ) ) {
|
||||
if ( in_array( $class, $plugin_classes, true ) ) {
|
||||
require_once(
|
||||
sprintf(
|
||||
'%s/inc/%s.class.php',
|
||||
'%s/inc/class-%s.php',
|
||||
STATIFYBLACKLIST_DIR,
|
||||
strtolower( str_replace( '_', '-', $class ) )
|
||||
)
|
||||
|
@ -20,17 +20,17 @@ const ABSPATH = false;
|
||||
/**
|
||||
* The StatifyBlacklist base class.
|
||||
*/
|
||||
require_once( 'inc/statifyblacklist.class.php' );
|
||||
require_once( 'inc/class-statifyblacklist.php' );
|
||||
|
||||
/**
|
||||
* The StatifyBlacklist system class.
|
||||
*/
|
||||
require_once( 'inc/statifyblacklist-system.class.php' );
|
||||
require_once( 'inc/class-statifyblacklist-system.php' );
|
||||
|
||||
/**
|
||||
* The StatifyBlacklist admin class.
|
||||
*/
|
||||
require_once( 'inc/statifyblacklist-admin.class.php' );
|
||||
require_once( 'inc/class-statifyblacklist-admin.php' );
|
||||
|
||||
/**
|
||||
* Class StatifyBlacklistTest.
|
||||
|
@ -10,7 +10,7 @@
|
||||
*/
|
||||
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
// Update plugin options.
|
||||
if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
@ -48,7 +48,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
}
|
||||
|
||||
// Update options (data will be sanitized).
|
||||
$statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options(
|
||||
$statifyblacklist_update_result = StatifyBlacklist_Admin::update_options(
|
||||
array(
|
||||
'referer' => array(
|
||||
'active' => (int) $_POST['statifyblacklist']['referer']['active'],
|
||||
@ -71,17 +71,18 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
);
|
||||
|
||||
// Generate messages.
|
||||
if ( false !== $statifyBlacklistUpdateResult ) {
|
||||
if ( array_key_exists( 'referer', $statifyBlacklistUpdateResult ) ) {
|
||||
$statifyBlacklistPostWarning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
|
||||
} elseif ( array_key_exists( 'ip', $statifyBlacklistUpdateResult ) ) {
|
||||
$statifyBlacklistPostWarning = sprintf( __( 'Some IPs are invalid : %s', 'statify-blacklist' ), implode( ', ', $statifyBlacklistUpdateResult['ip'] ) );
|
||||
if ( false !== $statifyblacklist_update_result ) {
|
||||
if ( array_key_exists( 'referer', $statifyblacklist_update_result ) ) {
|
||||
$statifyblacklist_post_warning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
|
||||
} elseif ( array_key_exists( 'ip', $statifyblacklist_update_result ) ) {
|
||||
// translators: List of invalid IP addresses (comma separated).
|
||||
$statifyblacklist_post_warning = sprintf( __( 'Some IPs are invalid : %s', 'statify-blacklist' ), implode( ', ', $statifyblacklist_update_result['ip'] ) );
|
||||
}
|
||||
} else {
|
||||
$statifyBlacklistPostSuccess = __( 'Settings updated successfully.', 'statify-blacklist' );
|
||||
}
|
||||
}
|
||||
$statifyblacklist_post_success = __( 'Settings updated successfully.', 'statify-blacklist' );
|
||||
}
|
||||
} // End if().
|
||||
} // End if().
|
||||
?>
|
||||
|
||||
<div class="wrap">
|
||||
@ -92,16 +93,16 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
esc_html( 'Statify plugin is not active.' );
|
||||
print '</p></div>';
|
||||
}
|
||||
if ( isset( $statifyBlacklistPostWarning ) ) {
|
||||
if ( isset( $statifyblacklist_post_warning ) ) {
|
||||
print '<div class="notice notice-warning"><p>' .
|
||||
esc_html( $statifyBlacklistPostWarning );
|
||||
esc_html( $statifyblacklist_post_warning );
|
||||
print '<br/>';
|
||||
esc_html_e( 'Settings have not been saved yet.', 'statify-blacklist' );
|
||||
print '</p></div>';
|
||||
}
|
||||
if ( isset( $statifyBlacklistPostSuccess ) ) {
|
||||
if ( isset( $statifyblacklist_post_success ) ) {
|
||||
print '<div class="notice notice-success"><p>' .
|
||||
esc_html( $statifyBlacklistPostSuccess ) .
|
||||
esc_html( $statifyblacklist_post_success ) .
|
||||
'</p></div>';
|
||||
}
|
||||
?>
|
||||
@ -152,8 +153,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
<label for="statify-blacklist_referer">
|
||||
<?php esc_html_e( 'Referer blacklist', 'statify-blacklist' ); ?>:<br />
|
||||
<textarea cols="40" rows="5" name="statifyblacklist[referer][blacklist]" id="statify-blacklist_referer"><?php
|
||||
if ( isset( $statifyBlacklistUpdateResult['referer'] ) ) {
|
||||
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['referer'] ) ) );
|
||||
if ( isset( $statifyblacklist_update_result['referer'] ) ) {
|
||||
print esc_html( implode( "\r\n", array_keys( $statifyblacklist_update_result['referer'] ) ) );
|
||||
} else {
|
||||
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['referer']['blacklist'] ) ) );
|
||||
}
|
||||
@ -214,8 +215,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
<label for="statify-blacklist_target">
|
||||
<?php esc_html_e( 'Target blacklist', 'statify-blacklist' ); ?>:<br />
|
||||
<textarea cols="40" rows="5" name="statifyblacklist[target][blacklist]" id="statify-blacklist_target"><?php
|
||||
if ( isset( $statifyBlacklistUpdateResult['target'] ) ) {
|
||||
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['target'] ) ) );
|
||||
if ( isset( $statifyblacklist_update_result['target'] ) ) {
|
||||
print esc_html( implode( "\r\n", array_keys( $statifyblacklist_update_result['target'] ) ) );
|
||||
} else {
|
||||
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['target']['blacklist'] ) ) );
|
||||
}
|
||||
@ -251,7 +252,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
||||
<label for="statify-blacklist_ip">
|
||||
<?php esc_html_e( 'IP blacklist', 'statify-blacklist' ); ?>:<br />
|
||||
<textarea cols="40" rows="5" name="statifyblacklist[ip][blacklist]" id="statify-blacklist_ip"><?php
|
||||
if ( isset( $statifyBlacklistUpdateResult['ip'] ) ) {
|
||||
if ( isset( $statifyblacklist_update_result['ip'] ) ) {
|
||||
print esc_html( $_POST['statifyblacklist']['ip']['blacklist'] );
|
||||
} else {
|
||||
print esc_html( implode( "\r\n", StatifyBlacklist::$_options['ip']['blacklist'] ) );
|
||||
|
Loading…
x
Reference in New Issue
Block a user