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

@ -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

@ -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 ] );
}
}
}

@ -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' );
}
}

@ -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 );
}
}
}
}

@ -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 */

@ -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'] ) ) {
<small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?>)</small>
</label>
</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>
<label for="statify-blacklist_referer">
<?php esc_html_e( 'Referer blacklist:', 'statify-blacklist' ); ?><br/>
@ -101,12 +120,12 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<p class="submit">
<input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>">
<hr />
<input class="button-secondary" type="submit" name="cleanUp"
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.');">
<br />
<small><?php esc_html_e( 'Applies filter (even if disabled) to data stored in database. This cannot be undone!', 'statify-blacklist' ); ?></small>
<hr />
<input class="button-secondary" type="submit" name="cleanUp"
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.');">
<br />
<small><?php esc_html_e( 'Applies filter (even if disabled) to data stored in database. This cannot be undone!', 'statify-blacklist' ); ?></small>
</p>
</form>
</div>