source: trunk/lib/Epoll/DB/common.pm @ 471

Last change on this file since 471 was 315, checked in by nanardon, 14 years ago
  • create a plugin system to get voters from externals base
  • Property svn:keywords set to Id Rev
File size: 3.5 KB
Line 
1package Epoll::DB::common;
2
3# $Id$
4
5use strict;
6use warnings;
7use DBI;
8use vars qw(@EXPORT);
9use base 'Exporter';
10@EXPORT = qw(gen_uid);
11use Epoll::DB::ImportV;
12
13=head1 NAME
14
15Epoll::Model::Vote - Catalyst Model
16
17=head1 DESCRIPTION
18
19Catalyst Model.
20
21=cut
22
23sub _newdb {
24    my ($dbstring) = @_;
25    my $db = DBI->connect_cached(
26        'dbi:Pg:' . $dbstring,
27        undef, undef,
28        {
29            RaiseError => 0,
30            AutoCommit => 0,
31            PrintWarn => 0,
32            PrintError => 1,
33        }
34    ) or return;
35    $db->do(q{set DATESTYLE to 'DMY'});
36    return $db;
37}
38
39sub db {
40    my ($self) = @_;
41    return $_[0]->{db} && $_[0]->{db}->ping
42        ? $_[0]->{db}
43        : ($self->{db} = _newdb($self->{dbstring}));
44}
45
46sub commit {
47    my ($self) = @_;
48    $self->{db} or return;
49    # If EPOLL_NO_COMMIT is true, we never commit, use for test
50    if ($ENV{EPOLL_NO_COMMIT}) { return 1 }
51    $self->{db}->commit;
52}
53
54sub rollback {
55    my ($self) = @_;
56    $self->{db} or return;
57    if ($ENV{EPOLL_NO_COMMIT}) { return 1 }
58    $self->{db}->rollback;
59}
60
61sub mail_header {
62    return(
63        'Content-Type' => 'text/plain; charset=UTF-8; format=flowed',
64        'Content-Transfer-Encoding' => '8bit',
65        'X-Epoll-version' => $Epoll::VERSION,
66    );
67}
68
69sub random_string {
70    my $lenght = $_[-1] || 8;
71
72    return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght));
73}
74
75sub gen_enc_passwd {
76    my ($self, $passwd) = @_;
77
78    $passwd ||= random_string(8);
79    return(crypt($passwd, '$1$' . random_string(8) . '$'));
80}
81
82sub dbtime {
83    my ($self) = @_;
84    my $sth = $self->db->prepare(
85        q{select to_char(now(), 'DD/MM/YYYY HH24:MI:SS') as d}
86    );
87
88    $sth->execute();
89    my $res = $sth->fetchrow_hashref;
90    $sth->finish;
91    $res->{d};
92}
93
94sub valid_date {
95    my ($self, $date) = @_;
96    $self->db->do(q{set DATESTYLE to 'DMY'});
97    my $res = $self->db->do(
98        sprintf(
99            q{ select %s::timestamp },
100            $self->db->quote($date),
101        )
102    );
103    $res or $self->rollback;
104}
105
106sub check_date_max {
107    my ($self, $maxdate, $mindate) = @_;
108    $self->db->do(q{set DATESTYLE to 'DMY'});
109    my $sth = $self->db->prepare(
110        sprintf(
111            q{ select %s::timestamp > %s::timestamp as res },
112            $self->db->quote($maxdate),
113            $mindate ? $self->db->quote($mindate) : 'now()',
114        )
115    );
116    $sth->execute or do {
117        $self->rollback;
118        return;
119    };
120    my $res = $sth->fetchrow_hashref;
121    $sth->finish;
122    $res->{res}
123}
124
125sub poll_id_from_uid {
126    my ($self, $uid) = @_;
127    my $sth = $self->db->prepare_cached(
128        q{ select poll from settings where var = 'uid' and val = ?
129            order by poll }
130    );
131    $sth->execute($uid);
132    my $res = $sth->fetchrow_hashref;
133    $sth->finish;
134    return $res->{poll} || $uid;
135}
136
137sub gen_uid {
138    unpack("H*", join("", map { chr(rand(256)) } (0..15)))
139}
140
141sub import_handle {
142    my  ($self, $type) = @_;
143    return Epoll::DB::ImportV->new($type);
144}
145
146sub stored_import_handle {
147    my ($self, $id) = @_;
148    my $fetch = $self->db->prepare_cached(
149        q{select * from extern_auth where id = ?}
150    );
151    $fetch->execute($id);
152    my $res = $fetch->fetchrow_hashref;
153    $fetch->finish;
154    $res or return;
155    my $handle = $self->import_handle($res->{auth_type}) or return;
156    $handle->load_xml_params($res->{params});
157    $handle;
158}
159
160=head1 AUTHOR
161
162Thauvin Olivier
163
164=head1 LICENSE
165
166This library is free software, you can redistribute it and/or modify
167it under the same terms as Perl itself or CeCILL.
168
169=cut
170
1711;
Note: See TracBrowser for help on using the repository browser.