explanation
[ikiwiki.git] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use IkiWiki::UserInfo;
7 use open qw{:utf8 :std};
8 use Encode;
9
10 package IkiWiki;
11
12 sub printheader ($) { #{{{
13         my $session=shift;
14         
15         if ($config{sslcookie}) {
16                 print $session->header(-charset => 'utf-8',
17                         -cookie => $session->cookie(-secure => 1));
18         } else {
19                 print $session->header(-charset => 'utf-8');
20         }
21
22 } #}}}
23
24 sub redirect ($$) { #{{{
25         my $q=shift;
26         my $url=shift;
27         if (! $config{w3mmode}) {
28                 print $q->redirect($url);
29         }
30         else {
31                 print "Content-type: text/plain\n";
32                 print "W3m-control: GOTO $url\n\n";
33         }
34 } #}}}
35
36 sub page_locked ($$;$) { #{{{
37         my $page=shift;
38         my $session=shift;
39         my $nonfatal=shift;
40         
41         my $user=$session->param("name");
42         return if defined $user && is_admin($user);
43
44         foreach my $admin (@{$config{adminuser}}) {
45                 my $locked_pages=userinfo_get($admin, "locked_pages");
46                 if (pagespec_match($page, userinfo_get($admin, "locked_pages"))) {
47                         return 1 if $nonfatal;
48                         error(htmllink("", "", $page, 1)." is locked by ".
49                               htmllink("", "", $admin, 1)." and cannot be edited.");
50                 }
51         }
52
53         return 0;
54 } #}}}
55
56 sub decode_form_utf8 ($) { #{{{
57         my $form = shift;
58         foreach my $f ($form->field) {
59                 next if Encode::is_utf8(scalar $form->field($f));
60                 $form->field(name  => $f,
61                              value => decode_utf8($form->field($f)),
62                              force => 1,
63                             );
64         }
65 } #}}}
66
67 sub cgi_recentchanges ($) { #{{{
68         my $q=shift;
69         
70         unlockwiki();
71
72         # Optimisation: building recentchanges means calculating lots of
73         # links. Memoizing htmllink speeds it up a lot (can't be memoized
74         # during page builds as the return values may change, but they
75         # won't here.)
76         eval q{use Memoize};
77         error($@) if $@;
78         memoize("htmllink");
79
80         eval q{use Time::Duration};
81         error($@) if $@;
82         eval q{use CGI 'escapeHTML'};
83         error($@) if $@;
84
85         my $changelog=[rcs_recentchanges(100)];
86         foreach my $change (@$changelog) {
87                 $change->{when} = concise(ago($change->{when}));
88
89                 if ($change->{user} =~ m!^https?://! &&
90                     eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
91                         # Munge user-urls, as used by eg, OpenID.
92                         my $oid=Net::OpenID::VerifiedIdentity->new(identity => $change->{user});
93                         $change->{user} = "<a href=\"".$change->{user}."\">".escapeHTML($oid->display)."</a>";
94                 }
95                 else {
96                         $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
97                 }
98
99                 my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
100                 delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
101                 $change->{pages} = [
102                         map {
103                                 $_->{link} = htmllink("", "", $_->{page}, 1);
104                                 $_;
105                         } @{$change->{pages}}
106                 ];
107                 push @{$change->{pages}}, { link => '...' } if $is_excess;
108         }
109
110         my $template=template("recentchanges.tmpl"); 
111         $template->param(
112                 title => "RecentChanges",
113                 indexlink => indexlink(),
114                 wikiname => $config{wikiname},
115                 changelog => $changelog,
116                 baseurl => baseurl(),
117         );
118         run_hooks(pagetemplate => sub {
119                 shift->(page => "", destpage => "", template => $template);
120         });
121         print $q->header(-charset => 'utf-8'), $template->output;
122 } #}}}
123
124 sub cgi_signin ($$) { #{{{
125         my $q=shift;
126         my $session=shift;
127
128         eval q{use CGI::FormBuilder};
129         error($@) if $@;
130         my $form = CGI::FormBuilder->new(
131                 title => "signin",
132                 fields => [qw(do title page subpage from name password openid_url)],
133                 header => 1,
134                 charset => "utf-8",
135                 method => 'POST',
136                 validate => {
137                         confirm_password => {
138                                 perl => q{eq $form->field("password")},
139                         },
140                         email => 'EMAIL',
141                 },
142                 required => 'NONE',
143                 javascript => 0,
144                 params => $q,
145                 action => $config{cgiurl},
146                 header => 0,
147                 template => (-e "$config{templatedir}/signin.tmpl" ?
148                              {template_params("signin.tmpl")} : ""),
149                 stylesheet => baseurl()."style.css",
150         );
151                 
152         decode_form_utf8($form);
153         
154         $form->field(name => "name", required => 0);
155         $form->field(name => "do", type => "hidden");
156         $form->field(name => "page", type => "hidden");
157         $form->field(name => "title", type => "hidden");
158         $form->field(name => "from", type => "hidden");
159         $form->field(name => "subpage", type => "hidden");
160         $form->field(name => "password", type => "password", required => 0);
161         if ($config{openid}) {
162                 $form->field(name => "openid_url", label => "OpenID",
163                         comment => htmllink("", "", "OpenID", 1, 0, "What's this?"));
164         }
165         else {
166                 $form->field(name => "openid_url", type => "hidden");
167         }
168         if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
169                 $form->title("register");
170                 $form->text("");
171                 $form->fields(qw(do title page subpage from name password confirm_password email));
172                 $form->field(name => "confirm_password", type => "password");
173                 $form->field(name => "email", type => "text");
174                 $form->field(name => "openid_url", type => "hidden");
175         }
176         if ($q->param("do") ne "signin" && !$form->submitted) {
177                 $form->text("You need to log in first.");
178         }
179         
180         if ($form->submitted) {
181                 my $submittype=$form->submitted;
182                 # OpenID login uses the Login button, but validates
183                 # differently.
184                 if ($submittype eq "Login" && $config{openid} && 
185                     length $form->field("openid_url")) {
186                         $submittype="OpenID";
187                         
188                         $form->field(
189                                 name => "openid_url",
190                                 validate => sub {
191                                         # FIXME: ugh
192                                         IkiWiki::Plugin::openid::validate($q, $session, $form, shift);
193                                 },
194                         );
195                 }
196
197                 # Set required fields based on how form was submitted.
198                 my %required=(
199                         "Login" => [qw(name password)],
200                         "Register" => [],
201                         "Create Account" => [qw(name password confirm_password email)],
202                         "Mail Password" => [qw(name)],
203                         "OpenID" => [qw(openid_url)],
204                 );
205                 foreach my $opt (@{$required{$submittype}}) {
206                         $form->field(name => $opt, required => 1);
207                 }
208         
209                 # Validate password differently depending on how
210                 # form was submitted.
211                 if ($submittype eq 'Login') {
212                         $form->field(
213                                 name => "password",
214                                 validate => sub {
215                                         length $form->field("name") &&
216                                         shift eq userinfo_get($form->field("name"), 'password');
217                                 },
218                         );
219                         $form->field(name => "name", validate => '/^\w+$/');
220                 }
221                 elsif ($submittype ne 'OpenID') {
222                         $form->field(name => "password", validate => 'VALUE');
223                 }
224                 # And make sure the entered name exists when logging
225                 # in or sending email, and does not when registering.
226                 if ($submittype eq 'Create Account' ||
227                     $submittype eq 'Register') {
228                         $form->field(
229                                 name => "name",
230                                 validate => sub {
231                                         my $name=shift;
232                                         length $name &&
233                                         $name=~/$config{wiki_file_regexp}/ &&
234                                         ! userinfo_get($name, "regdate");
235                                 },
236                         );
237                 }
238                 elsif ($submittype ne 'OpenID') {
239                         $form->field(
240                                 name => "name",
241                                 validate => sub {
242                                         my $name=shift;
243                                         length $name &&
244                                         userinfo_get($name, "regdate");
245                                 },
246                         );
247                 }
248         }
249         else {
250                 # First time settings.
251                 $form->field(name => "name", comment => "use FirstnameLastName");
252                 if ($session->param("name")) {
253                         $form->field(name => "name", value => $session->param("name"));
254                 }
255         }
256
257         if ($form->submitted && $form->validate) {
258                 if ($form->submitted eq 'Login') {
259                         $session->param("name", $form->field("name"));
260                         if (defined $form->field("do") && 
261                             $form->field("do") ne 'signin') {
262                                 redirect($q, cgiurl(
263                                         do => $form->field("do"),
264                                         page => $form->field("page"),
265                                         title => $form->field("title"),
266                                         from => $form->field("from"),
267                                         subpage => $form->field("subpage"),
268                                 ));
269                         }
270                         else {
271                                 redirect($q, $config{url});
272                         }
273                 }
274                 elsif ($form->submitted eq 'Create Account') {
275                         my $user_name=$form->field('name');
276                         if (userinfo_setall($user_name, {
277                                            'email' => $form->field('email'),
278                                            'password' => $form->field('password'),
279                                            'regdate' => time
280                                          })) {
281                                 $form->field(name => "confirm_password", type => "hidden");
282                                 $form->field(name => "email", type => "hidden");
283                                 $form->text("Account creation successful. Now you can Login.");
284                                 printheader($session);
285                                 print misctemplate($form->title, $form->render(submit => ["Login"]));
286                         }
287                         else {
288                                 error("Error creating account.");
289                         }
290                 }
291                 elsif ($form->submitted eq 'Mail Password') {
292                         my $user_name=$form->field("name");
293                         my $template=template("passwordmail.tmpl");
294                         $template->param(
295                                 user_name => $user_name,
296                                 user_password => userinfo_get($user_name, "password"),
297                                 wikiurl => $config{url},
298                                 wikiname => $config{wikiname},
299                                 REMOTE_ADDR => $ENV{REMOTE_ADDR},
300                         );
301                         
302                         eval q{use Mail::Sendmail};
303                         error($@) if $@;
304                         sendmail(
305                                 To => userinfo_get($user_name, "email"),
306                                 From => "$config{wikiname} admin <$config{adminemail}>",
307                                 Subject => "$config{wikiname} information",
308                                 Message => $template->output,
309                         ) or error("Failed to send mail");
310                         
311                         $form->text("Your password has been emailed to you.");
312                         $form->field(name => "name", required => 0);
313                         printheader($session);
314                         print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
315                 }
316                 elsif ($form->submitted eq "Register") {
317                         printheader($session);
318                         print misctemplate($form->title, $form->render(submit => ["Create Account"]));
319                 }
320         }
321         elsif ($form->submitted eq "Create Account") {
322                 printheader($session);
323                 print misctemplate($form->title, $form->render(submit => ["Create Account"]));
324         }
325         else {
326                 printheader($session);
327                 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
328         }
329 } #}}}
330
331 sub cgi_prefs ($$) { #{{{
332         my $q=shift;
333         my $session=shift;
334
335         eval q{use CGI::FormBuilder};
336         error($@) if $@;
337         my $form = CGI::FormBuilder->new(
338                 title => "preferences",
339                 fields => [qw(do name password confirm_password email 
340                               subscriptions locked_pages)],
341                 header => 0,
342                 charset => "utf-8",
343                 method => 'POST',
344                 validate => {
345                         confirm_password => {
346                                 perl => q{eq $form->field("password")},
347                         },
348                         email => 'EMAIL',
349                 },
350                 required => 'NONE',
351                 javascript => 0,
352                 params => $q,
353                 action => $config{cgiurl},
354                 template => (-e "$config{templatedir}/prefs.tmpl" ?
355                              {template_params("prefs.tmpl")} : ""),
356                 stylesheet => baseurl()."style.css",
357         );
358         my @buttons=("Save Preferences", "Logout", "Cancel");
359         
360         my $user_name=$session->param("name");
361         $form->field(name => "do", type => "hidden");
362         $form->field(name => "name", disabled => 1,
363                 value => $user_name, force => 1);
364         $form->field(name => "password", type => "password");
365         $form->field(name => "confirm_password", type => "password");
366         $form->field(name => "subscriptions", size => 50,
367                 comment => "(".htmllink("", "", "PageSpec", 1).")");
368         $form->field(name => "locked_pages", size => 50,
369                 comment => "(".htmllink("", "", "PageSpec", 1).")");
370         $form->field(name => "banned_users", size => 50);
371         
372         if (! is_admin($user_name)) {
373                 $form->field(name => "locked_pages", type => "hidden");
374                 $form->field(name => "banned_users", type => "hidden");
375         }
376
377         if ($config{httpauth}) {
378                 $form->field(name => "password", type => "hidden");
379                 $form->field(name => "confirm_password", type => "hidden");
380         }
381         
382         if (! $form->submitted) {
383                 $form->field(name => "email", force => 1,
384                         value => userinfo_get($user_name, "email"));
385                 $form->field(name => "subscriptions", force => 1,
386                         value => userinfo_get($user_name, "subscriptions"));
387                 $form->field(name => "locked_pages", force => 1,
388                         value => userinfo_get($user_name, "locked_pages"));
389                 if (is_admin($user_name)) {
390                         $form->field(name => "banned_users", force => 1,
391                                 value => join(" ", get_banned_users()));
392                 }
393         }
394         
395         decode_form_utf8($form);
396         
397         if ($form->submitted eq 'Logout') {
398                 $session->delete();
399                 redirect($q, $config{url});
400                 return;
401         }
402         elsif ($form->submitted eq 'Cancel') {
403                 redirect($q, $config{url});
404                 return;
405         }
406         elsif ($form->submitted eq "Save Preferences" && $form->validate) {
407                 foreach my $field (qw(password email subscriptions locked_pages)) {
408                         if (length $form->field($field)) {
409                                 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
410                         }
411                 }
412                 if (is_admin($user_name)) {
413                         set_banned_users(grep { ! is_admin($_) }
414                                         split(' ', $form->field("banned_users")));
415                 }
416                 $form->text("Preferences saved.");
417         }
418         
419         printheader($session);
420         print misctemplate($form->title, $form->render(submit => \@buttons));
421 } #}}}
422
423 sub cgi_editpage ($$) { #{{{
424         my $q=shift;
425         my $session=shift;
426
427         my @fields=qw(do rcsinfo subpage from page type editcontent comments);
428         my @buttons=("Save Page", "Preview", "Cancel");
429         
430         eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
431         error($@) if $@;
432         my $renderer=CGI::FormBuilder::Template::HTML->new(
433                 fields => \@fields,
434                 template_params("editpage.tmpl"),
435         );
436         run_hooks(pagetemplate => sub {
437                 shift->(page => "", destpage => "", template => $renderer->engine);
438         });
439         my $form = CGI::FormBuilder->new(
440                 fields => \@fields,
441                 header => 1,
442                 charset => "utf-8",
443                 method => 'POST',
444                 validate => {
445                         editcontent => '/.+/',
446                 },
447                 required => [qw{editcontent}],
448                 javascript => 0,
449                 params => $q,
450                 action => $config{cgiurl},
451                 table => 0,
452                 template => $renderer,
453         );
454         
455         decode_form_utf8($form);
456         
457         # This untaint is safe because titlepage removes any problematic
458         # characters.
459         my ($page)=$form->field('page');
460         $page=titlepage(possibly_foolish_untaint($page));
461         if (! defined $page || ! length $page ||
462             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
463                 error("bad page name");
464         }
465         
466         my $from;
467         if (defined $form->field('from')) {
468                 ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
469         }
470         
471         my $file;
472         my $type;
473         if (exists $pagesources{$page}) {
474                 $file=$pagesources{$page};
475                 $type=pagetype($file);
476         }
477         else {
478                 $type=$form->param('type');
479                 if (defined $type && length $type && $hooks{htmlize}{$type}) {
480                         $type=possibly_foolish_untaint($type);
481                 }
482                 elsif (defined $from) {
483                         # favor the type of linking page
484                         $type=pagetype($pagesources{$from});
485                 }
486                 $type=$config{default_pageext} unless defined $type;
487                 $file=$page.".".$type;
488         }
489
490         my $newfile=0;
491         if (! -e "$config{srcdir}/$file") {
492                 $newfile=1;
493         }
494
495         $form->field(name => "do", type => 'hidden');
496         $form->field(name => "from", type => 'hidden');
497         $form->field(name => "rcsinfo", type => 'hidden');
498         $form->field(name => "subpage", type => 'hidden');
499         $form->field(name => "page", value => $page, force => 1);
500         $form->field(name => "type", value => $type, force => 1);
501         $form->field(name => "comments", type => "text", size => 80);
502         $form->field(name => "editcontent", type => "textarea", rows => 20,
503                 cols => 80);
504         $form->tmpl_param("can_commit", $config{rcs});
505         $form->tmpl_param("indexlink", indexlink());
506         $form->tmpl_param("helponformattinglink",
507                 htmllink("", "", "HelpOnFormatting", 1));
508         $form->tmpl_param("baseurl", baseurl());
509         if (! $form->submitted) {
510                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
511                         force => 1);
512         }
513         
514         if ($form->submitted eq "Cancel") {
515                 if ($newfile && defined $from) {
516                         redirect($q, "$config{url}/".htmlpage($from));
517                 }
518                 elsif ($newfile) {
519                         redirect($q, $config{url});
520                 }
521                 else {
522                         redirect($q, "$config{url}/".htmlpage($page));
523                 }
524                 return;
525         }
526         elsif ($form->submitted eq "Preview") {
527                 my $content=$form->field('editcontent');
528                 my $comments=$form->field('comments');
529                 $form->field(name => "editcontent",
530                                 value => $content, force => 1);
531                 $form->field(name => "comments",
532                                 value => $comments, force => 1);
533                 $config{rss}=$config{atom}=0; # avoid preview writing a feed!
534                 $form->tmpl_param("page_preview",
535                         htmlize($page, $type,
536                         linkify($page, "",
537                         preprocess($page, $page,
538                         filter($page, $content)))));
539         }
540         else {
541                 $form->tmpl_param("page_preview", "");
542         }
543         $form->tmpl_param("page_conflict", "");
544         
545         if (! $form->submitted || $form->submitted eq "Preview" || 
546             ! $form->validate) {
547                 if ($form->field("do") eq "create") {
548                         my @page_locs;
549                         my $best_loc;
550                         if (! defined $from || ! length $from ||
551                             $from ne $form->field('from') ||
552                             $from=~/$config{wiki_file_prune_regexp}/ ||
553                             $from=~/^\// ||
554                             $form->submitted eq "Preview") {
555                                 @page_locs=$best_loc=$page;
556                         }
557                         else {
558                                 my $dir=$from."/";
559                                 $dir=~s![^/]+/+$!!;
560                                 
561                                 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
562                                     $page eq 'discussion') {
563                                         $best_loc="$from/$page";
564                                 }
565                                 else {
566                                         $best_loc=$dir.$page;
567                                 }
568                                 
569                                 push @page_locs, $dir.$page;
570                                 push @page_locs, "$from/$page";
571                                 while (length $dir) {
572                                         $dir=~s![^/]+/+$!!;
573                                         push @page_locs, $dir.$page;
574                                 }
575                         }
576
577                         @page_locs = grep {
578                                 ! exists $pagecase{lc $_} &&
579                                 ! page_locked($_, $session, 1)
580                         } @page_locs;
581                         
582                         if (! @page_locs) {
583                                 # hmm, someone else made the page in the
584                                 # meantime?
585                                 redirect($q, "$config{url}/".htmlpage($page));
586                                 return;
587                         }
588                         
589                         my @page_types;
590                         if (exists $hooks{htmlize}) {
591                                 @page_types=keys %{$hooks{htmlize}};
592                         }
593                         
594                         $form->tmpl_param("page_select", 1);
595                         $form->field(name => "page", type => 'select',
596                                 options => \@page_locs, value => $best_loc);
597                         $form->field(name => "type", type => 'select',
598                                 options => \@page_types);
599                         $form->title("creating ".pagetitle($page));
600                 }
601                 elsif ($form->field("do") eq "edit") {
602                         page_locked($page, $session);
603                         if (! defined $form->field('editcontent') || 
604                             ! length $form->field('editcontent')) {
605                                 my $content="";
606                                 if (exists $pagesources{$page}) {
607                                         $content=readfile(srcfile($pagesources{$page}));
608                                         $content=~s/\n/\r\n/g;
609                                 }
610                                 $form->field(name => "editcontent", value => $content,
611                                         force => 1);
612                         }
613                         $form->tmpl_param("page_select", 0);
614                         $form->field(name => "page", type => 'hidden');
615                         $form->field(name => "type", type => 'hidden');
616                         $form->title("editing ".pagetitle($page));
617                 }
618                 
619                 print $form->render(submit => \@buttons);
620         }
621         else {
622                 # save page
623                 page_locked($page, $session);
624                 
625                 my $content=$form->field('editcontent');
626
627                 $content=~s/\r\n/\n/g;
628                 $content=~s/\r/\n/g;
629                 writefile($file, $config{srcdir}, $content);
630                 
631                 my $message="web commit ";
632                 if (defined $session->param("name") && 
633                     length $session->param("name")) {
634                         $message.="by ".$session->param("name");
635                 }
636                 else {
637                         $message.="from $ENV{REMOTE_ADDR}";
638                 }
639                 if (defined $form->field('comments') &&
640                     length $form->field('comments')) {
641                         $message.=": ".$form->field('comments');
642                 }
643                 
644                 if ($config{rcs}) {
645                         if ($newfile) {
646                                 rcs_add($file);
647                         }
648                         # prevent deadlock with post-commit hook
649                         unlockwiki();
650                         # presumably the commit will trigger an update
651                         # of the wiki
652                         my $conflict=rcs_commit($file, $message,
653                                 $form->field("rcsinfo"));
654                 
655                         if (defined $conflict) {
656                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
657                                         force => 1);
658                                 $form->tmpl_param("page_conflict", 1);
659                                 $form->field("editcontent", value => $conflict, force => 1);
660                                 $form->field(name => "comments", value => $form->field('comments'), force => 1);
661                                 $form->field("do", "edit)");
662                                 $form->tmpl_param("page_select", 0);
663                                 $form->field(name => "page", type => 'hidden');
664                                 $form->field(name => "type", type => 'hidden');
665                                 $form->title("editing $page");
666                                 print $form->render(submit => \@buttons);
667                                 return;
668                         }
669                 }
670                 else {
671                         require IkiWiki::Render;
672                         refresh();
673                         saveindex();
674                 }
675                 
676                 # The trailing question mark tries to avoid broken
677                 # caches and get the most recent version of the page.
678                 redirect($q, "$config{url}/".htmlpage($page)."?updated");
679         }
680 } #}}}
681
682 sub cgi () { #{{{
683         eval q{use CGI; use CGI::Session};
684         error($@) if $@;
685         
686         my $q=CGI->new;
687         
688         run_hooks(cgi => sub { shift->($q) });
689         
690         my $do=$q->param('do');
691         if (! defined $do || ! length $do) {
692                 my $error = $q->cgi_error;
693                 if ($error) {
694                         error("Request not processed: $error");
695                 }
696                 else {
697                         error("\"do\" parameter missing");
698                 }
699         }
700         
701         # Things that do not need a session.
702         if ($do eq 'recentchanges') {
703                 cgi_recentchanges($q);
704                 return;
705         }
706         elsif ($do eq 'hyperestraier') {
707                 cgi_hyperestraier();
708         }
709         
710         CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
711         
712         my $oldmask=umask(077);
713         my $session = CGI::Session->new("driver:DB_File", $q,
714                 { FileName => "$config{wikistatedir}/sessions.db" });
715         umask($oldmask);
716         
717         # Auth hooks can sign a user in.
718         if ($do ne 'signin' && ! defined $session->param("name")) {
719                 run_hooks(auth => sub {
720                         shift->($q, $session)
721                 });
722                 if (defined $session->param("name")) {
723                         # Make sure whatever user was authed is in the
724                         # userinfo db.
725                         if (! userinfo_get($session->param("name"), "regdate")) {
726                                 userinfo_setall($session->param("name"), {
727                                         email => "",
728                                         password => "",
729                                         regdate => time,
730                                 });
731                         }
732                 }
733         }
734
735         # Everything below this point needs the user to be signed in.
736         if (((! $config{anonok} || $do eq 'prefs') &&
737              (! $config{httpauth}) &&
738              (! defined $session->param("name") ||
739              ! userinfo_get($session->param("name"), "regdate")))
740             || $do eq 'signin') {
741                 cgi_signin($q, $session);
742         
743                 # Force session flush with safe umask.
744                 my $oldmask=umask(077);
745                 $session->flush;
746                 umask($oldmask);
747                 
748                 return;
749         }
750
751         if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
752                 print $q->header(-status => "403 Forbidden");
753                 $session->delete();
754                 print "You are banned.";
755                 exit;
756         }
757         
758         if ($do eq 'create' || $do eq 'edit') {
759                 cgi_editpage($q, $session);
760         }
761         elsif ($do eq 'prefs') {
762                 cgi_prefs($q, $session);
763         }
764         elsif ($do eq 'blog') {
765                 my $page=titlepage(decode_utf8($q->param('title')));
766                 # if the page already exists, munge it to be unique
767                 my $from=$q->param('from');
768                 my $add="";
769                 while (exists $pagecase{lc "$from/$page$add"}) {
770                         $add=1 unless length $add;
771                         $add++;
772                 }
773                 $q->param('page', $page.$add);
774                 # now run same as create
775                 $q->param('do', 'create');
776                 cgi_editpage($q, $session);
777         }
778         else {
779                 error("unknown do parameter");
780         }
781 } #}}}
782
783 1