#!/usr/bin/perl
# stta_checksync.cgi
# Stta Services integrity check
# 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';
if ( $config{'is_ldapcache'} eq "true" ) {
     do "ldap-lib.pl";
}
 &foreign_require("proc", "proc-lib.pl");
 &ReadParse();

 &headerstta($text{index_checksync}, "", "checksync",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );

 print "<table width=100% border>\n";
 print "<tr $tb> <td><font color='$revtext'><b>$text{titidx_checksync}</b></td> </tr>\n";

 $filechecksync=$in{filechecksync} || "";
 $sttaobj=$in{sttaobj} || "";
 $fixobjsync=$in{fixobjsync} || "";

if ( $sttaobj ne ""  ) {
    print "<tr $bgimage> <td><table width=100%>\n";
    print "<tr><td>\n";
    if ( $filechecksync ne "" ) {
         if ( $filechecksync =~ /^\/\// ) {
              $OutDir=$filechecksync;
              $OutDir=~ s/^\///;
         } else {
              $OutDir="$config{tta_backup_dir}$filechecksync";
         }
    }
    %h_objs=();  
    %h_grpobjs=();  
    $del_objlink="";
    $mod_objlink="";

    if ($sttaobj eq "ous" ) {
        %h_objs= &list_ous();
    } elsif ($sttaobj eq "hosts" ) {
        %h_objs= &list_hosts();
        %h_grpobjs= &list_grphosts();
    } elsif ($sttaobj eq "grphosts" ) {
        %h_objs= &list_grphosts();
    } elsif ($sttaobj eq "apps" ) {
        %h_objs= &list_apps();
        %h_grpobjs= &list_grpapps();
    } elsif ($sttaobj eq "grpapps" ) {
        %h_objs= &list_grpapps();
    } elsif ($sttaobj eq "domains" ) {
        %h_objs= &list_domains();
    } elsif ($sttaobj eq "links" ) {
        %h_objs= &list_domains();
    } else {
        print "<tr><td>$text{'list_errno'}- $sttaobj </td></tr>";
        &footHere();
        exit;
    }
    if ( $filechecksync ne "" ) {  open( OUTSYNC, ">$OutDir"); }
    $str_html="<html><body>\n";
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<h1>".$text{"index_".$sttaobj}."</h1>\n";
    print $str_html;
    $str_html="<center><h1>$text{index_checksync} ->".$text{"index_".$sttaobj}."<h1></center>"; 
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="</td></tr><tr><td><table width=100% border=1><tr $tb>\n";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<td><center><b><font color='$revtext'>$text{l_alias}</b></center></font></td>\n";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<td><center><b><font color='$revtext'>$text{l_description}</b></center></font></td>\n";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<td><center><b><font color='$revtext'>$text{l_group}</b></center></font></td>\n";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<td><center><b><font color='$revtext'>$text{tta_obj}</b></center></font></td>\n";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<td><center><b><font color='$revtext'>$text{default_obj}</b></center></font></td>\n";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $i=0;
    $ldapcache_notdie=1;
    @listobjs=();
    @list_objs=();
    foreach $item (keys (%h_objs)) { push (@listobjs, $h_objs{$item}); }
    @list_objs = sort {$a->{'name'} cmp $b->{'name'}} @listobjs;
    foreach $item (@list_objs) {
        $aliasobj=$item->{'alias'};
        print "<tr $cb><td>$aliasobj</td>";
        if ( $filechecksync ne "" ) {  print OUTSYNC "<tr $cb><td>$aliasobj</td>";}
        if  ( $h_objs{$aliasobj}->{'alias'} ne $aliasobj ) {
              print "<td>$text{'list_errno'}</td><td>?</td><td>?</td><td>?</td></tr>";
              if ( $filechecksync ne "" ) {  
                 print OUTSYNC 
                 "<td>$text{'list_errno'}</td><td>?</td><td>?</td><td>?</td></tr>";
              }
              next;
        }
        $dnobj=$h_objs{$aliasobj}->{'obj'} || "";
        $ensobj=$h_objs{$aliasobj}->{'ens'} || "";
        $objname=$h_objs{$aliasobj}->{'name'} || "";
        $grpalias=$h_objs{$aliasobj}->{'grpalias'} || "";
        $descrip=$h_objs{$aliasobj}->{'descrip'} || "";
        $alias=$h_objs{$aliasobj}->{'alias'} || "";
        $maxsess=$h_objs{$aliasobj}->{'maxsess'} || "";
        print "<td>$objname <br> $descrip</td>";
        if ( $filechecksync ne "" ) {  print OUTSYNC "<td>$objname<br>$descrip </td>"; }
        print "<td>$grpalias <br>".$h_grpobjs{$grpalias}->{'name'}." </td>"; 
        if ( $filechecksync ne "" ) {  print OUTSYNC "<td>$grpalias <br>".$h_grpobjs{$grpalias}->{'name'}." </td>"; }
        if ( $ensobj ne "" ) {
             $ens_obj = "\"".$config{tta_base}."/$ensobj\"";
             # loading object attributes ...
             $op="attributes";
             $type_cmd="attributes";
             $debug=0;
             $status_cmd=&run_tta_cmd($type_cmd,$ens_obj);
       } else { $status_cmd=""; }
        if ( !$status_cmd ) { 
             $tta_descrip="<b>$text{'tta_noobject'}</b>";
             system("rm -f $temp_f 2>/dev/null");
             unlink($temp_f);
        } else {
             # Assigning and collecting values to be used in the form ...
             %h_ttaobj=&load_ttaobj();
             $tta_descrip=$h_ttaobj{description}{value};
        }
        $debug=$config{debug_code};
        print "<td>$ensobj <br> $tta_descrip</td>";
        if ( $filechecksync ne "" ) {  print OUTSYNC "<td>$ensobj <br> $tta_descrip</td>"; }
        if ( $config{'is_ldapcache'} eq "true" && $dnobj ne "" ) { 
             $dn_cache="cn=$config{ldapcache_defaultobj},$dnobj,$config{ldapcache_base}";
             $ldapcache_server=$in{ldapcacheserver} || "1";
             &connect_cache();
             # loading variables
             if ( &load_tta3tier_vars($dn_cache) eq "1" ) {
                  $ldapcache_descrip="<b>$text{'ldapcache_norecord'}</b>";
             } else { $ldapcache_descrip=$tta3tier_title; }
             $err_msg="";
             print "<td>$dnobj <br> $ldapcache_descrip</td>";
             if ( $filechecksync ne "" ) {  print OUTSYNC "<td>$dnobj <br> $ldapcache_descrip</td>"; }
        } else {
             print "<td> &nbsp;</td>" ;
             if ( $filechecksync ne "" ) {  print OUTSYNC "<td></td>"; }
        }
        print "</tr>\n";
        if ( $filechecksync ne "" ) { print OUTSYNC "</tr>\n"; }
        $i++;       
#        if ( $fixobjsync eq "true" ) {}
#        if ( $i gt 3 ) { last; }
    }
    $str_html="</table></td></tr>";
    print $str_html;
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    $str_html="<tr><td><hr><b> ".&text('l_total',$i)."</b>&nbsp;&nbsp;&nbsp;&nbsp;   [".localtime(time())."]</b>&nbsp;&nbsp;&nbsp;&nbsp;";
    print "$str_html <b>$OutDir</b></td></tr>\n";
    if ( $filechecksync ne "" ) {  print OUTSYNC "$str_html </td></tr>\n"; }
    $str_html="</body></html>\n";
    if ( $filechecksync ne "" ) {  print OUTSYNC $str_html; }
    if ( $filechecksync ne "" ) {  close( OUTSYNC); }
          &webmin_log("Checking Stta integrity ($sttaobj $i)","stta_checksync","$OutDir ($i $staobj) ",\%h_objs);  
    &footHere();
    exit;
}

 print "<tr $cb> <td><table width=100%>\n";
 print "<tr><td>\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 "\n"; 
 print "/******************************************************************\n"; 
 print "   Check_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Check_fields(obj) {\n"; 
 print "     if ( get_value(\"select\",obj.sttaobj) == \"none\" ) {\n";
 print "          alert(obj.sttaobj.name+\":\"+msg1);\n";
 print "                    obj.sttaobj.focus();\n";
 print "          return false;\n";
 print "     }         \n";
 print "     if ( emptyField(obj.filechecksync) == true ) {\n"; 
 print "          alert(obj.filechecksync.name+\":\"+msg1+\"->\"+obj.filechecksync.value+\"<-\");\n"; 
 print "		    obj.filechecksync.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "\n"; 
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "\n"; 
  print "<form METHOD=POST name=newgrp ACTION=\"stta_checksync.cgi\" onSubmit=\"return Check_fields(this)\">\n"; 

 print "   <tr><td valign=top>",&hlink("<b>$text{'f_obj'}</b>","fobj"),"&nbsp;</td>\n";
 print "   <td valign=top><select NAME=\"sttaobj\">\n";
 print "                <option VALUE=\"none\">".$text{'f_selecobjclass'}."\n";
$nItems=0;
@l_lists = split(/,/,$text{'check_objs_value'}, -1);
$nItems=0;
foreach $item (@l_lists) {
     print "         <option ";
     if ( $item eq $sttaobj ) { print "selected " ; }
     print "VALUE=\"$item\" ";
     print ">".$text{"index_".$item}."</b>\n";
     $nItems++;
}
 print "        </select></td></tr>\n";
#print "<tr><td valign=top>",&hlink("<b>$text{'f_fixobjsync'}</b>","ffixobjsync"),"&nbsp;</td>\n";
#print "<td><input type=checkbox name=\"fixobjsync\" value=\"true\" ></td></tr>\n";

 print "        <tr><td valign=top>",&hlink("<b>$text{'f_filechecksync'}</b>","ffilechecksync"),"</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"filechecksync\" VALUE=\"$filechecksync\" cols=\"30\" size=\"30\" ALIGN=left ", &file_chooser_button("filechecksync",0,0,$config{tta_backup_dir})," ($config{tta_backup_dir})</td></tr>\n";
 print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\">\n";
 print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"reset\" NAME=\"",$text{'f_cancelreq'},"\" VALUE=\"",$text{'f_cancelreq'},"\"></td></tr>\n";
 print "	</form>\n"; 
 print "</table></table><p>\n";
 &footer($config{'back_return'},$text{'index'});
 do "footer.pl";
 exit;
