commit cfa309885ac56787525808d6c0f1e32980833ec6 from: mischa date: Fri Aug 12 07:13:25 2022 UTC Initial commit commit - /dev/null commit + cfa309885ac56787525808d6c0f1e32980833ec6 blob - /dev/null blob + 16420d49703d479678d6b597d94fd2fa002043e6 (mode 644) --- /dev/null +++ .gitignore @@ -0,0 +1 @@ +conf.php blob - /dev/null blob + d01bea1cd42e9bc2884fcc51b34fb95981d93826 (mode 644) --- /dev/null +++ README.md @@ -0,0 +1,46 @@ +## Shortr + +Single PHP URL Shorter + +Database and table needed, build on MariaDB / MySQL: + + CREATE DATABASE IF NOT EXISTS `shortr` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; + USE `shortr`; + + CREATE TABLE `urls` ( + `id` varchar(255) NOT NULL, + `url` text DEFAULT NULL, + `timestamp` timestamp NOT NULL DEFAULT current_timestamp(), + `ip` varchar(255) DEFAULT NULL, + `count` int(11) NOT NULL + ) ENGINE=InnoDB DEFAULT CHARSET=latin1; + + +Example OpenBSD httpd.conf + + server "host.domain.tld" { + listen on $local_v4 port 80 + tcp { nodelay, sack } + log style forwarded + root "/htdocs/host.domain.tld/shortr" + directory { index "index.php" } + location match "^/[%l%u%d]+$" { + request rewrite "/index.php?hash=%1" + } + location "/*.php*" { + fastcgi socket "/run/php-fpm.sock" + } + } + +Configuration file needs to be renamed to conf.php + +Configuration options: + + define("SITE_TITLE", "NAME OF SITE"); + define("BASE_URL", 'https://host.domain.tld/'); + define("DB_HOST", 'localhost'); + define("DB_USER", 'shortr'); + define("DB_PASS", 'RandomStringOfChars'); + define("DB_NAME", 'shortr'); + define("DB_TABLE", 'urls'); + blob - /dev/null blob + 96087a94ed6f53c4e47237e5c979909d89f0dba6 (mode 644) --- /dev/null +++ conf.php-sample @@ -0,0 +1,26 @@ + blob - /dev/null blob + 224319639aa31002ba464838d4443be2adfd98a1 (mode 644) --- /dev/null +++ index.php @@ -0,0 +1,179 @@ + 0) { + $hash = substr($charset, 0, HASH_LENGTH); + } + $result = mysqli_query($mysqli, "INSERT INTO " . DB_TABLE . " (id, url, ip, count) VALUES ('$hash', '$url', '$clientip', '0')"); + if (!mysqli_affected_rows($mysqli)) { + print "FAILURE INSERTING\n"; + } + } + return $hash; +} + +function find_short($hash, $mysqli) { + $result = mysqli_query($mysqli, "SELECT * FROM " . DB_TABLE . " WHERE id='" . mysqli_real_escape_string($mysqli, $hash) . "'"); + if ($row = mysqli_fetch_assoc($result)) { + $link = $row['url']; + mysqli_query($mysqli, "UPDATE " . DB_TABLE . " SET count='" . ($row['count'] + 1) . "' WHERE id='" . $row['id'] . "'"); + + } else { + $link = false; + } + return $link; +} + +if (isset($_POST['url'])) { + $URL = $_POST['url']; + if ($URL != '' && strlen($URL) > 0) { + $db = db_connect(); + $link = generate_short($URL, $db); + } else { + $link = false; + } +} + +if (isset($_GET['hash']) && $_GET['hash'] != '' && strlen($_GET['hash']) > 0) { + $path = explode('/', $_SERVER['REQUEST_URI']); + $uri = $path[count($path)-1]; + if ($uri != '') { + $db = db_connect(); + $link = find_short($uri, $db); + if ($link != '') { + header("Cache-Control: no-cache, must-revalidate"); + header("Expires: Wed, 29 Feb 1984 00:00:00 GMT"); + header("Location: $link", TRUE, 301); + } + } +} + +if ($callback == 'NO') { + $db = db_connect(); + $count = count_urls($db); +?> + + + +<?php print SITE_TITLE ?> + + + + + + + +
+ +
+
+ +
+
+
+Unknown / Invalid URL"; + } else { + if ($link != '') { + echo "" . BASE_URL . $link . ""; + } + } +?> +
+
+
+

+Currently holding entries.


+

+
+ + +