]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/CGI.pm
* Support pages with utf8 filenames. Patch by Faidon Liambotis.
[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)=Encode::decode_utf8($form->param('page'));
312         $page=titlepage(possibly_foolish_untaint(lc($page)));
313         if (! defined $page || ! length $page ||
314             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
315                 error("bad page name");
316         }
317         $page=lc($page);
318         
319         my $file=$page.$config{default_pageext};
320         if (exists $pagesources{lc($page)}) {
321                 $file=$pagesources{lc($page)};
322         }
323         my $newfile=0;
324         if (! -e "$config{srcdir}/$file") {
325                 $newfile=1;
326         }
327
328         $form->field(name => "do", type => 'hidden');
329         $form->field(name => "from", type => 'hidden');
330         $form->field(name => "rcsinfo", type => 'hidden');
331         $form->field(name => "subpage", type => 'hidden');
332         $form->field(name => "page", value => "$page", force => 1);
333         $form->field(name => "comments", type => "text", size => 80);
334         $form->field(name => "editcontent", type => "textarea", rows => 20,
335                 cols => 80);
336         $form->tmpl_param("can_commit", $config{rcs});
337         $form->tmpl_param("indexlink", indexlink());
338         $form->tmpl_param("helponformattinglink",
339                 htmllink("", "", "HelpOnFormatting", 1));
340         $form->tmpl_param("styleurl", styleurl());
341         $form->tmpl_param("baseurl", "$config{url}/");
342         if (! $form->submitted) {
343                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
344                         force => 1);
345         }
346         
347         if ($form->submitted eq "Cancel") {
348                 print $q->redirect("$config{url}/".htmlpage($page));
349                 return;
350         }
351         elsif ($form->submitted eq "Preview") {
352                 require IkiWiki::Render;
353                 # Apparently FormBuilder doesn't not treat input as
354                 # utf-8, so decode from it.
355                 require Encode;
356                 my $content = Encode::decode_utf8($form->field('editcontent'));
357                 my $comments = Encode::decode_utf8($form->field('comments'));
358                 $form->field(name => "editcontent",
359                                 value => $content, force => 1);
360                 $form->field(name => "comments",
361                                 value => $comments, 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                 # Decode utf-8 since FormBuilder does not
445                 require Encode;
446                 my $content=Encode::decode_utf8($form->field('editcontent'));
447
448                 $content=~s/\r\n/\n/g;
449                 $content=~s/\r/\n/g;
450                 writefile($file, $config{srcdir}, $content);
451                 
452                 my $message="web commit ";
453                 if (defined $session->param("name") && 
454                     length $session->param("name")) {
455                         $message.="by ".$session->param("name");
456                 }
457                 else {
458                         $message.="from $ENV{REMOTE_ADDR}";
459                 }
460                 if (defined $form->field('comments') &&
461                     length $form->field('comments')) {
462                         $message.=": ".Encode::decode_utf8($form->field('comments'));
463                 }
464                 
465                 if ($config{rcs}) {
466                         if ($newfile) {
467                                 rcs_add($file);
468                         }
469                         # prevent deadlock with post-commit hook
470                         unlockwiki();
471                         # presumably the commit will trigger an update
472                         # of the wiki
473                         my $conflict=rcs_commit($file, $message,
474                                 $form->field("rcsinfo"));
475                 
476                         if (defined $conflict) {
477                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
478                                         force => 1);
479                                 $form->tmpl_param("page_conflict", 1);
480                                 $form->field("editcontent", value => $conflict, force => 1);
481                                 $form->field(name => "comments", value => Encode::decode_utf8($form->field('comments')), force => 1);
482                                 $form->field("do", "edit)");
483                                 $form->tmpl_param("page_select", 0);
484                                 $form->field(name => "page", type => 'hidden');
485                                 $form->title("editing $page");
486                                 print $form->render(submit => \@buttons);
487                                 return;
488                         }
489                 }
490                 else {
491                         require IkiWiki::Render;
492                         refresh();
493                         saveindex();
494                 }
495                 
496                 # The trailing question mark tries to avoid broken
497                 # caches and get the most recent version of the page.
498                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
499         }
500 } #}}}
501
502 sub cgi () { #{{{
503         eval q{use CGI};
504         eval q{use CGI::Session};
505         
506         my $q=CGI->new;
507         
508         if (exists $hooks{cgi}) {
509                 foreach my $id (keys %{$hooks{cgi}}) {
510                         $hooks{cgi}{$id}{call}->($q);
511                 }
512         }
513         
514         my $do=$q->param('do');
515         if (! defined $do || ! length $do) {
516                 error("\"do\" parameter missing");
517         }
518         
519         # Things that do not need a session.
520         if ($do eq 'recentchanges') {
521                 cgi_recentchanges($q);
522                 return;
523         }
524         elsif ($do eq 'hyperestraier') {
525                 cgi_hyperestraier();
526         }
527         
528         CGI::Session->name("ikiwiki_session_$config{wikiname}");
529         
530         my $oldmask=umask(077);
531         my $session = CGI::Session->new("driver:DB_File", $q,
532                 { FileName => "$config{wikistatedir}/sessions.db" });
533         umask($oldmask);
534         
535         # Everything below this point needs the user to be signed in.
536         if ((! $config{anonok} &&
537              (! defined $session->param("name") ||
538              ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
539                 cgi_signin($q, $session);
540         
541                 # Force session flush with safe umask.
542                 my $oldmask=umask(077);
543                 $session->flush;
544                 umask($oldmask);
545                 
546                 return;
547         }
548         
549         if ($do eq 'create' || $do eq 'edit') {
550                 cgi_editpage($q, $session);
551         }
552         elsif ($do eq 'prefs') {
553                 cgi_prefs($q, $session);
554         }
555         elsif ($do eq 'blog') {
556                 my $page=titlepage(lc($q->param('title')));
557                 # if the page already exists, munge it to be unique
558                 my $from=$q->param('from');
559                 my $add="";
560                 while (exists $oldpagemtime{"$from/$page$add"}) {
561                         $add=1 unless length $add;
562                         $add++;
563                 }
564                 $q->param('page', $page.$add);
565                 # now run same as create
566                 $q->param('do', 'create');
567                 cgi_editpage($q, $session);
568         }
569         else {
570                 error("unknown do parameter");
571         }
572 } #}}}
573
574 1