]> git.mxchange.org Git - friendica-addons.git/blob - blockbot/blockbot.php
41f31827a247bf47da738e38a566dbc7a23e733b
[friendica-addons.git] / blockbot / blockbot.php
1 <?php
2 /**
3  * Name: blockbot
4  * Description: Blocking bots based on detecting bots/crawlers/spiders via the user agent and http_from header.
5  * Version: 0.1
6  * Author: Philipp Holzer <admin@philipp.info>
7  *
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\System;
13 use Jaybizzle\CrawlerDetect\CrawlerDetect;
14 use Friendica\Core\Logger;
15
16 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
17
18 function blockbot_install() {
19         Hook::register('init_1', __FILE__, 'blockbot_init_1');
20 }
21
22
23 function blockbot_uninstall() {
24         Hook::unregister('init_1', __FILE__, 'blockbot_init_1');
25 }
26
27 function blockbot_init_1(App $a) {
28         $crawlerDetect = new CrawlerDetect();
29
30         if (empty($_SERVER['HTTP_USER_AGENT'])) {
31                 return;
32         }
33
34         $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
35
36         if (!$crawlerDetect->isCrawler()) {
37                 logger::debug('Good user agent detected', $logdata);
38                 return;
39         }
40
41         // List of false positives' strings of known "good" agents.
42         $agents = ['fediverse.network crawler', 'Active_Pods_CheckBot_3.0', 'Social-Relay/',
43                 'curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
44                 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
45                 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
46                 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;'];
47
48         foreach ($agents as $agent) {
49                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
50                         logger::notice('False positive', $logdata);
51                         return;
52                 }
53         }
54
55         // List of known crawlers. They are added here to avoid having them logged at the end of the function.
56         // This helps to detect false positives.
57         $agents = ['SEMrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
58                 'Diffbot/', 'Twitterbot/', 'YisouSpider/', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
59                 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
60                 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
61                 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider'];
62
63         foreach ($agents as $agent) {
64                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
65                         System::httpExit(403, ['title' => 'Bots are not allowed']);
66                 }
67         }
68
69         logger::info('Blocked bot', $logdata);
70         System::httpExit(403, ['title' => 'Bots are not allowed']);
71 }