-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
124 lines (114 loc) · 3.46 KB
/
index.php
File metadata and controls
124 lines (114 loc) · 3.46 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
<?php
$base_url = 'http://localhost/s/';
$db['host'] = 'localhost';
$db['user'] = 'root';
$db['pass'] = '';
$db['db'] = 'database';
$db['table'] = 'shorturls';
if (isset($_GET['shorturl'])) {
$idshort = trim($_GET['shorturl']);
$id = base_convert($idshort, 36, 10);
$db = new mysqli($db['host'], $db['user'], $db['pass'], $db['db']);
if ($db->connect_errno) {
die("MySQL Error (" . $db->connect_errno . "): " . $db->connect_error);
}
$query = "SELECT url FROM ".$db['table']." WHERE id = '$id'";
if ($res = $db->query($query)) {
if ($res->num_rows == 0) {
header("Location: $base_url");
} else {
$fila = $res->fetch_assoc();
$url = $fila['url'];
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
}
} else {
echo "Query Error!";
}
$db->close();
exit();
}
if (isset($_POST['url'])) {
$url = trim($_POST['url']);
if (!filter_var($url, FILTER_VALIDATE_URL) ||
!preg_match('/^(ftp|http|https):\/\//', $url)) {
exit("URL is invalid");
}
//Check URL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if (!$result = curl_exec($ch)) {
die("Error: Curl exec failed!");
}
$response = curl_getinfo($ch);
curl_close($ch);
if ($response['http_code'] == 404) {
die("Error: Page not found. Error " . $response['http_code'] . ".");
}
$db = new mysqli($db['host'], $db['user'], $db['pass'], $db['db']);
if ($db->connect_errno) {
die("MySQL Error (" . $db->connect_errno . "): " . $db->connect_error);
}
$query = "SELECT id FROM ".$db['table']." WHERE url = '$url'";
if ($res = $db->query($query)) {
if ($res->num_rows == 0) {
$query = "INSERT INTO ".$db['table']." (url) VALUES('$url')";
$db->query($query);
$id = $db->insert_id;
} else {
$fila = $res->fetch_assoc();
$id = $fila['id'];
}
$shorturl = $base_url . base_convert($id, 10, 36);
echo $shorturl;
} else {
echo "Query Error!";
}
$db->close();
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>Short URLs</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
</head>
<body>
<div class="row vertical-offset-100">
<div class="col-md-4 col-md-offset-4">
<form role="form" id="form-url">
<div class="form-group">
<label for="url">URL</label>
<input type="url" placeholder="Enter URL" id="url" class="form-control">
</div>
<div class="form-group" id="url-corta"></div>
<button class="btn btn-default btn-info" type="submit">Short</button>
<button class="btn btn-default btn-info" type="reset">Reset</button>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
$('#form-url').submit(function() {
$.ajax({
type: "POST",
url: "index.php",
data: { url: $('#url').val() },
success: function(data){
$('#url-corta').html('<a href="'+data+'">'+data+'</a>');
//alert(data);
}
});
return false;
});
$('#form-url :reset').click(function() {
$('#url-corta').empty();
});
</script>
</body>
</html>
<?php
}
?>