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(); } ?>