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