#!/usr/bin/perl
# log_parser.pl
# Functions for parsing this module's logs
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 
# debug=4 is for developers and will dump object-types vars !!!

 $parse_webmin_log=true;
 require 'stta-lib.pl';
use Net::LDAP::Util qw(ldap_error_name ldap_error_text);

 
# parse_webmin_log(user, script, action, type, object, &params)
# Converts logged information from this module into human-readable form
sub parse_webmin_log
{
    local ($user, $script, $action, $type, $object, $p) = @_;
    local $cginame=$0;
    local $text_str;
    local $html_str;
    $cginame= $0 =~ /([^\/]+)$/ ? $1 : '-';    
    $debug=4;  # Just uncoment here and bypass $config{debug}
    local %h_obj;
    local @line, $line_str, $LDAP_msgerr;

    if ( $cginame eq "view.cgi" ) {
         # This is need to fix parse log for STTA  and avoid webmin master changes
         # In STTA action, type and object are always among quotes ....
         open(LOG, $webmin_logfile);
         while(<LOG>) {
              @line=split(/ /, $_, -1);               
              if ( $line[0] eq $main::in{'id'}) {
                   $line_str=$_;
                   last;
              }
         }
         close(LOG);
         if ( $line_str ne "" ) {
              @line=split(/ "/, $line_str, -1);               
              if ( $line[1] ne "" ) { ${action} = $line[1]; }
              if ( $line[2] ne "" ) { ${type} = $line[2]; }
              if ( $line[3] ne "" ) { 
                   ${object} = $line[3];
                   @line=split(/"/, ${object}, -1);               
                   ${object} = $line[0];
              }
              ${action}=~s/"//g;
              ${type}=~s/"//g;
         }
         if ( ${object} =~ /\|\|/ ) {
              local @obj= split(/\|\|/, ${object}, -1);               
              foreach $item (@obj) {
                      local @objdata= split(/=>/, $item, -1);               
                      if ( ${action} =~ /^tta/ && $objdata[0] eq "cmd" ) { 
                          $objdata[1] =~ s/\\\\/<nl>/g;
                          $objdata[1] =~ s/\\/<br>/g;
                          $objdata[1] =~ s/<nl>/\\<br>/g;
                          $objdata[1] =~ s/'/"/g;
                      }
                      if (defined($h_obj{$objdata[0]}) ) {
                          $h_obj{$objdata[0]} .= ", ".$objdata[1];
                      } else {
                          $h_obj{$objdata[0]} = $objdata[1];
                      }
              }
              if ( $obj[0] gt 0 ) {
                  if ( ${action} =~ /^tta/ ) { 
                       $h_obj{$obj[0]}="<pre>$text{f_error}:$text{'msgerr_tta'}</pre>";
                  } else {
                       $LDAP_msgerr.=&ldap_error_text($obj[0]);
                       $LDAP_msgerr=~s/\n/<br>/g;
                       $h_obj{$obj[0]}="<pre>$LDAP_msgerr</pre>";
                  }
              } elsif ($obj[0] eq 0 ) {$h_obj{$obj[0]}="<pre>$text{'msgerr_0'}</pre>"}
         };
         $text_str=&text("log_stta", "<tt><b>$texto</b></tt><br>");
         if ($debug gt 1  && %{$p} ) { 
             $html_str=${action};
             if ( ! %h_obj ) { $html_str=" <b>${type}</b> <i>${object}</i>"; }
             $html_str=&html_objlink(${type},$html_str,\%{$p},${action}); 
             $html_str=~s/\0/, /g; #webminlog-lib.pl in &parse_logline
                                   # will add "\0" in a multivalue field
			           # it will crash javascript:window.document.writeln()
         } else {
             $html_str=" ${action}";
             $html_str.=" <b>${type}</b>"; 
             if ( ! %h_obj ) { $html_str.=" <i>${object}</i>"; }
         }
         if ( %h_obj ) {
                  &show_hash(\%h_obj,"","=","",\$text_str);
                  $text_str=~s/'//g;
                  $text_str=~s/\n/<\/tr><tr><td>/g;
                  $text_str=~s/===/:<\/td><td>/g;
                  $html_str.="<table width=100% border>";
                  $html_str.="<tr><td>$text_str</td></tr></table>";
         }
    }
    if ( $cginame eq "search.cgi" ) {
         $html_str=${script}.":".${action}." ".${type};
    }
    return "<tt>$html_str</tt>";
}
1;  # Return true
