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 $_ }
126 @{$cgi->param_fetch($f)});
131 sub safe_decode_utf8 ($) {
133 # call decode_utf8 on >= 5.20 only if it's not already decoded,
134 # otherwise it balks, on < 5.20, always call it
135 if ($] < 5.02 || !Encode::is_utf8($octets)) {
136 return decode_utf8($octets);
143 sub decode_form_utf8 ($) {
146 foreach my $f ($form->field) {
147 my @value=map { safe_decode_utf8($_) } $form->field($f);
148 $form->field(name => $f,
156 # Check if the user is signed in. If not, redirect to the signin form and
157 # save their place to return to later.
158 sub needsignin ($$) {
162 if (! defined $session->param("name") ||
163 ! userinfo_get($session->param("name"), "regdate")) {
164 $session->param(postsignin => $q->query_string);
165 cgi_signin($q, $session);
166 cgi_savesession($session);
171 sub cgi_signin ($$;$) {
174 my $returnhtml=shift;
177 eval q{use CGI::FormBuilder};
179 my $form = CGI::FormBuilder->new(
189 template => {type => 'div'},
192 my $buttons=["Login"];
194 $form->field(name => "do", type => "hidden", value => "signin",
197 decode_form_utf8($form);
198 run_hooks(formbuilder_setup => sub {
199 shift->(form => $form, cgi => $q, session => $session,
200 buttons => $buttons);
202 decode_form_utf8($form);
204 if ($form->submitted) {
209 $form=prepform($form, $buttons, $session, $q);
210 return $form->render(submit => $buttons);
213 showform($form, $buttons, $session, $q);
216 sub cgi_postsignin ($$) {
220 # Continue with whatever was being done before the signin process.
221 if (defined $session->param("postsignin")) {
222 my $postsignin=CGI->new($session->param("postsignin"));
223 $session->clear("postsignin");
224 cgi($postsignin, $session);
225 cgi_savesession($session);
229 if ($config{sslcookie} && ! $q->https()) {
230 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
233 error(gettext("login failed, perhaps you need to turn on cookies?"));
242 needsignin($q, $session);
245 # The session id is stored on the form and checked to
246 # guard against CSRF.
247 my $sid=$q->param('sid');
248 if (! defined $sid) {
251 elsif ($sid ne $session->id) {
252 error(gettext("Your login session has expired."));
255 eval q{use CGI::FormBuilder};
257 my $form = CGI::FormBuilder->new(
258 title => "preferences",
259 name => "preferences",
270 template => {type => 'div'},
273 [login => gettext("Login")],
274 [preferences => gettext("Preferences")],
275 [admin => gettext("Admin")]
278 my $buttons=["Save Preferences", "Logout", "Cancel"];
280 decode_form_utf8($form);
281 run_hooks(formbuilder_setup => sub {
282 shift->(form => $form, cgi => $q, session => $session,
283 buttons => $buttons);
285 decode_form_utf8($form);
287 $form->field(name => "do", type => "hidden", value => "prefs",
289 $form->field(name => "sid", type => "hidden", value => $session->id,
291 $form->field(name => "email", size => 50, fieldset => "preferences");
293 my $user_name=$session->param("name");
295 if (! $form->submitted) {
296 $form->field(name => "email", force => 1,
297 value => userinfo_get($user_name, "email"));
300 if ($form->submitted eq 'Logout') {
302 redirect($q, baseurl(undef));
305 elsif ($form->submitted eq 'Cancel') {
306 redirect($q, baseurl(undef));
309 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
310 if (defined $form->field('email')) {
311 userinfo_set($user_name, 'email', $form->field('email')) ||
312 error("failed to set email");
315 $form->text(gettext("Preferences saved."));
318 showform($form, $buttons, $session, $q,
319 prefsurl => "", # avoid showing the preferences link
323 sub cgi_custom_failure ($$$) {
325 my $httpstatus=shift;
329 -status => $httpstatus,
334 # Internet Explod^Hrer won't show custom 404 responses
335 # unless they're >= 512 bytes
341 sub check_banned ($$) {
346 my $name=$session->param("name");
348 grep { $name eq $_ } @{$config{banned_users}}) {
352 foreach my $b (@{$config{banned_users}}) {
353 if (pagespec_match("", $b,
354 ip => $session->remote_addr(),
355 name => defined $name ? $name : "",
364 cgi_savesession($session);
367 gettext("You are banned."));
371 sub cgi_getsession ($) {
374 eval q{use CGI::Session; use HTML::Entities};
376 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
378 my $oldmask=umask(077);
380 CGI::Session->new("driver:DB_File", $q,
381 { FileName => "$config{wikistatedir}/sessions.db" })
383 if (! $session || $@) {
385 error($error." ".CGI::Session->errstr());
393 # To guard against CSRF, the user's session id (sid)
394 # can be stored on a form. This function will check
395 # (for logged in users) that the sid on the form matches
396 # the session id in the cookie.
397 sub checksessionexpiry ($$) {
401 if (defined $session->param("name")) {
402 my $sid=$q->param('sid');
403 if (! defined $sid || $sid ne $session->id) {
404 error(gettext("Your login session has expired."));
409 sub cgi_savesession ($) {
412 # Force session flush with safe umask.
413 my $oldmask=umask(077);
424 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
429 binmode(STDIN, ":utf8");
431 run_hooks(cgi => sub { shift->($q) });
434 my $do=$q->param('do');
435 if (! defined $do || ! length $do) {
436 my $error = $q->cgi_error;
438 error("Request not processed: $error");
441 error("\"do\" parameter missing");
445 # Need to lock the wiki before getting a session.
450 $session=cgi_getsession($q);
453 # Auth hooks can sign a user in.
454 if ($do ne 'signin' && ! defined $session->param("name")) {
455 run_hooks(auth => sub {
456 shift->($q, $session)
458 if (defined $session->param("name")) {
459 # Make sure whatever user was authed is in the
461 if (! userinfo_get($session->param("name"), "regdate")) {
462 userinfo_setall($session->param("name"), {
463 email => defined $session->param("email") ? $session->param("email") : "",
466 }) || error("failed adding user");
471 check_banned($q, $session);
473 run_hooks(sessioncgi => sub { shift->($q, $session) });
475 if ($do eq 'signin') {
476 cgi_signin($q, $session);
477 cgi_savesession($session);
479 elsif ($do eq 'prefs') {
480 cgi_prefs($q, $session);
482 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
483 cgi_postsignin($q, $session);
486 error("unknown do parameter");
490 # Does not need to be called directly; all errors will go through here.
494 print "Content-type: text/html\n\n";
495 print cgitemplate(undef, gettext("Error"),
496 "<p class=\"error\">".gettext("Error").": $message</p>");