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 ($$$$;@) {
62 # The base url needs to be a full URL. If urlto returns relative,
63 # force it absolute, using the same URL scheme used for the cgi.
64 my $baseurl = URI->new_abs(urlto($params{page}), $cgi->url);
66 showform($form, $buttons, $session, $cgi, @_,
67 forcebaseurl => $baseurl);
73 my $url=URI->new(shift);
74 if (! $config{w3mmode}) {
75 print $q->redirect($url);
78 print "Content-type: text/plain\n";
79 print "W3m-control: GOTO $url\n\n";
83 sub decode_cgi_utf8 ($) {
84 # decode_form_utf8 method is needed for 5.01
87 foreach my $f ($cgi->param) {
88 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
93 sub decode_form_utf8 ($) {
96 foreach my $f ($form->field) {
97 my @value=map { decode_utf8($_) } $form->field($f);
98 $form->field(name => $f,
106 # Check if the user is signed in. If not, redirect to the signin form and
107 # save their place to return to later.
108 sub needsignin ($$) {
112 if (! defined $session->param("name") ||
113 ! userinfo_get($session->param("name"), "regdate")) {
114 $session->param(postsignin => $ENV{QUERY_STRING});
115 cgi_signin($q, $session);
116 cgi_savesession($session);
121 sub cgi_signin ($$;$) {
124 my $returnhtml=shift;
127 eval q{use CGI::FormBuilder};
129 my $form = CGI::FormBuilder->new(
139 template => {type => 'div'},
142 my $buttons=["Login"];
144 $form->field(name => "do", type => "hidden", value => "signin",
147 decode_form_utf8($form);
148 run_hooks(formbuilder_setup => sub {
149 shift->(form => $form, cgi => $q, session => $session,
150 buttons => $buttons);
152 decode_form_utf8($form);
154 if ($form->submitted) {
159 $form=prepform($form, $buttons, $session, $q);
160 return $form->render(submit => $buttons);
163 showform($form, $buttons, $session, $q);
166 sub cgi_postsignin ($$) {
170 # Continue with whatever was being done before the signin process.
171 if (defined $session->param("postsignin")) {
172 my $postsignin=CGI->new($session->param("postsignin"));
173 $session->clear("postsignin");
174 cgi($postsignin, $session);
175 cgi_savesession($session);
179 if ($config{sslcookie} && ! $q->https()) {
180 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
183 error(gettext("login failed, perhaps you need to turn on cookies?"));
192 needsignin($q, $session);
195 # The session id is stored on the form and checked to
196 # guard against CSRF.
197 my $sid=$q->param('sid');
198 if (! defined $sid) {
201 elsif ($sid ne $session->id) {
202 error(gettext("Your login session has expired."));
205 eval q{use CGI::FormBuilder};
207 my $form = CGI::FormBuilder->new(
208 title => "preferences",
209 name => "preferences",
220 template => {type => 'div'},
223 [login => gettext("Login")],
224 [preferences => gettext("Preferences")],
225 [admin => gettext("Admin")]
228 my $buttons=["Save Preferences", "Logout", "Cancel"];
230 decode_form_utf8($form);
231 run_hooks(formbuilder_setup => sub {
232 shift->(form => $form, cgi => $q, session => $session,
233 buttons => $buttons);
235 decode_form_utf8($form);
237 $form->field(name => "do", type => "hidden", value => "prefs",
239 $form->field(name => "sid", type => "hidden", value => $session->id,
241 $form->field(name => "email", size => 50, fieldset => "preferences");
243 my $user_name=$session->param("name");
245 if (! $form->submitted) {
246 $form->field(name => "email", force => 1,
247 value => userinfo_get($user_name, "email"));
250 if ($form->submitted eq 'Logout') {
252 redirect($q, baseurl(undef));
255 elsif ($form->submitted eq 'Cancel') {
256 redirect($q, baseurl(undef));
259 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
260 if (defined $form->field('email')) {
261 userinfo_set($user_name, 'email', $form->field('email')) ||
262 error("failed to set email");
265 $form->text(gettext("Preferences saved."));
268 showform($form, $buttons, $session, $q,
269 prefsurl => "", # avoid showing the preferences link
273 sub cgi_custom_failure ($$$) {
275 my $httpstatus=shift;
279 -status => $httpstatus,
284 # Internet Explod^Hrer won't show custom 404 responses
285 # unless they're >= 512 bytes
291 sub check_banned ($$) {
296 my $name=$session->param("name");
298 grep { $name eq $_ } @{$config{banned_users}}) {
302 foreach my $b (@{$config{banned_users}}) {
303 if (pagespec_match("", $b,
304 ip => $session->remote_addr(),
305 name => defined $name ? $name : "",
314 cgi_savesession($session);
317 gettext("You are banned."));
321 sub cgi_getsession ($) {
324 eval q{use CGI::Session; use HTML::Entities};
326 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
328 my $oldmask=umask(077);
330 CGI::Session->new("driver:DB_File", $q,
331 { FileName => "$config{wikistatedir}/sessions.db" })
333 if (! $session || $@) {
334 error($@." ".CGI::Session->errstr());
342 # To guard against CSRF, the user's session id (sid)
343 # can be stored on a form. This function will check
344 # (for logged in users) that the sid on the form matches
345 # the session id in the cookie.
346 sub checksessionexpiry ($$) {
350 if (defined $session->param("name")) {
351 my $sid=$q->param('sid');
352 if (! defined $sid || $sid ne $session->id) {
353 error(gettext("Your login session has expired."));
358 sub cgi_savesession ($) {
361 # Force session flush with safe umask.
362 my $oldmask=umask(077);
373 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
378 binmode(STDIN, ":utf8");
380 run_hooks(cgi => sub { shift->($q) });
383 my $do=$q->param('do');
384 if (! defined $do || ! length $do) {
385 my $error = $q->cgi_error;
387 error("Request not processed: $error");
390 error("\"do\" parameter missing");
394 # Need to lock the wiki before getting a session.
399 $session=cgi_getsession($q);
402 # Auth hooks can sign a user in.
403 if ($do ne 'signin' && ! defined $session->param("name")) {
404 run_hooks(auth => sub {
405 shift->($q, $session)
407 if (defined $session->param("name")) {
408 # Make sure whatever user was authed is in the
410 if (! userinfo_get($session->param("name"), "regdate")) {
411 userinfo_setall($session->param("name"), {
415 }) || error("failed adding user");
420 check_banned($q, $session);
422 run_hooks(sessioncgi => sub { shift->($q, $session) });
424 if ($do eq 'signin') {
425 cgi_signin($q, $session);
426 cgi_savesession($session);
428 elsif ($do eq 'prefs') {
429 cgi_prefs($q, $session);
431 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
432 cgi_postsignin($q, $session);
435 error("unknown do parameter");
439 # Does not need to be called directly; all errors will go through here.
443 print "Content-type: text/html\n\n";
444 print misctemplate(gettext("Error"),
445 "<p class=\"error\">".gettext("Error").": $message</p>");