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

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

&headerstta($text{'index_modapp'}, "", "modapp",undef,1,1,$text{headpage_rigth} );
print "<hr><p>\n"; 

 $type_cmd = $in{ttacmd};
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_modapp'}</b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $status_cmd=0;
 $descrip=$in{descrip};
 $maxsess=$in{maxsess};
 $aliasapp=$in{aliasapp};
 $appname=$in{appname};
 $grpalias=$in{grpapps};

 if ( !$aliasapp) { &footHere; exit }  ;
 $alias=$aliasapp;
 %h_apps=&list_apps();
 if ( $h_apps{$aliasapp}->{'alias'} ne $aliasapp ) {
         $err_msg=$text{'list_errno'};
         &footHere;
         exit;
 }
 $dn_app=$h_apps{$aliasapp}->{'obj'};
 $organizationname=$h_apps{$aliasapp}->{'name'};
 $ens_app=$h_apps{$aliasapp}->{'ens'};     
 $grpalias=$h_apps{$aliasapp}->{'grpalias'};     
 if ( $dn_app eq "" ) {
       $dn_org = "ou=$appname";
       $ens_org= "cn=$appname";
 } else {
       $dn_org= $dn_app;
       $ens_org= $ens_app;
 }
 $ens_obj = "--name \"".$config{tta_base}."/$ens_org\"";
 # Default object para LDAP cache ...
 if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
         $obj_descrip=$in{descrip};
         $dnldap = "default";
         $default_obj="cn=$config{ldapcache_defaultobj}";
         $dn_cache=$dn_org;
         $task="mod";
         $ldapcache_server=1;
         &connect_cache();
         $dn_wk=$default_obj.",".$dn_cache.",".$config{ldapcache_base};
	 $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
         if ( !defined($cur_entry) || $cur_entry->dn ne $dn_wk) { $task="add"; };
         @tta3tier=&set_tta3tier($default_obj,$dn_cache,$dnldap,$task,"app");
         if ( &cache_obj(\%tta3tier) eq 0 ) {
              if ( $config{ldapcache2_server}.length gt 0 ) {
                   $ldapcache_server=2;
                   &connect_cache ();
                   if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
              }
         } else { &html_errshow("ldap")};
 } 
 delete $h_apps{$aliasapp};
 foreach $item (keys (%h_apps)) { push (@listapps, $h_apps{$item}); }
 @list__apps = sort {$a->{'name'} cmp $b->{'name'}} @listapps;
 &lock_file($config{'app_list'});
 open(FILEAPPS, ">$config{'app_list'}");
      foreach $app (@list__apps) {
              $linapp=$app->{'alias'}."::".$app->{'name'}."::".$app->{'grpalias'}."::".
                       $app->{'obj'}."::".$app->{'ens'}."::".$app->{'maxsess'}."::".
                       $app->{'descrip'}."::\n";
              print FILEAPPS "$linapp";
      }
      $linapp=$aliasapp."::".$appname."::".$grpalias."::"."$dn_org"."::"."$ens_org"."::".$maxsess."::".$descrip."::\n";
      print FILEAPPS "$linapp";
 close (FILEAPPS);

 &unlock_file($config{'app_list'});
 $str=$text{'user_update'}.$aliasapaliasappp;
 &additional_log('modapps.cgi', "app", $str);
 &webmin_log($config{'app_list'}, "app", "update",$libap,$libappp);
 print "<tr><p><b>".$text{'user_update'}.$aliasapp." ->  ".$appname."</b></tr>\n";
 print "<tr></tr>\n";
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
