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 check_banned ($$) {
236 my $name=$session->param("name");
238 if (grep { $name eq $_ } @{$config{banned_users}}) {
239 print $q->header(-status => "403 Forbidden");
241 print gettext("You are banned.");
242 cgi_savesession($session);
248 sub cgi_getsession ($) {
251 eval q{use CGI::Session; use HTML::Entities};
253 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
255 my $oldmask=umask(077);
257 CGI::Session->new("driver:DB_File", $q,
258 { FileName => "$config{wikistatedir}/sessions.db" })
260 if (! $session || $@) {
261 error($@." ".CGI::Session->errstr());
269 # To guard against CSRF, the user's session id (sid)
270 # can be stored on a form. This function will check
271 # (for logged in users) that the sid on the form matches
272 # the session id in the cookie.
273 sub checksessionexpiry ($$) {
277 if (defined $session->param("name")) {
278 my $sid=$q->param('sid');
279 if (! defined $sid || $sid ne $session->id) {
280 error(gettext("Your login session has expired."));
285 sub cgi_savesession ($) {
288 # Force session flush with safe umask.
289 my $oldmask=umask(077);
300 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
305 binmode(STDIN, ":utf8");
307 run_hooks(cgi => sub { shift->($q) });
310 my $do=$q->param('do');
311 if (! defined $do || ! length $do) {
312 my $error = $q->cgi_error;
314 error("Request not processed: $error");
317 error("\"do\" parameter missing");
321 # Need to lock the wiki before getting a session.
326 $session=cgi_getsession($q);
329 # Auth hooks can sign a user in.
330 if ($do ne 'signin' && ! defined $session->param("name")) {
331 run_hooks(auth => sub {
332 shift->($q, $session)
334 if (defined $session->param("name")) {
335 # Make sure whatever user was authed is in the
337 if (! userinfo_get($session->param("name"), "regdate")) {
338 userinfo_setall($session->param("name"), {
342 }) || error("failed adding user");
347 check_banned($q, $session);
349 run_hooks(sessioncgi => sub { shift->($q, $session) });
351 if ($do eq 'signin') {
352 cgi_signin($q, $session);
353 cgi_savesession($session);
355 elsif ($do eq 'prefs') {
356 cgi_prefs($q, $session);
358 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
359 cgi_postsignin($q, $session);
362 error("unknown do parameter");
366 # Does not need to be called directly; all errors will go through here.
370 print "Content-type: text/html\n\n";
371 print misctemplate(gettext("Error"),
372 "<p class=\"error\">".gettext("Error").": $message</p>");