summaryrefslogtreecommitdiff
path: root/linkshorter/index.php
blob: 1d56be5838fe60c87cb745cbc39e2043222ec8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require 'functions.php';
require 'db.php';

//ob_start("sanitize_output");
ob_start();

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>
	<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
	<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
	<style>
	<?php echo file_get_contents("../tools/style.css"); ?>
	</style>
	<noscript><style>.navbar{margin-bottom:0;}</style></noscript>
	<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>
					<p>Short your link and use a easy to remembery query string</p>
					</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-primary" 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://s.moehm.org/".$short."</p>", "200 OK", false, "<h1>Success</h1>");
}