shortr/index.php

195 lines
4.8 KiB
PHP

<?php
require_once './conf.php';
define("SHORTER_NAME", "shortr");
define("SHORTER_VERSION", "v0.4");
define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
$url = "";
$link = "";
$callback = "NO";
$dbh = new PDO('mysql:host='. DB_HOST . ';dbname='. DB_NAME , DB_USER, DB_PASS);
if (DEBUG == 'true') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
} else {
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
}
function generate_short($url, $dbh) {
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'];
}
$sth = $dbh->prepare("SELECT id FROM " . DB_TABLE . " WHERE url=?");
$sth->bindParam(1, $url, PDO::PARAM_STR);
$sth->execute();
if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$hash = $row['id'];
} else {
$hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
$sth = $dbh->prepare("SELECT COUNT(*) FROM " . DB_TABLE . " WHERE id=?");
$sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
$sth->execute();
$loop = 0;
while ($sth->fetchColumn() > 0) {
if ($loop == 10) {
$hash = "ERROR<br />Unable to create hash!";
break;
}
$hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
$sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
$sth->execute();
$loop++;
}
try {
$sth = $dbh->prepare("INSERT INTO " . DB_TABLE . " (id, url, ip, count) VALUES (?, ?, ?, '0')");
$sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
$sth->bindParam(2, $url, PDO::PARAM_STR);
$sth->bindParam(3, $clientip, PDO::PARAM_STR, 255);
$sth->execute();
} catch (PDOException $e) {
$hash = "ERROR<br />Failed to insert hash!";
}
}
return $hash;
}
function find_short($hash, $dbh) {
$sth = $dbh->prepare("SELECT * FROM " . DB_TABLE . " WHERE id=?");
$sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
$sth->execute();
if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$link = $row['url'];
$sth = $dbh->prepare("UPDATE " . DB_TABLE . " SET count = count + 1 WHERE id=?");
$sth->bindParam(1, $row['id'], PDO::PARAM_STR, HASH_LENGTH);
$sth->execute();
} else {
$link = false;
}
return $link;
}
if (isset($_POST['url'])) {
if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
$link = generate_short($_POST['url'], $dbh);
} 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 != '') {
$link = find_short($uri, $dbh);
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') {
$sth = $dbh->query("SELECT COUNT(*) FROM ". DB_TABLE);
$count = $sth->fetchColumn();
?>
<!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>";
} elseif (str_contains($link, "ERROR")) {
echo "<span style='color: red;'>$link</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
}
?>