#!/usr/bin/perl
# tta_modhosts.cgi
# Mod Hosts 
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

require './stta-lib.pl';
&foreign_require("proc", "proc-lib.pl");        
if ( $config{'is_ldapcache'} eq "true" ) { 
   do "ldap-lib.pl";
}
&ReadParse();

 $aliashosts=$in{hostname};
 if (!$aliashosts) { &redirect("tta_selhosts.cgi?form=modhosts"); }
 $postform = $in{postform};
 &headerstta($text{'index_modhosts'}, "","modhosts",undef,1,1,$text{headpage_rigth} );
 $ttacmd="edit";
 # Cleaning and reset values ...
 $location="";
 $descrip="";
 $ntdomain="";
 $ipaddress="";
 $maxsess="";
 $poralive="";
 $usergrphosts="";
 $ens_obj="";
 $hostname="";
 $str_cmds="";
 $alias="";
 %h_hosts=&list_hosts();
 if  ( $h_hosts{$aliashosts}->{'alias'} ne $aliashosts ) {
       $err_msg=$text{'list_errno'};
       &footHere;
       exit;
 }
 $dn_host=$h_hosts{$aliashosts}->{'obj'};
 $ens_host=$h_hosts{$aliashosts}->{'ens'};
 $hostname=$h_hosts{$aliashosts}->{'name'};
 $usergrphosts=$h_hosts{$aliashosts}->{'grpalias'};
 $descrip=$h_hosts{$aliashosts}->{'descrip'};
 $alias=$h_hosts{$aliashosts}->{'alias'};
 $maxsess=$h_hosts{$aliashosts}->{'maxsess'}; 
 $portalive=$h_hosts{$aliashosts}->{'portalive'}; 
 $ip_address=$h_hosts{$aliashosts}->{'ipname'}; 
 if ( $ens_host ne "" ) {
      $ens_obj = " \"".$config{tta_base}."/$ens_host\"";
 }
 if ( $aliashosts ) {
      if ( $in{postform} ) {
           # loading object attributes ...
           $op="attributes";
           $type_cmd="attributes";
           $status_cmd=&run_tta_cmd($type_cmd,$ens_obj);
           if ( !$status_cmd ) {$str_cmds="<tr><td> ".$text{'tta_noobject'}."</td></tr>";}
       }
 } else {
      # loading object defaults ....
      $temp_f=$config{'tta_defaults_hosts'}; 
 } 
 %h_ttaobj=&load_ttaobj();
 $user =$h_ttaobj{'user'}->{'value'};
 $descrip =$h_ttaobj{'description'}->{'value'};
 $location =$h_ttaobj{'location'}->{'value'};
 $ntdomain =$h_ttaobj{'ntdomain'}->{'value'};
 $ipaddress =$h_ttaobj{'address'}->{'value'};
 if ( $aliashosts ) { unlink($temp_f); };
 $ipaddress = ($ipadress) ? $ipaddress : $ip_address;  

if ( $config{'is_ldapcache'} eq "true" ) { 
   $dn_cache="cn=$config{ldapcache_defaultobj},$dn_host,".$config{ldapcache_base};  
   $ldapcache_server=$in{ldapcacheserver} || "1";
   &connect_cache();
   # loading variables
   &load_tta3tier_vars($dn_cache);
}
 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 NTdomain=\"$ntdomain\";\n"; 
 print "\n"; 
 &htmlCheckAlias("hosts");
 &htmlCheckAlias("hostaddress");  
if ( $config{'is_ldapcache'} eq "true" ) { &htmlCacheaShowPassw }
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 print "     if ( get_value(\"select\",obj.usergrphosts) == \"none\" ) {\n";
 print "          alert(obj.usergrphosts.name+\":\"+msg1);\n";
 print "                    obj.usergrphosts.focus();\n";
 print "          return false;\n";
 print "     }         \n";
 print "     if ( emptyField(obj.hostsname) == true ) {\n"; 
 print "          alert(obj.hostsname.name+\":\"+msg1+\"->\"+obj.hostsname.value+\"<-\");\n"; 
 print "		    obj.hostsname.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( emptyField(obj.aliashosts) == true ) {\n"; 
 print "          alert(obj.aliashosts.name+\":\"+msg1+\"->\"+obj.aliashosts.value+\"<-\");\n"; 
 print "		    obj.aliashosts.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( emptyField(obj.ipaddress) == true ) {\n";
 print "          alert(obj.ipaddress.name+\":\"+msg1+\"->\"+obj.ipaddress.value+\"<-\");\n";
 print "                    obj.ipaddress.focus();\n";
 print "          return false;\n";
 print "     }         \n";
 print "     if ( checkAlias_hosts(obj.aliashosts) == true ) {\n";
 print "          return false;\n";
 print "     }         \n";
 print "     if ( checkAlias_hostaddress(obj.ipaddress) == true ) {\n";
 print "          return false;\n";
 print "     }         \n";
 print "     nro=parseInt(obj.maxsess.value);\n";
 print "     if ( nro+1 > 0) { \n";
 print "          obj.maxsess.value=nro;\n";
 print "     } else {;\n";
 print "          alert(obj.maxsess.name+\":\"+msg1+\"->\"+obj.maxsess.value+\"<-\");\n";
 print "          obj.maxsess.focus();\n";
 print "          return false;\n";
 print "     };\n";
 print "     nro=parseInt(obj.portalive.value);\n";
 print "     if ( nro+1 > 0) { \n";
 print "          obj.portalive.value=nro;\n";
 print "     } else {;\n";
 print "          alert(obj.portalive.name+\":\"+msg1+\"->\"+obj.portalive.value+\"<-\");\n";
 print "          obj.portalive.focus();\n";
 print "          return false;\n";
 print "     };\n";
 if ( $config{'is_ldapcache'} eq "true" ) { &script_cache_chek; };
 print "     if ( get_value(\"select\",obj.usergrphosts) != \"none\" ) {\n"; 
 print "          return confirm(obj.hostsname.value+msg7+get_value('select',obj.usergrphosts));\n"; 
 print "     }         \n"; 
 print "\n"; 
 print "     return true;\n"; 
 print "}\n"; 
 print "/******************************************************************\n"; 
 print "   setNTdomain(formObj)\n"; 
 print "   check for a NTdomain\n"; 
 print "*******************************************************************/\n"; 
 print "function setNTdomain(formObj) {\n"; 
 print "  if (emptyField(formObj.ntdomain)) {\n"; 
 print "        formObj.ntdomain.value=NTdomain;\n"; 
 print "        setStatusBar(msg0+NTdomain);\n"; 
 print "   }\n"; 
 print "   else {  \n"; 
 print "        name=StringClip(true,formObj.ntdomain.value);\n"; 
 print "        formObj.ntdomain.value=name;\n"; 
 print "   }\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_modhosts'} $aliashosts </b></td></tr>\n";
 print $str_cmds."\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<form METHOD=POST name=modhost ACTION=\"modhosts.cgi\" onSubmit=\"return Chek_fields(this)\">\n"; 
 print "	<input TYPE=\"hidden\" NAME=\"ttacmd\" VALUE=\"",$ttacmd,"\">\n"; 
 print "        <input TYPE=\"hidden\" NAME=\"aliashosts\" VALUE=\"",$aliashosts,"\">\n";
 print "	<input TYPE=\"hidden\" NAME=\"hostname\" VALUE=\"",$hostname,"\">\n"; 
 print "	<input TYPE=\"hidden\" NAME=\"usergrphosts\" VALUE=\"",$usergrphosts,"\">\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_aliashosts'}</b>","faliashosts"),"&nbsp;</td>\n";
 print "         <td valign=top><b>$aliashosts</b>&nbsp;</td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_hostname'}</b>","fhostsname"),"&nbsp;</td>\n";
 print "         <td valign=top><b>$hostname</b>&nbsp;</td></tr>\n"; 

 print "	<tr><td valign=top>",&hlink("<b>$text{'f_ipaddress'}</b>","fipaddress"),"&nbsp;</td>\n";
print "         <td valign=top><input TYPE=\"text\" NAME=\"ipaddress\" VALUE=\"$ipaddress\" cols=\"20'\" size=\"20\" ALIGN=left onChange=\"checkAlias_hostaddress(this)\"></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=\"$descrip\" cols=\"40\" size=\"40\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_location'}</b>","flocation"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"location\" VALUE=\"$location\" cols=\"60\" size=\"60\" ALIGN=left></td></tr>\n"; 
 print "  	<tr><td valign=top>",&hlink("<b>$text{'f_ntdomain'}</b>","fntdomain"),"</td>\n"; 
 print "        <td valign=top><input TYPE=\"text\" NAME=\"ntdomain\" VALUE=\"$ntdomain\" cols=\"12\" size=\"12\" ALIGN=left\n"; 
 print "		onChange=\"setNTdomain(modhost)\" onFocus=\"setNTdomain(modhost)\"></td></tr>\n"; 
 print "\n"; 
 print "   <tr><td valign=top>",&hlink("<b>$text{'f_usergrphosts'}</b>","fusergrphosts"),"&nbsp;</td>\n";
%h_grphosts=&list_grphosts();
if ( $h_grphosts{$usergrphosts}->{'alias'} eq $usergrphosts ) { 
          print "  <td valign=top><b>".$h_grphosts{$usergrphosts}->{'name'}."</b>&nbsp;</td></tr>\n"; 
}
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_maxsess'}</b>","fmaxsess"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"maxsess\" VALUE=\"$maxsess\" cols=\"4\" size=\"4\" ALIGN=left onChange=\"isNroThis(this)\">\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_portalive'}</b>","fportalive"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"portalive\" VALUE=\"$portalive\" cols=\"4\" size=\"4\" ALIGN=left onChange=\"isNroThis(this)\">\n";
if ( $config{'is_ldapcache'} eq "true" ) { $formObj="modhost";$msgObj=$text{'default_obj'};&htmlCacheapp(msgObj) }
 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;\n";
 print "        <td valign=top><input TYPE=\"reset\" NAME=\"",$text{'f_cancelreq'},"\" VALUE=\"",$text{'f_cancelreq'},"\"></td></tr>\n";
 print "	</form>\n"; 
 print "<tr><tr></table></table><hr><p>\n"; 
 &footer($config{'back_return'},$text{'index'}); 
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
