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);
71 run_hooks(pagetemplate => sub {
75 template => $template,
78 templateactions($template, "");
80 my $baseurl = baseurl();
85 wikiname => $config{wikiname},
88 html5 => $config{html5},
92 return $template->output;
100 if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
104 my $url=URI->new(urlabs(shift, $topurl));
105 if (! $config{w3mmode}) {
106 print $q->redirect($url);
109 print "Content-type: text/plain\n";
110 print "W3m-control: GOTO $url\n\n";
114 sub decode_cgi_utf8 ($) {
115 # decode_form_utf8 method is needed for 5.01
118 foreach my $f ($cgi->param) {
119 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
124 sub safe_decode_utf8 ($) {
126 # call decode_utf8 on >= 5.20 only if it's not already decoded,
127 # otherwise it balks, on < 5.20, always call it
128 if ($] < 5.02 || !Encode::is_utf8($octets)) {
129 return decode_utf8($octets);
136 sub decode_form_utf8 ($) {
139 foreach my $f ($form->field) {
140 my @value=map { safe_decode_utf8($_) } $form->field($f);
141 $form->field(name => $f,
149 # Check if the user is signed in. If not, redirect to the signin form and
150 # save their place to return to later.
151 sub needsignin ($$) {
155 if (! defined $session->param("name") ||
156 ! userinfo_get($session->param("name"), "regdate")) {
157 $session->param(postsignin => $q->query_string);
158 cgi_signin($q, $session);
159 cgi_savesession($session);
164 sub cgi_signin ($$;$) {
167 my $returnhtml=shift;
170 eval q{use CGI::FormBuilder};
172 my $form = CGI::FormBuilder->new(
182 template => {type => 'div'},
185 my $buttons=["Login"];
187 $form->field(name => "do", type => "hidden", value => "signin",
190 decode_form_utf8($form);
191 run_hooks(formbuilder_setup => sub {
192 shift->(form => $form, cgi => $q, session => $session,
193 buttons => $buttons);
195 decode_form_utf8($form);
197 if ($form->submitted) {
202 $form=prepform($form, $buttons, $session, $q);
203 return $form->render(submit => $buttons);
206 showform($form, $buttons, $session, $q);
209 sub cgi_postsignin ($$) {
213 # Continue with whatever was being done before the signin process.
214 if (defined $session->param("postsignin")) {
215 my $postsignin=CGI->new($session->param("postsignin"));
216 $session->clear("postsignin");
217 cgi($postsignin, $session);
218 cgi_savesession($session);
222 if ($config{sslcookie} && ! $q->https()) {
223 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
226 error(gettext("login failed, perhaps you need to turn on cookies?"));
235 needsignin($q, $session);
238 # The session id is stored on the form and checked to
239 # guard against CSRF.
240 my $sid=$q->param('sid');
241 if (! defined $sid) {
244 elsif ($sid ne $session->id) {
245 error(gettext("Your login session has expired."));
248 eval q{use CGI::FormBuilder};
250 my $form = CGI::FormBuilder->new(
251 title => "preferences",
252 name => "preferences",
263 template => {type => 'div'},
266 [login => gettext("Login")],
267 [preferences => gettext("Preferences")],
268 [admin => gettext("Admin")]
271 my $buttons=["Save Preferences", "Logout", "Cancel"];
273 decode_form_utf8($form);
274 run_hooks(formbuilder_setup => sub {
275 shift->(form => $form, cgi => $q, session => $session,
276 buttons => $buttons);
278 decode_form_utf8($form);
280 $form->field(name => "do", type => "hidden", value => "prefs",
282 $form->field(name => "sid", type => "hidden", value => $session->id,
284 $form->field(name => "email", size => 50, fieldset => "preferences");
286 my $user_name=$session->param("name");
288 if (! $form->submitted) {
289 $form->field(name => "email", force => 1,
290 value => userinfo_get($user_name, "email"));
293 if ($form->submitted eq 'Logout') {
295 redirect($q, baseurl(undef));
298 elsif ($form->submitted eq 'Cancel') {
299 redirect($q, baseurl(undef));
302 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
303 if (defined $form->field('email')) {
304 userinfo_set($user_name, 'email', $form->field('email')) ||
305 error("failed to set email");
308 $form->text(gettext("Preferences saved."));
311 showform($form, $buttons, $session, $q,
312 prefsurl => "", # avoid showing the preferences link
316 sub cgi_custom_failure ($$$) {
318 my $httpstatus=shift;
322 -status => $httpstatus,
327 # Internet Explod^Hrer won't show custom 404 responses
328 # unless they're >= 512 bytes
334 sub check_banned ($$) {
339 my $name=$session->param("name");
341 grep { $name eq $_ } @{$config{banned_users}}) {
345 foreach my $b (@{$config{banned_users}}) {
346 if (pagespec_match("", $b,
347 ip => $session->remote_addr(),
348 name => defined $name ? $name : "",
357 cgi_savesession($session);
360 gettext("You are banned."));
364 sub cgi_getsession ($) {
367 eval q{use CGI::Session; use HTML::Entities};
369 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
371 my $oldmask=umask(077);
373 CGI::Session->new("driver:DB_File", $q,
374 { FileName => "$config{wikistatedir}/sessions.db" })
376 if (! $session || $@) {
378 error($error." ".CGI::Session->errstr());
386 # To guard against CSRF, the user's session id (sid)
387 # can be stored on a form. This function will check
388 # (for logged in users) that the sid on the form matches
389 # the session id in the cookie.
390 sub checksessionexpiry ($$) {
394 if (defined $session->param("name")) {
395 my $sid=$q->param('sid');
396 if (! defined $sid || $sid ne $session->id) {
397 error(gettext("Your login session has expired."));
402 sub cgi_savesession ($) {
405 # Force session flush with safe umask.
406 my $oldmask=umask(077);
417 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
422 binmode(STDIN, ":utf8");
424 run_hooks(cgi => sub { shift->($q) });
427 my $do=$q->param('do');
428 if (! defined $do || ! length $do) {
429 my $error = $q->cgi_error;
431 error("Request not processed: $error");
434 error("\"do\" parameter missing");
438 # Need to lock the wiki before getting a session.
443 $session=cgi_getsession($q);
446 # Auth hooks can sign a user in.
447 if ($do ne 'signin' && ! defined $session->param("name")) {
448 run_hooks(auth => sub {
449 shift->($q, $session)
451 if (defined $session->param("name")) {
452 # Make sure whatever user was authed is in the
454 if (! userinfo_get($session->param("name"), "regdate")) {
455 userinfo_setall($session->param("name"), {
456 email => defined $session->param("email") ? $session->param("email") : "",
459 }) || error("failed adding user");
464 check_banned($q, $session);
466 run_hooks(sessioncgi => sub { shift->($q, $session) });
468 if ($do eq 'signin') {
469 cgi_signin($q, $session);
470 cgi_savesession($session);
472 elsif ($do eq 'prefs') {
473 cgi_prefs($q, $session);
475 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
476 cgi_postsignin($q, $session);
479 error("unknown do parameter");
483 # Does not need to be called directly; all errors will go through here.
487 print "Content-type: text/html\n\n";
488 print cgitemplate(undef, gettext("Error"),
489 "<p class=\"error\">".gettext("Error").": $message</p>");