Yoda conditions and strict comparison
This commit is contained in:
parent
5b1e490ace
commit
d769c6789c
@ -11,7 +11,8 @@ A blacklist extension for the famous [Statify](https://wordpress.org/plugins/sta
|
|||||||
|
|
||||||
This plugin adds customizable blacklist to Statify to allow blocking of referer spam or internal interactions.
|
This plugin adds customizable blacklist to Statify to allow blocking of referer spam or internal interactions.
|
||||||
|
|
||||||
### Current Features ##
|
### Features ##
|
||||||
|
|
||||||
#### Referer Blacklist ####
|
#### Referer Blacklist ####
|
||||||
Add a list of domains (for simplicity only second-level, e.g. _example.com_ which blocks _everything.example.com_).
|
Add a list of domains (for simplicity only second-level, e.g. _example.com_ which blocks _everything.example.com_).
|
||||||
|
|
||||||
@ -80,6 +81,10 @@ Because of this, an IP blacklist can only be applied while processing the reques
|
|||||||
|
|
||||||
## Changelog ##
|
## Changelog ##
|
||||||
|
|
||||||
|
### Work in Progress ###
|
||||||
|
* Relicensed to GPLv2 or later
|
||||||
|
* Minor changes for WP Coding Standard
|
||||||
|
|
||||||
### 1.4.0 / 10.06.2017 ###
|
### 1.4.0 / 10.06.2017 ###
|
||||||
* IP blacklist implemented (#7)
|
* IP blacklist implemented (#7)
|
||||||
* Target page blacklist implemented (#8)
|
* Target page blacklist implemented (#8)
|
||||||
|
@ -69,7 +69,7 @@ class StatifyBlacklist {
|
|||||||
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 ( 0 !== self::$_options['referer']['active'] ) {
|
||||||
add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) );
|
add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ class StatifyBlacklist {
|
|||||||
|
|
||||||
// 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 ( 1 === self::$_options['referer']['cron'] || 1 === self::$_options['target']['cron'] ) {
|
||||||
add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) );
|
add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ class StatifyBlacklist {
|
|||||||
* @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.
|
* @param array $options Optional. New options to save.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function update_options( $options = null ) {
|
public static function update_options( $options = null ) {
|
||||||
@ -159,20 +159,20 @@ class StatifyBlacklist {
|
|||||||
*/
|
*/
|
||||||
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'] ) && 0 !== self::$_options['referer']['active'] ) {
|
||||||
// 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 ( 2 === self::$_options['referer']['regexp'] ) {
|
||||||
$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 ( 1 === preg_match( $regexp, $referer ) ) ? 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 ) : '' ) );
|
||||||
@ -188,20 +188,20 @@ class StatifyBlacklist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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'] ) && 0 !== self::$_options['target']['active'] ) {
|
||||||
// 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'] ) && 0 < self::$_options['target']['regexp'] ) {
|
||||||
// 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 ( 2 === self::$_options['target']['regexp'] ) {
|
||||||
$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 ( 1 === preg_match( $regexp, $target ) ) ? 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'] ) : '/' );
|
||||||
@ -215,8 +215,8 @@ class StatifyBlacklist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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'] ) && 0 !== self::$_options['ip']['active'] ) {
|
||||||
if ( ( $ip = self::getIP() ) !== false ) {
|
if ( false !== ( $ip = self::getIP() ) ) {
|
||||||
foreach ( self::$_options['ip']['blacklist'] as $net ) {
|
foreach ( self::$_options['ip']['blacklist'] as $net ) {
|
||||||
if ( self::cidrMatch( $ip, $net ) ) {
|
if ( self::cidrMatch( $ip, $net ) ) {
|
||||||
return true;
|
return true;
|
||||||
@ -254,7 +254,7 @@ class StatifyBlacklist {
|
|||||||
) {
|
) {
|
||||||
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 ( false !== filter_var( $ip, FILTER_VALIDATE_IP ) ) {
|
||||||
return $ip;
|
return $ip;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -300,7 +300,7 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -326,7 +326,7 @@ class StatifyBlacklist {
|
|||||||
$mask = 32;
|
$mask = 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
/**
|
/**
|
||||||
* Update options.
|
* Update options.
|
||||||
*
|
*
|
||||||
* @param array $options New options to save.
|
* @param array $options Optional. 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
|
||||||
@ -30,7 +30,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
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 ( 0 === $options['referer']['regexp'] ) {
|
||||||
$sanitizedReferer = self::sanitizeURLs( $givenReferer );
|
$sanitizedReferer = self::sanitizeURLs( $givenReferer );
|
||||||
} else {
|
} else {
|
||||||
$sanitizedReferer = $givenReferer;
|
$sanitizedReferer = $givenReferer;
|
||||||
@ -107,7 +107,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function plugin_meta_link( $links, $file ) {
|
public static function plugin_meta_link( $links, $file ) {
|
||||||
if ( $file == STATIFYBLACKLIST_BASE ) {
|
if ( $file === STATIFYBLACKLIST_BASE ) {
|
||||||
$links[] = '<a href="https://github.com/stklcode/statify-blacklist">GitHub</a>';
|
$links[] = '<a href="https://github.com/stklcode/statify-blacklist">GitHub</a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
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' );
|
||||||
|
|
||||||
if ( $file == STATIFYBLACKLIST_BASE && current_user_can( 'manage_options' ) ) {
|
if ( $file === STATIFYBLACKLIST_BASE && current_user_can( 'manage_options' ) ) {
|
||||||
array_unshift(
|
array_unshift(
|
||||||
$links,
|
$links,
|
||||||
sprintf( '<a href="%s">%s</a>', esc_attr( add_query_arg( 'page', 'statify-blacklist', $base ) ), __( 'Settings' ) )
|
sprintf( '<a href="%s">%s</a>', esc_attr( add_query_arg( 'page', 'statify-blacklist', $base ) ), __( 'Settings' ) )
|
||||||
@ -152,8 +152,8 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
|
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
|
||||||
$cleanRef = ( self::$_options['referer']['cron'] == 1 );
|
$cleanRef = ( 1 === self::$_options['referer']['cron'] );
|
||||||
$cleanTrg = ( self::$_options['target']['cron'] == 1 );
|
$cleanTrg = ( 1 === self::$_options['target']['cron'] );
|
||||||
} else {
|
} else {
|
||||||
$cleanRef = true;
|
$cleanRef = true;
|
||||||
$cleanTrg = true;
|
$cleanTrg = true;
|
||||||
@ -192,7 +192,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
$wpdb->query(
|
$wpdb->query(
|
||||||
$wpdb->prepare(
|
$wpdb->prepare(
|
||||||
"DELETE FROM `$wpdb->statify` WHERE "
|
"DELETE FROM `$wpdb->statify` WHERE "
|
||||||
. ( ( self::$_options['referer']['regexp'] == 1 ) ? " BINARY " : "" )
|
. ( ( 1 === self::$_options['referer']['regexp'] ) ? " BINARY " : "" )
|
||||||
. "referrer REGEXP %s", $refererRegexp
|
. "referrer REGEXP %s", $refererRegexp
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -201,7 +201,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
$wpdb->query(
|
$wpdb->query(
|
||||||
$wpdb->prepare(
|
$wpdb->prepare(
|
||||||
"DELETE FROM `$wpdb->statify` WHERE "
|
"DELETE FROM `$wpdb->statify` WHERE "
|
||||||
. ( ( self::$_options['target']['regexp'] == 1 ) ? " BINARY " : "" )
|
. ( ( 1 === self::$_options['target']['regexp'] ) ? " BINARY " : "" )
|
||||||
. "target REGEXP %s", $targetRegexp
|
. "target REGEXP %s", $targetRegexp
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -125,7 +125,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
|||||||
),
|
),
|
||||||
'version' => 1.4
|
'version' => 1.4
|
||||||
);
|
);
|
||||||
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 {
|
||||||
update_option( 'statify-blacklist', $options );
|
update_option( 'statify-blacklist', $options );
|
||||||
@ -134,7 +134,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Version older than current major release.
|
// Version older than current major release.
|
||||||
if ( self::$_options['version'] < self::VERSION_MAIN ) {
|
if ( self::VERSION_MAIN > self::$_options['version'] ) {
|
||||||
// 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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user