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);
294 # cgi_goto(CGI, [page])
295 # Redirect to a specified page, or display "not found". If not specified,
296 # the page param from the CGI object is used.
301 if (!defined $page) {
302 $page = decode_utf8($q->param("page"));
304 if (!defined $page) {
305 error("missing page parameter");
311 # If the page is internal (like a comment), see if it has a
312 # permalink. Comments do.
313 if (isinternal($page) &&
314 defined $pagestate{$page}{meta}{permalink}) {
315 redirect($q, $pagestate{$page}{meta}{permalink});
318 my $link = bestlink("", $page);
320 if (! length $link) {
321 print $q->header(-status => "404 Not Found");
322 print misctemplate(gettext("missing page"),
324 sprintf(gettext("The page %s does not exist."),
325 htmllink("", "", $page)).
329 redirect($q, urlto($link, undef, 1));
335 sub cgi_page_from_404 ($$$) {
340 # fail if missing from environment or whatever
341 return undef unless defined $path;
342 return undef unless defined $baseurl;
344 # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or
345 # /~fred/foo/bar/index.html
346 # with usedirs off, path is like /~fred/foo/bar.html
347 # baseurl is like 'http://people.example.com/~fred'
349 # convert baseurl to ~fred
350 unless ($baseurl =~ s{^https?://[^/]+/?}{}) {
354 # convert path to /~fred/foo/bar
356 $path =~ s/\/*(?:index\.$config{htmlext})?$//;
359 $path =~ s/\.$config{htmlext}$//;
363 unless ($path =~ s{^/*\Q$baseurl\E/*}{}) {
367 # special case for the index
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 # goto is the preferred name for this; recentchanges_link and
403 # commenter are for compatibility with any saved URLs
404 if ($do eq 'goto' || $do eq 'recentchanges_link' ||
405 $do eq 'commenter') {
409 # Need to lock the wiki before getting a session.
414 $session=cgi_getsession($q);
417 # Auth hooks can sign a user in.
418 if ($do ne 'signin' && ! defined $session->param("name")) {
419 run_hooks(auth => sub {
420 shift->($q, $session)
422 if (defined $session->param("name")) {
423 # Make sure whatever user was authed is in the
425 if (! userinfo_get($session->param("name"), "regdate")) {
426 userinfo_setall($session->param("name"), {
430 }) || error("failed adding user");
435 check_banned($q, $session);
437 run_hooks(sessioncgi => sub { shift->($q, $session) });
439 if ($do eq 'signin') {
440 cgi_signin($q, $session);
441 cgi_savesession($session);
443 elsif ($do eq 'prefs') {
444 cgi_prefs($q, $session);
446 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
447 cgi_postsignin($q, $session);
450 error("unknown do parameter");
454 # Does not need to be called directly; all errors will go through here.
458 print "Content-type: text/html\n\n";
459 print misctemplate(gettext("Error"),
460 "<p class=\"error\">".gettext("Error").": $message</p>");