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 my $email = $form->field('email');
281 if (defined $email) {
282 userinfo_set($user_name, 'email', $email) ||
283 error("failed to set email");
286 $form->text(gettext("Preferences saved."));
289 showform($form, $buttons, $session, $q,
290 prefsurl => "", # avoid showing the preferences link
294 sub cgi_custom_failure ($$$) {
296 my $httpstatus=shift;
300 -status => $httpstatus,
305 # Internet Explod^Hrer won't show custom 404 responses
306 # unless they're >= 512 bytes
312 sub check_banned ($$) {
317 my $name=$session->param("name");
319 grep { $name eq $_ } @{$config{banned_users}}) {
323 foreach my $b (@{$config{banned_users}}) {
324 if (pagespec_match("", $b,
325 ip => $session->remote_addr(),
326 name => defined $name ? $name : "",
335 cgi_savesession($session);
338 gettext("You are banned."));
342 sub cgi_getsession ($) {
345 eval q{use CGI::Session; use HTML::Entities};
347 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
349 my $oldmask=umask(077);
351 CGI::Session->new("driver:DB_File", $q,
352 { FileName => "$config{wikistatedir}/sessions.db" })
354 if (! $session || $@) {
355 error($@." ".CGI::Session->errstr());
363 # To guard against CSRF, the user's session id (sid)
364 # can be stored on a form. This function will check
365 # (for logged in users) that the sid on the form matches
366 # the session id in the cookie.
367 sub checksessionexpiry ($$) {
371 if (defined $session->param("name")) {
372 my $sid=$q->param('sid');
373 if (! defined $sid || $sid ne $session->id) {
374 error(gettext("Your login session has expired."));
379 sub cgi_savesession ($) {
382 # Force session flush with safe umask.
383 my $oldmask=umask(077);
394 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
399 binmode(STDIN, ":utf8");
401 run_hooks(cgi => sub { shift->($q) });
404 my $do=$q->param('do');
405 if (! defined $do || ! length $do) {
406 my $error = $q->cgi_error;
408 error("Request not processed: $error");
411 error("\"do\" parameter missing");
415 # Need to lock the wiki before getting a session.
420 $session=cgi_getsession($q);
423 # Auth hooks can sign a user in.
424 if ($do ne 'signin' && ! defined $session->param("name")) {
425 run_hooks(auth => sub {
426 shift->($q, $session)
428 if (defined $session->param("name")) {
429 # Make sure whatever user was authed is in the
431 if (! userinfo_get($session->param("name"), "regdate")) {
432 userinfo_setall($session->param("name"), {
433 email => defined $session->param("email") ? $session->param("email") : "",
436 }) || error("failed adding user");
441 check_banned($q, $session);
443 run_hooks(sessioncgi => sub { shift->($q, $session) });
445 if ($do eq 'signin') {
446 cgi_signin($q, $session);
447 cgi_savesession($session);
449 elsif ($do eq 'prefs') {
450 cgi_prefs($q, $session);
452 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
453 cgi_postsignin($q, $session);
456 error("unknown do parameter");
460 # Does not need to be called directly; all errors will go through here.
464 eval q{use HTML::Entities};
465 $message = encode_entities($message);
467 print "Content-type: text/html\n\n";
468 print cgitemplate(undef, gettext("Error"),
469 "<p class=\"error\">".gettext("Error").": $message</p>");