#!/usr/bin/perl
# file_mod.cgi
# Select file from list files for 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 './stta-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_mod.cgi";
$wkdir = ($in{wkdir}) ? $in{wkdir} : eval("\$config{'$postform"."_path'}");
if ( ! $in{fName} && ! $in{kf})   { &redirect("file_manager.cgi"); }
if ( $postform eq "filemgr" ) { $postform="modfile"; }
$cmd=$in{cmd} || "";
$kf=$in{kf};
if ( $kf ) { 
       eval ("\$kfName=\$config{$kf.\"_name\"}"); 
       if ( $kfName ) {
             eval ("\$kfPerm=\$config{$kf.\"_fperm\"}"); 
             eval ("\$kperm=\$config{$kf.\"_perm\"}"); 
             eval ("\$ktail=\$config{$kf.\"_tail\"}"); 
       }
}
$fName=$kfName || $in{fName};
$fPerm=$kfPerm || $in{fPerm} || "0600";
$perm=$kperm || $in{perm} || "read";
$tail= $ktail || $in{tail} || $config{filemgr_tail} || 0;
$NrowsArea=$config{filemgr_rows} || "22";
$NcolsArea=$config{filemgr_cols} || "130";

&headerstta(eval("\$text{'index_$postform'}"), "", $postform ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );
if ( $fPerm ) {
     if ( substr($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 $fPerm ) {
                     $fMode=$l_target_tit[$nItems];
                     $fPerm=substr($fMode,2,1);
                     last; 
                }
                print  $l_target_tit{$nItems};
     		$nItems++;
	}
     } else {
        $fMode=$fPerm;
        $fPerm=substr($fMode,2,1);
     }
}
if ( $perm ne "new" && substr($fPerm,1,3) eq "rwx" ) { $perm="all"; } 
#if ( $tail ne 0 ) { $perm="read"; }

if ( $perm ne "read" && $fPerm eq "w" ) {
 print "<hr>\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 ask_confpassmon=true;\n"; 
 print "/******************************************************************\n";
 print "   showValue(formObj)\n";
 print "   Show Password value from form.\n";
 print "*******************************************************************/\n";
 print "function showValue() {\n";
 print "   alert(document.file_$postform.fName.value);\n";
 print "}\n";
 print "/******************************************************************\n"; 
 print "   set_filelistName(ialias)\n"; 
 print "*******************************************************************/\n"; 
 print "function set_filelistName(iname) {\n"; 
 print "    document.file_$postform.elements[0].value=iname;\n";
 print "    return;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "/******************************************************************\n";
 print "   set_cmd(cmd)\n";
 print "*******************************************************************/\n";
 print "function set_cmd(cmd) {\n";
 print "    document.file_$postform.elements[1].value=cmd;\n";
 print "    if (cmd ==  \"delete\" ) {;\n";
 print "       document.file_$postform.action=\"".$cgiPost."_del.cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"modify\" ) {;\n";
 print "       document.file_$postform.action=\"mod".$cgiPost.".cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"run\" ) {;\n";
 print "       document.file_$postform.action=\"run".$cgiPost.".cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"new\" ) {;\n";
 print "       document.file_$postform.action=\"add".$cgiPost.".cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"clean\" ) {;\n";
 print "       document.file_$postform.action=\"".$cgiPost."_clean.cgi\";\n";
 print "    }\n";
 print "    return;\n";
 print "}\n";
 print "//-->\n";
 print "/******************************************************************\n"; 
 print "   Check_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Check_fields(obj) {\n"; 
 print "     if ( emptyField(obj.fName) == true ) {\n"; 
 print "          alert(obj.fName.name+\":\"+msg1+\"->\"+obj.fName.value+\"<-\");\n"; 
 print "		    obj.fName.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
}

print "<table width=100% border>\n";
print "<tr $tb> <td><font color='$revtext'><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 ( $perm ne "read" && $fPerm eq "w" ) {
    print "<tr><td><left><form METHOD=POST name=file_".$postform." ACTION=\"".$cgiPost.".cgi\" onSubmit=\"return Check_fields(this)\">\n"; 
    print "<center><textarea name=\"f_lines\" rows=\"$NrowsArea\" cols=\"$NcolsArea\" wrap=\"virtual\">"; 
} elsif ( $perm eq "read" || $fPerm ne "w" ) {
    print "<tr><td><pre>\n"; 
}
 $lnum = 0;
 $f_path=$wkdir."/".$fName;
 if ( $tail ne 0 ) { 
      $nlines=0;
      open(FILELIST, $f_path);
          while(<FILELIST>) { $nlines++; }
      close(FILELIST);
      $tailLine=$nlines-$tail-1;
 }
 open(FILELIST, $f_path);
        while(<FILELIST>) {
                s/\r|\n//g;
                $lnum++;
                if ( $tail eq 0 || $nlines < $tail ||  $lnum > $tailLine ) { print "$_\n";}
        }
 close(FILELIST);
 if ( $perm eq "read" || $fPerm ne "w" ) { print "</pre>\n"; }
 if ( $perm ne "read"|| $fPerm eq "w" ) { print "</textarea>\n"; }
 if ( $tail ne 0 ) { 
      &webmin_log("$text{tail} ($tail) ","file","$f_path $fMode"); 
 } else {
      &webmin_log("$text{view} ($perm) ","file","$f_path $fMode"); 
 }

 print "</tr></td>\n";
 print "<tr><td><i>$wkdir</i>/<b>$fName</b>\n";
 print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
 if ( $tail ne 0 ) { print "  ($text{tail} <b>$tail</b>)&nbsp;&nbsp;&nbsp;&nbsp;  "; }
 print "  <i>".&text(l_total,$lnum)."</i> ";
 if ( $debug gt 0 ) { print " &nbsp;&nbsp; ($fMode) ";}
 print " </td>\n";
 print "</table></td></tr>\n";
 if ( $perm ne "read" ) {
    print "   <tr $cb> <td><table width=100%>\n";
    print "  <input TYPE=\"hidden\" NAME=\"cmd\" VALUE=\"",$cmd,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"perm\" VALUE=\"$perm\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"$postform\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"wkdir\" VALUE=\"$wkdir\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"fPerm\" VALUE=\"$fMode\">\n";
    print "  <tr><td valign=top>",&hlink("<b>$text{'f_filelistName'}</b>","ffilelistName"),"&nbsp;</td>\n";
    if ( $perm eq "new" ) {
        if ( $fName eq "." ) { $fName="" ;}
        print "  <td valign=top><input TYPE=\"text\" NAME=\"fName\" VALUE=\"$fName\" cols=\"30\" size=\"30\" ALIGN=left></td></tr>\n";
        print "  <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'create'},"\" VALUE=\"",$text{'create'},"\" onClick=\"set_cmd('new')\" ></td>\n";
    } else {
        print "  <input TYPE=\"hidden\" NAME=\"fName\" VALUE=\"$fName\">\n";
        print "  <td valign=top><b>$fName</b></td></tr>\n";
    }
    print "  <tr><td valign=top>",&hlink("<b>$text{'f_fperm'}</b>","ffperm"),"&nbsp;</td>\n";
    print "  <td valign=top><select NAME=\"filePerm\">\n";
        print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_perm_sel'}</b>","fperm_sel"),"\n";
   @l_target = split(/,/,$text{'f_perm_value'}, -1);
   @l_target_tit = split(/,/,$text{'f_perm_desc'}, -1);
   $nItems=0;
   foreach $item (@l_target) {
   	print "         <option ";
        if ( $fMode eq $l_target_tit[$nItems] ) { print "selected " ; }
        print "VALUE=\"$item\" ";
     	print ">$l_target_tit[$nItems]</b>\n";
     	$nItems++;
    }
    print "        </select></td></tr>\n";
    if ( $perm ne "new" ) {
        if ( $perm eq "run" || $perm eq "all" ) {
             print " <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'edit_run'},"\" VALUE=\"",$text{'edit_run'},"\" onClick=\"set_cmd('run')\"></td>\n";
        }
        if ( $perm eq "modify"|| $perm eq "all"  || $fPerm eq "w" ) {
             print "  <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'modify'},"\" VALUE=\"",$text{'modify'},"\" onClick=\"set_cmd('modify')\">\n";
        }
        if ( $perm eq "delete" || $perm eq "all" || $fPerm eq "w" ) {
             print "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
             print " <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'delete'},"\" VALUE=\"",$text{'delete'},"\" onClick=\"set_cmd('delete')\"></td>\n";
        }
        if ( $perm eq "clean" || $perm eq "all" || $fPerm eq "w" ) {
             print " <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'clean'},"\" VALUE=\"",$text{'clean'},"\" onClick=\"set_cmd('clean')\"></td>\n";
        }
   }
   print "     </tr></form></td></tr></table>\n";
}
 
#    print "<tr><td><b> $wkdir</b></td></tr>\n";
 
 print "</left></td></tr></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;


