#!/usr/bin/perl
####################################################
# npulse.cgi
# Network Monitoring Program
#
# Author: Steve Horsburgh  (npulse@horsburgh.com)
$Version = '0.54';
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
####################################################
$|=1;
require 5.005;
require "/usr/local/npulse/preferences.pl";
require "$Libdir/util.pl";
require "$Libdir/actions.pl";
#require $Cgilib;
use CGI qw/:standard/; $q = new CGI;
#------------------------------------------------------------------#
#-- Main Program --------------------------------------------------#
#------------------------------------------------------------------#

$DoneHeader = 0;

&DEBUG("Starting npulse.cgi");
CGI::ReadParse(%in);
print $q->header(-type => 'text/html', -expires => 'now');
#print &PrintHeader();
#&ReadParse();

# Browser sniff

$IsIE    ||= ($ENV{'HTTP_USER_AGENT'} =~ /MSIE/) ? 1 : 0;
$IsNS    ||= (!$IsIE and ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla/)) ? 1 : 0;
$IsMac   ||= ($ENV{'HTTP_USER_AGENT'} =~ /Mac/) ? 1 : 0;
$IsLinux ||= ($ENV{'HTTP_USER_AGENT'} =~ /linux/i) ? 1 : 0;

$IsIE = 0 if ($IsIE and $IsMac);
$IsNS = 0 if ($IsNS and $IsMac);

# Init some things

use locale;
foreach ('site','a','s','sort') {
  $in{$_} =~ s/[^\w\.\d-]//g if defined $in{$_};   # Trap bad/imposter characters
}
$HaveTelnet = (-f "$Libdir/telnet.pl")?1:0;
$in{'a'} = "display" unless $in{'a'};
if ($in{'a'} eq 'telnet') {
  &Error("Telnet support is disabled.") unless $HaveTelnet;
}

# Always Get Config and Service information

&GetConfigHash();
&ReadServices();

# Setup user info

$User = $ENV{'REMOTE_USER'} or &Error("Unable to determine user.");  
$AdminUser = ($User eq "admin") ? 1 : 0;

if ($AdminUser) {
  %allowedactions = (
                    display  => "$Libdir/gendisp.pl",
                    pref     => "$Libdir/prefs.pl",
                    setup    => "$Libdir/editor.pl",
                    recheck  => "$Libdir/gendisp.pl",
                    detail   => "$Libdir/detail.pl",
                    auto     => "$Libdir/prefs.pl",
                    filter   => "$Libdir/gendisp.pl",
                    telnet   => "$Libdir/telnet.pl",
                    sapi     => "$Libdir/sapi.pl",
                    script   => "$Libdir/prefs.pl",
                    status   => ""
                    );
  $allowedsubactions = 'traceroute quickport fullport history lookup discover save updateinfo help copyright on off cleareventlog usercmds recheckos runscript overview portconfig ';
  $UserDir = $Htmldir;

} else {
  %allowedactions = (
                    display  => "$Libdir/gendisp.pl",
                    detail   => "$Libdir/detail.pl",
                    filter   => "$Libdir/gendisp.pl"
                    );
  $allowedsubactions = 'traceroute quickport fullport history help copyright usercmds runscript overview ';
  $UserDir = $Htmldir.'/users/'.$User;
  unless (-d $UserDir) {
    mkdir($Htmldir.'/users',0750) unless (-d $Htmldir.'/users');
    mkdir($UserDir,0750);
  }
}
&GetKookie($UserDir);

# Now make sure we have valid commands

($ins = $in{'s'}) =~ s/_.*$/_/;
delete $in{'s'} unless ($in{'s'} && (index(lc($allowedsubactions),lc($ins)) >= 0));

# Load required modules and call the appropriate routine

if (exists $allowedactions{lc($in{'a'})}) {
  require $allowedactions{lc($in{'a'})} if $allowedactions{lc($in{'a'})};
  &{uc($in{'a'})}();
}

#&Error("Invalid call to nPulse");
$in{'a'} = 'display';
$in{'s'} = '';
&DISPLAY();

exit;

