#!/usr/bin/perl
# tta_selusr.cgi
# Select Users to Tarantella Services

require './tarantella-lib.pl';
do "ldap-lib.pl";
&ReadParse();
$postform="ldapusers";
if ($in{form}) { $postform = $in{form}; }
if ( !$in{filter} ) { &redirect("tta_filter.cgi?form=$postform"); }
$filter=$in{filter};
#    $filter = "(cn=*)";

$ldap_server=$in{ldapserver};

$base=$in{base};
if ( $base ) { 
   $base="$base,$config{ldap_dnusers}";
} else {
   $base=$config{ldap_dnusers};
};
$sort_on = ($in{sort_on}) ? $in{sort_on} : "sn";

if ( "$postform" eq "passwd" ) { 
     &header($text{'index_passwd'}, "", $postform );
}
if ( "$postform" eq "passwdcache" ) { 
     &header($text{'index_passwdcache'}, "", $postform );
}
if ( "$postform" eq "delusr" ) { 
     &header($text{'index_delusr'}, "", $postform );
}
if ( "$postform" eq "modusr" ) { 
      &header($text{'index_moddusr'}, "", $postform );
 };
if ( "$postform" eq "winldapusers" ) { 
#     &header($text{'index_winldapusers'}, "", $postform ); 
      &header("", "", $postform, 0, 1, 1);
 };
if ( "$postform" eq "ldapusers" ) { 
      &header ($text{'index_ldapusers'}, "", "$postform");
 } else {
 print "<hr><p>\n";
 print "<SCRIPT SRC=\"global.js\">\n"; 
 print "</SCRIPT>\n"; 
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n"; 
 print "<!--\n"; 
 print "/*  Strings for locales  */\n"; 
 print "var msg0=\"".$text{'msg_0'},"\";\n"; 
 print "var msg1=\"".$text{'msg_1'},"\";\n"; 
 print "var msg2=\"".$text{'msg_2'},"\";\n"; 
 print "var msg3=\"".$text{'msg_3'},"\";\n"; 
 print "var msg4=\"".$text{'msg_4'},"\";\n"; 
 print "var msg5=\"".$text{'msg_5'},"\";\n"; 
 print "var msg6=\"".$text{'msg_6'},"\";\n"; 
 print "var msg7=\"".$text{'msg_7'},"\";\n"; 
 print "var ask_confadmin=true;\n"; 
 print "var ask_confpasscache=true;\n"; 
 print "/******************************************************************\n";
 print "   showValue(formObj)\n";
 print "   Show Password value from form.\n";
 print "*******************************************************************/\n";
 print "function showValue() {\n";
 print "   alert(document.tta_$postform.dn.value);\n";
 print "}\n";
 print "/******************************************************************\n"; 
 print "   set_username(icn,iou,idn)\n"; 
 print "*******************************************************************/\n"; 
 print "function set_username(icn,iou,idn) {\n"; 
 print "    document.tta_$postform.elements[0].value=icn;\n";
 print "    document.tta_$postform.elements[1].value=iou;\n";
 print "    document.tta_$postform.elements[2].value=idn;\n";
 print "    return;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 print "     if ( emptyField(obj.username) == true ) {\n"; 
 print "          alert(obj.username.name+\":\"+msg1+\"->\"+obj.username.value+\"<-\");\n"; 
 print "		    obj.username.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( get_value(\"select\",obj.userou) == \"none\" ) {\n";
 print "          alert(obj.userou.name+\":\"+msg1);\n";
 print "                    obj.userou.focus();\n";
 print "          return false;\n";
 print "     }         \n";
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "<p>\n"; 
}
print "<table width=100% border>\n";
if ( "$postform" eq "delusr") { print "<tr $tb> <td><b>$text{'titidx_delusr'}</b></td> </tr>\n" };
if ( "$postform" eq "modusr") { print "<tr $tb> <td><b>$text{'titidx_modusr0'}</b></td> </tr>\n" };
if ( "$postform" eq "ldapusers") { print "<tr $tb> <td><b>$text{'titidx_ldapusers'}</b></td> </tr>\n" };
if ( "$postform" eq "passwd") { print "<tr $tb> <td><b>$text{'titidx_passwd'}</b></td> </tr>\n" };
if ( "$postform" eq "passwdcache") { print "<tr $tb> <td><b>$text{'titidx_passwdcache'}</b></td> </tr>\n" };
if ( "$postform" eq "winldapusers") { print "<tr $tb> <td><b>$text{'titidx_winldapusers'}</b></td> </tr>\n" };

&connect ();

    
    $entry = $conn->search ($base, "subtree",  "(cn=" . $filter . ")");
    &webmin_log("tta_selusr.cgi", "search", "subtree", "$filter");
 
    $i = 0;
    while ($entry) {
        my (%all_users);
 
        $all_users{dn} = $entry->getDN ();
        $all_users{uid} = $entry->{uid}[0];
        $all_users{sn} = $entry->{sn}[0];
        $all_users{cn} = $entry->{cn}[0];
        $all_users{email} = $entry->{mail}[0];
        $all_users{displayname} = $entry->{displayname}[0];

        if ( "$config{ldap_type}" eq "OpenLDAP" ) {
              $all_users{department} = $entry->{description}[0];
        } else {
              $all_users{department} = $entry->{departmentnumber}[0];
        }
        $all_users[$i++] = \%all_users;
 
        $entry = $conn->nextEntry ();
    }
if ($#all_users < 0) {
    print "<TR><TD colspan=4>" . $text{msg_1} . "\n";
}
else {
    print "<tr><td><B>-- $text{'f_ldapserver'}  ";
    if ( "$ldap_server" eq "1" ) {
         print $config{ldap_servertitle}."  (".$config{ldap_server}.") ";
    } elsif ( "$ldap_server" eq "2" ) {
         print $config{ldap2_servertitle}."  (".$config{ldap2_server}.") ";
    }
    print "<B>-- $text{'l_orderby'}  ";
    if ($sort_on eq "email") {
       @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
       print $text{l_sn} . "\n";
    }
    elsif ($sort_on eq "sn") {
       @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
       print $text{l_uid} . "\n";
    }
    elsif ($sort_on eq "cn") {
       @users = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_users;
       print $text{l_cn} . "\n";
    }
    else {
       @users = sort {($a->{department} . $a->{department}) cmp
       ($b->{l_department} . $b->{cn})} @all_users;
       print $text{l_department} . "\n";
    }
    print "</tr><tr > <td><table width=100%>\n";
    print "<hr>\n";
    print "<tr><td>\n";
    print "</b><TR $tb>\n";
    print "<TD><center><B>".$text{l_uid} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_cn} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_mail} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_department} . "</B></center></TD>\n";

   
    @l_ous=&list_ous();
    $i = 0;
    $i_ou ="";
    foreach $user (@users) {
        $i_dn = $user->{dn};
        $i_cn = $user->{cn};
        foreach $ou (@l_ous) {
            if ( $ou->{'name'} eq $user->{department} ) {
               $i_ou=$ou->{alias};
               last;
            }
        }
        print "<TR $cb>";
        if ( "$postform" ne "ldapusers" ) { 
            print "<TD><A href=\"javascript:set_username('".$i_cn."','".$i_ou."','".$i_dn."')";
            if ( "$postform" eq "winldapusers") {
               print ";setCookie('username','".$i_cn."')";
               print ";setCookie('userou','".$i_ou."')";
            }
            print "\">".$user->{cn}."</A></TD>";
        } else { 
            print "<TD><A href=\"javascript:alert('$i_dn')\">". $user->{cn}."</A></TD>"; 
        }
        print "<TD>" . $user->{sn}."</TD>";
        print "<TD>" . $user->{email}."</TD>";
        if ( "$config{ldap_type}" eq "OpenLDAP" ) {
             print "<TD>&nbsp;" . $user->{description}."</TD>";
        } else {
             print "<TD>&nbsp;" . $user->{department}."</TD>";
        }
        #print "<TD>" . $user->{dn}."</TD>";
        print "\n";
    }
    print "<tr><tr></table></table><hr><p>\n";
    if ( "$postform" ne "ldapusers" ) { 
        if ( "$postform" eq "winldapusers" ) { 
#           print "<center><form METHOD=POST name=tta_".$postform." ACTION=\"\" onSubmit=\"top.username.value=this.username.value;top.close()\">\n"; 
           print "<center><form METHOD=POST name=tta_".$postform." ACTION=\"\" onSubmit=\"top.close()\">\n"; 
        } else {
           print "<center><form METHOD=POST name=tta_".$postform." ACTION=\"tta_".$postform.".cgi\" onSubmit=\"return Chek_fields(this)\">\n"; 
        }
        print "  <td valign=top>",&hlink("<b>$text{'f_username'}</b>","fusername"),"&nbsp;</td>\n";
        print "  <td valign=top><input TYPE=\"text\" NAME=\"username\" VALUE=\"\" cols=\"23\" size=\"12\" ALIGN=left></td></tr>\n";
        print "  <input TYPE=\"hidden\" NAME=\"userou\" VALUE=\"",$userou,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"tta_selusr\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"ldapserver\" VALUE=\"",$ldap_server,"\">\n"; 
        print "  <td valign=top><input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"showValue();\" ></td>\n";
        if ( "$postform" eq "winldapusers" ) { 
           $button_name=$text{'msg_close'};
        } else {
           $button_name=$text{'f_sendreq'};
        }
        print "   <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$button_name,"\" VALUE=\"",$button_name,"\"></td></tr>\n";
        print "	</form></td></tr>\n"; 
    }
}

 print "<tr><tr></table></table><hr><p>\n";
if ( "$postform" ne "winldapusers") {
 &footer($config{'back_return'},$text{'index'}); 
}
 print "<p><p><p>\n";
 do "footer.pl";
 exit;




