#!/usr/bin/perl
# modfile.cgi
# update file for Tarantella Services

require './tarantella-lib.pl';
&ReadParse();
if ( ! $access{'supervision'} )   { &redirect("index.cgi"); }
$postform = ($in{postform}) ? $in{postform} : "modfile";
$cgiPost = ($in{cgiPost}) ? $in{cgiPost} : "file";
$cgiName = ($in{cgiName}) ? $in{cgiName} : "file_manager.cgi";
$wkdir = ($in{wkdir}) ? $in{wkdir} : eval("\$config{'$postform"."_path'}");
if ( ! $in{fName} )   { &redirect("index.cgi"); }
if ( $postform eq "filemgr" ) { $postform="modfile"; }  
$filePerm= $in{'filePerm'};  
$fName=$in{fName};
$cmd=$in{cmd};
if ( $in{fPerm} ) {
     if ( substr($in{fPerm},0,1) ne "-" ) {
        @l_target = split(/,/,$text{'f_perm_value'}, -1);
        @l_target_tit = split(/,/,$text{'f_perm_desc'}, -1);
        $nItems=0;
        foreach $item (@l_target) {
                if ( $item eq $in{fPerm} ) {
                     $fMode=$l_target_tit[$nItems];
                     $fPerm=substr($fMode,2,1);
                     last;
                }
                print  $l_target_tit{$nItems};
                $nItems++;
        }
     } else {
        $fMode=$in{fPerm};
        $fPerm=substr($fMode,2,1);
     }
}
$perm=$in{perm};
if ( $perm ne "new" && substr($in{fPerm},1,3) eq "rwx" ) { $perm="all"; }

&header(eval("\$text{'index_$postform'}"), "", $postform ); 

print "<table width=100% border>\n";
print "<tr $tb> <td><b>";
if ( $postform ne "modfile" ) { print $text{'modify'}; }
print " ".eval("\$text{'titidx_$postform'}")."</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";
if ( $fPerm ne "w" ) {
    print "<tr><td>";
    print &text('file_err',$text{'modify'},$wkdir,$fName,$fMode);
    print "</td></tr>\n";
    &footHere;
    exit 1;
} 
 $lnum = 0;
 $f_path=$wkdir."/".$fName;
 $in{'f_lines'} =~ s/\r//g;
 &lock_file($f_path);
 open(FILELIST, ">$f_path");
 print FILELIST $in{'f_lines'};
 close(FILELIST);
 &unlock_file($f_path);
 &webmin_log("$text{modify} ($perm) ","$f_path $fMode", $config{run_exec_user}, "");
 chmod(oct($filePerm), $f_path); 
 &webmin_log("$text{modify} ($filePerm) ","$f_path $fMode", $config{run_exec_user}, "");
 print "</tr></td>\n";
 print "<tr><td><b>$text{'user_update'} <b><i>$wkdir</i>/<b>$fName</b> $filePerm\n";
 if ( $debug gt 0 ) { print " &nbsp;&nbsp; ($fMode) ";}
 print " </td></tr>\n";
 print "</left><tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
