]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/CGI.pm
renamed the renamepage hook to renamelink, added rename hook
[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         # 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);
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;
299 # To guard against CSRF, the user's session id (sid)
300 # can be stored on a form. This function will check
301 # (for logged in users) that the sid on the form matches
302 # the session id in the cookie.
303 sub checksessionexpiry ($$) {
304         my $q=shift;
305         my $session = shift;
307         if (defined $session->param("name")) {
308                 my $sid=$q->param('sid');
309                 if (! defined $sid || $sid ne $session->id) {
310                         error(gettext("Your login session has expired."));
311                 }
312         }
315 sub cgi_savesession ($) {
316         my $session=shift;
318         # Force session flush with safe umask.
319         my $oldmask=umask(077);
320         $session->flush;
321         umask($oldmask);
324 sub cgi (;$$) {
325         my $q=shift;
326         my $session=shift;
328         eval q{use CGI};
329         error($@) if $@;
330         $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
332         if (! $q) {
333                 binmode(STDIN);
334                 $q=CGI->new;
335                 binmode(STDIN, ":utf8");
336         
337                 run_hooks(cgi => sub { shift->($q) });
338         }
340         my $do=$q->param('do');
341         if (! defined $do || ! length $do) {
342                 my $error = $q->cgi_error;
343                 if ($error) {
344                         error("Request not processed: $error");
345                 }
346                 else {
347                         error("\"do\" parameter missing");
348                 }
349         }
350         
351         # Need to lock the wiki before getting a session.
352         lockwiki();
353         loadindex();
354         
355         if (! $session) {
356                 $session=cgi_getsession($q);
357         }
358         
359         # Auth hooks can sign a user in.
360         if ($do ne 'signin' && ! defined $session->param("name")) {
361                 run_hooks(auth => sub {
362                         shift->($q, $session)
363                 });
364                 if (defined $session->param("name")) {
365                         # Make sure whatever user was authed is in the
366                         # userinfo db.
367                         if (! userinfo_get($session->param("name"), "regdate")) {
368                                 userinfo_setall($session->param("name"), {
369                                         email => "",
370                                         password => "",
371                                         regdate => time,
372                                 }) || error("failed adding user");
373                         }
374                 }
375         }
376         
377         check_banned($q, $session);
378         
379         run_hooks(sessioncgi => sub { shift->($q, $session) });
381         if ($do eq 'signin') {
382                 cgi_signin($q, $session);
383                 cgi_savesession($session);
384         }
385         elsif ($do eq 'prefs') {
386                 cgi_prefs($q, $session);
387         }
388         elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
389                 cgi_postsignin($q, $session);
390         }
391         else {
392                 error("unknown do parameter");
393         }
396 # Does not need to be called directly; all errors will go through here.
397 sub cgierror ($) {
398         my $message=shift;
400         print "Content-type: text/html\n\n";
401         print misctemplate(gettext("Error"),
402                 "<p class=\"error\">".gettext("Error").": $message</p>");
403         die $@;