This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
evoauth/admin/lib/Evoauth/Functions.pm
2005-09-12 19:51:19 +00:00

107 lines
2 KiB
Perl

package Evoauth::Functions;
use strict;
use warnings;
use Config::Tiny;
use DBI;
use MIME::Lite;
# Renvoit la date courrante
sub Date() {
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime(time);
$year += 1900;
my $temps = "$mday/$mon/$year - $hour:$min:$sec";
return $temps;
}
# Envoit un mail
sub Mail() {
my ($event, $ip) = @_;
my $temps = Date();
# paramètres de connexion
my $Config = Config::Tiny->read( '/etc/evoauth/evoauth.conf' );
my $db = $Config->{bdd}->{db};
my $username = $Config->{bdd}->{username};
my $userpass = $Config->{bdd}->{userpass};
# connexion
my $dbh = DBI->connect( $db, $username, $userpass )
&& &Log("La connexion a réussie.") ||
&Log("La connexion a échoué : $DBI::errstr");
# récupération du login correspondant à l'ip
my $sql = "SELECT login FROM users where ip = '".$ip."'";
my $sth = $dbh->prepare($sql);;
$sth->execute();
my $login;
$sth->bind_columns(undef, \$login) && $sth->fetch();
my $msg = new MIME::Lite
From => 'evoauth@shaktiware.fr',
To => 'aanriot@nerim.net',
Subject => $event,
Type => 'TEXT',
Data => "$temps : $event de $login ($ip).";
$msg -> send && &Log("Un mail a été envoyé.");
$dbh->disconnect();
}
# Ecrit dans le journal
sub Log() {
my $file = "/usr/local/share/evoauth/evoauth.log";
my $message = shift;
my $temps = &Date;
open(LOG, ">> $file") or
die "L'ouverture du journal evoauth.log a échoué: $!.\n";
print LOG "$temps $message\n";
close(LOG);
}
1;
__END__
=head1 NAME
Evoauth::Functions - Fonctions
=head1 SYNOPSIS
use Evoauth::Functions;
=head1 DESCRIPTION
Fonctions d'administration d'Evoauth.
=head2 EXPORT
...
=head1 SEE ALSO
...
=head1 AUTHOR
Alexandre Anriot, E<lt>aanriot@evolix.fr<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005 by Alexandre Anriot
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.6 or,
at your option, any later version of Perl 5 you may have available.
=cut