Passing through reviewson value, and fixing syntax error
[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::Circ2;
29 use HTML::Template;
30 use C4::Interface::CGI::Output;
31
32 my $query = new CGI;
33 my $dbh = C4::Context->dbh;
34
35 my ($template, $borrowernumber, $cookie) 
36     = get_template_and_user({template_name => "opac-passwd.tmpl",
37                              query => $query,
38                              type => "opac",
39                              authnotrequired => 0,
40                              flagsrequired => {borrow => 1},
41                              debug => 1,
42                              });
43
44 # get borrower information ....
45 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
46 my $sth = $dbh->prepare("UPDATE borrowers SET password = ? WHERE borrowernumber=?");
47
48 if ( $query->param('Oldkey') && $query->param('Newkey') && $query->param('Confirm') ){
49         if ( goodkey($dbh,$borrowernumber, $query->param('Oldkey')) ){
50                 if ( $query->param('Newkey') eq $query->param('Confirm') &&
51                         length($query->param('Confirm')) > 5 ){ # Record password
52                         my $clave = md5_base64($query->param('Newkey'));
53                         $sth->execute($clave,$borrowernumber);
54                         $template->param('password_updated' => '1');
55                         $template->param('borrowernumber' => $borrowernumber);
56                 }elsif ( $query->param('Newkey') ne $query->param('Confirm') ){
57                         $template->param('Ask_data' => '1');
58                         $template->param('Error_messages' => '1');
59                         $template->param('PassMismatch' => '1');
60                 }elsif (length($query->param('Confirm')) <= 5 ){
61                         $template->param('Ask_data' => '1');
62                         $template->param('Error_messages' => '1');
63                         $template->param('ShortPass' => '1');
64                 }else{
65                         $template->param('Error_messages' => '1');
66                 } 
67         }else{
68                 $template->param('Ask_data' => '1');
69                 $template->param('Error_messages' => '1');
70                 $template->param('WrongPass' => '1');
71         }
72 }else {
73 # Called Empty, Ask for data.
74         $template->param('Ask_data' => '1');
75 }
76 output_html_with_http_headers $query, $cookie, $template->output;
77
78
79 sub goodkey {
80 my ($dbh, $borrowernumber, $key) = @_;
81
82         my $sth=$dbh->prepare("SELECT password FROM borrowers WHERE borrowernumber=?");
83         $sth->execute($borrowernumber);
84         if ($sth->rows){
85                 my ($md5password) = $sth->fetchrow;
86                 if (md5_base64($key) eq $md5password) { return 1; } else { return 0; }
87         }else{ return 0; }
88 }