From 8c556ca5093acc0750ed685380ebebc26b1b428b Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 21 Mar 2018 18:16:25 +0100 Subject: [PATCH 01/42] Removed legacy code for WP prior to 4.6 Statify itself requires WP 4.7, so no reason to stick to legacy code anymore. --- README.md | 7 +++++-- composer.json | 2 +- inc/class-statifyblacklist-system.php | 3 --- phpcs.xml | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index bceb1bb..f069d7d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Statify Blacklist # * Contributors: Stefan Kalscheuer -* Requires at least: 4.4 +* Requires at least: 4.7 * Tested up to: 4.9 * Requires PHP: 5.5 * Stable tag: 1.4.3 @@ -46,7 +46,7 @@ The plugin is capable of handling multisite installations. ### Requirements ### * PHP 5.5 or above -* WordPress 4.4 or above +* WordPress 4.7 or above * Statify plugin installed and activated (1.5.0 or above) ## Frequently Asked Questions ## @@ -82,6 +82,9 @@ Because of this, an IP blacklist can only be applied while processing the reques ## Changelog ## +### 1.5.0 / unreleased ### +* Minimum required WordPress version is 4.7 + ### 1.4.3 / 09.01.2018 ### * Fix issues with multisite installation (#11) diff --git a/composer.json b/composer.json index be3a5d0..dc35535 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "stklcode/statify-blacklist", - "version": "1.4.3", + "version": "1.5.0-alpha", "description": "A blacklist extension for the famous Statify WordPress plugin", "keywords": [ "wordpress", diff --git a/inc/class-statifyblacklist-system.php b/inc/class-statifyblacklist-system.php index a9160b1..66b822f 100644 --- a/inc/class-statifyblacklist-system.php +++ b/inc/class-statifyblacklist-system.php @@ -31,9 +31,6 @@ public static function install( $network_wide = false ) { if ( $network_wide && is_multisite() ) { if ( function_exists( 'get_sites' ) ) { $sites = get_sites(); - } elseif ( function_exists( 'wp_get_sites' ) ) { - // @codingStandardsIgnoreLine Legacy support for WP < 4.6. - $sites = wp_get_sites(); } else { return; } diff --git a/phpcs.xml b/phpcs.xml index a40b6c5..ff901cc 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -10,7 +10,7 @@ statify-blacklist.php - + From 5e2dd4b6e19c084e593567b9290bb33889fdcc4e Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 21 Mar 2018 18:27:42 +0100 Subject: [PATCH 02/42] Removed Gulp build script Build process is now handled by Composer and Robo, so clean up old stuff --- Gulpfile.js | 65 ---------------------------------------------------- package.json | 14 ++--------- 2 files changed, 2 insertions(+), 77 deletions(-) delete mode 100644 Gulpfile.js diff --git a/Gulpfile.js b/Gulpfile.js deleted file mode 100644 index 2fe2690..0000000 --- a/Gulpfile.js +++ /dev/null @@ -1,65 +0,0 @@ -var gulp = require('gulp'); -var clean = require('gulp-clean'); -var copy = require('gulp-copy'); -var zip = require('gulp-zip'); -var composer = require('gulp-composer'); -var phpunit = require('gulp-phpunit'); -var exec = require('child_process').exec; -var phpcs = require('gulp-phpcs'); -var config = require('./package.json'); - -// Clean the target directory. -gulp.task('clean', function () { - console.log('Cleaning up target directory ...'); - return gulp.src('dist', {read: false}) - .pipe(clean()); -}); - -// Prepare composer. -gulp.task('compose', function () { - console.log('Preparing Composer ...'); - return composer('install'); -}); - -// Execute unit tests. -gulp.task('test', ['compose'], function () { - console.log('Running PHPUnit tests ...'); - return gulp.src('phpunit.xml') - .pipe(phpunit('./vendor/bin/phpunit', {debug: false})); -}); - -// Execute PHP Code Sniffer. -gulp.task('test-cs', function (cb) { - return exec('./vendor/bin/phpcs --config-set installed_paths vendor/wimg/php-compatibility,vendor/wp-coding-standards/wpcs', function (err, stdout, stderr) { - console.log(stdout); - console.log(stderr); - if (null === err) { - console.log('Running PHP Code Sniffer tests ...'); - // exec('./vendor/bin/phpcs --standard=phpcs.xml', function(err, stdout, stderr) { - // console.log(stdout); - // console.log(stderr); - // }); - gulp.src(['statify-blacklist.php', 'inc/**/*.php']) - .pipe(phpcs({bin: './vendor/bin/phpcs', standard: 'phpcs.xml'})) - .pipe(phpcs.reporter('log')); - } - cb(err); - }); -}); - -// Bundle files as required for plugin distribution.. -gulp.task('bundle', ['clean'], function () { - console.log('Collecting files for package dist/' + config.name + config.version + ' ...'); - return gulp.src(['**/*.php', '!RoboFile.php', '!test/**', '!vendor/**', 'README.md', 'LICENSE.md'], {base: './'}) - .pipe(copy('./dist/' + config.name + '.' + config.version + '/' + config.name)); -}); - -// Create a ZIP package of the relevant files for plugin distribution. -gulp.task('package', ['bundle'], function () { - console.log('Building package dist/' + config.name + config.version + '.zip ...'); - return gulp.src('./dist/' + config.name + '.' + config.version + '/**') - .pipe(zip(config.name + '.' + config.version + '.zip')) - .pipe(gulp.dest('./dist')); -}); - -gulp.task('default', ['clean', 'compose', 'test', 'test-cs', 'bundle', 'package']); diff --git a/package.json b/package.json index ebda245..cf9bc2f 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,7 @@ { "name": "statify-blacklist", - "version": "1.4.3", + "version": "1.5.0-alpha", "description": "A blacklist extension for the famous Statify WordPress plugin", "author": "Stefan Kalscheuer", - "license": "GPL-2.0+", - "devDependencies": { - "gulp": "^3.9.1", - "gulp-clean": "^0.3.2", - "gulp-copy": "^1.0.1", - "gulp-zip": "^4.0.0", - "gulp-composer": "^0.4.4", - "gulp-phpunit": "^0.24.1", - "gulp-phpcs": "^2.1.0", - "child_process": "^1.0.2" - } + "license": "GPL-2.0+" } From c77e1ee01235c4bef4a9f933f3a8ba5fe6cb5751 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 19 May 2018 13:44:39 +0200 Subject: [PATCH 03/42] Apply coding standards to unit test (except some mocks) --- test/statifyblacklist-test.php | 110 +++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index aa31aa9..523feb5 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -20,17 +20,17 @@ /** * The StatifyBlacklist base class. */ -require_once( 'inc/class-statifyblacklist.php' ); +require_once __DIR__ . '/../inc/class-statifyblacklist.php'; /** * The StatifyBlacklist system class. */ -require_once( 'inc/class-statifyblacklist-system.php' ); +require_once __DIR__ . '/../inc/class-statifyblacklist-system.php'; /** * The StatifyBlacklist admin class. */ -require_once( 'inc/class-statifyblacklist-admin.php' ); +require_once __DIR__ . '/../inc/class-statifyblacklist-admin.php'; /** * Class StatifyBlacklistTest. @@ -43,6 +43,8 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase { /** * Test simple referer filter. + * + * @return void */ public function test_referer_filter() { // Prepare Options: 2 blacklisted domains, disabled. @@ -103,8 +105,10 @@ public function test_referer_filter() { /** * Test referer filter using regular expressions. + * + * @return void */ - public function testRefererRegexFilter() { + public function test_referer_regex_filter() { // Prepare Options: 2 regular expressions. StatifyBlacklist::$_options = array( 'referer' => array( @@ -158,8 +162,10 @@ public function testRefererRegexFilter() { /** * Test the upgrade methodology for configuration options. + * + * @return void */ - public function testUpgrade() { + public function test_upgrade() { // Create configuration of version 1.3. $options13 = array( 'active_referer' => 1, @@ -179,41 +185,43 @@ public function testUpgrade() { StatifyBlacklist_System::upgrade(); // Retrieve updated options. - $optionsUpdated = get_option( 'statify-blacklist' ); + $options_updated = get_option( 'statify-blacklist' ); // Verify size against default options (no junk left). - $this->assertEquals( 4, count( $optionsUpdated ) ); - $this->assertEquals( 4, count( $optionsUpdated['referer'] ) ); - $this->assertEquals( 4, count( $optionsUpdated['target'] ) ); - $this->assertEquals( 2, count( $optionsUpdated['ip'] ) ); + $this->assertEquals( 4, count( $options_updated ) ); + $this->assertEquals( 4, count( $options_updated['referer'] ) ); + $this->assertEquals( 4, count( $options_updated['target'] ) ); + $this->assertEquals( 2, count( $options_updated['ip'] ) ); // Verify that original attributes are unchanged. - $this->assertEquals( $options13['active_referer'], $optionsUpdated['referer']['active'] ); - $this->assertEquals( $options13['cron_referer'], $optionsUpdated['referer']['cron'] ); - $this->assertEquals( $options13['referer'], $optionsUpdated['referer']['blacklist'] ); - $this->assertEquals( $options13['referer_regexp'], $optionsUpdated['referer']['regexp'] ); + $this->assertEquals( $options13['active_referer'], $options_updated['referer']['active'] ); + $this->assertEquals( $options13['cron_referer'], $options_updated['referer']['cron'] ); + $this->assertEquals( $options13['referer'], $options_updated['referer']['blacklist'] ); + $this->assertEquals( $options13['referer_regexp'], $options_updated['referer']['regexp'] ); // Verify that new attributes are present in config and filled with default values (disabled, empty). - $this->assertEquals( 0, $optionsUpdated['target']['active'] ); - $this->assertEquals( 0, $optionsUpdated['target']['cron'] ); - $this->assertEquals( 0, $optionsUpdated['target']['regexp'] ); - $this->assertEquals( array(), $optionsUpdated['target']['blacklist'] ); - $this->assertEquals( 0, $optionsUpdated['ip']['active'] ); - $this->assertEquals( array(), $optionsUpdated['ip']['blacklist'] ); + $this->assertEquals( 0, $options_updated['target']['active'] ); + $this->assertEquals( 0, $options_updated['target']['cron'] ); + $this->assertEquals( 0, $options_updated['target']['regexp'] ); + $this->assertEquals( array(), $options_updated['target']['blacklist'] ); + $this->assertEquals( 0, $options_updated['ip']['active'] ); + $this->assertEquals( array(), $options_updated['ip']['blacklist'] ); // Verify that version number has changed to current release. - $this->assertEquals( StatifyBlacklist::VERSION_MAIN, $optionsUpdated['version'] ); + $this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] ); } /** - * Test CIDR address matching for IP filter (#7) + * Test CIDR address matching for IP filter (#7). + * + * @return void */ - public function testCidrMatch() { + public function test_cidr_match() { // IPv4 tests. - $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1' ) ) ); - $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/32' ) ) ); + $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1' ) ) ); + $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/32' ) ) ); $this->assertFalse( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/33', @@ -221,7 +229,7 @@ public function testCidrMatch() { ) ); $this->assertFalse( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/-1', @@ -229,7 +237,7 @@ public function testCidrMatch() { ) ); $this->assertTrue( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '192.0.2.123', '192.0.2.0/24', @@ -237,7 +245,7 @@ public function testCidrMatch() { ) ); $this->assertFalse( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '192.0.3.123', '192.0.2.0/24', @@ -245,7 +253,7 @@ public function testCidrMatch() { ) ); $this->assertTrue( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '192.0.2.123', '192.0.2.120/29', @@ -253,16 +261,16 @@ public function testCidrMatch() { ) ); $this->assertFalse( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '192.0.2.128', '192.0.2.120/29', ) ) ); - $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.13', '10.0.0.0/8' ) ) ); + $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.13', '10.0.0.0/8' ) ) ); $this->assertFalse( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.345', '10.0.0.0/8', @@ -271,12 +279,12 @@ public function testCidrMatch() { ); // IPv6 tests. - $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1' ) ) ); - $this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/128' ) ) ); - $this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/129' ) ) ); - $this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/-1' ) ) ); + $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1' ) ) ); + $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/128' ) ) ); + $this->assertFalse( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/129' ) ) ); + $this->assertFalse( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/-1' ) ) ); $this->assertTrue( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '2001:db8:a0b:12f0:1:2:3:4', '2001:db8:a0b:12f0::1/64 ', @@ -284,7 +292,7 @@ public function testCidrMatch() { ) ); $this->assertTrue( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '2001:db8:a0b:12f0::123:456', '2001:db8:a0b:12f0::1/96 ', @@ -292,7 +300,7 @@ public function testCidrMatch() { ) ); $this->assertFalse( - invokeStatic( + invoke_static( StatifyBlacklist::class, 'cidr_match', array( '2001:db8:a0b:12f0::1:132:465', '2001:db8:a0b:12f0::1/96 ', @@ -302,13 +310,15 @@ public function testCidrMatch() { } /** - * Test sanitization of IP addresses + * Test sanitization of IP addresses. + * + * @return void */ - public function testSanitizeIPs() { + public function test_sanitize_ips() { // IPv4 tests. $valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' ); $invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' ); - $result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); + $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); $this->assertInternalType( 'array', $result ); $this->assertEquals( $valid, $result ); @@ -327,7 +337,7 @@ public function testSanitizeIPs() { '2001:db8:a0b:12f0::/129', '1:2:3:4:5:6:7:8:9', ); - $result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); + $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); $this->assertInternalType( 'array', $result ); $this->assertEquals( $valid, $result ); @@ -335,8 +345,10 @@ public function testSanitizeIPs() { /** * Test IP filter (#7). + * + * @return void */ - public function testIPFilter() { + public function test_ip_filter() { // Prepare Options: 2 blacklisted IPs, disabled. StatifyBlacklist::$_options = array( 'referer' => array( @@ -405,8 +417,10 @@ public function testIPFilter() { /** * Test simple target filter. + * + * @return void */ - public function testTargetFilter() { + public function test_target_filter() { // Prepare Options: 2 blacklisted domains, disabled. StatifyBlacklist::$_options = array( 'referer' => array( @@ -476,9 +490,9 @@ public function testTargetFilter() { /** @ignore */ -function invokeStatic( $class, $methodName, $parameters = array() ) { +function invoke_static( $class, $method_name, $parameters = array() ) { $reflection = new \ReflectionClass( $class ); - $method = $reflection->getMethod( $methodName ); + $method = $reflection->getMethod( $method_name ); $method->setAccessible( true ); return $method->invokeArgs( null, $parameters ); From f34b76194232e81ec90b7004efd24c9b77b65e1a Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 19 May 2018 15:09:57 +0200 Subject: [PATCH 04/42] Removed load_plugin_textdomain and domain path header Translation is handled via translate.wordpress.org and minimum required version is greater than 4.6, so the local translation artifacts are dropped. --- README.md | 1 + inc/class-statifyblacklist.php | 3 --- statify-blacklist.php | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index fe7eece..c22f181 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ Because of this, an IP blacklist can only be applied while processing the reques ### 1.5.0 / unreleased ### * Minimum required WordPress version is 4.7 +* Removed `load_plugin_textdomain()` and `Domain Path` header ### 1.4.4 / 19.05.2018 ### * Fix live filter chain when regular expressions are active (#12) diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index 3ebea16..b56b1a1 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -86,9 +86,6 @@ public static function init() { // 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_System', 'install_site' ) ); add_action( 'delete_blog', array( 'StatifyBlacklist_System', 'uninstall_site' ) ); diff --git a/statify-blacklist.php b/statify-blacklist.php index 7588846..af33b65 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -14,7 +14,6 @@ * Author: Stefan Kalscheuer (@stklcode) * Author URI: https://www.stklcode.de * Text Domain: statify-blacklist - * Domain Path: /lang * License: GPLv2 or later * * Statify Blacklist is free software: you can redistribute it and/or modify From bcd42bde2a34f1ba548579a532bc297f1d3e471c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 20 Oct 2018 10:12:38 +0200 Subject: [PATCH 05/42] Minor code style fixes --- composer.json | 8 ++++---- inc/class-statifyblacklist-admin.php | 23 ++++++++++++++++++----- inc/class-statifyblacklist-system.php | 2 +- inc/class-statifyblacklist.php | 3 ++- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index dc35535..36727f2 100644 --- a/composer.json +++ b/composer.json @@ -23,13 +23,13 @@ }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.4", - "consolidation/robo": "^1.0.0", + "consolidation/robo": "^1.3", "phpunit/phpunit": "*", "phpunit/php-code-coverage": "*", "slowprog/composer-copy-file": "~0.2", - "squizlabs/php_codesniffer": "^3.1", - "wimg/php-compatibility": "^8.0", - "wp-coding-standards/wpcs": "~0.14" + "squizlabs/php_codesniffer": "^3.3", + "wimg/php-compatibility": "^9.0", + "wp-coding-standards/wpcs": "^1.1" }, "scripts": { "build": [ diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index e807ef9..b391de3 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -74,14 +74,24 @@ public static 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( + '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( + 'options-general.php', + $title, + $title, + 'manage_options', + 'statify-blacklist', + array( 'StatifyBlacklist_Admin', 'settings_page', ) @@ -251,12 +261,15 @@ function ( $r ) { */ private static function sanitizeIPs( $ips ) { return array_filter( - $ips, function ( $ip ) { + $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 + '/^((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 + '/^(([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 ); } ); diff --git a/inc/class-statifyblacklist-system.php b/inc/class-statifyblacklist-system.php index 66b822f..8d65df5 100644 --- a/inc/class-statifyblacklist-system.php +++ b/inc/class-statifyblacklist-system.php @@ -1,6 +1,6 @@ Date: Sat, 27 Oct 2018 17:44:13 +0200 Subject: [PATCH 06/42] Merge if-clauses for cron job detection --- inc/class-statifyblacklist.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index 0d7efbc..428991d 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -109,10 +109,9 @@ public static function init() { } // CronJob to clean up database. - if ( defined( 'DOING_CRON' ) && DOING_CRON ) { - if ( 1 === self::$_options['referer']['cron'] || 1 === self::$_options['target']['cron'] ) { - add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) ); - } + if ( defined( 'DOING_CRON' ) && DOING_CRON && + ( 1 === self::$_options['referer']['cron'] || 1 === self::$_options['target']['cron'] ) ) { + add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) ); } } From 8e6cb5c553017a97eec23895960fe50d055072a7 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 27 Oct 2018 18:33:00 +0200 Subject: [PATCH 07/42] Added compatibility check for WP and PHP version (closes #17) --- README.md | 1 + statify-blacklist.php | 72 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 37a82fa..3479cab 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Because of this, an IP blacklist can only be applied while processing the reques ### 1.5.0 / unreleased ### * Minimum required WordPress version is 4.7 * Removed `load_plugin_textdomain()` and `Domain Path` header +* Added automatic compatibility check for WP and PHP version (#17) ### 1.4.4 / 19.05.2018 ### * Fix live filter chain when regular expressions are active (#12) diff --git a/statify-blacklist.php b/statify-blacklist.php index 3420de1..892a44b 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -38,18 +38,24 @@ define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) ); define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) ); -// System Hooks. -add_action( 'plugins_loaded', array( 'StatifyBlacklist', 'init' ) ); +// Check for compatibility. +if ( statify_blacklist_compatibility_check() ) { + // System Hooks. + add_action( 'plugins_loaded', array( 'StatifyBlacklist', 'init' ) ); -register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'install' ) ); + register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'install' ) ); -register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) ); + register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) ); -// Upgrade hook. -register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) ); + // Upgrade hook. + register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) ); -// Autoload. -spl_autoload_register( 'statify_blacklist_autoload' ); + // Autoload. + spl_autoload_register( 'statify_blacklist_autoload' ); +} else { + // Disbale plugin, if active. + add_action( 'admin_init', 'statify_blacklist_disable' ); +} /** * Autoloader for StatifyBlacklist classes. @@ -73,3 +79,53 @@ function statify_blacklist_autoload( $class ) { ); } } + +/** + * Check for compatibility with PHP and WP version. + * + * @since 1.5.0 + * + * @return boolean Whether minimum WP and PHP versions are met. + */ +function statify_blacklist_compatibility_check() { + return version_compare( $GLOBALS['wp_version'], '4.7', '>=' ) && + version_compare( phpversion(), '5.5', '>=' ); +} + +/** + * Disable plugin if active and incompatible. + * + * @return void + */ +function statify_blacklist_disable() { + if ( is_plugin_active( STATIFYBLACKLIST_BASE ) ) { + deactivate_plugins( STATIFYBLACKLIST_BASE ); + add_action( 'admin_notices', 'statify_blacklist_disabled_notice' ); + if ( isset( $_GET['activate'] ) ) { + unset( $_GET['activate'] ); + } + } +} + +/** + * Admin notification for unmet requirements. + * + * @return void + */ +function statify_blacklist_disabled_notice() { + echo '

'; + printf( + /* translators: minimum version numbers for WordPress and PHP inserted at placeholders */ + esc_html__( 'Statify Blacklist requires at least WordPress %1$s and PHP %2$s.', 'my-plugin' ), + '4.7', + '5.5' + ); + echo '
'; + printf( + /* translators: current version numbers for WordPress and PHP inserted at placeholders */ + esc_html__( 'Your site is running WordPress %1$s on PHP %2$s, thus the plugin has been disabled.', 'my-plugin' ), + esc_html( $GLOBALS['wp_version'] ), + esc_html( phpversion() ) + ); + echo '

'; +} From 74826384a8fd69fd8122cb07c6c88734548d23a2 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 27 Oct 2018 18:34:23 +0200 Subject: [PATCH 08/42] Moved admin initialization to admin class --- inc/class-statifyblacklist-admin.php | 31 ++++++++++++++++++++++++++++ inc/class-statifyblacklist.php | 21 +------------------ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index b391de3..319a462 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -18,6 +18,37 @@ * @since 1.0.0 */ class StatifyBlacklist_Admin extends StatifyBlacklist { + + /** + * Initialize admin-only components of the plugin. + * + * @since 1.5.0 + * + * @return void + */ + public static function init() { + // Add actions. + add_action( 'wpmu_new_blog', array( 'StatifyBlacklist_System', 'install_site' ) ); + add_action( 'delete_blog', array( 'StatifyBlacklist_System', 'uninstall_site' ) ); + add_filter( 'plugin_row_meta', array( 'StatifyBlacklist_Admin', 'plugin_meta_link' ), 10, 2 ); + + if ( self::$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 + ); + } else { + add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', 'add_menu_page' ) ); + add_filter( 'plugin_action_links', array( 'StatifyBlacklist_Admin', 'plugin_actions_links' ), 10, 2 ); + } + } + /** * Update options. * diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index 428991d..d630e25 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -86,26 +86,7 @@ public static function init() { // Admin only filters. if ( is_admin() ) { - // Add actions. - add_action( 'wpmu_new_blog', array( 'StatifyBlacklist_System', 'install_site' ) ); - add_action( 'delete_blog', array( 'StatifyBlacklist_System', 'uninstall_site' ) ); - add_filter( 'plugin_row_meta', array( 'StatifyBlacklist_Admin', 'plugin_meta_link' ), 10, 2 ); - - if ( self::$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 - ); - } else { - add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', 'add_menu_page' ) ); - add_filter( 'plugin_action_links', array( 'StatifyBlacklist_Admin', 'plugin_actions_links' ), 10, 2 ); - } + StatifyBlacklist_Admin::init(); } // CronJob to clean up database. From 74f2e0f9a71ad68ac718070bb4dbcddc59af368c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 27 Oct 2018 18:41:09 +0200 Subject: [PATCH 09/42] Removed deprecated instance() and __construct() methods from base class --- inc/class-statifyblacklist-admin.php | 6 ++++-- inc/class-statifyblacklist-system.php | 13 ++++++++++-- inc/class-statifyblacklist.php | 29 +++++++-------------------- statify-blacklist.php | 12 ++++++++--- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 319a462..a148228 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -9,8 +9,10 @@ * @since 1.0.0 */ -// Quit. -defined( 'ABSPATH' ) || exit; +// Quit if accessed directly. +if ( ! defined( 'ABSPATH' ) ) { + exit; +} /** * Statify Blacklist admin configuration. diff --git a/inc/class-statifyblacklist-system.php b/inc/class-statifyblacklist-system.php index 8d65df5..c136d29 100644 --- a/inc/class-statifyblacklist-system.php +++ b/inc/class-statifyblacklist-system.php @@ -9,8 +9,10 @@ * @since 1.0.0 */ -// Quit. -defined( 'ABSPATH' ) || exit; +// Quit if accessed directly. +if ( ! defined( 'ABSPATH' ) ) { + exit; +} /** * Statify Blacklist system configuration. @@ -25,6 +27,7 @@ class StatifyBlacklist_System extends StatifyBlacklist { * @since 1.0.0 * * @param bool $network_wide Whether the plugin was activated network-wide or not. + * @return void */ public static function install( $network_wide = false ) { // Create tables for each site in a network. @@ -59,6 +62,7 @@ public static function install( $network_wide = false ) { * @since 1.4.3 * * @param integer $site_id Site ID. + * @return void */ public static function install_site( $site_id ) { switch_to_blog( (int) $site_id ); @@ -74,6 +78,8 @@ public static function install_site( $site_id ) { * Plugin uninstall handler. * * @since 1.0.0 + * + * @return void */ public static function uninstall() { if ( is_multisite() ) { @@ -109,6 +115,7 @@ public static function uninstall() { * @since 1.4.3 * * @param integer $site_id Site ID. + * @return void */ public static function uninstall_site( $site_id ) { $old = get_current_blog_id(); @@ -121,6 +128,8 @@ public static function uninstall_site( $site_id ) { * Upgrade plugin options. * * @since 1.2.0 + * + * @return void */ public static function upgrade() { self::update_options(); diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index d630e25..faf8df3 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -8,8 +8,10 @@ * @since 1.0.0 */ -// Quit. -defined( 'ABSPATH' ) || exit; +// Quit if accessed directly. +if ( ! defined( 'ABSPATH' ) ) { + exit; +} /** * Statify Blacklist. @@ -42,30 +44,12 @@ class StatifyBlacklist { */ public static $multisite; - /** - * Class self initialize. - * - * @since 1.0.0 - * @deprecated 1.4.2 Replaced by init(). - */ - public static function instance() { - self::init(); - } - - /** - * Class constructor. - * - * @since 1.0.0 - * @deprecated 1.4.2 Replaced by init(). - */ - public function __construct() { - self::init(); - } - /** * Plugin initialization. * * @since 1.4.2 + * + * @return void */ public static function init() { // Skip on autosave or AJAX. @@ -103,6 +87,7 @@ public static function init() { * @since 1.2.1 update_options($options = null) Parameter with default value introduced. * * @param array $options Optional. New options to save. + * @return void */ public static function update_options( $options = null ) { if ( self::$multisite ) { diff --git a/statify-blacklist.php b/statify-blacklist.php index 892a44b..429754a 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -10,7 +10,7 @@ * Plugin Name: Statify Blacklist * Plugin URI: https://wordpress.org/plugins/statify-blacklist/ * Description: Extension for the Statify plugin to add a customizable blacklists. - * Version: 1.4.4 + * Version: 1.5.0-alpha * Author: Stefan Kalscheuer (@stklcode) * Author URI: https://www.stklcode.de * Text Domain: statify-blacklist @@ -30,8 +30,10 @@ * along with Statify Blacklist. If not, see http://www.gnu.org/licenses/gpl-2.0.html. */ -// Quit. -defined( 'ABSPATH' ) || exit; +// Quit if accessed directly. +if ( ! defined( 'ABSPATH' ) ) { + exit; +} // Constants. define( 'STATIFYBLACKLIST_FILE', __FILE__ ); @@ -95,6 +97,8 @@ function statify_blacklist_compatibility_check() { /** * Disable plugin if active and incompatible. * + * @since 1.5.0 + * * @return void */ function statify_blacklist_disable() { @@ -110,6 +114,8 @@ function statify_blacklist_disable() { /** * Admin notification for unmet requirements. * + * @since 1.5.0 + * * @return void */ function statify_blacklist_disabled_notice() { From 35b6d5592b75f4a8f16292ee6a88dbf81ce0efcb Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 27 Oct 2018 19:05:09 +0200 Subject: [PATCH 10/42] Cleaned up labels and added PHPCS rule for settings view --- phpcs.xml | 3 +- views/settings-page.php | 140 ++++++++++++++++++++-------------------- 2 files changed, 73 insertions(+), 70 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index ff901cc..b317a5b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -6,8 +6,9 @@ - inc statify-blacklist.php + inc + views diff --git a/views/settings-page.php b/views/settings-page.php index 2213995..e2a210b 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -19,7 +19,7 @@ // Check user capabilities. if ( ! current_user_can( 'manage_options' ) ) { - die( __( 'Are you sure you want to do this?' ) ); + die( esc_html__( 'Are you sure you want to do this?' ) ); } if ( ! empty( $_POST['cleanUp'] ) ) { @@ -27,24 +27,27 @@ StatifyBlacklist_Admin::cleanup_database(); } else { // Extract referer array. - if ( empty( trim( $_POST['statifyblacklist']['referer']['blacklist'] ) ) ) { + $referer_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['referer']['blacklist'] ) ); + if ( empty( trim( $referer_str ) ) ) { $referer = array(); } else { - $referer = explode( "\r\n", $_POST['statifyblacklist']['referer']['blacklist'] ); + $referer = explode( "\r\n", $referer_str ); } // Extract target array. - if ( empty( trim( $_POST['statifyblacklist']['target']['blacklist'] ) ) ) { + $target_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['target']['blacklist'] ) ); + if ( empty( trim( $target_str ) ) ) { $target = array(); } else { - $target = explode( "\r\n", str_replace( '\\\\', '\\', $_POST['statifyblacklist']['target']['blacklist'] ) ); + $target = explode( "\r\n", str_replace( '\\\\', '\\', $target_str ) ); } // Extract IP array. - if ( empty( trim( $_POST['statifyblacklist']['ip']['blacklist'] ) ) ) { + $ip_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ip']['blacklist'] ) ); + if ( empty( trim( $ip_str ) ) ) { $ip = array(); } else { - $ip = explode( "\r\n", $_POST['statifyblacklist']['ip']['blacklist'] ); + $ip = explode( "\r\n", $ip_str ); } // Update options (data will be sanitized). @@ -90,10 +93,17 @@ } } // End if(). } // End if(). + +/* + * Disable some code style rules that are impractical for textarea content: + * + * phpcs:disable Squiz.PHP.EmbeddedPhp.ContentBeforeOpen + * phpcs:disable Squiz.PHP.EmbeddedPhp.ContentAfterEnd + */ ?>
-

+

'; @@ -103,7 +113,7 @@ if ( isset( $statifyblacklist_post_warning ) ) { print '

' . esc_html( $statifyblacklist_post_warning ); - print '
'; + print '
'; esc_html_e( 'Settings have not been saved yet.', 'statify-blacklist' ); print '

'; } @@ -120,26 +130,24 @@
  • + ()
  • + ()
  • +
    + ()
  • @@ -182,26 +184,24 @@
  • + ()
  • + ()
  • +
    + + ( + + ) +
  • @@ -244,8 +244,8 @@
    • @@ -257,21 +257,21 @@
    • +
      + + ( + 127.0.0.1, 192.168.123.0/24, 2001:db8:a0b:12f0::1/64 + ) +
    @@ -279,14 +279,16 @@

    - -


    - -
    - - + +
    + +
    + + + +

    From 7e51c7d63eebfc901e6dc2dd1384457a14242c83 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 27 Oct 2018 19:09:34 +0200 Subject: [PATCH 11/42] Add PHP 7.3 to CI matrix --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index a1f594f..169a412 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ php: - '7.0' - '7.1' - '7.2' + - '7.3' before_script: - composer install script: From f60c6ec2fffe0501c2c4da6e01eb33b8b9544a26 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 30 Oct 2018 19:32:52 +0100 Subject: [PATCH 12/42] Fixed textdomain for compatibility notice --- statify-blacklist.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/statify-blacklist.php b/statify-blacklist.php index 429754a..02a5c39 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -122,14 +122,14 @@ function statify_blacklist_disabled_notice() { echo '

    '; printf( /* translators: minimum version numbers for WordPress and PHP inserted at placeholders */ - esc_html__( 'Statify Blacklist requires at least WordPress %1$s and PHP %2$s.', 'my-plugin' ), + esc_html__( 'Statify Blacklist requires at least WordPress %1$s and PHP %2$s.', 'statify-blacklist' ), '4.7', '5.5' ); echo '
    '; printf( /* translators: current version numbers for WordPress and PHP inserted at placeholders */ - esc_html__( 'Your site is running WordPress %1$s on PHP %2$s, thus the plugin has been disabled.', 'my-plugin' ), + esc_html__( 'Your site is running WordPress %1$s on PHP %2$s, thus the plugin has been disabled.', 'statify-blacklist' ), esc_html( $GLOBALS['wp_version'] ), esc_html( phpversion() ) ); From 36a65482e26c4c24d9565f271fef33fc15454103 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Thu, 1 Nov 2018 20:28:55 +0100 Subject: [PATCH 13/42] Add badges to ReadMe [skip ci] --- README.md | 5 +++++ RoboFile.php | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3479cab..4b72d1b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +[![Build Status](https://travis-ci.org/stklcode/statify-blacklist.svg?branch=master)](https://travis-ci.org/stklcode/statify-blacklist) +[![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=de.stklcode.web.wordpress.plugins%3Astatify-blacklist&metric=alert_status)](https://sonarcloud.io/dashboard?id=de.stklcode.web.wordpress.plugins%3Astatify-blacklist) +[![Packagist Version](https://img.shields.io/packagist/v/stklcode/statify-blacklist.svg)](https://packagist.org/packages/pluginkollektiv/statify) +[![License](https://img.shields.io/badge/license-GPL%20v2-blue.svg)](https://github.com/stklcode/statify-blacklist/blob/master/LICENSE.txt) + # Statify Blacklist # * Contributors: Stefan Kalscheuer * Requires at least: 4.7 diff --git a/RoboFile.php b/RoboFile.php index e991634..b08f75b 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -139,8 +139,14 @@ private function bundle() { 'views' => $this->target_dir . '/' . $this->final_name . '/views', ] )->run(); $this->_copy( 'statify-blacklist.php', $this->target_dir . '/' . $this->final_name . '/statify-blacklist.php' ); - $this->_copy( 'README.md', $this->target_dir . '/' . $this->final_name . '/README.md' ); $this->_copy( 'LICENSE.md', $this->target_dir . '/' . $this->final_name . '/LICENSE.md' ); + $this->_copy( 'README.md', $this->target_dir . '/' . $this->final_name . '/README.md' ); + + // Remove content before title (e.g. badges) from README file. + $this->taskReplaceInFile( $this->target_dir . '/' . $this->final_name . '/README.md' ) + ->regex( '/^[^\\#]*/' ) + ->to( '' ) + ->run(); } /** From abcaab7a33f89bf15d53f0bb0eb89830105b06a9 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Thu, 1 Nov 2018 21:58:06 +0100 Subject: [PATCH 14/42] Fixed ReadMe badge links [skip ci] --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4b72d1b..c591dd1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ [![Build Status](https://travis-ci.org/stklcode/statify-blacklist.svg?branch=master)](https://travis-ci.org/stklcode/statify-blacklist) [![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=de.stklcode.web.wordpress.plugins%3Astatify-blacklist&metric=alert_status)](https://sonarcloud.io/dashboard?id=de.stklcode.web.wordpress.plugins%3Astatify-blacklist) -[![Packagist Version](https://img.shields.io/packagist/v/stklcode/statify-blacklist.svg)](https://packagist.org/packages/pluginkollektiv/statify) -[![License](https://img.shields.io/badge/license-GPL%20v2-blue.svg)](https://github.com/stklcode/statify-blacklist/blob/master/LICENSE.txt) +[![Packagist Version](https://img.shields.io/packagist/v/stklcode/statify-blacklist.svg)](https://packagist.org/packages/stklcode/statify-blacklist) +[![License](https://img.shields.io/badge/license-GPL%20v2-blue.svg)](https://github.com/stklcode/statify-blacklist/blob/master/LICENSE.md) # Statify Blacklist # * Contributors: Stefan Kalscheuer From 4ce3a8f336780fd9d10b08c6797f7496a8ff5547 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 2 Nov 2018 16:05:15 +0100 Subject: [PATCH 15/42] Add Slack notification to Travis --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 169a412..b6c3e11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,6 @@ before_script: - composer install script: - composer test-all +notifications: + slack: + secure: "ScXTSMO65veI1jA6TBHGDUtvDqEMkqJykaNf7vLLbb7YIxPIHHNBiX/wcjOHVFfQXZCV3qxQrflB7Lbm9qVUsAv861jTO9x/ZkECl5QhRoc0DIznejwZoypx0HJ9tBZFYT6qNUkViXRKZ/ILAiBLU9Yw52WACtQB9hu3FNFZwmKsjipvV8Sne1qEyTkLYLaMphsbC5mtXYdKMHvdt39jsYsk91UWGeYbXQ37LkMbsaG/8YHXF724d5JO7BRGoThw6p5knKAO5fk29V7GfNqg2h+hnGyNIUOcmxujgMDMFLyFCGMZpPoBa+3jyWWgq4PgpQt0F5VZtJFGoXCGcoMQm5IbVfqkSKJ4jYhqiSIrqSebLmzoPHepWX3yn8tpfOiBWjC6K9w9esp6vcZf26rnAJcjcGkA01rMrHRwR+UEMCLvj7q0DR0qzi/AFeED6gtpODzUf93Rp42Tz1iGvWIbgeCtkCWjfPO6XLuNiqGVPEVaT5BDKqlqbijdKxxp7yh1fdt8s0fInWdIsgoWTbU9DC1W4ZiqtQW7oYO+QtFZMaD6kZWpSqJUwB3kW5JL3odAUEm8bLbRWBvK5ZjGdaGqSbOs6f9gAKcf86iQQhwzCJSOgFlLlKFv9smicjPC+BGOxgx32pgseHNPWn6tmEo/ihmmr/NbbqoOusUKX9gQbA4=" From c511dcb517aa61399e5868949d90c593f4641432 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 6 Mar 2019 15:06:47 +0100 Subject: [PATCH 16/42] rework settings page The settings page now features the 2 column layout like generated by WP settings API. The regular expression selects are slightly rewritten in preparation of additional mathing methods. --- views/settings-page.php | 313 +++++++++++++++++++++++----------------- 1 file changed, 181 insertions(+), 132 deletions(-) diff --git a/views/settings-page.php b/views/settings-page.php index e2a210b..27c0d93 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -124,159 +124,208 @@ } ?>

    -
    -

    -
      -
    • + + +

      + + + + + + + + + + + + + + + + + + + + +
      - -
    • +
    • + > +

      + +

      +
      - () - -
    • -
    • + > +

      +
      + + + + +

      + -
      - - - () - -

    • - -
      - () -
    • - - + - +

      +
      + + + +

      + +

      +
      + +

      -
      -

      -
        -
      • + + + + + + + + + + + + + + + + + + + +
        - -
      • +
      • + > +

        + +

        +
        - () - -
      • +
      • + > +

        + +

        +
        - () - -
      • +
      • + + +

        + - +
        + - +
        + - +

        +
        -
        - - ( - - ) - - - - +
        + + +

        + ( /, /test/page/, /?page_id=123) +

        +
        -
        -

        -
          -
        • +

          + + + + + + + + + + + + +
          - -
        • - - ( - ) - -
        • -
        • - -
          - - ( - 127.0.0.1, 192.168.123.0/24, 2001:db8:a0b:12f0::1/64 - ) - -
        • - - +
          + > +

          + +
          + +

          +
          + : + + - +

          + + 127.0.0.1, 192.168.123.0/24, 2001:db8:a0b:12f0::1/64 +

          +

          @@ -285,10 +334,10 @@ value="" onclick="return confirm('Do you really want to apply filters to database? This cannot be undone.');">
          - +

          - +

    From b691f2c618ed86993c071918691b07e0f2879e14 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Wed, 6 Mar 2019 17:23:06 +0100 Subject: [PATCH 17/42] adjust sanitization of settings and warning messages --- inc/class-statifyblacklist-admin.php | 57 ++++++++++++----- views/settings-page.php | 95 ++++++++++++++++++---------- 2 files changed, 105 insertions(+), 47 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index a148228..5fe6832 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -20,6 +20,9 @@ * @since 1.0.0 */ class StatifyBlacklist_Admin extends StatifyBlacklist { + const MODE_NORMAL = 0; + const MODE_REGEX = 1; + const MODE_REGEX_CI = 2; /** * Initialize admin-only components of the plugin. @@ -61,27 +64,51 @@ public static function init() { */ public static function update_options( $options = null ) { if ( isset( $options ) && current_user_can( 'manage_options' ) ) { - // Sanitize URLs and remove empty inputs. + + // Sanitize referer list. $given_referer = $options['referer']['blacklist']; - if ( 0 === $options['referer']['regexp'] ) { + if ( self::MODE_NORMAL === $options['referer']['regexp'] ) { + // Sanitize URLs and remove empty inputs. $sanitized_referer = self::sanitizeURLs( $given_referer ); + } elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) { + // TODO Check regular expressions. + $sanitized_referer = $given_referer; } else { $sanitized_referer = $given_referer; } - // Sanitize IPs and Subnets and remove empty inputs. + // Sanitize target list. + $given_target = $options['target']['blacklist']; + if ( self::MODE_REGEX === $options['target']['regexp'] || self::MODE_REGEX_CI === $options['target']['regexp'] ) { + // TODO Check regular expressions. + $sanitized_target = $given_target; + } else { + $sanitized_target = $given_target; + } + + // Sanitize IPs and subnets and remove empty inputs. $given_ip = $options['ip']['blacklist']; $sanitized_ip = self::sanitizeIPs( $given_ip ); // Abort on errors. - if ( ! empty( array_diff( array_keys( $given_referer ), array_keys( $sanitized_referer ) ) ) ) { - return array( - 'referer' => $sanitized_referer, - ); - } elseif ( ! empty( array_diff( $given_ip, $sanitized_ip ) ) ) { - return array( - 'ip' => array_diff( $given_ip, $sanitized_ip ), - ); + $errors = [ + 'referer' => [ + 'sanitized' => $sanitized_referer, + 'diff' => array_diff( $given_referer, $sanitized_referer ), + ], + 'target' => [ + 'sanitized' => $sanitized_target, + 'diff' => array_diff( $given_target, $sanitized_target ), + ], + 'ip' => [ + 'sanitized' => $sanitized_ip, + 'diff' => array_diff( $given_ip, $sanitized_ip ), + ], + ]; + if ( ! empty( $errors['referer']['diff'] ) + || ! empty( $errors['target']['diff'] ) + || ! empty( $errors['ip']['diff'] ) ) { + return $errors; } // Update database on success. @@ -300,10 +327,10 @@ function ( $ip ) { '/^((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 - ); + 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 + ); } ); } diff --git a/views/settings-page.php b/views/settings-page.php index 27c0d93..42dcdb4 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -29,31 +29,61 @@ // Extract referer array. $referer_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['referer']['blacklist'] ) ); if ( empty( trim( $referer_str ) ) ) { - $referer = array(); + $referer = []; } else { - $referer = explode( "\r\n", $referer_str ); + $referer = array_filter( + array_map( + function ( $a ) { + return trim( $a ); + }, + explode( "\r\n", $referer_str ) + ), + function ( $a ) { + return ! empty( $a ); + } + ); } // Extract target array. $target_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['target']['blacklist'] ) ); if ( empty( trim( $target_str ) ) ) { - $target = array(); + $target = []; } else { - $target = explode( "\r\n", str_replace( '\\\\', '\\', $target_str ) ); + $target = array_filter( + array_map( + function ( $a ) { + return trim( $a ); + }, + explode( "\r\n", str_replace( '\\\\', '\\', $target_str ) ) + ), + function ( $a ) { + return ! empty( $a ); + } + ); } // Extract IP array. $ip_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ip']['blacklist'] ) ); if ( empty( trim( $ip_str ) ) ) { - $ip = array(); + $ip = []; } else { - $ip = explode( "\r\n", $ip_str ); + $ip = array_filter( + array_map( + function ( $a ) { + return trim( $a ); + }, + explode( "\r\n", $ip_str ) + ), + function ( $a ) { + return ! empty( $a ); + } + ); } // Update options (data will be sanitized). $statifyblacklist_update_result = StatifyBlacklist_Admin::update_options( - array( - 'referer' => array( + [ + 'referer' => [ 'active' => isset( $_POST['statifyblacklist']['referer']['active'] ) ? (int) $_POST['statifyblacklist']['referer']['active'] : 0, 'cron' => isset( $_POST['statifyblacklist']['referer']['cron'] ) @@ -61,8 +91,8 @@ 'regexp' => isset( $_POST['statifyblacklist']['referer']['regexp'] ) ? (int) $_POST['statifyblacklist']['referer']['regexp'] : 0, 'blacklist' => array_flip( $referer ), - ), - 'target' => array( + ], + 'target' => [ 'active' => isset( $_POST['statifyblacklist']['target']['active'] ) ? (int) $_POST['statifyblacklist']['target']['active'] : 0, 'cron' => isset( $_POST['statifyblacklist']['target']['cron'] ) @@ -70,23 +100,25 @@ 'regexp' => isset( $_POST['statifyblacklist']['target']['regexp'] ) ? (int) $_POST['statifyblacklist']['target']['regexp'] : 0, 'blacklist' => array_flip( $target ), - ), - 'ip' => array( + ], + 'ip' => [ 'active' => isset( $_POST['statifyblacklist']['ip']['active'] ) ? (int) $_POST['statifyblacklist']['ip']['active'] : 0, 'blacklist' => $ip, - ), + ], 'version' => StatifyBlacklist::VERSION_MAIN, - ) + ] ); // Generate messages. if ( false !== $statifyblacklist_update_result ) { - if ( array_key_exists( 'referer', $statifyblacklist_update_result ) ) { - $statifyblacklist_post_warning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' ); - } elseif ( array_key_exists( 'ip', $statifyblacklist_update_result ) ) { + $statifyblacklist_post_warning = []; + if ( ! empty( $statifyblacklist_update_result['referer']['diff'] ) ) { + $statifyblacklist_post_warning[] = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' ); + } + if ( ! empty( $statifyblacklist_update_result['ip']['diff'] ) ) { // translators: List of invalid IP addresses (comma separated). - $statifyblacklist_post_warning = sprintf( __( 'Some IPs are invalid : %s', 'statify-blacklist' ), implode( ', ', $statifyblacklist_update_result['ip'] ) ); + $statifyblacklist_post_warning[] = sprintf( __( 'Some IPs are invalid: %s', 'statify-blacklist' ), implode( ', ', $statifyblacklist_update_result['ip']['diff'] ) ); } } else { $statifyblacklist_post_success = __( 'Settings updated successfully.', 'statify-blacklist' ); @@ -111,11 +143,10 @@ print '

    '; } if ( isset( $statifyblacklist_post_warning ) ) { - print '

    ' . - esc_html( $statifyblacklist_post_warning ); - print '
    '; - esc_html_e( 'Settings have not been saved yet.', 'statify-blacklist' ); - print '

    '; + foreach ( $statifyblacklist_post_warning as $w ) { + print '

    ' . esc_html( $w ) . '

    '; + } + print '

    ' . esc_html( 'Settings have not been saved yet.', 'statify-blacklist' ) . '

    '; } if ( isset( $statifyblacklist_post_success ) ) { print '

    ' . @@ -187,10 +218,10 @@

    @@ -270,10 +301,10 @@ @@ -311,10 +342,10 @@ From 2eb08ce673590d4154cb61a8557d6c0915f28d2d Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 12 Mar 2019 17:32:32 +0100 Subject: [PATCH 18/42] update dev dependencies --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 36727f2..6477aec 100644 --- a/composer.json +++ b/composer.json @@ -23,13 +23,13 @@ }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.4", - "consolidation/robo": "^1.3", + "consolidation/robo": "^1.4", "phpunit/phpunit": "*", "phpunit/php-code-coverage": "*", - "slowprog/composer-copy-file": "~0.2", - "squizlabs/php_codesniffer": "^3.3", - "wimg/php-compatibility": "^9.0", - "wp-coding-standards/wpcs": "^1.1" + "slowprog/composer-copy-file": "~0.3", + "squizlabs/php_codesniffer": "^3.4", + "phpcompatibility/php-compatibility": "^9.1", + "wp-coding-standards/wpcs": "^2.0" }, "scripts": { "build": [ From 0822537f0ed4adb17e8747b6b5a9fce979be8e46 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 12 Mar 2019 17:57:26 +0100 Subject: [PATCH 19/42] adjustments for PHPUnit 8 Added result cache to .gitignore and replaced assertInternalType() by assertIsArray() with backwords compatibility for PHP 5 builds. --- .gitignore | 3 ++- phpunit.xml | 2 +- test/statifyblacklist-test.php | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 29ba421..283f98c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ composer.lock /dist/ .idea tests-clover.xml -tests-junit.xml \ No newline at end of file +tests-junit.xml +.phpunit.result.cache diff --git a/phpunit.xml b/phpunit.xml index 4ea43f9..f6cdc73 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,6 +12,6 @@ - + diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index bd4d062..29a7b43 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -320,7 +320,15 @@ public function test_sanitize_ips() { $invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' ); $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); - $this->assertInternalType( 'array', $result ); + /* + * Unfortunately this is nencessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated + * as of PHPUnit 8, but "assertIsArray" has been introduces in PHPUnit 7.5 which requires PHP >= 7.1. + */ + if ( method_exists( $this, 'assertIsArray' ) ) { + $this->assertIsArray( $result ); + } else { + $this->assertInternalType( 'array', $result ); + } $this->assertEquals( $valid, $result ); // IPv6 tests. @@ -339,7 +347,11 @@ public function test_sanitize_ips() { ); $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); - $this->assertInternalType( 'array', $result ); + if ( method_exists( $this, 'assertIsArray' ) ) { + $this->assertIsArray( $result ); + } else { + $this->assertInternalType( 'array', $result ); + } $this->assertEquals( $valid, $result ); } From b7c3b5187326b70292081d6411b275c28406d47c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 12 Mar 2019 19:37:22 +0100 Subject: [PATCH 20/42] implement keyword filter for referer blacklist (closes #15) In addition to the pre-existing normal and regular expression filters a keyword mode is added. This filter matches if the referer string contains a given keyword (case insensitive). --- README.md | 2 + inc/class-statifyblacklist-admin.php | 24 +++++- inc/class-statifyblacklist.php | 105 ++++++++++++++++++++------- test/statifyblacklist-test.php | 71 +++++++++++++++--- views/settings-page.php | 19 +++-- 5 files changed, 174 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index b913aee..3dc84e4 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,8 @@ Because of this, an IP blacklist can only be applied while processing the reques * Minimum required WordPress version is 4.7 * Removed `load_plugin_textdomain()` and `Domain Path` header * Added automatic compatibility check for WP and PHP version (#17) +* Added keyword filter mode for referer blacklist (#15) +* Layout adjustments on settings page ### 1.4.4 / 19.05.2018 ### * Fix live filter chain when regular expressions are active (#12) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 5fe6832..35f78d8 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -20,9 +20,6 @@ * @since 1.0.0 */ class StatifyBlacklist_Admin extends StatifyBlacklist { - const MODE_NORMAL = 0; - const MODE_REGEX = 1; - const MODE_REGEX_CI = 2; /** * Initialize admin-only components of the plugin. @@ -96,7 +93,7 @@ public static function update_options( $options = null ) { 'sanitized' => $sanitized_referer, 'diff' => array_diff( $given_referer, $sanitized_referer ), ], - 'target' => [ + 'target' => [ 'sanitized' => $sanitized_target, 'diff' => array_diff( $given_target, $sanitized_target ), ], @@ -334,4 +331,23 @@ function ( $ip ) { } ); } + + /** + * Validate regular expressions, i.e. remove duplicates and empty values and validate others. + * + * @since 1.5.0 #13 + * + * @param array $expressions Given pre-sanitized array of regular expressions. + * + * @return array Array of invalid expressions. + */ + private static function sanitize_regex( $expressions ) { + return array_filter( + $expressions, + function ( $re ) { + // Check of preg_match() fails (warnings suppressed). + return false === @preg_match( $re, null ); + } + ); + } } diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index faf8df3..59b071e 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -28,6 +28,35 @@ class StatifyBlacklist { */ const VERSION_MAIN = 1.4; + /** + * Operation mode "normal". + * + * @var integer MODE_NORMAL + */ + const MODE_NORMAL = 0; + + /** + * Operation mode "regular expression". + * + * @var integer MODE_REGEX + */ + const MODE_REGEX = 1; + + /** + * Operation mode "regular expression case insensitive". + * + * @var integer MODE_REGEX_CI + */ + const MODE_REGEX_CI = 2; + + /** + * Operation mode "keyword". + * + * @since 1.5.0 + * @var integer MODE_KEYWORD + */ + const MODE_KEYWORD = 3; + /** * Plugin options. * @@ -137,35 +166,57 @@ protected static function default_options() { public static function apply_blacklist_filter() { // Referer blacklist. if ( isset( self::$_options['referer']['active'] ) && 0 !== self::$_options['referer']['active'] ) { - // Regular Expression filtering since 1.3.0. - if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) { - // Get full referer string. - $referer = wp_get_raw_referer(); - if ( ! $referer ) { - $referer = ''; - } - // Merge given regular expressions into one. - $regexp = '/' . implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ) . '/'; - if ( 2 === self::$_options['referer']['regexp'] ) { - $regexp .= 'i'; - } + // Determine filter mode. + $mode = isset( self::$_options['referer']['regexp'] ) ? intval( self::$_options['referer']['regexp'] ) : 0; - // Check blacklist (no return to continue filtering #12). - if ( 1 === preg_match( $regexp, $referer ) ) { - return true; - } - } else { - // Extract relevant domain parts. - $referer = wp_parse_url( wp_get_raw_referer() ); - $referer = strtolower( ( isset( $referer['host'] ) ? $referer['host'] : '' ) ); + // Get full referer string. + $referer = wp_get_raw_referer(); + if ( ! $referer ) { + $referer = ''; + } - // Get blacklist. - $blacklist = self::$_options['referer']['blacklist']; + switch ( $mode ) { - // Check blacklist. - if ( isset( $blacklist[ $referer ] ) ) { - return true; - } + // Regular Expression filtering since 1.3.0. + case self::MODE_REGEX: + case self::MODE_REGEX_CI: + // Merge given regular expressions into one. + $regexp = '/' . implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ) . '/'; + if ( self::MODE_REGEX_CI === self::$_options['referer']['regexp'] ) { + $regexp .= 'i'; + } + + // Check blacklist (no return to continue filtering #12). + if ( 1 === preg_match( $regexp, $referer ) ) { + return true; + } + break; + + // Keyword filter since 1.5.0 (#15). + case self::MODE_KEYWORD: + // Get blacklist. + $blacklist = self::$_options['referer']['blacklist']; + + foreach ( array_keys( $blacklist ) as $keyword ) { + if ( false !== strpos( strtolower( $referer ), strtolower( $keyword ) ) ) { + return true; + } + } + break; + + // Standard domain filter. + default: + // Extract relevant domain parts. + $referer = wp_parse_url( $referer ); + $referer = strtolower( ( isset( $referer['host'] ) ? $referer['host'] : '' ) ); + + // Get blacklist. + $blacklist = self::$_options['referer']['blacklist']; + + // Check blacklist. + if ( isset( $blacklist[ $referer ] ) ) { + return true; + } } } @@ -324,6 +375,6 @@ private static function cidr_match( $ip, $net ) { } return ( 0 === substr_compare( sprintf( '%032b', ip2long( $ip ) ), sprintf( '%032b', ip2long( $base ) ), 0, $mask ) ); - } // End if(). + } } } diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index 29a7b43..e91d378 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -151,7 +151,7 @@ public function test_referer_regex_filter() { // Matching both. $_SERVER['HTTP_REFERER'] = 'http://example.net/test/me'; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); - // Mathinc with wrong case. + // Matching with wrong case. $_SERVER['HTTP_REFERER'] = 'http://eXaMpLe.NeT/tEsT/mE'; $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); @@ -160,6 +160,59 @@ public function test_referer_regex_filter() { $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); } + /** + * Test referer filter using keywords. + * + * @return void + */ + public function test_referer_keyword_filter() { + // Prepare Options: 2 regular expressions. + StatifyBlacklist::$_options = array( + 'referer' => array( + 'active' => 1, + 'cron' => 0, + 'regexp' => StatifyBlacklist::MODE_KEYWORD, + 'blacklist' => array( + 'example' => 0, + 'test' => 1, + ), + ), + 'target' => array( + 'active' => 0, + 'cron' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, + 'blacklist' => array(), + ), + 'ip' => array( + 'active' => 0, + 'blacklist' => array(), + ), + 'version' => StatifyBlacklist::VERSION_MAIN, + ); + + // No multisite. + StatifyBlacklist::$multisite = false; + + // No referer. + unset( $_SERVER['HTTP_REFERER'] ); + $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); + // Non-blacklisted referer. + $_SERVER['HTTP_REFERER'] = 'http://not.evil'; + $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); + // Blacklisted referer. + $_SERVER['HTTP_REFERER'] = 'http://example.com'; + $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); + // Blacklisted referer with path. + $_SERVER['HTTP_REFERER'] = 'http://foobar.net/test/me'; + $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); + // Matching both. + $_SERVER['HTTP_REFERER'] = 'http://example.net/test/me'; + $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); + // Matching with wrong case. + $_SERVER['HTTP_REFERER'] = 'http://eXaMpLe.NeT/tEsT/mE'; + $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); + } + /** * Test the upgrade methodology for configuration options. * @@ -366,13 +419,13 @@ public function test_ip_filter() { 'referer' => array( 'active' => 0, 'cron' => 0, - 'regexp' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array(), ), 'target' => array( 'active' => 0, 'cron' => 0, - 'regexp' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array(), ), 'ip' => array( @@ -438,13 +491,13 @@ public function test_target_filter() { 'referer' => array( 'active' => 0, 'cron' => 0, - 'regexp' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array(), ), 'target' => array( 'active' => 0, 'cron' => 0, - 'regexp' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array( '/excluded/page/' => 0, '/?page_id=3' => 1, @@ -513,7 +566,7 @@ public function test_combined_filters() { 'referer' => array( 'active' => 1, 'cron' => 0, - 'regexp' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array( 'example.com' => 0, ), @@ -521,7 +574,7 @@ public function test_combined_filters() { 'target' => array( 'active' => 1, 'cron' => 0, - 'regexp' => 0, + 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array( '/excluded/page/' => 0 ), @@ -561,9 +614,9 @@ public function test_combined_filters() { $_SERVER['REMOTE_ADDR'] = '192.0.2.234'; // Same for RegExp filters. - StatifyBlacklist::$_options['referer']['regexp'] = 1; + StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX; StatifyBlacklist::$_options['referer']['blacklist'] = array( 'example\.com' => 0 ); - StatifyBlacklist::$_options['target']['regexp'] = 1; + StatifyBlacklist::$_options['target']['regexp'] = StatifyBlacklist::MODE_REGEX; StatifyBlacklist::$_options['target']['blacklist'] = array( '\/excluded\/.*' => 0 ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); diff --git a/views/settings-page.php b/views/settings-page.php index 42dcdb4..b4a69bb 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -194,13 +194,16 @@ function ( $a ) { @@ -208,6 +211,8 @@ function ( $a ) {

    -
    + - +
    -

    @@ -272,14 +277,14 @@ function ( $a ) { - + - - From 124b4ecb752c18fc1333c07e57a930332efb1454 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 12 Mar 2019 19:39:40 +0100 Subject: [PATCH 21/42] dependency corrections for PHP 5.5 composer-copy-file 0.3 requires PHP 5.6, so reverted back to ~0.2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6477aec..8b32fc6 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "consolidation/robo": "^1.4", "phpunit/phpunit": "*", "phpunit/php-code-coverage": "*", - "slowprog/composer-copy-file": "~0.3", + "slowprog/composer-copy-file": "~0.2", "squizlabs/php_codesniffer": "^3.4", "phpcompatibility/php-compatibility": "^9.1", "wp-coding-standards/wpcs": "^2.0" From 84ce89b1278c0f153e3a5d61df8ffc172b9a32b9 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 17 Mar 2019 16:46:59 +0100 Subject: [PATCH 22/42] Add .gitattributes Set development files, tests and assets to export ignore list to clean up the package distributed via Composer/Packagist. --- .gitattributes | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c323621 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,12 @@ +/assets export-ignore +/test export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.travis.yml export-ignore +composer.json export-ignore +composer.lock export-ignore +CONTRIBUTING.md export-ignore +package.json export-ignore +phpcs.xml export-ignore +phpunit.xml export-ignore +RoboFile.php export-ignore From 39dcce3eeb0ea9ac39206bc407f5ce67fac6010a Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 17 Mar 2019 16:50:50 +0100 Subject: [PATCH 23/42] Harmonize helper funcitons to snake_case --- inc/class-statifyblacklist-admin.php | 10 +++++----- test/statifyblacklist-test.php | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 35f78d8..1920157 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -66,7 +66,7 @@ public static function update_options( $options = null ) { $given_referer = $options['referer']['blacklist']; if ( self::MODE_NORMAL === $options['referer']['regexp'] ) { // Sanitize URLs and remove empty inputs. - $sanitized_referer = self::sanitizeURLs( $given_referer ); + $sanitized_referer = self::sanitize_urls( $given_referer ); } elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) { // TODO Check regular expressions. $sanitized_referer = $given_referer; @@ -85,7 +85,7 @@ public static function update_options( $options = null ) { // Sanitize IPs and subnets and remove empty inputs. $given_ip = $options['ip']['blacklist']; - $sanitized_ip = self::sanitizeIPs( $given_ip ); + $sanitized_ip = self::sanitize_ips( $given_ip ); // Abort on errors. $errors = [ @@ -234,7 +234,7 @@ public static function cleanup_database() { $referer_regexp = implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ); } else { // Sanitize URLs. - $referer = self::sanitizeURLs( self::$_options['referer']['blacklist'] ); + $referer = self::sanitize_urls( self::$_options['referer']['blacklist'] ); // Build filter regexp. $referer_regexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); @@ -294,7 +294,7 @@ public static function cleanup_database() { * * @return array sanitized array. */ - private static function sanitizeURLs( $urls ) { + private static function sanitize_urls( $urls ) { return array_flip( array_filter( array_map( @@ -316,7 +316,7 @@ function ( $r ) { * * @return array sanitized array. */ - private static function sanitizeIPs( $ips ) { + private static function sanitize_ips( $ips ) { return array_filter( $ips, function ( $ip ) { diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index e91d378..b741062 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -371,7 +371,7 @@ public function test_sanitize_ips() { // IPv4 tests. $valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' ); $invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' ); - $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); + $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); /* * Unfortunately this is nencessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated @@ -398,7 +398,7 @@ public function test_sanitize_ips() { '2001:db8:a0b:12f0::/129', '1:2:3:4:5:6:7:8:9', ); - $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) ); + $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); if ( method_exists( $this, 'assertIsArray' ) ) { $this->assertIsArray( $result ); From 1c69ba31bb43e26811934b1da83b1413cd53028e Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 17 Mar 2019 17:27:33 +0100 Subject: [PATCH 24/42] Preprocessing of regular expression in separate funciton --- inc/class-statifyblacklist.php | 48 ++++++++++++++++++++++++++++------ test/statifyblacklist-test.php | 7 ++++- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index 59b071e..7bbe94c 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -116,6 +116,7 @@ public static function init() { * @since 1.2.1 update_options($options = null) Parameter with default value introduced. * * @param array $options Optional. New options to save. + * * @return void */ public static function update_options( $options = null ) { @@ -181,10 +182,10 @@ public static function apply_blacklist_filter() { case self::MODE_REGEX: case self::MODE_REGEX_CI: // Merge given regular expressions into one. - $regexp = '/' . implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ) . '/'; - if ( self::MODE_REGEX_CI === self::$_options['referer']['regexp'] ) { - $regexp .= 'i'; - } + $regexp = self::regex( + array_keys( self::$_options['referer']['blacklist'] ), + self::MODE_REGEX_CI === self::$_options['referer']['regexp'] + ); // Check blacklist (no return to continue filtering #12). if ( 1 === preg_match( $regexp, $referer ) ) { @@ -229,10 +230,10 @@ public static function apply_blacklist_filter() { $target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' ); // @codingStandardsIgnoreEnd // Merge given regular expressions into one. - $regexp = '/' . implode( '|', array_keys( self::$_options['target']['blacklist'] ) ) . '/'; - if ( 2 === self::$_options['target']['regexp'] ) { - $regexp .= 'i'; - } + $regexp = self::regex( + array_keys( self::$_options['target']['blacklist'] ), + self::MODE_REGEX_CI === self::$_options['target']['regexp'] + ); // Check blacklist (no return to continue filtering #12). if ( 1 === preg_match( $regexp, $target ) ) { @@ -268,6 +269,37 @@ public static function apply_blacklist_filter() { return null; } + /** + * Preprocess regular expression provided by the user, i.e. add delimiters and optional ci flag. + * + * @param string|array $expression Original expression string or array of expressions. + * @param string|array $case_insensitive Make expression match case-insensitive. + * + * @return string Preprocessed expression ready for preg_match(). + */ + protected static function regex( $expression, $case_insensitive ) { + $res = '/'; + if ( is_string( $expression ) ) { + $res .= str_replace( '/', '\/', $expression ); + } elseif ( is_array( $expression ) ) { + $res .= implode( + '|', + array_map( + function ( $e ) { + return str_replace( '/', '\/', $e ); + }, + $expression + ) + ); + } + $res .= '/'; + if ( $case_insensitive ) { + $res .= 'i'; + } + + return $res; + } + /** * Helper method to determine the client's IP address. * diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index b741062..4d9609f 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -617,11 +617,16 @@ public function test_combined_filters() { StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX; StatifyBlacklist::$_options['referer']['blacklist'] = array( 'example\.com' => 0 ); StatifyBlacklist::$_options['target']['regexp'] = StatifyBlacklist::MODE_REGEX; - StatifyBlacklist::$_options['target']['blacklist'] = array( '\/excluded\/.*' => 0 ); + StatifyBlacklist::$_options['target']['blacklist'] = array( '/excluded/.*' => 0 ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['HTTP_REFERER'] = 'https://example.com'; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); + // Check case-insensitive match. + $_SERVER['HTTP_REFERER'] = 'https://eXaMpLe.com'; + $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); + StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX_CI; + $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['HTTP_REFERER'] = 'https://example.net'; $_SERVER['REQUEST_URI'] = '/excluded/page/'; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); From 44ee7ee83921e7dee74a1e0043711997a1200e5a Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 17 Mar 2019 17:34:57 +0100 Subject: [PATCH 25/42] Check regular expressions and prevent saving invalid settings (#13) --- inc/class-statifyblacklist-admin.php | 20 ++++++++++++++------ views/settings-page.php | 7 ++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 1920157..18692f4 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -63,22 +63,26 @@ public static function update_options( $options = null ) { if ( isset( $options ) && current_user_can( 'manage_options' ) ) { // Sanitize referer list. - $given_referer = $options['referer']['blacklist']; + $given_referer = $options['referer']['blacklist']; + $invalid_referer = []; if ( self::MODE_NORMAL === $options['referer']['regexp'] ) { // Sanitize URLs and remove empty inputs. $sanitized_referer = self::sanitize_urls( $given_referer ); } elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) { - // TODO Check regular expressions. $sanitized_referer = $given_referer; + // Check regular expressions. + $invalid_referer = self::sanitize_regex( $given_referer ); } else { $sanitized_referer = $given_referer; } // Sanitize target list. - $given_target = $options['target']['blacklist']; + $given_target = $options['target']['blacklist']; + $invalid_target = []; if ( self::MODE_REGEX === $options['target']['regexp'] || self::MODE_REGEX_CI === $options['target']['regexp'] ) { - // TODO Check regular expressions. $sanitized_target = $given_target; + // Check regular expressions. + $invalid_target = self::sanitize_regex( $given_target ); } else { $sanitized_target = $given_target; } @@ -92,10 +96,12 @@ public static function update_options( $options = null ) { 'referer' => [ 'sanitized' => $sanitized_referer, 'diff' => array_diff( $given_referer, $sanitized_referer ), + 'invalid' => $invalid_referer, ], 'target' => [ 'sanitized' => $sanitized_target, 'diff' => array_diff( $given_target, $sanitized_target ), + 'invalid' => $invalid_target, ], 'ip' => [ 'sanitized' => $sanitized_ip, @@ -103,7 +109,9 @@ public static function update_options( $options = null ) { ], ]; if ( ! empty( $errors['referer']['diff'] ) + || ! empty( $errors['referer']['invalid'] ) || ! empty( $errors['target']['diff'] ) + || ! empty( $errors['target']['invalid'] ) || ! empty( $errors['ip']['diff'] ) ) { return $errors; } @@ -343,10 +351,10 @@ function ( $ip ) { */ private static function sanitize_regex( $expressions ) { return array_filter( - $expressions, + array_flip( $expressions ), function ( $re ) { // Check of preg_match() fails (warnings suppressed). - return false === @preg_match( $re, null ); + return false === @preg_match( StatifyBlacklist::regex( $re, false ), null ); } ); } diff --git a/views/settings-page.php b/views/settings-page.php index b4a69bb..cd073cf 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -116,6 +116,9 @@ function ( $a ) { if ( ! empty( $statifyblacklist_update_result['referer']['diff'] ) ) { $statifyblacklist_post_warning[] = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' ); } + if ( ! empty( $statifyblacklist_update_result['referer']['invalid'] ) ) { + $statifyblacklist_post_warning[] = __( 'Some regular expressions are invalid:', 'statify-blacklist' ) . '
    ' . implode( '
    ', $statifyblacklist_update_result['referer']['invalid'] ); + } if ( ! empty( $statifyblacklist_update_result['ip']['diff'] ) ) { // translators: List of invalid IP addresses (comma separated). $statifyblacklist_post_warning[] = sprintf( __( 'Some IPs are invalid: %s', 'statify-blacklist' ), implode( ', ', $statifyblacklist_update_result['ip']['diff'] ) ); @@ -144,7 +147,9 @@ function ( $a ) { } if ( isset( $statifyblacklist_post_warning ) ) { foreach ( $statifyblacklist_post_warning as $w ) { - print '

    ' . esc_html( $w ) . '

    '; + print '

    ' . + wp_kses( $w, [ 'br' => [] ] ) . + '

    '; } print '

    ' . esc_html( 'Settings have not been saved yet.', 'statify-blacklist' ) . '

    '; } From 22373d2308b8ee99aa7c4a5d5f9d3b99aa0f5867 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 17 Mar 2019 17:43:29 +0100 Subject: [PATCH 26/42] CONTRIBUTING++ [skip ci] Adapt branch names to git flow and add a sentence on pull requests. --- CONTRIBUTING.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b70affd..8e24a86 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,17 +33,19 @@ If the changes introduce new functionality or affect major parts of existing cod For adding new functionality a new test case the corresponding PHPUnit test would be nice (no hard criterion though). +The `master` branch should also be target for most pull requests. +However it it features new functionality you might want to target the `develop` branch instead (see next section for details on branches). + ### Branches The `master` branch represents the current state of development. Please ensure your initial code is up to date with it at the time you start development. -The `master` should also be target for most pull requests. In addition, this project features a `develop` branch, which holds bleeding edge developments, not necessarily considered stable or even compatible. Do not expect this code to run smoothly, but you might have a look into the history to see if some work on an issue has already been started there. -For fixes and features, there might be additional branches, likely prefixed by `ft-` (feature) or `hf-` (hotfix) followed by an issue number (if applicable) and/or a title. -Feel free to adapt these naming scheme to your forks. +For fixes and features, there might be additional branches, likely prefixed by `hotfix/` or `feature/` followed by an issue number (if applicable) and/or a title. +Feel free to adapt this naming scheme to your forks. ### Merge Requirements From 0b07697db8640d07ee86f9c719c9342764b8c5e3 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 18 Aug 2019 18:21:57 +0200 Subject: [PATCH 27/42] check if POST values are actually set before sanitization --- views/settings-page.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/views/settings-page.php b/views/settings-page.php index cd073cf..dd1a75a 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -27,7 +27,9 @@ StatifyBlacklist_Admin::cleanup_database(); } else { // Extract referer array. - $referer_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['referer']['blacklist'] ) ); + if ( isset( $_POST['statifyblacklist']['referer']['blacklist'] ) ) { + $referer_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['referer']['blacklist'] ) ); + } if ( empty( trim( $referer_str ) ) ) { $referer = []; } else { @@ -45,7 +47,9 @@ function ( $a ) { } // Extract target array. - $target_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['target']['blacklist'] ) ); + if ( isset( $_POST['statifyblacklist']['target']['blacklist'] ) ) { + $target_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['target']['blacklist'] ) ); + } if ( empty( trim( $target_str ) ) ) { $target = []; } else { @@ -63,7 +67,9 @@ function ( $a ) { } // Extract IP array. - $ip_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ip']['blacklist'] ) ); + if ( isset( $_POST['statifyblacklist']['ip']['blacklist'] ) ) { + $ip_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ip']['blacklist'] ) ); + } if ( empty( trim( $ip_str ) ) ) { $ip = []; } else { From 82667dcf93d165207f0dabc3c3d5493f013fe020 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 18 Aug 2019 18:23:33 +0200 Subject: [PATCH 28/42] update Composer dependencies --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 8b32fc6..8dc9df3 100644 --- a/composer.json +++ b/composer.json @@ -22,14 +22,14 @@ "composer/installers": "~1.0" }, "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.4", + "dealerdirect/phpcodesniffer-composer-installer": "^0.5", "consolidation/robo": "^1.4", "phpunit/phpunit": "*", "phpunit/php-code-coverage": "*", "slowprog/composer-copy-file": "~0.2", "squizlabs/php_codesniffer": "^3.4", - "phpcompatibility/php-compatibility": "^9.1", - "wp-coding-standards/wpcs": "^2.0" + "phpcompatibility/php-compatibility": "^9.2", + "wp-coding-standards/wpcs": "^2.1" }, "scripts": { "build": [ From a88a89c4423c2f679f028add4e98d9d811173bee Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 6 Oct 2019 17:32:22 +0200 Subject: [PATCH 29/42] update WP Codex and license links [skip ci] --- LICENSE.md | 2 +- README.md | 4 ++-- statify-blacklist.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 5544f2d..28fbeca 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -357,5 +357,5 @@ into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the [GNU Lesser General Public -License](http://www.gnu.org/licenses/lgpl.html) instead of this +License](https://www.gnu.org/licenses/lgpl.html) instead of this License. diff --git a/README.md b/README.md index 3dc84e4..db3affb 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ * Requires PHP: 5.5 * Stable tag: 1.4.4 * License: GPLv2 or later -* License URI: http://www.gnu.org/licenses/gpl-2.0.html +* License URI: https://www.gnu.org/licenses/gpl-2.0.html ## Description ## A blacklist extension for the famous [Statify](https://wordpress.org/plugins/statify/) Wordpress plugin. @@ -45,7 +45,7 @@ The plugin is capable of handling multisite installations. * Special Thanks to [pluginkollektiv](https://github.com/pluginkollektiv) for maintaining _Statify_ ## Installation ## -* If you don’t know how to install a plugin for WordPress, [here’s how](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins). +* If you don’t know how to install a plugin for WordPress, [here’s how](https://wordpress.org/support/article/managing-plugins/#installing-plugins). * Make sure _Statify_ plugin is installed and active * Goto _Settings_ -> _Statify Blacklist_ to configure the plugin diff --git a/statify-blacklist.php b/statify-blacklist.php index 02a5c39..f7d5111 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -27,7 +27,7 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with Statify Blacklist. If not, see http://www.gnu.org/licenses/gpl-2.0.html. + * along with Statify Blacklist. If not, see https://www.gnu.org/licenses/gpl-2.0.html. */ // Quit if accessed directly. From 84cf79fd041b7ff1c5f1b02d1832a6437fe98a0a Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 6 Oct 2019 17:52:08 +0200 Subject: [PATCH 30/42] remove underscore prefix from options field --- inc/class-statifyblacklist-admin.php | 20 +++++++-------- inc/class-statifyblacklist-system.php | 20 +++++++-------- inc/class-statifyblacklist.php | 36 +++++++++++++-------------- test/statifyblacklist-test.php | 34 ++++++++++++------------- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 18692f4..9a77a26 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -229,20 +229,20 @@ public static function cleanup_database() { } if ( defined( 'DOING_CRON' ) && DOING_CRON ) { - $clean_ref = ( 1 === self::$_options['referer']['cron'] ); - $clean_trg = ( 1 === self::$_options['target']['cron'] ); + $clean_ref = ( 1 === self::$options['referer']['cron'] ); + $clean_trg = ( 1 === self::$options['target']['cron'] ); } else { $clean_ref = true; $clean_trg = true; } if ( $clean_ref ) { - if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) { + if ( isset( self::$options['referer']['regexp'] ) && self::$options['referer']['regexp'] > 0 ) { // Merge given regular expressions into one. - $referer_regexp = implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ); + $referer_regexp = implode( '|', array_keys( self::$options['referer']['blacklist'] ) ); } else { // Sanitize URLs. - $referer = self::sanitize_urls( self::$_options['referer']['blacklist'] ); + $referer = self::sanitize_urls( self::$options['referer']['blacklist'] ); // Build filter regexp. $referer_regexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); @@ -250,12 +250,12 @@ public static function cleanup_database() { } if ( $clean_trg ) { - if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) { + if ( isset( self::$options['target']['regexp'] ) && self::$options['target']['regexp'] > 0 ) { // Merge given regular expressions into one. - $target_regexp = implode( '|', array_keys( self::$_options['target']['blacklist'] ) ); + $target_regexp = implode( '|', array_keys( self::$options['target']['blacklist'] ) ); } else { // Build filter regexp. - $target_regexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) ); + $target_regexp = str_replace( '.', '\.', implode( '|', array_flip( self::$options['target']['blacklist'] ) ) ); } } @@ -268,7 +268,7 @@ public static function cleanup_database() { $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE " - . ( ( 1 === self::$_options['referer']['regexp'] ) ? ' BINARY ' : '' ) + . ( ( 1 === self::$options['referer']['regexp'] ) ? ' BINARY ' : '' ) . 'referrer REGEXP %s', $referer_regexp ) ); @@ -277,7 +277,7 @@ public static function cleanup_database() { $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE " - . ( ( 1 === self::$_options['target']['regexp'] ) ? ' BINARY ' : '' ) + . ( ( 1 === self::$options['target']['regexp'] ) ? ' BINARY ' : '' ) . 'target REGEXP %s', $target_regexp ) ); diff --git a/inc/class-statifyblacklist-system.php b/inc/class-statifyblacklist-system.php index c136d29..a0312c3 100644 --- a/inc/class-statifyblacklist-system.php +++ b/inc/class-statifyblacklist-system.php @@ -134,10 +134,10 @@ public static function uninstall_site( $site_id ) { 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 ) ) { + 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'] ); + $options = self::$options; + $options['referer'] = array_flip( self::$options['referer'] ); if ( self::$multisite ) { update_site_option( 'statify-blacklist', $options ); } else { @@ -146,14 +146,14 @@ public static function upgrade() { } // Version not set (pre 1.3.0) or older than 1.4. - if ( ! isset( self::$_options['version'] ) || self::$_options['version'] < 1.4 ) { + if ( ! isset( self::$options['version'] ) || self::$options['version'] < 1.4 ) { // Upgrade options to new schema. $options = array( 'referer' => array( - 'active' => self::$_options['active_referer'], - 'cron' => self::$_options['cron_referer'], - 'regexp' => self::$_options['referer_regexp'], - 'blacklist' => self::$_options['referer'], + 'active' => self::$options['active_referer'], + 'cron' => self::$options['cron_referer'], + 'regexp' => self::$options['referer_regexp'], + 'blacklist' => self::$options['referer'], ), 'target' => array( 'active' => 0, @@ -176,9 +176,9 @@ public static function upgrade() { } // Version older than current major release. - if ( self::VERSION_MAIN > self::$_options['version'] ) { + if ( self::VERSION_MAIN > self::$options['version'] ) { // Merge default options with current config, assuming only additive changes. - $options = array_merge_recursive( self::default_options(), self::$_options ); + $options = array_merge_recursive( self::default_options(), self::$options ); $options['version'] = self::VERSION_MAIN; if ( self::$multisite ) { update_site_option( 'statify-blacklist', $options ); diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index 7bbe94c..05b9b12 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -61,9 +61,9 @@ class StatifyBlacklist { * Plugin options. * * @since 1.0.0 - * @var array $_options + * @var array $options */ - public static $_options; + public static $options; /** * Multisite Status. @@ -93,7 +93,7 @@ public static function init() { self::update_options(); // Add Filter to statify hook if enabled. - if ( 0 !== self::$_options['referer']['active'] || 0 !== self::$_options['target']['active'] || 0 !== self::$_options['ip']['active'] ) { + if ( 0 !== self::$options['referer']['active'] || 0 !== self::$options['target']['active'] || 0 !== self::$options['ip']['active'] ) { add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) ); } @@ -104,7 +104,7 @@ public static function init() { // CronJob to clean up database. if ( defined( 'DOING_CRON' ) && DOING_CRON && - ( 1 === self::$_options['referer']['cron'] || 1 === self::$_options['target']['cron'] ) ) { + ( 1 === self::$options['referer']['cron'] || 1 === self::$options['target']['cron'] ) ) { add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) ); } } @@ -125,7 +125,7 @@ public static function update_options( $options = null ) { } else { $o = get_option( 'statify-blacklist' ); } - self::$_options = wp_parse_args( $o, self::default_options() ); + self::$options = wp_parse_args( $o, self::default_options() ); } /** @@ -166,9 +166,9 @@ protected static function default_options() { */ public static function apply_blacklist_filter() { // Referer blacklist. - if ( isset( self::$_options['referer']['active'] ) && 0 !== self::$_options['referer']['active'] ) { + if ( isset( self::$options['referer']['active'] ) && 0 !== self::$options['referer']['active'] ) { // Determine filter mode. - $mode = isset( self::$_options['referer']['regexp'] ) ? intval( self::$_options['referer']['regexp'] ) : 0; + $mode = isset( self::$options['referer']['regexp'] ) ? intval( self::$options['referer']['regexp'] ) : 0; // Get full referer string. $referer = wp_get_raw_referer(); @@ -183,8 +183,8 @@ public static function apply_blacklist_filter() { case self::MODE_REGEX_CI: // Merge given regular expressions into one. $regexp = self::regex( - array_keys( self::$_options['referer']['blacklist'] ), - self::MODE_REGEX_CI === self::$_options['referer']['regexp'] + array_keys( self::$options['referer']['blacklist'] ), + self::MODE_REGEX_CI === self::$options['referer']['regexp'] ); // Check blacklist (no return to continue filtering #12). @@ -196,7 +196,7 @@ public static function apply_blacklist_filter() { // Keyword filter since 1.5.0 (#15). case self::MODE_KEYWORD: // Get blacklist. - $blacklist = self::$_options['referer']['blacklist']; + $blacklist = self::$options['referer']['blacklist']; foreach ( array_keys( $blacklist ) as $keyword ) { if ( false !== strpos( strtolower( $referer ), strtolower( $keyword ) ) ) { @@ -212,7 +212,7 @@ public static function apply_blacklist_filter() { $referer = strtolower( ( isset( $referer['host'] ) ? $referer['host'] : '' ) ); // Get blacklist. - $blacklist = self::$_options['referer']['blacklist']; + $blacklist = self::$options['referer']['blacklist']; // Check blacklist. if ( isset( $blacklist[ $referer ] ) ) { @@ -222,17 +222,17 @@ public static function apply_blacklist_filter() { } // Target blacklist (since 1.4.0). - if ( isset( self::$_options['target']['active'] ) && 0 !== self::$_options['target']['active'] ) { + if ( isset( self::$options['target']['active'] ) && 0 !== self::$options['target']['active'] ) { // Regular Expression filtering since 1.3.0. - if ( isset( self::$_options['target']['regexp'] ) && 0 < self::$_options['target']['regexp'] ) { + if ( isset( self::$options['target']['regexp'] ) && 0 < self::$options['target']['regexp'] ) { // Get full referer string. // @codingStandardsIgnoreStart The globals are checked. $target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' ); // @codingStandardsIgnoreEnd // Merge given regular expressions into one. $regexp = self::regex( - array_keys( self::$_options['target']['blacklist'] ), - self::MODE_REGEX_CI === self::$_options['target']['regexp'] + array_keys( self::$options['target']['blacklist'] ), + self::MODE_REGEX_CI === self::$options['target']['regexp'] ); // Check blacklist (no return to continue filtering #12). @@ -245,7 +245,7 @@ public static function apply_blacklist_filter() { $target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' ); // @codingStandardsIgnoreEnd // Get blacklist. - $blacklist = self::$_options['target']['blacklist']; + $blacklist = self::$options['target']['blacklist']; // Check blacklist. if ( isset( $blacklist[ $target ] ) ) { return true; @@ -254,10 +254,10 @@ public static function apply_blacklist_filter() { } // IP blacklist (since 1.4.0). - if ( isset( self::$_options['ip']['active'] ) && 0 !== self::$_options['ip']['active'] ) { + if ( isset( self::$options['ip']['active'] ) && 0 !== self::$options['ip']['active'] ) { $ip = self::get_ip(); if ( false !== ( $ip ) ) { - foreach ( self::$_options['ip']['blacklist'] as $net ) { + foreach ( self::$options['ip']['blacklist'] as $net ) { if ( self::cidr_match( $ip, $net ) ) { return true; } diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index 4d9609f..cd3aad6 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -48,7 +48,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase { */ public function test_referer_filter() { // Prepare Options: 2 blacklisted domains, disabled. - StatifyBlacklist::$_options = array( + StatifyBlacklist::$options = array( 'referer' => array( 'active' => 0, 'cron' => 0, @@ -88,7 +88,7 @@ public function test_referer_filter() { $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); // Activate filter and run tests again. - StatifyBlacklist::$_options['referer']['active'] = 1; + StatifyBlacklist::$options['referer']['active'] = 1; unset( $_SERVER['HTTP_REFERER'] ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); @@ -110,7 +110,7 @@ public function test_referer_filter() { */ public function test_referer_regex_filter() { // Prepare Options: 2 regular expressions. - StatifyBlacklist::$_options = array( + StatifyBlacklist::$options = array( 'referer' => array( 'active' => 1, 'cron' => 0, @@ -156,7 +156,7 @@ public function test_referer_regex_filter() { $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); // Set RegExp filter to case insensitive. - StatifyBlacklist::$_options['referer']['regexp'] = 2; + StatifyBlacklist::$options['referer']['regexp'] = 2; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); } @@ -167,7 +167,7 @@ public function test_referer_regex_filter() { */ public function test_referer_keyword_filter() { // Prepare Options: 2 regular expressions. - StatifyBlacklist::$_options = array( + StatifyBlacklist::$options = array( 'referer' => array( 'active' => 1, 'cron' => 0, @@ -415,7 +415,7 @@ public function test_sanitize_ips() { */ public function test_ip_filter() { // Prepare Options: 2 blacklisted IPs, disabled. - StatifyBlacklist::$_options = array( + StatifyBlacklist::$options = array( 'referer' => array( 'active' => 0, 'cron' => 0, @@ -445,7 +445,7 @@ public function test_ip_filter() { $_SERVER['REMOTE_ADDR'] = '192.0.2.123'; $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); // Activate filter. - StatifyBlacklist::$_options['ip']['active'] = 1; + StatifyBlacklist::$options['ip']['active'] = 1; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); // Try matching v6 address. $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::1'; @@ -456,11 +456,11 @@ public function test_ip_filter() { $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::2'; $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); // Subnet matching. - StatifyBlacklist::$_options['ip']['blacklist'] = array( + StatifyBlacklist::$options['ip']['blacklist'] = array( '192.0.2.0/25', '2001:db8:a0b:12f0::/96', ); - $_SERVER['REMOTE_ADDR'] = '192.0.2.123'; + $_SERVER['REMOTE_ADDR'] = '192.0.2.123'; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['REMOTE_ADDR'] = '192.0.2.234'; $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); @@ -487,7 +487,7 @@ public function test_ip_filter() { */ public function test_target_filter() { // Prepare Options: 2 blacklisted domains, disabled. - StatifyBlacklist::$_options = array( + StatifyBlacklist::$options = array( 'referer' => array( 'active' => 0, 'cron' => 0, @@ -530,7 +530,7 @@ public function test_target_filter() { $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); // Activate filter and run tests again. - StatifyBlacklist::$_options['target']['active'] = 1; + StatifyBlacklist::$options['target']['active'] = 1; unset( $_SERVER['REQUEST_URI'] ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); @@ -562,7 +562,7 @@ public function test_target_filter() { */ public function test_combined_filters() { // Prepare Options: simple referer + simple target + ip. - StatifyBlacklist::$_options = array( + StatifyBlacklist::$options = array( 'referer' => array( 'active' => 1, 'cron' => 0, @@ -614,10 +614,10 @@ public function test_combined_filters() { $_SERVER['REMOTE_ADDR'] = '192.0.2.234'; // Same for RegExp filters. - StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX; - StatifyBlacklist::$_options['referer']['blacklist'] = array( 'example\.com' => 0 ); - StatifyBlacklist::$_options['target']['regexp'] = StatifyBlacklist::MODE_REGEX; - StatifyBlacklist::$_options['target']['blacklist'] = array( '/excluded/.*' => 0 ); + StatifyBlacklist::$options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX; + StatifyBlacklist::$options['referer']['blacklist'] = array( 'example\.com' => 0 ); + StatifyBlacklist::$options['target']['regexp'] = StatifyBlacklist::MODE_REGEX; + StatifyBlacklist::$options['target']['blacklist'] = array( '/excluded/.*' => 0 ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['HTTP_REFERER'] = 'https://example.com'; @@ -625,7 +625,7 @@ public function test_combined_filters() { // Check case-insensitive match. $_SERVER['HTTP_REFERER'] = 'https://eXaMpLe.com'; $this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); - StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX_CI; + StatifyBlacklist::$options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX_CI; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['HTTP_REFERER'] = 'https://example.net'; $_SERVER['REQUEST_URI'] = '/excluded/page/'; From 3f5990f1f37bb25c7c8305dbc34cb9794957a149 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 6 Oct 2019 17:57:55 +0200 Subject: [PATCH 31/42] update PHPCS ruleset and re-enable warnings --- RoboFile.php | 16 +++--- composer.json | 6 +- inc/class-statifyblacklist-admin.php | 2 + phpcs.xml | 10 +--- statify-blacklist.php | 2 + test/statifyblacklist-test.php | 83 +++++++++++++--------------- views/settings-page.php | 6 +- 7 files changed, 60 insertions(+), 65 deletions(-) diff --git a/RoboFile.php b/RoboFile.php index b08f75b..fc7fe1e 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -19,10 +19,10 @@ */ class RoboFile extends Tasks { const PROJECT_NAME = 'statify-blacklist'; - const SVN_URL = 'https://plugins.svn.wordpress.org/statify-blacklist'; + const SVN_URL = 'https://plugins.svn.wordpress.org/statify-blacklist'; - const OPT_TARGET = 'target'; - const OPT_SKIPTEST = 'skipTests'; + const OPT_TARGET = 'target'; + const OPT_SKIPTEST = 'skipTests'; const OPT_SKIPSTYLE = 'skipStyle'; /** @@ -134,10 +134,12 @@ public function build( */ private function bundle() { $this->say( 'Bundling resources...' ); - $this->taskCopyDir( [ - 'inc' => $this->target_dir . '/' . $this->final_name . '/inc', - 'views' => $this->target_dir . '/' . $this->final_name . '/views', - ] )->run(); + $this->taskCopyDir( + [ + 'inc' => $this->target_dir . '/' . $this->final_name . '/inc', + 'views' => $this->target_dir . '/' . $this->final_name . '/views', + ] + )->run(); $this->_copy( 'statify-blacklist.php', $this->target_dir . '/' . $this->final_name . '/statify-blacklist.php' ); $this->_copy( 'LICENSE.md', $this->target_dir . '/' . $this->final_name . '/LICENSE.md' ); $this->_copy( 'README.md', $this->target_dir . '/' . $this->final_name . '/README.md' ); diff --git a/composer.json b/composer.json index 8dc9df3..a2190ec 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "type": "wordpress-plugin", "require": { "php": ">=5.5", - "composer/installers": "~1.0" + "composer/installers": "~1.7" }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.5", @@ -27,8 +27,8 @@ "phpunit/phpunit": "*", "phpunit/php-code-coverage": "*", "slowprog/composer-copy-file": "~0.2", - "squizlabs/php_codesniffer": "^3.4", - "phpcompatibility/php-compatibility": "^9.2", + "squizlabs/php_codesniffer": "^3.5", + "phpcompatibility/php-compatibility": "^9.3", "wp-coding-standards/wpcs": "^2.1" }, "scripts": { diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 9a77a26..2ab7e29 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -354,6 +354,8 @@ private static function sanitize_regex( $expressions ) { array_flip( $expressions ), function ( $re ) { // Check of preg_match() fails (warnings suppressed). + + // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged return false === @preg_match( StatifyBlacklist::regex( $re, false ), null ); } ); diff --git a/phpcs.xml b/phpcs.xml index b317a5b..3e2fb67 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,7 +2,7 @@ Derived from WordPress Coding Standard - + @@ -13,13 +13,9 @@ - - - - - - + + diff --git a/statify-blacklist.php b/statify-blacklist.php index f7d5111..2e94b7c 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -105,9 +105,11 @@ function statify_blacklist_disable() { if ( is_plugin_active( STATIFYBLACKLIST_BASE ) ) { deactivate_plugins( STATIFYBLACKLIST_BASE ); add_action( 'admin_notices', 'statify_blacklist_disabled_notice' ); + // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['activate'] ) ) { unset( $_GET['activate'] ); } + // phpcs:enable } } diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index cd3aad6..16c6f41 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -275,59 +275,52 @@ public function test_cidr_match() { $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/32' ) ) ); $this->assertFalse( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '127.0.0.1', - '127.0.0.1/33', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '127.0.0.1', '127.0.0.1/33' ) ) ); $this->assertFalse( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '127.0.0.1', - '127.0.0.1/-1', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '127.0.0.1', '127.0.0.1/-1' ) ) ); $this->assertTrue( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '192.0.2.123', - '192.0.2.0/24', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '192.0.2.123', '192.0.2.0/24' ) ) ); $this->assertFalse( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '192.0.3.123', - '192.0.2.0/24', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '192.0.3.123', '192.0.2.0/24' ) ) ); $this->assertTrue( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '192.0.2.123', - '192.0.2.120/29', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '192.0.2.123', '192.0.2.120/29' ) ) ); $this->assertFalse( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '192.0.2.128', - '192.0.2.120/29', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '192.0.2.128', '192.0.2.120/29' ) ) ); $this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.13', '10.0.0.0/8' ) ) ); $this->assertFalse( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '10.11.12.345', - '10.0.0.0/8', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '10.11.12.345', '10.0.0.0/8' ) ) ); @@ -338,26 +331,23 @@ public function test_cidr_match() { $this->assertFalse( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/-1' ) ) ); $this->assertTrue( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '2001:db8:a0b:12f0:1:2:3:4', - '2001:db8:a0b:12f0::1/64 ', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '2001:db8:a0b:12f0:1:2:3:4', '2001:db8:a0b:12f0::1/64 ' ) ) ); $this->assertTrue( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '2001:db8:a0b:12f0::123:456', - '2001:db8:a0b:12f0::1/96 ', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '2001:db8:a0b:12f0::123:456', '2001:db8:a0b:12f0::1/96 ' ) ) ); $this->assertFalse( invoke_static( - StatifyBlacklist::class, 'cidr_match', array( - '2001:db8:a0b:12f0::1:132:465', - '2001:db8:a0b:12f0::1/96 ', - ) + StatifyBlacklist::class, + 'cidr_match', + array( '2001:db8:a0b:12f0::1:132:465', '2001:db8:a0b:12f0::1/96 ' ) ) ); } @@ -373,6 +363,7 @@ public function test_sanitize_ips() { $invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' ); $result = invoke_static( StatifyBlacklist_Admin::class, 'sanitize_ips', array( array_merge( $valid, $invalid ) ) ); $this->assertNotFalse( $result ); + /* * Unfortunately this is nencessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated * as of PHPUnit 8, but "assertIsArray" has been introduces in PHPUnit 7.5 which requires PHP >= 7.1. @@ -576,13 +567,13 @@ public function test_combined_filters() { 'cron' => 0, 'regexp' => StatifyBlacklist::MODE_NORMAL, 'blacklist' => array( - '/excluded/page/' => 0 + '/excluded/page/' => 0, ), ), 'ip' => array( 'active' => 1, 'blacklist' => array( - '192.0.2.123' + '192.0.2.123', ), ), 'version' => StatifyBlacklist::VERSION_MAIN, @@ -593,8 +584,8 @@ public function test_combined_filters() { // No match. $_SERVER['HTTP_REFERER'] = 'https://example.net'; - $_SERVER['REQUEST_URI'] = '/normal/page/'; - $_SERVER['REMOTE_ADDR'] = '192.0.2.234'; + $_SERVER['REQUEST_URI'] = '/normal/page/'; + $_SERVER['REMOTE_ADDR'] = '192.0.2.234'; unset( $_SERVER['HTTP_X_FORWARDED_FOR'] ); unset( $_SERVER['HTTP_X_REAL_IP'] ); @@ -604,7 +595,7 @@ public function test_combined_filters() { // Matching target. $_SERVER['HTTP_REFERER'] = 'https://example.net'; - $_SERVER['REQUEST_URI'] = '/excluded/page/'; + $_SERVER['REQUEST_URI'] = '/excluded/page/'; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); // Matching IP. @@ -628,7 +619,7 @@ public function test_combined_filters() { StatifyBlacklist::$options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX_CI; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['HTTP_REFERER'] = 'https://example.net'; - $_SERVER['REQUEST_URI'] = '/excluded/page/'; + $_SERVER['REQUEST_URI'] = '/excluded/page/'; $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $_SERVER['REQUEST_URI'] = '/normal/page/'; $_SERVER['REMOTE_ADDR'] = '192.0.2.123'; diff --git a/views/settings-page.php b/views/settings-page.php index dd1a75a..e4c6404 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -9,6 +9,8 @@ * @since 1.0.0 */ +// phpcs:disable WordPress.WhiteSpace.PrecisionAlignment.Found + // Quit. defined( 'ABSPATH' ) || exit; @@ -132,8 +134,8 @@ function ( $a ) { } else { $statifyblacklist_post_success = __( 'Settings updated successfully.', 'statify-blacklist' ); } - } // End if(). -} // End if(). + } +} /* * Disable some code style rules that are impractical for textarea content: From ebc44c722ecec5a074026559d3104990b0f84f1c Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 7 Jan 2020 19:24:38 +0100 Subject: [PATCH 32/42] use long array syntax For some reason short syntax is discouraged in the latest WPCS ruleset. To stay in line with WPCS we use long syntax now. --- inc/class-statifyblacklist-admin.php | 20 ++++++++++---------- views/settings-page.php | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 2ab7e29..2644d4c 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -64,7 +64,7 @@ public static function update_options( $options = null ) { // Sanitize referer list. $given_referer = $options['referer']['blacklist']; - $invalid_referer = []; + $invalid_referer = array(); if ( self::MODE_NORMAL === $options['referer']['regexp'] ) { // Sanitize URLs and remove empty inputs. $sanitized_referer = self::sanitize_urls( $given_referer ); @@ -78,7 +78,7 @@ public static function update_options( $options = null ) { // Sanitize target list. $given_target = $options['target']['blacklist']; - $invalid_target = []; + $invalid_target = array(); if ( self::MODE_REGEX === $options['target']['regexp'] || self::MODE_REGEX_CI === $options['target']['regexp'] ) { $sanitized_target = $given_target; // Check regular expressions. @@ -92,22 +92,22 @@ public static function update_options( $options = null ) { $sanitized_ip = self::sanitize_ips( $given_ip ); // Abort on errors. - $errors = [ - 'referer' => [ + $errors = array( + 'referer' => array( 'sanitized' => $sanitized_referer, 'diff' => array_diff( $given_referer, $sanitized_referer ), 'invalid' => $invalid_referer, - ], - 'target' => [ + ), + 'target' => array( 'sanitized' => $sanitized_target, 'diff' => array_diff( $given_target, $sanitized_target ), 'invalid' => $invalid_target, - ], - 'ip' => [ + ), + 'ip' => array( 'sanitized' => $sanitized_ip, 'diff' => array_diff( $given_ip, $sanitized_ip ), - ], - ]; + ), + ); if ( ! empty( $errors['referer']['diff'] ) || ! empty( $errors['referer']['invalid'] ) || ! empty( $errors['target']['diff'] ) diff --git a/views/settings-page.php b/views/settings-page.php index e4c6404..46ce8d0 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -33,7 +33,7 @@ $referer_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['referer']['blacklist'] ) ); } if ( empty( trim( $referer_str ) ) ) { - $referer = []; + $referer = array(); } else { $referer = array_filter( array_map( @@ -53,7 +53,7 @@ function ( $a ) { $target_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['target']['blacklist'] ) ); } if ( empty( trim( $target_str ) ) ) { - $target = []; + $target = array(); } else { $target = array_filter( array_map( @@ -73,7 +73,7 @@ function ( $a ) { $ip_str = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ip']['blacklist'] ) ); } if ( empty( trim( $ip_str ) ) ) { - $ip = []; + $ip = array(); } else { $ip = array_filter( array_map( @@ -90,8 +90,8 @@ function ( $a ) { // Update options (data will be sanitized). $statifyblacklist_update_result = StatifyBlacklist_Admin::update_options( - [ - 'referer' => [ + array( + 'referer' => array( 'active' => isset( $_POST['statifyblacklist']['referer']['active'] ) ? (int) $_POST['statifyblacklist']['referer']['active'] : 0, 'cron' => isset( $_POST['statifyblacklist']['referer']['cron'] ) @@ -99,8 +99,8 @@ function ( $a ) { 'regexp' => isset( $_POST['statifyblacklist']['referer']['regexp'] ) ? (int) $_POST['statifyblacklist']['referer']['regexp'] : 0, 'blacklist' => array_flip( $referer ), - ], - 'target' => [ + ), + 'target' => array( 'active' => isset( $_POST['statifyblacklist']['target']['active'] ) ? (int) $_POST['statifyblacklist']['target']['active'] : 0, 'cron' => isset( $_POST['statifyblacklist']['target']['cron'] ) @@ -108,19 +108,19 @@ function ( $a ) { 'regexp' => isset( $_POST['statifyblacklist']['target']['regexp'] ) ? (int) $_POST['statifyblacklist']['target']['regexp'] : 0, 'blacklist' => array_flip( $target ), - ], - 'ip' => [ + ), + 'ip' => array( 'active' => isset( $_POST['statifyblacklist']['ip']['active'] ) ? (int) $_POST['statifyblacklist']['ip']['active'] : 0, 'blacklist' => $ip, - ], + ), 'version' => StatifyBlacklist::VERSION_MAIN, - ] + ) ); // Generate messages. if ( false !== $statifyblacklist_update_result ) { - $statifyblacklist_post_warning = []; + $statifyblacklist_post_warning = array(); if ( ! empty( $statifyblacklist_update_result['referer']['diff'] ) ) { $statifyblacklist_post_warning[] = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' ); } @@ -156,7 +156,7 @@ function ( $a ) { if ( isset( $statifyblacklist_post_warning ) ) { foreach ( $statifyblacklist_post_warning as $w ) { print '

    ' . - wp_kses( $w, [ 'br' => [] ] ) . + wp_kses( $w, array( 'br' => array() ) ) . '

    '; } print '

    ' . esc_html( 'Settings have not been saved yet.', 'statify-blacklist' ) . '

    '; From 253d2fadd2ff9ffcec6c7d63dcf4b6158a93f3d4 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 7 Jan 2020 19:26:51 +0100 Subject: [PATCH 33/42] use only supported PHP versions + 5.6 for CI builds --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index b6c3e11..79000c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,9 @@ language: php -dist: trusty php: - - '5.5' - '5.6' - - '7.0' - - '7.1' - '7.2' - '7.3' + - '7.4' before_script: - composer install script: From 736cec1d1205a797dd6436604f565e35ababd179 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 7 Jan 2020 19:27:47 +0100 Subject: [PATCH 34/42] fix use of esc_html__() for translated escape in settings page --- views/settings-page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/settings-page.php b/views/settings-page.php index 46ce8d0..e69d429 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -159,7 +159,7 @@ function ( $a ) { wp_kses( $w, array( 'br' => array() ) ) . '

    '; } - print '

    ' . esc_html( 'Settings have not been saved yet.', 'statify-blacklist' ) . '

    '; + print '

    ' . esc_html__( 'Settings have not been saved yet.', 'statify-blacklist' ) . '

    '; } if ( isset( $statifyblacklist_post_success ) ) { print '

    ' . From ac73b2316d7dabac88f0c246c8b3d09be9f251dc Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 10 May 2020 11:35:18 +0200 Subject: [PATCH 35/42] typo fixes --- README.md | 4 ++-- inc/class-statifyblacklist-admin.php | 2 +- statify-blacklist.php | 2 +- test/statifyblacklist-test.php | 2 +- views/settings-page.php | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 70099f3..a8c1ead 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ visitors from search engines, just "false" referers from 301 redirects or you ow 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 negligible compared to the total loading procedure. +Yes, but probably not noticeable. Checking a single referer string against a (usually small) list should be negligible 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? ### @@ -127,7 +127,7 @@ Because of this, an IP blacklist can only be applied while processing the reques ### 1.2.0 / 29.08.2016 ### * Switched from `in_array()` to faster `isset()` for referer checking -* Optional cron execiton implemented +* Optional cron execution implemented ### 1.1.2 / 17.08.2016 ### * Prepared for localization diff --git a/inc/class-statifyblacklist-admin.php b/inc/class-statifyblacklist-admin.php index 2644d4c..78a43fb 100644 --- a/inc/class-statifyblacklist-admin.php +++ b/inc/class-statifyblacklist-admin.php @@ -263,7 +263,7 @@ public static function cleanup_database() { global $wpdb; // Execute filter on database. - // @codingStandardsIgnoreStart These statements prouce warnings, rework in future release (TODO). + // @codingStandardsIgnoreStart These statements produce warnings, rework in future release (TODO). if ( ! empty( $referer_regexp ) ) { $wpdb->query( $wpdb->prepare( diff --git a/statify-blacklist.php b/statify-blacklist.php index 2e94b7c..e9488c5 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -55,7 +55,7 @@ // Autoload. spl_autoload_register( 'statify_blacklist_autoload' ); } else { - // Disbale plugin, if active. + // Disable plugin, if active. add_action( 'admin_init', 'statify_blacklist_disable' ); } diff --git a/test/statifyblacklist-test.php b/test/statifyblacklist-test.php index 16c6f41..a7a18e4 100644 --- a/test/statifyblacklist-test.php +++ b/test/statifyblacklist-test.php @@ -365,7 +365,7 @@ public function test_sanitize_ips() { $this->assertNotFalse( $result ); /* - * Unfortunately this is nencessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated + * Unfortunately this is necessary as long as we run PHP 5 tests, because "assertInternalType" is deprecated * as of PHPUnit 8, but "assertIsArray" has been introduces in PHPUnit 7.5 which requires PHP >= 7.1. */ if ( method_exists( $this, 'assertIsArray' ) ) { diff --git a/views/settings-page.php b/views/settings-page.php index e69d429..fed2ed4 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -177,7 +177,7 @@ function ( $a ) { @@ -257,7 +257,7 @@ function ( $a ) { @@ -341,7 +341,7 @@ function ( $a ) { From 4a6cc49fcecb9a1e9ed42f59d66a3a71cbe8a62a Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 10 May 2020 11:37:17 +0200 Subject: [PATCH 36/42] fix use of $options variable in settings view Underscore prefix has been removed from this field, but the change did not reach the settings page yet. --- views/settings-page.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/views/settings-page.php b/views/settings-page.php index fed2ed4..ec41d70 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -183,7 +183,7 @@ function ( $a ) { > + value="1" >

    @@ -197,7 +197,7 @@ function ( $a ) { > + value="1" >

    @@ -207,16 +207,16 @@ function ( $a ) { @@ -237,7 +237,7 @@ function ( $a ) {