From 4168f7aff52f6e7cf7320e42252227dac5169c4a Mon Sep 17 00:00:00 2001
From: Horus3
Date: Sun, 16 Nov 2014 21:20:41 +0100
Subject: Initial commit.
---
linkshorter/index.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 108 insertions(+)
create mode 100644 linkshorter/index.php
(limited to 'linkshorter/index.php')
diff --git a/linkshorter/index.php b/linkshorter/index.php
new file mode 100644
index 0000000..75ac9aa
--- /dev/null
+++ b/linkshorter/index.php
@@ -0,0 +1,108 @@
+exists($key) ) {
+ header("X-Cache: Hit");
+ echo $db->get($key);
+ ob_end_flush();
+ exit;
+ }
+
+?>
+
+
+
+
+ Link Shorter
+
+
+
+
+
+
+
+
+
+
+set($key, $html, 3600);
+ ob_end_flush();
+
+} else {
+
+ if ( empty($_POST["url"]) || $_POST["url"] == "" ){
+ do_output("We need a link to be shortened.
", "400 Client Failed", false, "Missing URL
");
+ }
+
+ if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){
+ do_output("Only schemas like http:// or ftp:// are supported.
", "400 Client Failed", false, "This does not look like an url
");
+ }
+
+
+ $hash = md5($_POST["url"]);
+ if( ! empty($_POST["short"]) && $_POST["short"] != "" ) {
+ $short = $_POST["short"];
+ if ( $db->exists($short) == 1 && $_POST["url"] != $db->get($short) )
+ do_output("Someone else has already a registered entry under '".htmlentities($short)."'.
", "422 Unprocessable Entity", false, "Query string already exists.
");
+ } else {
+ if( ! $short = $db->get($hash) ){
+ $arr = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
+
+ do {
+ $short="";
+ for ($i=0;$i<5;$i++){
+ $r = mt_rand(0, count($arr)-1);
+ $short.=$arr[$r];
+ }
+ } while ( $db->exists($short) );
+ $db->set($hash, $short);
+ }
+ }
+ $db->set($short, $_POST["url"]);
+
+ do_output("Your short link for ".htmlentities($_POST["url"])." is
http://".$_SERVER["HTTP_HOST"]."/".$short."
", "200 OK", false, "Success
");
+}
--
cgit v1.2.3