index.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. $url0 = sanitise_data($addr[0]);
  111. $sql = "SELECT destination FROM url WHERE source = '$url0'";
  112. $result = $conn->query($sql);
  113. if ($result->num_rows > 0) {
  114. while($row = $result->fetch_assoc()) {
  115. $destination = $row["destination"];
  116. }
  117. if (0 === strpos($destination, 'http')) {
  118. header('Location: ' . $destination);
  119. }
  120. else{
  121. header('Location: http://' . $destination);
  122. }
  123. }
  124. else {
  125. $view = "form";
  126. }
  127. }
  128. //*******View*******\\
  129. ?>
  130. <!DOCTYPE html>
  131. <head>
  132. <title>Simple URL Shortener</title>
  133. <meta charset="utf-8">
  134. <meta name="viewport" content="width=device-width, initial-scale=1">
  135. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  136. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  137. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  138. <link rel="shortcut icon" type="image/x-icon" href="<?php echo $site_url; ?>/favicon.ico">
  139. </head>
  140. <body>
  141. <div class="container">
  142. <div class="page-header"><h1 style="font-size:3.5em;"><a href="/">Simple URL Shortener</a></h1></div>
  143. <?php if ($view == "form"){ ?>
  144. <form role="form" action="<?php echo $site_url; ?>" method="post">
  145. <div class="form-group <?php if ($error == 'no-destination'){ echo 'has-error';}?>">
  146. <label class="control-label" for="destination">Destination: <?php if ($error == 'no-destination'){ echo '(Must be entered)';}?></label>
  147. <input class="form-control" type="text" id="destination" name="destination" autofocus value="<?php echo $destination ?>">
  148. </div>
  149. <div class="form-group <?php if ($error == 'source-taken'){ echo 'has-error';}?>" >
  150. <label for="source"> <?php if ($error == 'source-taken'){ echo 'This source is already being used';}else{ echo "Source (Leave blank for random):";}?></label>
  151. <div class="input-group">
  152. <span class="input-group-addon"><?php echo $site_url?>/</span>
  153. <input class="form-control" type="text" id="source" name="source" value="<?php echo $source ?>">
  154. </div>
  155. </div>
  156. <input type="hidden" name="reload" value="1">
  157. <input style="padding-left:50px;padding-right:50px;" type="submit" value="Go!" class="btn btn-success btn-lg">
  158. </form>
  159. <?php } elseif ($view == "success") {
  160. if (0 === strpos($destination, 'http')) {
  161. echo "";
  162. }
  163. else{
  164. $destination = "http://" . $destination;
  165. }
  166. ?>
  167. <h4>The URL <a href=<?php echo "'" . $site_url . "/" . $source . "'>" . $site_url . "/" . $source . "</a> redirects to <a href='" . $destination . "'>" . $destination . "</a><br>"; ?></h4>
  168. <?php } ?>
  169. </div>
  170. <div class="footer">
  171. <p class="text-muted"><br>Simple URL Shortener<br>
  172. <a href="mailto:ajamesspeer@gmail.com">Aaron Speer</a>, <?php echo date("Y");?></p>
  173. </div>
  174. <div id = "scoped-content">
  175. <style type = "text/css" scoped>
  176. .footer{
  177. position: absolute;
  178. bottom: 0;
  179. width: 100%;
  180. background-color: #f5f5f5;
  181. position: absolute;
  182. left: 0;
  183. bottom: 0;
  184. height: 85px;
  185. width: 100%;
  186. }
  187. .footer p{
  188. text-align: center;
  189. }
  190. .table th, td{
  191. text-align: center;
  192. }
  193. body{
  194. margin: 0 0 110px; /* bottom = footer height */
  195. }
  196. html {
  197. position: relative;
  198. min-height: 100%;
  199. }
  200. </style>
  201. </div>
  202. </body>
  203. <?php