<?php

// sort of a last-visitors keeper.  Done with the WWIV BBS software's
// "last few users" function in mind.

// help yourself under terms of the GPL.

function guessisp($ss) {
    if (
ereg("AOL",$ss)) return "(AOL)";
    if (
eregi("Prodigy",$ss)) return "(Prodigy)";
    if (
ereg("X11",$ss)) return "(X11)";
    if (
eregi("proxy",$ss)) return "(proxy)";
    return 
"";
}

function 
guessplatform($ss) {
    if (
eregi("Win(dows ?)?(9[58]|16|32|2(000|k)|NT[0-9]?|CE|XP)",$ss,$b))
      return 
"Win$b[2]";
    if (
eregi("Mac(intosh)?.?(OS|(Power.?)?PPC)",$ss)) return "MacOS";
    if (
eregi("OS/?2",$ss)) return "OS/2";
    if (
eregi("WebTV",$ss)) return "WebTV";
    if (
eregi("(Free|Open|Net)?BSD"$ss,$b))
      return (
$b[1] ? $b[1] : "") . "BSD";
    if (
eregi("Linux",$ss)) return "Linux";
    if (
eregi("SunOS",$ss) || eregi("Solaris",$ss)) return "SunOS";
    if (
eregi("HP.UX",$ss)) return "HP/UX";
    if (
eregi("IRIX",$ss)) return "IRIX";
    if (
eregi("X11|UNIX",$ss)) return "UNIX";
    return 
'';
}

function 
guessbrowser($ss) {
    if (!(
$ss) || !$ss) return '';
    if (
ereg("Netscape6/([0-9](\.[0-9](\.?[0-9])?)?)",$ss,$b))
        return 
"Netscape $b[1]";
    if (!
ereg("compatible",$ss) && ereg("^Mozilla/([234])",$ss,$b))
        return 
"Netscape $b[1]";
    if (
ereg("^Mozilla/5.*Galeon",$ss)) return "Mozilla-Galeon";
    if (
ereg("^Mozilla/5.*Konqueror",$ss)) return "Konqueror";
    if (
ereg("^Mozilla/5",$ss) && !ereg("compatible|MSIE",$ss))
        return 
"Mozilla";
    if (
eregi("Opera",$ss)) return "Opera";
    if (
ereg("MSIE ([0-9](\.[1-9])?)",$ss,$b))
        return 
"M\$IE $b[1]";
    if (
eregi("Mosaic",$ss)) return "Mosaic";
    if (
ereg("Lynx",$ss)) return "Lynx";
    if (
ereg("Slurp/"$ss)) return "Inktomi Slurp Spider";
    if (
ereg("Googlebot/"$ss)) return "Googlebot";
    if (
ereg("libwww",$ss)) return "libwww";
    if (
ereg("^lwp",$ss)) return "libwww-perl";
    if (
ereg("Java[0-9]",$ss)) return "Java";
    if (
ereg("^Jigsaw",$ss)) return "W3C Jigsaw";
    if (
$ss) return $ss;
}

function 
sql_escape($str)
{
    if (
get_cfg_var('magic_quotes_gpc')) return $str;
    else return 
str_replace('\\''\\\\'str_replace("'","''",$str));
    
}

function 
browser_id($db$browser$os)
{
    
$id 0;
    if (!
$db) return -1;

    
$sql "SELECT id from visitor_browsers".
                
" WHERE name='".sql_escape($browser)."'";
    if (
$os)
        
$sql .= "  AND os='".sql_escape($os)."'";
    else
        
$sql .= "  AND os IS NULL";

    
$res pg_exec($db$sql);
    if (!
$res)
        return;

    if (!
pg_numrows($res)) {
        
$sql "SELECT nextval('visitor_browsers_id_seq')";
        if (!(
$res pg_exec($db$sql)))
            return -
1;
        
$id pg_result($res0'nextval');

        
$sql "INSERT into visitor_browsers ".
            
" (id, name, os, count, lastseen) values".
            
" ($id, '"sql_escape($browser) . "'," .
                (
$os "'".sql_escape($os)."'" 'NULL').', '.
                
'1, NOW())';
        
$res pg_exec($db$sql);
        if (!
$res) return -1;
        return 
$id;
    } else
        return 
pg_result($res00);
}

function 
herebefore($db$ip) {
    if (!
$db || !$ip)
        return;
    
$res pg_exec($db"SELECT * from visitor_history, ".
                
" visitor_browsers".
                
" WHERE ip='$ip' ".
                
" and visitor_history.browser_id = ".
                
" visitor_browsers.id");

    if (!
$res || !pg_numrows($res))
        return;
    if (!(
$a pg_fetch_array($res0PGSQL_ASSOC)))
        return;
    
// echo "$a[ip]/$a[browser_id]/$a[last_visited]/$a[hostname]<br>";
    
if ($a["os"]) return array($a['name'], $a['os']);
    else return array(
$a[name]);
}


function 
storevisit($db$ip$host$browser$os$comment) {
    if (!
$db || !$ip || !$browser)
        return 
0;

    if ((
$id browser_id($db$browser$os)) < 0)
        return 
0;

    if (!
pg_exec($db"UPDATE visitor_browsers set lastseen=NOW(), ".
            
" count=count+1 WHERE id=$id"))
        return 
0;

    
$sql "UPDATE visitor_history".
        
" SET last_visited=NOW(), browser_id=$id";
    if (
$host$sql .= ", hostname='".
        
sql_escape($host)."'";
    if (
$comment$sql .= ", comment='".
        
sql_escape(substr($comment,0,160))."'";
    else 
$sql .= ", comment = NULL";
    
$sql .= " WHERE ip='".sql_escape($ip)."'";

    if (!(
$res pg_exec($db$sql))) return 0;
    if (
pg_cmdtuples($res) >= 1) return 1;

    
$sql "INSERT into visitor_history ".
        
"(ip,browser_id,last_visited,hostname,comment) values ".
        
"('".sql_escape($ip)."', $id, NOW(), ".
        (
$host "'".sql_escape($host)."'" 'NULL').", ".
        (
$comment "'".sql_escape(substr($comment,0,160))."'" 'NULL').")";
    if (!
pg_exec($db$sql))
        return 
0;
    return 
1;
}

function 
recentvisitors($db) {
    
$howmany 5;
    
    if (!
$db) return array();
    
$sql "SELECT ip, hostname, name, os, comment, ".
        
"  EXTRACT(epoch from last_visited) as last_visited".
        
" FROM visitor_history, visitor_browsers".
        
" WHERE visitor_browsers.id=visitor_history.browser_id".
        
" ORDER BY last_visited desc".
        
" LIMIT $howmany";
    if (!(
$res pg_exec($db$sql))) return array();
    for (
$result=array(), $i=0$i pg_numrows($res); $i++)
        
$result[] = pg_fetch_array($res$iPGSQL_ASSOC);
    return 
$result;
}

$db pg_pconnect("dbname=aqua");
if (!
pg_exec($db'begin'))
    return;
$browser guessbrowser($_SERVER['HTTP_USER_AGENT']);
$os guessplatform($_SERVER['HTTP_USER_AGENT']);
$herebefore herebefore($db$_SERVER['REMOTE_ADDR']);
$recent recentvisitors($db);
if (!
ereg("^(192\.168\.|127\.0\.0\.1|66\.92\.186\.)"$_SERVER['REMOTE_ADDR']))
    if (!
storevisit($db$_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_HOST']
    ? 
$_SERVER['REMOTE_HOST'] : ''$browser$os$comment)) {
        
pg_exec($db'rollback');
        
$db 0;
    }



?>
<table width="100%" border=0 cellpadding=4 cellspacing=8 class=recentvisitors>
<tr>
  <td bgcolor="#ccccdd">
    <?php if ($browser && $herebefore) { ?>
        <?php if ($herebefore) { ?>
        <p>Hmm.  Someone's been here from your IP before -- maybe even you.
        Last time time it was with <?=$herebefore[0]?><?=($herebefore[1] ? " on $herebefore[1]" ""?>.
        <?php if ($herebefore[0] == $browser &&
                
$herebefore[1] == $os) { ?>
        Looks like it still is.  How consistent of you.
        <?php } else { ?>
        Now it's <?=$herebefore[0]?><?=($herebefore[1] ? " on $herebefore[1]" :
        
"")?>.  Hmm,
        <?php ?>
        <?php ?>
    <?php ?>
    <?php if ($recent) { ?>
    <table width="100%" class=recentvisitors>
    <tr>
        <th bgcolor="#bbbbdd" align=left colspan=4>
        Last few passers-through:
        </th>
    </tr><tr bgcolor="#bbbbdd">
        <th>Host</th><th>Browser</th><th>Time/Date</th><th>Comment?</th>
    </tr>

    <?php for ($i=0$recent[$i] && $i<500$i++) { 
        if (!
$recent[$i][hostname])
            
$recent[$i][hostname] = $recent[$i][ip];
        
$date date("D m/d h:iA"$recent[$i][last_visited]);
        
$date ereg_replace(" 0"," ",$date);
        
$date ereg_replace("/0","/",$date);
    
?>
    <tr>
        <td><?=htmlspecialchars($recent[$i][hostname])?></td>
        <td><?=htmlspecialchars($recent[$i][name])?></td>
        <td><?=htmlspecialchars($date)?></td>
        <td><?=htmlspecialchars($recent[$i][comment])?></td>
    </tr>
    <?php     ?>
    <?php ?>

    <tr><td colspan=4 bgcolor="#bbbbdd">
    <form method=get action="<?=$SCRIPT_NAME?>"><font size="-1"><input type=text size=30 name=comment>
         <input type=submit value="make a comment"></font></form></td></tr>
    </table>

        <p><font size="-1">(an experiment in PHP3; sorry for the resemblance to a any
        one of a vast number of tacky guestbooks adorning the pet-rock personal
        pages of the world; GPLed source is <a
        href="incl.visitors.phps">here</a>).</font>

                <p><font size="-1">Keep in mind that there have been many, many people in the world
                capable of thinking of far better insults to put in that field than you.  Many
                of them were AOL users.  You've got a ways to go.</font></p>
  </td>
  </tr>
</table>
<?php
if ($db) {
    
pg_exec($db'commit');
    
pg_close($db);
}
?>