Version Description
Download this release
Release Info
Developer | niteoweb |
Plugin | Spider Blocker |
Version | 1.0.6 |
Comparing to | |
See all releases |
Code changes from version 1.0.4 to 1.0.6
index.php
CHANGED
@@ -3,378 +3,503 @@ namespace Niteoweb\SpiderBlocker;
|
|
3 |
/**
|
4 |
* Plugin Name: Spider Blocker
|
5 |
* Description: Spider Blocker will block most common bots that consume bandwidth and slow down your server.
|
6 |
-
* Version: 1.0.
|
7 |
-
* Runtime: 5.3
|
8 |
-
* Author:
|
9 |
-
* Author URI: www.
|
10 |
*/
|
11 |
|
12 |
-
if (
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
}
|
21 |
|
22 |
|
23 |
-
class SpiderBlocker
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
<textarea
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
|
|
|
|
373 |
}
|
374 |
|
375 |
// Inside WordPress
|
376 |
-
if (defined('ABSPATH')) {
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
}
|
3 |
/**
|
4 |
* Plugin Name: Spider Blocker
|
5 |
* Description: Spider Blocker will block most common bots that consume bandwidth and slow down your server.
|
6 |
+
* Version: 1.0.6
|
7 |
+
* Runtime: 5.3+
|
8 |
+
* Author: Easy Blog Networks
|
9 |
+
* Author URI: www.easyblognetworks.com
|
10 |
*/
|
11 |
|
12 |
+
if ( ! function_exists( 'apache_get_version' ) ) {
|
13 |
+
function apache_get_version() {
|
14 |
+
if ( stristr( $_ENV["SERVER_SOFTWARE"], 'Apache' ) ) {
|
15 |
+
return $_ENV["SERVER_SOFTWARE"];
|
16 |
+
}
|
17 |
+
|
18 |
+
return false;
|
19 |
+
}
|
20 |
+
}
|
21 |
+
|
22 |
+
if ( version_compare( PHP_VERSION, '5.3.0', '<' ) ) {
|
23 |
+
?>
|
24 |
+
<div id="error-page">
|
25 |
+
<p>This plugin requires PHP 5.3.0 or higher. Please contact your hosting provider about upgrading your
|
26 |
+
server software. Your PHP version is <b><?php echo PHP_VERSION; ?></b></p>
|
27 |
+
</div>
|
28 |
+
<?php
|
29 |
+
die();
|
30 |
}
|
31 |
|
32 |
|
33 |
+
class SpiderBlocker {
|
34 |
+
|
35 |
+
public $default_bots = 'a:36:{i:0;O:8:"stdClass":4:{s:4:"name";s:10:"Ahrefs Bot";s:2:"re";s:9:"AhrefsBot";s:4:"desc";s:25:"https://ahrefs.com/robot/";s:5:"state";b:1;}i:1;O:8:"stdClass":4:{s:4:"name";s:8:"MJ12 bot";s:2:"re";s:7:"MJ12bot";s:4:"desc";s:56:"http://www.majestic12.co.uk/projects/dsearch/mj12bot.php";s:5:"state";b:1;}i:2;O:8:"stdClass":4:{s:4:"name";s:9:"Roger Bot";s:2:"re";s:8:"Rogerbot";s:4:"desc";s:40:"http://moz.com/help/pro/rogerbot-crawler";s:5:"state";b:1;}i:3;O:8:"stdClass":4:{s:4:"name";s:11:"Semrush Bot";s:2:"re";s:10:"SemrushBot";s:4:"desc";s:31:"http://www.semrush.com/bot.html";s:5:"state";b:1;}i:4;O:8:"stdClass":4:{s:4:"name";s:11:"ia_archiver";s:2:"re";s:11:"ia_archiver";s:4:"desc";s:36:"http://archive.org/about/exclude.php";s:5:"state";b:1;}i:5;O:8:"stdClass":4:{s:4:"name";s:8:"ScoutJet";s:2:"re";s:8:"ScoutJet";s:4:"desc";s:19:"http://scoutjet.com";s:5:"state";b:1;}i:6;O:8:"stdClass":4:{s:4:"name";s:7:"sistrix";s:2:"re";s:7:"sistrix";s:4:"desc";s:26:"http://crawler.sistrix.net";s:5:"state";b:1;}i:7;O:8:"stdClass":4:{s:4:"name";s:16:"SearchmetricsBot";s:2:"re";s:16:"SearchmetricsBot";s:4:"desc";s:50:"http://www.searchmetrics.com/en/searchmetrics-bot/";s:5:"state";b:1;}i:8;O:8:"stdClass":4:{s:4:"name";s:14:"SEOkicks-Robot";s:2:"re";s:14:"SEOkicks-Robot";s:4:"desc";s:33:"http://www.seokicks.de/robot.html";s:5:"state";b:1;}i:9;O:8:"stdClass":4:{s:4:"name";s:16:"Lipperhey Spider";s:2:"re";s:16:"Lipperhey Spider";s:4:"desc";s:43:"http://www.lipperhey.com/en/website-spider/";s:5:"state";b:1;}i:10;O:8:"stdClass":4:{s:4:"name";s:6:"Exabot";s:2:"re";s:6:"Exabot";s:4:"desc";s:44:"http://www.exalead.com/search/webmasterguide";s:5:"state";b:1;}i:11;O:8:"stdClass":4:{s:4:"name";s:6:"NC Bot";s:2:"re";s:5:"NCBot";s:4:"desc";s:55:"https://twitter.com/NetComber/status/334476871691550721";s:5:"state";b:1;}i:12;O:8:"stdClass":4:{s:4:"name";s:15:"BacklinkCrawler";s:2:"re";s:15:"BacklinkCrawler";s:4:"desc";s:40:"http://www.backlinktest.com/crawler.html";s:5:"state";b:1;}i:13;O:8:"stdClass":4:{s:4:"name";s:15:"archive.org Bot";s:2:"re";s:15:"archive.org_bot";s:4:"desc";s:42:"http://archive.org/details/archive.org_bot";s:5:"state";b:1;}i:14;O:8:"stdClass":4:{s:4:"name";s:12:"MeanPath Bot";s:2:"re";s:11:"meanpathbot";s:4:"desc";s:37:"https://meanpath.com/meanpathbot.html";s:5:"state";b:1;}i:15;O:8:"stdClass":4:{s:4:"name";s:18:"PagesInventory Bot";s:2:"re";s:14:"PagesInventory";s:4:"desc";s:56:"http://www.botsvsbrowsers.com/details/1002332/index.html";s:5:"state";b:1;}i:16;O:8:"stdClass":4:{s:4:"name";s:12:"Aboundex Bot";s:2:"re";s:11:"Aboundexbot";s:4:"desc";s:32:"http://www.aboundex.com/crawler/";s:5:"state";b:1;}i:17;O:8:"stdClass":4:{s:4:"name";s:15:"SeoProfiler Bot";s:2:"re";s:5:"spbot";s:4:"desc";s:31:"http://www.seoprofiler.com/bot/";s:5:"state";b:1;}i:18;O:8:"stdClass":4:{s:4:"name";s:11:"Linkdex Bot";s:2:"re";s:10:"linkdexbot";s:4:"desc";s:34:"http://www.linkdex.com/about/bots/";s:5:"state";b:1;}i:19;O:8:"stdClass":4:{s:4:"name";s:7:"Gigabot";s:2:"re";s:7:"Gigabot";s:4:"desc";s:45:"http://www.useragentstring.com/pages/Gigabot/";s:5:"state";b:1;}i:20;O:8:"stdClass":4:{s:4:"name";s:6:"DotBot";s:2:"re";s:6:"dotbot";s:4:"desc";s:35:"http://en.wikipedia.org/wiki/DotBot";s:5:"state";b:1;}i:21;O:8:"stdClass":4:{s:4:"name";s:5:"Nutch";s:2:"re";s:5:"Nutch";s:4:"desc";s:32:"http://nutch.apache.org/bot.html";s:5:"state";b:1;}i:22;O:8:"stdClass":4:{s:4:"name";s:8:"BLEX Bot";s:2:"re";s:7:"BLEXBot";s:4:"desc";s:27:"http://webmeup-crawler.com/";s:5:"state";b:1;}i:23;O:8:"stdClass":4:{s:4:"name";s:6:"Ezooms";s:2:"re";s:6:"Ezooms";s:4:"desc";s:49:"http://graphicline.co.za/blogs/what-is-ezooms-bot";s:5:"state";b:1;}i:24;O:8:"stdClass":4:{s:4:"name";s:11:"Majestic 12";s:2:"re";s:11:"Majestic-12";s:4:"desc";s:56:"http://www.majestic12.co.uk/projects/dsearch/mj12bot.php";s:5:"state";b:1;}i:25;O:8:"stdClass":4:{s:4:"name";s:12:"Majestic SEO";s:2:"re";s:12:"Majestic-SEO";s:4:"desc";s:56:"http://www.majestic12.co.uk/projects/dsearch/mj12bot.php";s:5:"state";b:1;}i:26;O:8:"stdClass":4:{s:4:"name";s:7:"DSearch";s:2:"re";s:7:"DSearch";s:4:"desc";s:56:"http://www.majestic12.co.uk/projects/dsearch/mj12bot.php";s:5:"state";b:1;}i:27;O:8:"stdClass":4:{s:4:"name";s:10:"Blekko Bot";s:2:"re";s:9:"BlekkoBot";s:4:"desc";s:33:"http://blekko.com/about/blekkobot";s:5:"state";b:1;}i:28;O:8:"stdClass":4:{s:4:"name";s:6:"Yandex";s:2:"re";s:6:"Yandex";s:4:"desc";s:41:"http://help.yandex.com/search/?id=1112030";s:5:"state";b:0;}i:29;O:8:"stdClass":4:{s:4:"name";s:10:"Google Bot";s:2:"re";s:9:"googlebot";s:4:"desc";s:57:"https://support.google.com/webmasters/answer/182072?hl=en";s:5:"state";b:0;}i:30;O:8:"stdClass":4:{s:4:"name";s:18:"Feedfetcher Google";s:2:"re";s:18:"Feedfetcher-Google";s:4:"desc";s:51:"https://support.google.com/webmasters/answer/178852";s:5:"state";b:0;}i:31;O:8:"stdClass":4:{s:4:"name";s:8:"Bing Bot";s:2:"re";s:7:"BingBot";s:4:"desc";s:36:"http://en.wikipedia.org/wiki/Bingbot";s:5:"state";b:0;}i:32;O:8:"stdClass":4:{s:4:"name";s:9:"Nerdy Bot";s:2:"re";s:8:"NerdyBot";s:4:"desc";s:20:"http://nerdybot.com/";s:5:"state";b:1;}i:33;O:8:"stdClass":4:{s:4:"name";s:9:"James BOT";s:2:"re";s:8:"JamesBOT";s:4:"desc";s:32:"http://cognitiveseo.com/bot.html";s:5:"state";b:1;}i:34;O:8:"stdClass":4:{s:4:"name";s:7:"Tin Eye";s:2:"re";s:6:"TinEye";s:4:"desc";s:34:"http://www.tineye.com/crawler.html";s:5:"state";b:1;}i:35;O:8:"stdClass":4:{s:5:"state";b:1;s:2:"re";s:11:"Baiduspider";s:4:"name";s:5:"Baidu";s:4:"desc";s:47:"http://www.baidu.com/search/robots_english.html";}}';
|
36 |
+
protected $option_name = 'Niteoweb.SpiderBlocker.Bots';
|
37 |
+
protected $nonce = 'Niteoweb.SpiderBlocker.Nonce';
|
38 |
+
|
39 |
+
function __construct() {
|
40 |
+
if ( is_admin() ) {
|
41 |
+
add_action( 'admin_notices', array( &$this, 'activatePluginNotice' ) );
|
42 |
+
add_action( 'admin_menu', array( &$this, 'adminMenu' ) );
|
43 |
+
add_action( 'wp_ajax_NSB-get_list', array( &$this, 'loadList' ) );
|
44 |
+
add_action( 'wp_ajax_NSB-set_list', array( &$this, 'saveList' ) );
|
45 |
+
add_action( 'wp_ajax_NSB-reset_list', array( &$this, 'resetList' ) );
|
46 |
+
}
|
47 |
+
add_action( 'generate_rewrite_rules', array( &$this, "generateRewriteRules" ) );
|
48 |
+
|
49 |
+
}
|
50 |
+
|
51 |
+
function generateRewriteRules() {
|
52 |
+
global $wp_rewrite;
|
53 |
+
|
54 |
+
// Protect plugin from direct access
|
55 |
+
$wp_rewrite->add_external_rule( $this->pluginURL() . 'index.php', 'index.php%{REQUEST_URI}' );
|
56 |
+
$wp_rewrite->add_external_rule( $this->pluginURL() . 'readme.txt', 'index.php%{REQUEST_URI}' );
|
57 |
+
$wp_rewrite->add_external_rule( $this->pluginURL(), 'index.php%{REQUEST_URI}' );
|
58 |
+
}
|
59 |
+
|
60 |
+
/**
|
61 |
+
* @codeCoverageIgnore
|
62 |
+
* @return string
|
63 |
+
*/
|
64 |
+
private function pluginURL() {
|
65 |
+
$url = wp_make_link_relative( plugin_dir_url( __FILE__ ) );
|
66 |
+
$url = ltrim( $url, "/" );
|
67 |
+
|
68 |
+
return $url;
|
69 |
+
}
|
70 |
+
|
71 |
+
function adminMenu() {
|
72 |
+
add_management_page(
|
73 |
+
'SpiderBlocker', 'SpiderBlocker', 'manage_options', 'ni_spider_block', array(
|
74 |
+
&$this,
|
75 |
+
'viewHandler'
|
76 |
+
)
|
77 |
+
);
|
78 |
+
}
|
79 |
+
|
80 |
+
/**
|
81 |
+
* @codeCoverageIgnore
|
82 |
+
*/
|
83 |
+
function activatePluginNotice() {
|
84 |
+
if ( get_option( $this->option_name ) === false ) {
|
85 |
+
update_option( $this->option_name, $this->default_bots );
|
86 |
+
?>
|
87 |
+
<div class="notice notice-success">
|
88 |
+
<p>SpiderBlocker plugin has enabled blocking of some bots, please review settings by visiting <a
|
89 |
+
href="<?php echo admin_url( 'tools.php?page=ni_spider_block' ); ?>">Setting page</a>!</p>
|
90 |
+
</div>
|
91 |
+
<?php
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
/**
|
96 |
+
* @codeCoverageIgnore
|
97 |
+
*/
|
98 |
+
function activatePlugin() {
|
99 |
+
if ( ! apache_get_version() ) {
|
100 |
+
?>
|
101 |
+
<div id="error-page">
|
102 |
+
<p>This plugin requires Apache2 server with mod_rewrite support. Please contact your hosting provider
|
103 |
+
about
|
104 |
+
upgrading your server software. Your Apache version is <b><?php echo apache_get_version(); ?></b>
|
105 |
+
</p>
|
106 |
+
</div>
|
107 |
+
<?php
|
108 |
+
die();
|
109 |
+
}
|
110 |
+
|
111 |
+
if ( ! SpiderBlocker::isHtaccessWritable() ) {
|
112 |
+
$state = SpiderBlocker::chmodHtaccess();
|
113 |
+
if ( ! SpiderBlocker::isHtaccessWritable() || ! $state ) {
|
114 |
+
?>
|
115 |
+
<div id="error-page">
|
116 |
+
<p>This plugin requires <b>.htaccess</b> file that is writable by the server. Please enable write
|
117 |
+
access
|
118 |
+
for file <b><?php echo ABSPATH . '.htaccess'; ?></b>.</p>
|
119 |
+
</div>
|
120 |
+
<?php
|
121 |
+
die();
|
122 |
+
}
|
123 |
+
}
|
124 |
+
$this->generateBlockRules();
|
125 |
+
|
126 |
+
}
|
127 |
+
|
128 |
+
/**
|
129 |
+
* @codeCoverageIgnore
|
130 |
+
* @return bool
|
131 |
+
*/
|
132 |
+
static function isHtaccessWritable() {
|
133 |
+
$home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
|
134 |
+
$htaccess_file = $home_path . '.htaccess';
|
135 |
+
|
136 |
+
return is_writable( $htaccess_file );
|
137 |
+
}
|
138 |
+
|
139 |
+
/**
|
140 |
+
* @codeCoverageIgnore
|
141 |
+
*
|
142 |
+
* @param int $mod octet value for chmod
|
143 |
+
*
|
144 |
+
* @return bool
|
145 |
+
*/
|
146 |
+
static function chmodHtaccess( $mod = 0644 ) {
|
147 |
+
$home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
|
148 |
+
$htaccess_file = $home_path . '.htaccess';
|
149 |
+
|
150 |
+
return chmod( $htaccess_file, $mod );
|
151 |
+
}
|
152 |
+
|
153 |
+
function generateBlockRules() {
|
154 |
+
global $wp_rewrite;
|
155 |
+
|
156 |
+
$home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
|
157 |
+
$htaccess_file = $home_path . '.htaccess';
|
158 |
+
|
159 |
+
if ( ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) ) || is_writable( $htaccess_file ) ) {
|
160 |
+
insert_with_markers( $htaccess_file, 'NiteowebSpiderBlocker', $this->getRules() );
|
161 |
+
}
|
162 |
+
|
163 |
+
$wp_rewrite->flush_rules();
|
164 |
+
}
|
165 |
+
|
166 |
+
/**
|
167 |
+
* Generate block rules based on enabled bots
|
168 |
+
*
|
169 |
+
* @return array
|
170 |
+
*/
|
171 |
+
function getRules() {
|
172 |
+
$list = array();
|
173 |
+
foreach ( $this->getBots() as $bot ) {
|
174 |
+
if ( $bot->state ) {
|
175 |
+
$list[] = 'SetEnvIfNoCase User-Agent "' . $bot->re . '" block_bot';
|
176 |
+
}
|
177 |
+
|
178 |
+
}
|
179 |
+
$list[] = "<Limit GET POST HEAD>";
|
180 |
+
$list[] = "Order Allow,Deny";
|
181 |
+
$list[] = "Allow from all";
|
182 |
+
$list[] = "Deny from env=block_bot";
|
183 |
+
$list[] = "</Limit>";
|
184 |
+
|
185 |
+
return $list;
|
186 |
+
}
|
187 |
+
|
188 |
+
private function getBots() {
|
189 |
+
return maybe_unserialize( get_option( $this->option_name, $this->default_bots ) );
|
190 |
+
}
|
191 |
+
|
192 |
+
function loadList() {
|
193 |
+
check_ajax_referer( $this->nonce, 'nonce' );
|
194 |
+
wp_send_json_success( $this->getBots() );
|
195 |
+
}
|
196 |
+
|
197 |
+
function resetList() {
|
198 |
+
check_ajax_referer( $this->nonce, 'nonce' );
|
199 |
+
delete_option( $this->option_name );
|
200 |
+
$this->generateBlockRules();
|
201 |
+
wp_send_json_success( $this->getBots() );
|
202 |
+
}
|
203 |
+
|
204 |
+
function removeBlockRules() {
|
205 |
+
global $wp_rewrite;
|
206 |
+
delete_option( $this->option_name );
|
207 |
+
$home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
|
208 |
+
$htaccess_file = $home_path . '.htaccess';
|
209 |
+
$empty = array();
|
210 |
+
if ( ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) ) || is_writable( $htaccess_file ) ) {
|
211 |
+
insert_with_markers( $htaccess_file, 'NiteowebSpiderBlocker', $empty );
|
212 |
+
}
|
213 |
+
|
214 |
+
$wp_rewrite->flush_rules();
|
215 |
+
}
|
216 |
+
|
217 |
+
function saveList() {
|
218 |
+
|
219 |
+
check_ajax_referer( $this->nonce, 'nonce' );
|
220 |
+
$data = json_decode( stripcslashes( $_POST['data'] ) );
|
221 |
+
|
222 |
+
if ( json_last_error() ) {
|
223 |
+
if ( function_exists( 'json_last_error_msg' ) ) {
|
224 |
+
wp_send_json_error( json_last_error_msg() );
|
225 |
+
} else {
|
226 |
+
wp_send_json_error( 'Failed parsing JSON' );
|
227 |
+
}
|
228 |
+
|
229 |
+
}
|
230 |
+
if ( get_option( $this->option_name ) !== false ) {
|
231 |
+
update_option( $this->option_name, maybe_serialize( $data ) );
|
232 |
+
} else {
|
233 |
+
add_option( $this->option_name, maybe_serialize( $data ), null, 'no' );
|
234 |
+
}
|
235 |
+
|
236 |
+
$this->generateBlockRules();
|
237 |
+
wp_send_json_success( $this->getBots() );
|
238 |
+
|
239 |
+
}
|
240 |
+
|
241 |
+
/**
|
242 |
+
* @codeCoverageIgnore
|
243 |
+
*/
|
244 |
+
function viewHandler() {
|
245 |
+
|
246 |
+
add_thickbox();
|
247 |
+
var_dump($this->pluginURL());
|
248 |
+
?>
|
249 |
+
<style>
|
250 |
+
.notice.fixed {
|
251 |
+
position: fixed;
|
252 |
+
right: 1em;
|
253 |
+
top: 3.5em;
|
254 |
+
}
|
255 |
+
|
256 |
+
tr.active {
|
257 |
+
background-color: rgba(54, 204, 255, 0.05);
|
258 |
+
}
|
259 |
+
|
260 |
+
.active th.bot-re {
|
261 |
+
border-left: 4px solid #2ea2cc;
|
262 |
+
}
|
263 |
+
</style>
|
264 |
+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js" type="text/javascript"></script>
|
265 |
+
<script>
|
266 |
+
window.sb_nonce = "<?php echo wp_create_nonce($this->nonce); ?>";
|
267 |
+
-(function () {
|
268 |
+
var spiderBlockApp = angular.module('spiderBlockApp', []);
|
269 |
+
|
270 |
+
spiderBlockApp.directive('jsonText', function () {
|
271 |
+
return {
|
272 |
+
restrict: 'A',
|
273 |
+
require: 'ngModel',
|
274 |
+
link: function (scope, element, attr, ngModel) {
|
275 |
+
function into(input) {
|
276 |
+
return angular.fromJson(input);
|
277 |
+
}
|
278 |
+
|
279 |
+
function out(data) {
|
280 |
+
return angular.toJson(data, true);
|
281 |
+
}
|
282 |
+
|
283 |
+
ngModel.$parsers.push(into);
|
284 |
+
ngModel.$formatters.push(out);
|
285 |
+
}
|
286 |
+
};
|
287 |
+
});
|
288 |
+
spiderBlockApp.controller('NotificationsCtrl', function ($scope, $rootScope, $timeout) {
|
289 |
+
$scope.notifications = [];
|
290 |
+
|
291 |
+
$rootScope.$on('notification', function (event, data) {
|
292 |
+
$scope.notifications.push(data);
|
293 |
+
$timeout(function () {
|
294 |
+
$scope.removeNotification(data);
|
295 |
+
}, 3000);
|
296 |
+
});
|
297 |
+
|
298 |
+
$scope.removeNotification = function (notification) {
|
299 |
+
var index;
|
300 |
+
if ($scope.notifications !== undefined) {
|
301 |
+
index = $scope.notifications.indexOf(notification);
|
302 |
+
$scope.notifications.splice(index, 1);
|
303 |
+
}
|
304 |
+
}
|
305 |
+
});
|
306 |
+
spiderBlockApp.controller('BotListCtrl', function ($scope, $http, $rootScope) {
|
307 |
+
var wp_ajax = function (_req) {
|
308 |
+
_req.nonce = window.sb_nonce;
|
309 |
+
return $http({
|
310 |
+
method: 'POST',
|
311 |
+
url: ajaxurl,
|
312 |
+
data: jQuery.param(_req),
|
313 |
+
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
|
314 |
+
})
|
315 |
+
};
|
316 |
+
|
317 |
+
var find_bot = function (re) {
|
318 |
+
for (var i = $scope.bots.length - 1; i >= 0; i--) {
|
319 |
+
if ($scope.bots[i]['re'] == re) {
|
320 |
+
return i;
|
321 |
+
}
|
322 |
+
}
|
323 |
+
return null;
|
324 |
+
};
|
325 |
+
|
326 |
+
$scope.bot = {"state": true};
|
327 |
+
|
328 |
+
wp_ajax({
|
329 |
+
action: 'NSB-get_list'
|
330 |
+
}).success(function (res) {
|
331 |
+
$scope.bots = res.data;
|
332 |
+
});
|
333 |
+
|
334 |
+
$scope.save = function () {
|
335 |
+
wp_ajax({
|
336 |
+
action: 'NSB-set_list',
|
337 |
+
data: angular.toJson($scope.bots)
|
338 |
+
}).success(function (res) {
|
339 |
+
if (res.success) {
|
340 |
+
$scope.bots = res.data;
|
341 |
+
$rootScope.$emit('notification', {
|
342 |
+
state: 'success',
|
343 |
+
msg: 'List of bots was saved and new blocklist applied!'
|
344 |
+
});
|
345 |
+
} else {
|
346 |
+
$rootScope.$emit('notification', {state: 'errror', msg: res.data});
|
347 |
+
}
|
348 |
+
});
|
349 |
+
};
|
350 |
+
|
351 |
+
$scope.reset = function () {
|
352 |
+
wp_ajax({
|
353 |
+
action: 'NSB-reset_list'
|
354 |
+
}).success(function (res) {
|
355 |
+
$scope.bots = res.data;
|
356 |
+
$rootScope.$emit('notification', {
|
357 |
+
state: 'success',
|
358 |
+
msg: 'List of bots was reset to defaults!'
|
359 |
+
});
|
360 |
+
});
|
361 |
+
};
|
362 |
+
|
363 |
+
$scope.add = function () {
|
364 |
+
$scope.bots.push($scope.bot);
|
365 |
+
$rootScope.$emit('notification', {
|
366 |
+
state: 'success',
|
367 |
+
msg: 'Bot ' + $scope.bot.name + ' was added!'
|
368 |
+
});
|
369 |
+
$scope.bot = {"state": true};
|
370 |
+
};
|
371 |
+
|
372 |
+
$scope.remove = function (at) {
|
373 |
+
$rootScope.$emit('notification', {state: 'success', msg: 'Bot was removed!'});
|
374 |
+
$scope.bots.splice(find_bot(at), 1);
|
375 |
+
};
|
376 |
+
});
|
377 |
+
})(angular, document, jQuery);
|
378 |
+
</script>
|
379 |
+
<h1>Spider Blocker</h1>
|
380 |
+
<hr/>
|
381 |
+
<div ng-app="spiderBlockApp">
|
382 |
+
<div ng-controller="NotificationsCtrl">
|
383 |
+
<div class="notice notice-{{ n.state }} fixed" ng-repeat="n in notifications"
|
384 |
+
style="top: {{3.5*($index+1)}}em">
|
385 |
+
<p>{{n.msg}}
|
386 |
+
<a ng-click="removeNotification(notification)">
|
387 |
+
<span class="dashicons dashicons-no-alt"></span>
|
388 |
+
</a>
|
389 |
+
</p>
|
390 |
+
</div>
|
391 |
+
</div>
|
392 |
+
|
393 |
+
|
394 |
+
<div ng-controller="BotListCtrl">
|
395 |
+
<h2>Add New Bot</h2>
|
396 |
+
|
397 |
+
<form name="add_form" ng-submit="add()">
|
398 |
+
<table class="form-table">
|
399 |
+
<tbody>
|
400 |
+
<tr>
|
401 |
+
<th scope="row"><label>User Agent</label></th>
|
402 |
+
<td><input bots="bots" ng-model='bot.re' class="regular-text"
|
403 |
+
required/></td>
|
404 |
+
</tr>
|
405 |
+
<tr>
|
406 |
+
<th scope="row"><label>Bot Name</label></th>
|
407 |
+
<td><input type="text" ng-model='bot.name' class="regular-text" required/></td>
|
408 |
+
</tr>
|
409 |
+
<tr>
|
410 |
+
<tr>
|
411 |
+
<th scope="row"><label>Bot Description URL</label></th>
|
412 |
+
<td><input type="url" ng-model='bot.desc' class="regular-text" placeholder="http://"/>
|
413 |
+
</td>
|
414 |
+
</tr>
|
415 |
+
</tbody>
|
416 |
+
</table>
|
417 |
+
<p class="submit"><input ng-disabled="add_form.$invalid" type="submit"
|
418 |
+
class="button button-primary" value="Add Bot"></p>
|
419 |
+
</form>
|
420 |
+
<h2>List of bots</h2>
|
421 |
+
<ng-form class="search-box">
|
422 |
+
<input size="35" type="search" id="ua-search-input" ng-model="query" placeholder="Filter...">
|
423 |
+
</ng-form>
|
424 |
+
<table class="wp-list-table widefat bots">
|
425 |
+
<thead>
|
426 |
+
<tr>
|
427 |
+
|
428 |
+
<th scope="col" class="manage-column column-description">
|
429 |
+
<a href=""
|
430 |
+
ng-click="predicate = 're'; reverse=false">User
|
431 |
+
Agent <span class="dashicons dashicons-sort"></span></a></th>
|
432 |
+
|
433 |
+
<th scope="col" class="manage-column column-name">Name</th>
|
434 |
+
<th scope="col" class="manage-column column-state">
|
435 |
+
<a href=""
|
436 |
+
ng-click="predicate = 'state'; reverse=false">State <span
|
437 |
+
class="dashicons dashicons-sort"></span></a>
|
438 |
+
</th>
|
439 |
+
<th scope="col" id="action" class="manage-column column-action">Action</th>
|
440 |
+
</tr>
|
441 |
+
</thead>
|
442 |
+
|
443 |
+
<tfoot>
|
444 |
+
<tr>
|
445 |
+
|
446 |
+
<th scope="col" class="manage-column column-description"><a href=""
|
447 |
+
ng-click="predicate = 're'; reverse=false">User
|
448 |
+
Agent</a></th>
|
449 |
+
|
450 |
+
<th scope="col" class="manage-column column-name">Name</th>
|
451 |
+
<th scope="col" class="manage-column column-state"><a href=""
|
452 |
+
ng-click="predicate = 'state'; reverse=false">State</a>
|
453 |
+
</th>
|
454 |
+
<th scope="col" id="action" class="manage-column column-action">Action</th>
|
455 |
+
</tr>
|
456 |
+
</tfoot>
|
457 |
+
|
458 |
+
<tbody id="the-list">
|
459 |
+
<tr id="spider-blocker" ng-repeat="bot in bots | filter:query | orderBy:predicate:reverse"
|
460 |
+
ng-class="{'active': bot.state}">
|
461 |
+
|
462 |
+
<th class="bot-re"> {{ bot.re }}</th>
|
463 |
+
<td class="bot-title"><strong>{{ bot.name }}</strong> <a target="_blank"
|
464 |
+
ng-href="{{bot.desc}}">{{
|
465 |
+
bot.desc }}</a></td>
|
466 |
+
<th class="expression" ng-class="{'blocked':bot.state}"> {{ bot.state?"Blocked":"Allowed"
|
467 |
+
}}
|
468 |
+
</th>
|
469 |
+
<td class="actions">
|
470 |
+
<input ng-hide="bot.state" type="button" ng-click="bot.state=true"
|
471 |
+
class="button button-primary" value="Block">
|
472 |
+
<input ng-show="bot.state" type="button" ng-click="bot.state=false"
|
473 |
+
class="button button-secondary" value="Allow">
|
474 |
+
<input type="button" ng-click="remove(bot.re)" class="button button-secondary"
|
475 |
+
value="Remove">
|
476 |
+
</td>
|
477 |
+
</tr>
|
478 |
+
</tbody>
|
479 |
+
</table>
|
480 |
+
<div id="rules-export-import" style="display:none;">
|
481 |
<textarea
|
482 |
+
style="-webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box;width: 100%;height: 99%;"
|
483 |
+
json-text ng-model="bots"></textarea>
|
484 |
+
</div>
|
485 |
+
<p class="submit">
|
486 |
+
<input type="button" class="button button-primary" ng-click="save()" value="Save">
|
487 |
+
<input type="button" class="button button-primary" ng-click="reset()" value="Reset to Defaults">
|
488 |
+
<a href="#TB_inline?width=540&height=360&inlineId=rules-export-import"
|
489 |
+
class="thickbox button button-secondary">Import/Export Definitions</a>
|
490 |
+
</p>
|
491 |
+
|
492 |
+
</div>
|
493 |
+
</div>
|
494 |
+
<?php
|
495 |
+
}
|
496 |
+
|
497 |
+
|
498 |
}
|
499 |
|
500 |
// Inside WordPress
|
501 |
+
if ( defined( 'ABSPATH' ) ) {
|
502 |
+
$NiteowebSpiderBlocker_ins = new SpiderBlocker;
|
503 |
+
register_activation_hook( __FILE__, array( &$NiteowebSpiderBlocker_ins, 'activatePlugin' ) );
|
504 |
+
register_deactivation_hook( __FILE__, array( &$NiteowebSpiderBlocker_ins, 'removeBlockRules' ) );
|
505 |
}
|