<?

// Michael Yarbrough
// opedog@comediccadavers.com
// http://www.comediccadavers.com/

// PHP-Nuke 5.1 Blocks version by Thiago Campos aka Mr. Hemp (mrhemp@amigoz.org)

// Updated to work with PHP-Nuke 5.5 and the phpBB2 forum port by
// Tom Nitzschner (tom@toms-home.com)
// http://bbtonuke.sourceforge.net (http://www.toms-home.com)
//
// Please change your prefix to suit your database.
// Set $numusers to the count of people you want listed +1
//

global $cookie, $prefix, $currentlang, $dbi, $username, $numusers;
$prefix = "nuke";
$numusers = 11;

if(file_exists("language/lastseen/lastseen-$currentlang.php")) {
	include("language/lastseen/lastseen-$currentlang.php");
}
else {
	include("language/lastseen/lastseen-english.php");
}

        $username = $cookie[1];

//        sql_query("CREATE TABLE IF NOT EXISTS $prefix"._lastseen." (id INT (15) not null AUTO_INCREMENT, username TEXT not null, date INT(15) not null, ip CHAR(50), PRIMARY KEY (id), UNIQUE (id))", $dbi);

        if (isset($username)) {
                $ip = getenv("REMOTE_HOST");
                if (empty($ip)) {
                        $ip = getenv("REMOTE_ADDR");
                }

                $result = sql_query("SELECT * FROM $prefix"._lastseen." WHERE username = \"$username\"", $dbi);
                if (sql_num_rows($result, $dbi) > 0) {
                        sql_query("UPDATE $prefix"._lastseen." SET date = " . time() . " WHERE username = \"$username\"", $dbi);
                } else {
                        sql_query("INSERT INTO $prefix"._lastseen." VALUES (\"\", \"$username\", ".time().", \"".$ip."\")", $dbi);
                }
        }
        $content = "";
        $result = sql_query("SELECT username, date FROM $prefix"._lastseen." ORDER BY date DESC limit $numusers", $dbi);
        while (list($uname, $date) = sql_fetch_row($result, $dbi)) {
                if ($uname != $username) {
                        $realtime = time() - $date;
                        $dont = false;

                        // how many days ago?
                        if ($realtime >= (60*60*24*2)) { // if it's been more than 2 days
                                $days = floor($realtime / (60*60*24));
                                $dont = true;
                        } else if ($realtime >= (60*60*24)) { // if it's been less than 2 days
                                $days = 1;
                                $realtime -= (60*60*24);
                        }

                        if (!$dont) {
                                // how many hours ago?
                                if ($realtime >= (60*60)) {
                                        //$content .= " ($realtime) ";
                                        $hours = floor($realtime / (60*60));
                                        $realtime -= (60*60*$hours);
                                }

                                // how many minutes ago?
                                if ($realtime >= 60) {
                                        $mins = floor($realtime / 60);
                                        $realtime -= (60*$mins);
                                }

                                // just a little precation, although I don't *think* mins will ever be 60...
                                if ($mins == 60) {
                                        $mins = 0;
                                        $hours += 1;
                                }
                        }
			$myresult = sql_query("select uid from nuke_users where (uname='$uname')", $dbi);
    			list($uid) = sql_fetch_row($myresult, $dbi);
                        $content .= "<font class=tiny><a href=\"modules.php?name=Forums&file=profile&mode=viewprofile&u=".$uid."\">".$uname."</a>:";
                        if ($dont) {
                                $content .= " ".$days." "._LASTSEENDAYS."";
                        } else {
                                if ($days > 0) {
                                        $content .= " ".$days." "._LASTSEENDAY."".(($hours == 0 && $mins == 0)?(""):(","));
                                }
                                if ($hours > 0) {
                                        $content .= " ".$hours." ".(($hours > 1)?(""._LASTSEENHOURS.""):(""._LASTSEENHOUR."")).(($mins == 0)?(""):(","));
                                }
                                if ($mins > 0) {
                                        $content .= " ".$mins." ".(($mins > 1)?(""._LASTSEENMINUTES.""):(""._LASTSEENMINUTE.""))."";
                                }  else { // less than a minute :)
                                        $content .= " ".$realtime." "._LASTSEENSECONDS."";
                                }
                        }

                        $content .= " "._LASTSEENAGO."</font><br>";

                        $days = 0;
                        $hours = 0;
                        $mins = 0;
                        $dont = false;
                }
        }

        $content .= "";


?>

