feat(UrlShortner): add module
This commit is contained in:
parent
c677114e08
commit
8f492f2432
4 changed files with 144 additions and 22 deletions
|
@ -25,7 +25,10 @@ return [
|
||||||
'baseconverter' => [
|
'baseconverter' => [
|
||||||
'alias' => ['encode', 'decode']
|
'alias' => ['encode', 'decode']
|
||||||
],
|
],
|
||||||
'uuid' => []
|
'uuid' => [],
|
||||||
|
'urlshortner' => [
|
||||||
|
'alias' => ['u', 'url']
|
||||||
|
]
|
||||||
],
|
],
|
||||||
'auth' => [
|
'auth' => [
|
||||||
'root' => 'password'
|
'root' => 'password'
|
||||||
|
|
22
index.php
22
index.php
|
@ -51,27 +51,7 @@ if ($selectedModule === null) {
|
||||||
generateHelp($selectedModule['key']);
|
generateHelp($selectedModule['key']);
|
||||||
|
|
||||||
if (isset($selectedModule['auth']) && $selectedModule['auth']) {
|
if (isset($selectedModule['auth']) && $selectedModule['auth']) {
|
||||||
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
|
askAuth($config['auth']);
|
||||||
{
|
|
||||||
header("WWW-Authenticate: Basic realm=\"You need to provide user and passwd\"");
|
|
||||||
http_response_code(401);
|
|
||||||
echo "Authentification Required";
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
$providedUsername = $_SERVER['PHP_AUTH_USER'];
|
|
||||||
$providedPassword = $_SERVER['PHP_AUTH_PW'];
|
|
||||||
$found = false;
|
|
||||||
foreach ($config['auth'] as $username => $password) {
|
|
||||||
if ($username === $providedUsername && $password == $providedPassword) {
|
|
||||||
$found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!$found) {
|
|
||||||
header("WWW-Authenticate: Basic realm=\"Invalid auth\"");
|
|
||||||
http_response_code(401);
|
|
||||||
echo "Invalid authentification, try again";
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
require('./modules/' . $selectedModule['key'] . '/' . $selectedModule['key'] . '.php');
|
require('./modules/' . $selectedModule['key'] . '/' . $selectedModule['key'] . '.php');
|
||||||
|
|
115
modules/urlshortner/urlshortner.php
Normal file
115
modules/urlshortner/urlshortner.php
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$path = __DIR__ . '/../../tmp/urlshortner';
|
||||||
|
|
||||||
|
function saveUrls($urlsToStore) {
|
||||||
|
global $path;
|
||||||
|
$data = json_encode([
|
||||||
|
'updated_at' => date('Y-m-d H:i:s'),
|
||||||
|
'urls' => $urlsToStore
|
||||||
|
]);
|
||||||
|
file_put_contents($path . '/urls.json', $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($path)) {
|
||||||
|
mkdir($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($path . '/urls.json') || file_get_contents($path . '/urls.json') === '') {
|
||||||
|
file_put_contents($path . '/urls.json', '{"urls": []}');
|
||||||
|
}
|
||||||
|
|
||||||
|
$urls = json_decode(file_get_contents($path . '/urls.json'), true)['urls'];
|
||||||
|
|
||||||
|
if (strpos($_SERVER['REQUEST_URI'], '/admin') === 0) {
|
||||||
|
// admin panel
|
||||||
|
askAuth($config['auth']);
|
||||||
|
|
||||||
|
if (strpos($_SERVER['REQUEST_URI'], '/create') !== false) {
|
||||||
|
$url = [
|
||||||
|
'slug' => $_GET['slug'] ?? $_GET['name'] ?? '',
|
||||||
|
'target' => $_GET['target'] ?? '',
|
||||||
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
|
];
|
||||||
|
if (empty($url['slug']) || empty($url['target'])) {
|
||||||
|
echo "Invalid values slug or target";
|
||||||
|
http_response_code(400);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
$urls[] = $url;
|
||||||
|
|
||||||
|
echo "Url was created";
|
||||||
|
saveUrls($urls);
|
||||||
|
if (isset($_GET['redirect'])) {
|
||||||
|
header('Location: ' . $_GET['redirect']);
|
||||||
|
http_response_code(302);
|
||||||
|
}
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
if (strpos($_SERVER['REQUEST_URI'], '/delete') !== false) {
|
||||||
|
if (!isset($_GET['slug']) || empty($_GET['slug'])) {
|
||||||
|
echo "Slug param missing";
|
||||||
|
http_response_code(400);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
$urls = array_filter([], fn ($u) => $u['slug'] !== $_GET['slug']);
|
||||||
|
echo "The urls with this slug were deleted";
|
||||||
|
|
||||||
|
saveUrls($urls);
|
||||||
|
if (isset($_GET['redirect'])) {
|
||||||
|
header('Location: ' . $_GET['redirect']);
|
||||||
|
}
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<h1>Url list</h1>
|
||||||
|
<b>Add a URL</b>
|
||||||
|
<form method="get" action="/admin/create">
|
||||||
|
<label for="slug">Slug</label>
|
||||||
|
<input type="text" name="slug" id="slug" />
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<label for="target">Target (link)</label>
|
||||||
|
<input type="url" name="target" id="target" style="min-width: 80%" />
|
||||||
|
|
||||||
|
<input type="hidden" name="redirect" value="/admin" />
|
||||||
|
<br>
|
||||||
|
<button>Create Shortcut</button>
|
||||||
|
</form>
|
||||||
|
<hr>
|
||||||
|
<div>
|
||||||
|
<?php
|
||||||
|
foreach ($urls as $url) {
|
||||||
|
?>
|
||||||
|
<div
|
||||||
|
style="
|
||||||
|
max-width: 900px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
border: 1px solid gray;
|
||||||
|
margin-bottom: .4em
|
||||||
|
">
|
||||||
|
<div>
|
||||||
|
/<?= $url['slug'] ?> : <a href="<?= $url['target'] ?>"><?= $url['target'] ?></a>
|
||||||
|
</div>
|
||||||
|
<a href="/admin/delete?slug=<?= $url['slug'] ?>&redirect=/admin">Delete</a>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($urls as $url) {
|
||||||
|
if (strpos($_SERVER['REQUEST_URI'], '/' . $url['slug']) === 0) {
|
||||||
|
header('Location: ' . $url['target']);
|
||||||
|
http_response_code(302);
|
||||||
|
echo "You've been redirected";
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http_response_code(404);
|
||||||
|
echo 'ERR: Unknown url';
|
24
utils.php
24
utils.php
|
@ -64,3 +64,27 @@ function deleteAllFiles($path) {
|
||||||
}
|
}
|
||||||
return $count;
|
return $count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function askAuth($authConfig) {
|
||||||
|
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
|
||||||
|
{
|
||||||
|
header("WWW-Authenticate: Basic realm=\"You need to provide user and passwd\"");
|
||||||
|
http_response_code(401);
|
||||||
|
echo "Authentification Required";
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
$providedUsername = $_SERVER['PHP_AUTH_USER'];
|
||||||
|
$providedPassword = $_SERVER['PHP_AUTH_PW'];
|
||||||
|
$found = false;
|
||||||
|
foreach ($authConfig as $username => $password) {
|
||||||
|
if ($username === $providedUsername && $password == $providedPassword) {
|
||||||
|
$found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$found) {
|
||||||
|
header("WWW-Authenticate: Basic realm=\"Invalid auth\"");
|
||||||
|
http_response_code(401);
|
||||||
|
echo "Invalid authentification, try again";
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue