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