Bug 20602: Use weighted fields in OPAC simple search
[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
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;    # checkauth, getborrowernumber.
26 use C4::Context;
27 use Digest::MD5 qw(md5_base64);
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Output;
31 use Koha::AuthUtils qw(hash_password);
32 use Koha::Patrons;
33
34 my $query = new CGI;
35 my $dbh   = C4::Context->dbh;
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => "opac-passwd.tt",
40         query           => $query,
41         type            => "opac",
42         authnotrequired => 0,
43         debug           => 1,
44     }
45 );
46
47 my $patron = Koha::Patrons->find( $borrowernumber );
48 if ( C4::Context->preference("OpacPasswordChange") ) {
49     my $sth =  $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
50     if (   $query->param('Oldkey')
51         && $query->param('Newkey')
52         && $query->param('Confirm') )
53     {
54         my $error;
55         my $new_password = $query->param('Newkey');
56         my $confirm_password = $query->param('Confirm');
57         if ( goodkey( $dbh, $borrowernumber, $query->param('Oldkey') ) ) {
58
59             if ( $new_password ne $confirm_password ) {
60                 $template->param( 'Ask_data'       => '1' );
61                 $template->param( 'Error_messages' => '1' );
62                 $template->param( 'passwords_mismatch'   => '1' );
63             } else {
64                 my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $new_password );
65                 unless ( $is_valid ) {
66                     $error = 'password_too_short' if $error eq 'too_short';
67                     $error = 'password_too_weak' if $error eq 'too_weak';
68                     $error = 'password_has_whitespaces' if $error eq 'has_whitespaces';
69                 } else {
70                     # Password is valid and match
71                     my $clave = hash_password( $new_password );
72                     $sth->execute( $clave, $borrowernumber );
73                     $template->param( 'password_updated' => '1' );
74                     $template->param( 'borrowernumber'   => $borrowernumber );
75                 }
76             }
77         }
78         else {
79             $error = 'WrongPass';
80         }
81         if ($error) {
82             $template->param(
83                 Ask_data       => 1,
84                 Error_messages => 1,
85                 $error         => 1,
86             );
87
88         }
89     }
90     else {
91
92         # Called Empty, Ask for data.
93         $template->param( 'Ask_data' => '1' );
94         if (!$query->param('Oldkey') && ($query->param('Newkey') || $query->param('Confirm'))){
95             # Old password is empty but one of the others isnt
96             $template->param( 'Error_messages' => '1' );
97             $template->param( 'WrongPass'      => '1' );
98         }
99         elsif ($query->param('Oldkey') && (!$query->param('Newkey') || !$query->param('Confirm'))){
100             # Oldpassword is entered but one of the other fields is empty
101             $template->param( 'Error_messages' => '1' );
102             $template->param( 'PassMismatch'   => '1' );
103         }
104     }
105 }
106 $template->param(
107     firstname  => $patron->firstname,
108     surname    => $patron->surname,
109     passwdview => 1,
110 );
111
112
113 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
114
115 sub goodkey {
116     my ( $dbh, $borrowernumber, $key ) = @_;
117
118     my $sth =
119       $dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
120     $sth->execute($borrowernumber);
121     if ( $sth->rows ) {
122         my $hash;
123         my ($stored_hash) = $sth->fetchrow;
124         if ( substr($stored_hash,0,2) eq '$2') {
125             $hash = hash_password($key, $stored_hash);
126         } else {
127             $hash = md5_base64($key);
128         }
129         if ( $hash eq $stored_hash ) { return 1; }
130         else { return 0; }
131     }
132     else { return 0; }
133 }