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