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