#!/usr/bin/perl

do "ldap_users.pl";
do "users.pl";
&connect ();
&ReadParse();

$sort_on = $in{sort_on};

# *** falta criar home dir, /etc/skel, etc...

if ($in{save}) {
    &user_from_form;

    if ($in{new}) {
        if (! &new_user_ok) {
            &error ($text{error_1});
        }

        &header ($text{header_1}, "");
        $uidnumber = ($uidnumber) ? $uidnumber : &max_uidnumber;
        $dn = &create_user;
        &set_passwd ($dn, $userpassword, $in{hash});
        if ($in{create}) {
            mkdir $homedirectory, 0700;
            if ($in{copy}) {
                system "cp -rf /etc/skel/* $homedirectory";
                system "cp -rf /etc/skel/.[a-z,A-Z,0-9]* $homedirectory";
            }
            system "chown -R $uidnumber.$gidnumber $homedirectory";
        }
    }
    else {
        if (! changed_user_ok) {
            &error ($text{error_2});
        }

        &header ($text{header_2}, "");
        &update_user ($in{dn});
    }
}

# display current user data

$dn = ($dn) ? $dn : $in{dn};
$user = &get_user_attr ($dn);

if ($user) {
    $header = $text{header_3};
    $new = 0;
}
else {
    $header = $text{header_4};
    $new = 1;
}
if (length ($in{new}) == 0) {
    &header ($header, "");
}
print "<HR noshade size=2>\n";


&user_from_entry ($user);

if ($new) {
    &user_defaults;
}

print "<FORM method=\"post\"action=\"edit_user.cgi\">\n";
print "<TD><INPUT type=\"hidden\" name=\"new\" value=\"$new\">\n";
print "<TD><INPUT type=\"hidden\" name=\"dn\" value=\"$dn\">\n";
print "<TD><INPUT type=\"hidden\" name=\"sort_on\" value=\"$sort_on\">\n";
print "<TABLE border width=100% $cb>\n";
print "<TR><TD>\n";
print "<TABLE border=0 cellspacing=0 cellpadding=2 width=100% $cb>\n";

# posixAccount
print "<TR><TD colspan=2 $tb><B>" . $text{posixAccount} . "</B> (posixAccount)\n";
print "</TD></TR>";
print "<TR><TD><B>" . $text{uid} . "</B> (uid)\n";
print "</TD>";
print "<TD><INPUT name=\"uid\" size=12 value=\"$uid\">\n";
print "</TD></TR>";
print "<TR><TD><B>" . $text {uidnumber} . "</B> (uidnumber)\n";
print "</TD>";
print "<TD><INPUT name=\"uidnumber\" size=5 value=\"$uidnumber\"> (*)\n";
print "</TD></TR>";
# should display a <SELECT> from the availabe users
print "<TR><TD><B>" . $text{gidnumber} . "</B> (gidnumber)\n";
print "</TD>";
print "<TD><INPUT name=\"gidnumber\" size=5 value=\"$gidnumber\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{gecos} . " (gecos)\n";
print "</TD>";
print "<TD><INPUT name=\"gecos\" size=40 value=\"$gecos\"> (*)\n";
print "</TD></TR>";
print "<TR><TD>" . $text {homedirectory} . " (homedirectory)\n";
print "</TD>";
print "<TD><INPUT name=\"homedirectory\" size=30 value=\"$homedirectory\"> (*)\n";
print "</TD></TR>";
# should display a <SELECT> from /etc/shells
print "<TR><TD><B>" . $text {loginshell} . "</B> (loginshell)\n";
print "</TD>";
print "<TD><INPUT name=\"loginshell\" size=20 value=\"$loginshell\">\n";
print "</TD></TR>";

# address book data (inetOrgPerson, person, etc...)
print "<TR><TD colspan=2 $tb><B>" . $text{addressbook} . "</B>\n";
print "</TD></TR>";
print "<TR><TD><B>" . $text{givenname} . "</B> (givenname)\n";
print "</TD>";
print "<TD><INPUT name=\"givenname\" size=20 value=\"$givenname\">\n";
print "</TD></TR>";
print "<TR><TD><B>" . $text{sn} . "</B> (sn)\n";
print "</TD>";
print "<TD><INPUT name=\"sn\" size=20 value=\"$sn\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{cn} . " (cn)\n";
print "</TD>";
print "<TD><INPUT name=\"cn\" size=40 value=\"$cn\"> (*)\n";
print "</TD></TR>";
print "<TR><TD>" . $text{mail} . " (mail)\n";
print "</TD>";
print "<TD><INPUT name=\"mail\" size=30 value=\"$mail\"> (*)\n";
print "</TD></TR>";
print "<TR><TD>" . $text{title} . " (title)\n";
print "</TD>";
print "<TD><INPUT name=\"title\" size=30 value=\"$title\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{organizationname} . " (organizationname)\n";
print "</TD>";
print "<TD><INPUT name=\"organizationname\" size=40 value=\"$organizationname\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text {department} ." (department)\n";
print "</TD>";
print "<TD><INPUT name=\"department\" size=40 value=\"$department\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{physicaldeliveryofficename} . " (physicaldeliveryofficename)\n";
print "</TD>";
print "<TD><INPUT name=\"physicaldeliveryofficename\" size=15 value=\"$physicaldeliveryofficename\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text {telephonenumber} . " (tephonenumber)\n";
print "</TD>";
print "<TD><INPUT name=\"telephonenumber\" size=20 value=\"$telephonenumber\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{mobile} . " (mobile)\n";
print "</TD>";
print "<TD><INPUT name=\"mobile\" size=20 value=\"$mobile\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{pager} . " (pager)\n";
print "</TD>";
print "<TD><INPUT name=\"pager\" size=20 value=\"$pager\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{officefax} . " (officefax)\n";
print "</TD>";
print "<TD><INPUT name=\"officefax\" size=20 value=\"$officefax\">\n";
print "</TD></TR>";
print "<TR><TD>" . $text{comment} . " (comment)\n";
print "</TD>";
print "<TD><TEXTAREA name=\"comment\" cols=60 rows=5>$comment</TEXTAREA>\n";
print "</TD></TR>";

# user creation options
if ($new) {
    print "<TR><TD colspan=2 $tb><B>" . $text{user_options} . "</B>\n";
    print "</TD></TR>";
    print "<TR><TD>" . $text{userpasswd} . "\n";
    print "</TD>";
    print "<TD><INPUT name=\"userpassword\" size=12 value=\"$userpassword\"> (*)\n";
    print "</TD></TR>";
    print "<TR><TD>" . $text{passwdtype} . "\n";
    print "</TD><TD>";
    print "<INPUT type=\"radio\" name=\"hash\" value=\"md5\">" .
        $text{md5} . "\n";
    print "<INPUT type=\"radio\" name=\"hash\" value=\"crypt\" checked>" .
        $text{crypt} . "\n";
    print "<INPUT type=\"radio\" name=\"hash\" value=\"nome\">" .
        $text {plaintext} . "\n";
    print "</TD></TR>";
    print "<TR><TD>" . $text{create_home} . "\n";
    print "</TD><TD>";
    print "<INPUT type=\"radio\" name=\"create\" value=\"1\" checked>" .
        $text{yes} . "\n";
    print "<INPUT type=\"radio\" name=\"create\" value=\"0\">" .
        $text{no} . "\n";
    print "</TD></TR>";
    print "<TR><TD>" . $text{copy_files} . "\n";
    print "</TD><TD>";
    print "<INPUT type=\"radio\" name=\"copy\" value=\"1\" checked>" .
        $text {yes} . "\n";
    print "<INPUT type=\"radio\" name=\"copy\" value=\"0\">" .
        $text {no} . "\n";
    print "</TD></TR>";
}

print "<TD colspan=2><HR>(*) " . $text{field_obs};
print "</TD></TR>";
print "</TABLE>\n";
print "</TD></TR>";
print "</TABLE><BR>\n";
# Change label to  Modify or Create
$label = ($new) ? $text{create} : $text{modify};
print "<TABLE width=100% border=0><TR><TD align=\"left\">\n";
print "<INPUT type=\"submit\" name=\"save\" value=\" $label \">\n"; 
# that's bad HTML, but Webmin uses this on standard users module...
print "</FORM></TD>\n";
if (! $new) {
    print "<TD align=\"center\">\n";
    print "<FORM method=\"post\" action=\"set_passwd.cgi\">\n";
    print "<INPUT type=\"hidden\" name=\"dn\" value=\"$dn\">\n";
    print "<INPUT type=\"hidden\" name=\"sort_on\" value=\"$sort_on\">\n";
    print "<INPUT type=\"submit\" value=\" " . $text{set_passwd} . " \">\n";
    print "</FORM></TD>\n";
    print "<TD align=\"right\">\n";
    print "<FORM method=\"post\" action=\"delete_user.cgi\">\n";
    print "<INPUT type=\"hidden\" name=\"dn\" value=\"$dn\">\n";
    print "<INPUT type=\"hidden\" name=\"sort_on\" value=\"$sort_on\">\n";
    print "<INPUT type=\"submit\" value=\" " . $text{delete} . " \">\n";
    print "</FORM></TD>\n";
}
print "</TR></TABLE>\n";

print "<BR>\n";
&footer ("index.cgi?sort_on=$sort_on", "LDAP Users");
do "footer.pl";

