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 $_ }
120 @{$cgi->param_fetch($f)});
125 sub safe_decode_utf8 ($) {
127 # call decode_utf8 on >= 5.20 only if it's not already decoded,
128 # otherwise it balks, on < 5.20, always call it
129 if ($] < 5.02 || !Encode::is_utf8($octets)) {
130 return decode_utf8($octets);
137 sub decode_form_utf8 ($) {
140 foreach my $f ($form->field) {
141 my @value=map { safe_decode_utf8($_) } $form->field($f);
142 $form->field(name => $f,
150 # Check if the user is signed in. If not, redirect to the signin form and
151 # save their place to return to later.
152 sub needsignin ($$) {
156 if (! defined $session->param("name") ||
157 ! userinfo_get($session->param("name"), "regdate")) {
158 $session->param(postsignin => $q->query_string);
159 cgi_signin($q, $session);
160 cgi_savesession($session);
165 sub cgi_signin ($$;$) {
168 my $returnhtml=shift;
171 eval q{use CGI::FormBuilder};
173 my $form = CGI::FormBuilder->new(
183 template => {type => 'div'},
186 my $buttons=["Login"];
188 $form->field(name => "do", type => "hidden", value => "signin",
191 decode_form_utf8($form);
192 run_hooks(formbuilder_setup => sub {
193 shift->(form => $form, cgi => $q, session => $session,
194 buttons => $buttons);
196 decode_form_utf8($form);
198 if ($form->submitted) {
203 $form=prepform($form, $buttons, $session, $q);
204 return $form->render(submit => $buttons);
207 showform($form, $buttons, $session, $q);
210 sub cgi_postsignin ($$) {
214 # Continue with whatever was being done before the signin process.
215 if (defined $session->param("postsignin")) {
216 my $postsignin=CGI->new($session->param("postsignin"));
217 $session->clear("postsignin");
218 cgi($postsignin, $session);
219 cgi_savesession($session);
223 if ($config{sslcookie} && ! $q->https()) {
224 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
227 error(gettext("login failed, perhaps you need to turn on cookies?"));
236 needsignin($q, $session);
239 # The session id is stored on the form and checked to
240 # guard against CSRF.
241 my $sid=$q->param('sid');
242 if (! defined $sid) {
245 elsif ($sid ne $session->id) {
246 error(gettext("Your login session has expired."));
249 eval q{use CGI::FormBuilder};
251 my $form = CGI::FormBuilder->new(
252 title => "preferences",
253 name => "preferences",
264 template => {type => 'div'},
267 [login => gettext("Login")],
268 [preferences => gettext("Preferences")],
269 [admin => gettext("Admin")]
272 my $buttons=["Save Preferences", "Logout", "Cancel"];
274 decode_form_utf8($form);
275 run_hooks(formbuilder_setup => sub {
276 shift->(form => $form, cgi => $q, session => $session,
277 buttons => $buttons);
279 decode_form_utf8($form);
281 $form->field(name => "do", type => "hidden", value => "prefs",
283 $form->field(name => "sid", type => "hidden", value => $session->id,
285 $form->field(name => "email", size => 50, fieldset => "preferences");
287 my $user_name=$session->param("name");
289 if (! $form->submitted) {
290 $form->field(name => "email", force => 1,
291 value => userinfo_get($user_name, "email"));
294 if ($form->submitted eq 'Logout') {
296 redirect($q, baseurl(undef));
299 elsif ($form->submitted eq 'Cancel') {
300 redirect($q, baseurl(undef));
303 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
304 if (defined $form->field('email')) {
305 userinfo_set($user_name, 'email', $form->field('email')) ||
306 error("failed to set email");
309 $form->text(gettext("Preferences saved."));
312 showform($form, $buttons, $session, $q,
313 prefsurl => "", # avoid showing the preferences link
317 sub cgi_custom_failure ($$$) {
319 my $httpstatus=shift;
323 -status => $httpstatus,
328 # Internet Explod^Hrer won't show custom 404 responses
329 # unless they're >= 512 bytes
335 sub check_banned ($$) {
340 my $name=$session->param("name");
342 grep { $name eq $_ } @{$config{banned_users}}) {
346 foreach my $b (@{$config{banned_users}}) {
347 if (pagespec_match("", $b,
348 ip => $session->remote_addr(),
349 name => defined $name ? $name : "",
358 cgi_savesession($session);
361 gettext("You are banned."));
365 sub cgi_getsession ($) {
368 eval q{use CGI::Session; use HTML::Entities};
370 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
372 my $oldmask=umask(077);
374 CGI::Session->new("driver:DB_File", $q,
375 { FileName => "$config{wikistatedir}/sessions.db" })
377 if (! $session || $@) {
379 error($error." ".CGI::Session->errstr());
387 # To guard against CSRF, the user's session id (sid)
388 # can be stored on a form. This function will check
389 # (for logged in users) that the sid on the form matches
390 # the session id in the cookie.
391 sub checksessionexpiry ($$) {
395 if (defined $session->param("name")) {
396 my $sid=$q->param('sid');
397 if (! defined $sid || $sid ne $session->id) {
398 error(gettext("Your login session has expired."));
403 sub cgi_savesession ($) {
406 # Force session flush with safe umask.
407 my $oldmask=umask(077);
418 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
423 binmode(STDIN, ":utf8");
425 run_hooks(cgi => sub { shift->($q) });
428 my $do=$q->param('do');
429 if (! defined $do || ! length $do) {
430 my $error = $q->cgi_error;
432 error("Request not processed: $error");
435 error("\"do\" parameter missing");
439 # Need to lock the wiki before getting a session.
444 $session=cgi_getsession($q);
447 # Auth hooks can sign a user in.
448 if ($do ne 'signin' && ! defined $session->param("name")) {
449 run_hooks(auth => sub {
450 shift->($q, $session)
452 if (defined $session->param("name")) {
453 # Make sure whatever user was authed is in the
455 if (! userinfo_get($session->param("name"), "regdate")) {
456 userinfo_setall($session->param("name"), {
457 email => defined $session->param("email") ? $session->param("email") : "",
460 }) || error("failed adding user");
465 check_banned($q, $session);
467 run_hooks(sessioncgi => sub { shift->($q, $session) });
469 if ($do eq 'signin') {
470 cgi_signin($q, $session);
471 cgi_savesession($session);
473 elsif ($do eq 'prefs') {
474 cgi_prefs($q, $session);
476 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
477 cgi_postsignin($q, $session);
480 error("unknown do parameter");
484 # Does not need to be called directly; all errors will go through here.
488 print "Content-type: text/html\n\n";
489 print cgitemplate(undef, gettext("Error"),
490 "<p class=\"error\">".gettext("Error").": $message</p>");