9 use open qw{:utf8
:std
};
15 if ($ENV{HTTPS
} || $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));
31 if (exists $hooks{formbuilder
}) {
32 run_hooks
(formbuilder
=> sub {
33 shift->(form
=> $form, cgi
=> $cgi, session
=> $session,
41 sub showform
($$$$;@
) {
42 my $form=prepform
(@_);
48 printheader
($session);
49 print misctemplate
($form->title, $form->render(submit
=> $buttons), @_);
55 my $url=URI
->new(shift);
56 if (! $config{w3mmode
}) {
57 print $q->redirect($url);
60 print "Content-type: text/plain\n";
61 print "W3m-control: GOTO $url\n\n";
65 sub decode_cgi_utf8
($) {
66 # decode_form_utf8 method is needed for 5.01
69 foreach my $f ($cgi->param) {
70 $cgi->param($f, map { decode_utf8
$_ } $cgi->param($f));
75 sub decode_form_utf8
($) {
78 foreach my $f ($form->field) {
79 my @value=map { decode_utf8
($_) } $form->field($f);
80 $form->field(name
=> $f,
88 # Check if the user is signed in. If not, redirect to the signin form and
89 # save their place to return to later.
94 if (! defined $session->param("name") ||
95 ! userinfo_get
($session->param("name"), "regdate")) {
96 $session->param(postsignin
=> $ENV{QUERY_STRING
});
97 cgi_signin
($q, $session);
98 cgi_savesession
($session);
103 sub cgi_signin
($$;$) {
106 my $returnhtml=shift;
109 eval q{use CGI::FormBuilder};
111 my $form = CGI
::FormBuilder
->new(
119 action
=> $config{cgiurl
},
121 template
=> {type
=> 'div'},
124 my $buttons=["Login"];
126 $form->field(name
=> "do", type
=> "hidden", value
=> "signin",
129 decode_form_utf8
($form);
130 run_hooks
(formbuilder_setup
=> sub {
131 shift->(form
=> $form, cgi
=> $q, session
=> $session,
132 buttons
=> $buttons);
134 decode_form_utf8
($form);
136 if ($form->submitted) {
141 $form=prepform
($form, $buttons, $session, $q);
142 return $form->render(submit
=> $buttons);
145 showform
($form, $buttons, $session, $q);
148 sub cgi_postsignin
($$) {
152 # Continue with whatever was being done before the signin process.
153 if (defined $session->param("postsignin")) {
154 my $postsignin=CGI
->new($session->param("postsignin"));
155 $session->clear("postsignin");
156 cgi
($postsignin, $session);
157 cgi_savesession
($session);
161 if ($config{sslcookie
} && ! $q->https()) {
162 error
(gettext
("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
165 error
(gettext
("login failed, perhaps you need to turn on cookies?"));
174 needsignin
($q, $session);
177 # The session id is stored on the form and checked to
178 # guard against CSRF.
179 my $sid=$q->param('sid');
180 if (! defined $sid) {
183 elsif ($sid ne $session->id) {
184 error
(gettext
("Your login session has expired."));
187 eval q{use CGI::FormBuilder};
189 my $form = CGI
::FormBuilder
->new(
190 title
=> "preferences",
191 name
=> "preferences",
201 action
=> $config{cgiurl
},
202 template
=> {type
=> 'div'},
205 [login
=> gettext
("Login")],
206 [preferences
=> gettext
("Preferences")],
207 [admin
=> gettext
("Admin")]
210 my $buttons=["Save Preferences", "Logout", "Cancel"];
212 decode_form_utf8
($form);
213 run_hooks
(formbuilder_setup
=> sub {
214 shift->(form
=> $form, cgi
=> $q, session
=> $session,
215 buttons
=> $buttons);
217 decode_form_utf8
($form);
219 $form->field(name
=> "do", type
=> "hidden", value
=> "prefs",
221 $form->field(name
=> "sid", type
=> "hidden", value
=> $session->id,
223 $form->field(name
=> "email", size
=> 50, fieldset
=> "preferences");
225 my $user_name=$session->param("name");
227 if (! $form->submitted) {
228 $form->field(name
=> "email", force
=> 1,
229 value
=> userinfo_get
($user_name, "email"));
232 if ($form->submitted eq 'Logout') {
234 redirect
($q, $config{url
});
237 elsif ($form->submitted eq 'Cancel') {
238 redirect
($q, $config{url
});
241 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
242 if (defined $form->field('email')) {
243 userinfo_set
($user_name, 'email', $form->field('email')) ||
244 error
("failed to set email");
247 $form->text(gettext
("Preferences saved."));
250 showform
($form, $buttons, $session, $q,
251 prefsurl
=> "", # avoid showing the preferences link
255 sub cgi_custom_failure
($$$) {
257 my $httpstatus=shift;
261 -status
=> $httpstatus,
266 # Internet Explod^Hrer won't show custom 404 responses
267 # unless they're >= 512 bytes
273 sub check_banned
($$) {
278 my $name=$session->param("name");
280 grep { $name eq $_ } @
{$config{banned_users
}}) {
284 foreach my $b (@
{$config{banned_users
}}) {
285 if (pagespec_match
("", $b,
286 ip
=> $session->remote_addr(),
287 name
=> defined $name ?
$name : "",
296 cgi_savesession
($session);
299 gettext
("You are banned."));
303 sub cgi_getsession
($) {
306 eval q{use CGI::Session; use HTML::Entities};
308 CGI
::Session
->name("ikiwiki_session_".encode_entities
($config{wikiname
}));
310 my $oldmask=umask(077);
312 CGI
::Session
->new("driver:DB_File", $q,
313 { FileName
=> "$config{wikistatedir}/sessions.db" })
315 if (! $session || $@
) {
316 error
($@
." ".CGI
::Session
->errstr());
324 # To guard against CSRF, the user's session id (sid)
325 # can be stored on a form. This function will check
326 # (for logged in users) that the sid on the form matches
327 # the session id in the cookie.
328 sub checksessionexpiry
($$) {
332 if (defined $session->param("name")) {
333 my $sid=$q->param('sid');
334 if (! defined $sid || $sid ne $session->id) {
335 error
(gettext
("Your login session has expired."));
340 sub cgi_savesession
($) {
343 # Force session flush with safe umask.
344 my $oldmask=umask(077);
355 $CGI::DISABLE_UPLOADS
=$config{cgi_disable_uploads
};
360 binmode(STDIN
, ":utf8");
362 run_hooks
(cgi
=> sub { shift->($q) });
365 my $do=$q->param('do');
366 if (! defined $do || ! length $do) {
367 my $error = $q->cgi_error;
369 error
("Request not processed: $error");
372 error
("\"do\" parameter missing");
376 # Need to lock the wiki before getting a session.
381 $session=cgi_getsession
($q);
384 # Auth hooks can sign a user in.
385 if ($do ne 'signin' && ! defined $session->param("name")) {
386 run_hooks
(auth
=> sub {
387 shift->($q, $session)
389 if (defined $session->param("name")) {
390 # Make sure whatever user was authed is in the
392 if (! userinfo_get
($session->param("name"), "regdate")) {
393 userinfo_setall
($session->param("name"), {
397 }) || error
("failed adding user");
402 check_banned
($q, $session);
404 run_hooks
(sessioncgi
=> sub { shift->($q, $session) });
406 if ($do eq 'signin') {
407 cgi_signin
($q, $session);
408 cgi_savesession
($session);
410 elsif ($do eq 'prefs') {
411 cgi_prefs
($q, $session);
413 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
414 cgi_postsignin
($q, $session);
417 error
("unknown do parameter");
421 # Does not need to be called directly; all errors will go through here.
425 print "Content-type: text/html\n\n";
426 print misctemplate
(gettext
("Error"),
427 "<p class=\"error\">".gettext
("Error").": $message</p>");