finish up the blog form
[ikiwiki] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 package IkiWiki;
7
8 sub page_locked ($$;$) { #{{{
9         my $page=shift;
10         my $session=shift;
11         my $nonfatal=shift;
12         
13         my $user=$session->param("name");
14         return if length $user && is_admin($user);
15
16         foreach my $admin (@{$config{adminuser}}) {
17                 my $locked_pages=userinfo_get($admin, "locked_pages");
18                 if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
19                         return 1 if $nonfatal;
20                         error(htmllink("", $page, 1)." is locked by ".
21                               htmllink("", $admin, 1)." and cannot be edited.");
22                 }
23         }
24
25         return 0;
26 } #}}}
27
28 sub cgi_recentchanges ($) { #{{{
29         my $q=shift;
30         
31         my $template=HTML::Template->new(
32                 filename => "$config{templatedir}/recentchanges.tmpl"
33         );
34         $template->param(
35                 title => "RecentChanges",
36                 indexlink => indexlink(),
37                 wikiname => $config{wikiname},
38                 changelog => [rcs_recentchanges(100)],
39         );
40         print $q->header, $template->output;
41 } #}}}
42
43 sub cgi_signin ($$) { #{{{
44         my $q=shift;
45         my $session=shift;
46
47         eval q{use CGI::FormBuilder};
48         my $form = CGI::FormBuilder->new(
49                 title => "signin",
50                 fields => [qw(do title page subpage from name password confirm_password email)],
51                 header => 1,
52                 method => 'POST',
53                 validate => {
54                         confirm_password => {
55                                 perl => q{eq $form->field("password")},
56                         },
57                         email => 'EMAIL',
58                 },
59                 required => 'NONE',
60                 javascript => 0,
61                 params => $q,
62                 action => $q->request_uri,
63                 header => 0,
64                 template => (-e "$config{templatedir}/signin.tmpl" ?
65                               "$config{templatedir}/signin.tmpl" : "")
66         );
67         
68         $form->field(name => "name", required => 0);
69         $form->field(name => "do", type => "hidden");
70         $form->field(name => "page", type => "hidden");
71         $form->field(name => "title", type => "hidden");
72         $form->field(name => "from", type => "hidden");
73         $form->field(name => "subpage", type => "hidden");
74         $form->field(name => "password", type => "password", required => 0);
75         $form->field(name => "confirm_password", type => "password", required => 0);
76         $form->field(name => "email", required => 0);
77         if ($q->param("do") ne "signin") {
78                 $form->text("You need to log in first.");
79         }
80         
81         if ($form->submitted) {
82                 # Set required fields based on how form was submitted.
83                 my %required=(
84                         "Login" => [qw(name password)],
85                         "Register" => [qw(name password confirm_password email)],
86                         "Mail Password" => [qw(name)],
87                 );
88                 foreach my $opt (@{$required{$form->submitted}}) {
89                         $form->field(name => $opt, required => 1);
90                 }
91         
92                 # Validate password differently depending on how
93                 # form was submitted.
94                 if ($form->submitted eq 'Login') {
95                         $form->field(
96                                 name => "password",
97                                 validate => sub {
98                                         length $form->field("name") &&
99                                         shift eq userinfo_get($form->field("name"), 'password');
100                                 },
101                         );
102                         $form->field(name => "name", validate => '/^\w+$/');
103                 }
104                 else {
105                         $form->field(name => "password", validate => 'VALUE');
106                 }
107                 # And make sure the entered name exists when logging
108                 # in or sending email, and does not when registering.
109                 if ($form->submitted eq 'Register') {
110                         $form->field(
111                                 name => "name",
112                                 validate => sub {
113                                         my $name=shift;
114                                         length $name &&
115                                         ! userinfo_get($name, "regdate");
116                                 },
117                         );
118                 }
119                 else {
120                         $form->field(
121                                 name => "name",
122                                 validate => sub {
123                                         my $name=shift;
124                                         length $name &&
125                                         userinfo_get($name, "regdate");
126                                 },
127                         );
128                 }
129         }
130         else {
131                 # First time settings.
132                 $form->field(name => "name", comment => "use FirstnameLastName");
133                 $form->field(name => "confirm_password", comment => "(only needed");
134                 $form->field(name => "email",            comment => "for registration)");
135                 if ($session->param("name")) {
136                         $form->field(name => "name", value => $session->param("name"));
137                 }
138         }
139
140         if ($form->submitted && $form->validate) {
141                 if ($form->submitted eq 'Login') {
142                         $session->param("name", $form->field("name"));
143                         if (defined $form->field("do") && 
144                             $form->field("do") ne 'signin') {
145                                 print $q->redirect(
146                                         "$config{cgiurl}?do=".$form->field("do").
147                                         "&page=".$form->field("page").
148                                         "&title=".$form->field("title").
149                                         "&subpage=".$form->field("subpage").
150                                         "&from=".$form->field("from"));;
151                         }
152                         else {
153                                 print $q->redirect($config{url});
154                         }
155                 }
156                 elsif ($form->submitted eq 'Register') {
157                         my $user_name=$form->field('name');
158                         if (userinfo_setall($user_name, {
159                                            'email' => $form->field('email'),
160                                            'password' => $form->field('password'),
161                                            'regdate' => time
162                                          })) {
163                                 $form->field(name => "confirm_password", type => "hidden");
164                                 $form->field(name => "email", type => "hidden");
165                                 $form->text("Registration successful. Now you can Login.");
166                                 print $session->header();
167                                 print misctemplate($form->title, $form->render(submit => ["Login"]));
168                         }
169                         else {
170                                 error("Error saving registration.");
171                         }
172                 }
173                 elsif ($form->submitted eq 'Mail Password') {
174                         my $user_name=$form->field("name");
175                         my $template=HTML::Template->new(
176                                 filename => "$config{templatedir}/passwordmail.tmpl"
177                         );
178                         $template->param(
179                                 user_name => $user_name,
180                                 user_password => userinfo_get($user_name, "password"),
181                                 wikiurl => $config{url},
182                                 wikiname => $config{wikiname},
183                                 REMOTE_ADDR => $ENV{REMOTE_ADDR},
184                         );
185                         
186                         eval q{use Mail::Sendmail};
187                         my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
188                         sendmail(
189                                 To => userinfo_get($user_name, "email"),
190                                 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
191                                 Subject => "$config{wikiname} information",
192                                 Message => $template->output,
193                         ) or error("Failed to send mail");
194                         
195                         $form->text("Your password has been emailed to you.");
196                         $form->field(name => "name", required => 0);
197                         print $session->header();
198                         print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
199                 }
200         }
201         else {
202                 print $session->header();
203                 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
204         }
205 } #}}}
206
207 sub cgi_prefs ($$) { #{{{
208         my $q=shift;
209         my $session=shift;
210
211         eval q{use CGI::FormBuilder};
212         my $form = CGI::FormBuilder->new(
213                 title => "preferences",
214                 fields => [qw(do name password confirm_password email locked_pages)],
215                 header => 0,
216                 method => 'POST',
217                 validate => {
218                         confirm_password => {
219                                 perl => q{eq $form->field("password")},
220                         },
221                         email => 'EMAIL',
222                 },
223                 required => 'NONE',
224                 javascript => 0,
225                 params => $q,
226                 action => $q->request_uri,
227                 template => (-e "$config{templatedir}/prefs.tmpl" ?
228                               "$config{templatedir}/prefs.tmpl" : "")
229         );
230         my @buttons=("Save Preferences", "Logout", "Cancel");
231         
232         my $user_name=$session->param("name");
233         $form->field(name => "do", type => "hidden");
234         $form->field(name => "name", disabled => 1,
235                 value => $user_name, force => 1);
236         $form->field(name => "password", type => "password");
237         $form->field(name => "confirm_password", type => "password");
238         $form->field(name => "locked_pages", size => 50,
239                 comment => "(".htmllink("", "GlobList", 1).")");
240         
241         if (! is_admin($user_name)) {
242                 $form->field(name => "locked_pages", type => "hidden");
243         }
244         
245         if (! $form->submitted) {
246                 $form->field(name => "email", force => 1,
247                         value => userinfo_get($user_name, "email"));
248                 $form->field(name => "locked_pages", force => 1,
249                         value => userinfo_get($user_name, "locked_pages"));
250         }
251         
252         if ($form->submitted eq 'Logout') {
253                 $session->delete();
254                 print $q->redirect($config{url});
255                 return;
256         }
257         elsif ($form->submitted eq 'Cancel') {
258                 print $q->redirect($config{url});
259                 return;
260         }
261         elsif ($form->submitted eq "Save Preferences" && $form->validate) {
262                 foreach my $field (qw(password email locked_pages)) {
263                         if (length $form->field($field)) {
264                                 userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
265                         }
266                 }
267                 $form->text("Preferences saved.");
268         }
269         
270         print $session->header();
271         print misctemplate($form->title, $form->render(submit => \@buttons));
272 } #}}}
273
274 sub cgi_editpage ($$) { #{{{
275         my $q=shift;
276         my $session=shift;
277
278         eval q{use CGI::FormBuilder};
279         my $form = CGI::FormBuilder->new(
280                 fields => [qw(do rcsinfo subpage from page content comments)],
281                 header => 1,
282                 method => 'POST',
283                 validate => {
284                         content => '/.+/',
285                 },
286                 required => [qw{content}],
287                 javascript => 0,
288                 params => $q,
289                 action => $q->request_uri,
290                 table => 0,
291                 template => "$config{templatedir}/editpage.tmpl"
292         );
293         my @buttons=("Save Page", "Preview", "Cancel");
294         
295         my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
296         if (! defined $page || ! length $page || $page ne $q->param('page') ||
297             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
298                 error("bad page name");
299         }
300         $page=lc($page);
301         
302         my $file=$page.$config{default_pageext};
303         my $newfile=1;
304         if (exists $pagesources{lc($page)}) {
305                 $file=$pagesources{lc($page)};
306                 $newfile=0;
307         }
308
309         $form->field(name => "do", type => 'hidden');
310         $form->field(name => "from", type => 'hidden');
311         $form->field(name => "rcsinfo", type => 'hidden');
312         $form->field(name => "subpage", type => 'hidden');
313         $form->field(name => "page", value => "$page", force => 1);
314         $form->field(name => "comments", type => "text", size => 80);
315         $form->field(name => "content", type => "textarea", rows => 20,
316                 cols => 80);
317         $form->tmpl_param("can_commit", $config{rcs});
318         $form->tmpl_param("indexlink", indexlink());
319         $form->tmpl_param("helponformattinglink",
320                 htmllink("", "HelpOnFormatting", 1));
321         if (! $form->submitted) {
322                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
323                         force => 1);
324         }
325         
326         if ($form->submitted eq "Cancel") {
327                 print $q->redirect("$config{url}/".htmlpage($page));
328                 return;
329         }
330         elsif ($form->submitted eq "Preview") {
331                 require IkiWiki::Render;
332                 $form->tmpl_param("page_preview",
333                         htmlize($config{default_pageext},
334                                 linkify($form->field('content'), $page)));
335         }
336         else {
337                 $form->tmpl_param("page_preview", "");
338         }
339         $form->tmpl_param("page_conflict", "");
340         
341         if (! $form->submitted || $form->submitted eq "Preview" || 
342             ! $form->validate) {
343                 if ($form->field("do") eq "create") {
344                         if (exists $pagesources{lc($page)}) {
345                                 # hmm, someone else made the page in the
346                                 # meantime?
347                                 print $q->redirect("$config{url}/".htmlpage($page));
348                                 return;
349                         }
350                         
351                         my @page_locs;
352                         my $best_loc;
353                         my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
354                         if (! defined $from || ! length $from ||
355                             $from ne $form->param('from') ||
356                             $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
357                                 @page_locs=$best_loc=$page;
358                         }
359                         else {
360                                 my $dir=$from."/";
361                                 $dir=~s![^/]+/$!!;
362                                 
363                                 if (length $form->param('subpage') ||
364                                     $page eq 'discussion') {
365                                         $best_loc="$from/$page";
366                                 }
367                                 else {
368                                         $best_loc=$dir.$page;
369                                 }
370                                 
371                                 push @page_locs, $dir.$page;
372                                 push @page_locs, "$from/$page";
373                                 while (length $dir) {
374                                         $dir=~s![^/]+/$!!;
375                                         push @page_locs, $dir.$page;
376                                 }
377
378                                 @page_locs = grep {
379                                         ! exists $pagesources{lc($_)} &&
380                                         ! page_locked($_, $session, 1)
381                                 } @page_locs;
382                         }
383
384                         $form->tmpl_param("page_select", 1);
385                         $form->field(name => "page", type => 'select',
386                                 options => \@page_locs, value => $best_loc);
387                         $form->title("creating ".pagetitle($page));
388                 }
389                 elsif ($form->field("do") eq "edit") {
390                         page_locked($page, $session);
391                         if (! defined $form->field('content') || 
392                             ! length $form->field('content')) {
393                                 my $content="";
394                                 if (exists $pagesources{lc($page)}) {
395                                         $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
396                                         $content=~s/\n/\r\n/g;
397                                 }
398                                 $form->field(name => "content", value => $content,
399                                         force => 1);
400                         }
401                         $form->tmpl_param("page_select", 0);
402                         $form->field(name => "page", type => 'hidden');
403                         $form->title("editing ".pagetitle($page));
404                 }
405                 
406                 print $form->render(submit => \@buttons);
407         }
408         else {
409                 # save page
410                 page_locked($page, $session);
411                 
412                 my $content=$form->field('content');
413                 $content=~s/\r\n/\n/g;
414                 $content=~s/\r/\n/g;
415                 writefile("$config{srcdir}/$file", $content);
416                 
417                 my $message="web commit ";
418                 if (length $session->param("name")) {
419                         $message.="by ".$session->param("name");
420                 }
421                 else {
422                         $message.="from $ENV{REMOTE_ADDR}";
423                 }
424                 if (defined $form->field('comments') &&
425                     length $form->field('comments')) {
426                         $message.=": ".$form->field('comments');
427                 }
428                 
429                 if ($config{rcs}) {
430                         if ($newfile) {
431                                 rcs_add($file);
432                         }
433                         # prevent deadlock with post-commit hook
434                         unlockwiki();
435                         # presumably the commit will trigger an update
436                         # of the wiki
437                         my $conflict=rcs_commit($file, $message,
438                                 $form->field("rcsinfo"));
439                 
440                         if (defined $conflict) {
441                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
442                                         force => 1);
443                                 $form->tmpl_param("page_conflict", 1);
444                                 $form->field("content", value => $conflict, force => 1);
445                                 $form->field("do", "edit)");
446                                 $form->tmpl_param("page_select", 0);
447                                 $form->field(name => "page", type => 'hidden');
448                                 $form->title("editing $page");
449                                 print $form->render(submit => \@buttons);
450                                 return;
451                         }
452                 }
453                 else {
454                         require IkiWiki::Render;
455                         refresh();
456                         saveindex();
457                 }
458                 
459                 # The trailing question mark tries to avoid broken
460                 # caches and get the most recent version of the page.
461                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
462         }
463 } #}}}
464
465 sub cgi () { #{{{
466         eval q{use CGI};
467         eval q{use CGI::Session};
468         
469         my $q=CGI->new;
470         
471         my $do=$q->param('do');
472         if (! defined $do || ! length $do) {
473                 error("\"do\" parameter missing");
474         }
475         
476         # Things that do not need a session.
477         if ($do eq 'recentchanges') {
478                 cgi_recentchanges($q);
479                 return;
480         }
481         
482         CGI::Session->name("ikiwiki_session");
483
484         my $oldmask=umask(077);
485         my $session = CGI::Session->new("driver:db_file", $q,
486                 { FileName => "$config{wikistatedir}/sessions.db" });
487         umask($oldmask);
488         
489         # Everything below this point needs the user to be signed in.
490         if ((! $config{anonok} && ! defined $session->param("name") ||
491              ! defined $session->param("name") ||
492              ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
493                 cgi_signin($q, $session);
494         
495                 # Force session flush with safe umask.
496                 my $oldmask=umask(077);
497                 $session->flush;
498                 umask($oldmask);
499                 
500                 return;
501         }
502         
503         if ($do eq 'create' || $do eq 'edit') {
504                 cgi_editpage($q, $session);
505         }
506         elsif ($do eq 'prefs') {
507                 cgi_prefs($q, $session);
508         }
509         elsif ($do eq 'blog') {
510                 # munge page name to be valid, no matter what freeform text
511                 # is entered
512                 my $page=lc($q->param('title'));
513                 $page=~y/ /_/;
514                 $page=~s/([^-A-Za-z0-9_:+])/"__".ord($1)."__"/eg;
515                 # if the page already exist, munge it to be unique
516                 my $from=$q->param('from');
517                 my $add="";
518                 while (exists $oldpagemtime{"$from/$page$add"}) {
519                         $add=1 unless length $add;
520                         $add++;
521                 }
522                 $q->param('page', $page.$add);
523                 # now run same as create
524                 $q->param('do', 'create');
525                 cgi_editpage($q, $session);
526         }
527         else {
528                 error("unknown do parameter");
529         }
530 } #}}}
531
532 1