use open qw{:utf8 :std};
use Encode;
-sub printheader ($) { #{{{
+sub printheader ($) {
my $session=shift;
if ($config{sslcookie}) {
print $session->header(-charset => 'utf-8',
-cookie => $session->cookie(-httponly => 1, -secure => 1));
- } else {
+ }
+ else {
print $session->header(-charset => 'utf-8',
-cookie => $session->cookie(-httponly => 1));
}
-} #}}}
+}
-sub showform ($$$$;@) { #{{{
+sub prepform {
my $form=shift;
my $buttons=shift;
my $session=shift;
});
}
+ return $form;
+}
+
+sub showform ($$$$;@) {
+ my $form=prepform(@_);
+ shift;
+ my $buttons=shift;
+ my $session=shift;
+ my $cgi=shift;
+
printheader($session);
print misctemplate($form->title, $form->render(submit => $buttons), @_);
}
-sub redirect ($$) { #{{{
+sub redirect ($$) {
my $q=shift;
- my $url=shift;
+ eval q{use URI};
+ my $url=URI->new(shift);
if (! $config{w3mmode}) {
print $q->redirect($url);
}
print "Content-type: text/plain\n";
print "W3m-control: GOTO $url\n\n";
}
-} #}}}
+}
-sub decode_cgi_utf8 ($) { #{{{
- # decode_form_utf8 method is needed for 5.10
+sub decode_cgi_utf8 ($) {
+ # decode_form_utf8 method is needed for 5.01
if ($] < 5.01) {
my $cgi = shift;
foreach my $f ($cgi->param) {
$cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
}
}
-} #}}}
+}
-sub decode_form_utf8 ($) { #{{{
+sub decode_form_utf8 ($) {
if ($] >= 5.01) {
my $form = shift;
foreach my $f ($form->field) {
+ my @value=map { decode_utf8($_) } $form->field($f);
$form->field(name => $f,
- value => decode_utf8($form->field($f)),
+ value => \@value,
force => 1,
);
}
}
-} #}}}
+}
# Check if the user is signed in. If not, redirect to the signin form and
# save their place to return to later.
-sub needsignin ($$) { #{{{
+sub needsignin ($$) {
my $q=shift;
my $session=shift;
cgi_savesession($session);
exit;
}
-} #}}}
+}
-sub cgi_signin ($$) { #{{{
+sub cgi_signin ($$;$) {
my $q=shift;
my $session=shift;
+ my $returnhtml=shift;
decode_cgi_utf8($q);
eval q{use CGI::FormBuilder};
action => $config{cgiurl},
header => 0,
template => {type => 'div'},
- stylesheet => baseurl()."style.css",
+ stylesheet => 1,
);
my $buttons=["Login"];
- if ($q->param("do") ne "signin" && !$form->submitted) {
- $form->text(gettext("You need to log in first."));
- }
$form->field(name => "do", type => "hidden", value => "signin",
force => 1);
$form->validate;
}
+ if ($returnhtml) {
+ $form=prepform($form, $buttons, $session, $q);
+ return $form->render(submit => $buttons);
+ }
+
showform($form, $buttons, $session, $q);
-} #}}}
+}
-sub cgi_postsignin ($$) { #{{{
+sub cgi_postsignin ($$) {
my $q=shift;
my $session=shift;
exit;
}
else {
- error(gettext("login failed, perhaps you need to turn on cookies?"));
+ if ($config{sslcookie} && ! $q->https()) {
+ error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
+ }
+ else {
+ error(gettext("login failed, perhaps you need to turn on cookies?"));
+ }
}
-} #}}}
+}
-sub cgi_prefs ($$) { #{{{
+sub cgi_prefs ($$) {
my $q=shift;
my $session=shift;
params => $q,
action => $config{cgiurl},
template => {type => 'div'},
- stylesheet => baseurl()."style.css",
+ stylesheet => 1,
fieldsets => [
[login => gettext("Login")],
[preferences => gettext("Preferences")],
my $user_name=$session->param("name");
- # XXX deprecated, should be removed eventually
- $form->field(name => "banned_users", size => 50, fieldset => "admin");
- if (! is_admin($user_name)) {
- $form->field(name => "banned_users", type => "hidden");
- }
if (! $form->submitted) {
$form->field(name => "email", force => 1,
value => userinfo_get($user_name, "email"));
- if (is_admin($user_name)) {
- my $value=join(" ", get_banned_users());
- if (length $value) {
- $form->field(name => "banned_users", force => 1,
- value => join(" ", get_banned_users()),
- comment => "deprecated; please move to banned_users in setup file");
- }
- else {
- $form->field(name => "banned_users", type => "hidden");
- }
- }
}
if ($form->submitted eq 'Logout') {
error("failed to set email");
}
- # XXX deprecated, should be removed eventually
- if (is_admin($user_name)) {
- set_banned_users(grep { ! is_admin($_) }
- split(' ',
- $form->field("banned_users"))) ||
- error("failed saving changes");
- if (! length $form->field("banned_users")) {
- $form->field(name => "banned_users", type => "hidden");
- }
- }
-
$form->text(gettext("Preferences saved."));
}
- showform($form, $buttons, $session, $q);
-} #}}}
+ showform($form, $buttons, $session, $q,
+ prefsurl => "", # avoid showing the preferences link
+ );
+}
+
+sub cgi_custom_failure ($$$) {
+ my $q=shift;
+ my $httpstatus=shift;
+ my $message=shift;
+
+ print $q->header(
+ -status => $httpstatus,
+ -charset => 'utf-8',
+ );
+ print $message;
-sub check_banned ($$) { #{{{
+ # Internet Explod^Hrer won't show custom 404 responses
+ # unless they're >= 512 bytes
+ print ' ' x 512;
+
+ exit;
+}
+
+sub check_banned ($$) {
my $q=shift;
my $session=shift;
+ my $banned=0;
my $name=$session->param("name");
- if (defined $name) {
- # XXX banned in userinfo is deprecated, should be removed
- # eventually, and only banned_users be checked.
- if (userinfo_get($session->param("name"), "banned") ||
- grep { $name eq $_ } @{$config{banned_users}}) {
- print $q->header(-status => "403 Forbidden");
- $session->delete();
- print gettext("You are banned.");
- cgi_savesession($session);
- exit;
+ if (defined $name &&
+ grep { $name eq $_ } @{$config{banned_users}}) {
+ $banned=1;
+ }
+
+ foreach my $b (@{$config{banned_users}}) {
+ if (pagespec_match("", $b,
+ ip => $ENV{REMOTE_ADDR},
+ name => defined $name ? $name : "",
+ )) {
+ $banned=1;
+ last;
}
}
+
+ if ($banned) {
+ $session->delete();
+ cgi_savesession($session);
+ cgi_custom_failure(
+ $q, "403 Forbidden",
+ gettext("You are banned."));
+ }
}
-sub cgi_getsession ($) { #{{{
+sub cgi_getsession ($) {
my $q=shift;
- eval q{use CGI::Session};
+ eval q{use CGI::Session; use HTML::Entities};
error($@) if $@;
- CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
+ CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
my $oldmask=umask(077);
my $session = eval {
umask($oldmask);
return $session;
-} #}}}
+}
+
+# To guard against CSRF, the user's session id (sid)
+# can be stored on a form. This function will check
+# (for logged in users) that the sid on the form matches
+# the session id in the cookie.
+sub checksessionexpiry ($$) {
+ my $q=shift;
+ my $session = shift;
+
+ if (defined $session->param("name")) {
+ my $sid=$q->param('sid');
+ if (! defined $sid || $sid ne $session->id) {
+ error(gettext("Your login session has expired."));
+ }
+ }
+}
-sub cgi_savesession ($) { #{{{
+sub cgi_savesession ($) {
my $session=shift;
# Force session flush with safe umask.
my $oldmask=umask(077);
$session->flush;
umask($oldmask);
-} #}}}
+}
-sub cgi (;$$) { #{{{
+sub cgi (;$$) {
my $q=shift;
my $session=shift;
error("\"do\" parameter missing");
}
}
-
+
# Need to lock the wiki before getting a session.
lockwiki();
loadindex();
else {
error("unknown do parameter");
}
-} #}}}
+}
# Does not need to be called directly; all errors will go through here.
-sub cgierror ($) { #{{{
+sub cgierror ($) {
my $message=shift;
print "Content-type: text/html\n\n";
print misctemplate(gettext("Error"),
"<p class=\"error\">".gettext("Error").": $message</p>");
die $@;
-} #}}}
+}
1