Blob


1 <?php
2 require_once './conf.php';
4 define("SHORTER_NAME", "shortr");
5 define("SHORTER_VERSION", "v0.4");
6 define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
8 $url = "";
9 $link = "";
10 $callback = "NO";
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);
17 } else {
18 ini_set('display_errors', 0);
19 ini_set('display_startup_errors', 0);
20 }
22 function generate_short($url, $dbh) {
23 if(!preg_match("/^((https?|ftp)[:\/\/].*\/{2,})/i",$url)) {
24 return false;
25 }
26 if (substr($url, 0, strlen(BASE_URL)) == BASE_URL){
27 return false;
28 }
29 if (!empty($_SERVER['HTTP_X_CLIENTIP'])) {
30 $clientip = $_SERVER['HTTP_X_CLIENTIP'];
31 } else {
32 $clientip = $_SERVER['REMOTE_ADDR'];
33 }
34 $sth = $dbh->prepare("SELECT id FROM " . DB_TABLE . " WHERE url=?");
35 $sth->bindParam(1, $url, PDO::PARAM_STR);
36 $sth->execute();
37 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
38 $hash = $row['id'];
39 } else {
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);
43 $sth->execute();
44 $loop = 0;
45 while ($sth->fetchColumn() > 0) {
46 if ($loop == 10) {
47 $hash = "ERROR<br />Unable to create hash!";
48 break;
49 }
50 $hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
51 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
52 $sth->execute();
53 $loop++;
54 }
55 try {
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);
60 $sth->execute();
61 } catch (PDOException $e) {
62 $hash = "ERROR<br />Failed to insert hash!";
63 }
64 }
65 return $hash;
66 }
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);
71 $sth->execute();
72 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
73 $link = $row['url'];
74 $sth = $dbh->prepare("UPDATE " . DB_TABLE . " SET count = count + 1 WHERE id=?");
75 $sth->bindParam(1, $row['id'], PDO::PARAM_STR, HASH_LENGTH);
76 $sth->execute();
77 } else {
78 $link = false;
79 }
80 return $link;
81 }
83 if (isset($_POST['url'])) {
84 if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
85 $link = generate_short($_POST['url'], $dbh);
86 } else {
87 $link = false;
88 }
89 }
91 if (isset($_GET['hash']) && $_GET['hash'] != '' && strlen($_GET['hash']) > 0) {
92 $path = explode('/', $_SERVER['REQUEST_URI']);
93 $uri = $path[count($path)-1];
94 if ($uri != '') {
95 $link = find_short($uri, $dbh);
96 if ($link != '') {
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();
107 ?>
108 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
109 <html>
110 <head>
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">
116 body {
117 background: #282828;
118 color: #ffffff;
119 font-family: Arial,"MS Trebuchet",sans-serif;
120 font-size: 14px;
121 margin: 0;
122 padding: 0;
123 text-align: center;
126 #container {
127 width: 500px;
128 margin: 0 auto;
129 padding: 20px;
130 display: block;
133 #header {
134 font-size: 20px;
135 height: 100px;
136 font-variant: small-caps;
139 #content form input {
140 width: 495px;
143 #shorterurl_wrapper {
144 width: 500px;
145 height: 100px;
146 border: 1px dashed;
147 margin-top: 50px;
148 background-color: #383838;
149 text-align: center;
152 #shorterurl {
153 margin: 30px 30px 30px 30px;
154 font-size: 25px;
155 font-family: Verdana,Arial;
156 font-weight: bold;
158 </style>
159 </head>
160 </html>
161 <body>
162 <div id="container">
163 <div id="header">
164 <h1 id="shortertitle"><i><?php print SITE_TITLE ?></i></h1>
165 </div>
166 <div id="content">
167 <form id="shorterform" method="post">
168 <input id="url" type="text" name="url" value="<?php print $url ?>" />
169 </form>
170 <div id="shorterurl_wrapper">
171 <div id="shorterurl">
172 <?php
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>";
177 } else {
178 if ($link != '') {
179 echo "<span style='color: white;'>" . BASE_URL . $link . "</span>";
182 ?>
183 </div>
184 </div>
185 </div>
186 <p>
187 <small>Currently holding <?php print "$count" ?> entries.<br /><br /><?php print SHORTER_NAME . " " . SHORTER_VERSION ?><br /></small>
188 </p>
189 </div>
190 </body>
191 </html>
192 <?php
194 ?>