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} && ! $config{reverse_proxy}) {
67 if (exists $params{page}) {
68 $page=delete $params{page};
69 $params{forcebaseurl}=urlto($page);
70 if (! $config{html5}) {
71 $params{forcebaseurl}=urlabs($params{forcebaseurl}, $topurl);
74 run_hooks(pagetemplate => sub {
78 template => $template,
81 templateactions($template, "");
83 my $baseurl = baseurl();
84 if (! $config{html5}) {
85 $baseurl = urlabs($baseurl, $topurl),
91 wikiname => $config{wikiname},
94 html5 => $config{html5},
98 return $template->output;
106 if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
110 my $url=URI->new(urlabs(shift, $topurl));
111 if (! $config{w3mmode}) {
112 print $q->redirect($url);
115 print "Content-type: text/plain\n";
116 print "W3m-control: GOTO $url\n\n";
120 sub decode_cgi_utf8 ($) {
121 # decode_form_utf8 method is needed for 5.01
124 foreach my $f ($cgi->param) {
125 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
130 sub safe_decode_utf8 ($) {
132 # call decode_utf8 on >= 5.20 only if it's not already decoded,
133 # otherwise it balks, on < 5.20, always call it
134 if ($] < 5.02 || !Encode::is_utf8($octets)) {
135 return decode_utf8($octets);
142 sub decode_form_utf8 ($) {
145 foreach my $f ($form->field) {
146 my @value=map { safe_decode_utf8($_) } $form->field($f);
147 $form->field(name => $f,
155 # Check if the user is signed in. If not, redirect to the signin form and
156 # save their place to return to later.
157 sub needsignin ($$) {
161 if (! defined $session->param("name") ||
162 ! userinfo_get($session->param("name"), "regdate")) {
163 $session->param(postsignin => $q->query_string);
164 cgi_signin($q, $session);
165 cgi_savesession($session);
170 sub cgi_signin ($$;$) {
173 my $returnhtml=shift;
176 eval q{use CGI::FormBuilder};
178 my $form = CGI::FormBuilder->new(
188 template => {type => 'div'},
191 my $buttons=["Login"];
193 $form->field(name => "do", type => "hidden", value => "signin",
196 decode_form_utf8($form);
197 run_hooks(formbuilder_setup => sub {
198 shift->(form => $form, cgi => $q, session => $session,
199 buttons => $buttons);
201 decode_form_utf8($form);
203 if ($form->submitted) {
208 $form=prepform($form, $buttons, $session, $q);
209 return $form->render(submit => $buttons);
212 showform($form, $buttons, $session, $q);
215 sub cgi_postsignin ($$) {
219 # Continue with whatever was being done before the signin process.
220 if (defined $session->param("postsignin")) {
221 my $postsignin=CGI->new($session->param("postsignin"));
222 $session->clear("postsignin");
223 cgi($postsignin, $session);
224 cgi_savesession($session);
228 if ($config{sslcookie} && ! $q->https()) {
229 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
232 error(gettext("login failed, perhaps you need to turn on cookies?"));
241 needsignin($q, $session);
244 # The session id is stored on the form and checked to
245 # guard against CSRF.
246 my $sid=$q->param('sid');
247 if (! defined $sid) {
250 elsif ($sid ne $session->id) {
251 error(gettext("Your login session has expired."));
254 eval q{use CGI::FormBuilder};
256 my $form = CGI::FormBuilder->new(
257 title => "preferences",
258 name => "preferences",
269 template => {type => 'div'},
272 [login => gettext("Login")],
273 [preferences => gettext("Preferences")],
274 [admin => gettext("Admin")]
277 my $buttons=["Save Preferences", "Logout", "Cancel"];
279 decode_form_utf8($form);
280 run_hooks(formbuilder_setup => sub {
281 shift->(form => $form, cgi => $q, session => $session,
282 buttons => $buttons);
284 decode_form_utf8($form);
286 $form->field(name => "do", type => "hidden", value => "prefs",
288 $form->field(name => "sid", type => "hidden", value => $session->id,
290 $form->field(name => "email", size => 50, fieldset => "preferences");
292 my $user_name=$session->param("name");
294 if (! $form->submitted) {
295 $form->field(name => "email", force => 1,
296 value => userinfo_get($user_name, "email"));
299 if ($form->submitted eq 'Logout') {
301 redirect($q, baseurl(undef));
304 elsif ($form->submitted eq 'Cancel') {
305 redirect($q, baseurl(undef));
308 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
309 if (defined $form->field('email')) {
310 userinfo_set($user_name, 'email', $form->field('email')) ||
311 error("failed to set email");
314 $form->text(gettext("Preferences saved."));
317 showform($form, $buttons, $session, $q,
318 prefsurl => "", # avoid showing the preferences link
322 sub cgi_custom_failure ($$$) {
324 my $httpstatus=shift;
328 -status => $httpstatus,
333 # Internet Explod^Hrer won't show custom 404 responses
334 # unless they're >= 512 bytes
340 sub check_banned ($$) {
345 my $name=$session->param("name");
347 grep { $name eq $_ } @{$config{banned_users}}) {
351 foreach my $b (@{$config{banned_users}}) {
352 if (pagespec_match("", $b,
353 ip => $session->remote_addr(),
354 name => defined $name ? $name : "",
363 cgi_savesession($session);
366 gettext("You are banned."));
370 sub cgi_getsession ($) {
373 eval q{use CGI::Session; use HTML::Entities};
375 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
377 my $oldmask=umask(077);
379 CGI::Session->new("driver:DB_File", $q,
380 { FileName => "$config{wikistatedir}/sessions.db" })
382 if (! $session || $@) {
384 error($error." ".CGI::Session->errstr());
392 # To guard against CSRF, the user's session id (sid)
393 # can be stored on a form. This function will check
394 # (for logged in users) that the sid on the form matches
395 # the session id in the cookie.
396 sub checksessionexpiry ($$) {
400 if (defined $session->param("name")) {
401 my $sid=$q->param('sid');
402 if (! defined $sid || $sid ne $session->id) {
403 error(gettext("Your login session has expired."));
408 sub cgi_savesession ($) {
411 # Force session flush with safe umask.
412 my $oldmask=umask(077);
423 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
428 binmode(STDIN, ":utf8");
430 run_hooks(cgi => sub { shift->($q) });
433 my $do=$q->param('do');
434 if (! defined $do || ! length $do) {
435 my $error = $q->cgi_error;
437 error("Request not processed: $error");
440 error("\"do\" parameter missing");
444 # Need to lock the wiki before getting a session.
449 $session=cgi_getsession($q);
452 # Auth hooks can sign a user in.
453 if ($do ne 'signin' && ! defined $session->param("name")) {
454 run_hooks(auth => sub {
455 shift->($q, $session)
457 if (defined $session->param("name")) {
458 # Make sure whatever user was authed is in the
460 if (! userinfo_get($session->param("name"), "regdate")) {
461 userinfo_setall($session->param("name"), {
462 email => defined $session->param("email") ? $session->param("email") : "",
465 }) || error("failed adding user");
470 check_banned($q, $session);
472 run_hooks(sessioncgi => sub { shift->($q, $session) });
474 if ($do eq 'signin') {
475 cgi_signin($q, $session);
476 cgi_savesession($session);
478 elsif ($do eq 'prefs') {
479 cgi_prefs($q, $session);
481 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
482 cgi_postsignin($q, $session);
485 error("unknown do parameter");
489 # Does not need to be called directly; all errors will go through here.
493 print "Content-type: text/html\n\n";
494 print cgitemplate(undef, gettext("Error"),
495 "<p class=\"error\">".gettext("Error").": $message</p>");