]> git.mxchange.org Git - friendica-addons.git/blob - blockbot/blockbot.php
2e35f21027c9bfe5f132f857ad5ddc78089a6e3c
[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.2
6  * Author: Philipp Holzer <admin@philipp.info>
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  *
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Config;
13 use Friendica\Core\Hook;
14 use Friendica\Core\System;
15 use Jaybizzle\CrawlerDetect\CrawlerDetect;
16 use Friendica\Core\Logger;
17 use Friendica\Core\Renderer;
18 use Friendica\Core\L10n;
19
20 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
21
22 function blockbot_install() {
23         Hook::register('init_1', __FILE__, 'blockbot_init_1');
24 }
25
26
27 function blockbot_uninstall() {
28         Hook::unregister('init_1', __FILE__, 'blockbot_init_1');
29 }
30
31 function blockbot_addon_admin(&$a, &$o) {
32         $t = Renderer::getMarkupTemplate("admin.tpl", "addon/blockbot/");
33
34         $o = Renderer::replaceMacros($t, [
35                 '$submit' => L10n::t('Save Settings'),
36                 '$training' => ['training', L10n::t('Training mode'), Config::get('blockbot', 'training'), "Activates the training mode. This is only meant for developing purposes. Don't activate this on a production machine."],
37                 '$block_gab' => ['block_gab', L10n::t('Block GabSocial'), Config::get('blockbot', 'block_gab'), 'Block the software GabSocial. This will block every access for that software. You can block dedicated gab instances in the blocklist settings in the admin section.'],
38         ]);
39 }
40
41 function blockbot_addon_admin_post(&$a) {
42         Config::set('blockbot', 'training', $_POST['training'] ?? false);
43         Config::set('blockbot', 'block_gab', $_POST['block_gab'] ?? false);
44         info(L10n::t('Settings updated.'). EOL);
45 }
46
47 function blockbot_init_1(App $a) {
48         if (empty($_SERVER['HTTP_USER_AGENT'])) {
49                 return;
50         }
51
52         $logdata = ['agent' => $_SERVER['HTTP_USER_AGENT'], 'uri' => $_SERVER['REQUEST_URI']];
53
54         // List of known crawlers.
55         $agents = ['SemrushBot', 's~feedly-nikon3', 'Qwantify/Bleriot/', 'ltx71', 'Sogou web spider/',
56                 'Diffbot/', 'Twitterbot/', 'YisouSpider', 'evc-batch/', 'LivelapBot/', 'TrendsmapResolver/',
57                 'PaperLiBot/', 'Nuzzel', 'um-LN/', 'Google Favicon', 'Datanyze', 'BLEXBot/', '360Spider',
58                 'adscanner/', 'HeadlessChrome', 'wpif', 'startmebot/', 'Googlebot/', 'Applebot/',
59                 'facebookexternalhit/', 'GoogleImageProxy', 'bingbot/', 'heritrix/', 'ldspider',
60                 'AwarioRssBot/', 'Zabbix', 'TweetmemeBot/', 'dcrawl/', 'PhantomJS/', 'Googlebot-Image/',
61                 'CrowdTanglebot/', 'Mediapartners-Google', 'Baiduspider/', 'datagnionbot',
62                 'MegaIndex.ru/', 'SMUrlExpander', 'Hatena-Favicon/', 'Wappalyzer', 'FlipboardProxy/',
63                 'NetcraftSurveyAgent/', 'Dataprovider.com', 'SMTBot/', 'Nimbostratus-Bot/',
64                 'DuckDuckGo-Favicons-Bot/', 'IndieWebCards/', 'proximic', 'netEstate NE Crawler',
65                 'AhrefsBot/', 'YandexBot/', 'Exabot/', 'Mediumbot-MetaTagFetcher/', 'WhatsApp/',
66                 'TelegramBot', 'SurdotlyBot/', 'BingPreview/', 'SabsimBot/', 'CCBot/', 'WbSrch/',
67                 'DuckDuckBot-Https/', 'HTTP Banner Detection', 'YandexImages/', 'archive.org_bot',
68                 'ArchiveTeam ArchiveBot/', 'yacybot', 'https://developers.google.com/+/web/snippet/',
69                 'Scrapy/', 'github-camo', 'MJ12bot/', 'DotBot/', 'Pinterestbot/', 'Jooblebot/',
70                 'Cliqzbot/', 'YaK/', 'Mediatoolkitbot', 'Snacktory', 'FunWebProducts', 'oBot/',
71                 '7Siters/', 'KOCMOHABT', 'Google-SearchByImage'];
72
73         if (Config::get('blockbot', 'block_gab')) {
74                 $agents[] = 'GabSocial/';
75         }
76
77         foreach ($agents as $agent) {
78                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
79                         System::httpExit(403, 'Bots are not allowed');
80                 }
81         }
82
83         // This switch here is only meant for developers who want to add more bots to the list above, it is not safe for production.
84         if (!Config::get('blockbot', 'training')) {
85                 return;
86         }
87
88         $crawlerDetect = new CrawlerDetect();
89
90         if (!$crawlerDetect->isCrawler()) {
91                 logger::debug('Good user agent detected', $logdata);
92                 return;
93         }
94
95         // List of false positives' strings of known "good" agents.
96         $agents = ['fediverse.network crawler', 'Active_Pods_CheckBot_3.0', 'Social-Relay/',
97                 'curl', 'zgrab', 'Go-http-client', 'curb', 'github.com', 'reqwest', 'Feedly/',
98                 'Python-urllib/', 'Liferea/', 'aiohttp/', 'WordPress.com Reader', 'hackney/',
99                 'Faraday v', 'okhttp', 'UniversalFeedParser', 'PixelFedBot', 'python-requests',
100                 'WordPress/', 'http.rb/', 'Apache-HttpClient/', 'WordPress.com;', 'Pleroma',
101                 'Dispatch/', 'Ruby', 'Uptimebot/', 'Java/', 'libwww-perl/', 'Mastodon/',
102                 'lua-resty-http/', 'Test Certificate Info'];
103
104         foreach ($agents as $agent) {
105                 if (stristr($_SERVER['HTTP_USER_AGENT'], $agent)) {
106                         logger::notice('False positive', $logdata);
107                         return;
108                 }
109         }
110
111         logger::info('Blocked bot', $logdata);
112         System::httpExit(403, 'Bots are not allowed');
113 }