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));
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);
253 sub cgi_custom_failure ($$$) {
255 my $httpstatus=shift;
259 -status => $httpstatus,
264 # Internet Explod^Hrer won't show custom 404 responses
265 # unless they're >= 512 bytes
271 sub check_banned ($$) {
276 my $name=$session->param("name");
278 grep { $name eq $_ } @{$config{banned_users}}) {
282 foreach my $b (@{$config{banned_users}}) {
283 if (pagespec_match("", $b,
284 ip => $ENV{REMOTE_ADDR},
285 name => defined $name ? $name : "",
294 cgi_savesession($session);
297 gettext("You are banned."));
301 sub cgi_getsession ($) {
304 eval q{use CGI::Session; use HTML::Entities};
306 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
308 my $oldmask=umask(077);
310 CGI::Session->new("driver:DB_File", $q,
311 { FileName => "$config{wikistatedir}/sessions.db" })
313 if (! $session || $@) {
314 error($@." ".CGI::Session->errstr());
322 # To guard against CSRF, the user's session id (sid)
323 # can be stored on a form. This function will check
324 # (for logged in users) that the sid on the form matches
325 # the session id in the cookie.
326 sub checksessionexpiry ($$) {
330 if (defined $session->param("name")) {
331 my $sid=$q->param('sid');
332 if (! defined $sid || $sid ne $session->id) {
333 error(gettext("Your login session has expired."));
338 sub cgi_savesession ($) {
341 # Force session flush with safe umask.
342 my $oldmask=umask(077);
353 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
358 binmode(STDIN, ":utf8");
360 run_hooks(cgi => sub { shift->($q) });
363 my $do=$q->param('do');
364 if (! defined $do || ! length $do) {
365 my $error = $q->cgi_error;
367 error("Request not processed: $error");
370 error("\"do\" parameter missing");
374 # Need to lock the wiki before getting a session.
379 $session=cgi_getsession($q);
382 # Auth hooks can sign a user in.
383 if ($do ne 'signin' && ! defined $session->param("name")) {
384 run_hooks(auth => sub {
385 shift->($q, $session)
387 if (defined $session->param("name")) {
388 # Make sure whatever user was authed is in the
390 if (! userinfo_get($session->param("name"), "regdate")) {
391 userinfo_setall($session->param("name"), {
395 }) || error("failed adding user");
400 check_banned($q, $session);
402 run_hooks(sessioncgi => sub { shift->($q, $session) });
404 if ($do eq 'signin') {
405 cgi_signin($q, $session);
406 cgi_savesession($session);
408 elsif ($do eq 'prefs') {
409 cgi_prefs($q, $session);
411 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
412 cgi_postsignin($q, $session);
415 error("unknown do parameter");
419 # Does not need to be called directly; all errors will go through here.
423 print "Content-type: text/html\n\n";
424 print misctemplate(gettext("Error"),
425 "<p class=\"error\">".gettext("Error").": $message</p>");