#!/usr/bin/perl
# tta_modusr.cgi
# Modify Tarantella Services Users

require './tarantella-lib.pl';
&foreign_require("proc", "proc-lib.pl");
&ReadParse();

 $dn = $in{dn};
 if (!$dn) { &redirect("tta_selusr.cgi?form=modusr"); }

 $administrador = $in{administrador};
 $passwdcache = $in{passwdcache};
 $userou = $in{userou};
 $username = $in{username};
 $userorg = $in{userorg};
 $postform = $in{postform};
 # Cleaning and reset values ...
 @l_bandwidth=(0,10000000,1000000,512000,256000,128000,57600,33600,28800,19200,14400,9600,4800,2400);
 $surname="";
 $descrip="";
 $user=$config{'loginUsername'}; 
 $email="";
 $ntdomain="";
# $ntdomain=$config{'companyntdomain'}; 
 $bandwidth="";
 $conntype="";
 $shared="";
 $enabled="";
 $inherit="";
 $cdm="";
 $webtop="";
 #if ( $in{postform} eq "tta_selusr" ) {
 #  $temp_f=$config{'ttadefaultsobj'}; 
 #} 
 #if ( $in{postform} eq "addusr" ) {
 #  
 #}
 &header($text{'index_moddusr'}, "", "modusr");
 print "<hr><p>\n"; 
 if ( $username ) {
      $ttacmd="edit";
      $organizationname="";
      $dn_ou="";
      $ens_ou="";
      foreach $ou (&list_ous()) {
         if ( $ou->{'alias'} eq $userou) {
            $dn_ou=$ou->{obj};
            $ens_ou=$ou->{ens};
            $organizationname=$ou->{name};
            last;
         }
      }
      $ens_obj = "\"".$config{tta_base}."/$ens_ou/cn=$username\"";
      if ( $in{postform} ) {
           # loading object attributes ...
           $op="attributes";
           $type_cmd="attributes";
           $status_cmd=&run_tta_cmd($type_cmd,$ens_obj);
           if ( !$status ) { $str_cmds="<tr><td> ".$text{'tta_noobject'}."</td></tr>";}
       }
 } else {
      # loading object defaults ....
      $temp_f=$config{'tta_defaults_obj'}; 
 } 
 # Assigning and collecting values to be used in the form ...
 foreach $i (&load_ttaobj()) {
            if ( $i->{'property'} eq "name" ) { $name=$i->{'value'} };
            if ( $i->{'property'} eq "surname" ) { $surname=$i->{'value'} };
            if ( $i->{'property'} eq "user" ) { $user=$i->{'value'} };
            if ( $i->{'property'} eq "description" ) { $descrip=$i->{'value'} };
            if ( $i->{'property'} eq "ntdomain" ) { $ntdomain=$i->{'value'} };
            if ( $i->{'property'} eq "email" ) { $email=$i->{'value'} };
            if ( $i->{'property'} eq "bandwidth" ) { $bandwidth=$i->{'value'} };
            if ( $i->{'property'} eq "cdm" ) { $cdm=$i->{'value'} };
            if ( $i->{'property'} eq "enabled" ) { $enabled=$i->{'value'} };
            if ( $i->{'property'} eq "inherit" ) { $inherit=$i->{'value'} };
            if ( $i->{'property'} eq "shared" ) { $shared=$i->{'value'} };
            if ( $i->{'property'} eq "webtop" ) { $webtop=$i->{'value'} };
            if ( $i->{'property'} eq "conntype" ) { $conntype=$i->{'value'} };
            if ( $i->{'property'} eq "links" ) { $links=$i->{'value'} };
 #      print "<p>(".$i->{'property'}.")-(".$i->{'value'}.")\n";
 }
 if ( $username ) { unlink($temp_f); };
 print "<SCRIPT SRC=\"global.js\">\n"; 
 print "</SCRIPT>\n"; 
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n"; 
 print "/*  Strings for locales  */\n"; 
 if ( $administrador eq "true" ) {
      print "var user_type=\"UNIX\";\n";
 } else {
      print "var user_type=\"LDAP\";\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_shared=true;\n"; 
 print "var ask_enabled=true;\n"; 
 print "var companyDomain=\"".$config{'companydomain'}."\"\n"; 
 print "var NTdomain=\"$ntdomain\";\n"; 
 print "var loginUXdefault=\"$user\";\n"; 
 print "/******************************************************************\n"; 
 print "   setEmailDefault(formObj)\n"; 
 print "   If email is empty assign user + companyDomain as default value in the form.\n"; 
 print "*******************************************************************/\n"; 
 print "function setEmailDefault(formObj) {\n"; 
 print "   if (emptyField(formObj.surname)) {\n"; 
 print "      formObj.surname.value=\"\";\n"; 
 print "      formObj.email.value=\"\";\n"; 
 print "      return\n"; 
 print "   }   \n"; 
 print "   emailAddr=StringClip(true,formObj.surname.value);\n"; 
 print "   formObj.surname.value=emailAddr;\n"; 
 print "   lpos=emailAddr.length;\n"; 
 print "   var cadena=\"\";\n"; 
 print "	if (lpos <= 1 ) \n"; 
 print "	    return ;\n"; 
 print "   for (var i=0; i < lpos ; i++) {\n"; 
 print "      if (emailAddr.substring(i,i+1) == \" \" ) break;\n"; 
 print "	} \n"; 
 print "	for ( var j=lpos; j>0; j-- ) {\n"; 
 print "      if (emailAddr.substring(j-1,j) == \" \" )  break;   \n"; 
 print "   }\n"; 
 print "\n"; 
 print "   if ( i < lpos ) {\n"; 
 print "      cadena=emailAddr.substring(0,i);\n"; 
 print "      if ( j > i+1 ) {  \n"; 
 print "            if ( j < lpos ) { cadena +=\".\"+emailAddr.substring(i+1,j-1)+emailAddr.substring(j,lpos);}\n"; 
 print "      } \n"; 
 print "      else {   \n"; 
 print "        cadena +=\".\"+emailAddr.substring(j,lpos);\n"; 
 print "      }\n"; 
 print "   }\n"; 
 print "   else {\n"; 
 print "     cadena=emailAddr;\n"; 
 print "   }   \n"; 
 print "   if (emptyField(formObj.email)) {\n"; 
 print "        formObj.email.value=cadena.toLowerCase()+\"@\"+companyDomain;\n"; 
 print "        setStatusBar(msg0);\n"; 
 print "   }\n"; 
 print "}\n"; 
 print "/******************************************************************\n"; 
 print "   setUser(formObj)\n"; 
 print "   check for a User\n"; 
 print "*******************************************************************/\n"; 
 print "function setUser(formObj) {\n"; 
 print "   if (emptyField(formObj.user)) {\n"; 
 print "       if ( user_type == \"LDAP\" ) {\n"; 
 print "             formObj.user.value=loginUXdefault;   \n"; 
 print "       }\n"; 
 print "       else {\n"; 
 print "             formObj.user.value=formObj.username.value;   \n"; 
 print "       }\n"; 
 print "   }   \n"; 
 print "   else  {\n"; 
 print "      name=StringClip(true,formObj.user.value);\n"; 
 print "      formObj.user.value=name;\n"; 
 print "   }   \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 "   showHelp(item)\n"; 
 print "*******************************************************************/\n"; 
 print "function showHelp(item) {\n"; 
 print "     var str=\"\";\n"; 
 print "     if ( item == \"conntype\" ) {\n"; 
 print "          window.open('/help.cgi/tarantella/conntype','help',",
                    "'toolbar=no,menubar=no,scrollbars=yes,width=400,height=300,resizable=yes')\n";
 print "     }\n"; 
 print "     if ( item == \"cdm\" ) {\n"; 
 print "          window.open('/help.cgi/tarantella/cdm','help',",
                    "'toolbar=no,menubar=no,scrollbars=yes,width=400,height=300,resizable=yes')\n";
 print "     }\n"; 
 print "}\n"; 
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 print "     if ( emptyField(obj.user) == true ) {\n"; 
 print "          alert(obj.user.name+\":\"+msg1+\"->\"+obj.user.value+\"<-\");\n"; 
 print "		    obj.user.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( emptyField(obj.surname) == true ) {\n"; 
 print "          alert(obj.surname.name+\":\"+msg1+\"->\"+obj.surname.value+\"<-\");\n"; 
 print "		    obj.surname.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( get_value(\"select\",obj.webtop) == \"none\" ) {\n"; 
 print "          alert(obj.webtop.name+\":\"+msg1);\n"; 
 print "		    obj.webtop.focus();\n"; 
 print "          return false;\n"; 
 print "     }     \n"; 
 print "     if ( obj.shared.checked ) {\n"; 
 print "          obj.shared.value=true; \n"; 
 print "          if ( ask_shared == true ) {\n"; 
 print "             alert(msg5);\n"; 
 print "             ask_shared=false;\n"; 
 print "             obj.shared.focus();\n"; 
 print "             return false;\n"; 
 print "          }  \n"; 
 print "     }\n"; 
 print "     else {\n"; 
 print "          obj.shared.value=false; \n"; 
 print "     }\n"; 
 print "     if ( obj.enabled.checked ) {\n"; 
 print "          obj.enabled.value=true; \n"; 
 print "     }\n"; 
 print "     else {\n"; 
 print "          obj.enabled.value=false; \n"; 
 print "          if ( ask_enabled == true ) {\n"; 
 print "             alert(msg6);\n"; 
 print "             ask_enabled=false;\n"; 
 print "             obj.enabled.focus();\n"; 
 print "             return false;\n"; 
 print "          }  \n"; 
 print "     }\n"; 
 print "     if ( obj.inherit.checked ) {\n"; 
 print "          obj.inherit.value=true; \n"; 
 print "     }\n"; 
 print "     else {\n"; 
 print "          obj.inherit.value=false; \n"; 
 print "     }\n"; 
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "<p>\n"; 
 print "<table width=100% border>\n";
 if ( $username ) {
     print "<tr $tb> <td><b>".&text('titidx_modusr',$username,$organizationname)."</b></td> </tr>\n";
 }
 else {
     print "<tr $tb> <td><b>".$text{'titidx_modusr0'}."</b></td> </tr>\n";
 }
 print $str_cmds."\n";  
 print "<tr $cb> <td><table width=100%>\n";
 print "<form METHOD=POST name=modusr ACTION=\"modusr.cgi\" onSubmit=\"return Chek_fields(this)\">\n"; 
 print "\n"; 
 if ( $username ) {
       print "	<input TYPE=\"hidden\" NAME=\"username\" VALUE=\"",$username,"\">\n"; 
       print "	<input TYPE=\"hidden\" NAME=\"userou\" VALUE=\"",$userou,"\">\n"; 
       print "	<input TYPE=\"hidden\" NAME=\"userorg\" VALUE=\"",$userou,"\">\n"; 
       print "	<input TYPE=\"hidden\" NAME=\"administrador\" VALUE=\"",$administrador,"\">\n"; 
       print "	<input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\n"; 
       print "	<input TYPE=\"hidden\" NAME=\"passwdcache\" VALUE=\"",$passwdcache,"\">\n"; 
 } 
 else {
  print "	<tr><td valign=top>",&hlink("<b>$text{'f_username'}</b>","fusername"),"&nbsp;",$username,"</td>\n"; 
 print "        <td valign=top><input TYPE=\"text\" NAME=\"username\" VALUE=\"\" cols=\"22\" size=\"12\" ALIGN=left></td></tr>\n";
#  print "	&nbsp;&nbsp;",&hlink("<b>$text{'f_grupo'}</b>","fgrupo")."  ".$userou,"\n"; 
  }
 print "	<input TYPE=\"hidden\" NAME=\"ttacmd\" VALUE=\"",$ttacmd,"\">\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_surname'}</b>","fsurname")."</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"surname\" VALUE=\"$surname\" cols=\"40\" size=\"40\" ALIGN=left\n"; 
 print "		onChange=\"modusr.email.value='';setEmailDefault(modusr)\" onFocus=\"setEmailDefault(modusr)\" ></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_user'}</b>","fuser"),"</td>\n"; 
 print "        <td valign=top><input TYPE=\"text\" NAME=\"user\" VALUE=\"$user\" cols=\"12\" size=\"12\" ALIGN=left\n"; 
 print "		onChange=\"setUser(modusr)\" onFocus=\"setUser(modusr)\" ></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_email'}</b>","femail"),"</td>\n"; 
 print "	<td valign=top><input TYPE=\"text\" NAME=\"email\" VALUE=\"$email\" cols=\"60\" size=\"60\" ALIGN=left\n"; 
 print "		onChange=\"setEmailDefault(modusr)\" onFocus=\"setEmailDefault(modusr)\"></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(modusr)\" onFocus=\"setNTdomain(modusr)\"></td></tr>\n"; 
 print "\n"; 
 print "   <tr><td valign=top>",&hlink("<b>$text{'f_webtop'}</b>","fwebtop"),"&nbsp;</td><td valign=top><select NAME=\"webtop\">\n"; 

$nItems=0;
foreach $item (&list_webtops()) {
     if ( $item->{'obj'} eq $webtop ) {
          print " <option VALUE=\"".$item->{'alias'}."\" selected>". $item->{'name'}."\n";
    }
     else {
          print " <option VALUE=\"".$item->{'alias'}."\" >".$item->{'name'}."  ";
     }
     $nItems++;
}
 print "	</select></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_inherit'}</b>","finherit"),"&nbsp; </td><td valign=top><input TYPE=\"checkbox\" NAME=\"inherit\" VALUE=\"$inherit\" ";
 if ( $inherit eq 1 ) { print " checked "; }
 print "></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_shared'}</b>","fshared"),"&nbsp;</td> <td valign=top><input TYPE=\"checkbox\" NAME=\"shared\" VALUE=\"$shared\" ";
 if ( $shared eq 1 ) { print " checked "; }
 print "></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_enabled'}</b>","fenabled"),"&nbsp; </td><td valign=top><input TYPE=\"checkbox\" NAME=\"enabled\" VALUE=\"$enabled\" ";
 if ( $enabled eq 1 ) { print " checked "; }
 print "></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_conect'}</b>","fconect"),"\n"; 
 print "	   ",&hlink("<b>$text{'f_ipaddr'}</b>","fipaddr"),"-",&hlink("<b>$text{'f_contype'}</b>","fcontype"),"&nbsp;</td>\n"; 
"\n"; 
 print "	  <td valign=top> <input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"showHelp('conntype');\" >\n"; 
 print "	   <input TYPE=\"text\" NAME=\"conntype\" VALUE=\"$conntype\" cols=\"32\" size=\"32\" ALIGN=left></td></tr>   \n"; 
 print "      <tr><td valign=top>",&hlink("<b>$text{'f_cdm'}</b>","fcdm"),"</td>\n"; 
 print "	   <td valign=top><input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"showHelp('cdm');\" >\n"; 
 print "	   <input TYPE=\"text\" NAME=\"cdm\" VALUE=\"$cdm\" cols=\"42\" size=\"42\" ALIGN=left></td></tr>   \n"; 
 print"        <tr><td valign=top>",&hlink("<b>$text{'f_bandwidth'}</b>","fbandwidth"),"</td>\n"; 
 print "	   <td valign=top><select NAME=\"bandwidth\">\n"; 
 foreach $item (@l_bandwidth) {
              print "  	     <option VALUE=\"$item\" ";
              if  ( $bandwidth eq $item ) { print "selected"; }
              print ">$item</b>\n";
 }
 print "	   </select></td></tr>\n"; 
 print "\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 "	\n"; 
 print "<tr></tr><tr></tr></table></table><hr><p>\n"; 
 &footer($config{'back_return'},$text{'index'}); 
 print "<p><p><p>\n";
 do "footer.pl";
 exit;



