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}=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;
98 if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
102 my $url=URI->new(urlabs(shift, $topurl));
103 if (! $config{w3mmode}) {
104 print $q->redirect($url);
107 print "Content-type: text/plain\n";
108 print "W3m-control: GOTO $url\n\n";
112 sub decode_cgi_utf8 ($) {
113 # decode_form_utf8 method is needed for 5.01
116 foreach my $f ($cgi->param) {
117 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
122 sub safe_decode_utf8 ($) {
124 # call decode_utf8 on >= 5.20 only if it's not already decoded,
125 # otherwise it balks, on < 5.20, always call it
126 if ($] < 5.02 || !Encode::is_utf8($octets)) {
127 return decode_utf8($octets);
134 sub decode_form_utf8 ($) {
137 foreach my $f ($form->field) {
138 my @value=map { safe_decode_utf8($_) } $form->field($f);
139 $form->field(name => $f,
147 # Check if the user is signed in. If not, redirect to the signin form and
148 # save their place to return to later.
149 sub needsignin ($$) {
153 if (! defined $session->param("name") ||
154 ! userinfo_get($session->param("name"), "regdate")) {
155 $session->param(postsignin => $q->query_string);
156 cgi_signin($q, $session);
157 cgi_savesession($session);
162 sub cgi_signin ($$;$) {
165 my $returnhtml=shift;
168 eval q{use CGI::FormBuilder};
170 my $form = CGI::FormBuilder->new(
180 template => {type => 'div'},
183 my $buttons=["Login"];
185 $form->field(name => "do", type => "hidden", value => "signin",
188 decode_form_utf8($form);
189 run_hooks(formbuilder_setup => sub {
190 shift->(form => $form, cgi => $q, session => $session,
191 buttons => $buttons);
193 decode_form_utf8($form);
195 if ($form->submitted) {
200 $form=prepform($form, $buttons, $session, $q);
201 return $form->render(submit => $buttons);
204 showform($form, $buttons, $session, $q);
207 sub cgi_postsignin ($$) {
211 # Continue with whatever was being done before the signin process.
212 if (defined $session->param("postsignin")) {
213 my $postsignin=CGI->new($session->param("postsignin"));
214 $session->clear("postsignin");
215 cgi($postsignin, $session);
216 cgi_savesession($session);
220 if ($config{sslcookie} && ! $q->https()) {
221 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
224 error(gettext("login failed, perhaps you need to turn on cookies?"));
233 needsignin($q, $session);
236 # The session id is stored on the form and checked to
237 # guard against CSRF.
238 my $sid=$q->param('sid');
239 if (! defined $sid) {
242 elsif ($sid ne $session->id) {
243 error(gettext("Your login session has expired."));
246 eval q{use CGI::FormBuilder};
248 my $form = CGI::FormBuilder->new(
249 title => "preferences",
250 name => "preferences",
261 template => {type => 'div'},
264 [login => gettext("Login")],
265 [preferences => gettext("Preferences")],
266 [admin => gettext("Admin")]
269 my $buttons=["Save Preferences", "Logout", "Cancel"];
271 decode_form_utf8($form);
272 run_hooks(formbuilder_setup => sub {
273 shift->(form => $form, cgi => $q, session => $session,
274 buttons => $buttons);
276 decode_form_utf8($form);
278 $form->field(name => "do", type => "hidden", value => "prefs",
280 $form->field(name => "sid", type => "hidden", value => $session->id,
282 $form->field(name => "email", size => 50, fieldset => "preferences");
284 my $user_name=$session->param("name");
286 if (! $form->submitted) {
287 $form->field(name => "email", force => 1,
288 value => userinfo_get($user_name, "email"));
291 if ($form->submitted eq 'Logout') {
293 redirect($q, baseurl(undef));
296 elsif ($form->submitted eq 'Cancel') {
297 redirect($q, baseurl(undef));
300 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
301 if (defined $form->field('email')) {
302 userinfo_set($user_name, 'email', $form->field('email')) ||
303 error("failed to set email");
306 $form->text(gettext("Preferences saved."));
309 showform($form, $buttons, $session, $q,
310 prefsurl => "", # avoid showing the preferences link
314 sub cgi_custom_failure ($$$) {
316 my $httpstatus=shift;
320 -status => $httpstatus,
325 # Internet Explod^Hrer won't show custom 404 responses
326 # unless they're >= 512 bytes
332 sub check_banned ($$) {
337 my $name=$session->param("name");
339 grep { $name eq $_ } @{$config{banned_users}}) {
343 foreach my $b (@{$config{banned_users}}) {
344 if (pagespec_match("", $b,
345 ip => $session->remote_addr(),
346 name => defined $name ? $name : "",
355 cgi_savesession($session);
358 gettext("You are banned."));
362 sub cgi_getsession ($) {
365 eval q{use CGI::Session; use HTML::Entities};
367 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
369 my $oldmask=umask(077);
371 CGI::Session->new("driver:DB_File", $q,
372 { FileName => "$config{wikistatedir}/sessions.db" })
374 if (! $session || $@) {
376 error($error." ".CGI::Session->errstr());
384 # To guard against CSRF, the user's session id (sid)
385 # can be stored on a form. This function will check
386 # (for logged in users) that the sid on the form matches
387 # the session id in the cookie.
388 sub checksessionexpiry ($$) {
392 if (defined $session->param("name")) {
393 my $sid=$q->param('sid');
394 if (! defined $sid || $sid ne $session->id) {
395 error(gettext("Your login session has expired."));
400 sub cgi_savesession ($) {
403 # Force session flush with safe umask.
404 my $oldmask=umask(077);
415 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
420 binmode(STDIN, ":utf8");
422 run_hooks(cgi => sub { shift->($q) });
425 my $do=$q->param('do');
426 if (! defined $do || ! length $do) {
427 my $error = $q->cgi_error;
429 error("Request not processed: $error");
432 error("\"do\" parameter missing");
436 # Need to lock the wiki before getting a session.
441 $session=cgi_getsession($q);
444 # Auth hooks can sign a user in.
445 if ($do ne 'signin' && ! defined $session->param("name")) {
446 run_hooks(auth => sub {
447 shift->($q, $session)
449 if (defined $session->param("name")) {
450 # Make sure whatever user was authed is in the
452 if (! userinfo_get($session->param("name"), "regdate")) {
453 userinfo_setall($session->param("name"), {
454 email => defined $session->param("email") ? $session->param("email") : "",
457 }) || error("failed adding user");
462 check_banned($q, $session);
464 run_hooks(sessioncgi => sub { shift->($q, $session) });
466 if ($do eq 'signin') {
467 cgi_signin($q, $session);
468 cgi_savesession($session);
470 elsif ($do eq 'prefs') {
471 cgi_prefs($q, $session);
473 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
474 cgi_postsignin($q, $session);
477 error("unknown do parameter");
481 # Does not need to be called directly; all errors will go through here.
485 print "Content-type: text/html\n\n";
486 print cgitemplate(undef, gettext("Error"),
487 "<p class=\"error\">".gettext("Error").": $message</p>");