Skip to content

Commit

Permalink
Autorelease
Browse files Browse the repository at this point in the history
 - add notices to some of the actions
 - upon activation, enable default rules
 - on deactivation remove block rules
  • Loading branch information
dz0ny committed Mar 11, 2015
1 parent acdf4f0 commit a98ea95
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tmp
build
.idea
*~
*.zip
28 changes: 16 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
language: php

secure: amBIQvCiCh/DsyOUTLXkMQ4EGj0TIRKyAb/qwIGKTIT3MEmXbUfnP5AXT3rRXmbtVc9oBUmpZmckPfooOOwv6AdC4w6qvjux2WXzsMPgDXt/IhoaxJaNFBKdkln90arWHclJbUs2XHhkGte+SI5sweOYK92Ida9d97nqwMZQGNk=
sudo: false
cache:
- vendor
- bin
php:
- 5.3
- 5.4
- 5.5
- 5.6

- 5.3
- 5.4
- 5.5
- 5.6
before_script:
- composer install
- mkdir -p build/logs
- touch /tmp/.htaccess

- composer install
- mkdir -p build/logs
- touch /tmp/.htaccess
script: bin/phpunit

after_script:
- bin/coveralls -v
- bin/coveralls -v
after_success:
- wget "https://github.com/aktau/github-release/releases/download/v0.5.3/linux-amd64-github-release.tar.bz2"
- tar xvf linux-amd64-github-release.tar.bz2
- make release
- make deploy
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
VERSION := 1.0.1

release:
cp -ar src spider_blocker
zip -r spider_blocker.zip spider_blocker
rm -rf spider_blocker
mv spider_blocker.zip build/

deploy:
-bin/linux/amd64/github-release delete -u niteoweb -r spiderblocker -t v$(VERSION)
-bin/linux/amd64/github-release delete -u niteoweb -r spiderblocker -t latest
bin/linux/amd64/github-release release -u niteoweb -r spiderblocker -t v$(VERSION)
bin/linux/amd64/github-release release -u niteoweb -r spiderblocker -t latest
bin/linux/amd64/github-release upload -u niteoweb -r spiderblocker -t v$(VERSION) -f build/spider_blocker.zip -n spider_blocker.zip
bin/linux/amd64/github-release upload -u niteoweb -r spiderblocker -t latest -f build/spider_blocker.zip -n spider_blocker.zip
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# SpiderBlocker

[![Build Status](https://travis-ci.org/niteoweb/spiderblocker.svg?branch=master)](https://travis-ci.org/niteoweb/spiderblocker)
[![Coverage Status](https://coveralls.io/repos/niteoweb/spiderblocker/badge.svg)](https://coveralls.io/r/niteoweb/spiderblocker)

## Plugin Name

Contributors: niteoweb
Expand All @@ -26,9 +29,3 @@ with Apache server and mod_rewrite enabled.
1. Upload 'spider-blocker' directory to the '/wp-content/plugins/' directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Go to **Tools** menu and then **SpiderBlocker* to configure

## Status

[![Build Status](https://travis-ci.org/niteoweb/spiderblocker.svg?branch=master)](https://travis-ci.org/niteoweb/spiderblocker)
[![Coverage Status](https://coveralls.io/repos/niteoweb/spiderblocker/badge.svg)](https://coveralls.io/r/niteoweb/spiderblocker)

166 changes: 139 additions & 27 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Plugin Name: Spider Blocker
* Description: Spider Blocker will block most common bots that consume bandwidth and slow down your server.
* Version: 1.0.0
* Version: 1.0.1
* Author: NiteoWeb Ltd.
* Author URI: www.niteoweb.com
*/
Expand All @@ -26,6 +26,7 @@ class NiteowebSpiderBlocker {

function __construct() {
if ( is_admin() ) {
add_action( 'admin_notices', array( &$this, 'activatePluginNotice' ) );
add_action( 'admin_menu', array( &$this, 'adminMenu' ) );
add_action( 'wp_ajax_NSB-get_list', array( &$this, 'loadList' ) );
add_action( 'wp_ajax_NSB-set_list', array( &$this, 'saveList' ) );
Expand All @@ -34,11 +35,22 @@ function __construct() {
add_action( 'generate_rewrite_rules', array( &$this, "generateRewriteRules" ) );
}

/**
* @codeCoverageIgnore
*/
static function isHtaccessWritable() {
$home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
$htaccess_file = $home_path . '.htaccess';

return is_writable( $htaccess_file );
}

function generateRewriteRules() {
global $wp_rewrite;

// Protect plugin from direct access
$wp_rewrite->add_external_rule( 'wp-content/plugins/spider_blocker/index.php', 'index.php%{REQUEST_URI}' );
$wp_rewrite->add_external_rule( 'wp-content/plugins/spider_blocker/readme.txt', 'index.php%{REQUEST_URI}' );
$wp_rewrite->add_external_rule( 'wp-content/plugins/spider_blocker/', 'index.php%{REQUEST_URI}' );
}

Expand All @@ -51,20 +63,26 @@ function adminMenu() {
);
}

function loadList() {
check_ajax_referer( $this->nonce, 'nonce' );
wp_send_json_success( $this->getBots() );
}

private function getBots() {
return maybe_unserialize( get_option( $this->option_name, $this->default_bots ) );
/**
* @codeCoverageIgnore
*/
function activatePluginNotice() {
if ( get_option( $this->option_name ) === false ) {
update_option( $this->option_name, $this->default_bots );
?>
<div class="notice notice-success">
<p>SpiderBlocker plugin has enabled blocking of some bots, please review settings by visiting <a
href="<?php echo admin_url( 'tools.php?page=ni_spider_block' ); ?>">Setting page</a>!</p>
</div>
<?php
}
}

function resetList() {
check_ajax_referer( $this->nonce, 'nonce' );
delete_option( $this->option_name );
/**
* @codeCoverageIgnore
*/
function activatePlugin() {
$this->generateBlockRules();
wp_send_json_success( $this->getBots() );
}

function generateBlockRules() {
Expand All @@ -87,7 +105,7 @@ static function modRewriteEnabled() {
}

/**
* Geneerate block rules based on enabled bots
* Generate block rules based on enabled bots
*
* @return array
*/
Expand All @@ -108,6 +126,37 @@ function getRules() {
return $list;
}

private function getBots() {
return maybe_unserialize( get_option( $this->option_name, $this->default_bots ) );
}

function loadList() {
check_ajax_referer( $this->nonce, 'nonce' );
wp_send_json_success( $this->getBots() );
}

function resetList() {
check_ajax_referer( $this->nonce, 'nonce' );
delete_option( $this->option_name );
$this->generateBlockRules();
wp_send_json_success( $this->getBots() );
}

function removeBlockRules() {
global $wp_rewrite;
delete_option( $this->option_name );
$home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
$htaccess_file = $home_path . '.htaccess';
$empty = array();
if ( ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) ) || is_writable( $htaccess_file ) ) {
if ( $this->modRewriteEnabled() ) {
insert_with_markers( $htaccess_file, 'NiteowebSpiderBlocker', $empty );
}
}

$wp_rewrite->flush_rules();
}

function saveList() {

check_ajax_referer( $this->nonce, 'nonce' );
Expand Down Expand Up @@ -139,6 +188,12 @@ function viewHandler() {
add_thickbox();
?>
<style>
.notice.fixed {
position: fixed;
right: 1em;
top: 3.5em;
}

tr.active {
background-color: rgba(54, 204, 255, 0.05);
}
Expand All @@ -147,9 +202,22 @@ function viewHandler() {
border-left: 4px solid #2ea2cc;
}
</style>

<h1>Spider Blocker</h1>
<hr/>
<div ng-app="spiderBlockApp">
<div ng-controller="NotificationsCtrl">
<div class="notice notice-{{ n.state }} fixed" ng-repeat="n in notifications"
style="top: {{3.5*($index+1)}}em">
<p>{{n.msg}}
<a ng-click="removeNotification(notification)">
<span class="dashicons dashicons-no-alt"></span>
</a>
</p>
</div>
</div>


<div ng-controller="BotListCtrl">
<h2>Add New Bot</h2>

Expand Down Expand Up @@ -276,7 +344,25 @@ function out(data) {
}
};
});
spiderBlockApp.controller('BotListCtrl', function ($scope, $http) {
spiderBlockApp.controller('NotificationsCtrl', function ($scope, $rootScope, $timeout) {
$scope.notifications = [];

$rootScope.$on('notification', function (event, data) {
$scope.notifications.push(data);
$timeout(function () {
$scope.removeNotification(data);
}, 3000);
});

$scope.removeNotification = function (notification) {
var index;
if ($scope.notifications !== undefined) {
index = $scope.notifications.indexOf(notification);
$scope.notifications.splice(index, 1);
}
}
});
spiderBlockApp.controller('BotListCtrl', function ($scope, $http, $rootScope) {
var wp_ajax = function (_req) {
_req.nonce = '<?php echo wp_create_nonce($this->nonce); ?>';
return $http({
Expand Down Expand Up @@ -311,8 +397,12 @@ function out(data) {
}).success(function (res) {
if (res.success) {
$scope.bots = res.data;
$rootScope.$emit('notification', {
state: 'success',
msg: 'List of bots was saved and new blocklist applied!'
});
} else {
alert(res.data);
$rootScope.$emit('notification', {state: 'errror', msg: res.data});
}
});
};
Expand All @@ -322,16 +412,24 @@ function out(data) {
action: 'NSB-reset_list'
}).success(function (res) {
$scope.bots = res.data;
$rootScope.$emit('notification', {
state: 'success',
msg: 'List of bots was reset to defaults!'
});
});
};

$scope.add = function () {
$scope.bots.push($scope.bot);
$rootScope.$emit('notification', {
state: 'success',
msg: 'Bot ' + $scope.bot.name + ' was added!'
});
$scope.bot = {"state": true};
};

$scope.remove = function (at) {
console.info(find_bot(at));
$rootScope.$emit('notification', {state: 'success', msg: 'Bot was removed!'});
$scope.bots.splice(find_bot(at), 1);
};
});
Expand All @@ -342,19 +440,33 @@ function out(data) {
}
}

if ( ! apache_get_version() || ! NiteowebSpiderBlocker::modRewriteEnabled() ) {
?>
<div id="error-page">
<p>This plugin requires Apache2 server with mod_rewrite support. Please contact your hosting provider about
upgrading your server software. Your Apache version is <b><?php echo apache_get_version(); ?></b></p>
</div>
<?php
die();
}

// Inside WordPress
if ( defined( 'ABSPATH' ) ) {
new NiteowebSpiderBlocker;
if ( ! apache_get_version() || ! NiteowebSpiderBlocker::modRewriteEnabled() ) {
?>
<div id="error-page">
<p>This plugin requires Apache2 server with mod_rewrite support. Please contact your hosting provider
about
upgrading your server software. Your Apache version is <b><?php echo apache_get_version(); ?></b>
</p>
</div>
<?php
die();
}

if ( ! NiteowebSpiderBlocker::isHtaccessWritable() ) {
?>
<div id="error-page">
<p>This plugin requires <b>.htaccess</b> file that is writable by the server. Please enable write access
for file <b><?php echo ABSPATH . '.htaccess'; ?></b>.</p>
</div>
<?php
die();
}

$NiteowebSpiderBlocker_ins = new NiteowebSpiderBlocker;
register_activation_hook( __FILE__, array( &$NiteowebSpiderBlocker_ins, 'activatePlugin' ) );
register_deactivation_hook( __FILE__, array( &$NiteowebSpiderBlocker_ins, 'removeBlockRules' ) );
}


Expand Down
6 changes: 3 additions & 3 deletions src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ using Apache .htaccess file to minimize impact on your website. It will also hid
with Apache server and mod_rewrite enabled.

= Plugin Features =
* **Block Unlimited bots from viewing your site**
* **Easy Export/Import rules (comes with most common list of bots)**
* **Zero Footprint**
* Block Unlimited bots from viewing your site
* Easy Export/Import rules (comes with most common list of bots)
* Zero Footprint

== Installation ==

Expand Down
Loading

0 comments on commit a98ea95

Please sign in to comment.