package DBIx::Password;
use strict;
use DBI();

@DBIx::Password::ISA = qw ( DBI::db );
($DBIx::Password::VERSION) = ' $Revision: 1.9 $ ' =~ /\$Revision:\s+([^\s]+)/;

my $virtual1 = {};

#We want to end up with such a structure:
#my $virtual1 = {
#        'acs' => {
#                         'username' => 'root',
#                         'password' => 'o.r,3',
#                         'port' => '',
#                         'database' => 'acs',
#                         'attributes' => {},
#                         'connect' => 'DBI:mysql:database=acs;host=localhost',
#                         'driver' => 'mysql',
#                         'host' => 'localhost'
#                       },
#        'personales' => {
#                         'username' => 'root',
#                         'password' => 'p.E.1',
#                         'port' => '',
#                         'database' => 'acs',
#                         'attributes' => {},
#                         'connect' => 'DBI:mysql:database=PaginasPersonales;host=
#localhost',
#                         'driver' => 'mysql',
#                         'host' => 'localhost'
#                       },
#};

sub clearConfig()
{
	$virtual1 = {};
	return 1;
}

sub readConfig($)
{
	my ($config) = @_;
	my $f;
	my @user;

	return undef unless -r $config;

	open ($f, $config) || die "Opening config file $config: $!";

	my @fields = qw(user username password port database connect driver
host);

	while (<$f>) {
		next if /^(#.*)?$/; #skip comments and blanks
		@user = m/:?'([^']*)':?/g;
		foreach (1 .. $#fields) { #write fields
			$virtual1->{$user[0]}->{$fields[$_]} = $user[$_];
		};
		$virtual1->{$user[0]}->{attributes} ||= {};
	}
	close $f;
	return $user[0];
}

#Now let's fill %virtual1 with values
readConfig('/etc/dbix-password.conf');

my %driver_cache;

sub connect {
	my ($class, $user, $options) = @_;
	return undef unless $virtual1->{$user};
	my $self;
	my $virtual = $virtual1->{$user};
	return undef unless $virtual;

	$self = DBI->connect($virtual->{connect}
			, $virtual->{'username'}
			, $virtual->{'password'}
			, $virtual->{'attributes'}
			);
	return undef unless $self;

	bless $self, $class;
	$driver_cache{$self} = $user;
	return $self;
}

sub connect_cached {
	my ($class, $user, $options) = @_;
	return undef unless $virtual1->{$user};
	my $self;
	my $virtual = $virtual1->{$user};
	return undef unless $virtual;

	$self = DBI->connect_cached($virtual->{connect}
			, $virtual->{'username'}
			, $virtual->{'password'}
			, $virtual->{'attributes'}
			);
	return undef unless $self;

	bless $self, $class;
	$driver_cache{$self} = $user;
	return $self;
}

sub getDriver {
	my ($self) = @_;
	unless(ref $self) {
		for my $key (keys %$virtual1) {
			return $virtual1->{$key}->{'driver'} if $self eq $key; 
		}
	} else {
		my $user = $driver_cache{$self};
		return $virtual1->{$user}{'driver'};
	}
}

sub checkVirtualUser {
	my ($user) = @_;
	return $virtual1->{$user} ? 1 : 0;
}

sub getVirtualUser {
	my ($user) = @_;
	return $virtual1->{$user} || undef;
}

sub DESTROY {
	my ($self) = @_;
	$self->SUPER::DESTROY;
}

1;

=head1 NAME

DBIx::Password - Allows you to create a global password file for DB passwords

=head1 SYNOPSIS

  use DBIx::Password;
  my $dbh = DBIx::Password->connect($user);
  my $dbh = DBIx::Password->connect_cached($user);
  $dbh->getDriver;
  DBIx::Password::getDriver($user);
  DBIx::Password::checkVirtualUser($user);

  DBIx::Password::clearConfig();
  DBIx::Password::readConfig("$ENV{HOME}/.my.secret.file");

=head1 DESCRIPTION

Don't you hate keeping track of database passwords and such throughout
your scripts? How about the problem of changing those passwords 
on a mass scale? This module is one possible solution. It stores all your
virtual users and data in /etc/dbix-password.conf.
For each user you need to specify the database module to use,
the database connect string, the username and the password.
You will have to give a name to this virtual user.
You can add as many as you like.

I would recommend that if you are only using this with
web applications that you change the final permissions on this 
package after it is installed in site_perl such that only
the webserver can read it.

A method called getDriver has been added so that you
can determine what driver is being used (handy for
working out database indepence issues).

If you want to find out if the virtual user is valid,
you can call the class method checkVirtualUser().
It returns true (1) if the username is valid, and
zero if not.

Once your are done you can use the connect method (or
the connect_cache method) that comes with DBIx-Password 
and just specify one of the virtual users you defined 
while making the module.

BTW I learned the bless hack that is used from Apache::DBI
so some credit should go to the authors of that module.
This is a rewrite of the module Tangent::DB that I did
for slashcode.

If your program does not need the system-wide information stored
in the /etc/dbix-password.conf file, you may use the clearConfig()
and readConfig() functions to get the data from another source.
At any time, readConfig() may also be used to merge the data from
another file into the currently-loaded configuration.

Hope you enjoy it.

=head1 HOME

To find out more information look at: http://www.tangent.org/DBIx-Password/

=head1 AUTHOR

Brian Aker, brian@tangent.org

=head1 SEE ALSO

perl(1). DBI(3).

=cut

