#!/usr/bin/perl
# mon_actarget.cgi
# filter Monitor to Services with Tarantella
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

require './stta-lib.pl';
if ( ! $access{'monitor'} )   { &redirect("index.cgi"); }
if ( $config{'has_monitor'} ne "true" )  { &redirect("index.cgi"); }
&ReadParse();
if ( !$in{monTarget} ) { &redirect("mon_actdef.cgi"); }
if ( !$in{monCondition} ) { &redirect("mon_actdef.cgi"); }
if ( !$in{monAction} ) { &redirect("mon_actdef.cgi"); }
do "ldap-lib.pl"; 
&headerstta($text{'index_monitor_act'}, "", "monitor_act" ,undef,1,1,$text{headpage_rigth} );
if ( $in{postform} ) {
     $aliasAct=$in{aliasAct};
     $descrip=$in{descrip};
     $postform=$in{postform};
     # To avoid ovewriting ReadParse variables from request some are as $req_mon*
     %h_actions=&list_actions();
     if  ( $h_actions{$aliasAct}->{'alias'} eq $aliasAct ) {
           $monAlias=$h_actions{$aliasAct}->{alias};
           $monName=$h_actions{$aliasAct}->{name};
           $monObject=$h_actions{$aliasAct}->{object};
           $req_monTarget=$h_actions{$aliasAct}->{target};
           $monObject=$h_actions{$aliasAct}->{object};
           $req_monCondition=$h_actions{$aliasAct}->{condition};
           $req_monConditionValue=$h_actions{$aliasAct}->{conditionValue};
           $req_monAction=$h_actions{$aliasAct}->{action};
           $req_monScript=$h_actions{$aliasAct}->{script};
           $req_monHow=$h_actions{$aliasAct}->{how};
           $monWhen=$h_actions{$aliasAct}->{when};
     }
}
$monTarget=$in{monTarget};
$monCondition=$in{monCondition};
$monAction=$in{monAction};
$monConditionValue=$in{monConditionValue};
$monScript=$in{monScript};
$monHow=$in{monHow};
 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"; 
 if ( $postform eq "" ) { &htmlCheckAlias("actions"); }
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 if ( $postform eq "" ) {
      print "     if ( checkAlias_actions(obj.aliasAct) == true ) {\n";
      print "          return false;\n";
      print "     }         \n";
      print "     if ( emptyField(obj.aliasAct) == true ) {\n";
      print "               alert(\"error obj.aliasAct.name\"+\":\"+msg1);\n";
      print "               return false;\n";
      print "     }  \n";
 }
 if ( $monTarget eq "user" ) {
    print "     if ( emptyField(obj.username) == true ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "ou" ) {
    print "     if ( get_value(\"select\",obj.user_ou) == \"none\" ) {\n";
    print "               alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "host" ) {
    print "     if ( get_value(\"select\",obj.hostname) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "grphosts" ) {
    print "     if ( get_value(\"select\",obj.usergrphosts) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "app" ) {
    print "     if ( get_value(\"select\",obj.appname) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif  ( $monTarget eq "grpapps" ) {
    print "     if ( get_value(\"select\",obj.grpappsname) == \"none\" ) {\n";
    print "               alert(\"error $monTarget\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 }
 if ( $monHow eq "auto") {
    print "     if ( get_value(\"select\",obj.monWhen) == \"none\" ) {\n";
    print "               alert(\"error $monWhen\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } elsif ( $monHow eq "task" ) {
    print "     if ( emptyField(obj.monWhen) == true ) {\n";
    print "               alert(\"error $monWhen\"+\":\"+msg1);\n";
    print "               return false;\n";
    print "     }  \n";
 } 
 print "     return true;\n";
 print "}\n"; 
 print "//-->\n"; 
 if ( $monTarget eq "user" ) {
	 print "/******************************************************************\n";
 	print "   testCookie(Obj,formObj)\n";
 	print "   testCookie from form.\n";
 	print "*******************************************************************/\n";
 	print "function testCookie(obj,formObj) {\n";
 	print "   if ( emptyField(obj) == true ) {\n";
 	print "         val=getCookie(formObj.username.name);\n";
 	print "         if ( val != formObj.username.value ) { \n";
 	print "               obj.value=\"\"; \n";
 	print "         } else { \n";
 	print "               CheckCookie(obj);\n";
 	print "         }\n";
 	print "    } \n";
 	print "}\n";
 }
 print "</SCRIPT>\n"; 
 print "<p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td>";   
 if ( $postform eq "modify" ) {
      print "<i>$text{'titidx_monitor_modact'}:</i>";
 } elsif ( $postform eq "delete" ) {
      print "<i>$text{'titidx_monitor_delact'}:</i>";
 } else {
      print "<i>$text{'titidx_monitor_defact'}</i> ";
 }
 print "<b> $monAlias - $monName </b> ";
 print "<tr $cb> <td><table width=100%>\n";
 print "<form METHOD=POST name=mon_act ";
 if ( $postform eq "modify" ) {
      print "ACTION=\"modact.cgi\"";
 } elsif ( $postform eq "delete" ) {
      print "ACTION=\"delact.cgi\"";
 } else {
      print "ACTION=\"addact.cgi\"";
 }
 print " onSubmit=\"return Chek_fields(this)\">\n"; 
 if ( $postform ne "" ) {
    print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"$postform\">\n";
 }
 print "<input TYPE=\"hidden\" NAME=\"monTarget\" VALUE=\"$monTarget\">\n";
 print "<input TYPE=\"hidden\" NAME=\"monCondition\" VALUE=\"$monCondition\">\n";
 print "<input TYPE=\"hidden\" NAME=\"monConditionValue\" VALUE=\"$monConditionValue\">\n";
 print "<input TYPE=\"hidden\" NAME=\"monAction\" VALUE=\"$monAction\">\n";
 print "<input TYPE=\"hidden\" NAME=\"monScript\" VALUE=\"$monScript\">\n";
 print "<input TYPE=\"hidden\" NAME=\"monHow\" VALUE=\"$monHow\">\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_aliasact'}</b>","faliasact"),"&nbsp;</td>\n";
 print "         <td valign=top>";
 if ( $postform ne "" ) {
      print "<b> $monAlias </b>";
      print "<input TYPE=\"hidden\" NAME=\"aliasAct\" VALUE=\"$monAlias\">\n";
 } else {
      print " <input TYPE=\"text\" NAME=\"aliasAct\" VALUE=\"$monAlias\" cols=\"12\" size=\"12\" ALIGN=left onChange=\"checkAlias_actions(this)\"></td></tr>\n"; 
 }
 print " </td></tr>\n"; 
print "        <tr><td valign=top>",&hlink("<b>$text{'f_descrip'}</b>","fdescrip"),"</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"descrip\" VALUE=\"$monName\" cols=\"40\" size=\"40\" ALIGN=left></td></tr>\n";

print "   <tr><td valign=top>",&hlink("<b>$text{'f_mon_target'}</b>","fmon_target"),"&nbsp;\n";

if ( $monTarget eq "user" ) {
    print "        ",&hlink("<b>$text{'f_username'}</b>","fusername"),"&nbsp;</td>";
    print "        <td valign=top>\n";
    $msgObj=$text{'selusr_win'};
    $formObj="mon_act.username";
    &htmlWinLdapUsers($msgObj,$formObj);
    print "        <input TYPE=\"text\" NAME=\"username\" VALUE=\"$username\" cols=\"22\" size=\"12\" ALIGN=left onFocus=\"CheckCookie(this);\" onChange=\"mon_act.userou.value=''\" >\n";
    print "        <input TYPE=\"text\" NAME=\"userou\" VALUE=\"$userou\" cols=\"22\" size=\"22\" ALIGN=left onFocus=\"testCookie(this,mon_act)\"></td></tr>\n";

} elsif  ( $monTarget eq "ou" ) {
    print "   ",&hlink("<b>$text{'f_userou'}</b>","fuserou"),"&nbsp;</td>\n";
    print "      <td valign=top><select NAME=\"user_ou\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecou'}</b>","fselecou"),"\n";
    $nItems=0;
    %h_ous=&list_ous();
    foreach $item (keys (%h_ous)) {
            print "         <option ";
            if ( $postform ne "" && $h_ous{$item}->{'alias'} eq $monObject )  {
                 print "selected " ; }
            print " <option VALUE=\"".$h_ous{$item}->{'alias'}."\">". $h_ous{$item}->{'name'}."\n";
            $nItems++;
    }
    print "        </select></td></tr>\n";
} elsif  ( $monTarget eq "host" ) {
    print "   ",&hlink("<b>$text{'f_hostname'}</b>","fhostname"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"hostname\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selechosts'}</b>","fselechosts"),"\n";
    $nItems=0;
    %h_hosts=&list_hosts();
    foreach $item (keys (%h_hosts)) {
            print "         <option ";
            if ( $postform ne "" && $h_hosts{$item}->{'alias'} eq $monObject )  {
                 print "selected " ; }
            print " <option VALUE=\"".$h_hosts{$item}->{'alias'}."\">". $h_hosts{$item}->{'name'}."\n";
            $nItems++;
    }
    print "   </select></td></tr>\n";
} elsif  ( $monTarget eq "grphosts" ) {
    print "   ",&hlink("<b>$text{'f_usergrphosts'}</b>","fusergrphosts"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"usergrphosts\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrphosts'}</b>","fselecgrphosts"),"\n";
    $nItems=0;
    %h_grphosts=&list_grphosts();
    foreach $item (keys (%h_grphosts)) {
            print "         <option ";
            if ( $postform ne "" && $h_grphosts{$item}->{'alias'} eq $monObject )  {
                 print "selected " ; }
            print " <option VALUE=\"".$h_grphosts{$item}->{'alias'}."\">". $h_grphosts{$item}->{'name'}."\n";
            $nItems++;
    }
    print "        </select></td></tr>\n";
} elsif  ( $monTarget eq "app" ) {
    print "   ",&hlink("<b>$text{'f_appobjname'}</b>","fappobjname"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"appname\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecapp'}</b>","fselecapps"),"\n";
    $nItems=0;
    %h_apps=&list_apps();
    foreach $item (keys (%h_apps)) {
            print "         <option ";
            if ( $postform ne "" && $h_apps{$item}->{'alias'} eq $monObject )  {
                 print "selected " ; }
            print " <option VALUE=\"".$h_apps{$item}->{'alias'}."\">". $h_apps{$item}->{'name'}."\n";
            $nItems++;
    }
    print "        </select></td></tr>\n";
} elsif  ( $monTarget eq "grpapps" ) {
    print "   ",&hlink("<b>$text{'f_grpappsobjname'}</b>","fgrpappsobjname"),"&nbsp;</td>\n";
    print "   <td valign=top><select NAME=\"grpappsname\">\n";
    print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrpapps'}</b>","fselecgrpapps"),"\n";
    %h_grpapps=&list_grpapps();
    foreach $item (keys (%h_grpapps)) {
            print "         <option ";
            if ( $postform ne "" && $h_grpapps{$item}->{'alias'} eq $monObject )  {
                 print "selected " ; }
            print " <option VALUE=\"".$h_grpapps{$item}->{'alias'}."\">". $h_grpapps{$item}->{'name'}."\n";
            $nItems++;
    }
    print "        </select></td></tr>\n";
}

&get_acttitles();

print "  <tr><td valign=top $cb><b> $t_monTarget </b>-><b> $t_monCondition $monConditionValue </td>\n";
print "  <td valign=top $cb>=> <b>$t_monAction</b></td></tr>\n";
print "  <tr><td valign=top $cb><i>[ $t_monScript ]</i></td></tr>\n";
print "  <tr><td valign=top>&nbsp;&nbsp</td> <td valign=top>&nbsp;&nbsp</td></tr>\n";
print "  <tr><td valign=top $cb><b>",&hlink("<b>$t_monHow</b>","fmon_When"),"</i></td>\n";
if ( $monHow eq "auto" ) {
     print " <td valign=top><select NAME=\"monWhen\">\n";
     print "        <option VALUE=\"none\">",&hlink("<b>$text{'f_mon_when_sel'}</b>","fmon_when_sel"),"\n";
     @l_when = split(/,/,$text{'mon_when_value'}, -1);
     @l_when_tit = split(/,/,$text{'mon_when_desc'}, -1);
     $nItems=0;
     foreach $item (@l_when) {
          print "         <option ";
          if ( $postform ne "" && $item eq $monWhen ) { print "selected " ; }
          print "VALUE=\"$item\" ";
          print ">$l_when_tit[$nItems]</b>\n";
          $nItems++;
     }
     print "  </select></td></tr>\n";
}
print "</table><hr></tr>\n";
if ( $monHow eq "task" ) {
     if ( $postform ne "" &&  $monHow eq $req_monHow && $monWhen ne ""  ) { 
	  @l_when = split(/ /, $monWhen, -1);
	  $job = { 'mins' =>	 $l_when[0],
        	   'hours' =>	 $l_when[1],
          	   'days' =>	 $l_when[2],
  	           'months' => 	 $l_when[3],
        	   'weekdays' => $l_when[4]
	  }
     }
     print "<tr $cb> <td><table width=100%>\n";
     print "<tr $tb> <td colspan=5><b>$text{'edit_when'}</b></td> </tr> <tr $tb>\n";
     print "<td><b>$text{'edit_mins'}</b></td> <td><b>$text{'edit_hours'}</b></td> ",
           "<td><b>$text{'edit_days'}</b></td> <td><b>$text{'edit_months'}</b></td>",
           "<td><b>$text{'edit_weekdays'}</b></td> </tr> <tr $cb>\n";
     
     @mins = (0..59);
     @hours = (0..23);
     @days = (1..31);
     @months = map { $text{"month_$_"}."=".$_ } (1 .. 12);
     @weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6);
     
     foreach $arr ("mins", "hours", "days", "months", "weekdays") {
	     # Find out which ones are being used
	     local %inuse;
	     $min = ($arr =~ /days|months/ ? 1 : 0);
	     $max = $min+scalar(@$arr)-1;
	     foreach $w (split(/,/ , $job->{$arr})) {
		     if ($w eq "*") {
			     # all values
			     for($j=$min; $j<=$max; $j++) { $inuse{$j}++; }
			     }
		     elsif ($w =~ /^\*\/(\d+)$/) {
			     # only every Nth
			     for($j=$min; $j<=$max; $j+=$1) { $inuse{$j}++; }
			     }
		     elsif ($w =~ /^(\d+)-(\d+)\/(\d+)$/) {
			     # only every Nth of some range
			     for($j=$1; $j<=$2; $j+=$3) { $inuse{int($j)}++; }
			     }
		     elsif ($w =~ /^(\d+)-(\d+)$/) {
			     # all of some range
			     for($j=$1; $j<=$2; $j++) { $inuse{int($j)}++; }
			     }
		     else {
			     # One value
			     $inuse{int($w)}++;
			     }
		     }
	     if ($job->{$arr} eq "*") { undef(%inuse); }
     
	     # Output selection list
	     print "<td valign=top>\n";
	     printf "<input type=radio name=all_$arr value=1 %s> $text{'edit_all'}<br>\n",
		     $job->{$arr} eq "*" ? "checked" : "";
	     printf "<input type=radio name=all_$arr value=0 %s> $text{'edit_selected'}<br>\n",
		     $job->{$arr} ne "*" ? "checked" : "";
	     print "<table> <tr>\n";
	     for($j=0; $j<@$arr; $j+=12) {
		     $jj = $j+11;
		     if ($jj >= @$arr) { $jj = @$arr - 1; }
		     @sec = @$arr[$j .. $jj];
		     printf "<td valign=top><select multiple size=%d name=$arr>\n",
			     @sec > 12 ? 12 : scalar(@sec);
		     foreach $v (@sec) {
			     if ($v =~ /^(.*)=(.*)$/) { $disp = $1; $code = $2; }
			     else { $disp = $code = $v; }
			     printf "<option value=\"$code\" %s>$disp\n",
				     $inuse{$code} ? "selected" : "";
			     }
		     print "</select></td>\n";
		     }
	     print "</tr></table></td>\n";
	     }
     print "</tr> </table></td></table>\n";
}
print "</table>\n";
print "<table width=100%>\n";
print "<tr $cb><td><table width=100%>\n";
print "        <tr $cb> <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><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
