From 8f492f243275dace08d1a05915c27b52fed7a1aa Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Sun, 3 Oct 2021 14:04:05 +0200 Subject: [PATCH] feat(UrlShortner): add module --- config.example.php | 5 +- index.php | 22 +----- modules/urlshortner/urlshortner.php | 115 ++++++++++++++++++++++++++++ utils.php | 24 ++++++ 4 files changed, 144 insertions(+), 22 deletions(-) create mode 100644 modules/urlshortner/urlshortner.php diff --git a/config.example.php b/config.example.php index d3d4a6f..5118a3a 100644 --- a/config.example.php +++ b/config.example.php @@ -25,7 +25,10 @@ return [ 'baseconverter' => [ 'alias' => ['encode', 'decode'] ], - 'uuid' => [] + 'uuid' => [], + 'urlshortner' => [ + 'alias' => ['u', 'url'] + ] ], 'auth' => [ 'root' => 'password' diff --git a/index.php b/index.php index e6b3892..30b28c5 100644 --- a/index.php +++ b/index.php @@ -51,27 +51,7 @@ if ($selectedModule === null) { generateHelp($selectedModule['key']); if (isset($selectedModule['auth']) && $selectedModule['auth']) { - 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 ($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(); - } + askAuth($config['auth']); } require('./modules/' . $selectedModule['key'] . '/' . $selectedModule['key'] . '.php'); diff --git a/modules/urlshortner/urlshortner.php b/modules/urlshortner/urlshortner.php new file mode 100644 index 0000000..5ea16f5 --- /dev/null +++ b/modules/urlshortner/urlshortner.php @@ -0,0 +1,115 @@ + 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(); + } + ?> +

Url list

+ Add a URL +
+ + + +
+ + + + +
+ +
+
+
+ +
+
+ / : +
+ Delete +
+ +
+ $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(); + } +}