#!/usr/bin/perl
require "config.cgi";
#$cellheadbg = "#454545";
#$cellcolor = "#606060";
#$headfont = "#00eeee";
#$table = "#505050";
#$actcolor = "#ccbb00";
#$fontcolor = "#dddddd";
#$menucolor = "#ffffff";
$act = param("act");
$user = param("user");
$menu = "";
$raduname = param("raduname");
$radgrp = param("radgrp");
$radpwd = param("radpwd");
$radunamee = param("radunamee");
$radgrpe = param("radgrpe");
$radpwde = param("radpwde");
$radunameo = param("radunameo");
$authl = param("authl");
$authp = param("authp");
$menu = "accounts";
require "top.cgi";
if ($act eq "del")
    {
    $sth = $dbh->prepare("DELETE FROM radcheck WHERE username='$user'");
    $sth->execute();
    $sth = $dbh->prepare("SELECT groupname FROM radusergroup WHERE username='$user'");
    $sth->execute();
    my ($group) = $sth->fetchrow_array();
    if ($group ne "")
	{
	$sth = $dbh->prepare("DELETE FROM radusergroup WHERE username='$user'");
	$sth->execute();
	}
    }
if ($radunamee ne "")
    {
        $sth = $dbh->prepare("SELECT username FROM radcheck WHERE username='$radunamee'");
        $sth->execute();
        my ($cuser) = $sth->fetchrow_array();
        if ($radunamee eq $cuser and $radunamee ne $radunameo)
        {
            print "
| Ошибка, пользователь $cuser существует | 
";
        } else {
    	    $sth = $dbh->prepare("UPDATE radcheck SET username='$radunamee',value='$radpwde' WHERE username='$radunameo'");
	    $sth->execute();
	    $sth = $dbh->prepare("SELECT groupname FROM radusergroup WHERE username='$radunameo'");
	    $sth->execute();
	    my ($group) = $sth->fetchrow_array();
	    if ($radunameo ne $radunamee and $group eq $radgrpe)
		{
		$sth = $dbh->prepare("UPDATE radusergroup SET username='$radunamee' WHERE username='$radunameo'");
		$sth->execute();
		}
	    if ($group eq "" and $radgrpe ne "")
		{
		$sth = $dbh->prepare("INSERT INTO radusergroup (username,groupname,priority) VALUES ('$radunamee','$radgrpe','1')");
		$sth->execute();
		}
	    if ($group ne "" and $radgrpe ne "" and $group ne $radgrpe)
		{
		$sth = $dbh->prepare("DELETE FROM radusergroup WHERE username='$radunameo' AND groupname='$group'");
		$sth->execute();
		$sth = $dbh->prepare("INSERT INTO radusergroup (username,groupname,priority) VALUES ('$radunamee','$radgrpe','1')");
		$sth->execute();
		}
	    if ($group ne "" and $radgrpe eq "")
		{
		$sth = $dbh->prepare("DELETE FROM radusergroup where username='$radunameo'");
		$sth->execute();
		}
	}
    }
if ($raduname ne "")
    {
	$sth = $dbh->prepare("SELECT username FROM radcheck WHERE username='$raduname'");
	$sth->execute();
	my ($cuser) = $sth->fetchrow_array();
	if ($raduname eq $cuser)
	{
	    print "| Ошибка, пользователь $cuser существует | 
";
	} else 
	{
	    $sth = $dbh->prepare("INSERT INTO radcheck (username,attribute,op,value) VALUES ('$raduname','Cleartext-Password',':=','$radpwd')");
	    $sth->execute();
	    if ($radgrp ne "")
	    {
		$sth = $dbh->prepare("INSERT INTO radusergroup (username,groupname,priority) VALUES ('$raduname','$radgrp','1')");
		$sth->execute();
	    
	    }
	}
    }
print "
| Имя пользователя
  
 | Пароль | Группа | Действие | 
";
$sth = $dbh->prepare("SELECT username,value FROM radcheck");
$sth->execute();
    while (my ($username, $pwd) = $sth->fetchrow_array())
    {
	    $sth1 = $dbh->prepare("SELECT groupname FROM radusergroup WHERE username='$username'");
	    $sth1->execute();
	my ($group) = $sth1->fetchrow_array();
	if ($act eq "edit" and $user eq $username)
	{
	    print "|
";
	    }
	}
    }
    if ($act ne "edit") {
    print "";}
    
print "
";