shortr/index.php

180 lines
4.3 KiB
PHP

<?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 = mysqli_real_escape_string($mysqli, $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) {
$hash = mysqli_real_escape_string($mysqli, $hash);
$result = mysqli_query($mysqli, "SELECT * FROM " . DB_TABLE . " WHERE id='$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'])) {
if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
$db = db_connect();
$link = generate_short($_POST['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
}
?>