feat: add url shortener

This commit is contained in:
Ashhhleyyy 2023-03-17 11:47:38 +00:00
parent 1e665a66ff
commit 03bded49ff
Signed by: ash
GPG key ID: 83B789081A0878FB
3 changed files with 55 additions and 0 deletions

1
shortener/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
links/

32
shortener/README.md Normal file
View file

@ -0,0 +1,32 @@
# ash.ley
A simple link shortener. Its a single PHP script which requires a small about of webserver configuration. Adding new links just requires adding a file to the `links/` folder containing the URL to link to.
## Webserver configuration
```nginx
server {
server_name ash.ley;
root /var/www/ash.ley;
location ~ ^/links {
deny all;
}
location / {
try_files @ashdotley;
}
location @ashdotley {
rewrite ^/(.*) /index.php?id=$1&$args last;
}
location ~ ^/index.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REDIRECT_STATUS 200;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
```

22
shortener/index.php Normal file
View file

@ -0,0 +1,22 @@
<?php
if (isset($_GET['l'])) {
$l = $_GET['l'];
if (str_contains($l, '/') || str_contains($l, '~') || str_contains($l, '.')) {
http_response_code(400);
echo "hehe no you don't";
die;
}
$path = 'links/'.$l;
if (file_exists($path)) {
$target = file_get_contents($path);
header('Location: ' . $target);
} else {
http_response_code(404);
echo "<h1>not found</h1>";
echo 'nyy-a?';
}
} else {
echo "<h1>nyaaaa!</h1>";
echo "~w~\n";
}
?>