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