From 4168f7aff52f6e7cf7320e42252227dac5169c4a Mon Sep 17 00:00:00 2001 From: Horus3 Date: Sun, 16 Nov 2014 21:20:41 +0100 Subject: Initial commit. --- linkshorter/insert.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 linkshorter/insert.php (limited to 'linkshorter/insert.php') 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 @@ +We need a link to be shortened.

", false, "400 Client Failed", "

Missing URL

"); +} + +if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){ + failure("

Only schemas like http:// or ftp:// are supported.

", false, "400 Client Failed", "

This does not look like an url

"); +} + +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("

Your short link for ".htmlentities($_POST["url"])." is
http://".$_SERVER["HTTP_HOST"]."/-".$short."

", false, "200 OK", "

Success

"); -- cgit v1.2.3