9 use open qw{:utf8 :std};
15 if ($config{sslcookie}) {
16 print $session->header(-charset => 'utf-8',
17 -cookie => $session->cookie(-httponly => 1, -secure => 1));
19 print $session->header(-charset => 'utf-8',
20 -cookie => $session->cookie(-httponly => 1));
24 sub showform ($$$$;@) {
30 if (exists $hooks{formbuilder}) {
31 run_hooks(formbuilder => sub {
32 shift->(form => $form, cgi => $cgi, session => $session,
37 printheader($session);
38 print misctemplate($form->title, $form->render(submit => $buttons), @_);
44 if (! $config{w3mmode}) {
45 print $q->redirect($url);
48 print "Content-type: text/plain\n";
49 print "W3m-control: GOTO $url\n\n";
53 sub decode_cgi_utf8 ($) {
54 # decode_form_utf8 method is needed for 5.10
57 foreach my $f ($cgi->param) {
58 $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
63 sub decode_form_utf8 ($) {
66 foreach my $f ($form->field) {
67 $form->field(name => $f,
68 value => decode_utf8($form->field($f)),
75 # Check if the user is signed in. If not, redirect to the signin form and
76 # save their place to return to later.
81 if (! defined $session->param("name") ||
82 ! userinfo_get($session->param("name"), "regdate")) {
83 $session->param(postsignin => $ENV{QUERY_STRING});
84 cgi_signin($q, $session);
85 cgi_savesession($session);
95 eval q{use CGI::FormBuilder};
97 my $form = CGI::FormBuilder->new(
105 action => $config{cgiurl},
107 template => {type => 'div'},
108 stylesheet => baseurl()."style.css",
110 my $buttons=["Login"];
112 if ($q->param("do") ne "signin" && !$form->submitted) {
113 $form->text(gettext("You need to log in first."));
115 $form->field(name => "do", type => "hidden", value => "signin",
118 decode_form_utf8($form);
119 run_hooks(formbuilder_setup => sub {
120 shift->(form => $form, cgi => $q, session => $session,
121 buttons => $buttons);
123 decode_form_utf8($form);
125 if ($form->submitted) {
129 showform($form, $buttons, $session, $q);
132 sub cgi_postsignin ($$) {
136 # Continue with whatever was being done before the signin process.
137 if (defined $session->param("postsignin")) {
138 my $postsignin=CGI->new($session->param("postsignin"));
139 $session->clear("postsignin");
140 cgi($postsignin, $session);
141 cgi_savesession($session);
145 error(gettext("login failed, perhaps you need to turn on cookies?"));
153 needsignin($q, $session);
156 # The session id is stored on the form and checked to
157 # guard against CSRF.
158 my $sid=$q->param('sid');
159 if (! defined $sid) {
162 elsif ($sid ne $session->id) {
163 error(gettext("Your login session has expired."));
166 eval q{use CGI::FormBuilder};
168 my $form = CGI::FormBuilder->new(
169 title => "preferences",
170 name => "preferences",
180 action => $config{cgiurl},
181 template => {type => 'div'},
182 stylesheet => baseurl()."style.css",
184 [login => gettext("Login")],
185 [preferences => gettext("Preferences")],
186 [admin => gettext("Admin")]
189 my $buttons=["Save Preferences", "Logout", "Cancel"];
191 decode_form_utf8($form);
192 run_hooks(formbuilder_setup => sub {
193 shift->(form => $form, cgi => $q, session => $session,
194 buttons => $buttons);
196 decode_form_utf8($form);
198 $form->field(name => "do", type => "hidden", value => "prefs",
200 $form->field(name => "sid", type => "hidden", value => $session->id,
202 $form->field(name => "email", size => 50, fieldset => "preferences");
204 my $user_name=$session->param("name");
206 if (! $form->submitted) {
207 $form->field(name => "email", force => 1,
208 value => userinfo_get($user_name, "email"));
211 if ($form->submitted eq 'Logout') {
213 redirect($q, $config{url});
216 elsif ($form->submitted eq 'Cancel') {
217 redirect($q, $config{url});
220 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
221 if (defined $form->field('email')) {
222 userinfo_set($user_name, 'email', $form->field('email')) ||
223 error("failed to set email");
226 $form->text(gettext("Preferences saved."));
229 showform($form, $buttons, $session, $q);
232 sub cgi_custom_failure ($$) {
239 # Internet Explod^Hrer won't show custom 404 responses
240 # unless they're >= 512 bytes
246 sub check_banned ($$) {
250 my $name=$session->param("name");
252 if (grep { $name eq $_ } @{$config{banned_users}}) {
254 cgi_savesession($session);
256 $q->header(-status => "403 Forbidden"),
257 gettext("You are banned."));
262 sub cgi_getsession ($) {
265 eval q{use CGI::Session; use HTML::Entities};
267 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
269 my $oldmask=umask(077);
271 CGI::Session->new("driver:DB_File", $q,
272 { FileName => "$config{wikistatedir}/sessions.db" })
274 if (! $session || $@) {
275 error($@." ".CGI::Session->errstr());
283 # To guard against CSRF, the user's session id (sid)
284 # can be stored on a form. This function will check
285 # (for logged in users) that the sid on the form matches
286 # the session id in the cookie.
287 sub checksessionexpiry ($$) {
291 if (defined $session->param("name")) {
292 my $sid=$q->param('sid');
293 if (! defined $sid || $sid ne $session->id) {
294 error(gettext("Your login session has expired."));
299 sub cgi_savesession ($) {
302 # Force session flush with safe umask.
303 my $oldmask=umask(077);
314 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
319 binmode(STDIN, ":utf8");
321 run_hooks(cgi => sub { shift->($q) });
324 my $do=$q->param('do');
325 if (! defined $do || ! length $do) {
326 my $error = $q->cgi_error;
328 error("Request not processed: $error");
331 error("\"do\" parameter missing");
335 # Need to lock the wiki before getting a session.
340 $session=cgi_getsession($q);
343 # Auth hooks can sign a user in.
344 if ($do ne 'signin' && ! defined $session->param("name")) {
345 run_hooks(auth => sub {
346 shift->($q, $session)
348 if (defined $session->param("name")) {
349 # Make sure whatever user was authed is in the
351 if (! userinfo_get($session->param("name"), "regdate")) {
352 userinfo_setall($session->param("name"), {
356 }) || error("failed adding user");
361 check_banned($q, $session);
363 run_hooks(sessioncgi => sub { shift->($q, $session) });
365 if ($do eq 'signin') {
366 cgi_signin($q, $session);
367 cgi_savesession($session);
369 elsif ($do eq 'prefs') {
370 cgi_prefs($q, $session);
372 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
373 cgi_postsignin($q, $session);
376 error("unknown do parameter");
380 # Does not need to be called directly; all errors will go through here.
384 print "Content-type: text/html\n\n";
385 print misctemplate(gettext("Error"),
386 "<p class=\"error\">".gettext("Error").": $message</p>");