Bug 929 : Last follow up, implementing the last of Katrins suggestions
[koha.git] / opac / opac-passwd.pl
blobca502aba947d15858ab1072bb028732ba06fe20a
1 #!/usr/bin/perl
2 # This script lets the users change the passwords by themselves.
4 # (c) 2005 Universidad ORT Uruguay.
6 # This file is part of the extensions and enhacments made to koha by Universidad ORT Uruguay
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.
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.
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
21 use strict;
22 use warnings;
24 use CGI;
26 use C4::Auth; # checkauth, getborrowernumber.
27 use C4::Context;
28 use Digest::MD5 qw(md5_base64);
29 use C4::Circulation;
30 use C4::Members;
31 use C4::Output;
33 my $query = new CGI;
34 my $dbh = C4::Context->dbh;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 template_name => "opac-passwd.tmpl",
39 query => $query,
40 type => "opac",
41 authnotrequired => 0,
42 flagsrequired => { borrow => 1 },
43 debug => 1,
47 # get borrower information ....
48 my ( $borr ) = GetMemberDetails( $borrowernumber );
49 my $sth = $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
50 my $minpasslen = C4::Context->preference("minPasswordLength");
51 if ( $query->param('Oldkey')
52 && $query->param('Newkey')
53 && $query->param('Confirm') )
55 if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
56 if ( $query->param('Newkey') eq $query->param('Confirm')
57 && length( $query->param('Confirm') ) >= $minpasslen )
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 );
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' );
69 elsif ( length( $query->param('Confirm') ) < $minpasslen ) {
70 $template->param( 'Ask_data' => '1' );
71 $template->param( 'Error_messages' => '1' );
72 $template->param( 'ShortPass' => '1' );
74 else {
75 $template->param( 'Error_messages' => '1' );
78 else {
79 $template->param( 'Ask_data' => '1' );
80 $template->param( 'Error_messages' => '1' );
81 $template->param( 'WrongPass' => '1' );
84 else {
86 # Called Empty, Ask for data.
87 $template->param( 'Ask_data' => '1' );
88 if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
89 # Old password is empty but one of the others isnt
90 $template->param( 'Error_messages' => '1' );
91 $template->param( 'WrongPass' => '1' );
93 elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
94 # Oldpassword is entered but one of the other fields is empty
95 $template->param( 'Error_messages' => '1' );
96 $template->param( 'PassMismatch' => '1' );
100 $template->param(firstname => $borr->{'firstname'},
101 surname => $borr->{'surname'},
102 minpasslen => $minpasslen,
103 passwdview => 1,
106 output_html_with_http_headers $query, $cookie, $template->output;
108 sub goodkey {
109 my ( $dbh, $borrowernumber, $key ) = @_;
111 my $sth =
112 $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
113 $sth->execute($borrowernumber);
114 if ( $sth->rows ) {
115 my ($md5password) = $sth->fetchrow;
116 if ( md5_base64($key) eq $md5password ) { return 1; }
117 else { return 0; }
119 else { return 0; }