]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/CGI.pm
updated de.po
[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);
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         }
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         if (! $form->submitted) {
207                 $form->field(name => "email", force => 1,
208                         value => userinfo_get($user_name, "email"));
209         }
210         
211         if ($form->submitted eq 'Logout') {
212                 $session->delete();
213                 redirect($q, $config{url});
214                 return;
215         }
216         elsif ($form->submitted eq 'Cancel') {
217                 redirect($q, $config{url});
218                 return;
219         }
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");
224                 }
226                 $form->text(gettext("Preferences saved."));
227         }
228         
229         showform($form, $buttons, $session, $q);
232 sub cgi_custom_failure ($$) {
233         my $header=shift;
234         my $message=shift;
236         print $header;
237         print $message;
239         # Internet Explod^Hrer won't show custom 404 responses
240         # unless they're >= 512 bytes
241         print ' ' x 512;
243         exit;
246 sub check_banned ($$) {
247         my $q=shift;
248         my $session=shift;
250         my $name=$session->param("name");
251         if (defined $name) {
252                 if (grep { $name eq $_ } @{$config{banned_users}}) {
253                         $session->delete();
254                         cgi_savesession($session);
255                         cgi_custom_failure(
256                                 $q->header(-status => "403 Forbidden"),
257                                 gettext("You are banned."));
258                 }
259         }
262 sub cgi_getsession ($) {
263         my $q=shift;
265         eval q{use CGI::Session; use HTML::Entities};
266         error($@) if $@;
267         CGI::Session->name("ikiwiki_session_".encode_entities($config{wikiname}));
268         
269         my $oldmask=umask(077);
270         my $session = eval {
271                 CGI::Session->new("driver:DB_File", $q,
272                         { FileName => "$config{wikistatedir}/sessions.db" })
273         };
274         if (! $session || $@) {
275                 error($@." ".CGI::Session->errstr());
276         }
277         
278         umask($oldmask);
280         return $session;
283 # To guard against CSRF, the user's session id (sid)
284 # can be stored on a form. This function will check
285 # (for logged in users) that the sid on the form matches
286 # the session id in the cookie.
287 sub checksessionexpiry ($$) {
288         my $q=shift;
289         my $session = shift;
291         if (defined $session->param("name")) {
292                 my $sid=$q->param('sid');
293                 if (! defined $sid || $sid ne $session->id) {
294                         error(gettext("Your login session has expired."));
295                 }
296         }
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);
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         }
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         }
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 $@;