Now that we're always using HTML5, <base href> can be relative
[ikiwiki.git] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use IkiWiki::UserInfo;
9 use open qw{:utf8 :std};
10 use Encode;
11
12 sub printheader ($) {
13         my $session=shift;
14         
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));
18         }
19         else {
20                 print $session->header(-charset => 'utf-8',
21                         -cookie => $session->cookie(-httponly => 1));
22         }
23 }
24
25 sub prepform {
26         my $form=shift;
27         my $buttons=shift;
28         my $session=shift;
29         my $cgi=shift;
30
31         if (exists $hooks{formbuilder}) {
32                 run_hooks(formbuilder => sub {
33                         shift->(form => $form, cgi => $cgi, session => $session,
34                                 buttons => $buttons);
35                 });
36         }
37
38         return $form;
39 }
40
41 sub showform ($$$$;@) {
42         my $form=prepform(@_);
43         shift;
44         my $buttons=shift;
45         my $session=shift;
46         my $cgi=shift;
47
48         printheader($session);
49         print cgitemplate($cgi, $form->title,
50                 $form->render(submit => $buttons), @_);
51 }
52
53 sub cgitemplate ($$$;@) {
54         my $cgi=shift;
55         my $title=shift;
56         my $content=shift;
57         my %params=@_;
58         
59         my $template=template("page.tmpl");
60
61         my $topurl = $config{url};
62         if (defined $cgi && ! $config{w3mmode} && ! $config{reverse_proxy}) {
63                 $topurl = $cgi->url;
64         }
65
66         my $page="";
67         if (exists $params{page}) {
68                 $page=delete $params{page};
69                 $params{forcebaseurl}=urlto($page);
70         }
71         run_hooks(pagetemplate => sub {
72                 shift->(
73                         page => $page,
74                         destpage => $page,
75                         template => $template,
76                 );
77         });
78         templateactions($template, "");
79
80         my $baseurl = baseurl();
81
82         $template->param(
83                 dynamic => 1,
84                 title => $title,
85                 wikiname => $config{wikiname},
86                 content => $content,
87                 baseurl => $baseurl,
88                 html5 => $config{html5},
89                 %params,
90         );
91         
92         return $template->output;
93 }
94
95 sub redirect ($$) {
96         my $q=shift;
97         eval q{use URI};
98
99         my $topurl;
100         if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
101                 $topurl = $q->url;
102         }
103
104         my $url=URI->new(urlabs(shift, $topurl));
105         if (! $config{w3mmode}) {
106                 print $q->redirect($url);
107         }
108         else {
109                 print "Content-type: text/plain\n";
110                 print "W3m-control: GOTO $url\n\n";
111         }
112 }
113
114 sub decode_cgi_utf8 ($) {
115         # decode_form_utf8 method is needed for 5.01
116         if ($] < 5.01) {
117                 my $cgi = shift;
118                 foreach my $f ($cgi->param) {
119                         $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
120                 }
121         }
122 }
123
124 sub safe_decode_utf8 ($) {
125     my $octets = shift;
126     # call decode_utf8 on >= 5.20 only if it's not already decoded,
127     # otherwise it balks, on < 5.20, always call it
128     if ($] < 5.02 || !Encode::is_utf8($octets)) {
129         return decode_utf8($octets);
130     }
131     else {
132         return $octets;
133     }
134 }
135
136 sub decode_form_utf8 ($) {
137         if ($] >= 5.01) {
138                 my $form = shift;
139                 foreach my $f ($form->field) {
140                         my @value=map { safe_decode_utf8($_) } $form->field($f);
141                         $form->field(name  => $f,
142                                      value => \@value,
143                                      force => 1,
144                         );
145                 }
146         }
147 }
148
149 # Check if the user is signed in. If not, redirect to the signin form and
150 # save their place to return to later.
151 sub needsignin ($$) {
152         my $q=shift;
153         my $session=shift;
154
155         if (! defined $session->param("name") ||
156             ! userinfo_get($session->param("name"), "regdate")) {
157                 $session->param(postsignin => $q->query_string);
158                 cgi_signin($q, $session);
159                 cgi_savesession($session);
160                 exit;
161         }
162 }
163
164 sub cgi_signin ($$;$) {
165         my $q=shift;
166         my $session=shift;
167         my $returnhtml=shift;
168
169         decode_cgi_utf8($q);
170         eval q{use CGI::FormBuilder};
171         error($@) if $@;
172         my $form = CGI::FormBuilder->new(
173                 title => "signin",
174                 name => "signin",
175                 charset => "utf-8",
176                 method => 'POST',
177                 required => 'NONE',
178                 javascript => 0,
179                 params => $q,
180                 action => cgiurl(),
181                 header => 0,
182                 template => {type => 'div'},
183                 stylesheet => 1,
184         );
185         my $buttons=["Login"];
186         
187         $form->field(name => "do", type => "hidden", value => "signin",
188                 force => 1);
189         
190         decode_form_utf8($form);
191         run_hooks(formbuilder_setup => sub {
192                 shift->(form => $form, cgi => $q, session => $session,
193                         buttons => $buttons);
194         });
195         decode_form_utf8($form);
196
197         if ($form->submitted) {
198                 $form->validate;
199         }
200
201         if ($returnhtml) {
202                 $form=prepform($form, $buttons, $session, $q);
203                 return $form->render(submit => $buttons);
204         }
205
206         showform($form, $buttons, $session, $q);
207 }
208
209 sub cgi_postsignin ($$) {
210         my $q=shift;
211         my $session=shift;
212         
213         # Continue with whatever was being done before the signin process.
214         if (defined $session->param("postsignin")) {
215                 my $postsignin=CGI->new($session->param("postsignin"));
216                 $session->clear("postsignin");
217                 cgi($postsignin, $session);
218                 cgi_savesession($session);
219                 exit;
220         }
221         else {
222                 if ($config{sslcookie} && ! $q->https()) {
223                         error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
224                 }
225                 else {
226                         error(gettext("login failed, perhaps you need to turn on cookies?"));
227                 }
228         }
229 }
230
231 sub cgi_prefs ($$) {
232         my $q=shift;
233         my $session=shift;
234
235         needsignin($q, $session);
236         decode_cgi_utf8($q);
237         
238         # The session id is stored on the form and checked to
239         # guard against CSRF.
240         my $sid=$q->param('sid');
241         if (! defined $sid) {
242                 $q->delete_all;
243         }
244         elsif ($sid ne $session->id) {
245                 error(gettext("Your login session has expired."));
246         }
247
248         eval q{use CGI::FormBuilder};
249         error($@) if $@;
250         my $form = CGI::FormBuilder->new(
251                 title => "preferences",
252                 name => "preferences",
253                 header => 0,
254                 charset => "utf-8",
255                 method => 'POST',
256                 validate => {
257                         email => 'EMAIL',
258                 },
259                 required => 'NONE',
260                 javascript => 0,
261                 params => $q,
262                 action => cgiurl(),
263                 template => {type => 'div'},
264                 stylesheet => 1,
265                 fieldsets => [
266                         [login => gettext("Login")],
267                         [preferences => gettext("Preferences")],
268                         [admin => gettext("Admin")]
269                 ],
270         );
271         my $buttons=["Save Preferences", "Logout", "Cancel"];
272         
273         decode_form_utf8($form);
274         run_hooks(formbuilder_setup => sub {
275                 shift->(form => $form, cgi => $q, session => $session,
276                         buttons => $buttons);
277         });
278         decode_form_utf8($form);
279         
280         $form->field(name => "do", type => "hidden", value => "prefs",
281                 force => 1);
282         $form->field(name => "sid", type => "hidden", value => $session->id,
283                 force => 1);
284         $form->field(name => "email", size => 50, fieldset => "preferences");
285         
286         my $user_name=$session->param("name");
287
288         if (! $form->submitted) {
289                 $form->field(name => "email", force => 1,
290                         value => userinfo_get($user_name, "email"));
291         }
292         
293         if ($form->submitted eq 'Logout') {
294                 $session->delete();
295                 redirect($q, baseurl(undef));
296                 return;
297         }
298         elsif ($form->submitted eq 'Cancel') {
299                 redirect($q, baseurl(undef));
300                 return;
301         }
302         elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
303                 if (defined $form->field('email')) {
304                         userinfo_set($user_name, 'email', $form->field('email')) ||
305                                 error("failed to set email");
306                 }
307
308                 $form->text(gettext("Preferences saved."));
309         }
310         
311         showform($form, $buttons, $session, $q,
312                 prefsurl => "", # avoid showing the preferences link
313         );
314 }
315
316 sub cgi_custom_failure ($$$) {
317         my $q=shift;
318         my $httpstatus=shift;
319         my $message=shift;
320
321         print $q->header(
322                 -status => $httpstatus,
323                 -charset => 'utf-8',
324         );
325         print $message;
326
327         # Internet Explod^Hrer won't show custom 404 responses
328         # unless they're >= 512 bytes
329         print ' ' x 512;
330
331         exit;
332 }
333
334 sub check_banned ($$) {
335         my $q=shift;
336         my $session=shift;
337
338         my $banned=0;
339         my $name=$session->param("name");
340         if (defined $name && 
341             grep { $name eq $_ } @{$config{banned_users}}) {
342                 $banned=1;
343         }
344
345         foreach my $b (@{$config{banned_users}}) {
346                 if (pagespec_match("", $b,
347                         ip => $session->remote_addr(),
348                         name => defined $name ? $name : "",
349                 )) {
350                         $banned=1;
351                         last;
352                 }
353         }
354
355         if ($banned) {
356                 $session->delete();
357                 cgi_savesession($session);
358                 cgi_custom_failure(
359                         $q, "403 Forbidden",
360                         gettext("You are banned."));
361         }
362 }
363
364 sub cgi_getsession ($) {
365         my $q=shift;
366
367         eval q{use CGI::Session; use HTML::Entities};
368         error($@) if $@;
369         CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
370         
371         my $oldmask=umask(077);
372         my $session = eval {
373                 CGI::Session->new("driver:DB_File", $q,
374                         { FileName => "$config{wikistatedir}/sessions.db" })
375         };
376         if (! $session || $@) {
377                 my $error = $@;
378                 error($error." ".CGI::Session->errstr());
379         }
380         
381         umask($oldmask);
382
383         return $session;
384 }
385
386 # To guard against CSRF, the user's session id (sid)
387 # can be stored on a form. This function will check
388 # (for logged in users) that the sid on the form matches
389 # the session id in the cookie.
390 sub checksessionexpiry ($$) {
391         my $q=shift;
392         my $session = shift;
393
394         if (defined $session->param("name")) {
395                 my $sid=$q->param('sid');
396                 if (! defined $sid || $sid ne $session->id) {
397                         error(gettext("Your login session has expired."));
398                 }
399         }
400 }
401
402 sub cgi_savesession ($) {
403         my $session=shift;
404
405         # Force session flush with safe umask.
406         my $oldmask=umask(077);
407         $session->flush;
408         umask($oldmask);
409 }
410
411 sub cgi (;$$) {
412         my $q=shift;
413         my $session=shift;
414
415         eval q{use CGI};
416         error($@) if $@;
417         $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
418
419         if (! $q) {
420                 binmode(STDIN);
421                 $q=CGI->new;
422                 binmode(STDIN, ":utf8");
423         
424                 run_hooks(cgi => sub { shift->($q) });
425         }
426
427         my $do=$q->param('do');
428         if (! defined $do || ! length $do) {
429                 my $error = $q->cgi_error;
430                 if ($error) {
431                         error("Request not processed: $error");
432                 }
433                 else {
434                         error("\"do\" parameter missing");
435                 }
436         }
437
438         # Need to lock the wiki before getting a session.
439         lockwiki();
440         loadindex();
441         
442         if (! $session) {
443                 $session=cgi_getsession($q);
444         }
445         
446         # Auth hooks can sign a user in.
447         if ($do ne 'signin' && ! defined $session->param("name")) {
448                 run_hooks(auth => sub {
449                         shift->($q, $session)
450                 });
451                 if (defined $session->param("name")) {
452                         # Make sure whatever user was authed is in the
453                         # userinfo db.
454                         if (! userinfo_get($session->param("name"), "regdate")) {
455                                 userinfo_setall($session->param("name"), {
456                                         email => defined $session->param("email") ? $session->param("email") : "",
457                                         password => "",
458                                         regdate => time,
459                                 }) || error("failed adding user");
460                         }
461                 }
462         }
463         
464         check_banned($q, $session);
465         
466         run_hooks(sessioncgi => sub { shift->($q, $session) });
467
468         if ($do eq 'signin') {
469                 cgi_signin($q, $session);
470                 cgi_savesession($session);
471         }
472         elsif ($do eq 'prefs') {
473                 cgi_prefs($q, $session);
474         }
475         elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
476                 cgi_postsignin($q, $session);
477         }
478         else {
479                 error("unknown do parameter");
480         }
481 }
482
483 # Does not need to be called directly; all errors will go through here.
484 sub cgierror ($) {
485         my $message=shift;
486
487         print "Content-type: text/html\n\n";
488         print cgitemplate(undef, gettext("Error"),
489                 "<p class=\"error\">".gettext("Error").": $message</p>");
490         die $@;
491 }
492
493 1