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 if (!Encode::is_utf8($octets)) {
128 return decode_utf8($octets);
135 sub decode_form_utf8 ($) {
138 foreach my $f ($form->field) {
139 my @value=map { safe_decode_utf8($_) } $form->field($f);
140 $form->field(name => $f,
148 # Check if the user is signed in. If not, redirect to the signin form and
149 # save their place to return to later.
150 sub needsignin ($$) {
154 if (! defined $session->param("name") ||
155 ! userinfo_get($session->param("name"), "regdate")) {
156 $session->param(postsignin => $q->query_string);
157 cgi_signin($q, $session);
158 cgi_savesession($session);
163 sub cgi_signin ($$;$) {
166 my $returnhtml=shift;
169 eval q{use CGI::FormBuilder};
171 my $form = CGI::FormBuilder->new(
181 template => {type => 'div'},
184 my $buttons=["Login"];
186 $form->field(name => "do", type => "hidden", value => "signin",
189 decode_form_utf8($form);
190 run_hooks(formbuilder_setup => sub {
191 shift->(form => $form, cgi => $q, session => $session,
192 buttons => $buttons);
194 decode_form_utf8($form);
196 if ($form->submitted) {
201 $form=prepform($form, $buttons, $session, $q);
202 return $form->render(submit => $buttons);
205 showform($form, $buttons, $session, $q);
208 sub cgi_postsignin ($$) {
212 # Continue with whatever was being done before the signin process.
213 if (defined $session->param("postsignin")) {
214 my $postsignin=CGI->new($session->param("postsignin"));
215 $session->clear("postsignin");
216 cgi($postsignin, $session);
217 cgi_savesession($session);
221 if ($config{sslcookie} && ! $q->https()) {
222 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
225 error(gettext("login failed, perhaps you need to turn on cookies?"));
234 needsignin($q, $session);
237 # The session id is stored on the form and checked to
238 # guard against CSRF.
239 my $sid=$q->param('sid');
240 if (! defined $sid) {
243 elsif ($sid ne $session->id) {
244 error(gettext("Your login session has expired."));
247 eval q{use CGI::FormBuilder};
249 my $form = CGI::FormBuilder->new(
250 title => "preferences",
251 name => "preferences",
262 template => {type => 'div'},
265 [login => gettext("Login")],
266 [preferences => gettext("Preferences")],
267 [admin => gettext("Admin")]
270 my $buttons=["Save Preferences", "Logout", "Cancel"];
272 decode_form_utf8($form);
273 run_hooks(formbuilder_setup => sub {
274 shift->(form => $form, cgi => $q, session => $session,
275 buttons => $buttons);
277 decode_form_utf8($form);
279 $form->field(name => "do", type => "hidden", value => "prefs",
281 $form->field(name => "sid", type => "hidden", value => $session->id,
283 $form->field(name => "email", size => 50, fieldset => "preferences");
285 my $user_name=$session->param("name");
287 if (! $form->submitted) {
288 $form->field(name => "email", force => 1,
289 value => userinfo_get($user_name, "email"));
292 if ($form->submitted eq 'Logout') {
294 redirect($q, baseurl(undef));
297 elsif ($form->submitted eq 'Cancel') {
298 redirect($q, baseurl(undef));
301 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
302 if (defined $form->field('email')) {
303 userinfo_set($user_name, 'email', $form->field('email')) ||
304 error("failed to set email");
307 $form->text(gettext("Preferences saved."));
310 showform($form, $buttons, $session, $q,
311 prefsurl => "", # avoid showing the preferences link
315 sub cgi_custom_failure ($$$) {
317 my $httpstatus=shift;
321 -status => $httpstatus,
326 # Internet Explod^Hrer won't show custom 404 responses
327 # unless they're >= 512 bytes
333 sub check_banned ($$) {
338 my $name=$session->param("name");
339 my $cloak=cloak($name) if defined $name;
341 grep { $name eq $_ || $cloak 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 : "")
349 || pagespec_match("", $b,
350 ip => cloak($session->remote_addr()),
351 name => defined $cloak ? $cloak : "")) {
359 cgi_savesession($session);
362 gettext("You are banned."));
366 sub cgi_getsession ($) {
369 eval q{use CGI::Session; use HTML::Entities};
371 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
373 my $oldmask=umask(077);
375 CGI::Session->new("driver:DB_File", $q,
376 { FileName => "$config{wikistatedir}/sessions.db" })
378 if (! $session || $@) {
380 error($error." ".CGI::Session->errstr());
388 # To guard against CSRF, the user's session id (sid)
389 # can be stored on a form. This function will check
390 # (for logged in users) that the sid on the form matches
391 # the session id in the cookie.
392 sub checksessionexpiry ($$) {
396 if (defined $session->param("name")) {
397 my $sid=$q->param('sid');
398 if (! defined $sid || $sid ne $session->id) {
399 error(gettext("Your login session has expired."));
404 sub cgi_savesession ($) {
407 # Force session flush with safe umask.
408 my $oldmask=umask(077);
420 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
426 binmode(STDIN, ":utf8");
428 run_hooks(cgi => sub { shift->($q) });
431 my $do=$q->param('do');
432 if (! defined $do || ! length $do) {
433 my $error = $q->cgi_error;
435 error("Request not processed: $error");
438 error("\"do\" parameter missing");
442 # Need to lock the wiki before getting a session.
447 $session=cgi_getsession($q);
450 # Auth hooks can sign a user in.
451 if ($do ne 'signin' && ! defined $session->param("name")) {
452 run_hooks(auth => sub {
453 shift->($q, $session)
455 if (defined $session->param("name")) {
456 # Make sure whatever user was authed is in the
458 if (! userinfo_get($session->param("name"), "regdate")) {
459 userinfo_setall($session->param("name"), {
460 email => defined $session->param("email") ? $session->param("email") : "",
463 }) || error("failed adding user");
468 check_banned($q, $session);
470 run_hooks(sessioncgi => sub { shift->($q, $session) });
472 if ($do eq 'signin') {
473 cgi_signin($q, $session);
474 cgi_savesession($session);
476 elsif ($do eq 'prefs') {
477 cgi_prefs($q, $session);
479 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
480 cgi_postsignin($q, $session);
483 error("unknown do parameter");
487 # Does not need to be called directly; all errors will go through here.
491 print "Content-type: text/html\n\n";
492 print cgitemplate(undef, gettext("Error"),
493 "<p class=\"error\">".gettext("Error").": $message</p>");