Add optional redirection confirmation

Fixes #9.
This commit is contained in:
Florine W. Dekker 2021-04-13 19:18:33 +02:00
parent a37195ebf4
commit b8ebe18992
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
3 changed files with 35 additions and 18 deletions

View File

@ -15,15 +15,16 @@ switch ($path) {
$error = false;
$target = null;
$needs_confirmation = false;
try {
$links = new Links(".links.db", "links.sql");
$target = $links->getTarget($path);
list($target, $needs_confirmation) = $links->getTarget($path);
$links->handleVisit($path);
} catch (Exception $exception) {
$error = true;
}
if ($target != null) {
if ($target != null && !$needs_confirmation) {
header("Location: {$target}");
exit();
}
@ -88,20 +89,33 @@ if ($target != null) {
Something went wrong internally, and you could not be redirected. Please try again later.<br /><br />
<br /><br />
<?php
} else if ($path != "/") {
} else if ($target == "/") {
?>
<b>Link not found</b><br /><br />
The short URL https://fwdkr.co<?= $path ?> could not be found.<br /><br />
<br /><br />
<?php
}
?>
<b>About fwdkr.co</b><br /><br />
You've arrived at this page because you typed or linked to "fwdkr.co", the URL shortcut for
<a href="https://fwdekker.com/">fwdekker.com</a>.<br /><br />
Whenever you see a "fwdkr.co" link, you can trust that it will always take you to a website affiliated with
FWDekker.
if ($target != null && $needs_confirmation) {
?>
<b>Confirm redirect</b><br /><br />
This URL redirects to <b><?= $target ?></b>.<br /><br />
<a href="<?= $target ?>">Click here to proceed</a>
<br /><br />
<?php
} else {
?>
<b>About fwdkr.co</b><br /><br />
You've arrived at this page because you typed or linked to "fwdkr.co", the URL shortcut for
<a href="https://fwdekker.com/">fwdekker.com</a>.<br /><br />
Whenever you see a "fwdkr.co" link, you can trust that it will always take you to a website affiliated with
FWDekker.
<?php
}
?>
</section>
<footer>
Made by <a href="https://fwdekker.com/">Felix W. Dekker</a><span>.
@ -109,7 +123,7 @@ if ($target != null) {
<a href="https://git.fwdekker.com/FWDekker/fwdkr.co/src/branch/master/LICENSE">MIT License</a>.
Source code available on </span><a href="https://git.fwdekker.com/FWDekker/fwdkr.co/">git</a>.
Consider reading the <a href="https://fwdekker.com/privacy/">privacy policy</a>.
<div style="float: right;">v1.0.9</div>
<div style="float: right;">v1.1.0</div>
</footer>
</main>
</body>

View File

@ -43,14 +43,15 @@ class Links
* Returns the target to the given path, or null if there is no such target.
*
* @param string $path the path to return the target of
* @return string|null the target to the given path, or null if there is no such target
* @return array|null the target to the given path and whether confirmation is required before redirecting, or null
* if there is no such target
*/
function getTarget(string $path): ?string
function getTarget(string $path): ?array
{
$stmt = $this->db->prepare("SELECT * FROM links WHERE path = :path;");
$stmt = $this->db->prepare("SELECT target, needs_confirmation FROM links WHERE path = :path;");
$stmt->bindValue(":path", $path);
if ($result = $stmt->execute()->fetchArray(SQLITE3_ASSOC))
return $result["target"];
return array($result["target"], $result["needs_confirmation"]);
return null;
}

View File

@ -1,11 +1,13 @@
-- auto-generated definition
create table links
(
path text not null
path text not null
constraint links_pk
primary key,
target text not null,
visits int default 0 not null,
last_visit text
target text not null,
needs_confirmation boolean default false not null,
visits int default 0 not null,
last_visit text
);
create unique index links_path_uindex