9 use open qw{:utf8 :std};
15 if (($ENV{HTTPS} && lc $ENV{HTTPS} ne "off") || $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 cgitemplate($cgi, $form->title,
50 $form->render(submit => $buttons), @_);
53 sub cgitemplate ($$$;@) {
59 my $template=template("page.tmpl");
62 if (exists $params{page}) {
63 $page=delete $params{page};
64 $params{forcebaseurl}=urlto($page);
66 run_hooks(pagetemplate => sub {
70 template => $template,
73 templateactions($template, "");
75 my $baseurl = baseurl();
80 wikiname => $config{wikiname},
83 html5 => $config{html5},
87 return $template->output;
94 my $topurl = $config{cgiurl};
95 if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
99 my $url=URI->new(urlabs(shift, $topurl));
100 if (! $config{w3mmode}) {
101 print $q->redirect($url);
104 print "Content-type: text/plain\n";
105 print "W3m-control: GOTO $url\n\n";
109 sub decode_cgi_utf8 ($) {
110 # decode_form_utf8 method is needed for 5.01
113 foreach my $f ($cgi->param) {
114 $cgi->param($f, map { decode_utf8 $_ }
115 @{$cgi->param_fetch($f)});
120 sub safe_decode_utf8 ($) {
122 if (!Encode::is_utf8($octets)) {
123 return decode_utf8($octets);
130 sub decode_form_utf8 ($) {
133 foreach my $f ($form->field) {
134 my @value=map { safe_decode_utf8($_) } $form->field($f);
135 $form->field(name => $f,
143 # Check if the user is signed in. If not, redirect to the signin form and
144 # save their place to return to later.
145 sub needsignin ($$) {
149 if (! length $session->param("name") ||
150 ! userinfo_get($session->param("name"), "regdate")) {
151 $session->param(postsignin => $q->query_string);
152 cgi_signin($q, $session);
153 cgi_savesession($session);
158 sub cgi_signin ($$;$) {
161 my $returnhtml=shift;
164 eval q{use CGI::FormBuilder};
166 my $form = CGI::FormBuilder->new(
176 template => {type => 'div'},
179 my $buttons=["Login"];
181 $form->field(name => "do", type => "hidden", value => "signin",
184 decode_form_utf8($form);
185 run_hooks(formbuilder_setup => sub {
186 shift->(form => $form, cgi => $q, session => $session,
187 buttons => $buttons);
189 decode_form_utf8($form);
191 if ($form->submitted) {
196 $form=prepform($form, $buttons, $session, $q);
197 return $form->render(submit => $buttons);
200 showform($form, $buttons, $session, $q);
203 sub cgi_postsignin ($$) {
207 # Continue with whatever was being done before the signin process.
208 if (defined $session->param("postsignin")) {
209 my $postsignin=CGI->new($session->param("postsignin"));
210 $session->clear("postsignin");
211 cgi($postsignin, $session);
212 cgi_savesession($session);
216 if ($config{sslcookie} && ! $q->https()) {
217 error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
220 error(gettext("Login succeeded, but I don't remember why you were logging in, so you'll have to navigate back to whatever you were doing. (This should not normally happen. Perhaps you need to enable cookies?)"));
229 needsignin($q, $session);
232 # The session id is stored on the form and checked to
233 # guard against CSRF.
234 my $sid=$q->param('sid');
235 if (! defined $sid) {
238 elsif ($sid ne $session->id) {
239 error(gettext("Your login session has expired."));
242 eval q{use CGI::FormBuilder};
244 my $form = CGI::FormBuilder->new(
245 title => "preferences",
246 name => "preferences",
257 template => {type => 'div'},
260 [login => gettext("Login")],
261 [preferences => gettext("Preferences")],
262 [admin => gettext("Admin")]
265 my $buttons=["Save Preferences", "Logout", "Cancel"];
267 decode_form_utf8($form);
268 run_hooks(formbuilder_setup => sub {
269 shift->(form => $form, cgi => $q, session => $session,
270 buttons => $buttons);
272 decode_form_utf8($form);
274 $form->field(name => "do", type => "hidden", value => "prefs",
276 $form->field(name => "sid", type => "hidden", value => $session->id,
278 $form->field(name => "email", size => 50, fieldset => "preferences");
280 my $user_name=$session->param("name");
282 if (! $form->submitted) {
283 $form->field(name => "email", force => 1,
284 value => userinfo_get($user_name, "email"));
287 if ($form->submitted eq 'Logout') {
289 redirect($q, baseurl(undef));
292 elsif ($form->submitted eq 'Cancel') {
293 redirect($q, baseurl(undef));
296 elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
297 my $email = $form->field('email');
298 if (defined $email) {
299 userinfo_set($user_name, 'email', $email) ||
300 error("failed to set email");
303 $form->text(gettext("Preferences saved."));
306 showform($form, $buttons, $session, $q,
307 prefsurl => "", # avoid showing the preferences link
311 sub cgi_custom_failure ($$$) {
313 my $httpstatus=shift;
317 -status => $httpstatus,
322 # Internet Explod^Hrer won't show custom 404 responses
323 # unless they're >= 512 bytes
329 sub check_banned ($$) {
334 my $name=$session->param("name");
335 my $cloak=cloak($name) if defined $name;
337 grep { $name eq $_ || $cloak eq $_ } @{$config{banned_users}}) {
341 foreach my $b (@{$config{banned_users}}) {
342 if (pagespec_match("", $b,
343 ip => $session->remote_addr(),
344 name => defined $name ? $name : "")
345 || pagespec_match("", $b,
346 ip => cloak($session->remote_addr()),
347 name => defined $cloak ? $cloak : "")) {
355 cgi_savesession($session);
358 gettext("You are banned."));
362 sub cgi_getsession ($) {
365 eval q{use CGI::Session; use HTML::Entities};
367 CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
369 my $oldmask=umask(077);
371 CGI::Session->new("driver:DB_File", $q,
372 { FileName => "$config{wikistatedir}/sessions.db" })
374 if (! $session || $@) {
376 error($error." ".CGI::Session->errstr());
384 # To guard against CSRF, the user's session id (sid)
385 # can be stored on a form. This function will check
386 # (for logged in users) that the sid on the form matches
387 # the session id in the cookie.
388 sub checksessionexpiry ($$) {
392 if (defined $session->param("name")) {
393 my $sid=$q->param('sid');
394 if (! defined $sid || $sid ne $session->id || ! length $session->param("name")) {
395 error(gettext("Your login session has expired."));
400 sub cgi_savesession ($) {
403 # Force session flush with safe umask.
404 my $oldmask=umask(077);
416 $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
422 binmode(STDIN, ":utf8");
424 run_hooks(cgi => sub { shift->($q) });
427 my $do=$q->param('do');
428 if (! defined $do || ! length $do) {
429 my $error = $q->cgi_error;
431 error("Request not processed: $error");
434 error("\"do\" parameter missing");
438 # Need to lock the wiki before getting a session.
443 $session=cgi_getsession($q);
446 # Auth hooks can sign a user in.
447 if ($do ne 'signin' && ! length $session->param("name")) {
448 run_hooks(auth => sub {
449 shift->($q, $session)
451 if (length $session->param("name")) {
452 # Make sure whatever user was authed is in the
454 if (! userinfo_get($session->param("name"), "regdate")) {
455 userinfo_setall($session->param("name"), {
456 email => defined $session->param("email") ? $session->param("email") : "",
459 }) || error("failed adding user");
464 check_banned($q, $session);
466 run_hooks(sessioncgi => sub { shift->($q, $session) });
468 if ($do eq 'signin') {
469 cgi_signin($q, $session);
470 cgi_savesession($session);
472 elsif ($do eq 'prefs') {
473 cgi_prefs($q, $session);
475 elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
476 cgi_postsignin($q, $session);
479 error("unknown do parameter");
483 # Does not need to be called directly; all errors will go through here.
487 eval q{use HTML::Entities};
488 $message = encode_entities($message);
490 print "Content-type: text/html\n\n";
491 print cgitemplate(undef, gettext("Error"),
492 "<p class=\"error\">".gettext("Error").": $message</p>");