Add a new, special GoatCounter solution
This commit is contained in:
parent
84fa144bdd
commit
80df81142a
|
@ -1,3 +1,4 @@
|
||||||
## Custom
|
## Custom
|
||||||
|
.goat.token
|
||||||
.links.db
|
.links.db
|
||||||
phpliteadmin.config.php
|
phpliteadmin.config.php
|
||||||
|
|
|
@ -3,6 +3,7 @@ A custom URL shortener.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
* PHP 7.3+
|
* 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 Multibyte String](https://www.php.net/manual/en/book.mbstring.php)
|
||||||
* [PHP SQLite 3](https://www.php.net/manual/en/book.sqlite3.php)
|
* [PHP SQLite 3](https://www.php.net/manual/en/book.sqlite3.php)
|
||||||
|
|
||||||
|
@ -14,5 +15,10 @@ $> php -S localhost:8080 ./index.php
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
Copy these files into the desired directory.
|
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`.
|
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.
|
The database at `.links.db` is automatically created when you visit the main page.
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
14
index.php
14
index.php
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
include_once("links.php");
|
include_once("links.php");
|
||||||
|
include_once("goatcounter.php");
|
||||||
|
|
||||||
|
|
||||||
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
|
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
|
||||||
|
@ -11,6 +12,9 @@ switch ($path) {
|
||||||
case "/favicon.ico":
|
case "/favicon.ico":
|
||||||
header("Location: https://fwdekker.com/favicon.ico");
|
header("Location: https://fwdekker.com/favicon.ico");
|
||||||
exit();
|
exit();
|
||||||
|
default:
|
||||||
|
goatcounter_record_hit($path);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$error = false;
|
$error = false;
|
||||||
|
@ -25,7 +29,7 @@ try {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($target != null && !$needs_confirmation) {
|
if ($target != null && !$needs_confirmation) {
|
||||||
header("Location: {$target}");
|
header("Location: $target");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -43,8 +47,8 @@ if ($target != null && !$needs_confirmation) {
|
||||||
|
|
||||||
<title>fwdkr.co</title>
|
<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/fonts/roboto/roboto.css" />
|
||||||
<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/lib/template/2.x.x/template.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main> <!-- Do not hide without JavaScript -->
|
<main> <!-- Do not hide without JavaScript -->
|
||||||
|
@ -93,7 +97,7 @@ if ($target != null && !$needs_confirmation) {
|
||||||
<div id="footer"></div>
|
<div id="footer"></div>
|
||||||
</main>
|
</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>
|
<script>
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
const {$, doAfterLoad, footer, header, nav, showPage, stringToHtml} = window.fwdekker;
|
const {$, doAfterLoad, footer, header, nav, showPage, stringToHtml} = window.fwdekker;
|
||||||
|
@ -103,7 +107,7 @@ if ($target != null && !$needs_confirmation) {
|
||||||
$("#header").appendChild(header({title: "fwdkr.co"}));
|
$("#header").appendChild(header({title: "fwdkr.co"}));
|
||||||
$("#footer").appendChild(footer({
|
$("#footer").appendChild(footer({
|
||||||
vcsURL: "https://git.fwdekker.com/FWDekker/fwdkr.co/",
|
vcsURL: "https://git.fwdekker.com/FWDekker/fwdkr.co/",
|
||||||
version: "v1.2.3"
|
version: "v1.3.0"
|
||||||
}));
|
}));
|
||||||
showPage();
|
showPage();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue