9 Commits

7 changed files with 517 additions and 396 deletions

View File

@ -1,8 +1,8 @@
# Statify Blacklist #
* Contributors: Stefan Kalscheuer
* Requires at least: 3.9
* Tested up to: 4.5.3
* Stable tag: 1.1.1
* Tested up to: 4.6
* Stable tag: 1.2.0
* License: GPLv3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
@ -13,13 +13,13 @@ This plugin adds customizable blacklist to Statify to allow blocking of referer
### Current Features ##
#### Referer Blacklist ####
Add a list of domains (for simplicity onl 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_).
#### CleanUp Database ####
Filters can be applied to data stored in database after modifying filter rules or for one-time clean-up.
#### Compatibility ####
This plugin requires Statify to be installed. The extension has been tested with Statify 1.4.2
This plugin requires Statify to be installed. The extension has been tested with Statify 1.4.3
The plugin is capable of handling multisite installations.
### Credits ###
@ -36,10 +36,41 @@ The plugin is capable of handling multisite installations.
* WordPress 3.9
* Statify plugin installed and activated (tested up to 1.4.3)
## Frequently Asked Questions ##
### What is blocked by default? ###
Nothing. By default all blacklists are empty and disabled. They can and have to be filled by the blog administrator.
A default blacklist is not provided, as the plugin itself is totally neutral. If you want to filter out referer spam,
visitors from search engines or just "false" referers from 301 redirects only depends on you.
### Does the filter effect user experience? ###
No. It only prevent's _Statify_ from tracking, nothing more or less.
### Does live filtering impact performance? ###
Yes, but probalby not noticeable. Checking a single referer string against a (usually small) list should be neglectible compared to the total loading procedure.
If this still is an issue for you, consider deactivating the filter and only run the one-time-cleanup or activate the cron job.
### Is any personal data collected? ###
No. The privacy policy of _Statify_ is untouched. Data is only processed, not stored or exposed to anyone.
### Are regular expression filters possible? ###
Not for now. At the moment it's only a simple domain filter, as regular expression matching is significantly slower.
If you like to have this feature, please leave a feature request in GitHub or the WordPress support forum.
## Screenshots ##
1. Statify Blacklist settings page
## Changelog ##
### 1.2.0 / 29.08.2016 ###
* Switched from `in_array()` to faster `isset()` for referer checking
* Optional cron execiton implemented
### 1.1.2 / 17.08.2016 ###
* Prepared for localization
### 1.1.1 / 16.08.2016 ###
* Some security fixes

View File

@ -8,8 +8,7 @@ defined('ABSPATH') OR exit;
*
* @since 1.0.0
*/
class StatifyBlacklist
{
class StatifyBlacklist {
/**
* Plugin options
*
@ -31,8 +30,7 @@ class StatifyBlacklist
*
* @since 1.0.0
*/
public static function instance()
{
public static function instance() {
new self();
}
@ -40,9 +38,9 @@ class StatifyBlacklist
* Class constructor
*
* @since 1.0.0
* @changed 1.1.2
*/
public function __construct()
{
public function __construct() {
/* Skip on autosave or AJAX */
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return;
@ -54,38 +52,55 @@ class StatifyBlacklist
/* Get multisite status */
self::$multisite = ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) );
/* Add Filter to statify hook */
/* Add Filter to statify hook if enabled */
if ( self::$_options['active_referer'] != 1 ) {
add_filter( 'statify_skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) );
}
/* Admin only filters */
if ( is_admin() ) {
/* Load Textdomain (only needed for backend */
load_plugin_textdomain( 'statifyblacklist', false, STATIFYBLACKLIST_DIR . '/lang/' );
/* Add actions */
add_action( 'wpmu_new_blog', array( 'StatifyBlacklist_Install', 'init_site' ) );
add_action( 'delete_blog', array( 'StatifyBlacklist_System', 'init_site' ) );
add_filter( 'plugin_row_meta', array( 'StatifyBlacklist_Admin', 'plugin_meta_link' ), 10, 2 );
if ( is_multisite() ) {
add_action( 'network_admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) );
add_filter('network_admin_plugin_action_links', array('StatifyBlacklist_Admin', 'plugin_actions_links'), 10, 2);
add_filter( 'network_admin_plugin_action_links', array(
'StatifyBlacklist_Admin',
'plugin_actions_links'
), 10, 2 );
} else {
add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) );
add_filter( 'plugin_action_links', array( 'StatifyBlacklist_Admin', 'plugin_actions_links' ), 10, 2 );
}
}
/* CronJob to clean up database */
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
if ( self::$_options['cron_referer'] == 1 ) {
add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) );
}
}
}
/**
* Update options
*
* @param $options array New options to save
*
* @since 1.0.0
* @changed 1.1.1
*/
public static function update_options() {
public static function update_options( $options = null ) {
self::$_options = wp_parse_args(
get_option( 'statify-blacklist' ),
array(
'active_referer' => 0,
'cron_referer' => 0,
'referer' => array()
)
);
@ -97,6 +112,7 @@ class StatifyBlacklist
* @return TRUE if referer matches blacklist.
*
* @since 1.0.0
* @changed 1.2.0
*/
public static function apply_blacklist_filter() {
/* Skip if blacklist is inactive */
@ -107,15 +123,17 @@ class StatifyBlacklist
/* 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
// if ( count( $referer ) > 1 ) {
// $referer = implode( '.', array_slice( $referer, - 2 ) );
// } else {
$referer = implode( '.', $referer );
// }
/* Get blacklist */
$blacklist = self::$_options['referer'];
/* Check blacklist */
return in_array($referer, $blacklist);
return isset( $blacklist[ $referer ] );
}
}

View File

@ -8,15 +8,15 @@ defined('ABSPATH') OR exit;
*
* @since 1.0.0
*/
class StatifyBlacklist_Admin extends StatifyBlacklist
{
class StatifyBlacklist_Admin extends StatifyBlacklist {
/**
* Update options
*
* @param $options array New options to save
* @return mixed array of sanitized array on errors, FALSE if there were none
* @since 1.1.1
*/
public static function update_options($options) {
public static function update_options( $options = null ) {
if ( isset( $options ) && current_user_can( 'manage_options' ) ) {
/* Sanitize URLs and remove empty inputs */
$givenReferer = $options['referer'];
@ -28,14 +28,15 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
}
/* Update database on success */
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 );
else
} else {
update_option( 'statify-blacklist', $options );
}
}
/* Refresh options */
parent::update_options();
parent::update_options( $options );
return false;
}
@ -47,10 +48,17 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
*/
public function _add_menu_page() {
$title = __( 'Statify Blacklist', 'statify-blacklist' );
if (self::$multisite)
add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'statify-blacklist-settings', array('StatifyBlacklist_Admin', 'settings_page') );
else
add_submenu_page( 'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array('StatifyBlacklist_Admin', 'settings_page') );
if ( self::$multisite ) {
add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'statify-blacklist-settings', array(
'StatifyBlacklist_Admin',
'settings_page'
) );
} else {
add_submenu_page( 'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array(
'StatifyBlacklist_Admin',
'settings_page'
) );
}
}
@ -63,6 +71,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
*
* @param $links
* @param $file
*
* @return array
*
* @since 1.0.0
@ -71,6 +80,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
if ( $file == STATIFYBLACKLIST_BASE ) {
$links[] = '<a href="https://github.com/stklcode/statify-blacklist">GitHub</a>';
}
return $links;
}
@ -78,6 +88,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
* Add plugin action links
*
* @param array $input Registered links
*
* @return array Merged links
*
* @since 1.0.0
@ -91,6 +102,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
sprintf( '<a href="%s">%s</a>', esc_attr( add_query_arg( 'page', 'statify-blacklist', $base ) ), __( 'Settings' ) )
);
}
return $links;
}
@ -98,12 +110,13 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
* Filter database for cleanup.
*
* @since 1.1.0
* @changed 1.1.1
* @changed 1.2.0
*/
public static function cleanup_database() {
/* Check user permissions */
if (!current_user_can('manage_options'))
if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
die( _e( 'Are you sure you want to do this?' ) );
}
global $wpdb;
@ -111,7 +124,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
$referer = self::sanitizeURLs( self::$_options['referer'] );
/* Build filter regexp */
$refererRegexp = str_replace('.', '\.', implode('|', $referer));
$refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
if ( ! empty( $refererRegexp ) ) {
/* Execute filter on database */
$wpdb->query(
@ -120,24 +133,32 @@ class StatifyBlacklist_Admin extends StatifyBlacklist
/* Optimize DB */
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
/* Delete transient statify data */
delete_transient('statify_data');
}
}
/**
* Sanitize URLs and remove empty results
*
* @param $urls array given array of URLs
*
* @return array sanitized array
*
* @since 1.1.1
* @changed 1.2.0
*/
private static function sanitizeURLs( $urls ) {
return array_filter(
return array_flip(
array_filter(
array_map(
function ( $r ) {
return preg_replace( '/[^\da-z\.-]/i', '', filter_var( $r, FILTER_SANITIZE_URL ) );
},
$urls
array_flip( $urls )
)
)
);
}

View File

@ -8,8 +8,7 @@ defined('ABSPATH') OR exit;
*
* @since 1.0.0
*/
class StatifyBlacklist_System extends StatifyBlacklist
{
class StatifyBlacklist_System extends StatifyBlacklist {
/**
* Plugin install handler.
*
@ -29,7 +28,10 @@ class StatifyBlacklist_System extends StatifyBlacklist
switch_to_blog( $site_id );
add_option(
'statify-blacklist',
array()
array(
'activate-referer' => 0,
'referer' => array()
)
);
}
@ -37,7 +39,10 @@ class StatifyBlacklist_System extends StatifyBlacklist
} else {
add_option(
'statify-blacklist',
array()
array(
'activate-referer' => 0,
'referer' => array()
)
);
}
}
@ -67,4 +72,28 @@ class StatifyBlacklist_System extends StatifyBlacklist
delete_option( 'statify-blacklist' );
}
/**
* Upgrade plugin options.
*
* @param object $upgrader Upgrader object (unused)
* @param array $options Options array
*
* @since 1.2.0
*/
public static function upgrade() {
self::update_options();
/* Check if config array is not associative (pre 1.2.0) */
if ( array_keys( self::$_options['referer'] ) === range( 0, count( self::$_options['referer'] ) - 1 ) ) {
/* Flip referer array to make domains keys */
$options = self::$_options;
$options['referer'] = array_flip( self::$_options['referer'] );
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 );
}
}
}
}

52
statify-blacklist.php Normal file
View File

@ -0,0 +1,52 @@
<?php
/*
Plugin Name: Statify Blacklist
Description: Extension for the statify plugin to add a customizable blacklists.
Text Domain: statify-blacklist
Domain Path: /lang
Author: Stefan Kalscheuer
Author URI: https://stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist
License: GPLv3 or later
Version: 1.2.0
*/
/* Quit */
defined( 'ABSPATH' ) OR exit;
/* Constants */
define( 'STATIFYBLACKLIST_FILE', __FILE__ );
define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) );
define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) );
/* System Hooks */
add_action( 'plugins_loaded', array( 'StatifyBlacklist', 'instance' ) );
register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'install' ) );
register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) );
/* Upgrade hook to v1.2.0 */
register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) );
/* Autoload */
spl_autoload_register( 'statifyBlacklist_autoload' );
/**
* Autoloader for StatifyBlacklist classes.
*
* @param $class
*
* @since 1.0.0
*/
function statifyBlacklist_autoload( $class ) {
$plugin_classes = array(
'StatifyBlacklist',
'StatifyBlacklist_Admin',
'StatifyBlacklist_System'
);
if ( in_array( $class, $plugin_classes ) ) {
require_once( sprintf( '%s/inc/%s.class.php', STATIFYBLACKLIST_DIR, strtolower( $class ) ) );
}
}

View File

@ -1,48 +0,0 @@
<?php
/*
Plugin Name: Statify Blacklist
Description: Extension for the statify plugin to add a customizable blacklists.
Text Domain: statify-blacklist
Domain Path: /lang
Author: Stefan Kalscheuer
Author URI: https://stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist
License: GPLv3 or later
Version: 1.1.1
*/
/* Quit */
defined('ABSPATH') OR exit;
/* Constants */
define('STATIFYBLACKLIST_FILE', __FILE__);
define('STATIFYBLACKLIST_DIR', dirname(__FILE__));
define('STATIFYBLACKLIST_BASE', plugin_basename(__FILE__));
/* System Hooks */
add_action('plugins_loaded', array('StatifyBlacklist', 'instance'));
register_activation_hook(STATIFYBLACKLIST_FILE, array('StatifyBlacklist_System', 'install'));
register_uninstall_hook(STATIFYBLACKLIST_FILE, array('StatifyBlacklist_System', 'uninstall'));
/* Autoload */
spl_autoload_register('statifyBlacklist_autoload');
/**
* Autoloader for StatifyBlacklist classes.
*
* @param $class
* @since 1.0.0
*/
function statifyBlacklist_autoload($class) {
$plugin_classes = array(
'StatifyBlacklist',
'StatifyBlacklist_Admin',
'StatifyBlacklist_System'
);
if (in_array($class, $plugin_classes)) {
require_once(sprintf('%s/inc/%s.class.php', STATIFYBLACKLIST_DIR, strtolower($class)));
}
}

46
views/settings_page.php Normal file → Executable file
View File

@ -18,14 +18,18 @@ if ( !empty($_POST['statifyblacklist']) ) {
StatifyBlacklist_Admin::cleanup_database();
} else {
/* Extract referer array */
if (empty(trim($_POST['statifyblacklist']['referer']))) $referer = array();
else $referer = explode("\r\n", $_POST['statifyblacklist']['referer']);
if ( empty( trim( $_POST['statifyblacklist']['referer'] ) ) ) {
$referer = array();
} else {
$referer = explode( "\r\n", $_POST['statifyblacklist']['referer'] );
}
/* Update options (data will be sanitized) */
$statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options(
array(
'active_referer' => (int) @$_POST['statifyblacklist']['active_referer'],
'referer' => $referer
'cron_referer' => (int) @$_POST['statifyblacklist']['cron_referer'],
'referer' => array_flip( $referer )
)
);
@ -37,7 +41,6 @@ if ( !empty($_POST['statifyblacklist']) ) {
}
}
}
?>
<div class="wrap">
@ -63,20 +66,34 @@ if ( !empty($_POST['statifyblacklist']) ) {
<ul style="list-style: none;">
<li>
<label for="statify-blacklist_active_referer">
<input type="checkbox" name="statifyblacklist[active_referer]" id="statifyblacklist_active_referer" value="1" <?php checked(StatifyBlacklist::$_options['active_referer'], 1); ?> />
<input type="checkbox" name="statifyblacklist[active_referer]" id="statifyblacklist_active_referer"
value="1" <?php checked( StatifyBlacklist::$_options['active_referer'], 1 ); ?> />
<?php esc_html_e( 'Activate referer blacklist', 'statify-blacklist' ); ?>
</label>
</li>
<li>
<label for="statify-blacklist_cron_referer">
<input type="checkbox" name="statifyblacklist[cron_referer]" id="statifyblacklist_cron_referer"
value="1" <?php checked( StatifyBlacklist::$_options['cron_referer'], 1 ); ?> />
<?php esc_html_e( 'CronJob execution', 'statify-blacklist' ); ?>
<small>(<?php esc_html_e( 'Clean database periodically in background', 'statify-blacklist' ); ?>)</small>
</label>
</li>
<li>
<label for="statify-blacklist_referer">
<?php esc_html_e( 'Referer blacklist:', 'statify-blacklist' ); ?><br/>
<textarea cols="40" rows="5" name="statifyblacklist[referer]" id="statify-blacklist_referer"><?php
if (isset($statifyBlacklistUpdateResult) &&$statifyBlacklistUpdateResult !== false)
print esc_html(implode("\r\n", $statifyBlacklistUpdateResult));
else
print esc_html(implode("\r\n", StatifyBlacklist::$_options['referer']));
?></textarea><br />
<small>(<?php esc_html_e('Add one domain (without subdomains) each line, e.g. example.com', 'statify-blacklist'); ?>)</small>
if ( isset( $statifyBlacklistUpdateResult ) && $statifyBlacklistUpdateResult !== false ) {
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult ) ) );
} else {
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['referer'] ) ) );
}
?></textarea>
<br />
<small>
(<?php esc_html_e( 'Add one domain (without subdomains) each line, e.g. example.com', 'statify-blacklist' ); ?>
)
</small>
</label>
</li>
</ul>
@ -84,10 +101,11 @@ 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') ?>"
<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>
<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>