package ot_aut;
use strict;
#use FindBin;
#use lib $FindBin::Bin;
use ot_const qw( %ot );
use ot_dblib qw( &db_connect &db_disconnect );
use Apache::Constants qw( OK DECLINED REDIRECT AUTH_REQUIRED);

=head1 NAME

ot_aut - authenticates admin access via web

=head1 SYNOPSIS

The handler is called by apache and authenticates user.

=head1 DESCRIPTION

All functions and procedures are described below.

=head2 handler - checks user request against database

S<in-param:	user request
returns:	status OK or AUTH_REQUIRED (apache constants)>

=cut

sub handler {
	my $request = shift;
	my ( $res, $send_pw ) = $request->get_basic_auth_pw;
	my $pw = '';
	my $user = $request->connection->user;
	if( !$user ) {
		return OK;
	}
	open( LH , '>>'.$ot{logpath}.'auth.log' ) || die 'error open log';
	print LH 'auth access at '.localtime()." by $user pw '$send_pw'";
	my $dbh = db_connect;
	my $query = $dbh->prepare( "select passwd from member where email='$user'" );
	$query->execute();
	if( my $row = $query->fetchrow_hashref()){
		$pw = $$row{'passwd'};
	}
	$query->finish();
	db_disconnect( $dbh );
	print( LH " db-pw: '$pw'\n" );
	close( LH );
	if( $send_pw eq $pw ) {
		return OK;
    	}
	return AUTH_REQUIRED;
}
1;
