diff --git a/README.md b/README.md index 6548b69..c702cd6 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,9 @@ If you like to have this feature, please leave a feature request in GitHub or th ## Changelog ## +### 1.3.0 / [under development] ### +* Regular expressions filtering implemented + ### 1.2.1 / 10.10.2016 ### * Fix live filter configuration check diff --git a/inc/statifyblacklist.class.php b/inc/statifyblacklist.class.php index d97f445..206285e 100644 --- a/inc/statifyblacklist.class.php +++ b/inc/statifyblacklist.class.php @@ -101,7 +101,8 @@ class StatifyBlacklist { array( 'active_referer' => 0, 'cron_referer' => 0, - 'referer' => array() + 'referer' => array(), + 'referer_regexp' => 0 ) ); } @@ -112,7 +113,7 @@ class StatifyBlacklist { * @return TRUE if referer matches blacklist. * * @since 1.0.0 - * @changed 1.2.0 + * @changed 1.3.0 */ public static function apply_blacklist_filter() { /* Skip if blacklist is inactive */ @@ -120,20 +121,26 @@ class StatifyBlacklist { return false; } - /* Extract relevant domain parts */ - $referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) ); - $referer = explode( '.', $referer ); -// if ( count( $referer ) > 1 ) { -// $referer = implode( '.', array_slice( $referer, - 2 ) ); -// } else { - $referer = implode( '.', $referer ); -// } + /* Regular Expression filtering since 1.3.0 */ + if ( isset(self::$_options['referer_regexp']) && self::$_options['referer_regexp'] > 0 ) { + /* Get full referer string */ + $referer = ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ); + /* Merge given regular expressions into one */ + $regexp = '/' . implode( "|", array_keys( self::$_options['referer'] ) ) . '/'; + if ( self::$_options['referer_regexp'] == 2 ) { + $regexp .= 'i'; + } + /* Check blacklist */ + return preg_match( $regexp, $referer) === 1; + } else { + /* Extract relevant domain parts */ + $referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) ); - /* Get blacklist */ - $blacklist = self::$_options['referer']; + /* Get blacklist */ + $blacklist = self::$_options['referer']; - /* Check blacklist */ - - return isset( $blacklist[ $referer ] ); + /* Check blacklist */ + return isset( $blacklist[ $referer ] ); + } } } diff --git a/inc/statifyblacklist_admin.class.php b/inc/statifyblacklist_admin.class.php index 9771745..07f9293 100644 --- a/inc/statifyblacklist_admin.class.php +++ b/inc/statifyblacklist_admin.class.php @@ -15,12 +15,16 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { * @param $options array New options to save * @return mixed array of sanitized array on errors, FALSE if there were none * @since 1.1.1 + * @changed 1.3.0 */ public static function update_options( $options = null ) { if ( isset( $options ) && current_user_can( 'manage_options' ) ) { /* Sanitize URLs and remove empty inputs */ $givenReferer = $options['referer']; - $sanitizedReferer = self::sanitizeURLs( $givenReferer ); + if ($options['referer_regexp'] == 0) + $sanitizedReferer = self::sanitizeURLs( $givenReferer ); + else + $sanitizedReferer = $givenReferer; /* Abort on errors */ if ( ! empty( array_diff( $givenReferer, $sanitizedReferer ) ) ) { @@ -110,7 +114,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { * Filter database for cleanup. * * @since 1.1.0 - * @changed 1.2.0 + * @changed 1.3.0 */ public static function cleanup_database() { /* Check user permissions */ @@ -120,11 +124,20 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { global $wpdb; - /* Sanitize URLs */ - $referer = self::sanitizeURLs( self::$_options['referer'] ); + if ( isset( self::$_options['referer_regexp'] ) && self::$_options['referer_regexp'] > 0 ) { + /* Merge given regular expressions into one */ + $refererRegexp = '/' . implode( "|", array_keys( self::$_options['referer'] ) ) . '/'; + if ( self::$_options['referer_regexp'] == 2 ) { + $refererRegexp .= 'i'; + } + } else { + /* Sanitize URLs */ + $referer = self::sanitizeURLs( self::$_options['referer'] ); + + /* Build filter regexp */ + $refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); + } - /* Build filter regexp */ - $refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); if ( ! empty( $refererRegexp ) ) { /* Execute filter on database */ $wpdb->query( @@ -135,7 +148,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { $wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" ); /* Delete transient statify data */ - delete_transient('statify_data'); + delete_transient( 'statify_data' ); } } diff --git a/inc/statifyblacklist_system.class.php b/inc/statifyblacklist_system.class.php index 2319aa9..bcf06c6 100644 --- a/inc/statifyblacklist_system.class.php +++ b/inc/statifyblacklist_system.class.php @@ -80,7 +80,8 @@ class StatifyBlacklist_System extends StatifyBlacklist { * @param object $upgrader Upgrader object (unused) * @param array $options Options array * - * @since 1.2.0 + * @since 1.2.0 + * @changed 1.3.0 */ public static function upgrade() { self::update_options(); @@ -95,5 +96,16 @@ class StatifyBlacklist_System extends StatifyBlacklist { update_option( 'statify-blacklist', $options ); } } + + /* Check if regular expressions option exists (pre 1.3.0) */ + if ( isset( self::$_options['referer_regexp'] ) ) { + $options = self::$_options; + $options['referer_regexp'] = 0; + if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) { + update_site_option( 'statify-blacklist', $options ); + } else { + update_option( 'statify-blacklist', $options ); + } + } } } diff --git a/statify-blacklist.php b/statify-blacklist.php index eaf5ea2..f747685 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -8,7 +8,7 @@ Author: Stefan Kalscheuer Author URI: https://stklcode.de Plugin URI: https://wordpress.org/plugins/statify-blacklist License: GPLv3 or later -Version: 1.2.1 +Version: 1.3.0 */ /* Quit */ diff --git a/views/settings_page.php b/views/settings_page.php index 1b51d08..f250e0e 100755 --- a/views/settings_page.php +++ b/views/settings_page.php @@ -29,7 +29,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) { array( 'active_referer' => (int) @$_POST['statifyblacklist']['active_referer'], 'cron_referer' => (int) @$_POST['statifyblacklist']['cron_referer'], - 'referer' => array_flip( $referer ) + 'referer' => array_flip( $referer ), + 'referer_regexp' => (int) @$_POST['statifyblacklist']['referer_regexp'] ) ); @@ -79,6 +80,24 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) { () +
  • + +