query('CREATE TABLE urls (url text, hash varchar(6), hits int)'); $db->query('CREATE TABLE referers (ref text, hash varchar(6), hits int)'); system ("chmod 666 $dbnew"); } } else { die ("Unable to open DB"); } function do_query($query) { global $db; $res = $db->query($query); return $res; } function do_query_single($query) { global $db; $res = $db->querySingle($query); return $res; } function get_url_from_hash ($hash) { global $db; $query = "SELECT url from urls where hash='$hash'"; $result = do_query_single($query); return $result; } function do_login () { header('WWW-Authenticate: Basic realm="Login to create short URL"'); header('HTTP/1.0 401 Unauthorized'); echo 'Not authorized to create or modify short URLs'; exit; } $hash = $_GET['hash']; $e = substr ($hash, -1); if ($e == " ") { $details = 1; $dethash = substr ($hash, 0, -1); $hash = 0; } else if ($e == "-") { $delete = 1; $delhash = substr ($hash, 0, -1); $hash = 0; } else { $details = $_GET['details']; $dethash = $_GET['dethash']; $delete = $_GET['delete']; $delhash = $_GET['delhash']; } $gethits = $_GET['hits']; $url = $_GET['url']; if ($url || $details || $gethits || $delete) { $auth = 0; if (!isset($_SERVER['PHP_AUTH_USER'])) { do_login (); } $u = $_SERVER['PHP_AUTH_USER']; $p = $_SERVER['PHP_AUTH_PW']; if ($details && $dethash && !$url) { $url = get_url_from_hash ($dethash); } if ($u != $username || $p != $password) do_login (); else { $auth = 1; } } if ($delete && $delhash) { if ($auth == 0) { echo "Not authorized!"; } else { $query = "delete from urls where hash='$delhash'"; do_query($query); $query = "delete from referers where hash='$delhash'"; do_query($query); $gethits = 1; if ($url) $msg = "

Deleted $base_url/$delhash :: $url

"; } } else if ($url) { if ($auth == 0) { echo "Not authorized!"; } else { if (filter_var($url, FILTER_VALIDATE_URL) == false) { $old_url = $url; $url = urlencode ($url); } if (filter_var($url, FILTER_VALIDATE_URL) == false) { $msg = "Invalid URL $old_url"; } else { $start = 0; if ($chash = rtrim(substr ($_GET['chash'], 0, 6))) { $e = substr ($chash, -1); if ($e == "+" || $e == "-") { $msg = "

Error: $base_url/$chash is reserved for internal use.

"; $query = "select hash from urls where url='$url'"; $hash = do_query_single($query); } else { $query = "SELECT COUNT(*) from urls where hash='$chash'"; if (do_query_single($query) == 1) { $word = "Displaying"; $query = "select hash from urls where url='$url'"; $hash = do_query_single($result); if ($chash != $hash) { $msg = "

Can't use $base_url/$chash, short URL already exists!

"; } } else { $word = "Updated"; $query = "select hash from urls where url='$url'"; $hash = do_query_single ($query); $query = "update urls set hash='$chash' where hash='$hash'"; do_query($query); $query = "update referers set hash='$chash' where hash='$hash'"; do_query($query); $hash = $chash; } } $details = 1; $dethash = $hash; } else { $query = "SELECT hash from urls where url='$url'"; if (($hash = do_query_single($query)) != FALSE) { $word = "Found"; $details = 1; $dethash = $hash; } else { $str = md5 ($url); $start = 6; do { $hash = substr ($str, $start, 6); $query = "SELECT COUNT(*) from urls where hash='$hash'"; $start ++; if ($start == strlen ($str) - 6) { $start = 6; $str = sha1 ($url); } } while (($count = do_query_single ($query)) != 0); $query = "INSERT INTO urls values ('$url', '$hash', 0)"; do_query($query); $word = "Created"; } } $dt = $_GET['dt'] ? $_GET['dt'] . " ": ""; $tstatus = "$dt ($base_url/$hash)"; $tstatus = urlencode ($tstatus); $furl = urlencode ("$base_url/$hash"); $ft = urlencode ($dt); $msg .= "

$word Short URL

$base_url/$hash :: $url

"; } } } else if ($hash = $_GET['hash']) { if ($url = get_url_from_hash ($hash)) { $query = "UPDATE urls set hits=(hits+1) WHERE hash='$hash'"; do_query($query); $ref = $_SERVER['HTTP_REFERER']; if ($ref && filter_var ($ref, FILTER_VALIDATE_URL)) { $query = "SELECT hits from referers where hash='$hash' and ref='$ref'"; if (($hits = do_query_single($query)) != FALSE) { $query = "UPDATE referers set hits=(hits+1) WHERE hash='$hash' and ref='$ref'"; } else { $query = "INSERT into referers values ('$ref', '$hash', 1)"; } do_query ($db, $query); } header ("Location: $url"); } } ?> <? echo $title; ?>

$msg

"); ?> fetchArray()) { echo ("\t\t \t\t\t \t\t\t \t\t\t \t\t\t \t\t"); } ?>
URL Short URL Delete Hits
$arr[0]$base_url/$arr[1][-]$arr[2]
fetchArray()) { $hits = $arr[2]; echo ("

$msg

"); echo ("

Short URL Statistics

"); $query = "select * from referers where hash='$hash'"; $result = do_query($query); ?> fetchArray()) { $sum += $arr[2]; $percent = number_format ($arr[2] * 100 / $hits, 2); echo (""); } $direct = $hits - $sum; $percent = number_format ($direct * 100 / $hits, 2); echo (""); ?>
Referer Hits %
$arr[0] $arr[2] $percent
Direct $direct $percent

Create Short URL