2 require_once './conf.php';
4 define("SHORTER_NAME", "shortr");
5 define("SHORTER_VERSION", "v0.2");
6 define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
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 db_connect() {
23 $dbh = new PDO('mysql:host='. DB_HOST . ';dbname='. DB_NAME , DB_USER, DB_PASS);
27 function count_urls($dbh) {
28 $sth = $dbh->query("SELECT COUNT(*) FROM ". DB_TABLE);
29 return $sth->fetchColumn();
32 function generate_short($url, $dbh) {
33 if(!preg_match("/^((https?|ftp)[:\/\/].*\/{2,})/i",$url)) {
36 if (substr($url, 0, strlen(BASE_URL)) == BASE_URL){
39 if (!empty($_SERVER['HTTP_X_CLIENTIP'])) {
40 $clientip = $_SERVER['HTTP_X_CLIENTIP'];
42 $clientip = $_SERVER['REMOTE_ADDR'];
44 $sth = $dbh->prepare("SELECT id FROM " . DB_TABLE . " WHERE url=?");
45 $sth->bindParam(1, $url, PDO::PARAM_STR);
47 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
50 $hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
51 $sth = $dbh->prepare("SELECT COUNT(*) FROM " . DB_TABLE . " WHERE id=?");
52 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
55 while ($sth->fetchColumn() > 0) {
57 $hash = "ERROR<br />Unable to create hash!";
60 $hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
61 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
65 $sth = $dbh->prepare("INSERT INTO " . DB_TABLE . " (id, url, ip, count) VALUES (?, ?, ?, '0')");
66 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
67 $sth->bindParam(2, $url, PDO::PARAM_STR);
68 $sth->bindParam(3, $clientip, PDO::PARAM_STR, 255);
69 if (!$sth->execute()) {
70 $hash = "ERROR<br />Failed to insert hash!";
76 function find_short($hash, $dbh) {
77 $sth = $dbh->prepare("SELECT * FROM " . DB_TABLE . " WHERE id=?");
78 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
80 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
82 $sth = $dbh->prepare("UPDATE " . DB_TABLE . " SET count = count + 1 WHERE id=?");
83 $sth->bindParam(1, $row['id'], PDO::PARAM_STR, HASH_LENGTH);
91 if (isset($_POST['url'])) {
92 if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
94 $link = generate_short($_POST['url'], $db);
100 if (isset($_GET['hash']) && $_GET['hash'] != '' && strlen($_GET['hash']) > 0) {
101 $path = explode('/', $_SERVER['REQUEST_URI']);
102 $uri = $path[count($path)-1];
105 $link = find_short($uri, $db);
107 header("Cache-Control: no-cache, must-revalidate");
108 header("Expires: Wed, 29 Feb 1984 00:00:00 GMT");
109 header("Location: $link", TRUE, 301);
114 if ($callback == 'NO') {
116 $count = count_urls($db);
118 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
121 <title><?php print SITE_TITLE ?></title>
122 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
123 <meta name="keywords" content="shorter url tinyurl" />
124 <meta name="viewport" content="width=device-width, initial-scale=1" />
125 <style type="text/css" media="screen">
129 font-family: Arial,"MS Trebuchet",sans-serif;
146 font-variant: small-caps;
149 #content form input {
153 #shorterurl_wrapper {
158 background-color: #383838;
163 margin: 30px 30px 30px 30px;
165 font-family: Verdana,Arial;
174 <h1 id="shortertitle"><i><?php print SITE_TITLE ?></i></h1>
177 <form id="shorterform" method="post">
178 <input id="url" type="text" name="url" value="<?php print $url ?>" />
180 <div id="shorterurl_wrapper">
181 <div id="shorterurl">
183 if ($link === false) {
184 echo "<span style='color: red;'>Unknown / Invalid URL</span>";
185 } elseif (str_contains($link, "ERROR")) {
186 echo "<span style='color: red;'>$link</span>";
189 echo "<span style='color: white;'>" . BASE_URL . $link . "</span>";
197 <small>Currently holding <?php print "$count" ?> entries.<br /><br /><?php print SHORTER_NAME . " " . SHORTER_VERSION ?><br /></small>