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