#!/usr/local/bin/perl
# list_users.cgi
# Display a list of all database users

require './mysql-lib.pl';
$access{'perms'} == 1 || &error($text{'perms_ecannot'});
&header($text{'users_title'}, "", "users");
print "<hr>\n";

print "<a href='edit_user.cgi?new=1'>$text{'users_add'}</a> <br>\n";
print "<table border>\n";
print "<tr $tb> <td><b>$text{'users_user'}</b></td>\n",
      "<td><b>$text{'users_host'}</b></td>\n",
      "<td><b>$text{'users_pass'}</b></td>\n",
      "<td><b>$text{'users_perms'}</b></td> </tr>\n";
$d = &execute_sql($master_db, "select * from user order by user");
$i = 0;
foreach $u (@{$d->{'data'}}) {
	print "<tr $cb>\n";
	print "<td><a href='edit_user.cgi?idx=$i'>",
		$u->[1] ? &html_escape($u->[1]) : $text{'users_anon'},
		"</a></td>\n";
	print "<td>", $u->[0] eq '' || $u->[0] eq '%' ?
		      $text{'users_all'} : &html_escape($u->[0]),"</td>\n";
	print "<td>",$u->[2] ? &html_escape($u->[2]) : "<br>","</td>\n";
	local @priv;
	for($j=3; $j<=16; $j++) {
		push(@priv, $text{"users_priv$j"}) if ($u->[$j] eq 'Y');
		}
	printf "<td>%s</td>\n",
		scalar(@priv) == 14 ? $text{'users_all'} :
		!@priv ? $text{'users_none'} : join("&nbsp;| ", @priv);
	print "</tr>\n";
	$i++;
	}
print "</table>\n";
print "<a href='edit_user.cgi?new=1'>$text{'users_add'}</a> <p>\n";

print "<hr>\n";
print "<form action=save_sync.cgi>\n";
print "$text{'users_sync'}<br>\n";
print "<table><tr><td valign=top>\n";
printf "<input type=checkbox name=sync_create value=1 %s> %s<br>\n",
	$config{'sync_create'} ? "checked" : "", $text{'users_sync_create'};
printf "<input type=checkbox name=sync_modify value=1 %s> %s<br>\n",
	$config{'sync_modify'} ? "checked" : "", $text{'users_sync_modify'};
printf "<input type=checkbox name=sync_delete value=1 %s> %s<br>\n",
	$config{'sync_delete'} ? "checked" : "", $text{'users_sync_delete'};

map { $priv{$_}++ } split(/\s+/, $config{'sync_privs'});
print "</td><td><select name=sync_privs multiple size=5>\n";
for($i=3; $i<=16; $i++) {
	printf "<option value=%d %s>%s\n",
		$i, $priv{$i} ? 'selected' : '',
		$text{"user_priv$i"};
	}
print "</select></td> </tr></table>\n";
print "<input type=submit value='$text{'save'}'></form>\n";

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

