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

require './tarantella-lib.pl';
do "ldap-lib.pl";
&ReadParse();
$postform="ldapobjs";
$ldapcache_server=$in{ldapcacheserver};    
if ($in{form}) { $postform = $in{form}; }
if ( $debug gt 0 ) { 
   &header ("IN", "", "");
   print "<p>$in{form} $in{base}, $in{method},  $in{filter}  ";  
   if ( $debug eq 2 ) { foreach $elem (@in) { print "<p>$elem  \n";  } };
}
$debug=2;
$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="subtree"; }
}
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="subtree"; 
     }
  }
} 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" ) { 
     &header($text{'index_delldapcache'}, "", $postform );
}
if ( "$postform" eq "modobj" ) { 
      &header($text{'index_modldapcache'}, "", $postform );
 };
if ( "$postform" eq "winldapobjs" ) { 
#     &header($text{'index_winldapobjs'}, "", $postform ); 
      &header("", "", $postform, 0, 1, 1);
 };
if ( "$postform" eq "ldapobjs" ||  "$postform" eq "tree" ) { 
      &header ($text{'index_ldapobjs'}, "", "$postform");
 } ;
 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 "   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)\n";
 print "*******************************************************************/\n";
 print "function set_values(iobj,idn) {\n";
 print "    document.cache_$postform.elements[0].value=iobj;\n";
 print "    document.cache_$postform.elements[1].value=idn;\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[2].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 "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\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><b>$text{'titidx_delldapcache'}</b></td> </tr>\n" };
if ( "$postform" eq "modobj") { print "<tr $tb> <td><b>$text{'titidx_modldapcache'}</b></td> </tr>\n" };
if ( "$postform" eq "ldapobjs" ||  "$postform" eq "tree" ) { print "<tr $tb> <td><b>$text{'titidx_ldapobjs'}</b></td> </tr>\n" };
if ( "$postform" eq "winldapobjs") { print "<tr $tb> <td><b>$text{'titidx_winldapobjs'}</b></td> </tr>\n" };
    print "<tr><TABLE border width=100% >\n";

if ($debug gt 0 ) { print "<p>$search_dn, $method,  $filter  "; };  
&connect_cache ();

 $entry = $conncache->search ($search_dn, $method, "(" . $filter . ")");  
 &webmin_log("cache_selobj.cgi", "search $search_dn", $method, "$filter");       

    $i = 0;
    while ($entry) {
        my (%all_elemen);
 
        $all_elemen{dn} = $entry->getDN ();
        $all_elemen{objectclass} = $entry->{objectclass}[0];
        if ( $all_elemen{objectclass} eq "ttacacheapp" ) {
           $all_elemen{objtype} = "cn";
           $all_elemen{objcnou} = $entry->{cn}[0];
           $all_elemen{description} = $entry->{tta3tiertitle}[0];
        } else {
           $all_elemen{objtype} = "ou";
           $all_elemen{objcnou} = $entry->{ou}[0];
           $all_elemen{description} = $entry->{description}[0];
        }

        $all_elemen[$i++] = \%all_elemen;
        $entry = $conncache->nextEntry ();
    }
if ($#all_elemen < 0) {
    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 href=\"cache_selobj.cgi?sort_on=objcnou&form=$postform&ldapcacheserver=$ldapcache_server\">";
    }
    print $text{l_objcnou};
    if ( "$postform" eq "tree" ) { print "</A>" };
    print "</B></center></TD>\n";
    print "<TD><center><B>";
    if ( "$postform" eq "tree" ) { 
          print "<B><A href=\"cache_selobj.cgi?sort_on=description&form=$postform&ldapcacheserver=$ldapcache_server\">";
    }
    print $text{l_description};
    if ( "$postform" eq "tree" ) { print "</A>" };
    print "</B></center></TD>\n";
    print "<TD><center><B>";
    if ( "$postform" eq "tree" ) { 
          print "<A href=\"cache_selobj.cgi?sort_on=dn&form=$postform&ldapcacheserver=$ldapcache_server\">"; 
    }
    print $text{l_dn} ;
    if ( "$postform" eq "tree" ) { print "</A>" };
    print "</B></center></TD>\n";
    $i = 0;
    foreach $item (@elemens) {
        print "<TR $cb>";
        if ( "$item->{objtype}" eq "ou") {
       	   $entry = $conncache->search ($item->{dn}, $method, "(" . $filter . ")");  
           if ($entry ) {
               print "<TD><A href=\"/tarantella/cache_selobj.cgi?form=tree&sort_on=$sort_on&base=".&html_escape($item->{dn})."&ldapcacheserver=$ldapcache_server\">".$item->{objcnou}."</A></TD>";
           } else {
               print "<TD>".$item->{objcnou}."</TD>";
           }
        } else {
           print "<TD><A href=\"javascript:set_values('".$item->{objcnou}."','".$item->{dn}."')\">".$item->{objcnou}."</A></TD>";
        }
        print "<TD>" . $item->{description}."</TD>";
        print "<TD>" . $item->{dn}."</TD>";
        print "\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 Chek_fields(this)\">\n"; 
    }
    print "  <td valign=top>",&hlink("<b>$text{'f_objname'}</b>","fobjname"),"&nbsp;</td>\n";
    print "  <td valign=top><input TYPE=\"text\" NAME=\"objcnou\" VALUE=\"\" cols=\"23\" size=\"12\" ALIGN=left></td></tr>\n";
    print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\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>\n";
    if ( "$postform" eq "winldapobjs" ) { 
         print "   <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$button_name,"\" VALUE=\"",$text{'msg_close'},"\"></td></tr>\n";
    } else {
         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";
 exit;
