#!/usr/bin/perl -w -I. -I/usr/local/positive/Perl

use strict;
use Pos::WebService::Modules::ParseServerXmlRequest;
use Pos::WebService::Modules::ParseServerXmlResponse;
#use Pos::WebService::Modules::Auth;
#use Pos::WebService::Modules::UserInfo;
use Pos::WebService::Modules::Other;
use CGI qw/:standard/;
use CGI::Cookie ();
use Config::General;

my $conf = new Config::General("/usr/local/positive/conf/WebService.conf");
my %config = $conf->getall;

my $raddr = $ENV{'REMOTE_ADDR'};

my $allowedUrl = 0;

my $cgi = CGI->new();
my $r = $cgi->param('r'); # the request
my $s = $cgi->param('s'); # interface with service not on Webtop (e.g., 2fa soft token
my $c = $cgi->param('c'); # convert xml
my $k = $cgi->param('k'); # siteKey
my $errMsg = undef;

my @aa;

push(@aa,split(",",$config{allowedurls}));

foreach my $aa ( @aa )
{
	$aa eq $raddr and $allowedUrl = 1;
}

if (!$allowedUrl)
{
	if( defined($k) )
	{
		warn "WEBSERVICE ERROR: CLIENT $raddr is not allowed to access the web service - checking siteKey\n\n" unless ( $config{'logLevel'} == 0 );

		if( $config{'siteKey'} eq $k )
		{
			warn "WEBSERVICE WARNING: CLIENT $raddr valid siteKey $k provided, permitting acccess to Two-Factor Secure Soft Token service\n\n" unless ( $config{'logLevel'} == 0 );
		}
		else
		{
			$errMsg = "Content-type: text/xml\n\nERROR: UNAUTHORIZED E100WS";
			warn "WEBSERVICE ERROR: CLIENT $raddr invalid siteKey $k provided while attempting to access Two-Factor Secure Soft Token service\n\n" unless ( $config{'logLevel'} == 0 );
		}
	}
	else
	{
		warn "WEBSERVICE ERROR: CLIENT $raddr is not allowed to access the web service\n\n";
		exit;
	}
}

if( defined( $errMsg ) )
{
	print "$errMsg";
}
else
{
	my $xmlRequestParserObj = Pos::WebService::Modules::ParseServerXmlRequest->new();
	my $xmlResponseParserObj = Pos::WebService::Modules::ParseServerXmlResponse->new();

	if( ! defined( $r ) || ! length( $r ) )
	{
		my $contents = $ENV{'QUERY_STRING'};
		my ( $method, $work );
		( $method, $work, $c ) = split( '=', $contents );
		( $r, $work ) = split( '&', $work );
	}

	$c = 0 unless defined( $c );

	warn "WEBSERVICE LOG: CLIENT $raddr sent request => $r\n\n" unless ( $config{'logLevel'} == 0 );

	if (!$xmlRequestParserObj->parseRequest( request => $r, convert=> $c ) )
	{
		if( defined( $s ) )
		{
			$errMsg = "Content-type: text/plain\n\nERROR: SYSTEM E101WS";
			print $errMsg;
			warn "WEBSERVICE ERROR: CLIENT $raddr provided invalid xml=> $r\n\n";
		}
		else
		{
			warn "Content-type: text/xml\n\n
               <root>
                  <message>
                     <error>
                        <code/>
                        <text>Cannot parse request xml</text>
                     </error>
                  </message>
               </root>
			";
		
			exit;
		}
	}

	my $otherObj = Pos::WebService::Modules::Other->new( xmlObj => $xmlRequestParserObj, xml=> $r );
	if (!$otherObj->callRequestMethod( xml=> $r )) 
	{	
			warn "Content-type: text/xml\n\n
						<root>
							<message>
								<error>
                        <code/>
                        <text>Cannot call method or method has broken.</text>
                     </error>
                  </message>
               </root>
			";
			exit;
	}
}

1;
