Skip to content

Commit

Permalink
🔧 bot detection can be disabled with option
Browse files Browse the repository at this point in the history
⬆️ upgraded dependencies

Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Feb 6, 2024
1 parent d7dfcbc commit 082018f
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 230 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ return [
| field.sqlite.file | `fn()` | callback. returns filepath to sqlite file |
| field.field.count | `viewcount` | string. name of field in page blueprint |
| field.field.timestamp | `lastvisited` | string. name of field in page blueprint |
| botDetection.CrawlerDetect | `true` | check for crawlers (~10ms) |
| botDetection.DeviceDetector | `true` | check for bots (~40ms) |

## Disclaimer

Expand Down
42 changes: 30 additions & 12 deletions classes/PageViewCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function __construct(array $options = [])
$defaults = [
'debug' => option('debug'),
'counter' => \option('bnomei.pageviewcounter.counter'),
'ignore-panel-users' => \option('bnomei.pageviewcounter.ignore-panel-users'),
'CrawlerDetect' => option('bnomei.pageviewcounter.botDetection.CrawlerDetect'),
'DeviceDetector' => option('bnomei.pageviewcounter.botDetection.DeviceDetector'),
];
$this->options = array_merge($defaults, $options);

Expand Down Expand Up @@ -76,12 +79,17 @@ public function timestamp(string $id): int

public function pixel()
{
$IMG = \imagecreate(1, 1);
$background = \imagecolorallocate($IMG, 0, 0, 0);
\header("Content-type: image/png");
\imagepng($IMG);
\imagecolordeallocate($IMG, $background);
\imagedestroy($IMG);
try {
$IMG = \imagecreate(1, 1);
$background = \imagecolorallocate($IMG, 0, 0, 0);
\header("Content-type: image/png");
\imagepng($IMG);
\imagecolordeallocate($IMG, $background);
\imagedestroy($IMG);
} catch (Exception $e) {
\header("Content-type: text/plain");
echo $e->getMessage();
}
exit;
}

Expand All @@ -91,17 +99,27 @@ public function willTrack(): bool
return false;
}

$hasUser = option('bnomei.pageviewcounter.ignore-panel-users') && kirby()->user();
$hasUser = $this->option('ignore-panel-users') && kirby()->user();
if ($hasUser) {
return false;
}

$useragent = A::get($_SERVER, "HTTP_USER_AGENT", '');
$device = new DeviceDetector($useragent);
$device->discardBotInformation();
$device->parse();
if ($device->isBot() || (new CrawlerDetect())->isCrawler($useragent)) {
return false;

if ($this->option('CrawlerDetect')) {
$isCrawler = (new CrawlerDetect())->isCrawler($useragent);
if ($isCrawler) {
return false;
}
}

if ($this->option('DeviceDetector')) {
$device = new DeviceDetector($useragent);
$device->discardBotInformation();
$device->parse();
if ($device->isBot()) {
return false;
}
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
}
],
"keywords": [
"kirby3",
"kirby3-cms",
"kirby3-plugin",
"kirby",
"kirby-cms",
"kirby-plugin",
"seo",
"pageview",
"tracking",
Expand All @@ -34,7 +34,7 @@
}
},
"require": {
"php": ">=8.0",
"php": ">=8.1",
"matomo/device-detector": "^6.1",
"jaybizzle/crawler-detect": "^1.2",
"getkirby/composer-installer": "^1.2"
Expand Down
Loading

0 comments on commit 082018f

Please sign in to comment.