#!/usr/bin/perl
# tta_selusr.cgi
# Select Users to Services with Tarantella
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require './stta-lib.pl';
$has_ldapusers="";  
if ( $ldap_ok ne true && $config{has_ldapusers} eq "true" ){ 
     $has_ldapusers=$config{'has_ldapusers'}; 
     $config{'has_ldapusers'}="false"; 
}
if ( $config{'has_ldapusers'} eq "true" ) {
     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};
$sysuser=$in{sysuser};
$syncuser=$in{syncuser};

$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" ) { 
     &headerstta($text{'index_passwd'}, "", $postform,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","onLoad='initJs();'" );
}
if ( "$postform" eq "passwdcache" ) { 
     &headerstta($text{'index_passwdcache'}, "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","onLoad='initJs();'" );
}
if ( "$postform" eq "delusr" ) { 
     &headerstta($text{'index_delusr'}, "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","onLoad='initJs();'" );
}
if ( "$postform" eq "modusr" ) { 
      &headerstta($text{'index_moddusr'}, "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","onLoad='initJs();'" );
 };
if ( "$postform" eq "winldapusers" ) { 
#     &headerstta($text{'index_winldapusers'}, "", $postform,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","" );
      &headerstta("", "", $postform, 0, 1, 1);
 };
if ( "$postform" eq "ldapusers" ) { 
      &headerstta ($text{'index_users'}, "", $postform,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}","","onLoad='initJs();'" );
 } 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,sysusr)\n"; 
 print "*******************************************************************/\n"; 
 print "function set_username(icn,iou,idn,sysusr) {\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 "    document.tta_$postform.elements[3].value=sysusr;\n";
 print "    return;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "/******************************************************************\n"; 
 print "   Check_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Check_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><font color='$revtext'><b>$text{'titidx_delusr'}</b></font></td> </tr>\n" };
if ( "$postform" eq "modusr") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_modusr0'}</b></font></td> </tr>\n" };
if ( "$postform" eq "ldapusers") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_ldapusers'}</font></b></td> </tr>\n" };
if ( "$postform" eq "passwd") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_passwd'}</b></font></td> </tr>\n" };
if ( "$postform" eq "passwdcache") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_passwdcache'}</b></font></td> </tr>\n" };
if ( "$postform" eq "winldapusers") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_winldapusers'}</b></font></td> </tr>\n" };

print "<tr><td>";
$i=0;
@all_users=();

sub callbackSearch 
{
   my ( $conn, $entry) = @_;   

   if ( !defined($entry) ) { return };

   $dn = $entry->dn;
 
   @attrs = $entry->attributes; # Obtain attributes for this entry.
   my (%all_users);
   $all_users{dn} = $dn;
   foreach my $var (@attrs) {
        $attr = $entry->get_value( $var, asref => 1 );
        if ( defined($attr) ) {          
             foreach my $value (@$attr) { $all_users{$var} = $value; } 
        }                                                                       
   }
   $dn_ou=$dn;
   $dn_ou=~s/,$config{ldap_base}//g;
   $i_cn = $all_users{cn};
#   @line=split(/,/, $dn_ou, -1);
#   $dn_ou=~s/$line[0],//g;
   $dn_ou=~s/cn=$i_cn,//g;
   if ( $h_ou{$dn_ou}->{'obj'} eq $dn_ou ) {
          $all_users{'grpalias'}=$h_ou{$dn_ou}->{'alias'};
          $all_users{'ens'}=$h_ou{$dn_ou}->{'ens'}."/cn=".$i_cn;
   } else {
          $all_users{'grpalias'}=$dn_ou;
   }
   $all_users[$i++] = \%all_users;
   $conn->pop_entry; 
}
if ( $config{'has_ldapusers'} eq "true" && $ldap_server ne "0" ) {
    &connect ();
    %h_ou=&list_ous(2);
    my $status_con = $ldapObj->search ( base => $base,
                                        scope => "sub",
                                        filter => "(cn=" . $filter . ")",
                                        callback => \&callbackSearch
                                       );
    if ( $conn->code ) {
       if ($conn->count == 0) {  # if conncache is not defined we have nothing.
           print $text{msg_1} . "\n";   }
    } else {
       print "<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 " -- [ $in{base} - $in{filter} ] ";
    }
}
if ( $config{'has_systemusers'} eq "true" && $sysuser eq "true" ) {
   &foreign_require("useradmin", "user-lib.pl");
   @ulist=&foreign_call("useradmin", "list_users");
   %h_ulist=();
   foreach $item (@ulist) { $h_ulist{$item->{user}} = $item  }
   %h_sysusers=&list_sysusers();
   foreach $item (keys (%h_sysusers)) {
        if ( $filter eq "*" ||  $h_sysusers{$item}->{name} =~ /$filter/ ) {
             $all_users[$i]{dn} = "-";
             $all_users[$i]{cn} = $h_sysusers{$item}->{name};
             $all_users[$i]{sn} = $h_ulist{$item}{real};
             $all_users[$i]{grpalias} = $h_sysusers{$item}->{grpalias};
             $all_users[$i]{ens} = $h_sysusers{$item}->{ens};
             $all_users[$i]{mail} = $h_sysusers{$item}->{email};
             $all_users[$i]{description} = "uid=".$h_ulist{$item}{uid}." ".$h_sysusers{$item}->{where};
             $i++;
       }
   }
}
if ($sort_on ne "browse") { print " <B>-- $text{'l_orderby'}  "; }
if ($sort_on eq "browse") {
       @users = sort {$a->{ens} cmp $b->{ens}} @all_users;
       print $text{browse} . "\n";
    }
elsif ($sort_on eq "mail") {
       @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->{description} . $a->{cn}) cmp
       ($b->{description} . $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><font color='$revtext'><center><B>".$text{l_uid} . "</B></center></font></TD>\n";
print "<TD><font color='$revtext'><center><B>".$text{l_cn} . "</B></center></font></TD>\n";
print "<TD><font color='$revtext'><center><B>".$text{l_mail} . "</B></center></font></TD>\n";
print "<TD><font color='$revtext'><center><B>".$text{l_department} . "</B></center></font></TD>\n";
$i = 0;
$i_ou ="";
$lastgrp="";
%h_ou=&list_ous();
foreach $user (@users) {
     $i_dn = $user->{dn};
     $i_cn = $user->{cn};
     $i_ou = $user->{grpalias};
     $i_sysuser= "";
     if ( $i_dn eq "-" ) {
             $i_sysuser=$i_cn;
     }
     if ($sort_on eq "browse") {
        if ( $i_ou ne $lastgrp ) {
            @line=split(/\//, $user->{ens}, -1);
            $nTabs=$#line;
            print "<TR $tb><td>";
            $lastgrp=$i_ou;
            for ( $i = 0 ; $i < $nTabs ; $i++ ) {
                if ( $i gt 0 ) {
                     print "<img align=middle border=0 alt='spacetab' src='images/omicons/spacetab.gif'>";
                }
                print "<img align=middle border=0 alt='vertline' src='images/omicons/vertline.gif'>";
            }
            if ( $nTabs gt 0 ) {
                 print "<img align=middle border=0 alt='spacetab' src='images/omicons/spacetab.gif'>";
                 print "<img align=middle border=0 alt='tvertline' src='images/omicons/tvertline.gif'>";
            }
            print " <img align=middle border=0 alt='minus' src='images/omicons/minus.gif'> ";
            print "<A class=\"menu1\" href=\"javascript:alert('".$h_ou{$i_ou}->{ens}."')\">"; 
            print " <img align=middle border=0 alt='ou' src='images/omicons/ou.gif'> ";
            print "</a>\n";
            print "<font color='$revtext'><b>".$h_ou{$i_ou}->{name}."</b></font></td>\n";
            print "<td></td>";
            print "<td></td>";
            print "<td></td></tr>\n";
        } 
        print "<tr $cb ><td>";
        for ( $i = 0 ; $i < $nTabs+1 ; $i++ ) {
            if ( $i gt 0 ) {
                print "<img align=middle border=0 alt='spacetab' src='images/omicons/spacetab.gif'>";
            }
            print "<img align=middle border=0 alt='vertline' src='images/omicons/vertline.gif'>";
        }
        print "<img align=middle border=0 alt='spacetab' src='images/omicons/spacetab.gif'>";
        print "<img align=middle border=0 alt='spacetab' src='images/omicons/spacetab.gif'>";
        print "<img align=middle border=0 alt='tvertline' src='images/omicons/tvertline.gif'>";
        print "<img align=middle border=0 alt='horizline' src='images/omicons/horizline.gif'>";
        print "<A class=\"menu1\" href=\"javascript:alert('".$user->{'ens'}."')\">"; 
        print "<img align=middle border=0 alt='ou' src='images/omicons/person.gif'>";
     } else {
       print "<TR $cb><td>";
     }
     if ( "$postform" ne "ldapusers" ) { 
         print "<A class=\"menu1\" href=\"javascript:set_username('".$i_cn."','".$i_ou."','".$i_dn."','".$i_sysuser."')";
         if ( "$postform" eq "winldapusers") {
               print ";setCookie('username','".$i_cn."')";
               print ";setCookie('userou','".$i_ou."')";
               print ";setCookie('dn','".$i_dn."')";
         }
         print "\">".$user->{cn}."</A></TD>";
     } else { 
            print "<A class=\"menu1\" href=\"javascript:alert('$i_ou: $i_dn')\">". $user->{cn}."</A></TD>"; 
        }
        print "<TD>" . $user->{sn}."</TD>";
        print "<TD>" . $user->{mail}."</TD>";
        print "<TD>&nbsp;" . $user->{description}."</TD>";
        #print "<TD>" . $user->{dn}."</TD>";
        print "\n";
        $i++;
     }
     print "<tr><tr></table></table>\n";
     print "<p><tr><td><b> ".&text('l_total',$i)."</b></td><td><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 Check_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\"></td>\n";
        print "  <input TYPE=\"hidden\" NAME=\"userou\" VALUE=\"",$userou,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"sysuser\" VALUE=\"",$sysuser,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"tta_selusr\">\n";
        print " <input TYPE=\"hidden\" NAME=\"ldapserver\" VALUE=\"",$ldap_server,"\">\n";
        print " <input TYPE=\"hidden\" NAME=\"base\" VALUE=\"",$in{base},"\">\n";
        print " <input TYPE=\"hidden\" NAME=\"filter\" VALUE=\"",$in{filter},"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"syncuser\" VALUE=\"",$syncuser,"\">\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"; 
     }
if ($sort_on eq "browse") { print "<tr $cb><td>$text{choose_msg_objtosel}</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";
 if ( $has_ldapusers ne "" ) { $config{'has_ldapusers'}= $has_ldapusers } 
 do "footer.pl";
 #if there is only one jump to it !!!
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n"; 
 print "/******************************************************************\n"; 
 print "   initJs()\n"; 
 print "*******************************************************************/\n"; 
 print "function initJs() {\n"; 
 if ( "$postform" ne "ldapusers" && $i == 1 ) {
       print "    set_username('".$i_cn."','".$i_ou."','".$i_dn."','".$i_sysuser."');\n";
       print "    document.tta_$postform.submit();\n";
 } 
 print "}\n"; 
 print "</SCRIPT>\n"; 
 exit;




