PageRenderTime 127ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/manage.php

https://github.com/fligtar/lilliputian
PHP | 123 lines | 89 code | 22 blank | 12 comment | 15 complexity | fb558078c6df65d5317145f6f8b5eeaa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Redirector
  4. * This file handles all incoming URL keys, increments the counter, and forwards
  5. * along to the destination if it exists
  6. */
  7. require 'init.php';
  8. session_start();
  9. // Check if the user is trying to log in
  10. if (!empty($_POST['password']) && $_POST['password'] == $admin_password) {
  11. $_SESSION['authenticated'] = true;
  12. }
  13. // Base URL
  14. $base_url = 'http'.($_SERVER['SERVER_PORT'] == 443 ? 's' : '').'://'.$_SERVER['SERVER_NAME'].$path;
  15. ?>
  16. <!DOCTYPE html>
  17. <html lang="en">
  18. <head>
  19. <title>lilliputian management</title>
  20. </head>
  21. <body>
  22. <h1>lilliputian management</h1>
  23. <?php
  24. // If not logged in, show the form and exit
  25. if (empty($_SESSION['authenticated'])) {
  26. ?>
  27. <form method="post">
  28. <label for="password">password:</label> <input type="password" name="password" id="password" />
  29. <input type="submit" value="log in" />
  30. </form>
  31. </body>
  32. </html>
  33. <?php
  34. exit;
  35. }
  36. // Check if any actions need to be handled
  37. if (isset($_GET['action'])) {
  38. // Check if the action is creating a URL
  39. if ($_GET['action'] == 'create') {
  40. $key = mysql_real_escape_string($_GET['key']);
  41. $url = mysql_real_escape_string($_GET['url']);
  42. $exists = mysql_query("SELECT * FROM lilliputian WHERE `key` = '{$key}'");
  43. if (mysql_num_rows($exists) > 0) {
  44. echo '<h3 style="color: red;">key already exists</h3>';
  45. }
  46. else {
  47. mysql_query("INSERT INTO lilliputian (`key`, url, created) VALUES('{$key}', '{$url}', NOW())");
  48. echo '<h3 style="color: green;">created <a href="'.htmlentities($base_url.'/'.$_GET['key']).'">'.htmlentities($base_url.'/'.$_GET['key']).'</a></h3>';
  49. }
  50. }
  51. if ($_GET['action'] == 'logout') {
  52. $_SESSION = array();
  53. session_destroy();
  54. // Not my finest work!
  55. die('<p>Logged out. <a href="'.$base_url.'/manage.php">Log back in?</a></p></body></html>');
  56. }
  57. }
  58. ?>
  59. <h2>new lilliput</h2>
  60. <div>
  61. <form>
  62. <label for="key">key:</label> <input type="text" name="key" id="key" /><br />
  63. <label for="url">URL:</label> <input type="text" name="url" id="url" /><br />
  64. <input type="submit" name="action" value="create"/>
  65. </form>
  66. </div>
  67. <h2>existing lilliputs</h2>
  68. <?php
  69. // Get the existing items
  70. $items_qry = mysql_query("SELECT * FROM lilliputian ORDER BY created DESC");
  71. if (mysql_num_rows($items_qry) > 0) {
  72. ?>
  73. <table>
  74. <thead>
  75. <tr>
  76. <th>key</th>
  77. <th>URL</th>
  78. <th>hits</th>
  79. <th>created</th>
  80. <th></th>
  81. </tr>
  82. </thead>
  83. <tbody>
  84. <?php while ($item = mysql_fetch_array($items_qry)) { ?>
  85. <tr>
  86. <td><a href="<?php echo htmlentities($base_url.'/'.$item['key']); ?>"><?php echo htmlentities($item['key']); ?></a></td>
  87. <td><a href="<?php echo htmlentities($item['url']); ?>"><?php echo htmlentities($item['url']); ?></a></td>
  88. <td><?php echo $item['hits']; ?></td>
  89. <td><?php echo $item['created']; ?></td>
  90. <td><!--edit | delete--></td>
  91. </tr>
  92. <?php } ?>
  93. </tbody>
  94. </table>
  95. <?php } else { ?>
  96. <p>none yet! create one above.</p>
  97. <?php } ?>
  98. <p>bookmarklet: <a href="javascript:var%20k=window.prompt('key?');window.open('<?php echo $base_url; ?>/manage.php?action=create&amp;key='+k+'&amp;url='+document.location.href);void(0);">lilliput it</a></p>
  99. <p><a href="manage.php?action=logout">log out</a></p>
  100. </body>
  101. </html>