#!/usr/bin/perl
# cache_selobj.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';
do "ldap-lib.pl";
&ReadParse();
$postform="ldapobjs";
$ldapcache_server=$in{ldapcacheserver} || "1";    
if ($in{form}) { $postform = $in{form}; }
if ( $debug gt 5 ) { 
   &headerstta ("IN", "", "");
   print "<p>$in{form} $in{base}, $in{method},  $in{filter}  ";  
   if ( $debug eq 2 ) { foreach $elem (@in) { print "<p>$elem  \n";  } };
}
$method=$in{method};
if ( !$in{base} ) { &redirect("cache_filter.cgi?form=$postform"); }
if ( $method eq "none" || $method eq "" ) { 
     if ( $postform eq "tree" ) {
          $method="one"; 
     } else { $method="sub"; }
}
if ( $in{filter} ne "" ) { 
  if ( "$in{objclass}" ne "none") { 
        $filter="objectclass=".$in{objclass}; 
  } else {
     if ( $postform eq "tree" ) {
          $filter="objectclass=*"; 
     } else {
        $filter="cn=".$in{filter};  
        $method="sub"; 
     }
  }
} else {
  $filter="objectclass=*"; 
}
if ( $in{base} eq "" ) { 
  $search_dn=$config{ldapcache_base};
} else {
  $search_dn = $in{base};
}
$sort_on = ($in{sort_on}) ? $in{sort_on} : "dn";

if ( "$postform" eq "delobj" ) { 
     &headerstta($text{'index_delldapcache'}, "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,"onLoad='initJs();'","$text{head_below}" ); 
}
if ( "$postform" eq "modobj" ) { 
      &headerstta($text{'index_modldapcache'}, "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,"onLoad='initJs();'","$text{head_below}" ); 
 };
if ( "$postform" eq "winldapobjs" ) { 
#     &headerstta($text{'index_winldapobjs'}, "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
      &headerstta("", "", $postform,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
 };
if ( "$postform" eq "ldapobjs" ||  "$postform" eq "tree" ) { 
      &headerstta ($text{'index_ldapobjs'}, "", "$postform" ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
 } ;
 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 msg8=\"".$text{'msg_8'},"\";\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 "   if (document.cache_$postform.dn.value.length > 0 ) {\n";
 print "        alert(document.cache_$postform.dn.value);\n";
 print "   }\n";
 print "}\n";
 print "/******************************************************************\n";
 print "   set_values(iobj,idn,idesc)\n";
 print "*******************************************************************/\n";
 print "function set_values(iobj,idn,idesc) {\n";
 print "    document.cache_$postform.elements[1].value=iobj;\n";
 print "    document.cache_$postform.elements[2].value=idn;\n";
 print "    document.cache_$postform.elements[3].value=idesc;\n";
 print "    document.cache_$postform.lnro.value=\"-1\";\n";
 print "    return;\n";
 print "}\n";
 print "/******************************************************************\n";
 print "   set_cmd(cmd)\n";
 print "*******************************************************************/\n";
 print "function set_cmd(cmd) {\n";
 print "    document.cache_$postform.elements[4].value=cmd;\n";
 print "    if (cmd ==  \"delete\" ) {;\n";
 print "       document.cache_$postform.action=\"cache_delobj.cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"modify\" ) {;\n";
 print "       document.cache_$postform.action=\"cache_modobj.cgi\";\n";
 print "    }\n";
 print "    return;\n";
 print "}\n";
 print "//-->\n"; 
 print "/******************************************************************\n"; 
 print "   Check_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Check_fields(obj) {\n"; 
 print "     var lnro=document.cache_$postform.lnro.value;\n";
 print "     if ( lnro > -1  ) {\n";
 print "          if ( document.cache_$postform.action == \"cache_modobj.cgi\") {  \n";
 print "               alert(\"$text{l_line} \"+document.cache_$postform.lnro.value+\":\"+msg8);\n"; 
 print "               setObjBoxes(obj,-1,\"\",\"\")\n";; 
 print "               return false;\n"; 
 print "          } \n"; 
 print "     }\n";
 print "     if ( emptyField(obj.objcnou) == true ) {\n"; 
 print "          alert(obj.objcnou.name+\":\"+msg1+\"->\"+obj.objcnou.value+\"<-\");\n"; 
 print "		    obj.objcnou.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( emptyField(obj.dn) == true ) {\n"; 
 print "          alert(obj.objcnou.name+\":\"+msg1);\n"; 
 print "		    obj.objcnou.value=\"\";\n"; 
 print "		    obj.objcnou.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
print "<table width=100% border>\n";
if ( "$postform" eq "delobj") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_delldapcache'}</b></font></td> </tr>\n" };
if ( "$postform" eq "modobj") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_modldapcache'}</b></font></td> </tr>\n" };
if ( "$postform" eq "ldapobjs" ||  "$postform" eq "tree" ) { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_ldapobjs'}</b></font></td> </tr>\n" };
if ( "$postform" eq "winldapobjs") { print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_winldapobjs'}</b></font></td> </tr>\n" };
    print "<tr><TABLE border width=100% >\n";

if ($debug gt 0 ) { print "<p>$search_dn, $method,  $filter  "; };  
sub callbackSearch 
{
   my ( $conncache, $entry) = @_;   
   my (%entryAttrs);

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

   $dn = $entry->dn;
 
   @attrs = $entry->attributes; # Obtain attributes for this entry.
   my (%all_elemen);
   $all_elemen{dn} = $dn;
   foreach my $var (@attrs) {
        $attr = $entry->get_value( $var, asref => 1 );
        if ( defined($attr) ) {          
             foreach my $value (@$attr) { $entryAttrs{$var} = $value; } 
        }                                                                       
   }
   $all_elemen{objectclass} = $entryAttrs{objectclass};
   if ( $entryAttrs{objectclass} eq "ttacacheapp" ||  $entryAttrs{objectClass} eq "ttacacheapp" ) {
        $all_elemen{objtype} = "cn";
        $all_elemen{objcnou} = $entryAttrs{cn};
        $all_elemen{description} = $entryAttrs{tta3tiertitle};
   } else {
        $all_elemen{objtype} = "ou";
        $all_elemen{objcnou} = $entryAttrs{ou};
        $all_elemen{description} = $entryAttrs{description};
   }
   $all_elemen[$i++] = \%all_elemen;
   $conncache->pop_entry; 
}

&connect_cache();
    $i=0;
    $ious=0;
    $conncache = $ldapcacheObj->search ( base => $search_dn,
                                          scope => "$method",
                                          filter => "(" . $filter . ")",
                                          callback => \&callbackSearch
                                         );
    $search_cmd="dn=>'$search_dn'||method=>'$method'||filter=>'$filter'";
    &ldapcache_logerr("search $search_dn");  
    
if ( $conncache->code ) {
    if ($conncache->count == 0) {  # if conncache is not defined we have nothing.
        print "<TR><TD colspan=4>" . $text{msg_1} . "</TD></TR>\n"; } 
}
else {
    print "<tr><td><B>-- $text{'f_ldapserver'}  ";
    if ( "$ldapcache_server" eq "1" ) {
         print $config{ldapcache_servertitle}."  (".$config{ldapcache_server}.") ";
    } elsif ( "$ldapcache_server" eq "2" ) {
         print $config{ldapcache2_servertitle}."  (".$config{ldapcache2_server}.") ";
    }
    print " -- $text{'l_orderby'}<B>  ";
    if ($sort_on eq "dn") {
       @elemens = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_elemen;
       print $text{l_dn} . "\n";
    }
    elsif ($sort_on eq "objcnou") {
       @elemens = sort {$a->{objcnou} cmp $b->{objcnou}} @all_elemen;
       print $text{l_objcnou} . "\n";
    }
    else {
       @elemens = sort {($a->{description} . $a->{objcnou}) cmp
       ($b->{description} . $b->{objcnou})} @all_elemen;
       print $text{l_description} . "\n";
    }
    print "</td></tr><tr> <td><table width=100%>\n";
    print "<TR $tb><TD><center><B>";
    if ( "$postform" eq "tree" ) { 
          print "<A class=\"menu1\" href=\"cache_selobj.cgi?sort_on=objcnou&form=$postform&ldapcacheserver=$ldapcache_server\">";
    }
    print "<font color='$revtext'> $text{l_objcnou}";
    if ( "$postform" eq "tree" ) { print "</A>" };
    print "</B></center></font></TD>\n";
    print "<TD><center><B>";
    if ( "$postform" eq "tree" ) { 
          print "<B><A class=\"menu1\" href=\"cache_selobj.cgi?sort_on=description&form=$postform&ldapcacheserver=$ldapcache_server\">";
    }
    print "<font color='$revtext'> $text{l_description}";
    if ( "$postform" eq "tree" ) { print "</A>" };
    print "</B></center></font></TD>\n";
    print "<TD><font color='$revtext'><center><B>";
    if ( "$postform" eq "tree" ) { 
          print "<A class=\"menu1\" href=\"cache_selobj.cgi?sort_on=dn&form=$postform&ldapcacheserver=$ldapcache_server\">"; 
    }
    print "<font color='$revtext'> $text{l_dn}";
    print "<center><form METHOD=POST name=\"cacheObj\" ACTION=\"\" >\n"; 
    if ( "$postform" eq "tree" ) { print "</A>" };
    print "</B></center></font></TD>\n";
    $i = 0;
    foreach $item (@elemens) {
        print "<TR $cb>";
        if ( "$item->{objtype}" eq "ou") {
           $conncache = $ldapcacheObj->search ( base => $item->{dn},
                                         scope => "one",
                                         filter => "(" . $filter . ")",
                                         );
           print "<TD><input type=\"checkbox\" name=\"objCheckBox$i\" value=\"no\" ";
           print "     onClick=\"setObjBoxes(this,$i,'".$item->{objcnou}."','".$item->{dn}."','".$item->{description}."')\">\n";
           if ($conncache->code) {
               print $item->{objcnou}."</TD>";
           } else {
               print "<A class=\"menu1\" href=\"cache_selobj.cgi?form=tree&sort_on=$sort_on&base=".&html_escape($item->{dn})."&ldapcacheserver=$ldapcache_server\">".$item->{objcnou}."</A></TD>";
           }
           $ious++;
        } else {
           print "<TD><A class=\"menu1\" href=\"javascript:set_values('".$item->{objcnou}."','".$item->{dn}."','".$item->{description}."')\">".$item->{objcnou}."</A></TD>";
        }
        print "<TD>" . $item->{description}."</TD>";
        print "<TD>" . $item->{dn}."</TD>";
        print "\n";
        $i++;
    }
    print "</form>\n";
    print "<tr></table></table><hr><p>\n";
    if ( "$postform" eq "winldapobjs" ) { 
           print "<center><form METHOD=POST name=cache_".$postform." ACTION=\"index.cgi\" onSubmit=\"top.close()\">\n"; 
    } else {
           print "<center><form METHOD=POST name=cache_".$postform." ACTION=\"index.cgi\" onSubmit=\"return Check_fields(this)\">\n"; 
    }
    print "  <tr><td valign=top>$text{f_checkboxdelobj} [$text{'delete'}] <input TYPE=\"hidden\" NAME=\"lnro\" size=2 VALUE=\"\">\n";
    print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
    print "   ",&hlink("<b>$text{'f_objname'}</b>","fobjname"),"&nbsp;</td>\n";
    print "  <td><input TYPE=\"text\" NAME=\"objcnou\" VALUE=\"\" cols=\"23\" size=\"30\" ></td>\n";
    print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"desc\" VALUE=\"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"cmd\" VALUE=\"",$cmd,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"base\" VALUE=\"",$search_dn,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"filter\" VALUE=\"",$filter,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"cache_selobj\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"ldapserver\" VALUE=\"",$ldapcache_server,"\">\n";
    print "  <td valign=top><input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"showValue();\" ></td></tr>\n";
    if ( "$postform" eq "winldapobjs" ) { 
         print "   <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$button_name,"\" VALUE=\"",$text{'msg_close'},"\"></td></tr>\n";
    } elsif ( $access{passwd} eq 1 && $config{'is_masterHost'} eq "true" ) {
         print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'modify'},"\" VALUE=\"",$text{'modify'},"\" onClick=\"set_cmd('modify')\">\n";
         print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
         print "        <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'delete'},"\" VALUE=\"",$text{'delete'},"\" onClick=\"set_cmd('delete')\"></td></tr>\n"
    }
    print "	</form></td></tr>\n"; 
}

 print "</tr></table></table></table><hr><p>\n";
if ( "$postform" ne "winldapobjs") {
 &footer($config{'back_return'},$text{'index'}); 
}
 print "<p> -- <p><p>\n";
 do "footer.pl";
#if there is only one jump to it !!!
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
 print "/******************************************************************\n"; 
 print "   setObjBoxes(obj,lnro,objcnou,dn,desc)\n"; 
 print "*******************************************************************/\n"; 
 print "function setObjBoxes(obj,lnro,objcnou,dn,desc) {\n"; 
 print "     nroBoxes=$i; \n";
 if ( $ious gt 1 ) {
      for ( $l = 0; $l < $ious; $l++) {
        print "   if ( lnro != $l )  { document.cacheObj.objCheckBox$l.checked=false; }\n";
      }
 }
 print "   set_values(objcnou,dn,desc);\n";
 print "   document.cache_$postform.lnro.value=lnro;\n";
 print "}\n";
 print "/******************************************************************\n";
 print "   initJs()\n";
 print "*******************************************************************/\n";
 print "function initJs() {\n";
 if ( $i == 1 ) {
      if ( "$postform" eq "delobj" || "$postform" eq "modobj" ) {
            print "    set_values('".$item_objcnou."','".$item_dn."');\n";
            if ( "$postform" eq "delobj" ) { print "    set_cmd('delete');\n"; }
            if ( "$postform" eq "modobj" ) { print "    set_cmd('modify');\n"; }
            print "    document.cache_$postform.submit();\n";
      }
 }
 print "}\n";
 print "</SCRIPT>\n";
 exit;
