summaryrefslogtreecommitdiff
path: root/linkshorter/insert.php
diff options
context:
space:
mode:
Diffstat (limited to 'linkshorter/insert.php')
-rw-r--r--linkshorter/insert.php36
1 files changed, 36 insertions, 0 deletions
diff --git a/linkshorter/insert.php b/linkshorter/insert.php
new file mode 100644
index 0000000..94d01a3
--- /dev/null
+++ b/linkshorter/insert.php
@@ -0,0 +1,36 @@
+<?php
+
+if ( $_SERVER['REQUEST_METHOD'] != 'POST'){
+ header($_SERVER['SERVER_PROTOCOL'] . " 301 Moved Permanently");
+ header("Location: http://". $_SERVER["HTTP_HOST"] . "/");
+ exit;
+}
+
+require 'functions.php';
+
+if ( empty($_POST["url"]) || $_POST["url"] == "" ){
+ failure("<p>We need a link to be shortened.</p>", false, "400 Client Failed", "<h1>Missing URL</h1>");
+}
+
+if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){
+ failure("<p>Only schemas like http:// or ftp:// are supported.</p>", false, "400 Client Failed", "<h1>This does not look like an url</h1>");
+}
+
+require 'db.php';
+
+$hash = md5($_POST["url"]);
+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");
+
+ $short="";
+ for ($i=0;$i<5;$i++){
+ $r = mt_rand(0, count($arr)-1);
+ $short.=$arr[$r];
+ }
+
+ $db->set($short, $_POST["url"]);
+ $db->set($hash, $short);;
+}
+
+failure("<p>Your short link for <a href=\"".htmlentities($_POST["url"])."\">".htmlentities($_POST["url"])."</a> is <br> <a href=\"http://".$_SERVER["HTTP_HOST"] . "/-" . $short ."\">http://".$_SERVER["HTTP_HOST"]."/-".$short."</a></p>", false, "200 OK", "<h1>Success</h1>");