1 package PciIds::Html::Users;
4 use PciIds::Html::Util;
5 use PciIds::Html::Forms;
11 use Apache2::Const qw(:common);
12 use Apache2::SubRequest;
17 our @EXPORT = qw(&checkLogin ¬LoggedComplaint);
19 sub genRegisterForm( $$$$ ) {
20 my( $req, $args, $error, $values ) = @_;
21 genHtmlHead( $req, 'Register a new user', undef );
22 print "<div class='top'>\n";
23 print '<h1>Register a new user</h1>';
24 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
25 print "<div class='clear'></div></div>\n";
26 print '<div class="error">'.$error.'</div>' if( defined $error );
27 print '<form name="register" id="register" method="POST" action="">
29 genForm( [ [ 'Email:', 'text', 'email', 'maxlength="255"' ],
30 [ '', 'submit', 'register', 'value="Register"' ] ], $values );
31 print '</table></form>';
36 sub registerForm( $$ ) {#Form for registering a new user
37 my( $req, $args ) = @_;
38 return genRegisterForm( $req, $args, undef, {} );
41 sub loginCheck( $$ ) {
42 my( $login, $tables ) = @_;
43 return undef if( ( not defined $login ) || ( $login eq '' ) );#empty login is ok
44 return 'Login too long' if( ( length $login ) > 50 );
45 return 'Login contains invalid characters' unless( $login =~ /^[-_a-zA-Z0-9]+$/ );
46 return 'This login already exists' if( $tables->hasLogin( $login ) );
50 sub registerSubmit( $$$ ) {#A registration form has been submited
51 my( $req, $args, $tables ) = @_;
52 my( $data, $error ) = getForm( {
54 return emailCheck( shift, $tables );
57 return genRegisterForm( $req, $args, $error, $data ) if( defined $error );
58 my $site = $req->hostname();
59 my $url = 'https://'.$req->hostname().setAddrPrefix( $req->uri(), 'mods' );
60 sendMail( $data->{'email'}, 'Confirm registration', "Someone, probably you, requested registration of this address\n".
61 "for the $site site. If it wasn't you, please ignore this email message.\n".
62 "\nOtherwise, please continue by filling in the form at this address:".
63 "\n".$url.'?action=register-confirm?email='.$data->{'email'}.'?confirm='.emailConfirm( $data->{'email'} )."\n".
65 "\n(This is an autogenerated email, do not respond to it)" );
66 genHtmlHead( $req, 'Registration email sent', undef );
67 print "<div class='top'>\n";
68 print "<h1>Registration email sent</h1>\n";
69 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
70 print "<div class='clear'></div></div>\n";
72 An email containing further information has been sent to you.
73 Please follow these instruction to finish the registration process.';
78 sub genConfirmForm( $$$$ ) {
79 my( $req, $args, $error, $values ) = @_;
80 genHtmlHead( $req, 'Confirm registration', undef );
81 print "<div class='top'>\n";
82 print '<h1>Confirm registration</h1>';
83 genLocMenu( $req, $args, [ [ 'Register', 'register' ], [ 'Help', 'help', 'account' ] ] );
84 print "<div class='clear'></div></div>\n";
85 print '<div class="error">'.$error.'</div>' if( defined $error );
86 print '<p>Email address: '.encode( $values->{'email'} );
87 print '<form name="register-confirm" id="register-confirm" method="POST" action="">';
88 print '<div class="hidden"><p><input type="hidden" value="'.encode( $values->{'email'} ).'" name="email"><input type="hidden" value="'.encode( $values->{'confirm'} ).'" name="confirm"></div>';
90 genForm( [ [ 'Login (Optional):', 'text', 'login', 'maxlength="50"' ],
91 [ 'Password:', 'password', 'password' ],
92 [ 'Confirm password:', 'password', 'confirm_password' ],
93 [ '', 'submit', 'register', 'value=Register' ] ], $values );
94 print '</table></form>';
99 sub usedAddress( $$ ) {
100 my( $req, $args ) = @_;
101 genHtmlHead( $req, 'Used address', undef );
102 print "<div class='top'>\n";
103 print "<h1>Used address</h1>\n";
104 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Reset password', 'respass' ], [ 'Register', 'register' ], [ 'Help', 'help', 'account' ] ] );
105 print "<div class='clear'></div></div>\n";
106 print '<div class="error">
108 An account for this address is already registered.
109 Please, reset or remember your password or start again with a different address.
115 sub checkRegHash( $$$$$ ) {
116 my( $req, $args, $tables, $email, $hash ) = @_;
117 if( ! checkConfirmHash( $email, $hash ) ) {
118 genHtmlHead( $req, 'Invalid registration request', undef );
119 print '<h1>Invalid registration request</h1>
122 This registration request is invalid.
123 Are you sure you got it from the registration email?
127 } elsif( $tables->hasEmail( $email ) ) {
128 return usedAddress( $req, $args );
134 sub confirmForm( $$$$ ) {
135 my( $req, $args, $tables, $auth ) = @_;
136 return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
137 if( ! checkRegHash( $req, $args, $tables, $args->{'email'}, $args->{'confirm'} ) ) {
140 return genConfirmForm( $req, $args, undef, $args );
144 sub passLenCheck( $ ) {
146 return ( ( length $pass ) >= 4 ) ? undef : 'Password must have at least 4 characters';
149 sub passSameCheck( $ ) {
151 return ( ( ( defined $data->{'password'} ) != ( defined $data->{'confirm_password'} ) ) || ( ( defined $data->{'password'} ) && ( $data->{'password'} ne $data->{'confirm_password'} ) ) ) ? 'Passwords do not match' : undef;
154 sub confirmSubmit( $$$ ) {
155 my( $req, $args, $tables ) = @_;
156 my( $data, $error ) = getForm( {
158 return emailCheck( shift, $tables );
162 return loginCheck( shift, $tables );
164 'password' => \&passLenCheck,
165 'confirm_password' => undef }, [ \&passSameCheck ] );
166 return OK if( ! checkRegHash( $req, $args, $tables, $data->{'email'}, $data->{'confirm'} ) );#Not much info, but this is an attack anyway
167 return genConfirmForm( $req, $args, $error, $data ) if( defined $error );
168 unless( addUser( $tables, $data->{'login'}, $data->{'email'}, $data->{'password'} ) ) {
169 usedAddress( $req, $args );
172 genHtmlHead( $req, 'Registered', undef );
173 print "<div class='top'>\n";
174 print "<h1>Registered</h1>\n";
175 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
176 print "<div class='clear'></div></div>\n";
178 You have registered successfully.';
183 sub genLoginForm( $$$$ ) {
184 my( $req, $args, $error, $values ) = @_;
185 $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'cookie-test', -value => 1 ) );
186 genHtmlHead( $req, 'Login', undef );
187 print "<div class='top'>\n";
188 print '<h1>Login</h1>';
189 genLocMenu( $req, $args, [ [ 'Register', 'register' ], [ 'Reset password', 'respass' ], [ 'Help', 'help', 'account' ] ] );
190 print "<div class='clear'></div></div>\n";
191 print '<div class="error"><p>'.$error.'</div>' if( defined $error );
192 print '<form name="login" id="login" method="POST" action="'.setAddrPrefix( $req->uri(), 'mods' ).buildExcept( 'action', $args ).'?action=login"><table>';
193 genForm( [ [ 'Login name or email:', 'text', 'login', 'maxlength="255"' ],
194 [ 'Password:', 'password', 'password' ],
195 [ '', 'submit', 'login', 'value="Login"' ] ], $values );
196 print '</table></form>';
201 sub loginForm( $$$ ) {
202 my( $req, $args, $tables, $auth ) = @_;
203 return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless( $auth->{'ssl'} );
204 return genLoginForm( $req, $args, undef, {} );
207 sub loginSubmit( $$$ ) {
208 my( $req, $args, $tables ) = @_;
209 my( $data, $error ) = getForm( {
214 my $cookies = fetch CGI::Cookie;
215 unless( $cookies->{'cookie-test'} ) {
216 return genLoginForm( $req, $args, 'You need to enable cookies', $data );
218 my( $id, $passwd, $email, $last ) = $tables->getLogInfo( $data->{'login'} );
219 if( defined $passwd && defined $data->{'password'} ) {
220 my $salted = saltedPasswd( $email, $data->{'password'} );
221 $logged = $salted eq $passwd;
224 $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) ) );
225 $args->{'action'} = ( defined $args->{'redirectaction'} ) ? $args->{'redirectaction'} : 'list';
226 my $prefix = ( !defined( $args->{'action'} ) or ( $args->{'action'} eq '' ) or ( $args->{'action'} eq 'list' ) ) ? 'read' : 'mods';
227 my $url = "http://".$req->hostname().setAddrPrefix( $req->uri(), $prefix ).buildExcept( 'redirectaction', $args );
228 genHtmlHead( $req, 'Logged in', undef );
229 print "<div class='top'>\n";
230 print '<h1>Logged in</h1>';
231 genPath( $req, PciIds::Address::new( $req->uri() ), 1 );
232 print "<div class='clear'></div></div>\n";
233 print "<p>You are logged in" . ( defined $args->{'redirectaction'} ? ", continue with your <a href='$url'>action</a>.\n" : ".\n" );
234 print '<div class="lastlog"><p>'.encode( $last ).'</div>' if( defined( $last ) );
238 return genLoginForm( $req, $args, 'Invalid login credetials', $data );
243 my( $req, $args, $tables, $auth ) = @_;
244 $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => '0' ) );
245 return PciIds::Html::List::list( $req, $args, $tables, {} );
248 sub checkLogin( $$ ) {
249 my( $req, $tables ) = @_;
250 my $cookies = fetch CGI::Cookie;
251 my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookies->{'auth'} ) ? $cookies->{'auth'}->value : undef );
253 $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights, $name ) ) );
255 my $hterror = $authed ? '' : '<div class="error"><p>'.$error.'</div>';
256 return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror, 'name' => $authed ? $name : undef };
259 sub notLoggedComplaint( $$$ ) {
260 my( $req, $args, $auth ) = @_;
261 return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
262 $args->{'redirectaction'} = $args->{'action'};
263 return genLoginForm( $req, $args, 'This action requires you to be logged in', undef );
266 sub genResetPasswdForm( $$$$ ) {
267 my( $req, $args, $error, $values ) = @_;
268 genHtmlHead( $req, 'Reset password', undef );
269 print "<div class='top'>\n";
270 print "<h1>Reset password</h1>\n";
271 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Register', 'register' ], [ 'Help', 'help', 'account' ] ] );
272 print "<div class='clear'></div></div>\n";
273 print "<p>If you forgot your password (or didn't create one yet), you can reset it to a new value here.\n";
274 print "Provide your email address here and further instructions will be sent to you.\n";
275 print '<div class="error">'.$error.'</div>' if( defined $error );
276 print '<form name="respass" id="respass" method="POST" action="">
278 genForm( [ [ 'Email:', 'text', 'email', 'maxlength="255"' ],
279 [ '', 'submit', 'respass', 'value="Send"' ] ], $values );
280 print '</table></form>';
285 sub resetPasswdForm( $$$$ ) {
286 my( $req, $args ) = @_;
287 return genResetPasswdForm( $req, $args, undef, {} );
290 sub resetPasswdFormSubmit( $$$ ) {
291 my( $req, $args, $tables ) = @_;
292 my( $data, $error ) = getForm( {
295 my( $id, $login, $passwd ) = $tables->resetInfo( $data->{'email'} );
296 if( defined( $id ) ) {
297 $login = '' unless( defined( $login ) );
298 my $site = $req->hostname();
299 my $url = 'https://'.$req->hostname().setAddrPrefix( $req->uri(), 'mods' );
300 my $hash = genResetHash( $id, $data->{'email'}, $login, $passwd );
301 sendMail( $data->{'email'}, 'Reset password',
302 "A request to reset password for the $site site was received for this address\n".
303 "If you really wish to get a new password, visit this link:\n\n".
304 $url.'?action=respass-confirm?email='.$data->{'email'}.'?confirm='.$hash."\n".
306 "\n(This is an autogenerated email, do not respond to it)" );
307 genHtmlHead( $req, 'Reset password', undef );
308 print "<div class='top'>\n";
309 print "<h1>Reset password</h1>\n";
310 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
311 print "<div class='clear'></div></div>\n";
312 print "<p>An email with information has been sent to your address.\n";
316 $error = '<p>This email address is not registered. Check it for typos or register it.';
318 return genResetPasswdForm( $req, $args, $error, $data ) if( defined( $error ) );
321 sub genResetPasswdConfigForm( $$$$$$ ) {
322 my( $req, $args, $error, $values, $email, $hash ) = @_;
323 genHtmlHead( $req, 'Reset password', undef );
324 print "<div class='top'>\n";
325 print "<h1>Reset password</h1>\n";
326 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
327 print "<div class='clear'></div></div>\n";
328 print '<div class="error">'.$error.'</div>' if( defined $error );
329 print "<p>You can enter new password here:\n";
330 print '<form name="respass-confirm" id="respass-confirm" method="POST" action="">
332 genForm( [ [ 'Password:', 'password', 'password' ],
333 [ 'Confirm password:', 'password', 'confirm_password' ],
334 [ '', 'submit', 'respass', 'value="Send"' ] ], $values );
336 print "<input type='hidden' name='email' value='".encode( $email )."'><input type='hidden' name='hash' value='".encode( $hash )."'>\n";
342 sub resetPasswdConfirmForm( $$$$ ) {
343 my( $req, $args, $tables, $auth ) = @_;
344 my( $email, $hash ) = ( $args->{'email'}, $args->{'confirm'} );
345 my( $id, $login, $passwd ) = $tables->resetInfo( $email );
347 return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
348 $myHash = genResetHash( $id, $email, $login, $passwd ) if( defined( $id ) );
349 if( defined( $myHash ) && ( $myHash eq $hash ) ) {#Ok, it is his mail and he asked
350 return genResetPasswdConfigForm( $req, $args, undef, {}, $email, $hash );
352 genHtmlHead( $req, 'Reset password', undef );
353 print "<h1>Reset password</h1>\n";
354 print "<p>Provided link is not valid. Did you use it already?\n";
355 print "<p>You can get a <a href='".$req->uri()."?action=respass'>new one</a>.\n";
361 sub resetPasswdConfirmFormSubmit( $$$ ) {
362 my( $req, $args, $tables ) = @_;
363 my( $data, $error ) = getForm( {
364 'password' => \&passLenCheck,
365 'confirm_password' => undef,
368 }, [ \&passSameCheck ] );
369 my( $email, $hash ) = ( $data->{'email'}, $args->{'confirm'} );
370 if( defined( $error ) ) {
371 return genResetPasswdConfigForm( $req, $args, $error, $data, $email, $hash );
373 my( $id, $login, $passwd ) = $tables->resetInfo( $email );
375 $myHash = genResetHash( $id, $email, $login, $passwd ) if( defined( $id ) );
376 if( defined( $myHash ) && ( $myHash eq $hash ) ) {
377 changePasswd( $tables, $id, $data->{'password'}, $email );
378 genHtmlHead( $req, 'Reset password', undef );
379 print "<div class='top'>\n";
380 print "<h1>Reset password</h1>\n";
381 genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
382 print "<div class='clear'></div></div>\n";
383 print "<p>Your password was successfuly changed.\n";
387 return genResetPasswdConfigForm( $req, $args, $error, $data, $email, $hash );
392 sub genProfileForm( $$$$$$ ) {
393 my( $req, $args, $auth, $error, $data, $info ) = @_;
394 genHtmlHead( $req, 'User profile', undef );
395 delete $data->{'current_password'};
396 delete $data->{'confirm_password'};
397 delete $data->{'password'};
398 print "<div class='top'>\n";
399 print "<h1>User profile</h1>\n";
400 genLocMenu( $req, $args, [ logItem( $auth ), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'profile' ] ] );
401 print "<div class='clear'></div></div>\n";
402 print '<div class="error"><p>'.$error.'</div>' if defined $error;
403 print "<div class='info'><p>$info</div>\n" if defined $info;
404 print '<form name="profile" id="profile" method="POST" action=""><table>';
405 genForm( [ [ 'Email:', 'text', 'email', 'maxlength="255"' ],
406 [ 'Login:', 'text', 'login', 'maxlength="50"' ],
407 [ 'Xmpp:', 'text', 'xmpp', 'maxlength="255"' ],
408 [ 'New password:', 'password', 'password' ],
409 [ 'Confirm password:', 'password', 'confirm_password' ],
410 [ 'Current password:', 'password', 'current_password' ],
411 [ 'Email batch time (min):', 'text', 'email_time', 'maxlength="10"' ],
412 [ 'Xmpp batch time (min):', 'text', 'xmpp_time', 'maxlength="10"' ],
413 [ '', 'submit', 'profile', 'value="Submit"' ] ], $data );
414 print '</table></form>';
419 sub profileForm( $$$$ ) {
420 my( $req, $args, $tables, $auth ) = @_;
421 return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
422 return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
423 return genProfileForm( $req, $args, $auth, undef, $tables->profileData( $auth->{'authid'} ), undef );
427 my( $value, $name ) = @_;
428 return ( "$name has invalid number format", '0' ) unless ( $value =~ /\d+/ );
432 sub profileFormSubmit( $$$$ ) {
433 my( $req, $args, $tables, $auth ) = @_;
434 return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
435 my $oldData = $tables->profileData( $auth->{'authid'} );
436 my( $data, $error ) = getForm( {
439 return undef if ( defined $email ) && ( $email eq $oldData->{'email'} );
440 return emailCheck( $email, $tables );
444 $login = undef if ( defined $login ) && ( $login eq '' );
445 return undef if ( defined $login ) && ( defined $oldData->{'login'} ) && ( $oldData->{'login'} eq $login );
446 return ( undef, $login ) if ( !defined $login ) && ( !defined $oldData->{'login'} );
447 return loginCheck( $login, $tables );
451 return ( undef, undef ) if ( !defined $xmpp ) || ( $xmpp eq '' );
452 return "Xmpp address limit is 255" if length $xmpp > 255;
453 return "Invalid Xmpp address" unless $xmpp =~ /^([^'"\@<>\/]+\@)?[^\@'"<>\/]+(\/.*)?/;
458 $passwd = undef if ( defined $passwd ) && ( $passwd eq '' );
459 return ( undef, undef ) unless defined $passwd;
460 return passLenCheck( $passwd );
462 'confirm_password' => undef,
463 'current_password' => undef,
464 'email_time' => sub {
465 return checkNum( shift, "Email batch time" );
468 return checkNum( shift, "Xmpp batch time" );
472 return undef unless defined $data->{'password'};
473 return passSameCheck( $data );
477 $change = 1 if $data->{'email'} ne $oldData->{'email'};
478 $change = 1 if ( ( ( defined $data->{'login'} ) != ( defined $oldData->{'login'} ) ) || ( ( defined $data->{'login'} ) && ( defined $oldData->{'login'} ) && ( $data->{'login'} ne $oldData->{'login'} ) ) );
479 $change = 1 if ( defined $data->{'password'} ) && ( $data->{'password'} ne '' );
480 return undef unless $change;
482 my( $id, $passwd, $email, $last ) = $tables->getLogInfo( $oldData->{'email'} );
483 if( defined $passwd && defined $data->{'current_password'} ) {
484 my $salted = saltedPasswd( $email, $data->{'current_password'} );
485 $logged = ( $salted eq $passwd ) && ( $id == $auth->{'authid'} );
487 return "You need to provide correct current password to change email, login or password" unless $logged;
490 return genProfileForm( $req, $args, $auth, $error, $data, undef ) if defined $error;
491 pushProfile( $tables, $auth->{'authid'}, $oldData, $data );
492 return genProfileForm( $req, $args, $auth, undef, $data, "Profile updated." );