#!/usr/pkg/bin/perl
# ACL managing
#
require './functions-lib.pl';

use CGI; $q=new CGI; 
iframe_header();

my $err="", $root="", $reload=0;
my $mbox=$q->param('mbox');

$cyrus = &cyrus_connect();

if ($mbox)
	{
	check_mailbox($mbox);
    @list=$cyrus->list($mbox.$SEP."*"); 
    unshift @list, $cyrus->list("$mbox"); 
	}

# set the ACLs of the mailbox and all subfolders
if ($q->param('setACL'))
    {
# first, delete all ACLs (except the admin!)
    %acls=$cyrus->listacl($mbox);
    while (($user,$acl)=each (%acls))
        {
        next if $user eq $config{admin_user};        
        foreach (@list)
            {
	        $cyrus->deleteacl($_->[0], $user);
	        $err=$cyrus->error;
	        msglog("delete ACL $user on ".name_decode($_->[0])) unless $err;
	        }
        }
    $n=0; $err="";
    while ($q->param("user$n") && !$err)
        {
        my $myACL="";
        foreach $acl (@ACLS) { $myACL.=$acl if $q->param($acl.$n)==1; }
        foreach (@list)
            {
            $cyrus->setacl($_->[0], $q->param("user$n")=>$myACL);
            $err=$cyrus->error;
            msglog("set ACL ".$q->param("user$n")." on ".name_decode($_->[0])) unless $err;	
            }
        $n++;
        }
    }

# delete an ACL of the mailbox and all subfolders
if ($q->param('delACL'))
	{ 
    foreach (@list)
        {
	    $cyrus->deleteacl($_->[0], $q->param('delACL'));
	    $err=$cyrus->error;
	    msglog("delete ACL ".$q->param('delACL')." on ".name_decode($_->[0])) unless $err;
	    }
	}

iframe_footer($mbox,$err,$reload);