grrr typo... must test before checkin
[koha.git] / member-password.pl
1 #!/usr/bin/perl
2 #script to set the password, and optionally a userid, for a borrower
3 #written 2/5/00
4 #by chris@katipo.co.nz
5 #converted to using templates 3/16/03 by mwhansen@hmc.edu
6
7 use strict;
8 use C4::Auth;
9 use C4::Output;
10 use C4::Interface::CGI::Output;
11 use C4::Search;
12 use C4::Context;
13 use C4::Circulation::Circ2;
14 use CGI;
15 use HTML::Template;
16 use Digest::MD5 qw(md5_base64);
17
18 my $input = new CGI;
19
20 my $theme = $input->param('theme') || "default";
21                         # only used if allowthemeoverride is set
22
23 my ($template, $loggedinuser, $cookie)
24     = get_template_and_user({template_name => "members/member-password.tmpl",
25                              query => $input,
26                              type => "intranet",
27                              authnotrequired => 0,
28                              flagsrequired => {borrowers => 1},
29                              debug => 1,
30                              });
31
32 my $flagsrequired;
33 $flagsrequired->{borrowers}=1;
34 my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired);
35
36 my $member=$input->param('member');
37 my %env;
38 $env{'nottodayissues'}=1;
39 my %member2;
40 $member2{'borrowernumber'}=$member;
41 my $issues=currentissues(\%env,\%member2);
42 my $i=0;
43 foreach (sort keys %$issues) {
44     $i++;
45 }
46
47 my ($bor,$flags)=getpatroninformation(\%env, $member,'');
48 my $newpassword = $input->param('newpassword');
49
50 if ( $newpassword ) {
51     my $digest=md5_base64($input->param('newpassword'));
52     my $uid = $input->param('newuserid');
53     my $dbh=C4::Context->dbh;
54
55         if ($uid eq '') { $uid = $bor->{'userid'} }
56         #Make sure the userid chosen is unique and not theirs. If it is not,
57         #Then we need to tell the user and have them create a new one.
58         my $sth2=$dbh->prepare("select * from borrowers where userid=? and borrowernumber != ?");
59         $sth2->execute($uid,$member);
60
61         if ( $sth2->fetchrow ) {
62         #The userid exists so we should display a warning.
63                 my $warn = 1;
64         $template->param( warn => $warn,
65                         othernames => $bor->{'othernames'},
66                         surname     => $bor->{'surname'},
67                         firstname   => $bor->{'firstname'},
68                         userid      => $bor->{'userid'},
69                         defaultnewpassword => $newpassword );
70     } else {
71                 #Everything is good so we can update the information.
72                 my $sth=$dbh->prepare("update borrowers set userid=?, password=? where borrowernumber=?");
73         $sth->execute($uid, $digest, $member);
74                 $template->param(newpassword => $newpassword);
75         }
76
77 } else {
78     my $userid = $bor->{'userid'};
79
80     my $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
81     my $length=int(rand(2))+4;
82     my $defaultnewpassword='';
83     for (my $i=0; $i<$length; $i++) {
84         $defaultnewpassword.=substr($chars, int(rand(length($chars))),1);
85     }
86         $template->param(       othernames => $bor->{'othernames'},
87                         surname     => $bor->{'surname'},
88                         firstname   => $bor->{'firstname'},
89                         userid      => $bor->{'userid'},
90                         defaultnewpassword => $defaultnewpassword );
91
92
93 }
94
95 $template->param( member => $member );
96
97 output_html_with_http_headers $input, $cookie, $template->output;