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 = defined $cgi ? $cgi->url : $config{url};
64 if (exists $params{page}) {
65 $page=delete $params{page};
66 $params{forcebaseurl}=urlabs(urlto($page), $topurl);
68 run_hooks(pagetemplate => sub {
72 template => $template,
75 templateactions($template, "");
80 wikiname => $config{wikiname},
82 baseurl => urlabs(baseurl(), $topurl),
83 html5 => $config{html5},
87 return $template->output;
93 my $url=URI->new(urlabs(shift, $q->url));
94 if (! $config{w3mmode}) {
95 print $q->redirect($url);
98 print "Content-type: text/plain\n";
99 print "W3m-control: GOTO $url\n\n";
103 sub decode_cgi_utf8 ($) {
104 # decode_form_utf8 method is needed for 5.01
107 foreach my $f ($cgi->param) {
108 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
113 sub decode_form_utf8 ($) {
116 foreach my $f ($form->field) {
117 my @value=map { decode_utf8($_) } $form->field($f);
118 $form->field(name => $f,
126 # Check if the user is signed in. If not, redirect to the signin form and
127 # save their place to return to later.
128 sub needsignin ($$) {
132 if (! defined $session->param("name") ||
133 ! userinfo_get($session->param("name"), "regdate")) {
134 $session->param(postsignin => $q->query_string);
135 cgi_signin($q, $session);
136 cgi_savesession($session);
141 sub cgi_signin ($$;$) {
144 my $returnhtml=shift;
147 eval q{use CGI::FormBuilder};
149 my $form = CGI::FormBuilder->new(
159 template => {type => 'div'},
162 my $buttons=["Login"];
164 $form->field(name => "do", type => "hidden", value => "signin",
167 decode_form_utf8($form);
168 run_hooks(formbuilder_setup => sub {
169 shift->(form => $form, cgi => $q, session => $session,
170 buttons => $buttons);
172 decode_form_utf8($form);
174 if ($form->submitted) {
179 $form=prepform($form, $buttons, $session, $q);
180 return $form->render(submit => $buttons);
183 showform($form, $buttons, $session, $q);
186 sub cgi_postsignin ($$) {
190 # Continue with whatever was being done before the signin process.
191 if (defined $session->param("postsignin")) {
192 my $postsignin=CGI->new($session->param("postsignin"));
193 $session->clear("postsignin");
194 cgi($postsignin, $session);
195 cgi_savesession($session);
199 if ($config{sslcookie} && ! $q->https()) {
200 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
203 error(gettext("login failed, perhaps you need to turn on cookies?"));
212 needsignin($q, $session);
215 # The session id is stored on the form and checked to
216 # guard against CSRF.
217 my $sid=$q->param('sid');
218 if (! defined $sid) {
221 elsif ($sid ne $session->id) {
222 error(gettext("Your login session has expired."));
225 eval q{use CGI::FormBuilder};
227 my $form = CGI::FormBuilder->new(
228 title => "preferences",
229 name => "preferences",
240 template => {type => 'div'},
243 [login => gettext("Login")],
244 [preferences => gettext("Preferences")],
245 [admin => gettext("Admin")]
248 my $buttons=["Save Preferences", "Logout", "Cancel"];
250 decode_form_utf8($form);
251 run_hooks(formbuilder_setup => sub {
252 shift->(form => $form, cgi => $q, session => $session,
253 buttons => $buttons);
255 decode_form_utf8($form);
257 $form->field(name => "do", type => "hidden", value => "prefs",
259 $form->field(name => "sid", type => "hidden", value => $session->id,
261 $form->field(name => "email", size => 50, fieldset => "preferences");
263 my $user_name=$session->param("name");
265 if (! $form->submitted) {
266 $form->field(name => "email", force => 1,
267 value => userinfo_get($user_name, "email"));
270 if ($form->submitted eq 'Logout') {
272 redirect($q, baseurl(undef));
275 elsif ($form->submitted eq 'Cancel') {
276 redirect($q, baseurl(undef));
279 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
280 if (defined $form->field('email')) {
281 userinfo_set($user_name, 'email', $form->field('email')) ||
282 error("failed to set email");
285 $form->text(gettext("Preferences saved."));
288 showform($form, $buttons, $session, $q,
289 prefsurl => "", # avoid showing the preferences link
293 sub cgi_custom_failure ($$$) {
295 my $httpstatus=shift;
299 -status => $httpstatus,
304 # Internet Explod^Hrer won't show custom 404 responses
305 # unless they're >= 512 bytes
311 sub check_banned ($$) {
316 my $name=$session->param("name");
318 grep { $name eq $_ } @{$config{banned_users}}) {
322 foreach my $b (@{$config{banned_users}}) {
323 if (pagespec_match("", $b,
324 ip => $session->remote_addr(),
325 name => defined $name ? $name : "",
334 cgi_savesession($session);
337 gettext("You are banned."));
341 sub cgi_getsession ($) {
344 eval q{use CGI::Session; use HTML::Entities};
346 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
348 my $oldmask=umask(077);
350 CGI::Session->new("driver:DB_File", $q,
351 { FileName => "$config{wikistatedir}/sessions.db" })
353 if (! $session || $@) {
354 error($@." ".CGI::Session->errstr());
362 # To guard against CSRF, the user's session id (sid)
363 # can be stored on a form. This function will check
364 # (for logged in users) that the sid on the form matches
365 # the session id in the cookie.
366 sub checksessionexpiry ($$) {
370 if (defined $session->param("name")) {
371 my $sid=$q->param('sid');
372 if (! defined $sid || $sid ne $session->id) {
373 error(gettext("Your login session has expired."));
378 sub cgi_savesession ($) {
381 # Force session flush with safe umask.
382 my $oldmask=umask(077);
393 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
398 binmode(STDIN, ":utf8");
400 run_hooks(cgi => sub { shift->($q) });
403 my $do=$q->param('do');
404 if (! defined $do || ! length $do) {
405 my $error = $q->cgi_error;
407 error("Request not processed: $error");
410 error("\"do\" parameter missing");
414 # Need to lock the wiki before getting a session.
419 $session=cgi_getsession($q);
422 # Auth hooks can sign a user in.
423 if ($do ne 'signin' && ! defined $session->param("name")) {
424 run_hooks(auth => sub {
425 shift->($q, $session)
427 if (defined $session->param("name")) {
428 # Make sure whatever user was authed is in the
430 if (! userinfo_get($session->param("name"), "regdate")) {
431 userinfo_setall($session->param("name"), {
432 email => defined $session->param("email") ? $session->param("email") : "",
435 }) || error("failed adding user");
440 check_banned($q, $session);
442 run_hooks(sessioncgi => sub { shift->($q, $session) });
444 if ($do eq 'signin') {
445 cgi_signin($q, $session);
446 cgi_savesession($session);
448 elsif ($do eq 'prefs') {
449 cgi_prefs($q, $session);
451 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
452 cgi_postsignin($q, $session);
455 error("unknown do parameter");
459 # Does not need to be called directly; all errors will go through here.
463 print "Content-type: text/html\n\n";
464 print cgitemplate(undef, gettext("Error"),
465 "<p class=\"error\">".gettext("Error").": $message</p>");