<?php

/* Generate a unique key from the current time and an IP address. It starts
 * with -time() so that newest hits are stored first in alphabetical order. */
function genkey($ip) {
    return sprintf('%08x', -1 - time()) . substr(md5($ip . rand()), 0, 8);
}

/* Get back the date value from a key */
function key2date($key) {
    return 0xffffffff - hexdec(substr($key, 0, 8));
}

/* Store a new hit in the database and return a unique key.
 * Only variables from the server are used, except $_GET['u'] which is an
 * optional username for LM stats. If the IP is amongst the last 10 hits it
 * is ignored. The clipboard value is not stored at this time but later on. */
function store_hit() {
    $ip = $_SERVER['REMOTE_ADDR'];
    $port = $_SERVER['REMOTE_PORT'];
    $hostname = base64_encode('');
    $uagent = base64_encode($_SERVER['HTTP_USER_AGENT']);
    $referer = base64_encode($_SERVER['HTTP_REFERER']);
    $user = base64_encode($_GET['u']);
    $clipboard = base64_encode('');

    $key = genkey($ip);
    $val = "$ip $port $hostname $uagent $referer $user $clipboard";

    $id = dba_open($_SERVER['DOCUMENT_ROOT'].'/db/hit.db', 'c', 'db4');
    if($id) {
        /* First check that this IP hasn't already been recorded within
         * the last 60 seconds. */
        $before = time() - 60;
        $tmpkey = dba_firstkey($id);
        while($tmpkey) {
            if(key2date($tmpkey) < $before) {
                break;
            }
            $tmpval = dba_fetch($tmpkey, $id);
            $tmplist = explode(' ', $tmpval);
            $tmpip = $tmplist[0];
            if($tmpip == $ip) {
                return $tmpkey;
            }
            $tmpkey = dba_nextkey($id);
        }

        /* Okay, it's a new IP, store it in the base */
        dba_insert($key, $val, $id);
        dba_close($id);
    }

    $id = dba_open($_SERVER['DOCUMENT_ROOT'].'/db/uagent.db', 'c', 'db4');
    if($id) {
        if(dba_exists($uagent, $id)) {
            $count = dba_fetch($uagent, $id);
            dba_replace($uagent, $count + 1, $id);
        } else {
            dba_insert($uagent, '1', $id);
        }
        dba_close($id);
    }

    $id = dba_open($_SERVER['DOCUMENT_ROOT'].'/db/user.db', 'c', 'db4');
    if($id) {
        if(dba_exists($user, $id)) {
            $count = dba_fetch($user, $id);
            dba_replace($user, $count + 1, $id);
        } else {
            dba_insert($user, '1', $id);
        }
        dba_close($id);
    }

    return $key;
}

function store_clipboard($key, $content) {
    $id = dba_open($_SERVER['DOCUMENT_ROOT'].'/db/hit.db', 'c', 'db4');
    if(!$id) {
        return;
    }
    if(dba_exists($key, $id)) {
        $val = dba_fetch($key, $id);
        list($ip, $port, $hostname,
             $uagent, $referer, $user, $clipboard) = explode(' ', $val);
        $clipboard = base64_encode($content);
        $val = "$ip $port $hostname $uagent $referer $user $clipboard";
        dba_replace($key, $val, $id);
    }
    dba_close($id);
}

?>
