#!/usr/bin/perl
# config_info.pl
# configure Services with Tarantella 
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require '../web-lib.pl';

do 'web-lib.pl';
do 'global-lib.pl';
&init_config();
&load_dataconf();

$dataConf="dataconf";
$listfcfgs="ordercfgs";
$configValues="sttavalues";
$defaultcfg="defaultcfg";
$scancachecfg="scancache";
$envsttashell="envstta";
$scanlistcfgs="scanlistcfgs";
$envshlistcfgs="envshlistcfgs";

$now_is=&set_now();

sub config_form
{
  &set_interface_colors();   #  just to be sure !!!
  if ( -r "$config_directory/$module_name/.insupd.lock" ) {
     print "<tr><td><h2>$text{stta_locked} <h2></td><tr>\n";
  }
  $m = "stta";
  &set_variables_cfg();
  if (!(-d $configDir )) {
        if ( $_[0] eq "report" ) { 
             print &error('config_error','config.cgi?stta');
             return;
        }
        if ( -d "/etc/openldap" ||  -x "/usr/local/openldap/slapd" || -x "/usr/local/libexec/slapd" ) { 
              &read_file("$configDef/$defaultcfg.openldap", \%sttaconfig);
        } else {
              &read_file("$configDef/$defaultcfg.iplanet", \%sttaconfig);
        }
	# Use config.info to create config inputs
	&read_file("$module_dir/configinit.info", \%info);
	foreach $o (@lang_order_list) {
	   &read_file("$module_dir/configinit.info.$o", \%info);
	}
        @info_order=();
        foreach $c (keys (%info)) {
           push(@info_order, { 'name' => $c, 'title' => $info{$c} });
        }
	%einfo = %info;
	@info_order = &unique(@info_order);
        &set_values_from();
   } else {
	&read_file("$module_dir/config.info", \%infoall, \@info_orderall);
	foreach $o (@lang_order_list) {
		&read_file("$module_dir/config.info.$o", \%infoall, \@info_orderall);
		}
        if ( $fileCFG ne "" ) {
                 %listfcfgs=();
                 @orderfcfgs=();
                 &read_file("$baseDataConfDir/$listfcfgs", \%listaf);
                 foreach $d (keys (%listaf) ) {
                       if ( $d eq $fileCFG ) {
                            push(@orderfcfgs, $d ); 
                            $listfcfgs{$d}=$listaf{$d};
                            last;
                       }
                 }
        } else {
             &read_file("$baseDataConfDir/$listfcfgs", \%listfcfgs, \@orderfcfgs);
             print "<table border=0 align=center>\n";
             print "<tr $cb><td><ul></td></tr>\n";
             local $i=0;
             foreach $d (@orderfcfgs) {
                     $i++;
                     $item_title=$d."_title";
                     $item_help=$listfcfgs{$d};
                     print "<tr $cb><td valign=center><li>";
                     if ( $_[0] ne "report" ) { 
                         print "<a class=\"menu1\" href=\"config.cgi?$d\"> <img SRC='/stta/images/icon_pencil.gif' width=20 heigth=18 align=top border=0></a>&nbsp;&nbsp; ";
                     }
                     print "<a class=\"menu1\" href=\"#$d\">$i - $text{$item_title}</a></td><td>",&hlink("<img SRC='/stta/images/info.gif' width=20 heigth=20 align=top border=0>","$item_help"),"</td><td align=rigth> - $d&nbsp;</li></td></tr>\n";
             }
             print "</ul></table><br>\n";
        }
        @orderf_cfgs = @orderfcfgs;
        @orderfcfgs = sort @orderf_cfgs;
        
        foreach $d (@orderfcfgs) {
                %sttaconfig=();
                %sttabase=();
                @stta_base=();
                %info=();
	        @info_order=();
                &read_file("$configDir/$d", \%sttaconfig);
                &read_file("$baseDataConf/$d", \%sttabase, \@stta_base);
                if ( $d eq "session" ) {
                     if ( $sttaconfig{msg_noticket} eq "" ) {
                          $sttaconfig{msg_noticket}=$text{msg_noticket};
                     }
                     if ( $sttaconfig{msg_nosess} eq "" ) {
                          $sttaconfig{msg_nosess}=$text{msg_nosess};
                     }
                }
                $item_title=$d."_title";
                $item_help=$listfcfgs{$d};
                print "<tr><td>&nbsp;<b>$text{$item_title}<a name=\"$d\"></b></td></tr>\n"; 
                print "<tr><td align=center><table border align=center>\n";
                print "<tr $tb> <td>",&hlink("<img SRC='/stta/images/info.gif' width=20 heigth=20 align=top border=0><b>$text{$item_title}</b>","$item_help","stta","menu2"),"</b> - $d&nbsp;";
                if ( $fileCFG eq "" &&  $_[0] ne "report" ) { 
                     print "<a class=\"menu2\" href=\"config.cgi?$d\"><img SRC='/stta/images/icon_pencil.gif' width=20 heigth=18 align=top border=0>$text{modify}</a>";
                }
                print "</font></td></tr>\n";
                print "<tr $cb> <td><table width=100%>\n";
                foreach $c (@stta_base) {
                     $info{$c}=$infoall{$c};
                     push(@info_order, { 'name' => $c, 'title' => $infoall{$c} });
                }
                $einfo=%info;
		@info_order = &unique(@info_order);
#                @info_order = sort {$a->{'title'} cmp $b->{'title'}} @info_order;
                if ( $_[0] eq 'report'  ) { 
                     &view_values_from();
                } else {
                     &set_values_from();
                }
	        print "</table></table></td></tr>\n";
        }
        if ( $ENV{'HTTP_USER_AGENT'} =~ /MSIE/ && $ENV{'HTTP_USER_AGENT'} =~ /Windows/ 
             && $fileCFG eq "" ) {
             $text{'save'}="";
        }
   }

}

sub config_save
{
  $m = $in{module} || "stta";
  $fileCFG = $in{fcfg} || "";
  &set_variables_cfg();

  if (!(-d $configDir )) {
        mkdir($configDir, 0777);
        &init_dataconf();
        if ( $main::config{'tta_dir'} ne "" ) {
              &test_minfo("migrate","html");
              system("cat $config_directory/$m/config > $configDir/config.old");
              %main::config=();  # Reset
              $main::config{'MIGRATE'}="$now_is $remote_user [".&get_system_hostname()."]  dataconf/config.old";
        } else {
              %main::config=();  # Reset
        }
        $main::config{'CONFIG'}="look at dataconf directory --- everything is there !!!";
        $main::config{'INSTALL'}="$now_is  $remote_user [".&get_system_hostname()."]";
   } else {
     if ( $ENV{'HTTP_USER_AGENT'} =~ /MSIE/ && $ENV{'HTTP_USER_AGENT'} =~ /Windows/ && $fileCFG eq "" ) {
            &header($text{'config_title'}, "", undef, 0, 1);
            print "<hr width=\"100%\" noshade size=2><p>\n";
            print "<p><p><p>\n";
            print "<table width=100% border>\n";
            print "<tr></tr><tr $cb><td align=center><b>$text{'ieconfigsave_err'}</b></td></tr>\n";
            print "</table>\n";
            print "$ENV{'HTTP_USER_AGENT'}<br>";
            print "<hr noshade size=2>\n";
            print "<p><p><p>\n";
            &footer($config{'back_return'},$text{'index'});
            print "<p><p><p>\n";
            exit;
     }
     &read_file("$module_dir/config.info", \%infoall, \@info_orderall);
     if ( $fileCFG ne "" && $fileCFG ne "ALL" ) {
          %listfcfgs=();
          @orderfcfgs=();
          &read_file("$baseDataConfDir/$listfcfgs", \%listaf);
          foreach $d (keys (%listaf) ) {
                  if ( $d eq $fileCFG ) {
                       push(@orderfcfgs, $d ); 
                       $listfcfgs{$d}=$listaf{$d};
                       $main::config{$fileCFG}="$now_is  $remote_user  [".&get_system_hostname()."]";
                       last;
                 }
         }
     } else {
         &read_file("$baseDataConfDir/$listfcfgs", \%listfcfgs, \@orderfcfgs);
         $main::config{'UPDATEFULL'}="$now_is  $remote_user [".&get_system_hostname()."]";
     }
     foreach $d (@orderfcfgs) {
                %sttabase=();
                @stta_base=();
                %info=();
	        @info_order=();
                &read_file("$baseDataConf/$d", \%sttabase, \@stta_base);
                foreach $c (@stta_base) {
                     push(@info_order, $c ); 
                     $info{$c}=$infoall{$c};
                }
	        @info_order = &unique(@info_order);
                %sttaconfig=();
                &get_values_tosave();
                &lock_file("$configDir/$d");
                open(CFGFILE, ">$configDir/$d") || &error(&text("efilewrite", $configDir/$d, $!));
                foreach $c (@info_order) {
                     if (exists($sttaconfig{$c})) {
                         (print CFGFILE $c,"=",$sttaconfig{$c},"\n") ||
                             &error(&text("efilewrite", $configDir/$d, $!));
                     }
                }
                close(CFGFILE);
                &unlock_file("$configDir/$d");
     }
   }
   &make_scancfg();
   &make_envshfile();
}

sub set_variables_cfg
{
     $module_dir="$root_directory/stta";

     $configDir="$config_directory/$m/$dataConf";
     $configDef="$module_dir/$dataConf/defs";
     $baseDataConf="$module_dir/dataconf/base";
     $baseDataConfDir="$module_dir/dataconf";
     
     foreach $l (@lang_order_list) {
             &read_file("$module_dir/lang/cfg/$l", \%text);
     }
     foreach $k (keys %text) {
             $text{$k} =~ s/\$([A-Za-z0-9\.\-\_]+)/text_subs($1,\%text)/ge;
     }
}

sub make_scancfg
{
    local %scanconfig=();
    local %listfcfgs=();
    local @scanfcfgs=();
    local $d="";
    local $v="";

    &read_file("$baseDataConfDir/$scanlistcfgs", \%listfcfgs, \@scanfcfgs);
    foreach $d (@scanfcfgs) {
          &read_file("$configDir/$d", \%scanconfig);
    }
    &write_file("$configDir/$scancachecfg", \%scanconfig);
}

sub make_envshfile
{
    local %listvcfgs=();
    local @scanvcfgs=();
    local $d="";
    local $v="";

    %config=();
    &load_dataconf();
    &read_file("$baseDataConfDir/$envshlistcfgs", \%listvcfgs, \@scanvcfgs);
    open(ARFILE,">$configDir/$envsttashell");
    print ARFILE "#!/bin/sh \n";
    print ARFILE "$text{set_env_vars}\n";
    print ARFILE "# \n";
    print ARFILE "STTA_CFGDIR=$configDir ; export STTA_CFGDIR \n";
    print ARFILE "WEBMIN_ETC=$config_directory ; export WEBMIN_ETC \n";
    print ARFILE "WEBMIN_ROOT=$root_directory ; export WEBMIN_ROOT \n";
    print ARFILE "WEBMIN_LANG=$gconfig{lang};  export WEBMIN_LANG \n";
    @scanvcfgs = sort @scanvcfgs;
    foreach $d (@scanvcfgs) {
            $v=$listvcfgs{$d}; 
            print ARFILE "$d=\"".$config{$v}."\"; export $d\n";
    }
    close(ARFILE);
    chmod(0555, "$configDir/$envsttashell");
    if ( $config{stta_tools_dir} && -d $config{stta_tools_dir} ) { 
         system("cp -p $configDir/$envsttashell $config{stta_tools_dir}");
         chmod(0555, "$config{stta_tools_dir}/$envsttashell");
    }
}

sub get_values_tosave
{
	foreach $c (@info_order) {
		@p = split(/,/, $info{$c});
		if ($p[1] == 0 || $p[1] == 7 || $p[1] == 8 || $p[1] == 12 ) {
			# Free text input
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 1 || $p[1] == 4) {
			# One of many
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 5 || $p[1] == 6) {
			# User or group
			$sttaconfig{$c} = ($p[2] && $in{$c."_def"} ? "" : $in{$c});
			}
		elsif ($p[1] == 2) {
			# Many of many
			$in{$c} =~ s/\0/,/g;
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 3) {
			# Optional free text
			if ($in{$c."_none"}) { $sttaconfig{$c} = ""; }
			else { $sttaconfig{$c} = $in{$c}; }
			}
		elsif ($p[1] == 9) {
			# Multilines of free text
			local $sp = $p[4] ? eval "\"$p[4]\"" : " ";
			$in{$c} =~ s/\r//g;
			$in{$c} =~ s/\n/$sp/g;
			$in{$c} =~ s/\s+$//;
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 10) {
			# One of many or free text
			if ($in{$c} eq 'free') {
				$sttaconfig{$c} = $in{$c.'_free'};
				}
			else {
				$sttaconfig{$c} = $in{$c};
				}
			}
		}
}
sub set_values_from
{
	foreach $lineform (@info_order) {
                $c=$lineform->{'name'};
		@p = split(/,/, $info{$c});
		@ep = split(/,/, $einfo{$c});
		if (scalar(@ep) > scalar(@p)) {
			push(@p, @ep[scalar(@p) .. @ep-1]);
			}
		if ($p[1] == 11) {
			print "<tr><td colspan=3 $tb><font color='\$revtext'>\n" ;
			print "\t<b>" . &hlink($p[0],"config/$c") . "</b></font>\n</td></tr>\n" ;
			next;
			}
		print "<tr> <td valign=top><b>".&hlink($p[0],"config/$c")."</b></td>\n";
		print "<td nowrap>\n";
		$qw = $sttaconfig{$c} =~ /'/ ? '"' : "'";
		if ($p[1] == 0 || $p[1] == 12) {
			$size = $p[2] ? "size=$p[2]" : "size=40";
			$max = $p[3] ? "maxlength=$p[3]" : "";
			$type = $p[1] == 12 ? "type=password" : "";
			print "<input name=\"$c\" $size $max $type value=$qw$sttaconfig{$c}$qw>\n";
			}
		elsif ($p[1] == 1) {
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<input type=radio name=\"$c\" value='$1' %s>\n",
					$sttaconfig{$c} eq $1 ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				}
			}
		elsif ($p[1] == 2) {
			local %sel;
			map { $sel{$_}++ } split(/,/, $sttaconfig{$c});
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<input type=checkbox name=\"$c\" value='$1' %s>\n", $sel{$1} ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				}
			}
		elsif ($p[1] == 3) {
			$none = $p[2] ? $p[2] : $text{'config_none'};
			printf "<input type=radio name=\"%s_none\" value=1 %s> $none\n",
				$c, $sttaconfig{$c} eq "" ? "checked" : "";
			print "&nbsp;&nbsp;\n";
			printf "<input type=radio name=\"%s_none\" value=0 %s>\n",
				$c, $sttaconfig{$c} eq "" ? "" : "checked";
			print "<input name=\"$c\" size=20 value=$qw$sttaconfig{$c}$qw>\n";
			}
		elsif ($p[1] == 4) {
			print "<select name=\"$c\">\n";
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S+)\-(.*)$/;
				printf "<option value=\"$1\" %s>$2\n",
					$sttaconfig{$c} eq $1 ? "selected" : "";
				}
			print "</select>\n";
			}
		elsif ($p[1] == 5) {
			if ($p[2]) {
				printf
				   "<input type=radio name=${c}_def value=1 %s>$p[2]\n",
				   $sttaconfig{$c} eq "" ? "checked" : "";
				printf "<input type=radio name=${c}_def value=0 %s>\n",
				   $sttaconfig{$c} eq "" ? "" : "checked";
				}
			print &unix_user_input($c, $sttaconfig{$c});
			}
		elsif ($p[1] == 6) {
			if ($p[2]) {
				printf
				   "<input type=radio name=${c}_def value=1 %s>$p[2]\n",
				   $sttaconfig{$c} eq "" ? "checked" : "";
				printf "<input type=radio name=${c}_def value=0 %s>\n",
				   $sttaconfig{$c} eq "" ? "" : "checked";
				}
			print &unix_group_input($c, $sttaconfig{$c});
			}
		elsif ($p[1] == 7) {
			print "<input name=\"$c\" size=40 value=$qw$sttaconfig{$c}$qw> ",
			      &file_chooser_button($c, 1);
			}
		elsif ($p[1] == 8) {
			print "<input name=\"$c\" size=40 value=$qw$sttaconfig{$c}$qw> ",
			      &file_chooser_button($c, 0);
			}
		elsif ($p[1] == 9) {
			local $cols = $p[2] ? $p[2] : 40;
			local $rows = $p[3] ? $p[3] : 5;
			local $sp = $p[4] ? eval "\"$p[4]\"" : " ";
			print "<textarea name=\"$c\" rows=$rows cols=$cols>",
				join("\n", split(/$sp/, $sttaconfig{$c})),
				"</textarea>\n";
			}
		elsif ($p[1] == 10) {
			local $fv = $sttaconfig{$c};
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<input type=radio name=\"$c\" value=\"$1\" %s>\n",
					$sttaconfig{$c} eq $1 ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				$fv = undef if ($sttaconfig{$c} eq $1);
				}
			printf "<input type=radio name=\"$c\" value=free %s>\n",
				$fv ? "checked" : "";
			print "<input name=\"${c}_free\" value='$fv'>\n";
			}
		print "</td> </tr>\n";
		}
}
sub view_values_from
{
	foreach $lineform (@info_order) {
                $c=$lineform->{'name'};
		@p = split(/,/, $info{$c});
		@ep = split(/,/, $einfo{$c});
		if (scalar(@ep) > scalar(@p)) {
			push(@p, @ep[scalar(@p) .. @ep-1]);
			}
		if ($p[1] == 11) {
			print "<tr><td colspan=3 $tb><font color='\$revtext'>\n" ;
			print "\t<b>" . $p[0] . "</b></font>\n</td></tr>\n" ;
			next;
			}
		print "<tr> <td valign=top><b>$p[0]</b></td>\n";
		print "<td nowrap>\n";
		$qw = $sttaconfig{$c} =~ /'/ ? '"' : "'";
		if ($p[1] == 0 || $p[1] == 12) {
			$size = $p[2] ? "size=$p[2]" : "size=40";
			$max = $p[3] ? "maxlength=$p[3]" : "";
			$type = $p[1] == 12 ? "type=password" : "";
			print "$sttaconfig{$c}\n";
			}
		elsif ($p[1] == 1) {
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
                                if ( $sttaconfig{$c} eq $1 ) { print "$2 &nbsp;&nbsp;\n";}
				}
			}
		elsif ($p[1] == 2) {
			local %sel;
			map { $sel{$_}++ } split(/,/, $sttaconfig{$c});
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
                                if ( $sel{$1} ) { print "$2 &nbsp;&nbsp;\n";}
				}
			}
		elsif ($p[1] == 3) {
			$none = $p[2] ? $p[2] : $text{'config_none'};
                        if ( $sttaconfig{$c} ) { print "$c &nbsp;&nbsp; $sttaconfig{$c}\n";}
			}
		elsif ($p[1] == 4) {
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S+)\-(.*)$/;
                                if ( $sttaconfig{$c} eq $1 ) { print "$2\n";}
				}
			}
		elsif ($p[1] == 5) {
			if ($p[2]) {
                                if ( $sttaconfig{$c} ) { print "$p[2]\n";}
				}
			print &unix_user_input($c, $sttaconfig{$c});
			}
		elsif ($p[1] == 6) {
			if ($p[2]) {
                                if ( $sttaconfig{$c} ) { print "$p[2]\n";}
				}
			print &unix_group_input($c, $sttaconfig{$c});
			}
		elsif ($p[1] == 7) {
			print "$sttaconfig{$c}";
			}
		elsif ($p[1] == 8) {
			print "$sttaconfig{$c}";
			}
		elsif ($p[1] == 9) {
			local $cols = $p[2] ? $p[2] : 40;
			local $rows = $p[3] ? $p[3] : 5;
			local $sp = $p[4] ? eval "\"$p[4]\"" : " ";
			print join("\n", split(/$sp/, $sttaconfig{$c})),"\n";
			}
		elsif ($p[1] == 10) {
			local $fv = $sttaconfig{$c};
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
                                if ( $sttaconfig{$c} eq $1 ) { print "$2 &nbsp;&nbsp;\n";}
				$fv = undef if ($sttaconfig{$c} eq $1);
				}
			printf "%s\n",
				$fv ? "checked" : "";
			print "$fv\n";
			}
		print "</td> </tr>\n";
		}
}
sub init_dataconf
{
        %sttaconfig=();
	# Use config.info to parse config inputs
	&read_file("$module_dir/configinit.info", \%info, \@info_order);
	%einfo = %info;
	foreach $o (@lang_order_list) {
		&read_file("$module_dir/configinit.info.$o", \%info, \@info_order);
		}
	@info_order = &unique(@info_order);
        &get_values_tosave;

        &write_file("$configDef/$configValues", \%sttaconfig);

        if ( $in{ldap_type} eq "OpenLDAP" ) {
             $suffixcfg=".openldap";
        } else {
             $suffixcfg=".iplanet";
        }
        &read_file("$configDef/$listfcfgs$suffixcfg", \%listfcfgs, \@orderfcfgs);
        @orderfcfgs = &unique(@orderfcfgs);
        &read_file("$configDef/$configValues", \%sttaValues);

        foreach $d (@orderfcfgs) {
              %config=();
              $nomFile=$d;
              $nomFile=~ s/$suffixcfg//g;
              &lock_file("$configDir/$nomFile");
              &read_file("$configDef/$d", \%config);
	      foreach $o (@lang_order_list) {
                &read_file("$configDef/$d.$o", \%config);
              }
              foreach $c (keys (%config)) {
                   foreach $v (keys (%sttaValues)) {
                       if ( $config{$c} =~ /$v/ ) { 
                            $config{$c}=~s/$v/$sttaValues{$v}/g;
                       }
                       if ( $c eq $v &&  $c ne "" ) { $config{$c}=$sttaValues{$v} }
                   }
                   if ( $main::config{'tta_dir'} ne "" && $main::config{$c} ne "" ) {
                        $config{$c}=$main::config{$c};  # this is to migrate config file
                   }
              }
              &write_file("$configDir/$nomFile", \%config);
              &unlock_file("$configDir/$nomFile");
        }
        &write_file("$configDir/$configValues", \%sttaValues);
}

1;  # Return true
