]> 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         # This untaint is safe because titlepage removes any problimatic
298         # characters.
299         my ($page)=titlepage(possibly_foolish_untaint(lc($form->param('page'))));
300         if (! defined $page || ! length $page ||
301             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
302                 error("bad page name");
303         }
304         $page=lc($page);
305         
306         my $file=$page.$config{default_pageext};
307         my $newfile=1;
308         if (exists $pagesources{lc($page)}) {
309                 $file=$pagesources{lc($page)};
310                 $newfile=0;
311         }
312
313         $form->field(name => "do", type => 'hidden');
314         $form->field(name => "from", type => 'hidden');
315         $form->field(name => "rcsinfo", type => 'hidden');
316         $form->field(name => "subpage", type => 'hidden');
317         $form->field(name => "page", value => "$page", force => 1);
318         $form->field(name => "comments", type => "text", size => 80);
319         $form->field(name => "content", type => "textarea", rows => 20,
320                 cols => 80);
321         $form->tmpl_param("can_commit", $config{rcs});
322         $form->tmpl_param("indexlink", indexlink());
323         $form->tmpl_param("helponformattinglink",
324                 htmllink("", "HelpOnFormatting", 1));
325         if (! $form->submitted) {
326                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
327                         force => 1);
328         }
329         
330         if ($form->submitted eq "Cancel") {
331                 print $q->redirect("$config{url}/".htmlpage($page));
332                 return;
333         }
334         elsif ($form->submitted eq "Preview") {
335                 require IkiWiki::Render;
336                 $form->tmpl_param("page_preview",
337                         htmlize($config{default_pageext},
338                                 linkify($form->field('content'), $page)));
339         }
340         else {
341                 $form->tmpl_param("page_preview", "");
342         }
343         $form->tmpl_param("page_conflict", "");
344         
345         if (! $form->submitted || $form->submitted eq "Preview" || 
346             ! $form->validate) {
347                 if ($form->field("do") eq "create") {
348                         if (exists $pagesources{lc($page)}) {
349                                 # hmm, someone else made the page in the
350                                 # meantime?
351                                 print $q->redirect("$config{url}/".htmlpage($page));
352                                 return;
353                         }
354                         
355                         my @page_locs;
356                         my $best_loc;
357                         my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
358                         if (! defined $from || ! length $from ||
359                             $from ne $form->param('from') ||
360                             $from=~/$config{wiki_file_prune_regexp}/ ||
361                             $from=~/^\// ||
362                             $form->submitted eq "Preview") {
363                                 @page_locs=$best_loc=$page;
364                         }
365                         else {
366                                 my $dir=$from."/";
367                                 $dir=~s![^/]+/$!!;
368                                 
369                                 if ((defined $form->param('subpage') && length $form->param('subpage')) ||
370                                     $page eq 'discussion') {
371                                         $best_loc="$from/$page";
372                                 }
373                                 else {
374                                         $best_loc=$dir.$page;
375                                 }
376                                 
377                                 push @page_locs, $dir.$page;
378                                 push @page_locs, "$from/$page";
379                                 while (length $dir) {
380                                         $dir=~s![^/]+/$!!;
381                                         push @page_locs, $dir.$page;
382                                 }
383
384                                 @page_locs = grep {
385                                         ! exists $pagesources{lc($_)} &&
386                                         ! page_locked($_, $session, 1)
387                                 } @page_locs;
388                         }
389
390                         $form->tmpl_param("page_select", 1);
391                         $form->field(name => "page", type => 'select',
392                                 options => \@page_locs, value => $best_loc);
393                         $form->title("creating ".pagetitle($page));
394                 }
395                 elsif ($form->field("do") eq "edit") {
396                         page_locked($page, $session);
397                         if (! defined $form->field('content') || 
398                             ! length $form->field('content')) {
399                                 my $content="";
400                                 if (exists $pagesources{lc($page)}) {
401                                         $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
402                                         $content=~s/\n/\r\n/g;
403                                 }
404                                 $form->field(name => "content", value => $content,
405                                         force => 1);
406                         }
407                         $form->tmpl_param("page_select", 0);
408                         $form->field(name => "page", type => 'hidden');
409                         $form->title("editing ".pagetitle($page));
410                 }
411                 
412                 print $form->render(submit => \@buttons);
413         }
414         else {
415                 # save page
416                 page_locked($page, $session);
417                 
418                 my $content=$form->field('content');
419                 $content=~s/\r\n/\n/g;
420                 $content=~s/\r/\n/g;
421                 writefile("$config{srcdir}/$file", $content);
422                 
423                 my $message="web commit ";
424                 if (length $session->param("name")) {
425                         $message.="by ".$session->param("name");
426                 }
427                 else {
428                         $message.="from $ENV{REMOTE_ADDR}";
429                 }
430                 if (defined $form->field('comments') &&
431                     length $form->field('comments')) {
432                         $message.=": ".$form->field('comments');
433                 }
434                 
435                 if ($config{rcs}) {
436                         if ($newfile) {
437                                 rcs_add($file);
438                         }
439                         # prevent deadlock with post-commit hook
440                         unlockwiki();
441                         # presumably the commit will trigger an update
442                         # of the wiki
443                         my $conflict=rcs_commit($file, $message,
444                                 $form->field("rcsinfo"));
445                 
446                         if (defined $conflict) {
447                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
448                                         force => 1);
449                                 $form->tmpl_param("page_conflict", 1);
450                                 $form->field("content", value => $conflict, force => 1);
451                                 $form->field("do", "edit)");
452                                 $form->tmpl_param("page_select", 0);
453                                 $form->field(name => "page", type => 'hidden');
454                                 $form->title("editing $page");
455                                 print $form->render(submit => \@buttons);
456                                 return;
457                         }
458                 }
459                 else {
460                         require IkiWiki::Render;
461                         refresh();
462                         saveindex();
463                 }
464                 
465                 # The trailing question mark tries to avoid broken
466                 # caches and get the most recent version of the page.
467                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
468         }
469 } #}}}
470
471 sub cgi () { #{{{
472         eval q{use CGI};
473         eval q{use CGI::Session};
474         
475         my $q=CGI->new;
476         
477         my $do=$q->param('do');
478         if (! defined $do || ! length $do) {
479                 error("\"do\" parameter missing");
480         }
481         
482         # Things that do not need a session.
483         if ($do eq 'recentchanges') {
484                 cgi_recentchanges($q);
485                 return;
486         }
487         
488         CGI::Session->name("ikiwiki_session_$config{wikiname}");
489         
490         my $oldmask=umask(077);
491         my $session = CGI::Session->new("driver:db_file", $q,
492                 { FileName => "$config{wikistatedir}/sessions.db" });
493         umask($oldmask);
494         
495         # Everything below this point needs the user to be signed in.
496         if ((! $config{anonok} && ! defined $session->param("name") ||
497              ! defined $session->param("name") ||
498              ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
499                 cgi_signin($q, $session);
500         
501                 # Force session flush with safe umask.
502                 my $oldmask=umask(077);
503                 $session->flush;
504                 umask($oldmask);
505                 
506                 return;
507         }
508         
509         if ($do eq 'create' || $do eq 'edit') {
510                 cgi_editpage($q, $session);
511         }
512         elsif ($do eq 'prefs') {
513                 cgi_prefs($q, $session);
514         }
515         elsif ($do eq 'blog') {
516                 my $page=titlepage(lc($q->param('title')));
517                 # if the page already exists, 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