#!/usr/local/bin/perl
# index.cgi
# Display a list of protected directories and their users. The user can
# add more directories, and specify the encryption mode for each.

require './htaccess-lib.pl';
&ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);

@accessdirs || &error($text{'index_eaccess'});

@dirs = &list_directories();
@dirs = grep { &can_access_dir($_->[0]) && -d $_->[0] } @dirs;
if (@dirs) {
	print "<a href='edit_dir.cgi?new=1'>$text{'index_add'}</a><br>\n";
	print "<table border width=100%>\n";
	print "<tr $tb> <td width=30%><b>$text{'index_dir'}</b></td> ",
	      "<td width=70%><b>$text{'index_users'}</b></td> </tr>\n";
	foreach $d (@dirs) {
		print "<tr $cb>\n";
		print "<td valign=top width=30%><a href='edit_dir.cgi?dir=",
			&urlize($d->[0]),"'>$d->[0]</a></td>\n";
		print "<td width=70%><table width=100%>\n";
		$users = &list_users($d->[1]);
		if ($userconfig{'sort'} == 1 || $config{'sort'} == 1) {
			$users = [ sort { $a->{'user'} cmp $b->{'user'} }
					@$users ];
			}
		for($i=0; $i<@$users; $i++) {
			$u = $users->[$i];
			$link = "<a href='edit_user.cgi?idx=$u->{'index'}&dir=".
				&urlize($d->[0])."'>$u->{'user'}</a>";
			print "<tr>\n" if ($i%4 == 0);
			if ($u->{'enabled'}) {
				print "<td width=25%>$link</td>\n";
				}
			else {
				print "<td width=25%><i>$link</i></td>\n";
				}
			print "</tr>\n" if ($i%4 == 3);
			}
		if ($i%4) {
			while($i++%4) { print "<td width=25%></td>\n"; }
			print "</tr>\n";
			}
		if (!@$users) {
			print "<tr> <td colspan=4><i>$text{'index_nousers'}",
			      "</i></td> </tr>\n";
			}
		print "</table>\n";
		print "<a href='edit_user.cgi?new=1&dir=",&urlize($d->[0]),"'>$text{'index_uadd'}</a>\n";
		print "</td>\n";
		print "</tr>\n";
		}
	print "</table>\n";
	}
else {
	print "<b>$text{'index_none'}</b><p>\n";
	}
print "<a href='edit_dir.cgi?new=1'>$text{'index_add'}</a><p>\n";

print "<hr>\n";
print "<form action=search.cgi>\n";
print "<input type=submit value='$text{'index_search'}'>\n";
printf "<input name=search size=30 value='%s'> %s<br>\n",
	$accessdirs[0] eq "/" ? "" : $accessdirs[0],
	&file_chooser_button("search", 1);
print "</form>\n";

&ui_print_footer("/", $text{'index'});
