9 use open qw{:utf8 :std};
15 if ($ENV{HTTPS} || $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 misctemplate($form->title, $form->render(submit => $buttons), @_);
52 # Like showform, but the base url will be set to allow edit previews
53 # that use links relative to the specified page.
54 sub showform_preview ($$$$;@) {
61 # The base url needs to be a full URL, and urlto may return a path.
62 my $baseurl = absurl(urlto($params{page}), $cgi->url);
64 showform($form, $buttons, $session, $cgi, @_,
65 forcebaseurl => $baseurl);
68 # Forces a partial url (path only) to absolute, using the same
69 # URL scheme as the CGI. Full URLs are left unchanged.
75 return URI->new_abs($partialurl, $q);
81 my $url=URI->new(absurl(shift, $q));
82 if (! $config{w3mmode}) {
83 print $q->redirect($url);
86 print "Content-type: text/plain\n";
87 print "W3m-control: GOTO $url\n\n";
91 sub decode_cgi_utf8 ($) {
92 # decode_form_utf8 method is needed for 5.01
95 foreach my $f ($cgi->param) {
96 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
101 sub decode_form_utf8 ($) {
104 foreach my $f ($form->field) {
105 my @value=map { decode_utf8($_) } $form->field($f);
106 $form->field(name => $f,
114 # Check if the user is signed in. If not, redirect to the signin form and
115 # save their place to return to later.
116 sub needsignin ($$) {
120 if (! defined $session->param("name") ||
121 ! userinfo_get($session->param("name"), "regdate")) {
122 $session->param(postsignin => $ENV{QUERY_STRING});
123 cgi_signin($q, $session);
124 cgi_savesession($session);
129 sub cgi_signin ($$;$) {
132 my $returnhtml=shift;
135 eval q{use CGI::FormBuilder};
137 my $form = CGI::FormBuilder->new(
147 template => {type => 'div'},
150 my $buttons=["Login"];
152 $form->field(name => "do", type => "hidden", value => "signin",
155 decode_form_utf8($form);
156 run_hooks(formbuilder_setup => sub {
157 shift->(form => $form, cgi => $q, session => $session,
158 buttons => $buttons);
160 decode_form_utf8($form);
162 if ($form->submitted) {
167 $form=prepform($form, $buttons, $session, $q);
168 return $form->render(submit => $buttons);
171 showform($form, $buttons, $session, $q);
174 sub cgi_postsignin ($$) {
178 # Continue with whatever was being done before the signin process.
179 if (defined $session->param("postsignin")) {
180 my $postsignin=CGI->new($session->param("postsignin"));
181 $session->clear("postsignin");
182 cgi($postsignin, $session);
183 cgi_savesession($session);
187 if ($config{sslcookie} && ! $q->https()) {
188 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
191 error(gettext("login failed, perhaps you need to turn on cookies?"));
200 needsignin($q, $session);
203 # The session id is stored on the form and checked to
204 # guard against CSRF.
205 my $sid=$q->param('sid');
206 if (! defined $sid) {
209 elsif ($sid ne $session->id) {
210 error(gettext("Your login session has expired."));
213 eval q{use CGI::FormBuilder};
215 my $form = CGI::FormBuilder->new(
216 title => "preferences",
217 name => "preferences",
228 template => {type => 'div'},
231 [login => gettext("Login")],
232 [preferences => gettext("Preferences")],
233 [admin => gettext("Admin")]
236 my $buttons=["Save Preferences", "Logout", "Cancel"];
238 decode_form_utf8($form);
239 run_hooks(formbuilder_setup => sub {
240 shift->(form => $form, cgi => $q, session => $session,
241 buttons => $buttons);
243 decode_form_utf8($form);
245 $form->field(name => "do", type => "hidden", value => "prefs",
247 $form->field(name => "sid", type => "hidden", value => $session->id,
249 $form->field(name => "email", size => 50, fieldset => "preferences");
251 my $user_name=$session->param("name");
253 if (! $form->submitted) {
254 $form->field(name => "email", force => 1,
255 value => userinfo_get($user_name, "email"));
258 if ($form->submitted eq 'Logout') {
260 redirect($q, baseurl(undef));
263 elsif ($form->submitted eq 'Cancel') {
264 redirect($q, baseurl(undef));
267 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
268 if (defined $form->field('email')) {
269 userinfo_set($user_name, 'email', $form->field('email')) ||
270 error("failed to set email");
273 $form->text(gettext("Preferences saved."));
276 showform($form, $buttons, $session, $q,
277 prefsurl => "", # avoid showing the preferences link
281 sub cgi_custom_failure ($$$) {
283 my $httpstatus=shift;
287 -status => $httpstatus,
292 # Internet Explod^Hrer won't show custom 404 responses
293 # unless they're >= 512 bytes
299 sub check_banned ($$) {
304 my $name=$session->param("name");
306 grep { $name eq $_ } @{$config{banned_users}}) {
310 foreach my $b (@{$config{banned_users}}) {
311 if (pagespec_match("", $b,
312 ip => $session->remote_addr(),
313 name => defined $name ? $name : "",
322 cgi_savesession($session);
325 gettext("You are banned."));
329 sub cgi_getsession ($) {
332 eval q{use CGI::Session; use HTML::Entities};
334 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
336 my $oldmask=umask(077);
338 CGI::Session->new("driver:DB_File", $q,
339 { FileName => "$config{wikistatedir}/sessions.db" })
341 if (! $session || $@) {
342 error($@." ".CGI::Session->errstr());
350 # To guard against CSRF, the user's session id (sid)
351 # can be stored on a form. This function will check
352 # (for logged in users) that the sid on the form matches
353 # the session id in the cookie.
354 sub checksessionexpiry ($$) {
358 if (defined $session->param("name")) {
359 my $sid=$q->param('sid');
360 if (! defined $sid || $sid ne $session->id) {
361 error(gettext("Your login session has expired."));
366 sub cgi_savesession ($) {
369 # Force session flush with safe umask.
370 my $oldmask=umask(077);
381 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
386 binmode(STDIN, ":utf8");
388 run_hooks(cgi => sub { shift->($q) });
391 my $do=$q->param('do');
392 if (! defined $do || ! length $do) {
393 my $error = $q->cgi_error;
395 error("Request not processed: $error");
398 error("\"do\" parameter missing");
402 # Need to lock the wiki before getting a session.
407 $session=cgi_getsession($q);
410 # Auth hooks can sign a user in.
411 if ($do ne 'signin' && ! defined $session->param("name")) {
412 run_hooks(auth => sub {
413 shift->($q, $session)
415 if (defined $session->param("name")) {
416 # Make sure whatever user was authed is in the
418 if (! userinfo_get($session->param("name"), "regdate")) {
419 userinfo_setall($session->param("name"), {
423 }) || error("failed adding user");
428 check_banned($q, $session);
430 run_hooks(sessioncgi => sub { shift->($q, $session) });
432 if ($do eq 'signin') {
433 cgi_signin($q, $session);
434 cgi_savesession($session);
436 elsif ($do eq 'prefs') {
437 cgi_prefs($q, $session);
439 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
440 cgi_postsignin($q, $session);
443 error("unknown do parameter");
447 # Does not need to be called directly; all errors will go through here.
451 print "Content-type: text/html\n\n";
452 print misctemplate(gettext("Error"),
453 "<p class=\"error\">".gettext("Error").": $message</p>");