Fixing updatedatabase after 3.18
[koha.git] / opac / opac-passwd.pl
blobbf6b1758316deba2618c805bd550072309fc8bfd
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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;
32 use Koha::AuthUtils qw(hash_password);
34 my $query = new CGI;
35 my $dbh = C4::Context->dbh;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 template_name => "opac-passwd.tt",
40 query => $query,
41 type => "opac",
42 authnotrequired => 0,
43 flagsrequired => { borrow => 1 },
44 debug => 1,
48 # get borrower information ....
49 my ( $borr ) = GetMemberDetails( $borrowernumber );
50 my $minpasslen = C4::Context->preference("minPasswordLength");
51 if ( C4::Context->preference("OpacPasswordChange") ) {
52 my $sth = $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
53 if ( $query->param('Oldkey')
54 && $query->param('Newkey')
55 && $query->param('Confirm') )
57 if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
58 if ( $query->param('Newkey') =~ m|^\s+| or $query->param('Newkey') =~ m|\s+$| ) {
59 $template->param(
60 Error_messages => 1,
61 PasswordContainsTrailingSpaces => 1,
64 elsif ( $query->param('Newkey') eq $query->param('Confirm')
65 && length( $query->param('Confirm') ) >= $minpasslen )
66 { # Record password
67 my $clave = hash_password( $query->param('Newkey') );
68 $sth->execute( $clave, $borrowernumber );
69 $template->param( 'password_updated' => '1' );
70 $template->param( 'borrowernumber' => $borrowernumber );
72 elsif ( $query->param('Newkey') ne $query->param('Confirm') ) {
73 $template->param( 'Ask_data' => '1' );
74 $template->param( 'Error_messages' => '1' );
75 $template->param( 'PassMismatch' => '1' );
77 elsif ( length( $query->param('Confirm') ) < $minpasslen ) {
78 $template->param( 'Ask_data' => '1' );
79 $template->param( 'Error_messages' => '1' );
80 $template->param( 'ShortPass' => '1' );
82 else {
83 $template->param( 'Error_messages' => '1' );
86 else {
87 $template->param( 'Ask_data' => '1' );
88 $template->param( 'Error_messages' => '1' );
89 $template->param( 'WrongPass' => '1' );
92 else {
94 # Called Empty, Ask for data.
95 $template->param( 'Ask_data' => '1' );
96 if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
97 # Old password is empty but one of the others isnt
98 $template->param( 'Error_messages' => '1' );
99 $template->param( 'WrongPass' => '1' );
101 elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
102 # Oldpassword is entered but one of the other fields is empty
103 $template->param( 'Error_messages' => '1' );
104 $template->param( 'PassMismatch' => '1' );
108 $template->param(firstname => $borr->{'firstname'},
109 surname => $borr->{'surname'},
110 minpasslen => $minpasslen,
111 passwdview => 1,
114 output_html_with_http_headers $query, $cookie, $template->output;
116 sub goodkey {
117 my ( $dbh, $borrowernumber, $key ) = @_;
119 my $sth =
120 $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
121 $sth->execute($borrowernumber);
122 if ( $sth->rows ) {
123 my $hash;
124 my ($stored_hash) = $sth->fetchrow;
125 if ( substr($stored_hash,0,2) eq '$2') {
126 $hash = hash_password($key, $stored_hash);
127 } else {
128 $hash = md5_base64($key);
130 if ( $hash eq $stored_hash ) { return 1; }
131 else { return 0; }
133 else { return 0; }