9 use open qw{:utf8
:std
};
15 if ($config{sslcookie
}) {
16 print $session->header(-charset
=> 'utf-8',
17 -cookie
=> $session->cookie(-httponly
=> 1, -secure
=> 1));
20 print $session->header(-charset
=> 'utf-8',
21 -cookie
=> $session->cookie(-httponly
=> 1));
25 sub showform
($$$$;@
) {
31 if (exists $hooks{formbuilder
}) {
32 run_hooks
(formbuilder
=> sub {
33 shift->(form
=> $form, cgi
=> $cgi, session
=> $session,
38 printheader
($session);
39 print misctemplate
($form->title, $form->render(submit
=> $buttons), @_);
45 my $url=URI
->new(shift);
46 if (! $config{w3mmode
}) {
47 print $q->redirect($url);
50 print "Content-type: text/plain\n";
51 print "W3m-control: GOTO $url\n\n";
55 sub decode_cgi_utf8
($) {
56 # decode_form_utf8 method is needed for 5.01
59 foreach my $f ($cgi->param) {
60 $cgi->param($f, map { decode_utf8
$_ } $cgi->param($f));
65 sub decode_form_utf8
($) {
68 foreach my $f ($form->field) {
69 my @value=map { decode_utf8
($_) } $form->field($f);
70 $form->field(name
=> $f,
78 # Check if the user is signed in. If not, redirect to the signin form and
79 # save their place to return to later.
84 if (! defined $session->param("name") ||
85 ! userinfo_get
($session->param("name"), "regdate")) {
86 $session->param(postsignin
=> $ENV{QUERY_STRING
});
87 cgi_signin
($q, $session);
88 cgi_savesession
($session);
98 eval q{use CGI::FormBuilder};
100 my $form = CGI
::FormBuilder
->new(
108 action
=> $config{cgiurl
},
110 template
=> {type
=> 'div'},
111 stylesheet
=> baseurl
()."style.css",
113 my $buttons=["Login"];
115 if ($q->param("do") ne "signin" && !$form->submitted) {
116 $form->text(gettext
("You need to log in first."));
118 $form->field(name
=> "do", type
=> "hidden", value
=> "signin",
121 decode_form_utf8
($form);
122 run_hooks
(formbuilder_setup
=> sub {
123 shift->(form
=> $form, cgi
=> $q, session
=> $session,
124 buttons
=> $buttons);
126 decode_form_utf8
($form);
128 if ($form->submitted) {
132 showform
($form, $buttons, $session, $q);
135 sub cgi_postsignin
($$) {
139 # Continue with whatever was being done before the signin process.
140 if (defined $session->param("postsignin")) {
141 my $postsignin=CGI
->new($session->param("postsignin"));
142 $session->clear("postsignin");
143 cgi
($postsignin, $session);
144 cgi_savesession
($session);
148 if ($config{sslcookie
} && ! $q->https()) {
149 error
(gettext
("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
152 error
(gettext
("login failed, perhaps you need to turn on cookies?"));
161 needsignin
($q, $session);
164 # The session id is stored on the form and checked to
165 # guard against CSRF.
166 my $sid=$q->param('sid');
167 if (! defined $sid) {
170 elsif ($sid ne $session->id) {
171 error
(gettext
("Your login session has expired."));
174 eval q{use CGI::FormBuilder};
176 my $form = CGI
::FormBuilder
->new(
177 title
=> "preferences",
178 name
=> "preferences",
188 action
=> $config{cgiurl
},
189 template
=> {type
=> 'div'},
190 stylesheet
=> baseurl
()."style.css",
192 [login
=> gettext
("Login")],
193 [preferences
=> gettext
("Preferences")],
194 [admin
=> gettext
("Admin")]
197 my $buttons=["Save Preferences", "Logout", "Cancel"];
199 decode_form_utf8
($form);
200 run_hooks
(formbuilder_setup
=> sub {
201 shift->(form
=> $form, cgi
=> $q, session
=> $session,
202 buttons
=> $buttons);
204 decode_form_utf8
($form);
206 $form->field(name
=> "do", type
=> "hidden", value
=> "prefs",
208 $form->field(name
=> "sid", type
=> "hidden", value
=> $session->id,
210 $form->field(name
=> "email", size
=> 50, fieldset
=> "preferences");
212 my $user_name=$session->param("name");
214 if (! $form->submitted) {
215 $form->field(name
=> "email", force
=> 1,
216 value
=> userinfo_get
($user_name, "email"));
219 if ($form->submitted eq 'Logout') {
221 redirect
($q, $config{url
});
224 elsif ($form->submitted eq 'Cancel') {
225 redirect
($q, $config{url
});
228 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
229 if (defined $form->field('email')) {
230 userinfo_set
($user_name, 'email', $form->field('email')) ||
231 error
("failed to set email");
234 $form->text(gettext
("Preferences saved."));
237 showform
($form, $buttons, $session, $q);
240 sub cgi_custom_failure
($$$) {
242 my $httpstatus=shift;
246 -status
=> $httpstatus,
251 # Internet Explod^Hrer won't show custom 404 responses
252 # unless they're >= 512 bytes
258 sub check_banned
($$) {
263 my $name=$session->param("name");
265 grep { $name eq $_ } @
{$config{banned_users
}}) {
269 foreach my $b (@
{$config{banned_users
}}) {
270 if (pagespec_match
("", $b,
271 ip
=> $ENV{REMOTE_ADDR
},
272 name
=> defined $name ?
$name : "",
281 cgi_savesession
($session);
284 gettext
("You are banned."));
288 sub cgi_getsession
($) {
291 eval q{use CGI::Session; use HTML::Entities};
293 CGI
::Session
->name("ikiwiki_session_".encode_entities
($config{wikiname
}));
295 my $oldmask=umask(077);
297 CGI
::Session
->new("driver:DB_File", $q,
298 { FileName
=> "$config{wikistatedir}/sessions.db" })
300 if (! $session || $@
) {
301 error
($@
." ".CGI
::Session
->errstr());
309 # To guard against CSRF, the user's session id (sid)
310 # can be stored on a form. This function will check
311 # (for logged in users) that the sid on the form matches
312 # the session id in the cookie.
313 sub checksessionexpiry
($$) {
317 if (defined $session->param("name")) {
318 my $sid=$q->param('sid');
319 if (! defined $sid || $sid ne $session->id) {
320 error
(gettext
("Your login session has expired."));
325 sub cgi_savesession
($) {
328 # Force session flush with safe umask.
329 my $oldmask=umask(077);
340 $CGI::DISABLE_UPLOADS
=$config{cgi_disable_uploads
};
345 binmode(STDIN
, ":utf8");
347 run_hooks
(cgi
=> sub { shift->($q) });
350 my $do=$q->param('do');
351 if (! defined $do || ! length $do) {
352 my $error = $q->cgi_error;
354 error
("Request not processed: $error");
357 error
("\"do\" parameter missing");
361 # Need to lock the wiki before getting a session.
366 $session=cgi_getsession
($q);
369 # Auth hooks can sign a user in.
370 if ($do ne 'signin' && ! defined $session->param("name")) {
371 run_hooks
(auth
=> sub {
372 shift->($q, $session)
374 if (defined $session->param("name")) {
375 # Make sure whatever user was authed is in the
377 if (! userinfo_get
($session->param("name"), "regdate")) {
378 userinfo_setall
($session->param("name"), {
382 }) || error
("failed adding user");
387 check_banned
($q, $session);
389 run_hooks
(sessioncgi
=> sub { shift->($q, $session) });
391 if ($do eq 'signin') {
392 cgi_signin
($q, $session);
393 cgi_savesession
($session);
395 elsif ($do eq 'prefs') {
396 cgi_prefs
($q, $session);
398 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
399 cgi_postsignin
($q, $session);
402 error
("unknown do parameter");
406 # Does not need to be called directly; all errors will go through here.
410 print "Content-type: text/html\n\n";
411 print misctemplate
(gettext
("Error"),
412 "<p class=\"error\">".gettext
("Error").": $message</p>");