+sub _check_divert_core ($) {
+ my ($r) = @_;
+
+ my $meth = $r->_ch('get_method');
+ my $cooks = $r->_ch('get_cookie');
+ my $parmh = $r->_rp('assoc_param_name');
+ my $cookh = defined $cooks ? $r->hash($cooks) : undef;
+
+ my ($cookt,$cooku) = $r->_identify($cookh, $cooks);
+ my $parmt = $r->_identify($parmh, undef);
+
+ print STDERR "_c_d_c cookt=$cookt parmt=$parmt\n";
+
+ if ($r->_ch('is_logout')) {
+ $r->_must_be_post();
+ die unless $parmt;
+ $r->_db_revoke($cookh);
+ $r->_db_revoke($parmh);
+ return ({ Kind => 'REDIRECT-LOGGEDOUT',
+ Message => "Logging out...",
+ CookieSecret => '',
+ Params => { } });
+ }
+ if ($r->_ch('is_loggedout')) {
+ die unless $meth eq 'GET';
+ die unless $cookt;
+ die unless $parmt;
+ return ({ Kind => 'SMALLPAGE-LOGGEDOUT',
+ Message => "You have been logged out.",
+ CookieSecret => '',
+ Params => { } });
+ }
+ if ($r->_ch('is_login')) {
+ $r->_must_be_post();
+ die unless $parmt;
+ if (!$cookt && $parmt eq 't') {
+ return ({ Kind => 'SMALLPAGE-NOCOOKIE',
+ Message => "You do not seem to have cookies enabled. ".
+ "You must enable cookies as we use them for login.",
+ CookieSecret => $r->_fresh_secret(),
+ Params => $r->_chain_params() })
+ }
+ if (!$cookt || $cookt eq 'n' || $cookh ne $parmh) {
+ $r->_db_revoke($cookh);
+ return ({ Kind => 'LOGIN-STALE',
+ Message => "Stale session; you need to log in again.",
+ CookieSecret => $r->_fresh_secret(),
+ Params => { } })
+ }
+ die unless $parmt eq 't' || $parmt eq 'y';
+ my $username = $r->_ch('login_ok');
+ unless (defined $username && length $username) {
+ return ({ Kind => 'LOGIN-BAD',
+ Message => "Incorrect username/password.",
+ CookieSecret => $cooks,
+ Params => $r->_chain_params() })
+ }
+ $r->_db_record_login_ok($parmh,$username);
+ return ({ Kind => 'REDIRECT-LOGGEDIN',
+ Message => "Logging in...",
+ CookieSecret => $cooks,
+ Params => $r->_chain_params() });
+ }
+ if ($cookt eq 't') {
+ $cookt = '';
+ }
+ die if $parmt eq 't';
+
+ if ($cookt eq 'y' && $parmt eq 'y' && $cookh ne $parmh) {
+ $r->_db_revoke($parmh) if $meth eq 'POST';
+ $parmt = 'n';
+ }
+
+ if ($cookt ne 'y') {
+ die unless !$cookt || $cookt eq 'n';
+ die unless !$parmt || $parmt eq 'n' || $parmt eq 'y';
+ my $news = $r->_fresh_secret();
+ if ($meth eq 'GET') {
+ return ({ Kind => 'LOGIN-INCOMINGLINK',
+ Message => "You need to log in again.",
+ CookieSecret => $news,
+ Params => $r->_chain_params() });
+ } else {
+ $r->_db_revoke($parmh);
+ return ({ Kind => 'LOGIN-FRESH',
+ Message => "You need to log in again.",
+ CookieSecret => $news,
+ Params => { } });
+ }
+ }