-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.php
110 lines (99 loc) · 4.5 KB
/
index.php
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
<?php
include("inc/bdd.php");
if (isset($_GET['site']) && !empty($_GET['site'])) { //url shortened
/* BDD plan
TABLE : shortener
short | url | comment | views | username | date
*/
$site = $_GET['site'];
$get_site = $connexion->prepare('SELECT url, short, views FROM shortener WHERE short=?');
$get_site->execute(array($site));
$res_site = $get_site->fetch(PDO::FETCH_ASSOC);
if ($res_site) //if it exists
{
$views_plus_1 = $res_site['views'] + 1;
$query_update = $connexion->prepare('UPDATE shortener SET views=? WHERE short=?');
$query_update->execute(array($views_plus_1, $res_site['short']));
header('Location: ' . $res_site['url']);
} else {
header('Location: ' . DEFAULT_URL);
}
exit();
}
session_start(['cookie_lifetime' => '1728000', 'name' => 'shortener', 'cookie_httponly' => true, 'cookie_secure' => true]);
if(!empty($_SESSION['username'])){
$username = $_SESSION['username'];
$token = $_SESSION['token'];
}
if (PUBLIC_INSTANCE == 'true' and empty($username)){
$username = 'UNKNOWN';
$token = '';
}
if (PUBLIC_INSTANCE != 'true' and empty($username)) {
$token = '';
}
include 'inc/header.php';
// Main page
$code_js = 'javascript:(function () {var d = document;var w = window;var enc = encodeURIComponent;var f =\' ' . DEFAULT_URL . '\';var l = d.location;var p = \'shorten.php?url=\' + enc(l.href) + \'&comment=\' + enc(d.title) + \'&token=' . $token . '\';var u = f + p;var a = function () {if (!w.open(u))l.href = u;};if (/Firefox/.test(navigator.userAgent))setTimeout(a, 0); else a();void(0);})()';
?>
<a class="forkit" href="https://github.com/azlux/Simple-URL-Shortener/">
<span>Fork me on GitHub!</span>
<span>Get free cookie!</span>
</a>
<script>
async function checkCustomUrlAvailable(e){
const {value} = e.target
if(value === ""){
return disableCustomUrlField(false)
}
const result = await fetch('shorten.php', {method: "POST", headers: {"Content-Type": "application/x-www-form-urlencoded"}, body: `is_short_free=${value}`})
if(!result.ok){
return disableCustomUrlField(true)
}
try{
const {ok} = await result.json()
disableCustomUrlField(!ok)
}
catch(e){
console.error(e)
return disableCustomUrlField(true)
}
}
function disableCustomUrlField(isDisabled){
const i = document.querySelector('input[name=custom]')
i.setCustomValidity(isDisabled ? "This custom url allready exists." : '')
}
</script>
<div id="content">
<form name="url_form" action="shorten.php" method="post">
<div class="form-group">
<label class="form-label" for="shorten_form_url">Link to shorten</label>
<input class="form-input" type="text" id="shorten_form_url" name="url"/>
</div>
<div class="flex flex-space">
<div class="form-group">
<label class="form-label" for="shorten_form_custom">Optional short url</label>
<input class="form-input" type="text" id="shorten_form_custom" name="custom" onkeyup="checkCustomUrlAvailable(event)" maxlength="30">
</div>
<div class="form-group">
<label class="form-label" for="shorten_form_comment">Optional comment</label>
<input class="form-input" type="text" id="shorten_form_comment" name="comment" maxlength="30">
</div>
</div>
<div>
<input type="submit" value="Shorten" class="btn btn-primary"/>
<button class="btn float-right" onclick="event.preventDefault(); window.location.href = 'list.php'">List of shortened links</button>
</div>
</form>
<div class="flex">
<div id="credits" class="flex-grow flex-center">
<a href="https://github.com/azlux/Simple-URL-Shortener/" class="text-center">
Simple-URL-Shortener Project
</a>
</div>
<a href="<?php echo $code_js ?>" class="btn tooltip" data-tooltip="You can add this link as bookmark
(click and drop into your bookmark toolbar).
After that, you can click to short the current url!" onclick="event.preventDefault();"/>Bookmark</a>
</div>
</div>
<?php include 'inc/footer.php';?>