-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
148 lines (138 loc) · 6.15 KB
/
index.php
File metadata and controls
148 lines (138 loc) · 6.15 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
include('functions.php');
$uri = $_SERVER['REQUEST_URI'];
$parameter = explode('/', $uri);
$url = end($parameter);
if (!empty($url)) {
redirect($url);
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mini URL</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- JS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
</head>
<body>
<div class="container py-5">
<div class="text-center mb-5">
<h1 class="display-4 fw-bold">Mini URL</h1>
<p class="lead text-muted">Minimize your long links quickly and easily</p>
</div>
<div class="row justify-content-center">
<div class="col-lg-6 col-md-8">
<form id="miniurlForm">
<div class="mb-4">
<label for="originalUrl" class="form-label">Enter a long URL</label>
<input type="text" class="form-control" id="url" name="url" placeholder="https://example.com/">
</div>
<div class="mb-4">
<label for="vanityUrl" class="form-label">Custom URL (optional)</label>
<div class="input-group w-100">
<span class="input-group-text" id="domainPrefix"><?= $domainUrl ?></span>
<input type="text" class="form-control" id="vanityUrl" name="vanityUrl"
aria-describedby="domainPrefix" placeholder="your-custom-name">
</div>
<div id="vanityUrl-error" class="text-danger"></div>
<div class="form-text">Choose your custom short link (letters, numbers, hyphens)</div>
</div>
<div class="d-grid mb-3">
<button type="submit" id="getUrl" class="btn btn-primary btn-lg">Get Mini URL</button>
</div>
<div id="result" class="alert alert-success d-none" role="alert">
Your short URL is: <a href="#" id="miniUrl" target="_blank" class="fw-bold"></a>
<small><small>Copied to clipboard!</small></small>
</div>
<div id="error" class="alert alert-danger d-none" role="alert"></div>
</form>
</div>
</div>
</div>
</body>
<script>
$(document).ready(function () {
$.validator.addMethod("regex", function (value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
}, "Invalid format.");
$("#miniurlForm").validate({
rules: {
url: {
required: true,
url: true
},
vanityUrl: {
required: false,
regex: "^[a-zA-Z0-9\\-]+$",
minlength: 3
},
},
messages: {
url: {
required: "Url is required",
url: "Invalid Url"
},
vanityUrl: {
required: "vanityUrl",
regex: "Only letters, numbers, and dashes(-) are allowed",
minlength: "Please enter at least 3 characters"
},
},
errorClass: "text-danger",
errorElement: "div",
errorPlacement: function (error, element) {
if (element.attr("id") === "vanityUrl") {
$("#vanityUrl-error").html(error);
} else {
error.insertAfter(element);
}
},
submitHandler: function (form, event) {
event.preventDefault();
let data = new FormData(form);
$.ajax({
type: 'post',
url: "getUrl.php",
data: data,
dataType: 'JSON',
contentType: false,
processData: false,
async: true,
cache: false,
beforeSend: function () {
$("#getUrl").prop("disabled", true);
},
success: function (data) {
$("#getUrl").prop("disabled", false);
if (data.status == 1) {
const miniUrl = data.url;
$("#miniUrl").attr("href", miniUrl).text(miniUrl);
$("#result").removeClass("d-none");
$("#error").addClass("d-none");
const tempInput = document.createElement("textarea");
document.body.appendChild(tempInput);
tempInput.value = miniUrl;
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
} else {
$("#result").addClass("d-none");
$("#error").text(data.message);
$("#error").removeClass("d-none");
}
}
});
}
});
});
</script>
</html>
<?php
}
?>