#!/usr/local/bin/perl
# search_user.cgi
# Search the password file, and display a list of results

require './user-lib.pl';
&ReadParse();
%access = &get_module_acl();
@ulist = &list_users();
$m = $in{'match'};
$w = $in{'what'};
for($i=0; $i<@ulist; $i++) {
	$u = $ulist[$i];
	$f = $u->{$in{'field'}};
	if ($m == 0 && $f eq $w ||
	    $m == 1 && eval { $f =~ /$w/i } ||
	    $m == 4 && index($f, $w) >= 0 ||
	    $m == 2 && $f ne $w ||
	    $m == 3 && eval { $f !~ /$w/i } ||
	    $m == 5 && index($f, $w) < 0) {
		push(@match, $u) if (&can_edit_user(\%access, $u));
		}
	}
if (@match == 1) {
	&redirect("edit_user.cgi?num=".$match[0]->{'num'});
	}
else {
	&header($text{'search_title'}, "");
	print "<hr>\n";
	if (@match == 0) {
		print "<p><b>$text{'search_notfound'}</b>. <p>\n";
		}
	else {
		print "<table border width=100%>\n";
		print "<tr $tb> <td><b>$text{'user'}</b></td>\n";
		print "<td><b>$text{'uid'}</b></td>\n";
		print "<td><b>$text{'real'}</b></td>\n";
		print "<td><b>$text{'home'}</b></td> </tr>\n";
		foreach $m (@match) {
			$m->{'real'} =~ s/,.*$//;
			print "<tr $cb>\n";
			print "<td><a href=\"edit_user.cgi?num=$m->{'num'}\">",
			      &html_escape($m->{'user'}),"</a></td>\n";
			print "<td>$m->{'uid'}</td>\n";
			print "<td>",&html_escape($m->{'real'}),"&nbsp;</td>\n";
			print "<td>",&html_escape($m->{'home'}),"</td>\n";
			print "</tr>\n";
			}
		print "</table><p>\n";
		}
	print "<hr>\n";
	&footer("", $text{'index_return'});
	}

