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");
62 if (exists $params{page}) {
63 $page=delete $params{page};
64 $params{forcebaseurl}=urlto($page);
66 run_hooks(pagetemplate => sub {
70 template => $template,
73 templateactions($template, "");
75 my $baseurl = baseurl();
80 wikiname => $config{wikiname},
83 html5 => $config{html5},
87 return $template->output;
95 if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
99 my $url=URI->new(urlabs(shift, $topurl));
100 if (! $config{w3mmode}) {
101 print $q->redirect($url);
104 print "Content-type: text/plain\n";
105 print "W3m-control: GOTO $url\n\n";
109 sub decode_cgi_utf8 ($) {
110 # decode_form_utf8 method is needed for 5.01
113 foreach my $f ($cgi->param) {
114 $cgi->param($f, map { decode_utf8 $_ }
115 @{$cgi->param_fetch($f)});
120 sub safe_decode_utf8 ($) {
122 if (!Encode::is_utf8($octets)) {
123 return decode_utf8($octets);
130 sub decode_form_utf8 ($) {
133 foreach my $f ($form->field) {
134 my @value=map { safe_decode_utf8($_) } $form->field($f);
135 $form->field(name => $f,
143 # Check if the user is signed in. If not, redirect to the signin form and
144 # save their place to return to later.
145 sub needsignin ($$) {
149 if (! defined $session->param("name") ||
150 ! userinfo_get($session->param("name"), "regdate")) {
151 $session->param(postsignin => $q->query_string);
152 cgi_signin($q, $session);
153 cgi_savesession($session);
158 sub cgi_signin ($$;$) {
161 my $returnhtml=shift;
164 eval q{use CGI::FormBuilder};
166 my $form = CGI::FormBuilder->new(
176 template => {type => 'div'},
179 my $buttons=["Login"];
181 $form->field(name => "do", type => "hidden", value => "signin",
184 decode_form_utf8($form);
185 run_hooks(formbuilder_setup => sub {
186 shift->(form => $form, cgi => $q, session => $session,
187 buttons => $buttons);
189 decode_form_utf8($form);
191 if ($form->submitted) {
196 $form=prepform($form, $buttons, $session, $q);
197 return $form->render(submit => $buttons);
200 showform($form, $buttons, $session, $q);
203 sub cgi_postsignin ($$) {
207 # Continue with whatever was being done before the signin process.
208 if (defined $session->param("postsignin")) {
209 my $postsignin=CGI->new($session->param("postsignin"));
210 $session->clear("postsignin");
211 cgi($postsignin, $session);
212 cgi_savesession($session);
216 if ($config{sslcookie} && ! $q->https()) {
217 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
220 error(gettext("login failed, perhaps you need to turn on cookies?"));
229 needsignin($q, $session);
232 # The session id is stored on the form and checked to
233 # guard against CSRF.
234 my $sid=$q->param('sid');
235 if (! defined $sid) {
238 elsif ($sid ne $session->id) {
239 error(gettext("Your login session has expired."));
242 eval q{use CGI::FormBuilder};
244 my $form = CGI::FormBuilder->new(
245 title => "preferences",
246 name => "preferences",
257 template => {type => 'div'},
260 [login => gettext("Login")],
261 [preferences => gettext("Preferences")],
262 [admin => gettext("Admin")]
265 my $buttons=["Save Preferences", "Logout", "Cancel"];
267 decode_form_utf8($form);
268 run_hooks(formbuilder_setup => sub {
269 shift->(form => $form, cgi => $q, session => $session,
270 buttons => $buttons);
272 decode_form_utf8($form);
274 $form->field(name => "do", type => "hidden", value => "prefs",
276 $form->field(name => "sid", type => "hidden", value => $session->id,
278 $form->field(name => "email", size => 50, fieldset => "preferences");
280 my $user_name=$session->param("name");
282 if (! $form->submitted) {
283 $form->field(name => "email", force => 1,
284 value => userinfo_get($user_name, "email"));
287 if ($form->submitted eq 'Logout') {
289 redirect($q, baseurl(undef));
292 elsif ($form->submitted eq 'Cancel') {
293 redirect($q, baseurl(undef));
296 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
297 if (defined $form->field('email')) {
298 userinfo_set($user_name, 'email', $form->field('email')) ||
299 error("failed to set email");
302 $form->text(gettext("Preferences saved."));
305 showform($form, $buttons, $session, $q,
306 prefsurl => "", # avoid showing the preferences link
310 sub cgi_custom_failure ($$$) {
312 my $httpstatus=shift;
316 -status => $httpstatus,
321 # Internet Explod^Hrer won't show custom 404 responses
322 # unless they're >= 512 bytes
328 sub check_banned ($$) {
333 my $name=$session->param("name");
334 my $cloak=cloak($name) if defined $name;
336 grep { $name eq $_ || $cloak eq $_ } @{$config{banned_users}}) {
340 foreach my $b (@{$config{banned_users}}) {
341 if (pagespec_match("", $b,
342 ip => $session->remote_addr(),
343 name => defined $name ? $name : "")
344 || pagespec_match("", $b,
345 ip => cloak($session->remote_addr()),
346 name => defined $cloak ? $cloak : "")) {
354 cgi_savesession($session);
357 gettext("You are banned."));
361 sub cgi_getsession ($) {
364 eval q{use CGI::Session; use HTML::Entities};
366 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
368 my $oldmask=umask(077);
370 CGI::Session->new("driver:DB_File", $q,
371 { FileName => "$config{wikistatedir}/sessions.db" })
373 if (! $session || $@) {
375 error($error." ".CGI::Session->errstr());
383 # To guard against CSRF, the user's session id (sid)
384 # can be stored on a form. This function will check
385 # (for logged in users) that the sid on the form matches
386 # the session id in the cookie.
387 sub checksessionexpiry ($$) {
391 if (defined $session->param("name")) {
392 my $sid=$q->param('sid');
393 if (! defined $sid || $sid ne $session->id) {
394 error(gettext("Your login session has expired."));
399 sub cgi_savesession ($) {
402 # Force session flush with safe umask.
403 my $oldmask=umask(077);
415 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
421 binmode(STDIN, ":utf8");
423 run_hooks(cgi => sub { shift->($q) });
426 my $do=$q->param('do');
427 if (! defined $do || ! length $do) {
428 my $error = $q->cgi_error;
430 error("Request not processed: $error");
433 error("\"do\" parameter missing");
437 # Need to lock the wiki before getting a session.
442 $session=cgi_getsession($q);
445 # Auth hooks can sign a user in.
446 if ($do ne 'signin' && ! defined $session->param("name")) {
447 run_hooks(auth => sub {
448 shift->($q, $session)
450 if (defined $session->param("name")) {
451 # Make sure whatever user was authed is in the
453 if (! userinfo_get($session->param("name"), "regdate")) {
454 userinfo_setall($session->param("name"), {
455 email => defined $session->param("email") ? $session->param("email") : "",
458 }) || error("failed adding user");
463 check_banned($q, $session);
465 run_hooks(sessioncgi => sub { shift->($q, $session) });
467 if ($do eq 'signin') {
468 cgi_signin($q, $session);
469 cgi_savesession($session);
471 elsif ($do eq 'prefs') {
472 cgi_prefs($q, $session);
474 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
475 cgi_postsignin($q, $session);
478 error("unknown do parameter");
482 # Does not need to be called directly; all errors will go through here.
486 eval q{use HTML::Entities};
487 $message = encode_entities($message);
489 print "Content-type: text/html\n\n";
490 print cgitemplate(undef, gettext("Error"),
491 "<p class=\"error\">".gettext("Error").": $message</p>");