#!/usr/bin/perl
# runfile.cgi
# RUN file for Services with Tarantella
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

require './stta-lib.pl';
&foreign_require("proc", "proc-lib.pl");    
&ReadParse();
if ( ! $access{'supervision'} )   { &redirect("index.cgi"); }
$postform = ($in{postform}) ? $in{postform} : "runfile";
$cgiPost = ($in{cgiPost}) ? $in{cgiPost} : "file";
$cgiName = ($in{cgiName}) ? $in{cgiName} : "runfile.cgi";
$wkdir = ($in{wkdir}) ? $in{wkdir} : eval("\$config{'$postform"."_path'}");
if ( ! $in{fName} )   { &redirect("index.cgi"); }
if ( $postform eq "filemgr" || $postform eq "modfile" ) { $postform="runfile"; }  
$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,3,1);
                     last;
                }
                print  $l_target_tit{$nItems};
                $nItems++;
        }
     } else {
        $fMode=$in{fPerm};
        $fPerm=substr($fMode,3,1);
     }
}
$perm=$in{perm};
if ( $perm ne "new" && substr($in{fPerm},1,3) eq "rwx" ) { $perm="all"; }
&headerstta(eval("\$text{'index_$postform'}"), "", $postform ,undef,1,1,$text{headpage_rigth} );

print "<table width=100% border>\n";
print "<tr $tb> <td><b>";
if ( $postform ne "runfile" ) { print $text{'execute'}; }
print " ".eval("\$text{'titidx_$postform'}")."</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

if ( $fPerm ne "x" ) {
    print "<tr><td>$fPerm</td></tr>";
    print &text('file_err',$text{'execute'},$wkdir,$fName,$fMode); 
    print "</td></tr>\n";
    &footHere;
    exit 1;
} 
$f_path=$wkdir."/".$fName;
if (  -x $f_path ) {
      print "<tr><td><b>$text{'execute'} </b><i>$wkdir</i>/<b>$fName</b>\n";
      if ( $debug gt 0 ) { print " &nbsp;&nbsp; ($fMode) ";}
      $str=$f_path;
      $exec_str="$text{execute} ($perm)";
      $cmd_str="$f_path $fMode";
      if (&run_cmd($str)) {
          print "<tr><td><center><b>-- oOo --</b></center></td></tr>\n";
      } else {
          print "<tr><td><b>$text{'index_cmderr'}</b></td></tr>\n";
      }
    print "<tr><td>";
 } else {
    print &text('file_errerun',$wkdir,$fName); 
 }
 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;
