devshort

private self-hosted shortlink service
git clone https://git.clttr.info/devshort.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

redirect.php (4164B)


      1 <?php
      2 
      3 // This file is part of the devShort project under the MIT License. Visit https://sr.ht/~rwa/devshort for more information.
      4 
      5 $short = strtolower(htmlspecialchars($_GET["short"]));
      6 
      7 $return_404 = array("assets/vendor/bootstrap/bootstrap.min.css.map", "assets/vendor/frappe-charts/frappe-charts.min.umd.js.map");
      8 if (in_array($short, $return_404)) {
      9     header("HTTP/1.1 404 Not Found");
     10     exit;
     11 }
     12 
     13 $config_path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "data", "config.json"));
     14 $config_content = json_decode(file_get_contents($config_path), true);
     15 $stats_path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "data", "stats.json"));
     16 
     17 // Count the access to the given $name
     18 function count_access($name) {
     19     global $stats_path;
     20 
     21     $statsfile = fopen($stats_path, 'c+');
     22     if (flock($statsfile, LOCK_EX))
     23     {
     24         $stats_content = json_decode(fread($statsfile, filesize($stats_path)), true);
     25         $stats_content[$name][date("Y-m-d")] += 1;
     26 
     27         ftruncate($statsfile, 0);
     28         fseek($statsfile, 0);
     29         fwrite($statsfile, json_encode($stats_content, JSON_PRETTY_PRINT));
     30         flock($statsfile, LOCK_UN);
     31     }
     32     fclose($statsfile);
     33 }
     34 
     35 if (array_key_exists($short, $config_content["shortlinks"])) {
     36     header("Location: " . $config_content["shortlinks"][$short], $http_response_code=303);
     37     count_access($short);
     38     exit;
     39 } else if ($short === "") {
     40     header("Location: index.php", $http_response_code=301);
     41     exit;
     42 } else {
     43     header("HTTP/1.1 404 Not Found");
     44     count_access("404-request");
     45 
     46     // Generate custom buttons for the footer
     47 	$links_string = "";
     48 	if ($config_content["settings"]["custom_links"]) {
     49 		foreach ($config_content["settings"]["custom_links"] as $name => $url) {
     50 			$links_string = $links_string . "<a href=\"$url\" class=\"badge badge-primary\" target=\"_blank\">$name</a> ";
     51 		}
     52 		$links_string = substr($links_string, 0, -1);
     53 	}
     54 
     55 	$author_string = "";
     56 	if ($config_content["settings"]["author_link"]) {
     57 		$author_string = "<a rel=\"me\" target=\"_blank\" href=\"". $config_content["settings"]["author_link"] ."\">".$config_content["settings"]["author"]."</a>";
     58 	} else {
     59 		$author_string = $config_content["settings"]["author"];
     60 	}
     61 }
     62 
     63 ?>
     64 
     65 <!doctype html>
     66 <html class="h-100" lang="en">
     67 
     68 <head>
     69     <meta charset="utf-8">
     70     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     71     <meta name="robots" content="noindex, nofollow">
     72     <meta name="author" content="<?php echo $config_content["settings"]["author"]; ?> and the devShort team">
     73     <link href="<?php echo $config_content["settings"]["favicon"]; ?>" rel="icon">
     74     <title>404 | <?php echo $config_content["settings"]["name"]; ?></title>
     75     <link href="assets/vendor/bootstrap/bootstrap.min.css" rel="stylesheet">
     76 </head>
     77 
     78 <body class="d-flex flex-column h-100">
     79 
     80     <main class="flex-shrink-0">
     81         <div class="container">
     82             <nav class="mt-3" aria-label="breadcrumb">
     83                 <ol class="breadcrumb shadow-sm">
     84                     <li class="breadcrumb-item"><a href="<?php echo $config_content["settings"]["home_link"]; ?>">Home</a></li>
     85                     <li class="breadcrumb-item" aria-current="page"><a href="/"><?php echo $config_content["settings"]["name"]; ?></a></li>
     86                     <li class="breadcrumb-item active" aria-current="page">404</li>
     87                 </ol>
     88             </nav>
     89             <h1 class="mt-5">404 | Shortlink Not Found.</h1>
     90             <p class="lead">The requested shortlink <i><?php echo $short; ?></i> was not found on this server. It was either deleted, expired, misspelled or eaten by a monster.</p>
     91         </div>
     92     </main>
     93 
     94     <footer class="footer mt-auto py-3">
     95         <div class="container">
     96             <div class="d-flex justify-content-between align-items-center breadcrumb shadow-sm">
     97                 <span class="text-dark">&copy; 2020-2023 <?php echo $author_string; ?></a> and <a href="https://sr.ht/~rwa/devshort" target="_blank">devShort</a></span>
     98 				<?php if ($links_string) { echo "<span class=\"text-muted\">$links_string</span>"; } ?>
     99             </div>
    100         </div>
    101     </footer>
    102 
    103 </body>
    104 
    105 </html>