index.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. //***Configuration***\\
  3. $error_reporting = 0; // 1 = errors 0 = none
  4. $site_url = "http://short.home.ie"; //don't include the trailing /
  5. $mysql_username = "root";
  6. $mysql_password = "root";
  7. $mysql_servername = "localhost";
  8. $mysql_database = "urlshort";
  9. $source_length = 5; //number of random bytes to be converted to Base64
  10. //*******************\\
  11. if ($error_reporting == 1){
  12. ini_set('display_errors', 1);
  13. ini_set('display_startup_errors', 1);
  14. error_reporting(E_ALL);}
  15. // Create connection
  16. $conn = mysqli_connect($mysql_servername, $mysql_username, $mysql_password, $mysql_database);
  17. // Check connection
  18. if ($conn->connect_error) {
  19. die("Connection failed: " . $conn->connect_error);
  20. }
  21. function sanitise_data($data) {
  22. global $conn;
  23. $data = mysqli_real_escape_string($conn, $data);
  24. $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
  25. return $data;
  26. }
  27. function writeToDB($sour, $dest) {
  28. global $conn;
  29. $sql = "INSERT INTO url (source, destination) VALUES ('$sour', '$dest')";
  30. if (mysqli_query($conn, $sql)) {
  31. //echo "Wrote " . $sour . " => " . $dest . " to database";
  32. } else {
  33. echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  34. }
  35. }
  36. $sql = "SHOW TABLES LIKE 'url'";
  37. $result = $conn->query($sql);
  38. $db_installed = $result->num_rows;
  39. if ($db_installed == 0){ //install db
  40. $sql = "CREATE TABLE url (id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY, source VARCHAR(60), destination VARCHAR(1024))";
  41. if (mysqli_query($conn, $sql)) {
  42. } else {
  43. echo "Error creating table: " . mysqli_error($conn);
  44. }
  45. }
  46. function getCurrentUri()
  47. {
  48. $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  49. $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  50. if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
  51. $uri = '/' . trim($uri, '/');
  52. return $uri;
  53. }
  54. $base_url = getCurrentUri();
  55. $routes = array();
  56. $routes = explode('/', $base_url);
  57. $addr = array();
  58. foreach($routes as $route) {
  59. if(trim($route) != '')
  60. array_push($routes, $route);
  61. if ($route != ""){
  62. $addr[] = $route;
  63. }
  64. }
  65. $source = sanitise_data($_POST["source"]);
  66. $destination = sanitise_data($_POST["destination"]);
  67. $reload = $_POST["reload"];
  68. if ($addr[0] == ""){
  69. if($source == "" && $destination == "" && $reload == ""){
  70. $view = "form";
  71. }
  72. elseif($destination == ""){
  73. $error= "no-destination";
  74. $view = "form";
  75. }
  76. elseif($source != ""){
  77. $sql = "SELECT source FROM url WHERE source = '$source'";
  78. $result = $conn->query($sql);
  79. if ($result->num_rows > 0) {
  80. $view = "form";
  81. $error = "source-taken";
  82. }
  83. else {
  84. writeToDB($source, $destination);
  85. $view = "success";
  86. }
  87. }
  88. else{
  89. $view = "success";
  90. $error = "source-taken";
  91. while ($error == "source-taken"){
  92. $source = sanitise_data(rtrim(strtr(base64_encode(openssl_random_pseudo_bytes($source_length)), '+/', '-_'), '='));
  93. $sql = "SELECT source FROM url WHERE source = '$source'";
  94. $result = $conn->query($sql);
  95. if ($result->num_rows > 0) {
  96. $error = "source-taken";
  97. $view = "form";
  98. }
  99. else {
  100. writeToDB($source, $destination);
  101. $error = "";
  102. $view = "success";
  103. }
  104. }
  105. }
  106. }
  107. else{
  108. $view = "blank";
  109. $sql = "SELECT destination FROM url WHERE source = '$addr[0]'";
  110. $result = $conn->query($sql);
  111. if ($result->num_rows > 0) {
  112. while($row = $result->fetch_assoc()) {
  113. $destination = $row["destination"];
  114. }
  115. if (0 === strpos($destination, 'http')) {
  116. header('Location: ' . $destination);
  117. }
  118. else{
  119. header('Location: http://' . $destination);
  120. }
  121. }
  122. else {
  123. $view = "form";
  124. }
  125. }
  126. //*******View*******\\
  127. ?>
  128. <!DOCTYPE html>
  129. <head>
  130. <title>Simple URL Shortener</title>
  131. <meta charset="utf-8">
  132. <meta name="viewport" content="width=device-width, initial-scale=1">
  133. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  134. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  135. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  136. <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
  137. </head>
  138. <body>
  139. <div class="container">
  140. <div class="page-header"><h1 style="font-size:3.5em;"><a href="/">Simple URL Shortener</a></h1></div>
  141. <?php if ($view == "form"){ ?>
  142. <form role="form" action="/" method="post">
  143. <div class="form-group <?php if ($error == 'no-destination'){ echo 'has-error';}?>">
  144. <label class="control-label" for="destination">Destination: <?php if ($error == 'no-destination'){ echo '(Must be entered)';}?></label>
  145. <input class="form-control" type="text" id="destination" name="destination" autofocus value="<?php echo $destination ?>">
  146. </div>
  147. <div class="form-group <?php if ($error == 'source-taken'){ echo 'has-error';}?>" >
  148. <label for="source"> <?php if ($error == 'source-taken'){ echo 'This source is already being used';}else{ echo "Source (Leave blank for random):";}?></label>
  149. <div class="input-group">
  150. <span class="input-group-addon"><?php echo $site_url?>/</span>
  151. <input class="form-control" type="text" id="source" name="source" value="<?php echo $source ?>">
  152. </div>
  153. </div>
  154. <input type="hidden" name="reload" value="1">
  155. <input style="padding-left:50px;padding-right:50px;" type="submit" value="Go!" class="btn btn-success btn-lg">
  156. </form>
  157. <?php } elseif ($view == "success") {
  158. if (0 === strpos($destination, 'http')) {
  159. echo "";
  160. }
  161. else{
  162. $destination = "http://" . $destination;
  163. }
  164. ?>
  165. <h4>The URL <a href=<?php echo "'" . $site_url . "/" . $source . "'>" . $site_url . "/" . $source . "</a> redirects to <a href='" . $destination . "'>" . $destination . "</a><br>"; ?></h4>
  166. <?php } ?>
  167. </div>
  168. <div class="footer">
  169. <p class="text-muted"><br>Simple URL Shortener<br>
  170. <a href="mailto:ajamesspeer@gmail.com">Aaron Speer</a>, <?php echo date("Y");?></p>
  171. </div>
  172. <div id = "scoped-content">
  173. <style type = "text/css" scoped>
  174. .footer{
  175. position: absolute;
  176. bottom: 0;
  177. width: 100%;
  178. background-color: #f5f5f5;
  179. position: absolute;
  180. left: 0;
  181. bottom: 0;
  182. height: 85px;
  183. width: 100%;
  184. }
  185. .footer p{
  186. text-align: center;
  187. }
  188. .table th, td{
  189. text-align: center;
  190. }
  191. body{
  192. margin: 0 0 110px; /* bottom = footer height */
  193. }
  194. html {
  195. position: relative;
  196. min-height: 100%;
  197. }
  198. </style>
  199. </div>
  200. </body>
  201. <?php