Ignore IPs and add configs for GoatCounter

Fixes #11.
This commit is contained in:
Florine W. Dekker 2021-05-03 20:05:16 +02:00
parent 4d6eae5f14
commit 6ded3bb27f
Signed by: FWDekker
GPG Key ID: 78B3EAF58145AF25
5 changed files with 40 additions and 11 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
## Custom
dist/.goat.ignore
dist/.goat.token
dist/.goat.url
dist/.links.db
phpliteadmin.config.php
dist/phpliteadmin.config.php

View File

View File

@ -18,8 +18,16 @@ $> php -S localhost:8080 ./index.php
Copy these files into the desired directory.
There's a few hardcoded URLs in there that you'll have to fix yourself.
One day I'll create a config file for that.
You may override settings from `phpliteadmin.php` in the file `phpliteadmin.config.php`.
Add your [GoatCounter](https://github.com/zgoat/goatcounter) API token with "Record pageviews" permission in the
`.goat.token` file.
The database at `.links.db` is automatically created when you visit the main page.
## Configuration
### phpLiteAdmin
You may override settings from `phpliteadmin.php` in the file `phpliteadmin.config.php`.
### GoatCounter
GoatCounter is disabled unless the `.goat.token` and `.goat.url` files exist.
* Add your [GoatCounter](https://github.com/zgoat/goatcounter) API token with "Record pageviews" permission in the
`.goat.token` file.
* Add the URL of the `/count` API endpoint in `.goat.url`, e.g. `https://example.com/api/v0/count`.
* Add IP addresses that should not be counted to `.goat.ignore`, separated by newlines.

29
dist/goatcounter.php vendored
View File

@ -1,4 +1,25 @@
<?php
/**
* Returns true if GoatCounter should process the current request.
*
* @return bool true if GoatCounter should process the current request
*/
function goatcounter_should_process(): bool
{
if (!is_readable(".goat.token") || !is_readable(".goat.url")) return false;
if (is_readable(".goat.ignore")) {
$ignored_ips = file(".goat.ignore", FILE_IGNORE_NEW_LINES);
if (in_array($_SERVER["REMOTE_ADDR"], $ignored_ips)) return false;
}
if (isset($_SERVER["HTTP_USER_AGENT"])
&& preg_match("/bot|crawl|slurp|spider|mediapartners/i", $_SERVER["HTTP_USER_AGENT"])) return false;
return true;
}
/**
* Sends a request to this website's GoatCounter server to record a hit.
*
@ -6,16 +27,14 @@
*/
function goatcounter_record_hit(string $path)
{
if (isset($_SERVER["HTTP_USER_AGENT"])
&& preg_match("/bot|crawl|slurp|spider|mediapartners/i", $_SERVER["HTTP_USER_AGENT"])) {
return;
}
if (!goatcounter_should_process()) return;
$token = file_get_contents(".goat.token");
$url = file_get_contents(".goat.url");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://stats.fwdkr.co/api/v0/count");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,

2
dist/index.php vendored
View File

@ -107,7 +107,7 @@ if ($target != null && !$needs_confirmation) {
$("#header").appendChild(header({title: "fwdkr.co"}));
$("#footer").appendChild(footer({
vcsURL: "https://git.fwdekker.com/FWDekker/fwdkr.co/",
version: "v1.3.2"
version: "v1.3.3"
}));
});
</script>