#!/usr/bin/perl

use LWP::Simple;
use Sys::Syslog qw(:DEFAULT setlogsock);

my $LOG_LEVEL = "warning";
my $LOG_INIT = 0;

sub log_init
{
	setlogsock('unix');
	openlog($0, '', $ENV{"user"});
	$LOG_INIT = 1;
}

sub log_close {
	closelog if ($LOG_INIT);
}

sub _log {
	&syslog if ($LOG_INIT);
}

sub validate
{
	my ($username, $password, $local_1) = @_;

	my $url = sprintf('http://127.0.0.1/verify/validate.php?usernamex=%s&passwordx=%s&local_1x=%s', $username, $password, $local_1);
	_log($LOG_LEVEL, $url);

	if ($password ne "" && $username ne "")
	{
		my $result = get($url);
		_log($LOG_LEVEL, "validate: Data requested for user $username with status: $result");

		if ($result eq "pass") {
			return 1;
		} else {
			return 0;
		}
	}
	else {
		_log($LOG_LEVEL, "validate: Data missing -> un: $username, pa: $password, ip: $local_1");
		return 0;
	}
}

1;
