2022-08-12 09:13:25 +02:00
|
|
|
<?php
|
|
|
|
require_once './conf.php';
|
|
|
|
|
|
|
|
define("SHORTER_NAME", "shortr");
|
2022-08-24 19:34:52 +02:00
|
|
|
define("SHORTER_VERSION", "v0.4");
|
2022-08-12 09:13:25 +02:00
|
|
|
define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
|
|
|
|
|
|
|
|
$url = "";
|
|
|
|
$link = "";
|
|
|
|
$callback = "NO";
|
|
|
|
|
2022-08-24 15:13:17 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:13:25 +02:00
|
|
|
function db_connect() {
|
2022-08-24 15:04:16 +02:00
|
|
|
$dbh = new PDO('mysql:host='. DB_HOST . ';dbname='. DB_NAME , DB_USER, DB_PASS);
|
|
|
|
return $dbh;
|
2022-08-12 09:13:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 15:04:16 +02:00
|
|
|
function count_urls($dbh) {
|
2022-08-24 15:06:34 +02:00
|
|
|
$sth = $dbh->query("SELECT COUNT(*) FROM ". DB_TABLE);
|
2022-08-24 15:04:16 +02:00
|
|
|
return $sth->fetchColumn();
|
2022-08-12 09:13:25 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 15:04:16 +02:00
|
|
|
function generate_short($url, $dbh) {
|
2022-08-12 09:13:25 +02:00
|
|
|
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'];
|
|
|
|
}
|
2022-08-24 15:04:16 +02:00
|
|
|
$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)) {
|
2022-08-12 09:13:25 +02:00
|
|
|
$hash = $row['id'];
|
|
|
|
} else {
|
2022-08-24 17:19:25 +02:00
|
|
|
$hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
|
2022-08-24 15:04:16 +02:00
|
|
|
$sth = $dbh->prepare("SELECT COUNT(*) FROM " . DB_TABLE . " WHERE id=?");
|
|
|
|
$sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
|
|
|
|
$sth->execute();
|
2022-08-24 17:19:25 +02:00
|
|
|
$loop = 0;
|
2022-08-24 15:04:16 +02:00
|
|
|
while ($sth->fetchColumn() > 0) {
|
2022-08-24 17:19:25 +02:00
|
|
|
if ($loop == 10) {
|
|
|
|
$hash = "ERROR<br />Unable to create hash!";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
|
2022-08-24 15:04:16 +02:00
|
|
|
$sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
|
|
|
|
$sth->execute();
|
2022-08-24 17:19:25 +02:00
|
|
|
$loop++;
|
2022-08-12 09:13:25 +02:00
|
|
|
}
|
2022-08-24 19:34:31 +02:00
|
|
|
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) {
|
2022-08-24 17:19:25 +02:00
|
|
|
$hash = "ERROR<br />Failed to insert hash!";
|
2022-08-12 09:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:04:16 +02:00
|
|
|
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)) {
|
2022-08-12 09:13:25 +02:00
|
|
|
$link = $row['url'];
|
2022-08-24 15:04:16 +02:00
|
|
|
$sth = $dbh->prepare("UPDATE " . DB_TABLE . " SET count = count + 1 WHERE id=?");
|
|
|
|
$sth->bindParam(1, $row['id'], PDO::PARAM_STR, HASH_LENGTH);
|
|
|
|
$sth->execute();
|
2022-08-12 09:13:25 +02:00
|
|
|
} else {
|
|
|
|
$link = false;
|
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_POST['url'])) {
|
2022-08-12 10:07:22 +02:00
|
|
|
if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
|
2022-08-12 09:13:25 +02:00
|
|
|
$db = db_connect();
|
2022-08-12 11:41:03 +02:00
|
|
|
$link = generate_short($_POST['url'], $db);
|
2022-08-12 09:13:25 +02:00
|
|
|
} 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>";
|
2022-08-24 17:19:25 +02:00
|
|
|
} elseif (str_contains($link, "ERROR")) {
|
|
|
|
echo "<span style='color: red;'>$link</span>";
|
2022-08-12 09:13:25 +02:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
?>
|