Commit Diff


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 @@
+<?php
+ini_set('display_errors', 1);
+ini_set('display_startup_errors', 1);
+error_reporting(E_ALL);
+mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
+
+define("SITE_TITLE", "jn.gs");
+define("BASE_URL", 'http://jn.gs/');
+define("DB_HOST", 'localhost');
+define("DB_USER", 'shortr');
+define("DB_PASS", 'RandomStringOfChars');
+define("DB_NAME", 'shortr');
+define("DB_TABLE", 'urls');
+
+/*
+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;
+*/
+>
blob - /dev/null
blob + 224319639aa31002ba464838d4443be2adfd98a1 (mode 644)
--- /dev/null
+++ index.php
@@ -0,0 +1,179 @@
+<?php
+require_once './conf.php';
+
+define("SHORTER_NAME", "shortr");
+define("SHORTER_VERSION", "v0.1");
+define("HASH_LENGTH", 4);
+define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
+
+$url = "";
+$link = "";
+$callback = "NO";
+
+function db_connect() {
+	if (!$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME)) {
+		return false;
+	}
+	return $mysqli;
+}
+
+function count_urls($mysqli) {
+	$count = mysqli_num_rows(mysqli_query($mysqli, "SELECT * FROM ". DB_TABLE));
+	return $count;
+}
+
+function generate_short($url, $mysqli) {
+	$url = stripslashes($url);
+	if(!preg_match("/^((https?|ftp)[:\/\/].*\/{2,})/i",$url)) {
+		return false;
+	}
+	if (substr($url, 0, strlen(BASE_URL)) == BASE_URL){
+		return false;
+	}
+	if (!empty($_SERVER['HTTP_X_CLIENTIP'])) {
+		$clientip = $_SERVER['HTTP_X_CLIENTIP'];
+	} else {
+		$clientip = $_SERVER['REMOTE_ADDR'];
+	}
+	$result = mysqli_query($mysqli, "SELECT id FROM " . DB_TABLE . " WHERE url='$url'");
+	if ($row = mysqli_fetch_assoc($result)) {
+		$hash = $row['id'];
+	} else {
+		$charset = str_shuffle(CHARSET);
+		$hash = substr($charset, 0, HASH_LENGTH);
+		while (mysqli_num_rows(mysqli_query($mysqli, "SELECT * FROM " . DB_TABLE . " WHERE id='$hash'")) > 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);
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+<head>
+<title><?php print SITE_TITLE ?></title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
+<meta name="keywords" content="shorter url tinyurl" /> 
+<meta name="viewport" content="width=device-width, initial-scale=1" />
+<style type="text/css" media="screen">
+body {
+	background: #282828;
+	color: #ffffff;
+	font-family: Arial,"MS Trebuchet",sans-serif;
+	font-size: 14px;
+	margin: 0;
+	padding: 0;
+	text-align: center;
+}
+
+#container {
+	width: 500px;
+	margin: 0 auto;
+	padding: 20px;
+	display: block;
+}
+
+#header {
+	font-size: 20px;
+	height: 100px;
+	font-variant: small-caps;
+}
+
+#content form input {
+	width: 495px;
+}
+
+#shorterurl_wrapper {
+	width: 500px;
+	height: 100px;
+	border: 1px dashed;
+	margin-top: 50px;
+	background-color: #383838;
+	text-align: center;
+}
+
+#shorterurl {
+	margin: 30px 30px 30px 30px;
+	font-size: 25px;
+	font-family: Verdana,Arial;
+	font-weight: bold;
+}
+</style>
+</head> 
+</html>
+<body>
+<div id="container">
+<div id="header">
+<h1 id="shortertitle"><i><?php print SITE_TITLE ?></i></h1>
+</div>
+<div id="content">
+<form id="shorterform" method="post">
+<input id="url" type="text" name="url" value="<?php print $url ?>" />
+</form>
+<div id="shorterurl_wrapper">
+<div id="shorterurl">
+<?php 
+	if ($link === false) {
+		echo "<span style='color: red;'>Unknown / Invalid URL</span>"; 
+	} else {
+		if ($link != '') {
+			echo "<span style='color: white;'>" . BASE_URL . $link . "</span>";
+		}
+	}
+?>
+</div>
+</div>
+</div>
+<p>
+<small>Currently holding <?php print "$count" ?> entries.<br /><br /><?php print SHORTER_NAME . " " . SHORTER_VERSION ?><br /></small>
+</p>
+</div>
+</body>
+</html>
+<?php
+}
+?>