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 |
if ( $query->param('sendEmail') || $query->param('resendEmail') ) { |
53 |
#send mail + confirmation |
54 |
|
55 |
#try with the main email |
56 |
my $borrower_number; |
57 |
my %borrower_infos = GetMember( email => $email ); |
58 |
if (%borrower_infos) { |
59 |
$borrower_number = GetMember( email => $email )->{'borrowernumber'}; |
60 |
$username = GetMemberDetails($borrower_number)->{'userid'}; |
61 |
} |
62 |
else { |
63 |
#try with the secondary email |
64 |
%borrower_infos = GetMember( emailpro => $email ); |
65 |
if (%borrower_infos) { |
66 |
$borrower_number = GetMember( emailpro => $email )->{'borrowernumber'}; |
67 |
$username = GetMemberDetails($borrower_number)->{'userid'}; |
68 |
} |
69 |
else { |
70 |
#try when the other contact email |
71 |
%borrower_infos = GetMember( B_email => $email ); |
72 |
if (%borrower_infos) { |
73 |
$borrower_number = GetMember( B_email => $email )->{'borrowernumber'}; |
74 |
$username = GetMemberDetails($borrower_number)->{'userid'}; |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
if ( !$email || !$username || !$borrower_number ) { |
80 |
$hasError = 1; |
81 |
$errNoEmailFound = 1; |
82 |
} |
83 |
elsif ( !$query->param('resendEmail') ) { |
84 |
my $sth = $dbh->prepare( |
85 |
"SELECT borrowernumber FROM borrower_password_recovery WHERE NOW() < valid_until AND borrowernumber = ?" |
86 |
); |
87 |
$sth->execute($borrower_number); |
88 |
if ( my $already = $sth->fetchrow ) { |
89 |
$hasError = 1; |
90 |
$errAlreadyStartRecovery = 1; |
91 |
} |
92 |
} |
93 |
|
94 |
if ($hasError) { |
95 |
$template->param( |
96 |
hasError => 1, |
97 |
errNoEmailFound => $errNoEmailFound, |
98 |
errAlreadyStartRecovery => $errAlreadyStartRecovery, |
99 |
password_recovery => 1, |
100 |
email => HTML::Entities::encode($email), |
101 |
); |
102 |
} |
103 |
else { |
104 |
#generate uuid and send recovery email |
105 |
if ( SendPasswordRecoveryEmail( $borrower_number, $email, $query ) ) { |
106 |
# if it works.... |
107 |
$template->param( |
108 |
mail_sent => 1, |
109 |
email => $email |
110 |
); |
111 |
} |
112 |
else { |
113 |
# if it doesnt work.... |
114 |
$template->param( |
115 |
password_recovery => 1, |
116 |
sendmailError => 1 |
117 |
); |
118 |
} |
119 |
} |
120 |
} |
121 |
elsif ( $query->param('passwordReset') ) { |
122 |
#new password form |
123 |
#check if the link is still valid |
124 |
my $sth = $dbh->prepare( |
125 |
"SELECT borrower_password_recovery.borrowernumber, userid |
126 |
FROM borrower_password_recovery, borrowers |
127 |
WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber |
128 |
AND NOW() < valid_until |
129 |
AND uuid = ?" |
130 |
); |
131 |
$sth->execute($uniqueKey); |
132 |
( $borrower_number, $username ) = $sth->fetchrow; |
133 |
|
134 |
#validate password length & match |
135 |
if ( ($borrower_number) |
136 |
&& ( $password eq $repeatPassword ) |
137 |
&& ( length($password) >= $minPassLength ) ) |
138 |
{ #apply changes |
139 |
changepassword( $username, $borrower_number, md5_base64($password) ); |
140 |
|
141 |
#remove entry |
142 |
my $sth = $dbh->prepare( |
143 |
"DELETE FROM borrower_password_recovery |
144 |
WHERE uuid = ? |
145 |
ORDER BY valid_until DESC LIMIT 1" |
146 |
); |
147 |
$sth->execute($uniqueKey); |
148 |
|
149 |
$template->param( |
150 |
password_reset_done => 1, |
151 |
username => $username |
152 |
); |
153 |
} |
154 |
else { #errors |
155 |
if ( !$borrower_number ) { #parameters not valid |
156 |
$errLinkNotValid = 1; |
157 |
} |
158 |
elsif ( $password ne $repeatPassword ) { #passwords does not match |
159 |
$errPassNotMatch = 1; |
160 |
} |
161 |
elsif ( length($password) < $minPassLength ) { #password too short |
162 |
$errPassTooShort = 1; |
163 |
} |
164 |
$template->param( |
165 |
new_password => 1, |
166 |
minPassLength => $minPassLength, |
167 |
email => $email, |
168 |
uniqueKey => $uniqueKey, |
169 |
errLinkNotValid => $errLinkNotValid, |
170 |
errPassNotMatch => $errPassNotMatch, |
171 |
errPassTooShort => $errPassTooShort, |
172 |
hasError => 1 |
173 |
); |
174 |
} |
175 |
} |
176 |
elsif ($uniqueKey) { #reset password form |
177 |
#check if the link is valid |
178 |
my $sth = $dbh->prepare( |
179 |
"SELECT borrower_password_recovery.borrowernumber, userid |
180 |
FROM borrower_password_recovery, borrowers |
181 |
WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber |
182 |
AND NOW() < valid_until |
183 |
AND uuid = ?" |
184 |
); |
185 |
$sth->execute($uniqueKey); |
186 |
( $borrower_number, $username ) = $sth->fetchrow; |
187 |
if ( !$borrower_number ) { |
188 |
$errLinkNotValid = 1; |
189 |
} |
190 |
$template->param( |
191 |
new_password => 1, |
192 |
minPassLength => $minPassLength, |
193 |
email => $email, |
194 |
uniqueKey => $uniqueKey, |
195 |
username => $username, |
196 |
errLinkNotValid => $errLinkNotValid |
197 |
); |
198 |
} |
199 |
else { #password recovery form (to send email) |
200 |
$template->param( password_recovery => 1 ); |
201 |
} |
202 |
|
203 |
output_html_with_http_headers $query, $cookie, $template->output; |