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