From 089ed347fa8670f5d5179eaf461c42cb8416b528 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 16 Jul 2017 12:34:49 +0200 Subject: [PATCH] Code style again Altered variable names from camel to snake case, renamed class files from *.class.php to class-*.php, added some comments. --- ...s.php => class-statifyblacklist-admin.php} | 56 ++++++++++--------- ....php => class-statifyblacklist-system.php} | 8 ++- ...t.class.php => class-statifyblacklist.php} | 33 ++++++----- phpcs.xml | 1 + statify-blacklist.php | 6 +- test/statifyblacklist-test.php | 6 +- views/settings-page.php | 39 ++++++------- 7 files changed, 81 insertions(+), 68 deletions(-) rename inc/{statifyblacklist-admin.class.php => class-statifyblacklist-admin.php} (79%) rename inc/{statifyblacklist-system.class.php => class-statifyblacklist-system.php} (94%) rename inc/{statifyblacklist.class.php => class-statifyblacklist.php} (91%) diff --git a/inc/statifyblacklist-admin.class.php b/inc/class-statifyblacklist-admin.php similarity index 79% rename from inc/statifyblacklist-admin.class.php rename to inc/class-statifyblacklist-admin.php index 2b819e4..f436f23 100644 --- a/inc/statifyblacklist-admin.class.php +++ b/inc/class-statifyblacklist-admin.php @@ -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`" ); diff --git a/inc/statifyblacklist-system.class.php b/inc/class-statifyblacklist-system.php similarity index 94% rename from inc/statifyblacklist-system.class.php rename to inc/class-statifyblacklist-system.php index 9ec589b..7dac2f0 100644 --- a/inc/statifyblacklist-system.class.php +++ b/inc/class-statifyblacklist-system.php @@ -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; } diff --git a/inc/statifyblacklist.class.php b/inc/class-statifyblacklist.php similarity index 91% rename from inc/statifyblacklist.class.php rename to inc/class-statifyblacklist.php index 0a2144d..3b2a7b2 100644 --- a/inc/statifyblacklist.class.php +++ b/inc/class-statifyblacklist.php @@ -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(). } } diff --git a/phpcs.xml b/phpcs.xml index 797a05d..410eb97 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -8,6 +8,7 @@ + diff --git a/statify-blacklist.php b/statify-blacklist.php index 3e5c74f..156a4b4 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -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 ) ) ) diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index 90a666e..6b20bbb 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -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. diff --git a/views/settings-page.php b/views/settings-page.php index c9919df..c7ffbd0 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -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(). ?>
@@ -92,16 +93,16 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) { esc_html( 'Statify plugin is not active.' ); print '

'; } - if ( isset( $statifyBlacklistPostWarning ) ) { + if ( isset( $statifyblacklist_post_warning ) ) { print '

' . - esc_html( $statifyBlacklistPostWarning ); + esc_html( $statifyblacklist_post_warning ); print '
'; esc_html_e( 'Settings have not been saved yet.', 'statify-blacklist' ); print '

'; } - if ( isset( $statifyBlacklistPostSuccess ) ) { + if ( isset( $statifyblacklist_post_success ) ) { print '

' . - esc_html( $statifyBlacklistPostSuccess ) . + esc_html( $statifyblacklist_post_success ) . '

'; } ?> @@ -152,8 +153,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {