#!/usr/local/bin/perl
# list_sessions.cgi
# Display current login sessions

require './acl-lib.pl';
$access{'sessions'} || &error($text{'sessions_ecannot'});
&header($text{'sessions_title'}, "");
print "<hr>\n";

&get_miniserv_config(\%miniserv);
&open_session_db(\%miniserv);
$time_now = time();

foreach $u (&list_users()) {
	$hasuser{$u->{'name'}}++;
	}

print "<b>$text{'sessions_desc'}</b><p>\n";
print "<table border>\n";
print "<tr $tb> <td><b>$text{'sessions_id'}</b></td> ",
      "<td><b>$text{'sessions_user'}</b></td> ",
      "<td><b>$text{'sessions_login'}</b></td> ",
      "<td><br></td> </tr>\n";
foreach $k (sort { @a=split(/\s+/, $sessiondb{$a}); @b=split(/\s+/, $sessiondb{$b}); $b[1] <=> $a[1] } keys %sessiondb) {
	next if ($k =~ /^1111111/);
	local ($user, $ltime) = split(/\s+/, $sessiondb{$k});
	next if ($miniserv{'logouttime'} &&
		 $time_now - $ltime > $miniserv{'logouttime'}*60);
	print "<tr $cb>\n";
	print "<td><a href='delete_session.cgi?id=$k'>$k</a></td>\n";
	if ($hasuser{$user}) {
		print "<td><a href='edit_user.cgi?user=$user'>$user</a></td>\n";
		}
	elsif ($miniserv{'unixauth'}) {
		print "<td>$user (<a href='edit_user.cgi?user=$miniserv{'unixauth'}'>$miniserv{'unixauth'}</a>)</td>\n";
		}
	else {
		print "<td>$user</td>\n";
		}
	local $tm = localtime($ltime);
	print "<td><tt>$tm</tt></td>\n";
	print "<td><a href='/webminlog/search.cgi?uall=1&mall=1&tall=1&sid=$k'>",
	      "$text{'sessions_lview'}</a></td>\n";
	print "</tr>\n";
	}
print "</table><br>\n";

print "<hr>\n";
&footer("", $text{'index_return'});

