adding a warn to log why we're redirecting to installer
[koha.git] / opac / opac-passwd.pl
1 #!/usr/bin/perl
2 # This script lets the users change the passwords by themselves.
3 #
4 # (c) 2005 Universidad ORT Uruguay.
5 #
6 # This file is part of the extensions and enhacments made to koha by Universidad ORT Uruguay
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 require Exporter;
23 use CGI;
24
25 use C4::Auth;    # checkauth, getborrowernumber.
26 use C4::Context;
27 use Digest::MD5 qw(md5_base64);
28 use C4::Circulation;
29
30 use C4::Output;
31
32 my $query = new CGI;
33 my $dbh   = C4::Context->dbh;
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-passwd.tmpl",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => 0,
41         flagsrequired   => { borrow => 1 },
42         debug           => 1,
43     }
44 );
45
46 # get borrower information ....
47 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
48 my $sth =
49   $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
50
51 if (   $query->param('Oldkey')
52     && $query->param('Newkey')
53     && $query->param('Confirm') )
54 {
55     if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
56         if ( $query->param('Newkey') eq $query->param('Confirm')
57             && length( $query->param('Confirm') ) > 5 )
58         {    # Record password
59             my $clave = md5_base64( $query->param('Newkey') );
60             $sth->execute( $clave, $borrowernumber );
61             $template->param( 'password_updated' => '1' );
62             $template->param( 'borrowernumber'   => $borrowernumber );
63         }
64         elsif ( $query->param('Newkey') ne $query->param('Confirm') ) {
65             $template->param( 'Ask_data'       => '1' );
66             $template->param( 'Error_messages' => '1' );
67             $template->param( 'PassMismatch'   => '1' );
68         }
69         elsif ( length( $query->param('Confirm') ) <= 5 ) {
70             $template->param( 'Ask_data'       => '1' );
71             $template->param( 'Error_messages' => '1' );
72             $template->param( 'ShortPass'      => '1' );
73         }
74         else {
75             $template->param( 'Error_messages' => '1' );
76         }
77     }
78     else {
79         $template->param( 'Ask_data'       => '1' );
80         $template->param( 'Error_messages' => '1' );
81         $template->param( 'WrongPass'      => '1' );
82     }
83 }
84 else {
85
86     # Called Empty, Ask for data.
87     $template->param( 'Ask_data' => '1' );
88 }
89
90 output_html_with_http_headers $query, $cookie, $template->output;
91
92 sub goodkey {
93     my ( $dbh, $borrowernumber, $key ) = @_;
94
95     my $sth =
96       $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
97     $sth->execute($borrowernumber);
98     if ( $sth->rows ) {
99         my ($md5password) = $sth->fetchrow;
100         if ( md5_base64($key) eq $md5password ) { return 1; }
101         else { return 0; }
102     }
103     else { return 0; }
104 }