# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]
# web-lib.pl
# changes to manage webminlog sid 

$stta_sid="";
$querySTR="";

# webmin_log(action, type, object, &params, [module])
# Log some action taken by a user
sub webmin_log
{
return if (!$gconfig{'log'});
local $m = $_[4] ? $_[4] : $module_name;

if ($gconfig{'logclear'}) {
	# check if it is time to clear the log
	local @st = stat("$webmin_logfile.time");
	local $write_logtime = 0;
	if (@st) {
		if ($st[9]+$gconfig{'logtime'}*60*60 < time()) {
			# clear logfile and all diff files
			system("rm -f $ENV{'WEBMIN_VAR'}/diffs/* 2>/dev/null");
			unlink($webmin_logfile);
			$write_logtime = 1;
			}
		}
	else { $write_logtime = 1; }
	if ($write_logtime) {
		open(LOGTIME, ">$webmin_logfile.time");
		print LOGTIME time(),"\n";
		close(LOGTIME);
		}
	}

# should logging be done at all?
return if ($gconfig{'logusers'} && &indexof($base_remote_user,
	   split(/\s+/, $gconfig{'logusers'})) < 0);
return if ($gconfig{'logmodules'} && &indexof($m,
	   split(/\s+/, $gconfig{'logmodules'})) < 0);

# log the action
local $now = time();
local @tm = localtime($now);
local $script_name = $0 =~ /([^\/]+)$/ ? $1 : '-';
local $id = sprintf "%d.%d.%d",
		$now, $$, $main::action_id_count;
$stta_sid=$id;

$main::action_id_count++;
local $line = sprintf "%s [%2.2d/%s/%4.4d %2.2d:%2.2d:%2.2d] %s %s %s %s %s \"%s\" \"%s\" \"%s\"",
	$id, $tm[3], $text{"smonth_".($tm[4]+1)}, $tm[5]+1900,
	$tm[2], $tm[1], $tm[0],
	$remote_user, $main::session_id ? $main::session_id : '-',
	$ENV{'REMOTE_HOST'},
	$m, $script_name,
	$_[0], $_[1] ne '' ? $_[1] : '-', $_[2] ne '' ? $_[2] : '-';
foreach $k (sort { $a cmp $b } keys %{$_[3]}) {
	local $v = $_[3]->{$k};
	if ($v eq '') {
		$line .= " $k=''";
		}
	elsif (ref($v) eq 'ARRAY') {
		foreach $vv (@$v) {
			next if (ref($vv));
			$vv =~ s/(['"\\\r\n\t\%])/sprintf("%%%2.2X",ord($1))/ge;
			$line .= " $k='$vv'";
			}
		}
	elsif (!ref($v)) {
		foreach $vv (split(/\0/, $v)) {
			$vv =~ s/(['"\\\r\n\t\%])/sprintf("%%%2.2X",ord($1))/ge;
			$line .= " $k='$vv'";
			}
		}
	}
open(WEBMINLOG, ">>$webmin_logfile");
print WEBMINLOG $line,"\n";
close(WEBMINLOG);

if ($gconfig{'logfiles'}) {
	# Find and record the changes made to any locked files
	local $i = 0;
	mkdir("$ENV{'WEBMIN_VAR'}/diffs", 0700);
	foreach $d (@main::locked_file_diff) {
		open(DIFFLOG, ">$ENV{'WEBMIN_VAR'}/diffs/$id.$i");
		print DIFFLOG "$d->{'type'} $d->{'object'}\n";
		print DIFFLOG $d->{'data'};
		close(DIFFLOG);
		$i++;
		}
	@main::locked_file_diff = undef;
	}
}
# hlink(text, page, [module], [css-class])
sub hlink
{
local $mod = $_[2] ? $_[2] : $module_name;
local $cssClass = $_[3] || "menu1";
local $cgi_obj=$cginame;
local $item=$_[1]; 
local $f_item=$_[1]; 

      $cgi_obj=~ s/.cgi//;
      if ( $cgi_obj eq "" ) { $cgi_obj="help" }
      if ( $querySTR  && $querySTR ne "" ) { $cgi_obj=$querySTR }
  
  if ( $config{help_active} eq 1 || $config{help_active} eq true ) {
       if ( $cginame =~ /stta_webhelp/ &&  $langSel ne "" ) {
	      if ( $item =~ /#/ ) { 
                     local @lin=split(/#/, $item); $item =~ s/$lin[0]#/$lin[0].html#/g;
              } else {
                     $item .=".html"; 
              }
       }
       
       if ( defined($URLBase) && $URLBase ne "" ) { 
            return "<a onClick='window.open(\"$URLBase/$item?$cgi_obj\", \"$cgi_obj\", \"toolbar=no,menubar=no,scrollbars=yes,width=$config{help_width},height=$config{help_height},resizable=yes\"); return false' class='$cssClass' href=\"$URLBase/$item?$cgi_obj\">$_[0]</a>";
       } else {
       	    $f_item=$item;
       	    if ( $f_item =~ /#/ ) { local @lin=split(/#/, $f_item); $f_item=$lin[0]; }
       	    if ( $f_item =~ /\?/ ) { local @lin=split(/\?/, $f_item); $f_item=$lin[0]; }
       	    if ( $f_item =~ /^config\// ) { $f_item=$current_lang."/".$f_item; }
            if ( -r "$root_directory/$mod/help/$f_item.html" ) {
                  return "<a onClick='window.open(\"$gconfig{'webprefix'}/$mod/help.cgi/$mod/$item?$cgi_obj\", \"$cgi_obj\", \"toolbar=no,menubar=no,scrollbars=yes,width=$config{help_width},height=$config{help_height},resizable=yes\"); return false' class='$cssClass' href=\"$gconfig{'webprefix'}/$mod/help.cgi/$mod/$item?$cgi_obj\">$_[0]</a>";
            } else { return "$_[0]"; }
       }
  } else {
       return "$_[0]";
  }
}
# footer([page, name]+, [noendbody])
# Output a footer for returning to some page
sub footer
{
&load_theme_library();
if (defined(&theme_footer)) {
	&theme_footer(@_);
	return;
	}
local $i;
for($i=0; $i+1<@_; $i+=2) {
	local $url = $_[$i];
	if ($url ne '/' || !$tconfig{'noindex'}) {
		if ($url eq '/') {
			$url = "/?cat=$module_info{'category'}";
			}
		elsif ($url eq '' && $module_name) {
			$url = "/$module_name/";
			}
		elsif ($url =~ /^\?/ && $module_name) {
			$url = "/$module_name/$url";
			}
		$url = "$gconfig{'webprefix'}$url" if ($url =~ /^\//);
		if ($i == 0) {
			print "<a href=\"$url\"><img alt=\"<-\" align=middle border=0 src=$gconfig{'webprefix'}/images/left.gif></a>\n";
			}
		else {
			print "&nbsp;|\n";
			}
		print "&nbsp;<a href=\"$url\">",&text('main_return', $_[$i+1]),"</a>\n";
		}
	}
print "<br>\n";
if (!$_[$i]) {
	local $postbody = $tconfig{'postbody'};
	if ($postbody) {
		local $hostname = &get_system_hostname();
		local $version = &get_webmin_version();
		local $os_type = $gconfig{'real_os_type'} ?
				$gconfig{'real_os_type'} : $gconfig{'os_type'};
		local $os_version = $gconfig{'real_os_version'} ?
				$gconfig{'real_os_version'} : $gconfig{'os_version'};
		$postbody =~ s/%HOSTNAME%/$hostname/g;
		$postbody =~ s/%VERSION%/$version/g;
		$postbody =~ s/%USER%/$remote_user/g;
		$postbody =~ s/%OS%/$os_type $os_version/g;
		print "$postbody\n";
		}
	if ($tconfig{'postbodyinclude'}) {
		open(INC,
		 "$root_directory/$current_theme/$tconfig{'postbodyinclude'}");
		while(<INC>) {
			print;
			}
		close(INC);
		}
	if (defined(&theme_postbody)) {
		&theme_postbody(@_);
		}
	}
}
1;  # Return true

