]> mj.ucw.cz Git - pciids.git/blobdiff - PciIds/Html/Users.pm
Do the login by a redirect
[pciids.git] / PciIds / Html / Users.pm
index eb0b6f133608f25205ae895d39e9f743d35a14a1..7783f0c04ab2e988a4893356c9a8c844975b22a1 100644 (file)
@@ -5,6 +5,7 @@ use PciIds::Html::Util;
 use PciIds::Html::Forms;
 use PciIds::Email;
 use PciIds::Users;
+use PciIds::Address;
 use CGI;
 use CGI::Cookie;
 use Apache2::Const qw(:common);
@@ -18,7 +19,10 @@ our @EXPORT = qw(&checkLogin &notLoggedComplaint);
 sub genRegisterForm( $$$$ ) {
        my( $req, $args, $error, $values ) = @_;
        genHtmlHead( $req, 'Register a new user', undef );
+       print "<div class='top'>\n";
        print '<h1>Register a new user</h1>';
+       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
        print '<div class="error">'.$error.'</div>' if( defined $error );
        print '<form name="register" id="register" method="POST" action="">
                <table>';
@@ -43,15 +47,6 @@ sub loginCheck( $$ ) {
        return undef;
 }
 
-sub emailCheck( $$ ) {
-       my( $email, $tables ) = @_;
-       my $newmail;
-       return 'Does not look like an email address' unless ( ( $newmail ) = ( $email =~ /^([^,? "'`;]+@[^@,?\/ "'`;]+)$/ ) );#make sure the mail is not only reasonable looking, but safe to work with too
-       return 'Email too long' if length $newmail > 255;
-       return 'An account for this email address already exists' if( $tables->hasEmail( $newmail ) );
-       return ( undef, $newmail );
-}
-
 sub registerSubmit( $$$ ) {#A registration form has been submited
        my( $req, $args, $tables ) = @_;
        my( $data, $error ) = getForm( {
@@ -69,8 +64,11 @@ sub registerSubmit( $$$ ) {#A registration form has been submited
                "\nThank you\n".
                "\n(This is an autogenerated email, do not respond to it)" );
        genHtmlHead( $req, 'Registration email sent', undef );
-       print '<h1>Register email sent</h1>
-               <p>
+       print "<div class='top'>\n";
+       print "<h1>Registration email sent</h1>\n";
+       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
+       print '<p>
                        An email containing further information has been sent to you.
                        Please follow these instruction to finish the registration process.';
        genHtmlTail();
@@ -80,7 +78,10 @@ sub registerSubmit( $$$ ) {#A registration form has been submited
 sub genConfirmForm( $$$$ ) {
        my( $req, $args, $error, $values ) = @_;
        genHtmlHead( $req, 'Confirm registration', undef );
+       print "<div class='top'>\n";
        print '<h1>Confirm registration</h1>';
+       genLocMenu( $req, $args, [ [ 'Register', 'register' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
        print '<div class="error">'.$error.'</div>' if( defined $error );
        print '<p>Email address: '.encode( $values->{'email'} );
        print '<form name="register-confirm" id="register-confirm" method="POST" action="">';
@@ -95,21 +96,24 @@ sub genConfirmForm( $$$$ ) {
        return OK;
 }
 
-sub usedAddress( $ ) {
-       my( $req ) = @_;
+sub usedAddress( $$ ) {
+       my( $req, $args ) = @_;
        genHtmlHead( $req, 'Used address', undef );
-       print '<h1>Used address</h1>
-               <div class="error">
+       print "<div class='top'>\n";
+       print "<h1>Used address</h1>\n";
+       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Reset password', 'respass' ], [ 'Register', 'register' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
+       print '<div class="error">
                <p>
                        An account for this address is already registered.
-                       Please, start again with <a href="'.setAddrPrefix( $req->uri(), 'mods' ).'?action=register">requesting a registration email</a> or <a href="'.setAddrPrefix( $req->uri(), 'mods' ).'?action=login">log in</a>.
+                       Please, reset or remember your password or start again with a different address.
                </div>';
        genHtmlTail();
        return 0;
 }
 
-sub checkRegHash( $$$$ ) {
-       my( $req, $tables, $email, $hash ) = @_;
+sub checkRegHash( $$$$$ ) {
+       my( $req, $args, $tables, $email, $hash ) = @_;
        if( ! checkConfirmHash( $email, $hash ) ) {
                genHtmlHead( $req, 'Invalid registration request', undef );
                print '<h1>Invalid registration request</h1>
@@ -121,7 +125,7 @@ sub checkRegHash( $$$$ ) {
                genHtmlTail();
                return 0;
        } elsif( $tables->hasEmail( $email ) ) {
-               return usedAddress( $req );
+               return usedAddress( $req, $args );
        } else {
                return 1;
        }
@@ -130,7 +134,7 @@ sub checkRegHash( $$$$ ) {
 sub confirmForm( $$$$ ) {
        my( $req, $args, $tables, $auth ) = @_;
        return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
-       if( ! checkRegHash( $req, $tables, $args->{'email'}, $args->{'confirm'} ) ) {
+       if( ! checkRegHash( $req, $args, $tables, $args->{'email'}, $args->{'confirm'} ) ) {
                return OK;
        } else {
                return genConfirmForm( $req, $args, undef, $args );
@@ -159,17 +163,19 @@ sub confirmSubmit( $$$ ) {
                },
                'password' => \&passLenCheck,
                'confirm_password' => undef }, [ \&passSameCheck ] );
-       return OK if( ! checkRegHash( $req, $tables, $data->{'email'}, $data->{'confirm'} ) );#Not much info, but this is an attack anyway
+       return OK if( ! checkRegHash( $req, $args, $tables, $data->{'email'}, $data->{'confirm'} ) );#Not much info, but this is an attack anyway
        return genConfirmForm( $req, $args, $error, $data ) if( defined $error );
        unless( addUser( $tables, $data->{'login'}, $data->{'email'}, $data->{'password'} ) ) {
-               usedAddress( $req );
+               usedAddress( $req, $args );
                return OK;
        }
        genHtmlHead( $req, 'Registered', undef );
-       print '<h1>Registered</h1>
-               <p>
-                       You are now registered.
-                       You can continue by <a href="'.setAddrPrefix( $req->uri(), 'mods' ).buildExcept( 'action', $args ).'?action=login">logging in</a> or continue <a href="http://'.$req->hostname().setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args ).'?action=list">anonymously</a>.';
+       print "<div class='top'>\n";
+       print "<h1>Registered</h1>\n";
+       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
+       print '<p>
+                       You have registered successfully.';
        genHtmlTail();
        return OK;
 }
@@ -178,9 +184,10 @@ sub genLoginForm( $$$$ ) {
        my( $req, $args, $error, $values ) = @_;
        $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'cookie-test', -value => 1 ) );
        genHtmlHead( $req, 'Login', undef );
+       print "<div class='top'>\n";
        print '<h1>Login</h1>';
-       my $addr = PciIds::Address::new( $req->uri() );
-       genCustomMenu( $addr, $args, [ [ 'Register', 'register' ], [ 'Reset password', 'respass' ] ] );
+       genLocMenu( $req, $args, [ [ 'Register', 'register' ], [ 'Reset password', 'respass' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
        print '<div class="error"><p>'.$error.'</div>' if( defined $error );
        print '<form name="login" id="login" method="POST" action="'.setAddrPrefix( $req->uri(), 'mods' ).buildExcept( 'action', $args ).'?action=login"><table>';
        genForm( [ [ 'Login name or email:', 'text', 'login', 'maxlength="255"' ],
@@ -214,36 +221,31 @@ sub loginSubmit( $$$ ) {
                $logged = $salted eq $passwd;
        }
        if( $logged ) {
-               $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef ) ) );
-               $args->{'action'} = ( defined $args->{'redirectaction'} ) ? $args->{'redirectaction'} : 'list';
-               my $prefix = ( !defined( $args->{'action'} ) or ( $args->{'action'} eq '' ) or ( $args->{'action'} eq 'list' ) ) ? 'read' : 'mods';
-               my $url = "http://".$req->hostname().setAddrPrefix( $req->uri(), $prefix ).buildExcept( 'redirectaction', $args );
-               genHtmlHead( $req, 'Logged in', undef );
-               print '<h1>Logged in</h1>';
-               print '<div class="lastlog"><p>'.encode( $last ).'</div>' if( defined( $last ) );
-               print "<p><a href='$url'>Continue here</a>";
-               genHtmlTail();
-               return OK;
+               $req->err_headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) ) );
+               $args->{'action'} = ( defined $args->{'redirectaction'} && $args->{'redirectaction'} ne '' ) ? $args->{'redirectaction'} : 'list';
+               my $url = 'http://'.$req->hostname().setAddrPrefix( $req->uri(), $args->{'action'} eq 'list' ? 'read' : 'mods' ).buildExcept( 'redirectaction', $args );
+               return HTTPRedirect( $req, $url );
        } else {
                return genLoginForm( $req, $args, 'Invalid login credetials', $data );
        }
 }
 
 sub logout( $$ ) {
-       my( $req, $args, $tables, $auth ) = @_;
-       $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => '0' ) );
-       return PciIds::Html::List::list( $req, $args, $tables, {} );
+       my( $req, $args ) = @_;
+       $req->err_headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => '0' ) );
+       return HTTPRedirect( $req, 'http://'.$req->hostname().setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args ) );
 }
 
 sub checkLogin( $$ ) {
        my( $req, $tables ) = @_;
        my $cookies = fetch CGI::Cookie;
-       my( $authed, $id, $regen, $rights, $error ) = checkAuthToken( $tables, $req, defined( $cookies->{'auth'} ) ? $cookies->{'auth'}->value : undef );
+       my $cookie = $cookies->{'auth'};
+       my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookie ) ? $cookie->value : undef );
        if( $regen ) {
-               $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights ) ) );
+               $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights, $name ) ) );
        }
        my $hterror = $authed ? '' : '<div class="error"><p>'.$error.'</div>';
-       return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror };
+       return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror, 'name' => $authed ? $name : undef };
 }
 
 sub notLoggedComplaint( $$$ ) {
@@ -256,7 +258,10 @@ sub notLoggedComplaint( $$$ ) {
 sub genResetPasswdForm( $$$$ ) {
        my( $req, $args, $error, $values ) = @_;
        genHtmlHead( $req, 'Reset password', undef );
+       print "<div class='top'>\n";
        print "<h1>Reset password</h1>\n";
+       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Register', 'register' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
        print "<p>If you forgot your password (or didn't create one yet), you can reset it to a new value here.\n";
        print "Provide your email address here and further instructions will be sent to you.\n";
        print '<div class="error">'.$error.'</div>' if( defined $error );
@@ -292,12 +297,15 @@ sub resetPasswdFormSubmit( $$$ ) {
                        "\n\nThank you\n".
                        "\n(This is an autogenerated email, do not respond to it)" );
                genHtmlHead( $req, 'Reset password', undef );
+               print "<div class='top'>\n";
                print "<h1>Reset password</h1>\n";
-               print "<p>An email with information was sent to your address.\n";
+               genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
+               print "<div class='clear'></div></div>\n";
+               print "<p>An email with information has been sent to your address.\n";
                genHtmlTail();
                return OK;
        } else {
-               $error = '<p>This email address is not registered. Check it for typos or <a href="'.setAddrPrefix( $req->uri(), 'mods' ).buildExcept( 'action', $args ).'?action=register">register</a> it.';
+               $error = '<p>This email address is not registered. Check it for typos or register it.';
        }
        return genResetPasswdForm( $req, $args, $error, $data ) if( defined( $error ) );
 }
@@ -305,7 +313,10 @@ sub resetPasswdFormSubmit( $$$ ) {
 sub genResetPasswdConfigForm( $$$$$$ ) {
        my( $req, $args, $error, $values, $email, $hash ) = @_;
        genHtmlHead( $req, 'Reset password', undef );
+       print "<div class='top'>\n";
        print "<h1>Reset password</h1>\n";
+       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
+       print "<div class='clear'></div></div>\n";
        print '<div class="error">'.$error.'</div>' if( defined $error );
        print "<p>You can enter new password here:\n";
        print '<form name="respass-confirm" id="respass-confirm" method="POST" action="">
@@ -357,8 +368,11 @@ sub resetPasswdConfirmFormSubmit( $$$ ) {
                if( defined( $myHash ) && ( $myHash eq $hash ) ) {
                        changePasswd( $tables, $id, $data->{'password'}, $email );
                        genHtmlHead( $req, 'Reset password', undef );
+                       print "<div class='top'>\n";
                        print "<h1>Reset password</h1>\n";
-                       print "<p>Your password was successfuly changed. You can <a href='".$req->uri()."?action=login'>log in</a>.\n";
+                       genLocMenu( $req, $args, [ [ 'Log in', 'login' ], [ 'Help', 'help', 'account' ] ] );
+                       print "<div class='clear'></div></div>\n";
+                       print "<p>Your password was successfuly changed.\n";
                        genHtmlTail();
                        return OK;
                } else {
@@ -367,13 +381,16 @@ sub resetPasswdConfirmFormSubmit( $$$ ) {
        }
 }
 
-sub genProfileForm( $$$$$ ) {
-       my( $req, $args, $error, $data, $info ) = @_;
+sub genProfileForm( $$$$$$ ) {
+       my( $req, $args, $auth, $error, $data, $info ) = @_;
        genHtmlHead( $req, 'User profile', undef );
        delete $data->{'current_password'};
        delete $data->{'confirm_password'};
        delete $data->{'password'};
+       print "<div class='top'>\n";
        print "<h1>User profile</h1>\n";
+       genLocMenu( $req, $args, [ logItem( $auth ), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'profile' ] ] );
+       print "<div class='clear'></div></div>\n";
        print '<div class="error"><p>'.$error.'</div>' if defined $error;
        print "<div class='info'><p>$info</div>\n" if defined $info;
        print '<form name="profile" id="profile" method="POST" action=""><table>';
@@ -387,7 +404,6 @@ sub genProfileForm( $$$$$ ) {
                [ 'Xmpp batch time (min):', 'text', 'xmpp_time', 'maxlength="10"' ],
                [ '', 'submit', 'profile', 'value="Submit"' ] ], $data );
        print '</table></form>';
-       print "<p><a class='navigation' href='http://".$req->hostname().setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args )."?action=list'>Back to browsing</a>\n";
        genHtmlTail();
        return OK;
 }
@@ -396,7 +412,7 @@ sub profileForm( $$$$ ) {
        my( $req, $args, $tables, $auth ) = @_;
        return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
        return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
-       return genProfileForm( $req, $args, undef, $tables->profileData( $auth->{'authid'} ), undef );
+       return genProfileForm( $req, $args, $auth, undef, $tables->profileData( $auth->{'authid'} ), undef );
 }
 
 sub checkNum( $$ ) {
@@ -463,9 +479,9 @@ sub profileFormSubmit( $$$$ ) {
                return "You need to provide correct current password to change email, login or password" unless $logged;
                return undef;
        } ] );
-       return genProfileForm( $req, $args, $error, $data, undef ) if defined $error;
+       return genProfileForm( $req, $args, $auth, $error, $data, undef ) if defined $error;
        pushProfile( $tables, $auth->{'authid'}, $oldData, $data );
-       return genProfileForm( $req, $args, undef, $data, "Profile updated." );
+       return genProfileForm( $req, $args, $auth, undef, $data, "Profile updated." );
 }
 
 1;