2 require_once './conf.php';
4 define("SHORTER_NAME", "shortr");
5 define("SHORTER_VERSION", "v0.4");
6 define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
11 $dbh = new PDO('mysql:host='. DB_HOST . ';dbname='. DB_NAME , DB_USER, DB_PASS);
13 if (DEBUG == 'true') {
14 ini_set('display_errors', 1);
15 ini_set('display_startup_errors', 1);
16 error_reporting(E_ALL);
18 ini_set('display_errors', 0);
19 ini_set('display_startup_errors', 0);
22 function generate_short($url, $dbh) {
23 if(!preg_match("/^((https?|ftp)[:\/\/].*\/{2,})/i",$url)) {
26 if (substr($url, 0, strlen(BASE_URL)) == BASE_URL){
29 if (!empty($_SERVER['HTTP_X_CLIENTIP'])) {
30 $clientip = $_SERVER['HTTP_X_CLIENTIP'];
32 $clientip = $_SERVER['REMOTE_ADDR'];
34 $sth = $dbh->prepare("SELECT id FROM " . DB_TABLE . " WHERE url=?");
35 $sth->bindParam(1, $url, PDO::PARAM_STR);
37 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
40 $hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
41 $sth = $dbh->prepare("SELECT COUNT(*) FROM " . DB_TABLE . " WHERE id=?");
42 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
45 while ($sth->fetchColumn() > 0) {
47 $hash = "ERROR<br />Unable to create hash!";
50 $hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
51 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
56 $sth = $dbh->prepare("INSERT INTO " . DB_TABLE . " (id, url, ip, count) VALUES (?, ?, ?, '0')");
57 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
58 $sth->bindParam(2, $url, PDO::PARAM_STR);
59 $sth->bindParam(3, $clientip, PDO::PARAM_STR, 255);
61 } catch (PDOException $e) {
62 $hash = "ERROR<br />Failed to insert hash!";
68 function find_short($hash, $dbh) {
69 $sth = $dbh->prepare("SELECT * FROM " . DB_TABLE . " WHERE id=?");
70 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
72 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
74 $sth = $dbh->prepare("UPDATE " . DB_TABLE . " SET count = count + 1 WHERE id=?");
75 $sth->bindParam(1, $row['id'], PDO::PARAM_STR, HASH_LENGTH);
83 if (isset($_POST['url'])) {
84 if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
85 $link = generate_short($_POST['url'], $dbh);
91 if (isset($_GET['hash']) && $_GET['hash'] != '' && strlen($_GET['hash']) > 0) {
92 $path = explode('/', $_SERVER['REQUEST_URI']);
93 $uri = $path[count($path)-1];
95 $link = find_short($uri, $dbh);
97 header("Cache-Control: no-cache, must-revalidate");
98 header("Expires: Wed, 29 Feb 1984 00:00:00 GMT");
99 header("Location: $link", TRUE, 301);
104 if ($callback == 'NO') {
105 $sth = $dbh->query("SELECT COUNT(*) FROM ". DB_TABLE);
106 $count = $sth->fetchColumn();
108 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
111 <title><?php print SITE_TITLE ?></title>
112 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
113 <meta name="keywords" content="shorter url tinyurl" />
114 <meta name="viewport" content="width=device-width, initial-scale=1" />
115 <style type="text/css" media="screen">
119 font-family: Arial,"MS Trebuchet",sans-serif;
136 font-variant: small-caps;
139 #content form input {
143 #shorterurl_wrapper {
148 background-color: #383838;
153 margin: 30px 30px 30px 30px;
155 font-family: Verdana,Arial;
164 <h1 id="shortertitle"><i><?php print SITE_TITLE ?></i></h1>
167 <form id="shorterform" method="post">
168 <input id="url" type="text" name="url" value="<?php print $url ?>" />
170 <div id="shorterurl_wrapper">
171 <div id="shorterurl">
173 if ($link === false) {
174 echo "<span style='color: red;'>Unknown / Invalid URL</span>";
175 } elseif (str_contains($link, "ERROR")) {
176 echo "<span style='color: red;'>$link</span>";
179 echo "<span style='color: white;'>" . BASE_URL . $link . "</span>";
187 <small>Currently holding <?php print "$count" ?> entries.<br /><br /><?php print SHORTER_NAME . " " . SHORTER_VERSION ?><br /></small>