Add a new, special GoatCounter solution

This commit is contained in:
Florine W. Dekker 2021-04-23 17:47:20 +02:00
parent 84fa144bdd
commit 80df81142a
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
4 changed files with 50 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
## Custom
.goat.token
.links.db
phpliteadmin.config.php

View File

@ -3,6 +3,7 @@ A custom URL shortener.
## Requirements
* PHP 7.3+
* [PHP cURL](https://www.php.net/manual/en/book.curl.php)
* [PHP Multibyte String](https://www.php.net/manual/en/book.mbstring.php)
* [PHP SQLite 3](https://www.php.net/manual/en/book.sqlite3.php)
@ -14,5 +15,10 @@ $> php -S localhost:8080 ./index.php
## Installation
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.

34
goatcounter.php Normal file
View File

@ -0,0 +1,34 @@
<?php
/**
* Sends a request to this website's GoatCounter server to record a hit.
*
* @param $path string the path to record a hit for
*/
function goatcounter_record_hit(string $path)
{
$token = file_get_contents(".goat.token");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://stats.fwdkr.co/api/v0/count");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(array(
"hits" => array(array(
"path" => $path,
"ip" => $_SERVER["REMOTE_ADDR"],
"ref" => $_SERVER["HTTP_REFERER"],
"user_agent" => $_SERVER["HTTP_USER_AGENT"]
))
))
);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Content-Type: application/json",
"Authorization: Bearer $token"
)
);
curl_exec($ch);
curl_close($ch);
}

View File

@ -1,5 +1,6 @@
<?php
include_once("links.php");
include_once("goatcounter.php");
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
@ -11,6 +12,9 @@ switch ($path) {
case "/favicon.ico":
header("Location: https://fwdekker.com/favicon.ico");
exit();
default:
goatcounter_record_hit($path);
break;
}
$error = false;
@ -25,7 +29,7 @@ try {
}
if ($target != null && !$needs_confirmation) {
header("Location: {$target}");
header("Location: $target");
exit();
}
?>
@ -43,8 +47,8 @@ if ($target != null && !$needs_confirmation) {
<title>fwdkr.co</title>
<link rel="stylesheet" href="https://static.fwdekker.com/fonts/roboto.css" crossorigin="anonymous" />
<link rel="stylesheet" href="https://static.fwdekker.com/lib/template/1.x.x/bundle.css" crossorigin="anonymous" />
<link rel="stylesheet" href="https://static.fwdekker.com/fonts/roboto/roboto.css" />
<link rel="stylesheet" href="https://static.fwdekker.com/lib/template/2.x.x/template.css" />
</head>
<body>
<main> <!-- Do not hide without JavaScript -->
@ -93,7 +97,7 @@ if ($target != null && !$needs_confirmation) {
<div id="footer"></div>
</main>
<script src="https://static.fwdekker.com/lib/template/1.x.x/bundle.js" crossorigin="anonymous"></script>
<script src="https://static.fwdekker.com/lib/template/2.x.x/template.js"></script>
<script>
// noinspection JSUnresolvedVariable
const {$, doAfterLoad, footer, header, nav, showPage, stringToHtml} = window.fwdekker;
@ -103,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.2.3"
version: "v1.3.0"
}));
showPage();
});