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