index.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. writeToDB("index.php", "/");
  43. } else {
  44. echo "Error creating table: " . mysqli_error($conn);
  45. }
  46. }
  47. function getCurrentUri()
  48. {
  49. $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  50. $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  51. if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
  52. $uri = '/' . trim($uri, '/');
  53. return $uri;
  54. }
  55. $base_url = getCurrentUri();
  56. $routes = array();
  57. $routes = explode('/', $base_url);
  58. $addr = array();
  59. foreach($routes as $route) {
  60. if(trim($route) != '')
  61. array_push($routes, $route);
  62. if ($route != ""){
  63. $addr[] = $route;
  64. }
  65. }
  66. $source = sanitise_data($_POST["source"]);
  67. $destination = sanitise_data($_POST["destination"]);
  68. $reload = $_POST["reload"];
  69. if ($addr[0] == ""){
  70. if($source == "" && $destination == "" && $reload == ""){
  71. $view = "form";
  72. }
  73. elseif($destination == ""){
  74. $error= "no-destination";
  75. $view = "form";
  76. }
  77. elseif($source != ""){
  78. $sql = "SELECT source FROM url WHERE source = '$source'";
  79. $result = $conn->query($sql);
  80. if ($result->num_rows > 0) {
  81. $view = "form";
  82. $error = "source-taken";
  83. }
  84. else {
  85. writeToDB($source, $destination);
  86. $view = "success";
  87. }
  88. }
  89. else{
  90. $view = "success";
  91. $error = "source-taken";
  92. while ($error == "source-taken"){
  93. $source = sanitise_data(rtrim(strtr(base64_encode(openssl_random_pseudo_bytes($source_length)), '+/', '-_'), '='));
  94. $sql = "SELECT source FROM url WHERE source = '$source'";
  95. $result = $conn->query($sql);
  96. if ($result->num_rows > 0) {
  97. $error = "source-taken";
  98. $view = "form";
  99. }
  100. else {
  101. writeToDB($source, $destination);
  102. $error = "";
  103. $view = "success";
  104. }
  105. }
  106. }
  107. }
  108. else{
  109. $view = "blank";
  110. $sql = "SELECT destination FROM url WHERE source = '$addr[0]'";
  111. $result = $conn->query($sql);
  112. if ($result->num_rows > 0) {
  113. while($row = $result->fetch_assoc()) {
  114. $destination = $row["destination"];
  115. }
  116. if (0 === strpos($destination, 'http')) {
  117. header('Location: ' . $destination);
  118. }
  119. else{
  120. header('Location: http://' . $destination);
  121. }
  122. }
  123. else {
  124. $view = "form";
  125. }
  126. }
  127. //*******View*******\\
  128. ?>
  129. <!DOCTYPE html>
  130. <head>
  131. <title>Simple URL Shortener</title>
  132. <meta charset="utf-8">
  133. <meta name="viewport" content="width=device-width, initial-scale=1">
  134. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  135. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  136. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  137. <link rel="shortcut icon" type="image/x-icon" href="<?php echo $site_url; ?>/favicon.ico">
  138. </head>
  139. <body>
  140. <div class="container">
  141. <div class="page-header"><h1 style="font-size:3.5em;"><a href="/">Simple URL Shortener</a></h1></div>
  142. <?php if ($view == "form"){ ?>
  143. <form role="form" action="<?php echo $site_url; ?>" method="post">
  144. <div class="form-group <?php if ($error == 'no-destination'){ echo 'has-error';}?>">
  145. <label class="control-label" for="destination">Destination: <?php if ($error == 'no-destination'){ echo '(Must be entered)';}?></label>
  146. <input class="form-control" type="text" id="destination" name="destination" autofocus value="<?php echo $destination ?>">
  147. </div>
  148. <div class="form-group <?php if ($error == 'source-taken'){ echo 'has-error';}?>" >
  149. <label for="source"> <?php if ($error == 'source-taken'){ echo 'This source is already being used';}else{ echo "Source (Leave blank for random):";}?></label>
  150. <div class="input-group">
  151. <span class="input-group-addon"><?php echo $site_url?>/</span>
  152. <input class="form-control" type="text" id="source" name="source" value="<?php echo $source ?>">
  153. </div>
  154. </div>
  155. <input type="hidden" name="reload" value="1">
  156. <input style="padding-left:50px;padding-right:50px;" type="submit" value="Go!" class="btn btn-success btn-lg">
  157. </form>
  158. <?php } elseif ($view == "success") {
  159. if (0 === strpos($destination, 'http')) {
  160. echo "";
  161. }
  162. else{
  163. $destination = "http://" . $destination;
  164. }
  165. ?>
  166. <h4>The URL <a href=<?php echo "'" . $site_url . "/" . $source . "'>" . $site_url . "/" . $source . "</a> redirects to <a href='" . $destination . "'>" . $destination . "</a><br>"; ?></h4>
  167. <?php } ?>
  168. </div>
  169. <div class="footer">
  170. <p class="text-muted"><br>Simple URL Shortener<br>
  171. <a href="mailto:ajamesspeer@gmail.com">Aaron Speer</a>, <?php echo date("Y");?></p>
  172. </div>
  173. <div id = "scoped-content">
  174. <style type = "text/css" scoped>
  175. .footer{
  176. position: absolute;
  177. bottom: 0;
  178. width: 100%;
  179. background-color: #f5f5f5;
  180. position: absolute;
  181. left: 0;
  182. bottom: 0;
  183. height: 85px;
  184. width: 100%;
  185. }
  186. .footer p{
  187. text-align: center;
  188. }
  189. .table th, td{
  190. text-align: center;
  191. }
  192. body{
  193. margin: 0 0 110px; /* bottom = footer height */
  194. }
  195. html {
  196. position: relative;
  197. min-height: 100%;
  198. }
  199. </style>
  200. </div>
  201. </body>
  202. <?php