# $Id: db-backend-utils.pl,v 1.4 2002/02/01 14:37:46 fpmip Exp $
#
# Miscellaneous utility functions for the database backend

# Author : Tycho Fruru <tycho@fruru.com>

# Copyright (C) 2001 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>

# 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
#

use MIME::Base64;
use URI::Escape;
use POSIX qw(strftime);

#sub show_msg($$) {
#	my $show_level = shift;
#	my $show_msg = shift;
#	chomp $show_msg;
#	my $timestamp = strftime "%Y-%m-%d %H:%M:%S%z",localtime;
#	if ($show_level <= $VERBOSITY) {
#		print "$timestamp : $show_msg\n";
#	}
#}

sub decode_entry_generic($$$) {

# takes the following part of an XML file :
#
# <fetchez-la-vache format="ascii">meuh</fetchez-la-vache>
#
# which parses to :
#
# $VAR1 = {
#           'format' => 'ascii',
#           'content' => 'meuh'
#         }
#
# or
#
# <fetchez-la-vache>meuh</fetchez-la-vache>
#
# which parses to :
#
#$VAR1 = 'meuh'
#        

my $r_entry = shift;
my $encoding_tag = shift;
my $content_tag = shift;

my $decoded_value;

if (ref($r_entry) ne 'HASH') {
	# we have the entry itself
	$decoded_value = $r_entry;
} else {
	my $format = $r_entry->{$encoding_tag};
	my $encoded_value = $r_entry->{$content_tag};
	if (!defined($encoded_value)) {
		return undef;
	}
	if (!defined($format)) { 
		$decoded_value = $encoded_value;
	} elsif ($format eq 'base64') {
		$decoded_value = decode_base64($encoded_value);
	} elsif ($format eq 'uri') {
		$decoded_value = uri_unescape($encoded_value);
	} elsif ($format eq 'ascii') {
		$decoded_value = $encoded_value;
	}
}
return $decoded_value;
}

sub decode_entry($) 
{
	my $r_entry = shift;
	return decode_entry_generic($r_entry,'format','content');
}

sub decode_check_entry($$)
# first parameter as in decode_entry
# second parameter is a regular expression which must match,
#  otherwise undef is returned
{
	my $entry = shift;
	my $check = shift;
	my $decoded_value = decode_entry($entry);
	
	if (!defined($decoded_value)) {
		IPFC::Log::logger(1,"a to be decoded value could not be decoded !");
		return undef;
	}	

	if ($decoded_value =~ /$check/) {
		return $decoded_value;
	} else {
		return undef;
	}
}

sub check_alpha($)
{
	my $string = shift;
	
	if ($string =~ /^[a-zA-Z]*$/) {
		return $string;
	} else {
		return undef;
	}
}

sub check_numeric($)
{
	my $string = shift;
	
	if ($string =~ /^[0-9]*$/) {
		return $string;
	} else {
		return undef;
	}
}

sub check_integer($)
{
	my $string = shift;
	
	if ($string =~ /^-?[0-9]+$/) {
		return $string;
	} else {
		return undef;
	}
}

sub check_alphanumeric($)
{
	my $string = shift;
	
	if ($string =~ /^[a-zA-Z0-9]*$/) {
		return $string;
	} else {
		return undef;
	}
}

sub check_string($)
{
	my $string = shift;
	
	if ($string =~ /^[a-zA-Z0-9\ \.\,\:\;\-\_\/]*$/) {
		return $string;
	} else {
		return undef;
	}
}

sub check_string_nospace($)
{
	my $string = shift;
	
	if ($string =~ /^[a-zA-Z0-9\.\,\:\;\-\_\/]*$/) {
		return $string;
	} else {
		return undef;
	}
}

sub check_ip($)
{
	my $string = shift;
	
	if ($string =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) {
		return $string
	} else {
		return undef;
	}
}

return 1; 
