diff options
| author | Horus3 | 2014-11-16 21:20:41 +0100 |
|---|---|---|
| committer | Horus3 | 2014-11-16 21:20:41 +0100 |
| commit | 4168f7aff52f6e7cf7320e42252227dac5169c4a (patch) | |
| tree | f73e5e93e8bc8d9b5ad8ef415b59ede323663f76 /linkshorter | |
| download | tools.iamfabulous.de-4168f7aff52f6e7cf7320e42252227dac5169c4a.tar.gz | |
Initial commit.
Diffstat (limited to 'linkshorter')
| -rw-r--r-- | linkshorter/.gitignore | 4 | ||||
| -rw-r--r-- | linkshorter/db.php | 16 | ||||
| -rw-r--r-- | linkshorter/error.php | 13 | ||||
| -rw-r--r-- | linkshorter/functions.php | 58 | ||||
| -rw-r--r-- | linkshorter/goto.php | 13 | ||||
| -rw-r--r-- | linkshorter/header.php | 18 | ||||
| -rw-r--r-- | linkshorter/index.html | 50 | ||||
| -rw-r--r-- | linkshorter/index.php | 108 | ||||
| -rw-r--r-- | linkshorter/insert.php | 36 | ||||
| -rw-r--r-- | linkshorter/style.css | 21 |
10 files changed, 337 insertions, 0 deletions
diff --git a/linkshorter/.gitignore b/linkshorter/.gitignore new file mode 100644 index 0000000..f7f0c78 --- /dev/null +++ b/linkshorter/.gitignore @@ -0,0 +1,4 @@ +*.swp +*~ +*.tmp +*notmin* diff --git a/linkshorter/db.php b/linkshorter/db.php new file mode 100644 index 0000000..4dcf58a --- /dev/null +++ b/linkshorter/db.php @@ -0,0 +1,16 @@ +<?php + +$db = new Redis(); +$db->connect('/var/run/redis/redis.sock'); + +try { + $db->ping(); +} catch (Exception $e){ + do_output("<p>No connection to the database established.</p>", "500 Server Failure", false, "<h1>Redis went away</h1>"); +} + +try { + $db->select(1); +} catch (Exception $e){ + do_output("<p>No connection to the database established.</p>", "500 Server Failure", false, "<h1>Redis went away</h1>"); +} diff --git a/linkshorter/error.php b/linkshorter/error.php new file mode 100644 index 0000000..92ba713 --- /dev/null +++ b/linkshorter/error.php @@ -0,0 +1,13 @@ +<?php + +require 'functions.php'; +ob_start("sanitize_output"); + +switch($_GET["e"]){ + case("404"): + do_output("<p><strong>The requested url ( ".htmlentities($_SERVER['REQUEST_URI'])." ) wasn't found on this server.</strong></p>", "404 Not Found", false, "<h1>400 - Not Found</h1>"); + break; + + default: + do_output("<p>There was a failure and your request can't be proceeded.</p>", "500 Error", false, "<h1>Error!</h1>"); +} diff --git a/linkshorter/functions.php b/linkshorter/functions.php new file mode 100644 index 0000000..f6f021a --- /dev/null +++ b/linkshorter/functions.php @@ -0,0 +1,58 @@ +<?php + +function do_output($reason, $httpcode, $ajax = true, $heading = NULL){ + header ($_SERVER['SERVER_PROTOCOL'] . " " . $httpcode); + if( $ajax ){ + echo $reason; + ob_end_flush(); + exit; + } +?> +<!doctype html> +<html> +<head> + <meta charset="utf-8"> + <title>Link Shorter</title> + <!--style>html{position:relative;min-height:100%}body{margin-bottom:60px}.footer{position:absolute;bottom:0;width:100%}#copyright-text{text-decoration:underline;color:#333}</style--> + <style> + <?php echo file_get_contents("../tools/style.css"); ?> + </style> + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> + <link rel='shortcut icon' href='../tools/favicon.ico' type='image/x-icon'> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> +</head> + <?php require("../tools/navbar.php"); ?> +<div class="container text-center pagination-centered"> + <div class="row"> + <?php echo $heading; ?> + <hr> + </div> + <div class="text-center"> + <?php echo $reason; ?> + </div> +</div> + <?php require("../tools/footer.php"); ?> +</body> +<?php + ob_end_flush(); + exit; +} + +function sanitize_output($buffer) { + + $search = array( + '/\>[^\S ]+/s', // strip whitespaces after tags, except space + '/[^\S ]+\</s', // strip whitespaces before tags, except space + '/(\s)+/s' // shorten multiple whitespace sequences + ); + + $replace = array( + '>', + '<', + '\\1' + ); + + $buffer = preg_replace($search, $replace, $buffer); + + return $buffer; +} diff --git a/linkshorter/goto.php b/linkshorter/goto.php new file mode 100644 index 0000000..d77e1e2 --- /dev/null +++ b/linkshorter/goto.php @@ -0,0 +1,13 @@ +<?php +require 'functions.php'; +ob_start("sanitize_output"); +require 'db.php'; + +$url = $db->get($_GET["goto"]); +if( ! $url || $url == "" ){ + do_output("<p>This url wasn't found on this server.</p>", "404 Not Found", false, "<h1>404 - Not found</h1>"); +} + +header($_SERVER['SERVER_PROTOCOL'] . " 301 Moved Permanently"); +header("Location: ".$url); +exit; diff --git a/linkshorter/header.php b/linkshorter/header.php new file mode 100644 index 0000000..c207321 --- /dev/null +++ b/linkshorter/header.php @@ -0,0 +1,18 @@ + <nav class="navbar navbar-default navbar-custom" role="navigation"> + <div class="container"> + <div class="navbar-header"> + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbarCollapse"> + <span class="sr-only">Toggle navigation</span> + <span class="icon-bar"></span> + </button> + <a class="navbar-brand" href="/">Home</a> + </div> + <div class="collapse navbar-collapse" id="navbarCollapse"> + <ul class="nav navbar-nav"> + <li> + <a href="/rules" >Help</a> + </li> + </ul> + </div> + </div> + </nav> diff --git a/linkshorter/index.html b/linkshorter/index.html new file mode 100644 index 0000000..62ff295 --- /dev/null +++ b/linkshorter/index.html @@ -0,0 +1,50 @@ +<!doctype html> +<html> +<head> +<meta charset=utf-8> +<title>Link Shorter</title> +<link rel='shortcut icon' href='/favicon.ico' type='image/x-icon'> +<link rel=stylesheet href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> +<style>html{position:relative;min-height:100%}body{margin-bottom:60px}.footer{position:absolute;bottom:0;width:100%}#copyright-text{text-decoration:underline;color:#333}</style> +<meta name=viewport content="width=device-width, initial-scale=1.0"> +</head> +<nav class="navbar navbar-default navbar-custom" role=navigation> +<div class=container> +<div class=navbar-header> +<button type=button class=navbar-toggle data-toggle=collapse data-target="#navbarCollapse"> +<span class=sr-only>Toggle navigation</span> +<span class=icon-bar></span> +</button> +<a class=navbar-brand href="">Home</a> +</div> +</div> +</nav> +<div class=container> +<div class=text-center> +<div class="row center-block vertical-center"> +<form class="form-horizontal " method=POST action=insert> +<fieldset> +<legend class=text-centered><h1>Amazing Linkshorter</h1></legend> +<div class=form-group> +<label class="col-md-4 control-label" for=url>Link:</label> +<div class=col-md-5> +<input id=url name=url placeholder="http://www.moehm.org/" class="form-control input-md" required> +</div> +</div> +<div class=form-group> +<label class="col-md-4 control-label" for=singlebutton></label> +<div class=col-md-4> +<button id=singlebutton name=singlebutton class="btn btn-info" type=submit>Short!</button> +</div> +</div> +</fieldset> +</form> +</div> +</div> +</div> +<div class="footer text-right"> +<div class=container> +<p> Copyright 2014 <a id=copyright-text href="//www.moehm.org/" target=_blank>Maximilian Möhring</a></p> +</div> +</div> +</body> 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 @@ +<?php +require 'functions.php'; +require 'db.php'; + +ob_start("sanitize_output"); + +if ( $_SERVER['REQUEST_METHOD'] != 'POST'){ + + $key = "lscache_" . md5( strtolower($_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].$_SERVER["QUERY_STRING"])); + if ( $db->exists($key) ) { + header("X-Cache: Hit"); + echo $db->get($key); + ob_end_flush(); + exit; + } + +?> +<!doctype html> +<html> +<head> + <meta charset="utf-8"> + <title>Link Shorter</title> + <!--style>html{position:relative;min-height:100%}body{margin-bottom:60px}.footer{position:absolute;bottom:0;width:100%}#copyright-text{text-decoration:underline;color:#333}</style--> + <style> + <?php echo file_get_contents("../tools/style.css"); ?> + </style> + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> + <link rel='shortcut icon' href='../tools/favicon.ico' type='image/x-icon'> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> +</head> + <?php require("../tools/navbar.php"); ?> + <div class="container"> + <div class="text-center"> + <div class="row center-block vertical-center"> + <form class="form-horizontal " method="POST"> + <fieldset> + + <legend class="text-centered"><h1>Amazing Linkshorter</h1></legend> + + <div class="form-group"> + <label class="col-md-4 control-label" for="url">Link:</label> + <div class="col-md-5"> + <input id="url" name="url" placeholder="http://www.moehm.org/" class="form-control input-md" required="" type="text"> + + </div> + </div> + + <div class="form-group"> + <label class="col-md-4 control-label" for="short">(optional)</label> + <div class="col-md-4"> + <input id="short" name="short" placeholder="Your own query string here." class="form-control input-md" type="text"> + + </div> + </div> + + <div class="form-group"> + <label class="col-md-4 control-label" for="singlebutton"></label> + <div class="col-md-4"> + <button id="singlebutton" name="singlebutton" class="btn btn-info" type="submit">Short!</button> + </div> + </div> + + </fieldset> + </form> + </div> + </div> + </div> + <?php require("../tools/footer.php"); ?> +</body> +<?php + $html = ob_get_contents(); + $db->set($key, $html, 3600); + ob_end_flush(); + +} else { + + if ( empty($_POST["url"]) || $_POST["url"] == "" ){ + do_output("<p>We need a link to be shortened.</p>", "400 Client Failed", false, "<h1>Missing URL</h1>"); + } + + if ( ! preg_match("/^[a-z]+:\/\/[a-z0-9_]+/i", $_POST["url"]) ){ + do_output("<p>Only schemas like http:// or ftp:// are supported.</p>", "400 Client Failed", false, "<h1>This does not look like an url</h1>"); + } + + + $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("<p>Someone else has already a registered entry under '".htmlentities($short)."'.</p>", "422 Unprocessable Entity", false, "<h1>Query string already exists.</h1>"); + } 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("<p>Your short link for <a href=\"".htmlentities($_POST["url"])."\">".htmlentities($_POST["url"])."</a> is <br> http://".$_SERVER["HTTP_HOST"]."/".$short."</p>", "200 OK", false, "<h1>Success</h1>"); +} 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>"); diff --git a/linkshorter/style.css b/linkshorter/style.css new file mode 100644 index 0000000..c762039 --- /dev/null +++ b/linkshorter/style.css @@ -0,0 +1,21 @@ +html { + position: relative; + min-height: 100%; +} + +body { + margin-bottom: 60px; +} + +/* footer */ + +.footer { + position: absolute; + bottom: 0; + width: 100%; +} + +#copyright-text { + text-decoration: underline; + color: #333; +} |
