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

require './stta-lib.pl';
&ReadParse();
if ( ! $access{'supervision'} )   { &redirect("index.cgi"); }
$postform = ($in{postform}) ? $in{postform} : "filemgr";
$cgiPost = ($in{cgiPost}) ? $in{cgiPost} : "file";
$cgiName = ($in{cgiName}) ? $in{cgiName} : "file_manager.cgi";
$sort_on = ($in{sort_on}) ? $in{sort_on} : "default";
$ls_match = ($in{ls_cmd}) ? $in{ls_cmd} : " ";
$perm = ($in{perm}) ? $in{perm} : "read";
$wkdir = ($in{wkdir}) ? $in{wkdir} : eval("\$config{'$postform"."_path'}");
&headerstta(eval("\$text{'index_$postform'}"), "", $postform ,undef,1,1,$text{headpage_rigth} );  
 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(iname,iperms)\n"; 
 print "*******************************************************************/\n"; 
 print "function set_filelistName(iname,iperms) {\n"; 
 print "    document.file_$postform.elements[0].value=iname;\n";
 print "    document.file_$postform.elements[1].value=iperms;\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[2].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=\"".$cgiPost."_mod.cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"run\" ) {;\n";
 print "       document.file_$postform.action=\"run".$cgiPost.".cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"clean\" ) {;\n";
 print "       document.file_$postform.action=\"".$cgiPost."_clean.cgi\";\n";
 print "    }\n";
 print "    if (cmd ==  \"showlist\" ) {;\n";
 print "       document.file_$postform.action=\"".$cgiPost."_showlist.cgi\";\n";
 print "    }\n";
 print "    return;\n";
 print "}\n";
 print "//-->\n";
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_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><b>";
if ( $postform ne "filemgr" ) { print $text{'select'}; }
print " ".eval("\$text{'titidx_$postform'}")."</b></td> </tr>\n";

if (  ! -d $wkdir || $wkdir eq "" ) {
    print "<tr><td>";
    print &text('file_errdir',$wkdir);
    print "</td></tr>";
    &footHere;
    exit 1;
 }
    %h_files=&list_files($wkdir,$ls_match);
    foreach $item (keys (%h_files)) { push (@all_filelists, $h_files{$item}); }  
    print "<tr><td><B> $wkdir </B> -- <i>$text{'l_orderby'} </i>  ";
    if ($sort_on eq "name") {
       @filelists = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_filelists;
       print $text{f_fname} . "\n";
    }
    elsif ($sort_on eq "size") {
       @filelists = sort {$a->{$sort_on} <=> $b->{$sort_on}} @all_filelists;
       print $text{f_fsize} . "\n";
    }
    elsif ($sort_on eq "day") {
       @filelists = sort {$a->{$sort_on} <=> $b->{$sort_on}} @all_filelists;
       print $text{f_fday} . "\n";
    }
    elsif ($sort_on eq "month") {
       @filelists = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_filelists;
       print $text{f_fmonth} . "\n";
    }
    elsif ($sort_on eq "time") {
       @filelists = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_filelists;
       print $text{f_ftime} . "\n";
    }
    elsif ($sort_on eq "owner") {
       @filelists = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_filelists;
       print $text{f_fowner} . "\n";
    }
    elsif ($sort_on eq "group") {
       @filelists = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_filelists;
       print $text{f_fgroup} . "\n";
    }
    elsif ($sort_on eq "perm") {
       @filelists = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_filelists;
       print $text{f_fperm} . "\n";
    }
    elsif ($sort_on eq "default") {
       @filelists = @all_filelists;
       print $text{f_fdefault} . "\n";
    }
    print "</td></tr><tr> <td><table width=100%>\n";
    print "<tr><td>\n";

    print "</b><TR $tb><TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=name&postform=$postform&perm=$perm\">";
    print $text{f_fname}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=size&postform=$postform&perm=$perm\">";
    print $text{f_fsize}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=day&postform=$postform&perm=$perm\">";
    print $text{f_fday}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=month&postform=$postform&perm=$perm\">";
    print $text{f_fmonth}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=time&postform=$postform&perm=$perm\">";
    print $text{f_ftime}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=owner&postform=$postform&perm=$perm\">";
    print $text{f_fowner}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=group&postform=$postform&perm=$perm\">";
    print $text{f_fgroup}."</A></B></center></TD>\n";
    print "<TD><center><B>";
    print "<A href=\"".$cgiName."?wkdir=$wkdir&sort_on=perm&postform=$postform&perm=$perm\">";
    print $text{f_fperm}."</A></B></center></TD>\n";

    $lnum++;
    foreach $filelist (@filelists) {
        $fName=$filelist->{name};
        next if ( $fName eq "" || $fname eq "." ||  $fname eq ".." );
        $fPerm=$filelist->{perm};
        print "<TR $cb><TD>";
        if ( $filelist->{perm} =~ /^d/) { 
             print "<A href=\"".$cgiName."?wkdir=$wkdir/$fName&sort_on=$sort_on&postform=$postform&perm=$perm";
        } else {
             print "<A href=\"javascript:set_filelistName('".$fName."','".$fPerm."')";
        }
        print "\">".$filelist->{name}."</A></TD>";
        print "<TD>" . $filelist->{size}."</TD>";
        print "<TD>" . $filelist->{day}."</TD>";
        print "<TD>" . $filelist->{month}."</TD>";
        print "<TD>" . $filelist->{time}."</TD>";
        print "<TD>" . $filelist->{owner}."</TD>";
        print "<TD>" . $filelist->{group}."</TD>";
        print "<TD>" . $filelist->{perm}."</TD>";
        print "\n";
        $lnum++;
    }
    print "<tr><tr></table></table><hr><p>\n";
    print "<left><form METHOD=POST name=file_".$postform." ACTION=\"file_".$postform.".cgi\" onSubmit=\"return Chek_fields(this)\">\n"; 
    print "  <td valign=top>",&hlink("<b>$text{'f_filelistName'}</b>","ffilelistName"),"&nbsp;</td>\n";
    print "  <td valign=top><input TYPE=\"text\" NAME=\"fName\" VALUE=\"\" cols=\"30\" size=\"30\" ALIGN=left>\n";
#, &file_chooser_button("fName",0,0,$wkdir);
    print "  <input TYPE=\"hidden\" NAME=\"fPerm\" VALUE=\"$fPerm\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"cmd\" VALUE=\"",$cmd,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"$postform\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"wkdir\" VALUE=\"$wkdir\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"perm\" VALUE=\"$perm\">\n";
    print "  <input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"showValue();\" ></td></tr>\n";
    print "   <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'edit_run'},"\" VALUE=\"",$text{'edit_run'},"\" onClick=\"set_cmd('run')\"></td>\n";
    print "        <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'modify'},"\" VALUE=\"",$text{'modify'},"\" onClick=\"set_cmd('modify')\">\n";
    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 ( $postform eq "dirdefstta") {
        print "        <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'showlist'},"\" VALUE=\"",$text{'showlist'},"\" onClick=\"set_cmd('showlist')\"></td>\n";
    }
    print " <td valign=top><input TYPE=\"submit\" NAME=\"",$text{'clean'},"\" VALUE=\"",$text{'clean'},"\" onClick=\"set_cmd('clean')\"></td>\n";
    print "	</tr></form></td></tr>\n"; 
    print "<tr><td><b>".&text(l_total,$lnum)."</b><i>$wkdir</i></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;




