· 7 years ago · Sep 26, 2018, 01:40 PM
1<!DOCTYPE html>
2<head>
3 <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
4 <meta charset="utf-8" />
5 <title>Oleo Acme Redirect</title>
6 <link rel="stylesheet" href="style.css">
7 <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
8 <script src='https://www.google.com/recaptcha/api.js'></script>
9</head>
10<body>
11 <h1>Oleo Acme Redirect</h1>
12 <form>
13 <input type="text" name="redirectUrl" placeholder="Redirect Link">
14 <input type="submit" name="submit" value="Generate">
15 <input type="checkbox" name="hide" value="true">Hide redirect destination<br />
16 <div class="g-recaptcha" data-sitekey="6LcLZ2wUAAAAAD5JPq-pu6ciRm7_j6bidwcVPuGA"></div>
17 </form>
18 <?php
19 $conn = mysqli_connect("localhost", "----", "----", 'webDB');
20
21 function IsInDb($key, $value){
22 global $conn;
23 $sql = "SELECT * FROM `redirects` WHERE binary `".$key."` = binary '".$value."';";
24 $result = mysqli_query($conn, $sql);
25 if(mysqli_num_rows($result) > 0) return true;
26 return false;
27 }
28
29 function RandomString($length = 7){
30 $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
31 $charSize = strlen($char) - 1;
32 while (true){
33 $str = '';
34 for($i = 0; $i != $length; $i++){
35 $str = $str.$char[rand(0, $charSize)];
36 }
37 if(!IsInDb("shortUrl", $str)) break;
38 }
39 return $str;
40 }
41
42 if (isset($_GET['submit']))
43 {
44 global $conn;
45 $url = $_GET['redirectUrl'];
46 $hide = (isset($_GET['hide'])? '1' : '0');
47 $url = filter_var($url, FILTER_SANITIZE_URL);
48
49 if (filter_var($url, FILTER_VALIDATE_URL)) {
50 $secretKey = "-----------------------";
51 $responseKey = $_GET['g-recaptcha-response'];
52
53 $capchaUrl = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$responseKey;
54 $response = file_get_contents($capchaUrl);
55 $response = json_decode($response);
56 if ($response->success){
57 $short = '';
58 if(IsInDb("redirectUrl", $url)){
59 $sql = "SELECT `shortUrl` FROM `redirects` WHERE binary `redirectUrl` = binary '".$url."' LIMIT 1;";
60 $result = mysqli_query($conn, $sql);
61 if(mysqli_num_rows($result) > 0) {
62 $short = mysqli_fetch_assoc($result)['shortUrl'];
63 }
64 }
65 else{
66 $short = RandomString();
67 $sql = "INSERT INTO `redirects` (`redirectUrl`, `shortUrl`, `hide`) VALUES ('".$url."', '".$short."', '".$hide."');";
68 $result = mysqli_query($conn, $sql);
69 }
70 $link = "https://oleo.acme.si/redirect/".$short;
71 header("Location: ?short=".$short);
72 }
73 else{
74 echo("<p>reCaptcha verification has failed. Try again.</p>");
75 }
76 } else {
77 echo("<p>".$url." is not a valid URL.</p>");
78 }
79 }
80 else if (isset($_GET['short'])){
81 $short = $_GET['short'];
82 if(IsInDb("shortUrl", $short)){
83 $link = "https://oleo.acme.si/redirect/".$short;
84 echo '<p>Generated a redirect url at <a href="'.$link.'">'.$link.'</a></p>';
85 }
86 else{
87 echo "<p>This redirect link doesn't exist</p>";
88 }
89 }
90 else {
91 echo "<p>Enter a redirect link</p>";
92 }
93 ?>
94 <br />
95 <p>
96 Go back <a href="http://oleo.acme.si">home</a>
97 </p>
98</body>