From 4f2fd52fe48ec5ce21dcd144744d915a4994f453 Mon Sep 17 00:00:00 2001 From: alopexc0de Date: Mon, 24 Dec 2012 02:30:02 -0500 Subject: [PATCH] Cleaned up source + commented all php + HTML4 clean --- short/index.php | 234 +++++++++++++++++++++++++----------------------- 1 file changed, 121 insertions(+), 113 deletions(-) diff --git a/short/index.php b/short/index.php index d6e8260..05a389e 100755 --- a/short/index.php +++ b/short/index.php @@ -1,132 +1,140 @@ - + + + URL Shortner + + + + + + + + + -function input($input){ - if ($input == null) die("No Input Provided, Aborting\r\n
"); +"); $input = mysql_real_escape_string($input); return $input; } -function prepOutputText($text) { - if ($text == null) die("No Input Provided, Aborting\r\n
"); +function prepOutputText($text) { // Takes mysql string and makes it able to be used + if ($text == null) die("No Input Provided, Aborting\r\n
"); $output = htmlentities(stripslashes($text),ENT_QUOTES); return $output; } -/*function is_available($url, $timeout = 30) { - $ch = curl_init(); // get cURL handle - $opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser - CURLOPT_URL => $url, // set URL - CURLOPT_NOBODY => true, // do a HEAD request only - CURLOPT_TIMEOUT => $timeout); // set timeout - curl_setopt_array($ch, $opts); - curl_exec($ch); // do it! - $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK - curl_close($ch); // close handle - return $retval; -}*/ +function getServerStatus($ip, $port = 80) { // Tests if host is online or not + return @fsockopen($ip, $port, $errnum, $errstr) == false ? 'offline' : 'online'; +} -function GetServerStatus($site, $port){ - $status = array("OFFLINE", "ONLINE"); - $fp = @fsockopen($site, $port, $errno, $errstr, 2); - if (!$fp){ - return $status[0]; - } else{ - return $status[1]; - } -} +$submit; // Declare this since my logs are annoying me to no end x.x -$submit; - -if(isset($_GET['l'])) { - $l = $_GET['l']; - $l = input($l); - $sql = "SELECT id, link, shortlink FROM $tbl_name WHERE shortlink='$l'"; - $result = mysql_query($sql); - $count = mysql_num_rows($result); - if($count == 1){ - while ($row = mysql_fetch_assoc($result)){ // Attempt to pull all data concerning that one user from table - $id = $row['id']; - $link = $row['link']; - $short = $row['shortlink']; - mysql_close(); - echo "Redirecting you to your site, please wait..."; - $link = prepOutputText($link); - header("location:".$link); +if(isset($_GET['l'])) { // if there is a link... + $l = $_GET['l']; // Bring the link into a variable + $l = input($l); // Clean said variable to be used in mysql + $sql = "SELECT id, link, shortlink FROM $tbl_name WHERE shortlink='$l'"; // Find the link in the table + $result = mysql_query($sql); // mysql stuff + $count = mysql_num_rows($result); // How many rows were found? + if($count == 1){ // There should only be one row + while ($row = mysql_fetch_assoc($result)){ // Start pulling data from the row + $id = $row['id']; // Lets get the id - not used, just a number used to identify the link or something, it's unique + $link = $row['link']; // This is the full link + $short = $row['shortlink']; // This is the short link - Should be a unique number + mysql_close(); // close the mysql connection + echo "Redirecting you to your site, please wait..."; // never seen + $link = prepOutputText($link); // make the link work + header("location:".$link); // redirect right away } }else{ - echo "Hmmm... It appears that your link doesn't exist in my database. Try again?"; - header("location:http://unps.us/"); + echo "Hmmm... It appears that your link doesn't exist in my database. Try again?"; // not seen + header("location:http://unps.us/"); // redirect right away } } ?> -URL Shortner - - -
-
-

Welcome to the UnPS-GAMA link shortner


-

All you gotta do is put a link into the box and click submit

- -
-

Destination:

- -
-Here's the link: http://unps.us/?l=$short"; - } - }else{ - if (strpos($dest, 'http://') === false) { - if (strpos($dest, 'https://') === false){ - $ip = gethostbyname($dest); - $dest = 'http://'.$dest; + +
+ Header image
+

Welcome to the UnPS-GAMA link shortner


+

All you gotta do is put a link into the box and click submit

+ + + +
+

Destination:
+

+ +
+ + Here's the link: http://unps.us/?l=$short"; // Return link that's already in table + } + }else{ // If no row is returned, it is assumed that the link isn't there + if (strpos(strtolower($dest), 'http://') === false) { // Simple test to see if http:// not part of the link + if (strpos(strtolower($dest), 'https://') === false){ // Simple test to see if https:// not part of the link passing the above check + $ip = gethostbyname($dest); // Get IP address of link + $dest = 'http://'.$dest; // Add http:// to link + } + } + if (strpos(strtolower($dest), 'http://') !== false) { // Simple test to see if http:// is part of the link + if (strpos(strtolower($dest), 'https://') !== false){ // Simple test to see if https:// is part of the link failing the above check + $dest = str_replace("https://", "", $dest); // If https:// exists, remove it + $ip = gethostbyname($dest); // Get IP address of link + $dest = 'https://'.$dest; // Re-add https:// to link + } + $dest = str_replace("http://", "", $dest); // if http:// exists, remove it + $ip = gethostbyname($dest); // Get IP address of link + $dest = 'http://'.$dest; // Re-add http:// to link + } + if(GetServerStatus($ip, 80) != "online") die("Hmmm it seems that your link is dead.\r\nPlease try again"); // Check to see if the host is alive + $short = substr(number_format(time() * rand(),0,'',''),0,10); // Create a random number 10 digits long + $short = base_convert($short, 10, 36); // Convert the 10 digit random number into Base36 + $sql="INSERT INTO $tbl_name (link, shortlink) VALUES ('$dest', '$short')"; // Try to add the link and short link into table + $result=mysql_query($sql); // mysql stuff + if($result){ // If the link is added to the table + echo "It appears that I have succeded in making a short link.
You'll find it here: http://unps.us/?l=$short "; + }else { // If the link is not added to the table + echo "There was a problem trying to register your link - Could be a database error"; + } + } } - } - if (strpos($dest, 'http://') !== false) { - if (strpos($dest, 'https://') !== false){ - $dest = str_replace("https://", "", $dest); - $ip = gethostbyname($dest); - $dest = 'https://'.$dest; + if(!$dest){ // If the textbox was empty when loaded + echo ' +

Sorry, you are not able to shorten something without a url

\r\n + Back to index
\r\n + Home + '; + } } - $dest = str_replace("http://", "", $dest); - $ip = gethostbyname($dest); - $dest = 'http://'.$dest; - } - if(GetServerStatus($ip, 80) != "ONLINE") die("Hmmm it seems that your link is dead.\r\nPlease try again"); - $short = substr(number_format(time() * rand(),0,'',''),0,10); - $short = base_convert($short, 10, 36); - $sql="INSERT INTO $tbl_name (link, shortlink) VALUES ('$dest', '$short')"; - $result=mysql_query($sql); - if($result){ - echo "It appears that I have succeded in making a short link.
You'll find it here: http://unps.us/?l=$short "; - }else { - echo "There was a problem trying to register your link - Could be a database error"; - } - } - } - if(!$dest){ - echo ' - Sorry, you are not able to shorten something without a url
- Back to index
- Home - '; - } -} -mysql_close(); -?> + mysql_close(); // Close any mysql connections still open + ?> +
+ +