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:
Stefan Kalscheuer 2017-07-16 12:34:49 +02:00
parent 2e7883bc62
commit 089ed347fa
7 changed files with 81 additions and 68 deletions

View File

@ -10,7 +10,7 @@
*/ */
// Quit. // Quit.
defined( 'ABSPATH' ) or exit; defined( 'ABSPATH' ) || exit;
/** /**
* Statify Blacklist admin configuration. * Statify Blacklist admin configuration.
@ -29,22 +29,26 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
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']; $given_referer = $options['referer']['blacklist'];
if ( 0 === $options['referer']['regexp'] ) { if ( 0 === $options['referer']['regexp'] ) {
$sanitizedReferer = self::sanitizeURLs( $givenReferer ); $sanitized_referer = self::sanitizeURLs( $given_referer );
} else { } else {
$sanitizedReferer = $givenReferer; $sanitized_referer = $given_referer;
} }
// Sanitize IPs and Subnets and remove empty inputs. // Sanitize IPs and Subnets and remove empty inputs.
$givenIP = $options['ip']['blacklist']; $given_ip = $options['ip']['blacklist'];
$sanitizedIP = self::sanitizeIPs( $givenIP ); $sanitized_ip = self::sanitizeIPs( $given_ip );
// Abort on errors. // Abort on errors.
if ( ! empty( array_diff( array_keys( $givenReferer ), array_keys( $sanitizedReferer ) ) ) ) { if ( ! empty( array_diff( array_keys( $given_referer ), array_keys( $sanitized_referer ) ) ) ) {
return array( 'referer' => $sanitizedReferer ); return array(
} elseif ( ! empty( array_diff( $givenIP, $sanitizedIP ) ) ) { 'referer' => $sanitized_referer,
return array( 'ip' => array_diff( $givenIP, $sanitizedIP ) ); );
} elseif ( ! empty( array_diff( $given_ip, $sanitized_ip ) ) ) {
return array(
'ip' => array_diff( $given_ip, $sanitized_ip ),
);
} }
// Update database on success. // Update database on success.
@ -150,58 +154,60 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
} }
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
$cleanRef = ( 1 === self::$_options['referer']['cron'] ); $clean_ref = ( 1 === self::$_options['referer']['cron'] );
$cleanTrg = ( 1 === self::$_options['target']['cron'] ); $clean_trg = ( 1 === self::$_options['target']['cron'] );
} else { } else {
$cleanRef = true; $clean_ref = true;
$cleanTrg = true; $clean_trg = true;
} }
if ( $cleanRef ) { if ( $clean_ref ) {
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'] ) ); $referer_regexp = 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 ) ) ); $referer_regexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
} }
} }
if ( $cleanTrg ) { if ( $clean_trg ) {
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'] ) ); $target_regexp = 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'] ) ) ); $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; global $wpdb;
// Execute filter on database. // Execute filter on database.
if ( ! empty( $refererRegexp ) ) { // @codingStandardsIgnoreStart These statements prouce warnings, rework in future release (TODO).
if ( ! empty( $referer_regexp ) ) {
$wpdb->query( $wpdb->query(
$wpdb->prepare( $wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE " "DELETE FROM `$wpdb->statify` WHERE "
. ( ( 1 === self::$_options['referer']['regexp'] ) ? ' BINARY ' : '' ) . ( ( 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->query(
$wpdb->prepare( $wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE " "DELETE FROM `$wpdb->statify` WHERE "
. ( ( 1 === self::$_options['target']['regexp'] ) ? ' BINARY ' : '' ) . ( ( 1 === self::$_options['target']['regexp'] ) ? ' BINARY ' : '' )
. 'target REGEXP %s', $targetRegexp . 'target REGEXP %s', $target_regexp
) )
); );
} }
// @codingStandardsIgnoreEnd
// Optimize DB. // Optimize DB.
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" ); $wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );

View File

@ -10,7 +10,7 @@
*/ */
// Quit. // Quit.
defined( 'ABSPATH' ) or exit; defined( 'ABSPATH' ) || exit;
/** /**
* Statify Blacklist system configuration. * Statify Blacklist system configuration.
@ -32,7 +32,8 @@ 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. // @codingStandardsIgnoreLine Legacy support for WP < 4.6.
$sites = wp_get_sites();
} else { } else {
return; return;
} }
@ -67,7 +68,8 @@ 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. // @codingStandardsIgnoreLine Legacy support for WP < 4.6.
$sites = wp_get_sites();
} else { } else {
return; return;
} }

View File

@ -9,7 +9,7 @@
*/ */
// Quit. // Quit.
defined( 'ABSPATH' ) or exit; defined( 'ABSPATH' ) || exit;
/** /**
* Statify Blacklist. * Statify Blacklist.
@ -58,7 +58,7 @@ class StatifyBlacklist {
*/ */
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 ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return; return;
} }
@ -223,7 +223,8 @@ class StatifyBlacklist {
// IP blacklist (since 1.4.0). // IP blacklist (since 1.4.0).
if ( isset( self::$_options['ip']['active'] ) && 0 !== self::$_options['ip']['active'] ) { 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 ) { foreach ( self::$_options['ip']['blacklist'] as $net ) {
if ( self::cidr_match( $ip, $net ) ) { if ( self::cidr_match( $ip, $net ) ) {
return true; return true;
@ -247,14 +248,15 @@ class StatifyBlacklist {
*/ */
private static function get_ip() { private static function get_ip() {
foreach ( 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( array(
// 'HTTP_CLIENT_IP',
'HTTP_X_REAL_IP', 'HTTP_X_REAL_IP',
'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR',
// 'HTTP_X_FORWARDED',
// 'HTTP_X_CLUSTER_CLIENT_IP',
// 'HTTP_FORWARDED_FOR',
// 'HTTP_FORWARDED',
'REMOTE_ADDR', 'REMOTE_ADDR',
) as $k ) as $k
) { ) {
@ -303,18 +305,19 @@ class StatifyBlacklist {
$mask = 128; $mask = 128;
} }
$bytesAddr = unpack( 'n*', inet_pton( $base ) ); $bytes_addr = unpack( 'n*', inet_pton( $base ) );
$bytesTest = unpack( 'n*', inet_pton( $ip ) ); $bytes_est = unpack( 'n*', inet_pton( $ip ) );
if ( ! $bytesAddr || ! $bytesTest ) { if ( ! $bytes_addr || ! $bytes_est ) {
return false; 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 = $mask - 16 * ( $i - 1 );
$left = ( $left <= 16 ) ? $left : 16; $left = ( $left <= 16 ) ? $left : 16;
$maskB = ~( 0xffff >> $left ) & 0xffff; $mask_b = ~( 0xffff >> $left ) & 0xffff;
if ( ( $bytesAddr[ $i ] & $maskB ) !== ( $bytesTest[ $i ] & $maskB ) ) { if ( ( $bytes_addr[ $i ] & $mask_b ) !== ( $bytes_est[ $i ] & $mask_b ) ) {
return false; return false;
} }
} }
@ -341,6 +344,6 @@ class StatifyBlacklist {
} }
return ( 0 === substr_compare( sprintf( '%032b', ip2long( $ip ) ), sprintf( '%032b', ip2long( $base ) ), 0, $mask ) ); return ( 0 === substr_compare( sprintf( '%032b', ip2long( $ip ) ), sprintf( '%032b', ip2long( $base ) ), 0, $mask ) );
} } // End if().
} }
} }

View File

@ -8,6 +8,7 @@
<!-- The plugin uses switch_to_blog for multisite handling. --> <!-- 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"/>
<exclude name="WordPress.VIP.RestrictedFunctions.switch_to_blog_switch_to_blog"/>
<!-- Direct queries used to clean up statify table. --> <!-- Direct queries used to clean up statify table. -->
<exclude name="WordPress.VIP.DirectDatabaseQuery.DirectQuery"/> <exclude name="WordPress.VIP.DirectDatabaseQuery.DirectQuery"/>

View File

@ -33,7 +33,7 @@
*/ */
// Quit. // Quit.
defined( 'ABSPATH' ) or exit; defined( 'ABSPATH' ) || exit;
// Constants. // Constants.
define( 'STATIFYBLACKLIST_FILE', __FILE__ ); define( 'STATIFYBLACKLIST_FILE', __FILE__ );
@ -67,10 +67,10 @@ function statify_blacklist_autoload( $class ) {
'StatifyBlacklist_System', 'StatifyBlacklist_System',
); );
if ( in_array( $class, $plugin_classes ) ) { if ( in_array( $class, $plugin_classes, true ) ) {
require_once( require_once(
sprintf( sprintf(
'%s/inc/%s.class.php', '%s/inc/class-%s.php',
STATIFYBLACKLIST_DIR, STATIFYBLACKLIST_DIR,
strtolower( str_replace( '_', '-', $class ) ) strtolower( str_replace( '_', '-', $class ) )
) )

View File

@ -20,17 +20,17 @@ const ABSPATH = false;
/** /**
* The StatifyBlacklist base class. * The StatifyBlacklist base class.
*/ */
require_once( 'inc/statifyblacklist.class.php' ); require_once( 'inc/class-statifyblacklist.php' );
/** /**
* The StatifyBlacklist system class. * The StatifyBlacklist system class.
*/ */
require_once( 'inc/statifyblacklist-system.class.php' ); require_once( 'inc/class-statifyblacklist-system.php' );
/** /**
* The StatifyBlacklist admin class. * The StatifyBlacklist admin class.
*/ */
require_once( 'inc/statifyblacklist-admin.class.php' ); require_once( 'inc/class-statifyblacklist-admin.php' );
/** /**
* Class StatifyBlacklistTest. * Class StatifyBlacklistTest.

View File

@ -10,7 +10,7 @@
*/ */
// Quit. // Quit.
defined( 'ABSPATH' ) or exit; defined( 'ABSPATH' ) || exit;
// Update plugin options. // Update plugin options.
if ( ! empty( $_POST['statifyblacklist'] ) ) { if ( ! empty( $_POST['statifyblacklist'] ) ) {
@ -48,7 +48,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
} }
// Update options (data will be sanitized). // Update options (data will be sanitized).
$statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options( $statifyblacklist_update_result = StatifyBlacklist_Admin::update_options(
array( array(
'referer' => array( 'referer' => array(
'active' => (int) $_POST['statifyblacklist']['referer']['active'], 'active' => (int) $_POST['statifyblacklist']['referer']['active'],
@ -71,17 +71,18 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
); );
// Generate messages. // Generate messages.
if ( false !== $statifyBlacklistUpdateResult ) { if ( false !== $statifyblacklist_update_result ) {
if ( array_key_exists( 'referer', $statifyBlacklistUpdateResult ) ) { if ( array_key_exists( 'referer', $statifyblacklist_update_result ) ) {
$statifyBlacklistPostWarning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' ); $statifyblacklist_post_warning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
} elseif ( array_key_exists( 'ip', $statifyBlacklistUpdateResult ) ) { } elseif ( array_key_exists( 'ip', $statifyblacklist_update_result ) ) {
$statifyBlacklistPostWarning = sprintf( __( 'Some IPs are invalid : %s', 'statify-blacklist' ), implode( ', ', $statifyBlacklistUpdateResult['ip'] ) ); // 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 { } else {
$statifyBlacklistPostSuccess = __( 'Settings updated successfully.', 'statify-blacklist' ); $statifyblacklist_post_success = __( 'Settings updated successfully.', 'statify-blacklist' );
} }
} } // End if().
} } // End if().
?> ?>
<div class="wrap"> <div class="wrap">
@ -92,16 +93,16 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
esc_html( 'Statify plugin is not active.' ); esc_html( 'Statify plugin is not active.' );
print '</p></div>'; print '</p></div>';
} }
if ( isset( $statifyBlacklistPostWarning ) ) { if ( isset( $statifyblacklist_post_warning ) ) {
print '<div class="notice notice-warning"><p>' . print '<div class="notice notice-warning"><p>' .
esc_html( $statifyBlacklistPostWarning ); esc_html( $statifyblacklist_post_warning );
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( $statifyblacklist_post_success ) ) {
print '<div class="notice notice-success"><p>' . print '<div class="notice notice-success"><p>' .
esc_html( $statifyBlacklistPostSuccess ) . esc_html( $statifyblacklist_post_success ) .
'</p></div>'; '</p></div>';
} }
?> ?>
@ -152,8 +153,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<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( $statifyblacklist_update_result['referer'] ) ) {
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['referer'] ) ) ); print esc_html( implode( "\r\n", array_keys( $statifyblacklist_update_result['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'] ) ) );
} }
@ -214,8 +215,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<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( $statifyblacklist_update_result['target'] ) ) {
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult['target'] ) ) ); print esc_html( implode( "\r\n", array_keys( $statifyblacklist_update_result['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'] ) ) );
} }
@ -251,7 +252,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<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( $statifyblacklist_update_result['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'] ) );