Blob


1 <?php
2 require_once './conf.php';
4 define("SHORTER_NAME", "shortr");
5 define("SHORTER_VERSION", "v0.2");
6 define("CHARSET", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
8 $url = "";
9 $link = "";
10 $callback = "NO";
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 db_connect() {
23 $dbh = new PDO('mysql:host='. DB_HOST . ';dbname='. DB_NAME , DB_USER, DB_PASS);
24 return $dbh;
25 }
27 function count_urls($dbh) {
28 $sth = $dbh->query("SELECT COUNT(*) FROM ". DB_TABLE);
29 return $sth->fetchColumn();
30 }
32 function generate_short($url, $dbh) {
33 if(!preg_match("/^((https?|ftp)[:\/\/].*\/{2,})/i",$url)) {
34 return false;
35 }
36 if (substr($url, 0, strlen(BASE_URL)) == BASE_URL){
37 return false;
38 }
39 if (!empty($_SERVER['HTTP_X_CLIENTIP'])) {
40 $clientip = $_SERVER['HTTP_X_CLIENTIP'];
41 } else {
42 $clientip = $_SERVER['REMOTE_ADDR'];
43 }
44 $sth = $dbh->prepare("SELECT id FROM " . DB_TABLE . " WHERE url=?");
45 $sth->bindParam(1, $url, PDO::PARAM_STR);
46 $sth->execute();
47 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
48 $hash = $row['id'];
49 } else {
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);
53 $sth->execute();
54 $loop = 0;
55 while ($sth->fetchColumn() > 0) {
56 if ($loop == 10) {
57 $hash = "ERROR<br />Unable to create hash!";
58 break;
59 }
60 $hash = substr(str_shuffle(CHARSET), 0, HASH_LENGTH);
61 $sth->bindParam(1, $hash, PDO::PARAM_STR, HASH_LENGTH);
62 $sth->execute();
63 $loop++;
64 }
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!";
71 }
72 }
73 return $hash;
74 }
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);
79 $sth->execute();
80 if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
81 $link = $row['url'];
82 $sth = $dbh->prepare("UPDATE " . DB_TABLE . " SET count = count + 1 WHERE id=?");
83 $sth->bindParam(1, $row['id'], PDO::PARAM_STR, HASH_LENGTH);
84 $sth->execute();
85 } else {
86 $link = false;
87 }
88 return $link;
89 }
91 if (isset($_POST['url'])) {
92 if ($_POST['url'] != '' && strlen($_POST['url']) > 0) {
93 $db = db_connect();
94 $link = generate_short($_POST['url'], $db);
95 } else {
96 $link = false;
97 }
98 }
100 if (isset($_GET['hash']) && $_GET['hash'] != '' && strlen($_GET['hash']) > 0) {
101 $path = explode('/', $_SERVER['REQUEST_URI']);
102 $uri = $path[count($path)-1];
103 if ($uri != '') {
104 $db = db_connect();
105 $link = find_short($uri, $db);
106 if ($link != '') {
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') {
115 $db = db_connect();
116 $count = count_urls($db);
117 ?>
118 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
119 <html>
120 <head>
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">
126 body {
127 background: #282828;
128 color: #ffffff;
129 font-family: Arial,"MS Trebuchet",sans-serif;
130 font-size: 14px;
131 margin: 0;
132 padding: 0;
133 text-align: center;
136 #container {
137 width: 500px;
138 margin: 0 auto;
139 padding: 20px;
140 display: block;
143 #header {
144 font-size: 20px;
145 height: 100px;
146 font-variant: small-caps;
149 #content form input {
150 width: 495px;
153 #shorterurl_wrapper {
154 width: 500px;
155 height: 100px;
156 border: 1px dashed;
157 margin-top: 50px;
158 background-color: #383838;
159 text-align: center;
162 #shorterurl {
163 margin: 30px 30px 30px 30px;
164 font-size: 25px;
165 font-family: Verdana,Arial;
166 font-weight: bold;
168 </style>
169 </head>
170 </html>
171 <body>
172 <div id="container">
173 <div id="header">
174 <h1 id="shortertitle"><i><?php print SITE_TITLE ?></i></h1>
175 </div>
176 <div id="content">
177 <form id="shorterform" method="post">
178 <input id="url" type="text" name="url" value="<?php print $url ?>" />
179 </form>
180 <div id="shorterurl_wrapper">
181 <div id="shorterurl">
182 <?php
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>";
187 } else {
188 if ($link != '') {
189 echo "<span style='color: white;'>" . BASE_URL . $link . "</span>";
192 ?>
193 </div>
194 </div>
195 </div>
196 <p>
197 <small>Currently holding <?php print "$count" ?> entries.<br /><br /><?php print SHORTER_NAME . " " . SHORTER_VERSION ?><br /></small>
198 </p>
199 </div>
200 </body>
201 </html>
202 <?php
204 ?>