Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use strict; |
4 |
|
5 |
use CGI; |
6 |
use Mail::Sendmail; |
7 |
use Digest::MD5 qw(md5_base64); |
8 |
use HTML::Entities; |
9 |
|
10 |
use C4::Auth; |
11 |
use C4::Koha; |
12 |
use C4::Members qw(changepassword GetMember GetMemberDetails SendPasswordRecoveryEmail); |
13 |
use C4::Output; |
14 |
use C4::Context; |
15 |
|
16 |
my $query = new CGI; |
17 |
|
18 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
19 |
{ |
20 |
template_name => "opac-password-recovery.tmpl", |
21 |
query => $query, |
22 |
type => "opac", |
23 |
authnotrequired => 1, |
24 |
debug => 1, |
25 |
} |
26 |
); |
27 |
|
28 |
my $email = $query->param('email'); |
29 |
my $password = $query->param('password'); |
30 |
my $repeatPassword = $query->param('repeatPassword'); |
31 |
my $minPassLength = C4::Context->preference( 'minPasswordLength' ); |
32 |
my $id = $query->param('id'); |
33 |
my $uniqueKey = $query->param('uniqueKey'); |
34 |
my $username = $query->param('username'); |
35 |
my $borrower_number; |
36 |
|
37 |
#errors |
38 |
my $hasError; |
39 |
|
40 |
#email form error |
41 |
my $errNoEmailFound; |
42 |
my $errAlreadyStartRecovery; |
43 |
|
44 |
#new password form error |
45 |
my $errLinkNotValid; |
46 |
my $errPassNotMatch; |
47 |
my $errPassTooShort; |
48 |
|
49 |
my $dbh = C4::Context->dbh; |
50 |
|
51 |
$template->param( OpacResetPassword => C4::Context->preference("OpacResetPassword") ); |
52 |
|
53 |
if ( $query->param('sendEmail') || $query->param('resendEmail') ) |
54 |
{ |
55 |
#send mail + confirmation |
56 |
|
57 |
#try with the main email |
58 |
my $borrower_number; |
59 |
my $accountActivated; |
60 |
my %borrower_infos = GetMember(email => $email); |
61 |
if ( %borrower_infos ) |
62 |
{ |
63 |
$borrower_number = GetMember(email => $email)->{'borrowernumber'}; |
64 |
$username = GetMemberDetails( $borrower_number )->{'userid'}; |
65 |
$accountActivated = ( GetMemberDetails( $borrower_number )->{'categorycode'} ne 'UNAUTHORIZ' ) ? 1 : 0; |
66 |
} |
67 |
else |
68 |
{ |
69 |
#try with the secondary email |
70 |
%borrower_infos = GetMember(emailpro => $email); |
71 |
if ( %borrower_infos ) |
72 |
{ |
73 |
$borrower_number = GetMember(emailpro => $email)->{'borrowernumber'}; |
74 |
$username = GetMemberDetails( $borrower_number )->{'userid'}; |
75 |
$accountActivated = ( GetMemberDetails( $borrower_number )->{'categorycode'} ne 'UNAUTHORIZ' ) ? 1 : 0; |
76 |
} |
77 |
else |
78 |
{ |
79 |
#try when the other contact email |
80 |
%borrower_infos = GetMember(B_email => $email); |
81 |
if ( %borrower_infos ) |
82 |
{ |
83 |
$borrower_number = GetMember(B_email => $email)->{'borrowernumber'}; |
84 |
$username = GetMemberDetails( $borrower_number )->{'userid'}; |
85 |
$accountActivated = ( GetMemberDetails( $borrower_number )->{'categorycode'} ne 'UNAUTHORIZ' ) ? 1 : 0; |
86 |
} |
87 |
} |
88 |
} |
89 |
|
90 |
if ( !$email || !$username || !$borrower_number || !$accountActivated ) |
91 |
{ |
92 |
$hasError = 1; |
93 |
$errNoEmailFound = 1; |
94 |
} |
95 |
elsif ( !$query->param('resendEmail') ) |
96 |
{ |
97 |
my $sth = $dbh->prepare( "SELECT borrowernumber FROM borrower_password_recovery WHERE NOW() < valid_until AND borrowernumber = ?" ); |
98 |
$sth->execute($borrower_number); |
99 |
if ( my $already = $sth->fetchrow ) |
100 |
{ |
101 |
$hasError = 1; |
102 |
$errAlreadyStartRecovery = 1; |
103 |
} |
104 |
} |
105 |
|
106 |
if ( $hasError ) |
107 |
{ |
108 |
$template->param( hasError => 1, |
109 |
errNoEmailFound => $errNoEmailFound, |
110 |
errAlreadyStartRecovery => $errAlreadyStartRecovery, |
111 |
password_recovery => 1, |
112 |
email => HTML::Entities::encode($email), |
113 |
); |
114 |
} |
115 |
else |
116 |
{ |
117 |
#generate uuid and send recovery email |
118 |
if ( SendPasswordRecoveryEmail($borrower_number, $email) ) |
119 |
{ |
120 |
# if it works.... |
121 |
$template->param( mail_sent => 1, |
122 |
email => $email |
123 |
); |
124 |
} |
125 |
else |
126 |
{ |
127 |
# if it doesnt work.... |
128 |
$template->param( password_recovery => 1, |
129 |
sendmailError => 1 |
130 |
); |
131 |
} |
132 |
} |
133 |
} |
134 |
elsif ( $query->param('passwordReset') ) |
135 |
{ |
136 |
#new password form |
137 |
#check if the link is still valid |
138 |
my $sth = $dbh->prepare( "SELECT borrower_password_recovery.borrowernumber, userid |
139 |
FROM borrower_password_recovery, borrowers |
140 |
WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber |
141 |
AND NOW() < valid_until |
142 |
AND uuid = ?" ); |
143 |
$sth->execute( $uniqueKey ); |
144 |
( $borrower_number , $username) = $sth->fetchrow; |
145 |
|
146 |
#validate password length & match |
147 |
if ( ( $borrower_number ) && ( $password eq $repeatPassword ) && ( length( $password ) >= $minPassLength ) ) |
148 |
{ |
149 |
#apply changes |
150 |
changepassword( $username, $borrower_number, md5_base64($password) ); |
151 |
#this line needed to fix a log bug in "Log.pm:75" |
152 |
# |
153 |
# if ( !$usernumber ) { $usernumber=0; } |
154 |
# |
155 |
|
156 |
#remove entry |
157 |
my $sth = $dbh->prepare( "DELETE FROM borrower_password_recovery |
158 |
WHERE uuid = ? |
159 |
ORDER BY valid_until DESC LIMIT 1" ); |
160 |
$sth->execute( $uniqueKey); |
161 |
|
162 |
$template->param( password_reset_done => 1, |
163 |
username => $username |
164 |
); |
165 |
} |
166 |
else |
167 |
{ |
168 |
#errors |
169 |
if ( !$borrower_number ) |
170 |
{ |
171 |
#parameters not valid |
172 |
$errLinkNotValid = 1; |
173 |
} |
174 |
elsif ( $password ne $repeatPassword ) |
175 |
{ |
176 |
#passwords does not match |
177 |
$errPassNotMatch = 1; |
178 |
} |
179 |
elsif ( length( $password ) < $minPassLength ) |
180 |
{ |
181 |
#password too short |
182 |
$errPassTooShort = 1; |
183 |
} |
184 |
$template->param( new_password => 1, |
185 |
minPassLength => $minPassLength, |
186 |
email => $email, |
187 |
uniqueKey => $uniqueKey, |
188 |
errLinkNotValid => $errLinkNotValid, |
189 |
errPassNotMatch => $errPassNotMatch, |
190 |
errPassTooShort => $errPassTooShort, |
191 |
hasError => 1 ); |
192 |
} |
193 |
} |
194 |
elsif ( $uniqueKey ) |
195 |
{ |
196 |
#reset password form |
197 |
#check if the link is valid |
198 |
my $sth = $dbh->prepare( "SELECT borrower_password_recovery.borrowernumber, userid |
199 |
FROM borrower_password_recovery, borrowers |
200 |
WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber |
201 |
AND NOW() < valid_until |
202 |
AND uuid = ?" ); |
203 |
$sth->execute( $uniqueKey ); |
204 |
( $borrower_number, $username ) = $sth->fetchrow; |
205 |
if( !$borrower_number ) |
206 |
{ |
207 |
$errLinkNotValid = 1; |
208 |
} |
209 |
$template->param( new_password => 1, |
210 |
minPassLength => $minPassLength, |
211 |
email => $email, |
212 |
uniqueKey => $uniqueKey, |
213 |
username => $username, |
214 |
errLinkNotValid => $errLinkNotValid |
215 |
); |
216 |
} |
217 |
else |
218 |
{ |
219 |
#password recovery form (to send email) |
220 |
$template->param( password_recovery => 1 ); |
221 |
} |
222 |
|
223 |
output_html_with_http_headers $query, $cookie, $template->output; |
224 |
|