PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms 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
  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
  8require 'init.php';
  9
 10session_start();
 11
 12// Check if the user is trying to log in
 13if (!empty($_POST['password']) && $_POST['password'] == $admin_password) {
 14    $_SESSION['authenticated'] = true;
 15}
 16
 17// Base URL
 18$base_url = 'http'.($_SERVER['SERVER_PORT'] == 443 ? 's' : '').'://'.$_SERVER['SERVER_NAME'].$path;
 19?>
 20<!DOCTYPE html>
 21<html lang="en">
 22
 23<head>
 24    <title>lilliputian management</title>
 25</head>
 26
 27<body>
 28<h1>lilliputian management</h1>
 29
 30<?php
 31// If not logged in, show the form and exit
 32if (empty($_SESSION['authenticated'])) {
 33?>
 34    <form method="post">
 35        <label for="password">password:</label> <input type="password" name="password" id="password" />
 36        <input type="submit" value="log in" />
 37    </form>
 38    
 39    </body>
 40    </html>
 41<?php
 42    exit;
 43}
 44
 45// Check if any actions need to be handled
 46if (isset($_GET['action'])) {
 47    
 48    // Check if the action is creating a URL
 49    if ($_GET['action'] == 'create') {
 50        $key = mysql_real_escape_string($_GET['key']);
 51        $url = mysql_real_escape_string($_GET['url']);
 52        
 53        $exists = mysql_query("SELECT * FROM lilliputian WHERE `key` = '{$key}'");
 54        
 55        if (mysql_num_rows($exists) > 0) {
 56            echo '<h3 style="color: red;">key already exists</h3>';
 57        }
 58        else {
 59            mysql_query("INSERT INTO lilliputian (`key`, url, created) VALUES('{$key}', '{$url}', NOW())");
 60            
 61            echo '<h3 style="color: green;">created <a href="'.htmlentities($base_url.'/'.$_GET['key']).'">'.htmlentities($base_url.'/'.$_GET['key']).'</a></h3>';
 62        }
 63    }
 64    
 65    if ($_GET['action'] == 'logout') {
 66        $_SESSION = array();
 67        session_destroy();
 68        // Not my finest work!
 69        die('<p>Logged out. <a href="'.$base_url.'/manage.php">Log back in?</a></p></body></html>');
 70    }
 71}
 72?>
 73
 74<h2>new lilliput</h2>
 75<div>
 76    <form>
 77        <label for="key">key:</label> <input type="text" name="key" id="key" /><br />
 78        <label for="url">URL:</label> <input type="text" name="url" id="url" /><br />
 79        <input type="submit" name="action" value="create"/>
 80    </form>
 81</div>
 82
 83<h2>existing lilliputs</h2>
 84<?php
 85// Get the existing items
 86$items_qry = mysql_query("SELECT * FROM lilliputian ORDER BY created DESC");
 87
 88if (mysql_num_rows($items_qry) > 0) {
 89?>
 90    <table>
 91        <thead>
 92            <tr>
 93                <th>key</th>
 94                <th>URL</th>
 95                <th>hits</th>
 96                <th>created</th>
 97                <th></th>
 98            </tr>
 99        </thead>
100    
101        <tbody>
102    
103    <?php while ($item = mysql_fetch_array($items_qry)) { ?>
104        <tr>
105            <td><a href="<?php echo htmlentities($base_url.'/'.$item['key']); ?>"><?php echo htmlentities($item['key']); ?></a></td>
106            <td><a href="<?php echo htmlentities($item['url']); ?>"><?php echo htmlentities($item['url']); ?></a></td>
107            <td><?php echo $item['hits']; ?></td>
108            <td><?php echo $item['created']; ?></td>
109            <td><!--edit | delete--></td>
110        </tr>
111    <?php } ?>
112
113        </tbody>
114    </table>
115<?php } else { ?>
116    <p>none yet! create one above.</p>
117<?php } ?>
118
119<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>
120<p><a href="manage.php?action=logout">log out</a></p>
121
122</body>
123</html>