Another one ...
[friendica-addons.git/.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\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\System;
14 use Jaybizzle\CrawlerDetect\CrawlerDetect;
15 use Friendica\Core\Logger;
16
17 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
18
19 function blockbot_install() {
20         Hook::register('init_1', __FILE__, 'blockbot_init_1');
21 }
22
23
24 function blockbot_uninstall() {
25         Hook::unregister('init_1', __FILE__, 'blockbot_init_1');
26 }
27
28 function blockbot_init_1(App $a) {
29         if (empty($_SERVER['HTTP_USER_AGENT'])) {
30                 return;
31         }
32
33         $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
34
35         // List of known crawlers.
36         $agents = ['SEMrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
37                 'Diffbot/', 'Twitterbot/', 'YisouSpider/', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
38                 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
39                 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
40                 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider',
41                 'AwarioRssBot/', 'Zabbix', 'TweetmemeBot/', 'dcrawl/', 'PhantomJS/', 'Googlebot-Image/',
42                 'CrowdTanglebot/', 'Mediapartners-Google', 'Baiduspider/', 'datagnionbot',
43                 'MegaIndex.ru/', 'SMUrlExpander', 'Hatena-Favicon/', 'Wappalyzer', 'FlipboardProxy/',
44                 'NetcraftSurveyAgent/', 'Dataprovider.com', 'SMTBot/', 'Nimbostratus-Bot/',
45                 'DuckDuckGo-Favicons-Bot/', 'IndieWebCards/', 'proximic', 'netEstate NE Crawler',
46                 'AhrefsBot/', 'YandexBot/', 'Exabot/', 'Mediumbot-MetaTagFetcher/',
47                 'WhatsApp/', 'TelegramBot', 'SurdotlyBot/', 'BingPreview/', 'SabsimBot/',
48                 'CCBot/'];
49
50         foreach ($agents as $agent) {
51                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
52                         System::httpExit(403, 'Bots are not allowed');
53                 }
54         }
55
56         // This switch here is only meant for developers who want to add more bots to the list above, it is not safe for production.
57         if (!Config::get('blockbot', 'training')) {
58                 return;
59         }
60
61         $crawlerDetect = new CrawlerDetect();
62
63         if (!$crawlerDetect->isCrawler()) {
64                 logger::debug('Good user agent detected', $logdata);
65                 return;
66         }
67
68         // List of false positives' strings of known "good" agents.
69         $agents = ['fediverse.network crawler', 'Active_Pods_CheckBot_3.0', 'Social-Relay/',
70                 'curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
71                 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
72                 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
73                 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;', 'Pleroma',
74                 'Dispatch/', 'Ruby', 'Uptimebot/', 'Java/', 'libwww-perl/'];
75
76         foreach ($agents as $agent) {
77                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
78                         logger::notice('False positive', $logdata);
79                         return;
80                 }
81         }
82
83         logger::info('Blocked bot', $logdata);
84         System::httpExit(403, 'Bots are not allowed');
85 }