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 = defined $cgi ? $cgi->url : $config{url};
64 if (exists $params{page}) {
65 $page=delete $params{page};
66 $params{forcebaseurl}=urlabs(urlto($page), $topurl);
68 run_hooks(pagetemplate => sub {
72 template => $template,
75 templateactions($template, "");
80 wikiname => $config{wikiname},
82 baseurl => urlabs(baseurl(), $topurl),
83 html5 => $config{html5},
87 return $template->output;
93 my $url=URI->new(urlabs(shift, $q->url));
94 if (! $config{w3mmode}) {
95 print $q->redirect($url);
98 print "Content-type: text/plain\n";
99 print "W3m-control: GOTO $url\n\n";
103 sub decode_cgi_utf8 ($) {
104 # decode_form_utf8 method is needed for 5.01
107 foreach my $f ($cgi->param) {
108 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
113 sub safe_decode_utf8 ($) {
115 # call decode_utf8 on >= 5.20 only if it's not already decoded,
116 # otherwise it balks, on < 5.20, always call it
117 if ($] < 5.02 || !Encode::is_utf8($octets)) {
118 return decode_utf8($octets);
125 sub decode_form_utf8 ($) {
128 foreach my $f ($form->field) {
129 my @value=map { safe_decode_utf8($_) } $form->field($f);
130 $form->field(name => $f,
138 # Check if the user is signed in. If not, redirect to the signin form and
139 # save their place to return to later.
140 sub needsignin ($$) {
144 if (! defined $session->param("name") ||
145 ! userinfo_get($session->param("name"), "regdate")) {
146 $session->param(postsignin => $q->query_string);
147 cgi_signin($q, $session);
148 cgi_savesession($session);
153 sub cgi_signin ($$;$) {
156 my $returnhtml=shift;
159 eval q{use CGI::FormBuilder};
161 my $form = CGI::FormBuilder->new(
171 template => {type => 'div'},
174 my $buttons=["Login"];
176 $form->field(name => "do", type => "hidden", value => "signin",
179 decode_form_utf8($form);
180 run_hooks(formbuilder_setup => sub {
181 shift->(form => $form, cgi => $q, session => $session,
182 buttons => $buttons);
184 decode_form_utf8($form);
186 if ($form->submitted) {
191 $form=prepform($form, $buttons, $session, $q);
192 return $form->render(submit => $buttons);
195 showform($form, $buttons, $session, $q);
198 sub cgi_postsignin ($$) {
202 # Continue with whatever was being done before the signin process.
203 if (defined $session->param("postsignin")) {
204 my $postsignin=CGI->new($session->param("postsignin"));
205 $session->clear("postsignin");
206 cgi($postsignin, $session);
207 cgi_savesession($session);
211 if ($config{sslcookie} && ! $q->https()) {
212 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
215 error(gettext("login failed, perhaps you need to turn on cookies?"));
224 needsignin($q, $session);
227 # The session id is stored on the form and checked to
228 # guard against CSRF.
229 my $sid=$q->param('sid');
230 if (! defined $sid) {
233 elsif ($sid ne $session->id) {
234 error(gettext("Your login session has expired."));
237 eval q{use CGI::FormBuilder};
239 my $form = CGI::FormBuilder->new(
240 title => "preferences",
241 name => "preferences",
252 template => {type => 'div'},
255 [login => gettext("Login")],
256 [preferences => gettext("Preferences")],
257 [admin => gettext("Admin")]
260 my $buttons=["Save Preferences", "Logout", "Cancel"];
262 decode_form_utf8($form);
263 run_hooks(formbuilder_setup => sub {
264 shift->(form => $form, cgi => $q, session => $session,
265 buttons => $buttons);
267 decode_form_utf8($form);
269 $form->field(name => "do", type => "hidden", value => "prefs",
271 $form->field(name => "sid", type => "hidden", value => $session->id,
273 $form->field(name => "email", size => 50, fieldset => "preferences");
275 my $user_name=$session->param("name");
277 if (! $form->submitted) {
278 $form->field(name => "email", force => 1,
279 value => userinfo_get($user_name, "email"));
282 if ($form->submitted eq 'Logout') {
284 redirect($q, baseurl(undef));
287 elsif ($form->submitted eq 'Cancel') {
288 redirect($q, baseurl(undef));
291 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
292 if (defined $form->field('email')) {
293 userinfo_set($user_name, 'email', $form->field('email')) ||
294 error("failed to set email");
297 $form->text(gettext("Preferences saved."));
300 showform($form, $buttons, $session, $q,
301 prefsurl => "", # avoid showing the preferences link
305 sub cgi_custom_failure ($$$) {
307 my $httpstatus=shift;
311 -status => $httpstatus,
316 # Internet Explod^Hrer won't show custom 404 responses
317 # unless they're >= 512 bytes
323 sub check_banned ($$) {
328 my $name=$session->param("name");
330 grep { $name eq $_ } @{$config{banned_users}}) {
334 foreach my $b (@{$config{banned_users}}) {
335 if (pagespec_match("", $b,
336 ip => $session->remote_addr(),
337 name => defined $name ? $name : "",
346 cgi_savesession($session);
349 gettext("You are banned."));
353 sub cgi_getsession ($) {
356 eval q{use CGI::Session; use HTML::Entities};
358 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
360 my $oldmask=umask(077);
362 CGI::Session->new("driver:DB_File", $q,
363 { FileName => "$config{wikistatedir}/sessions.db" })
365 if (! $session || $@) {
367 error($error." ".CGI::Session->errstr());
375 # To guard against CSRF, the user's session id (sid)
376 # can be stored on a form. This function will check
377 # (for logged in users) that the sid on the form matches
378 # the session id in the cookie.
379 sub checksessionexpiry ($$) {
383 if (defined $session->param("name")) {
384 my $sid=$q->param('sid');
385 if (! defined $sid || $sid ne $session->id) {
386 error(gettext("Your login session has expired."));
391 sub cgi_savesession ($) {
394 # Force session flush with safe umask.
395 my $oldmask=umask(077);
406 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
411 binmode(STDIN, ":utf8");
413 run_hooks(cgi => sub { shift->($q) });
416 my $do=$q->param('do');
417 if (! defined $do || ! length $do) {
418 my $error = $q->cgi_error;
420 error("Request not processed: $error");
423 error("\"do\" parameter missing");
427 # Need to lock the wiki before getting a session.
432 $session=cgi_getsession($q);
435 # Auth hooks can sign a user in.
436 if ($do ne 'signin' && ! defined $session->param("name")) {
437 run_hooks(auth => sub {
438 shift->($q, $session)
440 if (defined $session->param("name")) {
441 # Make sure whatever user was authed is in the
443 if (! userinfo_get($session->param("name"), "regdate")) {
444 userinfo_setall($session->param("name"), {
445 email => defined $session->param("email") ? $session->param("email") : "",
448 }) || error("failed adding user");
453 check_banned($q, $session);
455 run_hooks(sessioncgi => sub { shift->($q, $session) });
457 if ($do eq 'signin') {
458 cgi_signin($q, $session);
459 cgi_savesession($session);
461 elsif ($do eq 'prefs') {
462 cgi_prefs($q, $session);
464 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
465 cgi_postsignin($q, $session);
468 error("unknown do parameter");
472 # Does not need to be called directly; all errors will go through here.
476 print "Content-type: text/html\n\n";
477 print cgitemplate(undef, gettext("Error"),
478 "<p class=\"error\">".gettext("Error").": $message</p>");