summaryrefslogtreecommitdiff
path: root/linkshorter/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'linkshorter/index.php')
-rw-r--r--linkshorter/index.php108
1 files changed, 108 insertions, 0 deletions
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>");
+}