devshort

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

index.php (4280B)


      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 $config_path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "data", "config.json"));
      6 $config_content = json_decode(file_get_contents($config_path), true);
      7 $stats_path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "data", "stats.json"));
      8 $stats_content = json_decode(file_get_contents($stats_path), true);
      9 
     10 // Count the access
     11 $statsfile = fopen($stats_path, 'c+');
     12 if (flock($statsfile, LOCK_EX))
     13 {
     14     $stats_content = json_decode(fread($statsfile, filesize($stats_path)), true);
     15     $stats_content["index"][date("Y-m-d")] += 1;
     16 
     17     ftruncate($statsfile, 0);
     18     fseek($statsfile, 0);
     19     fwrite($statsfile, json_encode($stats_content, JSON_PRETTY_PRINT));
     20     flock($statsfile, LOCK_UN);
     21 }
     22 fclose($statsfile);
     23 
     24 // Generate custom buttons for the footer
     25 $links_string = "";
     26 if ($config_content["settings"]["custom_links"]) {
     27     foreach ($config_content["settings"]["custom_links"] as $name => $url) {
     28         $links_string = $links_string . "<a href=\"$url\" class=\"badge badge-primary\" target=\"_blank\">$name</a> ";
     29     }
     30     $links_string = substr($links_string, 0, -1);
     31 }
     32 
     33 $author_string = "";
     34 if ($config_content["settings"]["author_link"]) {
     35     $author_string = "<a rel=\"me\" target=\"_blank\" href=\"". $config_content["settings"]["author_link"] ."\">".$config_content["settings"]["author"]."</a>";
     36 } else {
     37     $author_string = $config_content["settings"]["author"];
     38 }
     39 
     40 function show_random_shortlink() {
     41 	global $config_content;
     42 
     43 	if (!$config_content["settings"]["random_shortlink"]) { return; }
     44 	
     45 	$random_short = array_rand($config_content["shortlinks"]);
     46 	$random_url = dirname($_SERVER['PHP_SELF']) . $random_short; 
     47 	echo "<h2>Explore</h2><p class=\"lead\">We've choosen a random shortlink for you: <a href=\"$random_url\">$random_short</a></p>";
     48 }
     49 
     50 ?>
     51 
     52 <!doctype html>
     53 <html class="h-100" lang="en">
     54 
     55 <head>
     56     <meta charset="utf-8">
     57     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     58     <meta name="robots" content="noindex, nofollow">
     59     <meta name="author" content="<?php echo $config_content["settings"]["author"]; ?> and the devShort team">
     60     <link href="<?php echo $config_content["settings"]["favicon"]; ?>" rel="icon">
     61     <title><?php echo $config_content["settings"]["name"]; ?></title>
     62     <link href="assets/vendor/bootstrap/bootstrap.min.css" rel="stylesheet">
     63 </head>
     64 
     65 <body class="d-flex flex-column h-100">
     66 
     67     <main class="flex-shrink-0">
     68         <div class="container">
     69             <nav class="mt-3" aria-label="breadcrumb">
     70                 <ol class="breadcrumb shadow-sm">
     71                     <li class="breadcrumb-item"><a href="<?php echo $config_content["settings"]["home_link"]; ?>">Home</a></li>
     72                     <li class="breadcrumb-item active" aria-current="page"><?php echo $config_content["settings"]["name"]; ?></li>
     73                 </ol>
     74             </nav>
     75             <h1 class="mt-5"><?php echo $config_content["settings"]["name"]; ?></h1>
     76             <p class="lead">This is a shortlink service. You need a valid shortlink to get redirected.</p>
     77             <?php show_random_shortlink(); ?>
     78             <ul class="list-inline">
     79                 <li class="list-inline-item"><a href="https://en.wikipedia.org/wiki/URL_shortening" target="_blank">What is URL shortening?</a></li>
     80                 <li class="list-inline-item">-</li>
     81                 <li class="list-inline-item"><a href="<?php echo $config_content["settings"]["home_link"]; ?>">Home page</a></li>
     82                 <li class="list-inline-item">-</li>
     83                 <li class="list-inline-item"><a href="admin.php">Admin panel</a></li>
     84             </ul>
     85         </div>
     86     </main>
     87 
     88     <footer class="footer mt-auto py-3">
     89         <div class="container">
     90             <div class="d-flex justify-content-between align-items-center breadcrumb shadow-sm">
     91                 <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>
     92 				<?php if ($links_string) { echo "<span class=\"text-muted\">$links_string</span>"; } ?>
     93             </div>
     94         </div>
     95     </footer>
     96 
     97 </body>
     98 
     99 </html>