Comments and code style reworked
To adhere a little more to the WP Coding Standards, PHPdoc blocks and inline have been reworked. No code has been changed.
This commit is contained in:
@ -1,36 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Statify Blacklist: StatifyBlacklist class
|
||||
*
|
||||
* This file contains the plugin's base class.
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
/* Quit */
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) OR exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist
|
||||
* Statify Blacklist.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @version 1.4.0
|
||||
*/
|
||||
class StatifyBlacklist {
|
||||
|
||||
/**
|
||||
* Plugin major version.
|
||||
*
|
||||
* @since 1.4.0
|
||||
* @var int VERSION_MAIN
|
||||
*/
|
||||
const VERSION_MAIN = 1.4;
|
||||
|
||||
/**
|
||||
* Plugin options
|
||||
* Plugin options.
|
||||
*
|
||||
* @var array
|
||||
* @since 1.0.0
|
||||
* @var array $_options
|
||||
*/
|
||||
public static $_options;
|
||||
|
||||
/**
|
||||
* Multisite Status
|
||||
* Multisite Status.
|
||||
*
|
||||
* @var bool
|
||||
* @since 1.0.0
|
||||
* @var bool $multisite
|
||||
*/
|
||||
public static $multisite;
|
||||
|
||||
/**
|
||||
* Class self initialize
|
||||
* Class self initialize.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@ -39,50 +52,52 @@ class StatifyBlacklist {
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
* Class constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct() {
|
||||
/* Skip on autosave or AJAX */
|
||||
// Skip on autosave or AJAX.
|
||||
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Plugin options */
|
||||
// Plugin options.
|
||||
self::update_options();
|
||||
|
||||
/* Get multisite status */
|
||||
// Get multisite status.
|
||||
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 ) {
|
||||
add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) );
|
||||
}
|
||||
|
||||
/* Admin only filters */
|
||||
// Admin only filters.
|
||||
if ( is_admin() ) {
|
||||
/* Load Textdomain (only needed for backend */
|
||||
// Load Textdomain (only needed for backend.
|
||||
load_plugin_textdomain( 'statifyblacklist', false, STATIFYBLACKLIST_DIR . '/lang/' );
|
||||
|
||||
/* Add actions */
|
||||
// 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(
|
||||
add_filter(
|
||||
'network_admin_plugin_action_links', array(
|
||||
'StatifyBlacklist_Admin',
|
||||
'plugin_actions_links'
|
||||
), 10, 2 );
|
||||
), 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 */
|
||||
// CronJob to clean up database.
|
||||
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
|
||||
if ( self::$_options['referer']['cron'] == 1 || self::$_options['target']['cron'] == 1 ) {
|
||||
add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) );
|
||||
@ -91,12 +106,13 @@ class StatifyBlacklist {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update options
|
||||
*
|
||||
* @param array $options New options to save
|
||||
* Update options.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
*/
|
||||
public static function update_options( $options = null ) {
|
||||
self::$_options = wp_parse_args(
|
||||
@ -110,7 +126,7 @@ class StatifyBlacklist {
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @return array the options array
|
||||
* @return array The options array.
|
||||
*/
|
||||
protected static function defaultOptions() {
|
||||
return array(
|
||||
@ -137,68 +153,68 @@ class StatifyBlacklist {
|
||||
/**
|
||||
* Apply the blacklist filter if active
|
||||
*
|
||||
* @return bool TRUE if referer matches blacklist.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool TRUE if referer matches blacklist.
|
||||
*/
|
||||
public static function apply_blacklist_filter() {
|
||||
/* Referer blacklist */
|
||||
// Referer blacklist.
|
||||
if ( isset( self::$_options['referer']['active'] ) && self::$_options['referer']['active'] != 0 ) {
|
||||
/* 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 ) {
|
||||
/* Get full referer string */
|
||||
// Get full referer string.
|
||||
$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'] ) ) . '/';
|
||||
if ( self::$_options['referer']['regexp'] == 2 ) {
|
||||
$regexp .= 'i';
|
||||
}
|
||||
|
||||
/* Check blacklist (return NULL to continue filtering) */
|
||||
// Check blacklist (return NULL to continue filtering).
|
||||
|
||||
return ( preg_match( $regexp, $referer ) === 1 ) ? true : null;
|
||||
} else {
|
||||
/* Extract relevant domain parts */
|
||||
// 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'];
|
||||
|
||||
/* Check blacklist */
|
||||
if ( isset( $blacklist[ $referer ] ) ) {
|
||||
// Check blacklist.
|
||||
if ( isset( $blacklist[$referer] ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Target blacklist (since 1.4.0) */
|
||||
// Target blacklist (since 1.4.0)
|
||||
if ( isset( self::$_options['target']['active'] ) && self::$_options['target']['active'] != 0 ) {
|
||||
/* 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 ) {
|
||||
/* Get full referer string */
|
||||
// Get full referer string.
|
||||
$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'] ) ) . '/';
|
||||
if ( self::$_options['target']['regexp'] == 2 ) {
|
||||
$regexp .= 'i';
|
||||
}
|
||||
|
||||
/* Check blacklist (return NULL to continue filtering) */
|
||||
// Check blacklist (return NULL to continue filtering).
|
||||
|
||||
return ( preg_match( $regexp, $target ) === 1 ) ? true : null;
|
||||
} else {
|
||||
/* Extract target page */
|
||||
// Extract target page.
|
||||
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' );
|
||||
/* Get blacklist */
|
||||
// Get blacklist.
|
||||
$blacklist = self::$_options['target']['blacklist'];
|
||||
/* Check blacklist */
|
||||
if ( isset( $blacklist[ $target ] ) ) {
|
||||
// Check blacklist.
|
||||
if ( isset( $blacklist[$target] ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* IP blacklist (since 1.4.0) */
|
||||
// IP blacklist (since 1.4.0).
|
||||
if ( isset ( self::$_options['ip']['active'] ) && self::$_options['ip']['active'] != 0 ) {
|
||||
if ( ( $ip = self::getIP() ) !== false ) {
|
||||
foreach ( self::$_options['ip']['blacklist'] as $net ) {
|
||||
@ -209,34 +225,35 @@ class StatifyBlacklist {
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip and continue (return NULL), if all blacklists are inactive */
|
||||
// Skip and continue (return NULL), if all blacklists are inactive.
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to determine the client's IP address.
|
||||
*
|
||||
* If a proxy is used, the X-Real-IP or X-Forwarded-For header is checked, otherwise the default remote address.
|
||||
* For performance reasons only the most common flags are checked. This might be even reduce by user configuration.
|
||||
* Maybe some community feedback will ease the decision on that.
|
||||
*
|
||||
* @return string|bool the client's IP address or FALSE, if none could be determined
|
||||
* @return string|bool the client's IP address or FALSE, if none could be determined.
|
||||
*/
|
||||
private static function getIP() {
|
||||
foreach (
|
||||
array(
|
||||
// 'HTTP_CLIENT_IP',
|
||||
// 'HTTP_CLIENT_IP',
|
||||
'HTTP_X_REAL_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
// 'HTTP_X_FORWARDED',
|
||||
// 'HTTP_X_CLUSTER_CLIENT_IP',
|
||||
// 'HTTP_FORWARDED_FOR',
|
||||
// 'HTTP_FORWARDED',
|
||||
// 'HTTP_X_FORWARDED',
|
||||
// 'HTTP_X_CLUSTER_CLIENT_IP',
|
||||
// 'HTTP_FORWARDED_FOR',
|
||||
// 'HTTP_FORWARDED',
|
||||
'REMOTE_ADDR'
|
||||
) as $k
|
||||
) {
|
||||
if ( isset( $_SERVER[ $k ] ) ) {
|
||||
foreach ( explode( ',', $_SERVER[ $k ] ) as $ip ) {
|
||||
if ( isset( $_SERVER[$k] ) ) {
|
||||
foreach ( explode( ',', $_SERVER[$k] ) as $ip ) {
|
||||
if ( filter_var( $ip, FILTER_VALIDATE_IP ) !== false ) {
|
||||
return $ip;
|
||||
}
|
||||
@ -250,18 +267,18 @@ class StatifyBlacklist {
|
||||
/**
|
||||
* Helper function to check if an IP address matches a given subnet.
|
||||
*
|
||||
* @param string $ip IP address to check
|
||||
* @param string $net IP address or subnet in CIDR notation
|
||||
* @param string $ip IP address to check.
|
||||
* @param string $net IP address or subnet in CIDR notation.
|
||||
*
|
||||
* @return bool TRUE, if the given IP addresses matches the given subnet
|
||||
* @return bool TRUE, if the given IP addresses matches the given subnet.
|
||||
*/
|
||||
private static function cidrMatch( $ip, $net ) {
|
||||
if ( substr_count( $net, ':' ) > 1 ) { /* Check for IPv6 */
|
||||
if ( substr_count( $net, ':' ) > 1 ) { // Check for IPv6.
|
||||
if ( ! ( ( extension_loaded( 'sockets' ) && defined( 'AF_INET6' ) ) || @inet_pton( '::1' ) ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( false !== strpos( $net, '/' ) ) { /* Parse CIDR subnet */
|
||||
if ( false !== strpos( $net, '/' ) ) { // Parse CIDR subnet.
|
||||
list( $base, $mask ) = explode( '/', $net, 2 );
|
||||
|
||||
if ( $mask < 1 || $mask > 128 ) {
|
||||
@ -283,18 +300,18 @@ class StatifyBlacklist {
|
||||
$left = $mask - 16 * ( $i - 1 );
|
||||
$left = ( $left <= 16 ) ? $left : 16;
|
||||
$maskB = ~( 0xffff >> $left ) & 0xffff;
|
||||
if ( ( $bytesAddr[ $i ] & $maskB ) != ( $bytesTest[ $i ] & $maskB ) ) {
|
||||
if ( ( $bytesAddr[$i] & $maskB ) != ( $bytesTest[$i] & $maskB ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} else { /* Check for IPv4 */
|
||||
} else { // Check for IPv4.
|
||||
if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( false !== strpos( $net, '/' ) ) { /* Parse CIDR subnet */
|
||||
if ( false !== strpos( $net, '/' ) ) { // Parse CIDR subnet.
|
||||
list( $base, $mask ) = explode( '/', $net, 2 );
|
||||
|
||||
if ( $mask === '0' ) {
|
||||
@ -304,7 +321,7 @@ class StatifyBlacklist {
|
||||
if ( $mask < 0 || $mask > 32 ) {
|
||||
return false;
|
||||
}
|
||||
} else { /* Use single address */
|
||||
} else { // Use single address.
|
||||
$base = $net;
|
||||
$mask = 32;
|
||||
}
|
||||
|
@ -1,26 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* Statify Blacklist: StatifyBlacklist_Admin class
|
||||
*
|
||||
* This file contains the derived class for the plugin's administration features.
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
* @subpackge Admin
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
/* Quit */
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) OR exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist admin configuration
|
||||
* Statify Blacklist admin configuration.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @version 1.4.0
|
||||
*/
|
||||
class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
/**
|
||||
* Update options
|
||||
* Update options.
|
||||
*
|
||||
* @param array $options New options to save
|
||||
* @param array $options 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
|
||||
*/
|
||||
public static function update_options( $options = null ) {
|
||||
if ( isset( $options ) && current_user_can( 'manage_options' ) ) {
|
||||
/* Sanitize URLs and remove empty inputs */
|
||||
// Sanitize URLs and remove empty inputs.
|
||||
$givenReferer = $options['referer']['blacklist'];
|
||||
if ( $options['referer']['regexp'] == 0 ) {
|
||||
$sanitizedReferer = self::sanitizeURLs( $givenReferer );
|
||||
@ -28,18 +36,18 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
$sanitizedReferer = $givenReferer;
|
||||
}
|
||||
|
||||
/* Sanitize IPs and Subnets and remove empty inputs */
|
||||
// Sanitize IPs and Subnets and remove empty inputs.
|
||||
$givenIP = $options['ip']['blacklist'];
|
||||
$sanitizedIP = self::sanitizeIPs( $givenIP );
|
||||
|
||||
/* Abort on errors */
|
||||
// Abort on errors.
|
||||
if ( ! empty( array_diff( array_keys( $givenReferer ), array_keys( $sanitizedReferer ) ) ) ) {
|
||||
return array( 'referer' => $sanitizedReferer );
|
||||
} elseif ( ! empty( array_diff( $givenIP, $sanitizedIP ) ) ) {
|
||||
return array( 'ip' => array_diff( $givenIP, $sanitizedIP ) );
|
||||
}
|
||||
|
||||
/* Update database on success */
|
||||
// Update database on success.
|
||||
if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) {
|
||||
update_site_option( 'statify-blacklist', $options );
|
||||
} else {
|
||||
@ -47,33 +55,42 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
}
|
||||
}
|
||||
|
||||
/* Refresh options */
|
||||
// Refresh options.
|
||||
parent::update_options( $options );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configuration page to admin menu
|
||||
* Add configuration page to admin menu.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
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'
|
||||
) );
|
||||
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'
|
||||
) );
|
||||
add_submenu_page(
|
||||
'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array(
|
||||
'StatifyBlacklist_Admin',
|
||||
'settings_page'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the Statify-Blacklist settings page.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function settings_page() {
|
||||
include STATIFYBLACKLIST_DIR . '/views/settings_page.php';
|
||||
}
|
||||
@ -81,12 +98,13 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
/**
|
||||
* Add plugin meta links
|
||||
*
|
||||
* @param array $links Registered links
|
||||
* @param string $file The filename
|
||||
*
|
||||
* @return array Merged links
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $links Registered links.
|
||||
* @param string $file The filename.
|
||||
*
|
||||
* @return array Merged links.
|
||||
*
|
||||
*/
|
||||
public static function plugin_meta_link( $links, $file ) {
|
||||
if ( $file == STATIFYBLACKLIST_BASE ) {
|
||||
@ -97,14 +115,15 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add plugin action links
|
||||
*
|
||||
* @param array $links Registered links
|
||||
* @param string $file The filename
|
||||
*
|
||||
* @return array Merged links
|
||||
* Add plugin action links.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $links Registered links
|
||||
* @param string $file The filename
|
||||
*
|
||||
* @return array Merged links.
|
||||
*
|
||||
*/
|
||||
public static function plugin_actions_links( $links, $file ) {
|
||||
$base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
|
||||
@ -123,9 +142,11 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
* Filter database for cleanup.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database.
|
||||
*/
|
||||
public static function cleanup_database() {
|
||||
/* Check user permissions */
|
||||
// Check user permissions.
|
||||
if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
|
||||
die( __( 'Are you sure you want to do this?' ) );
|
||||
}
|
||||
@ -141,23 +162,23 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
|
||||
if ( $cleanRef ) {
|
||||
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) {
|
||||
/* Merge given regular expressions into one */
|
||||
// Merge given regular expressions into one.
|
||||
$refererRegexp = implode( "|", array_keys( self::$_options['referer']['blacklist'] ) );
|
||||
} else {
|
||||
/* Sanitize URLs */
|
||||
// Sanitize URLs.
|
||||
$referer = self::sanitizeURLs( self::$_options['referer']['blacklist'] );
|
||||
|
||||
/* Build filter regexp */
|
||||
// Build filter regexp.
|
||||
$refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $cleanTrg ) {
|
||||
if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) {
|
||||
/* Merge given regular expressions into one */
|
||||
// Merge given regular expressions into one.
|
||||
$targetRegexp = implode( "|", array_keys( self::$_options['target']['blacklist'] ) );
|
||||
} else {
|
||||
/* Build filter regexp */
|
||||
// Build filter regexp.
|
||||
$targetRegexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) );
|
||||
}
|
||||
}
|
||||
@ -166,39 +187,44 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
if ( ! empty( $refererRegexp ) || ! empty( $targetRegexp ) ) {
|
||||
global $wpdb;
|
||||
|
||||
/* Execute filter on database */
|
||||
// Execute filter on database.
|
||||
if ( ! empty( $refererRegexp ) ) {
|
||||
$wpdb->query(
|
||||
$wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE "
|
||||
. ( ( self::$_options['referer']['regexp'] == 1 ) ? " BINARY " : "" )
|
||||
. "referrer REGEXP %s", $refererRegexp )
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM `$wpdb->statify` WHERE "
|
||||
. ( ( self::$_options['referer']['regexp'] == 1 ) ? " BINARY " : "" )
|
||||
. "referrer REGEXP %s", $refererRegexp
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( ! empty( $targetRegexp ) ) {
|
||||
$wpdb->query(
|
||||
$wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE "
|
||||
. ( ( self::$_options['target']['regexp'] == 1 ) ? " BINARY " : "" )
|
||||
. "target REGEXP %s", $targetRegexp )
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM `$wpdb->statify` WHERE "
|
||||
. ( ( self::$_options['target']['regexp'] == 1 ) ? " BINARY " : "" )
|
||||
. "target REGEXP %s", $targetRegexp
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/* Optimize DB */
|
||||
// Optimize DB.
|
||||
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
|
||||
|
||||
/* Delete transient statify data */
|
||||
// Delete transient statify data.
|
||||
delete_transient( 'statify_data' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sanitize URLs and remove empty results
|
||||
*
|
||||
* @param array $urls given array of URLs
|
||||
*
|
||||
* @return array sanitized array
|
||||
* Sanitize URLs and remove empty results.
|
||||
*
|
||||
* @since 1.1.1
|
||||
*
|
||||
* @param array $urls given array of URLs.
|
||||
*
|
||||
* @return array sanitized array.
|
||||
*
|
||||
*/
|
||||
private static function sanitizeURLs( $urls ) {
|
||||
return array_flip(
|
||||
@ -214,20 +240,27 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize IP addresses with optional CIDR notation and remove empty results
|
||||
*
|
||||
* @param array $ips given array of URLs
|
||||
*
|
||||
* @return array sanitized array
|
||||
* Sanitize IP addresses with optional CIDR notation and remove empty results.
|
||||
*
|
||||
* @since 1.4.0
|
||||
*
|
||||
* @param array $ips given array of URLs.
|
||||
*
|
||||
* @return array sanitized array.
|
||||
*
|
||||
*/
|
||||
private static function sanitizeIPs( $ips ) {
|
||||
return array_filter( $ips, function ( $ip ) {
|
||||
return preg_match( '/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' .
|
||||
'(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $ip ) ||
|
||||
preg_match( '/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' .
|
||||
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/', $ip );
|
||||
} );
|
||||
return array_filter(
|
||||
$ips, function ( $ip ) {
|
||||
return preg_match(
|
||||
'/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' .
|
||||
'(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $ip
|
||||
) ||
|
||||
preg_match(
|
||||
'/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' .
|
||||
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/', $ip
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* Statify Blacklist: StatifyBlacklist_Syste, class
|
||||
*
|
||||
* This file contains the derived class for the plugin's system operations.
|
||||
*
|
||||
* @package Statify_Blacklist
|
||||
* @subpackge System
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
/* Quit */
|
||||
// Quit.
|
||||
defined( 'ABSPATH' ) OR exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist system configuration
|
||||
* Statify Blacklist system configuration.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @version 1.4.0
|
||||
*/
|
||||
class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
|
||||
@ -24,7 +32,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
if ( function_exists( 'get_sites' ) ) {
|
||||
$sites = get_sites();
|
||||
} elseif ( function_exists( 'wp_get_sites' ) ) {
|
||||
$sites = wp_get_sites(); /* legacy support for WP < 4.6 */
|
||||
$sites = wp_get_sites(); // legacy support for WP < 4.6.
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@ -59,7 +67,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
if ( function_exists( 'get_sites' ) ) {
|
||||
$sites = get_sites();
|
||||
} elseif ( function_exists( 'wp_get_sites' ) ) {
|
||||
$sites = wp_get_sites(); /* legacy support for WP < 4.6 */
|
||||
$sites = wp_get_sites(); // legacy support for WP < 4.6.
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@ -83,9 +91,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
*/
|
||||
public static function upgrade() {
|
||||
self::update_options();
|
||||
/* Check if config array is not associative (pre 1.2.0) */
|
||||
// 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 */
|
||||
// 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' ) ) ) ) {
|
||||
@ -95,9 +103,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
}
|
||||
}
|
||||
|
||||
/* Version not set (pre 1.3.0) or older than 1.4 */
|
||||
// Version not set (pre 1.3.0) or older than 1.4.
|
||||
if ( ! isset( self::$_options['version'] ) || self::$_options['version'] < 1.4 ) {
|
||||
/* Upgrade options to new schema */
|
||||
// Upgrade options to new schema.
|
||||
$options = array(
|
||||
'referer' => array(
|
||||
'active' => self::$_options['active_referer'],
|
||||
@ -125,9 +133,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
|
||||
self::update_options();
|
||||
}
|
||||
|
||||
/* Version older than current major release */
|
||||
// Version older than current major release.
|
||||
if ( self::$_options['version'] < self::VERSION_MAIN ) {
|
||||
/* 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['version'] = self::VERSION_MAIN;
|
||||
if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) {
|
||||
|
Reference in New Issue
Block a user