9 use open qw{:utf8 :std};
15 if (($ENV{HTTPS} && lc $ENV{HTTPS} ne "off") || $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 cgitemplate($cgi, $form->title,
50 $form->render(submit => $buttons), @_);
53 sub cgitemplate ($$$;@) {
59 my $template=template("page.tmpl");
61 my $topurl = $config{url};
62 if (defined $cgi && ! $config{w3mmode}) {
67 if (exists $params{page}) {
68 $page=delete $params{page};
69 $params{forcebaseurl}=urlabs(urlto($page), $topurl);
71 run_hooks(pagetemplate => sub {
75 template => $template,
78 templateactions($template, "");
83 wikiname => $config{wikiname},
85 baseurl => urlabs(baseurl(), $topurl),
86 html5 => $config{html5},
90 return $template->output;
96 my $url=URI->new(urlabs(shift, $q->url));
97 if (! $config{w3mmode}) {
98 print $q->redirect($url);
101 print "Content-type: text/plain\n";
102 print "W3m-control: GOTO $url\n\n";
106 sub decode_cgi_utf8 ($) {
107 # decode_form_utf8 method is needed for 5.01
110 foreach my $f ($cgi->param) {
111 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
116 sub safe_decode_utf8 ($) {
118 # call decode_utf8 on >= 5.20 only if it's not already decoded,
119 # otherwise it balks, on < 5.20, always call it
120 if ($] < 5.02 || !Encode::is_utf8($octets)) {
121 return decode_utf8($octets);
128 sub decode_form_utf8 ($) {
131 foreach my $f ($form->field) {
132 my @value=map { safe_decode_utf8($_) } $form->field($f);
133 $form->field(name => $f,
141 # Check if the user is signed in. If not, redirect to the signin form and
142 # save their place to return to later.
143 sub needsignin ($$) {
147 if (! defined $session->param("name") ||
148 ! userinfo_get($session->param("name"), "regdate")) {
149 $session->param(postsignin => $q->query_string);
150 cgi_signin($q, $session);
151 cgi_savesession($session);
156 sub cgi_signin ($$;$) {
159 my $returnhtml=shift;
162 eval q{use CGI::FormBuilder};
164 my $form = CGI::FormBuilder->new(
174 template => {type => 'div'},
177 my $buttons=["Login"];
179 $form->field(name => "do", type => "hidden", value => "signin",
182 decode_form_utf8($form);
183 run_hooks(formbuilder_setup => sub {
184 shift->(form => $form, cgi => $q, session => $session,
185 buttons => $buttons);
187 decode_form_utf8($form);
189 if ($form->submitted) {
194 $form=prepform($form, $buttons, $session, $q);
195 return $form->render(submit => $buttons);
198 showform($form, $buttons, $session, $q);
201 sub cgi_postsignin ($$) {
205 # Continue with whatever was being done before the signin process.
206 if (defined $session->param("postsignin")) {
207 my $postsignin=CGI->new($session->param("postsignin"));
208 $session->clear("postsignin");
209 cgi($postsignin, $session);
210 cgi_savesession($session);
214 if ($config{sslcookie} && ! $q->https()) {
215 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
218 error(gettext("login failed, perhaps you need to turn on cookies?"));
227 needsignin($q, $session);
230 # The session id is stored on the form and checked to
231 # guard against CSRF.
232 my $sid=$q->param('sid');
233 if (! defined $sid) {
236 elsif ($sid ne $session->id) {
237 error(gettext("Your login session has expired."));
240 eval q{use CGI::FormBuilder};
242 my $form = CGI::FormBuilder->new(
243 title => "preferences",
244 name => "preferences",
255 template => {type => 'div'},
258 [login => gettext("Login")],
259 [preferences => gettext("Preferences")],
260 [admin => gettext("Admin")]
263 my $buttons=["Save Preferences", "Logout", "Cancel"];
265 decode_form_utf8($form);
266 run_hooks(formbuilder_setup => sub {
267 shift->(form => $form, cgi => $q, session => $session,
268 buttons => $buttons);
270 decode_form_utf8($form);
272 $form->field(name => "do", type => "hidden", value => "prefs",
274 $form->field(name => "sid", type => "hidden", value => $session->id,
276 $form->field(name => "email", size => 50, fieldset => "preferences");
278 my $user_name=$session->param("name");
280 if (! $form->submitted) {
281 $form->field(name => "email", force => 1,
282 value => userinfo_get($user_name, "email"));
285 if ($form->submitted eq 'Logout') {
287 redirect($q, baseurl(undef));
290 elsif ($form->submitted eq 'Cancel') {
291 redirect($q, baseurl(undef));
294 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
295 if (defined $form->field('email')) {
296 userinfo_set($user_name, 'email', $form->field('email')) ||
297 error("failed to set email");
300 $form->text(gettext("Preferences saved."));
303 showform($form, $buttons, $session, $q,
304 prefsurl => "", # avoid showing the preferences link
308 sub cgi_custom_failure ($$$) {
310 my $httpstatus=shift;
314 -status => $httpstatus,
319 # Internet Explod^Hrer won't show custom 404 responses
320 # unless they're >= 512 bytes
326 sub check_banned ($$) {
331 my $name=$session->param("name");
333 grep { $name eq $_ } @{$config{banned_users}}) {
337 foreach my $b (@{$config{banned_users}}) {
338 if (pagespec_match("", $b,
339 ip => $session->remote_addr(),
340 name => defined $name ? $name : "",
349 cgi_savesession($session);
352 gettext("You are banned."));
356 sub cgi_getsession ($) {
359 eval q{use CGI::Session; use HTML::Entities};
361 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
363 my $oldmask=umask(077);
365 CGI::Session->new("driver:DB_File", $q,
366 { FileName => "$config{wikistatedir}/sessions.db" })
368 if (! $session || $@) {
370 error($error." ".CGI::Session->errstr());
378 # To guard against CSRF, the user's session id (sid)
379 # can be stored on a form. This function will check
380 # (for logged in users) that the sid on the form matches
381 # the session id in the cookie.
382 sub checksessionexpiry ($$) {
386 if (defined $session->param("name")) {
387 my $sid=$q->param('sid');
388 if (! defined $sid || $sid ne $session->id) {
389 error(gettext("Your login session has expired."));
394 sub cgi_savesession ($) {
397 # Force session flush with safe umask.
398 my $oldmask=umask(077);
409 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
414 binmode(STDIN, ":utf8");
416 run_hooks(cgi => sub { shift->($q) });
419 my $do=$q->param('do');
420 if (! defined $do || ! length $do) {
421 my $error = $q->cgi_error;
423 error("Request not processed: $error");
426 error("\"do\" parameter missing");
430 # Need to lock the wiki before getting a session.
435 $session=cgi_getsession($q);
438 # Auth hooks can sign a user in.
439 if ($do ne 'signin' && ! defined $session->param("name")) {
440 run_hooks(auth => sub {
441 shift->($q, $session)
443 if (defined $session->param("name")) {
444 # Make sure whatever user was authed is in the
446 if (! userinfo_get($session->param("name"), "regdate")) {
447 userinfo_setall($session->param("name"), {
448 email => defined $session->param("email") ? $session->param("email") : "",
451 }) || error("failed adding user");
456 check_banned($q, $session);
458 run_hooks(sessioncgi => sub { shift->($q, $session) });
460 if ($do eq 'signin') {
461 cgi_signin($q, $session);
462 cgi_savesession($session);
464 elsif ($do eq 'prefs') {
465 cgi_prefs($q, $session);
467 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
468 cgi_postsignin($q, $session);
471 error("unknown do parameter");
475 # Does not need to be called directly; all errors will go through here.
479 print "Content-type: text/html\n\n";
480 print cgitemplate(undef, gettext("Error"),
481 "<p class=\"error\">".gettext("Error").": $message</p>");