]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/CGI.pm
po: memoized istranslatable, eventually
[git.ikiwiki.info.git] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use IkiWiki::UserInfo;
9 use open qw{:utf8 :std};
10 use Encode;
12 sub printheader ($) { #{{{
13         my $session=shift;
14         
15         if ($config{sslcookie}) {
16                 print $session->header(-charset => 'utf-8',
17                         -cookie => $session->cookie(-httponly => 1, -secure => 1));
18         } else {
19                 print $session->header(-charset => 'utf-8',
20                         -cookie => $session->cookie(-httponly => 1));
21         }
22 } #}}}
24 sub showform ($$$$;@) { #{{{
25         my $form=shift;
26         my $buttons=shift;
27         my $session=shift;
28         my $cgi=shift;
30         if (exists $hooks{formbuilder}) {
31                 run_hooks(formbuilder => sub {
32                         shift->(form => $form, cgi => $cgi, session => $session,
33                                 buttons => $buttons);
34                 });
35         }
37         printheader($session);
38         print misctemplate($form->title, $form->render(submit => $buttons), @_);
39 }
41 sub redirect ($$) { #{{{
42         my $q=shift;
43         my $url=shift;
44         if (! $config{w3mmode}) {
45                 print $q->redirect($url);
46         }
47         else {
48                 print "Content-type: text/plain\n";
49                 print "W3m-control: GOTO $url\n\n";
50         }
51 } #}}}
53 sub decode_cgi_utf8 ($) { #{{{
54         # decode_form_utf8 method is needed for 5.10
55         if ($] < 5.01) {
56                 my $cgi = shift;
57                 foreach my $f ($cgi->param) {
58                         $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
59                 }
60         }
61 } #}}}
63 sub decode_form_utf8 ($) { #{{{
64         if ($] >= 5.01) {
65                 my $form = shift;
66                 foreach my $f ($form->field) {
67                         $form->field(name  => $f,
68                                      value => decode_utf8($form->field($f)),
69                                      force => 1,
70                         );
71                 }
72         }
73 } #}}}
75 # Check if the user is signed in. If not, redirect to the signin form and
76 # save their place to return to later.
77 sub needsignin ($$) { #{{{
78         my $q=shift;
79         my $session=shift;
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);
86                 exit;
87         }
88 } #}}}  
90 sub cgi_signin ($$) { #{{{
91         my $q=shift;
92         my $session=shift;
94         decode_cgi_utf8($q);
95         eval q{use CGI::FormBuilder};
96         error($@) if $@;
97         my $form = CGI::FormBuilder->new(
98                 title => "signin",
99                 name => "signin",
100                 charset => "utf-8",
101                 method => 'POST',
102                 required => 'NONE',
103                 javascript => 0,
104                 params => $q,
105                 action => $config{cgiurl},
106                 header => 0,
107                 template => {type => 'div'},
108                 stylesheet => baseurl()."style.css",
109         );
110         my $buttons=["Login"];
111         
112         if ($q->param("do") ne "signin" && !$form->submitted) {
113                 $form->text(gettext("You need to log in first."));
114         }
115         $form->field(name => "do", type => "hidden", value => "signin",
116                 force => 1);
117         
118         decode_form_utf8($form);
119         run_hooks(formbuilder_setup => sub {
120                 shift->(form => $form, cgi => $q, session => $session,
121                         buttons => $buttons);
122         });
123         decode_form_utf8($form);
125         if ($form->submitted) {
126                 $form->validate;
127         }
129         showform($form, $buttons, $session, $q);
130 } #}}}
132 sub cgi_postsignin ($$) { #{{{
133         my $q=shift;
134         my $session=shift;
135         
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);
142                 exit;
143         }
144         else {
145                 error(gettext("login failed, perhaps you need to turn on cookies?"));
146         }
147 } #}}}
149 sub cgi_prefs ($$) { #{{{
150         my $q=shift;
151         my $session=shift;
153         needsignin($q, $session);
154         decode_cgi_utf8($q);
155         
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) {
160                 $q->delete_all;
161         }
162         elsif ($sid ne $session->id) {
163                 error(gettext("Your login session has expired."));
164         }
166         eval q{use CGI::FormBuilder};
167         error($@) if $@;
168         my $form = CGI::FormBuilder->new(
169                 title => "preferences",
170                 name => "preferences",
171                 header => 0,
172                 charset => "utf-8",
173                 method => 'POST',
174                 validate => {
175                         email => 'EMAIL',
176                 },
177                 required => 'NONE',
178                 javascript => 0,
179                 params => $q,
180                 action => $config{cgiurl},
181                 template => {type => 'div'},
182                 stylesheet => baseurl()."style.css",
183                 fieldsets => [
184                         [login => gettext("Login")],
185                         [preferences => gettext("Preferences")],
186                         [admin => gettext("Admin")]
187                 ],
188         );
189         my $buttons=["Save Preferences", "Logout", "Cancel"];
190         
191         decode_form_utf8($form);
192         run_hooks(formbuilder_setup => sub {
193                 shift->(form => $form, cgi => $q, session => $session,
194                         buttons => $buttons);
195         });
196         decode_form_utf8($form);
197         
198         $form->field(name => "do", type => "hidden", value => "prefs",
199                 force => 1);
200         $form->field(name => "sid", type => "hidden", value => $session->id,
201                 force => 1);
202         $form->field(name => "email", size => 50, fieldset => "preferences");
203         
204         my $user_name=$session->param("name");
206         # XXX deprecated, should be removed eventually
207         $form->field(name => "banned_users", size => 50, fieldset => "admin");
208         if (! is_admin($user_name)) {
209                 $form->field(name => "banned_users", type => "hidden");
210         }
211         if (! $form->submitted) {
212                 $form->field(name => "email", force => 1,
213                         value => userinfo_get($user_name, "email"));
214                 if (is_admin($user_name)) {
215                         my $value=join(" ", get_banned_users());
216                         if (length $value) {
217                                 $form->field(name => "banned_users", force => 1,
218                                         value => join(" ", get_banned_users()),
219                                         comment => "deprecated; please move to banned_users in setup file");
220                         }
221                         else {
222                                 $form->field(name => "banned_users", type => "hidden");
223                         }
224                 }
225         }
226         
227         if ($form->submitted eq 'Logout') {
228                 $session->delete();
229                 redirect($q, $config{url});
230                 return;
231         }
232         elsif ($form->submitted eq 'Cancel') {
233                 redirect($q, $config{url});
234                 return;
235         }
236         elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
237                 if (defined $form->field('email')) {
238                         userinfo_set($user_name, 'email', $form->field('email')) ||
239                                 error("failed to set email");
240                 }
242                 # XXX deprecated, should be removed eventually
243                 if (is_admin($user_name)) {
244                         set_banned_users(grep { ! is_admin($_) }
245                                         split(' ',
246                                                 $form->field("banned_users"))) ||
247                                 error("failed saving changes");
248                         if (! length $form->field("banned_users")) {
249                                 $form->field(name => "banned_users", type => "hidden");
250                         }
251                 }
253                 $form->text(gettext("Preferences saved."));
254         }
255         
256         showform($form, $buttons, $session, $q);
257 } #}}}
259 sub check_banned ($$) { #{{{
260         my $q=shift;
261         my $session=shift;
263         my $name=$session->param("name");
264         if (defined $name) {
265                 # XXX banned in userinfo is deprecated, should be removed
266                 # eventually, and only banned_users be checked.
267                 if (userinfo_get($session->param("name"), "banned") ||
268                     grep { $name eq $_ } @{$config{banned_users}}) {
269                         print $q->header(-status => "403 Forbidden");
270                         $session->delete();
271                         print gettext("You are banned.");
272                         cgi_savesession($session);
273                         exit;
274                 }
275         }
278 sub cgi_getsession ($) { #{{{
279         my $q=shift;
281         eval q{use CGI::Session; use HTML::Entities};
282         error($@) if $@;
283         CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
284         
285         my $oldmask=umask(077);
286         my $session = eval {
287                 CGI::Session->new("driver:DB_File", $q,
288                         { FileName => "$config{wikistatedir}/sessions.db" })
289         };
290         if (! $session || $@) {
291                 error($@." ".CGI::Session->errstr());
292         }
293         
294         umask($oldmask);
296         return $session;
297 } #}}}
299 sub cgi_savesession ($) { #{{{
300         my $session=shift;
302         # Force session flush with safe umask.
303         my $oldmask=umask(077);
304         $session->flush;
305         umask($oldmask);
306 } #}}}
308 sub cgi (;$$) { #{{{
309         my $q=shift;
310         my $session=shift;
312         eval q{use CGI};
313         error($@) if $@;
314         $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
316         if (! $q) {
317                 binmode(STDIN);
318                 $q=CGI->new;
319                 binmode(STDIN, ":utf8");
320         
321                 run_hooks(cgi => sub { shift->($q) });
322         }
324         my $do=$q->param('do');
325         if (! defined $do || ! length $do) {
326                 my $error = $q->cgi_error;
327                 if ($error) {
328                         error("Request not processed: $error");
329                 }
330                 else {
331                         error("\"do\" parameter missing");
332                 }
333         }
334         
335         # Need to lock the wiki before getting a session.
336         lockwiki();
337         loadindex();
338         
339         if (! $session) {
340                 $session=cgi_getsession($q);
341         }
342         
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)
347                 });
348                 if (defined $session->param("name")) {
349                         # Make sure whatever user was authed is in the
350                         # userinfo db.
351                         if (! userinfo_get($session->param("name"), "regdate")) {
352                                 userinfo_setall($session->param("name"), {
353                                         email => "",
354                                         password => "",
355                                         regdate => time,
356                                 }) || error("failed adding user");
357                         }
358                 }
359         }
360         
361         check_banned($q, $session);
362         
363         run_hooks(sessioncgi => sub { shift->($q, $session) });
365         if ($do eq 'signin') {
366                 cgi_signin($q, $session);
367                 cgi_savesession($session);
368         }
369         elsif ($do eq 'prefs') {
370                 cgi_prefs($q, $session);
371         }
372         elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
373                 cgi_postsignin($q, $session);
374         }
375         else {
376                 error("unknown do parameter");
377         }
378 } #}}}
380 # Does not need to be called directly; all errors will go through here.
381 sub cgierror ($) { #{{{
382         my $message=shift;
384         print "Content-type: text/html\n\n";
385         print misctemplate(gettext("Error"),
386                 "<p class=\"error\">".gettext("Error").": $message</p>");
387         die $@;
388 } #}}}