Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > dca483b59ba61f3fa092de932ddd570e > files > 236

nuface-2.0.14-2mdv2009.1.i586.rpm

<?php
# Copyright(C) 2007 INL http://www.inl.fr/
# Written by Victor Stinner <victor.stinner AT inl.fr>
#
# $Id: groups.php 17927 2009-02-16 13:16:09Z haypo $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

require_once('page_function.php');   # $page_new_button

class group
{
    var $name;
    var $ID;
    var $enabled;
    var $comment;

    function group($ress,$type='xml',$id=0){
        if ($type != 'xml') {
            if ($id==0){
                log_error("Must be passed a valid ID!");
                return(-1);
            }
            $this->ID=$id;
            $this->name=$ress['name'];
            $this->enabled=$ress['enabled'];
            $this->comment=$ress['comment'];
        }else{
            $this->name=$ress->get_attribute('name');
            $this->ID=$ress->get_attribute('ID');
            $this->enabled=$ress->get_attribute('enabled');
            $this->comment=$ress->get_attribute('comment');
        }
    }

    function str()
    {
        return sprintf(_("ACL group #%s"), $this->ID);
    }

    function usage($acls)
    {
        $usage=0;
        foreach ($acls->list_tab() as $aclid=>$thisaclname)
        {
            $thisacl=$acls->get_elt($aclid);
            if ($thisacl->group == $this->ID)
            {
                $usage++;
            }
        }
        return $usage;
    }

    function is_used_acls($acls)
    {
        foreach ($acls->list_tab() as $aclid=>$thisaclname)
        {
            $thisacl=$acls->get_elt($aclid);
            if ($thisacl->group == $this->ID)
                return true;
        }
        return false;
    }

    function is_used($ruleset)
    {
        return ($this->is_used_acls($ruleset->acls)
            or $this->is_used_acls($ruleset->localsin)
            or $this->is_used_acls($ruleset->localsout));
    }

    function del_elt($index){
        unset ($this);
    }

    function xmldump($xml,$node){
        $node->set_attribute('name',$this->name);
        $node->set_attribute('ID',$this->ID);
        $node->set_attribute('comment',$this->comment);
        $node->set_attribute('enabled',$this->enabled);
    }

    function new_id(){
        return($this->get_max_id()+1);
    }

    function get_id(){
        return($this->ID);
    }

    function cloneobj(){
        $t=array();
        $t['name']=$this->name;
        $t['ID']=$this->ID;
        $t['enabled']=$this->enabled;
        $t['comment']=$this->comment;
        return new group($t,$type='toto',$id=$t['ID']);
    }
}

function display_groups($ruleset)
{
    global $title, $page_new_button, $singtitle, $acl_group;

    echo "<h3>"._("ACL groups")."</h3>";

    print '<form action="'.$title.'.php" method="post">'."\n";
    print '<input type="hidden" name="modify_groups" value="1">'."\n";
    print '<table class="aclgroups">'."\n";
    print "<tr>";
    print "<th>"._('Enabled')."</th>";
    print "<th>"._('Name')."</th>";
    print "<th>"._('Symbol')."</th>";
    print "<th>"._('Comment')."</th>";
    print "<th>"._("Count")."</th>";
    print "<th>"._('Delete')."</th>";
    print "</tr>\n";

    $groups=$ruleset->groups;
    $array_groups=array();
    foreach ($groups->list_tab() as $key=>$foo)
    {
        if($foo){
            array_push($array_groups,$key);
        }
    }
    sort($array_groups);

    foreach ($array_groups as $key)
    {
        $group = $groups->get_elt($key);
        $url = "$title.php?grhigh=$key";

        print "<tr>";
        print "<td><input type=checkbox name=enabled".$key." ";
        if ($group->enabled == 1)
            echo "checked";
        echo "></td>";
        print "<td>";
        html_input_text("name$key", $group->name, Array('style' => 'width: 6em;'));
        print "</td>";
        print "<td><a href=\"$url\"><img src=\"images/".$key.".png\" alt=\"grp$key\"></a></td>";
        print "<td>";
        html_input_text("comment$key", $group->comment, Array('style' => 'width: 10em;'));
        print "</td>";

        $usages = Array(
            $group->usage($ruleset->acls),
            $group->usage($ruleset->localsin),
            $group->usage($ruleset->localsout));
        $total_usage = array_sum($usages);

        if ($title == 'localsin')
            $local_usage = $usages[1];
        else if ($title == 'localsout')
            $local_usage = $usages[2];
        else
            $local_usage = $usages[0];

        echo "<td>";
        if ($local_usage>0)
            print "<a href=\"$url\">$local_usage</a>";
        else
            print $local_usage;
        echo "</td>";

        if ($total_usage==0) {
            echo '<td><input type="checkbox" name="del_grp['.$group->ID.']"/></td>';
        } else {
            echo "<td>-</td>";
        }

        echo "</tr>\n";

    }
    print "</table>\n";
    print '<input type="submit">'."\n";
    print "</form>\n";
}

function modify_groups($ruleset)
{
    $del_grp = getHttp('del_grp', Array());
    $modified = 0;
    $groups = $ruleset->groups;
    foreach ($groups->list_tab() as $key=>$foo)
    {
        $local_mod=0;
        $received=array();
        $group=$groups->get_elt($key);
        if (isset($_REQUEST["enabled".$key]) and $_REQUEST["enabled".$key]=='on')
            $received['enabled']=1;
        else
            $received['enabled']=0;

        $name = getHttp("name$key");
        if (!$name || !check_comment($name, "name".$key)) {
            $name = $group->name;
        }
        $received['name'] = $name;

        $comment = getHttp("comment".$key, "");
        if (!check_comment($comment, "comment".$key)) {
            $comment = '';
        }
        $received['comment'] = $comment;

        if ($received['enabled'] != $group->enabled)
            $local_mod++;
        if ($received['name'] != $group->name)
            $local_mod++;
        if ($received['comment'] != $group->comment)
            $local_mod++;
        $grp_deleted = false;
        if (array_key_exists($group->ID, $del_grp)
            and $del_grp[$group->ID] == 'on'
            and !$group->is_used($ruleset)){
            /* delete group if checkbox is checked */
            $groups->del_elt($group->ID);
            $grp_deleted = true;
            $modified++;
        }
        if ($local_mod>0 && !$grp_deleted)
        {
            try {
                $new_grp=new group($received,'data',$group->ID);
                $groups->replace_elt($new_grp);
            } catch (Exception $err) {
                log_error(sprintf(_("Unable to change %s: %s"),
                    $group->str(), $err->getMessage()), $err->getTrace());
                return;
            }
            $modified++;
        }
    }
    if ($modified>0)
    {
        saveRuleset($ruleset);
    }
}

function createAclGroup($ruleset)
{
    $new_name=getHttp('name');
    if (!check_name($new_name, 'name')) {
        return;
    }

    $new_group = Array(
        'name' => $new_name,
        'modified' => modifiedTimestamp(),
        'enabled' => true,
        'comment' => '',
    );
    try {
        $group = new group($new_group,'data',$ruleset->groups->new_lower_id());
        $ruleset->groups->add_elt($group);
        saveRuleset($ruleset);
    } catch (Exception $err) {
        log_error(sprintf(_("Unable to create the new ACL group: %s"),
                    $err->getMessage()), $err->getTrace());
        return null;
    }
}

?>