· 7 years ago · Aug 29, 2018, 07:08 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 <div class="g-recaptcha" data-sitekey="6LcLZ2wUAAAAAD5JPq-pu6ciRm7_j6bidwcVPuGA"></div>
16 </form>
17 <?php
18 $conn = mysqli_connect("localhost", "root", "admin", 'redirectdb');
19
20 function IsInDb($key, $value){
21 global $conn;
22 $sql = "SELECT * FROM `redirects` WHERE binary `".$key."` = binary '".$value."';";
23 $result = mysqli_query($conn, $sql);
24 if(mysqli_num_rows($result) > 0) return true;
25 return false;
26 }
27
28 function RandomString($length = 7){
29 $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
30 $charSize = strlen($char) - 1;
31 while (true){
32 $str = '';
33 for($i = 0; $i != $length; $i++){
34 $str = $str.$char[rand(0, $charSize)];
35 }
36 if(!IsInDb("shortUrl", $str)) break;
37 }
38 return $str;
39 }
40
41 if (isset($_GET['submit']))
42 {
43 global $conn;
44 $url = $_GET['redirectUrl'];
45 $url = filter_var($url, FILTER_SANITIZE_URL);
46
47 if (filter_var($url, FILTER_VALIDATE_URL)) {
48 $secretKey = "6LcLZ2wUAAAAALcYjS_w6mGC2sxtQV9xTw4kkrh7";
49 $responseKey = $_GET['g-recaptcha-response'];
50
51 $capchaUrl = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$responseKey;
52 $response = file_get_contents($capchaUrl);
53 $response = json_decode($response);
54 if ($response->success){
55 $short = '';
56 if(IsInDb("redirectUrl", $url)){
57 $sql = "SELECT `shortUrl` FROM `redirects` WHERE binary `redirectUrl` = binary '".$url."' LIMIT 1;";
58 $result = mysqli_query($conn, $sql);
59 if(mysqli_num_rows($result) > 0) {
60 $short = mysqli_fetch_assoc($result)['shortUrl'];
61 }
62 }
63 else{
64 $short = RandomString();
65 $sql = "INSERT INTO `redirects` (`redirectUrl`, `shortUrl`) VALUES ('".$url."', '".$short."');";
66 $result = mysqli_query($conn, $sql);
67 }
68 $link = "http://oleo.acme.si/redirect/".$short;
69 echo '<p>Generated a redirect url at <a href="'.$link.'">'.$link.'</a></p>';
70 }
71 else{
72 echo("<p>reCaptcha verification has failed. Try again.</p>");
73 }
74 } else {
75 echo("<p>".$url." is not a valid URL.</p>");
76 }
77 }
78 else {
79 echo "<p>Enter a redirect link</p>";
80 }
81 ?>
82 <br />
83 <p>
84 Go back <a href="http://oleo.acme.si">home</a>
85 </p>
86</body>