#!/usr/bin/perl
#

use 5.006;
use DBI;
use CGI;
use lib qw(/mnt/sda/tmm/handlers/genc);
use TClubMD5;

my $server = "127.0.0.1";
my $user = $primary_user;
my $passwd = $primary_passwd;
my $dbase = $primary_dbase;
my $port = $primary_port;

my $query = new CGI;

print "Content-Type: text/html; charset=UTF-8\n\n";

my $r_user = $query->param('who');
my $token = $query->param('token');
my $ru_en = $query->param('l');
my $language = $query->param('lang');
my $db = $query->param('db');
my $gmtimeshift = $query->param('gmshift');

my $session = TClubMD5::md5_hex($r_user.$salt);

die("show_active.pl: token is $token, session is $session!\n") if ($token ne $session);
die("psssooff!\n") if (in_list($who,$pssoff_guys_string));

my $min = '';
my $sec = '';

if ($language eq 'russian') {
$min = 'мин.';
$sec = 'сек. назад';
} else {
$min = 'min.';
$sec = 'sec. ago';
};

($dbase,$server,$port,$user,$passwd) = TClubMD5::get_connect_req($ru_en);

$dbase = "clone" if ($dbase eq 'rlx');

$dbconn=DBI->connect("dbi:Pg:dbname=$dbase;port=$port;host=$server",$user, $passwd);
	
my $and = '';
$and = " and (e_masterst ~ '$opz' or e_masterst ~ '$opf' or e_masterst ~ '$tpz' or e_masterst ~ '$tpf' or e_masterst ~ '$hpz') " 
	if ($db eq 'three'); 
	
$comm = "select a_nkname, (current_timestamp - p_vremya_z), oid, proj_code, blacklist from members where (current_timestamp - p_vremya_z)<= '5 min' and (current_timestamp - p_vremya_z) > '0 sec' and proj_code != '$extra_admin' $and order by p_vremya_z desc";
&getTable;

#print STDERR "Here comm is $comm !\n";
&make_date($gmtimeshift);

my $xml = $this__." ".$thistime;
$xml .= " MSK" if ($ru_en eq 'ru' || $ru_en eq 'ru_THR' || $ru_en eq 'ru_BRL' || $ru_en eq 'ru_KLG' || $ru_en eq 'ru_VRN');
$xml .= '&';
$xml .= '<div style="padding:2px;">';

for (my $i=0; $i<$ntuples;$i++) {

	my $who = ${${$listresult}[$i]}[0];
	my $ago = ${${$listresult}[$i]}[1];
	my $bl_string = ${${$listresult}[$i]}[4];
	next if (in_list($r_user,$bl_string) && $r_user ne $extra_admin);

	$who = ${${$listresult}[$i]}[3] if (in_list(${${$listresult}[$i]}[3],$changing_guys_string));
	$who = substr($who,0,rindex ($who,' ')+5).'..' if ($who =~ /(.+)\s/ && length($who) > 24);
	$ago =~ /^00:0(\d):(\d\d)(.*)/;
	$ago = $1.' '.$min.' '.$2.' '.$sec;

	$xml .= '<div style="float:left;padding:2px;" onClick="document.getElementById(\'log\').style.display=\'none\';document.getElementById(\'ask\').style.display=\'inline\';">
	<a href=javascript:window.alert_updater('.${${$listresult}[$i]}[2].')>' . $who . '</a></div>';
	$xml .= '<div style="float:right;padding-right:2px;">' . $ago . '</div>';
	$xml .= '<div style="clear:both;"></div>';
}

$xml .= '</div>';
print $xml;

$dbconn->disconnect;
$query->delete_all;
exit;

sub getTable {

	$result=$dbconn->prepare($comm);
    	$result->execute;
	&dBaseError($result, $comm."  (".$result->rows()." rows found)") if ($result->rows() ==
	-2);
	
	$listresult = $result->fetchall_arrayref;
	$ntuples = $result->rows();

};

sub dBaseError {

    my ($check, $message) = @_;
    print "<H4><FONT COLOR=BLACK><P>$message<BR>Error: ".$check->errstr."</FONT></H4>";
    die("Action failed on command:$message  Error_was:$DBI::errstr");
};

sub in_list {
my $guy = shift;
my $str = shift;
my $comma = shift;

if (defined($comma)) {
return 1 if ($str eq $guy || $str =~ /^$guy\,/ || $str =~ /\,$guy\,/ || $str =~ /\,$guy$/);
return 0;
} else {
return 1 if ($str eq $guy || $str =~ /^$guy\#/ || $str =~ /\#$guy\#/ || $str =~ /\#$guy$/);
return 0;
}
}

sub make_date 
{

my $addedtime = shift;
my $t; my $this_;
    ($thissec,$thismin,$thishour,$mday,$mon,$thisyear,$t,$t,$t) = gmtime(time+$addedtime);
    $mon++;
	my $month = $months[$mon];
	     $month = $$month if $$month;
    $thisyear += 1900;
    $current_date = "$mday $month $thisyear";
    $current_date = "$month $mday, $thisyear" if ($language eq "english");
    
    if ($mday >= 10 && $mon >= 10 ) { $this_ = "$mon/$mday";$this__ = "$mday/$mon";}
    elsif ($mon >= 10) { $this_ = "$mon/0$mday";$this__ = "0$mday/$mon";}
    elsif ($mday >= 10) { $this_ = "0$mon/$mday"; $this__ = "$mday/0$mon";}
    else { $this_ = "0$mon/0$mday"; $this__ = "0$mday/0$mon";}

	$thisdate = "$thisyear/$this_";
	
    
    if ($thishour >= 10 && $thismin >= 10 ) { $thistime = "$thishour:$thismin";}
    elsif ($thishour >= 10) { $thistime = "$thishour:0$thismin";}
    elsif ($thismin >= 10) { $thistime = "0$thishour:$thismin";}
    else { $thistime = "0$thishour:0$thismin";}
}
