Feature #4 Regular expression filters

This commit is contained in:
Stefan Kalscheuer 2016-10-09 17:59:30 +02:00
parent e80040fb7e
commit 25b16746b2
6 changed files with 85 additions and 31 deletions

View File

@ -65,6 +65,9 @@ If you like to have this feature, please leave a feature request in GitHub or th
## Changelog ## ## Changelog ##
### 1.3.0 / [under development] ###
* Regular expressions filtering implemented
### 1.2.1 / 10.10.2016 ### ### 1.2.1 / 10.10.2016 ###
* Fix live filter configuration check * Fix live filter configuration check

View File

@ -101,7 +101,8 @@ class StatifyBlacklist {
array( array(
'active_referer' => 0, 'active_referer' => 0,
'cron_referer' => 0, 'cron_referer' => 0,
'referer' => array() 'referer' => array(),
'referer_regexp' => 0
) )
); );
} }
@ -112,7 +113,7 @@ class StatifyBlacklist {
* @return TRUE if referer matches blacklist. * @return TRUE if referer matches blacklist.
* *
* @since 1.0.0 * @since 1.0.0
* @changed 1.2.0 * @changed 1.3.0
*/ */
public static function apply_blacklist_filter() { public static function apply_blacklist_filter() {
/* Skip if blacklist is inactive */ /* Skip if blacklist is inactive */
@ -120,20 +121,26 @@ class StatifyBlacklist {
return false; return false;
} }
/* Extract relevant domain parts */ /* Regular Expression filtering since 1.3.0 */
$referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) ); if ( isset(self::$_options['referer_regexp']) && self::$_options['referer_regexp'] > 0 ) {
$referer = explode( '.', $referer ); /* Get full referer string */
// if ( count( $referer ) > 1 ) { $referer = ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' );
// $referer = implode( '.', array_slice( $referer, - 2 ) ); /* Merge given regular expressions into one */
// } else { $regexp = '/' . implode( "|", array_keys( self::$_options['referer'] ) ) . '/';
$referer = implode( '.', $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 */ /* Get blacklist */
$blacklist = self::$_options['referer']; $blacklist = self::$_options['referer'];
/* Check blacklist */ /* Check blacklist */
return isset( $blacklist[ $referer ] );
return isset( $blacklist[ $referer ] ); }
} }
} }

View File

@ -15,12 +15,16 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* @param $options array New options to save * @param $options array New options to save
* @return mixed array of sanitized array on errors, FALSE if there were none * @return mixed array of sanitized array on errors, FALSE if there were none
* @since 1.1.1 * @since 1.1.1
* @changed 1.3.0
*/ */
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']; $givenReferer = $options['referer'];
$sanitizedReferer = self::sanitizeURLs( $givenReferer ); if ($options['referer_regexp'] == 0)
$sanitizedReferer = self::sanitizeURLs( $givenReferer );
else
$sanitizedReferer = $givenReferer;
/* Abort on errors */ /* Abort on errors */
if ( ! empty( array_diff( $givenReferer, $sanitizedReferer ) ) ) { if ( ! empty( array_diff( $givenReferer, $sanitizedReferer ) ) ) {
@ -110,7 +114,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* Filter database for cleanup. * Filter database for cleanup.
* *
* @since 1.1.0 * @since 1.1.0
* @changed 1.2.0 * @changed 1.3.0
*/ */
public static function cleanup_database() { public static function cleanup_database() {
/* Check user permissions */ /* Check user permissions */
@ -120,11 +124,20 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
global $wpdb; global $wpdb;
/* Sanitize URLs */ if ( isset( self::$_options['referer_regexp'] ) && self::$_options['referer_regexp'] > 0 ) {
$referer = self::sanitizeURLs( self::$_options['referer'] ); /* 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 ) ) { if ( ! empty( $refererRegexp ) ) {
/* Execute filter on database */ /* Execute filter on database */
$wpdb->query( $wpdb->query(
@ -135,7 +148,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" ); $wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
/* Delete transient statify data */ /* Delete transient statify data */
delete_transient('statify_data'); delete_transient( 'statify_data' );
} }
} }

View File

@ -80,7 +80,8 @@ class StatifyBlacklist_System extends StatifyBlacklist {
* @param object $upgrader Upgrader object (unused) * @param object $upgrader Upgrader object (unused)
* @param array $options Options array * @param array $options Options array
* *
* @since 1.2.0 * @since 1.2.0
* @changed 1.3.0
*/ */
public static function upgrade() { public static function upgrade() {
self::update_options(); self::update_options();
@ -95,5 +96,16 @@ class StatifyBlacklist_System extends StatifyBlacklist {
update_option( 'statify-blacklist', $options ); 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 );
}
}
} }
} }

View File

@ -8,7 +8,7 @@ Author: Stefan Kalscheuer
Author URI: https://stklcode.de Author URI: https://stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist Plugin URI: https://wordpress.org/plugins/statify-blacklist
License: GPLv3 or later License: GPLv3 or later
Version: 1.2.1 Version: 1.3.0
*/ */
/* Quit */ /* Quit */

View File

@ -29,7 +29,8 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
array( array(
'active_referer' => (int) @$_POST['statifyblacklist']['active_referer'], 'active_referer' => (int) @$_POST['statifyblacklist']['active_referer'],
'cron_referer' => (int) @$_POST['statifyblacklist']['cron_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'] ) ) {
<small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?>)</small> <small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?>)</small>
</label> </label>
</li> </li>
<li>
<label for="statify-blacklist_referer_regexp">
<?php esc_html_e( 'Use regular expressions', 'statify-blacklist' ); ?>:
<select name="statifyblacklist[referer_regexp]" id="statifyblacklist_referer_regexp">
<option value="0" <?php selected( StatifyBlacklist::$_options['referer_regexp'], 0 ); ?>>
<?php esc_html_e( 'Disabled', 'statify-blacklist' ); ?>
</option>
<option value="1" <?php selected( StatifyBlacklist::$_options['referer_regexp'], 1 ); ?>>
<?php esc_html_e( 'Case-sensitive', 'statify-blacklist' ); ?>
</option>
<option value="2" <?php selected( StatifyBlacklist::$_options['referer_regexp'], 2 ); ?>>
<?php esc_html_e( 'Case-insensitive', 'statify-blacklist' ); ?>
</option>
</select>
<br />
<small>(<?php esc_html_e( 'Performance slower than standard domain filter. Recommended for cron or manual execition only.', 'statify-blacklist' ); ?>)</small>
</label>
</li>
<li> <li>
<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/>
@ -101,12 +120,12 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<p class="submit"> <p class="submit">
<input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>"> <input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>">
<hr /> <hr />
<input class="button-secondary" type="submit" name="cleanUp" <input class="button-secondary" type="submit" name="cleanUp"
value="<?php esc_html_e( 'CleanUp Database', 'statify-blacklist' ) ?>" value="<?php esc_html_e( 'CleanUp Database', 'statify-blacklist' ) ?>"
onclick="return confirm('Do you really want to apply filters to database? This cannot be undone.');"> onclick="return confirm('Do you really want to apply filters to database? This cannot be undone.');">
<br /> <br />
<small><?php esc_html_e( 'Applies filter (even if disabled) to data stored in database. This cannot be undone!', 'statify-blacklist' ); ?></small> <small><?php esc_html_e( 'Applies filter (even if disabled) to data stored in database. This cannot be undone!', 'statify-blacklist' ); ?></small>
</p> </p>
</form> </form>
</div> </div>