#!/usr/bin/perl
# tta_sess.cgi
# Display Tarantella Services Sessions
# STTA Rev. 1.2 Copyright (c) Jesús Pérez Lorenzo  --- license GNU GPL 

 require './tarantella-lib.pl';
if ($access{'sessions'} ne 1 ) { &redirect("index.cgi"); } 
&ReadParse();  
$sort_on=$in{sort_on};
if ( !$sort_on ) { &redirect("sess_filter.cgi"); } 
&foreign_require("proc", "proc-lib.pl");

if ( $debug gt 0 ) {
         $DebugFile="/tmp/log";
         open(DebugLog, ">$DebugFile");
}

&headerstta($text{'index_ttasess'}, "", "ttasess",undef,1,1,$text{headpage_rigth} );
 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 "/******************************************************************\n";
 print "   showValue(formObj)\n";
 print "   Show Password value from form.\n";
 print "*******************************************************************/\n";
 print "function showValue() {\n";
 print "   alert(document.tta_$postform.dn.value);\n";
 print "}\n";
 print "/******************************************************************\n"; 
 print "   set_username(icn,iou,idn)\n"; 
 print "*******************************************************************/\n"; 
 print "function set_username(icn,iou,idn) {\n"; 
 print "    document.tta_$postform.elements[0].value=icn;\n";
 print "    document.tta_$postform.elements[1].value=iou;\n";
 print "    document.tta_$postform.elements[2].value=idn;\n";
 print "    return;\n"; 
 print "}\n"; 
 print "/*******************************************************************\n"; 
print " open_peID(nlin,ob_type,peID,i_name,i_grp,i_value,id_value) \n";
print "*******************************************************************/\n"; 
print "function open_peID(nlin,ob_type,peID,i_name,i_grp,i_value,id_value) {\n";
print "   strquote='\\'';\n";
print "   eval(\"w_name='w_'+peID\");\n";
print "   str=\"w_name=window.open('', 'emulsess_'+peID,'toolbar=no,menubar=no,scrollbars=yes,width=800,height=300,resizable=yes')\";\n";
print "  eval (str) \n";
print "  eval (\"w_name.document.writeln('<body bgcolor=#ffffff link=#0000ee vlink=#0000ee text=#000000>');\")\n";
print "  eval (\"w_name.document.writeln('<table width=100% border>');\")\n";
print "  str='<form METHOD=POST name=\"sess_mgmt\" ACTION=\"sess_mgmt.cgi?ob='\n";
print "  str2='\" onSubmit=\"\">';\n";
print "  eval (\"w_name.document.writeln(str+ob_type+str2);\")\n";
print "  if ( ob_type == 'user' ) { \n";
print "        eval (\"w_name.document.writeln('<tr $tb><td><b> $text{'tit_ttasess'}   '+peID+'  ---  $text{l_ses_user} </b></td></tr>');\")\n";
 print "       eval (\"w_name.document.writeln('<tr $cb><td><table width=100% border>');\")\n";
 print "       eval (\"w_name.document.write('<tr><td>Name </td><td>'+i_name+'</td></tr></b>')\")\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"objname\" VALUE=\"';\n";
 print "       str2='\">';";
print "        eval (\"w_name.document.writeln(str+i_name+str2);\");\n";
 print "       eval (\"w_name.document.writeln('<tr><td>Group </td><td>'+i_grp+'</td></tr></b>')\")\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"grpname\" VALUE=\"';\n";
 print "       str2='\">';\n";
print "        eval (\"w_name.document.writeln(str+i_grp+str2);\");\n";
 print "  }\n"; 
 print "  if ( ob_type == 'app' ) { \n";
print "        eval (\"w_name.document.writeln('<tr $tb><td><b> $text{'tit_ttasess'}   '+peID+' --- $text{l_ses_app} </h> </td></tr>');\")\n";
 print "       eval (\"w_name.document.writeln('<tr $cb><td><table width=100% border>');\")\n";
 print "       eval (\"w_name.document.writeln('<tr><td><b>Name </td><td>'+i_name+'</td></tr></b>')\")\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"objname\" VALUE=\"';\n";
 print "       str2='\">';\n";
print "        eval (\"w_name.document.writeln(str+i_name+str2);\");\n";
 print "       eval (\"w_name.document.writeln('<tr><td><b>Group </td><td>'+i_grp+'</td></tr></b>')\")\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"grpname\" VALUE=\"';\n";
 print "       str2='\">';\n";
print "        eval (\"w_name.document.writeln(str+i_grp+str2);\");\n";
 print "  }\n"; 
 print "  if ( ob_type == 'appserv' ) { \n";
print "        eval (\"w_name.document.writeln('<tr $tb><td><b> $text{'tit_ttasess'}   '+peID+' --- $text{l_ses_appsrv} </b> </td></tr>');\")\n";
 print "       eval (\"w_name.document.writeln('<tr $cb><td><table width=100% border>');\")\n";
 print "       eval (\"w_name.document.writeln('<tr><td><b>Name </td><td>'+i_name+'</td></tr></b>')\")\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"objname\" VALUE=\"';\n";
 print "       str2='\">';\n";
print "        eval (\"w_name.document.writeln(str+i_name+str2);\");\n";
 print "       eval (\"w_name.document.writeln('<tr><td><b>Group </td><td>'+i_grp+'</td></tr></b>')\")\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"grpname\" VALUE=\"';\n";
 print "       str2='\">';\n";
print "        eval (\"w_name.document.writeln(str+i_grp+str2);\");\n";
 print "  }\n"; 
 print "  if ( ob_type == 'pe_id' ) { \n";
print "        eval (\"w_name.document.writeln('<tr $tb><td><b> $text{'tit_ttasess'}   '+peID+' --- $text{l_ses_peid} </b> </td></tr>');\")\n";
 print "       eval (\"w_name.document.writeln('<tr $cb><td><table width=100% border>');\")\n";
 print "       i_value=peID;\n";
 print "       str='<input TYPE=\"hidden\" NAME=\"objvalue\" VALUE=\"'\n";
 print "       str2='\">';\n";
print "        eval (\"w_name.document.writeln(str+i_value+str2);\")\n";
 print "  }\n"; 
 print "   eval (\"w_name.document.writeln('<td><b> $text{'tit_ttasess'} </b> </td>');\")\n";
 print "  str='<td valign=top><input TYPE=\"button\" NAME=\"?\" VALUE=\"?\" onClick=\"javascript:alert(';\n";
 print "  str2=')\">';\n";
 print "  eval (\"w_name.document.writeln(str+strquote+i_value+strquote+str2);\");\n";
 print "  str='<input TYPE=\"hidden\" NAME=\"objvalue\" VALUE=\"'\n";
 print "  str2='\">';\n";
print "   eval (\"w_name.document.writeln(str+i_value+str2);\")\n";
 print "  str='<input TYPE=\"hidden\" NAME=\"id\" VALUE=\"'\n";
 print "  str2='\">';\n";
print "   eval (\"w_name.document.writeln(str+id_value+str2);\")\n";
 print "  str='<input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\">';\n";
 print "  eval (\"w_name.document.writeln(str);\");\n";
 print "  str='<input TYPE=\"button\" NAME=\"",$text{'msg_close'},"\" VALUE=\"",$text{'msg_close'},"\" onClick=\"javascript:self.close()\"></td></tr>';\n";
 print "  eval (\"w_name.document.writeln(str);\");\n";

 print "  eval (\"w_name.document.writeln('</tr></td></table></form></table>');\");\n";
# print "  str=StringClip(true,'$ahora');\n";
# print "  eval (\"w_name.document.writeln(str);\");\n";
 print "  eval (\"w_name.document.close();\");\n";
 print "}\n"; 
 print "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 print "     if ( emptyField(obj.username) == true ) {\n"; 
 print "          alert(obj.username.name+\":\"+msg1+\"->\"+obj.username.value+\"<-\");\n"; 
 print "		    obj.username.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( get_value(\"select\",obj.userou) == \"none\" ) {\n";
 print "          alert(obj.userou.name+\":\"+msg1);\n";
 print "                    obj.userou.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>$text{'titidx_ttasess'} $ahora</b></td> </tr>\n";

 $ahora=`date "+%d-%m-%Y %H:%M:%S"`;    

 $is_reload=true;
 if ( $config{has_cronsess} eq "true" && $config{'tta_sessout_file'} eq "" ) {
      $ahora_str=set_datetime_str($ahora);
      $counter_time="";
      @count_sess=&list_countsess();
      $counter_time_str=set_datetime_str($counter_time);
      $counter_times=get_datetime_str($counter_time_str);

      # If counters are not same hour ... to limit to day use 8
      $limit=10;
      $temp_f=$config{'tta_sessout_file'}; 
      if (substr($counter_time_str,0,$limit) eq substr($ahora_str,0,$limit) ) {
                $is_reload=false;
      }
 }
 if ( $is_reload eq true ) {
      $type_cmd="emulatorsession";
      $no_out=1;
#      &run_tta_cmd($type_cmd);
      $temp_f="/tmp/emu-tta2";
      if ( $config{has_cronsess} eq "true" && $config{'tta_sessout_file'} eq "" ) {
           &lock_file($config{'count_sessout_file'});
           open(TTASESSFILE, ">$config{'count_sessout_file'}");
                   print TTASESSFILE "#datetime: $ahora\n";
                        open(TEMPF, "$temp_f'}");
                        while(<TEMPF>) { print TTASESSFILE $_; }
                   close(TEMPF);
          close (TTASESSFILE);
          &unlock_file($config{'count_sessout_file'});
          unlink($temp_f);
          $str=$text{'user_made'}." TTA SESS List";
          &additional_log('tta_sess.cgi', undef, $str);
          &webmin_log($config{'count_sessout_file'}, undef, "update");
          $temp_f=$config{'tta_sessout_file'};
     }
 }
 &load_tta_sess_info();
 if ( $config{has_cronsess} ne "true" && $is_reload eq true ) {
 #     unlink($temp_f);
 }
if ($#all_ttasess < 0) { 
 print "<TR><TD colspan=4>" . $text{msg_1} . "</td></tr>\n"
 &footHere();
 exit:
}

    print "<tr><td><B>-- $text{'l_orderby'}  ";
    if ($sort_on eq "pe_id") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_peid} . "\n";
    }
    if ($sort_on eq "application") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_app} . "\n";
    }
    if ($sort_on eq "user") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_user} . "\n";
    }
    if ($sort_on eq "startTime") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_starttime} . "\n";
    }
    if ($sort_on eq "resume") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_resume} . "\n";
    }
    if ($sort_on eq "status") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_status} . "\n";
    }
    if ($sort_on eq "applicationServer") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_appsrv} . "\n";
    }
    if ($sort_on eq "client") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_client} . "\n";
    }
    if ($sort_on eq "serverUser") {
       @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
       print $text{l_ses_serveruser} . "\n";
    }
    print "</td></tr>\n";
    print "<tr><td><table width=100%>\n";
    print "</b><TR $tb>\n";
    print "<TD><center><B>".$text{l_ses_peid} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_user} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_client} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_app} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_starttime} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_resume} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_status} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_appsrv} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_serveruser} . "</B></center></TD>\n";
    $lnum=0;

    foreach $ttasess (@ttasess) {
       $UserName=$ttasess->{'user'};
       $AppName=$ttasess->{'application'};
       $HostName=$ttasess->{'applicationServer'};
       &scan_envsess();
       &alias_envsess();
       if ( $is_reload eq true ) { &setcount_envsess(); }
       $pe_ID=$ttasess->{'pe_id'};
       print "<TR $cb>\n"; 
#      print "<TD>" . $ttasess->{'id'}."</TD>\n"; 
       print "<TD><A href=\"javascript:".
                open_peID($lnum,'pe_id','$pe_ID','','','".$ttasess->{'id'}.
                           "','".$ttasess->{'id'}."')\">";
       print "$pe_ID</A></TD>\n"; 
       print "<TD><A href=\"javascript:".
                open_peID($lnum,'user','$pe_ID','$NameUser','$OuName','".
                          $ttasess->{'user'}."','".$ttasess->{'id'}."')\">";
       print "$NameUser ($OuName) $userType</A></TD>\n";
       print "<TD>" . $ttasess->{'client'}."</TD>\n"; 
       print "<TD><A href=\"javascript: ".
                open_peID($lnum,'app','$pe_ID','$NameApp','$NameAppGrp','".
                          $ttasess->{'application'}."','".$ttasess->{'id'}."')\">";
       print "$NameApp";
       if ( $NameAppGrp.length ne 0 ) { print " ($NameAppGrp)"; }
       print "</A></TD>\n";
       print "<TD>" . $ttasess->{'startTime'}."</TD>\n"; 
       print "<TD>" . $ttasess->{'resume'}."</TD>\n"; 
       print "<TD>" . $ttasess->{'status'}."</TD>\n"; 
       print "<TD><A href=\"javascript: ".
                open_peID($lnum,'appserv','$pe_ID','$NameHost','$NameHostGrp','".
                    $ttasess->{'applicationServer'}."','".$ttasess->{'id'}."')\">";
       print "$NameHost";
       if ( $NameHostGrp.length ne 0 ) { print " ($NameHostGrp)"; }
       print "</A></TD>\n";
       print "<TD>" . $ttasess->{'serverUser'}."</TD>\n"; 
       print "<\TR>\n"; 
       if ( $debug gt 0 ) { &print_emulses(); }
       $lnum++;
    }
    if ( $debug gt 0 ) { close (DebugLog); }

    print "<p></table>\n"; 
    print "<TR><TD><B>".$text{l_ses_count} . "</B></TD><TR>\n";
    print "</td></tr>\n";
    print "<tr><td><table width=50%>\n";
    print "</b><TR $tb>\n";
    print "<TD><center><B>".$text{l_ses_object} . "</B></center></TD>\n";
    print "<TD><center><B>".$text{l_ses_num} . "</B></center></TD>\n";
    if ( $debug eq 2 ) {
            print "<TD><center><B> obj </B></center></TD>\n";
            print "<TD><center><B> objalias </B></center></TD>\n";
            print "<TD><center><B> alias </B></center></TD>\n";
            print "<TD><center><B> maxsess </B></center></TD>\n";
            print "<TD><center><B> objgrpalias </B></center></TD>\n";
            print "<TD><center><B> grpalias </B></center></TD>\n";
            print "<TD><center><B> name </B></center></TD>\n";
    }
    if ( $is_reload eq true ) { &sum_grp_sess();};
    foreach $countsess (@count_sess) {
       $objdescrip=$countsess->{'descrip'}; 
       print "<TR $cb>\n";
       print "<TD><A href=\"javascript: alert('".$countsess->{'name'};
       if ( $objdescrip ne $countsess->{'alias'} ) { print " (".$objdescrip.")"; }
       print "')\">";
       $objdescrip=~s/ou=//g;     
       $objdescrip=~s/cn=//g;     
       print $objdescrip;
       print "</A></TD>\n";
       print "<TD>" . $countsess->{'numsess'}."</TD>\n";
       if ( $debug eq 2 ) {
            print "<TD>" . $countsess->{'obj'}."</TD>\n";
            print "<TD>" . $countsess->{'objalias'}."</TD>\n";
            print "<TD>" . $countsess->{'alias'}."</TD>\n";
            print "<TD>" . $countsess->{'maxsess'}."</TD>\n";
            print "<TD>" . $countsess->{'objgrpalias'}."</TD>\n";
            print "<TD>" . $countsess->{'grpalias'}."</TD>\n";
            print "<TD>" . $countsess->{'name'}."</TD>\n";
       }
       print "<\TR>\n";
    }
 print "<p></table>\n"; 
 print "<p></table>\n"; 
 print "<tr>\n";
 print "<tr>\n";
 if ( $is_reload eq true ) { 
      if ( $config{has_cronsess} eq "true" && $config{'count_sessout_file'} ne "" ) {
           &write_countsess();
           print "<tr><p><b> $text{'msg_count_update'} $ahora </b></tr>\n";  
      }
 } else {
      print "<tr><p><b> $text{'msg_count_update'} $counter_time </b></tr>\n";  
 }
 print "<tr>\n";
 print "</table></table><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;



