]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/comments.pm
comments: Use HTML entities to escape directives
[ikiwiki.git] / IkiWiki / Plugin / comments.pm
1 #!/usr/bin/perl
2 # Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
3 # Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
4 # Licensed under the GNU GPL, version 2, or any later version published by the
5 # Free Software Foundation
6 package IkiWiki::Plugin::comments;
7
8 use warnings;
9 use strict;
10 use IkiWiki 2.00;
11
12 use constant PREVIEW => "Preview";
13 use constant POST_COMMENT => "Post comment";
14 use constant CANCEL => "Cancel";
15
16 sub import { #{{{
17         hook(type => "getsetup", id => 'comments',  call => \&getsetup);
18         hook(type => "preprocess", id => 'comments', call => \&preprocess);
19         hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
20         hook(type => "htmlize", id => "_comment", call => \&htmlize);
21         IkiWiki::loadplugin("inline");
22         IkiWiki::loadplugin("mdwn");
23 } # }}}
24
25 sub htmlize { # {{{
26         eval q{use IkiWiki::Plugin::mdwn};
27         error($@) if ($@);
28         return IkiWiki::Plugin::mdwn::htmlize(@_)
29 } # }}}
30
31 sub getsetup () { #{{{
32         return
33                 plugin => {
34                         safe => 1,
35                         rebuild => undef,
36                 },
37 } #}}}
38
39 # Somewhat based on IkiWiki::Plugin::inline blog posting support
40 sub preprocess (@) { #{{{
41         my %params=@_;
42
43         unless (length $config{cgiurl}) {
44                 error(gettext("[[!comments plugin requires CGI enabled]]"));
45         }
46
47         my $page = $params{page};
48         $pagestate{$page}{comments}{comments} = defined $params{closed}
49                 ? (not IkiWiki::yesno($params{closed}))
50                 : 1;
51         $pagestate{$page}{comments}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
52         $pagestate{$page}{comments}{commit} = defined $params{commit}
53                 ? IkiWiki::yesno($params{commit})
54                 : 1;
55
56         my $formtemplate = IkiWiki::template("comments_embed.tmpl",
57                 blind_cache => 1);
58         $formtemplate->param(cgiurl => $config{cgiurl});
59         $formtemplate->param(page => $params{page});
60
61         if (not $pagestate{$page}{comments}{comments}) {
62                 $formtemplate->param("disabled" =>
63                         gettext('comments are closed'));
64         }
65         elsif ($params{preview}) {
66                 $formtemplate->param("disabled" =>
67                         gettext('not available during Preview'));
68         }
69
70         debug("page $params{page} => destpage $params{destpage}");
71
72         unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
73                 my $posts = '';
74                 eval q{use IkiWiki::Plugin::inline};
75                 error($@) if ($@);
76                 my @args = (
77                         pages => "internal($params{page}/_comment_*)",
78                         template => "comments_display",
79                         show => 0,
80                         reverse => "yes",
81                         # special stuff passed through
82                         page => $params{page},
83                         destpage => $params{destpage},
84                         preview => $params{preview},
85                 );
86                 push @args, atom => $params{atom} if defined $params{atom};
87                 push @args, rss => $params{rss} if defined $params{rss};
88                 push @args, feeds => $params{feeds} if defined $params{feeds};
89                 push @args, feedshow => $params{feedshow} if defined $params{feedshow};
90                 push @args, timeformat => $params{timeformat} if defined $params{timeformat};
91                 push @args, feedonly => $params{feedonly} if defined $params{feedonly};
92                 $posts = IkiWiki::preprocess_inline(@args);
93                 $formtemplate->param("comments" => $posts);
94         }
95
96         return $formtemplate->output;
97 } # }}}
98
99 # FIXME: logic taken from editpage, should be common code?
100 sub getcgiuser ($) { # {{{
101         my $session = shift;
102         my $user = $session->param('name');
103         $user = $ENV{REMOTE_ADDR} unless defined $user;
104         debug("getcgiuser() -> $user");
105         return $user;
106 } # }}}
107
108 # FIXME: logic adapted from recentchanges, should be common code?
109 sub linkuser ($) { # {{{
110         my $user = shift;
111         my $oiduser = eval { IkiWiki::openiduser($user) };
112
113         if (defined $oiduser) {
114                 return ($user, $oiduser);
115         }
116         else {
117                 my $page = bestlink('', (length $config{userdir}
118                                 ? "$config{userdir}/"
119                                 : "").$user);
120                 return (urlto($page, undef, 1), $user);
121         }
122 } # }}}
123
124 # Mostly cargo-culted from IkiWiki::plugin::editpage
125 sub sessioncgi ($$) { #{{{
126         my $cgi=shift;
127         my $session=shift;
128
129         my $do = $cgi->param('do');
130         return unless $do eq 'comment';
131
132         IkiWiki::decode_cgi_utf8($cgi);
133
134         eval q{use CGI::FormBuilder};
135         error($@) if $@;
136
137         my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
138         my $form = CGI::FormBuilder->new(
139                 fields => [qw{do sid page subject body}],
140                 charset => 'utf-8',
141                 method => 'POST',
142                 required => [qw{body}],
143                 javascript => 0,
144                 params => $cgi,
145                 action => $config{cgiurl},
146                 header => 0,
147                 table => 0,
148                 template => scalar IkiWiki::template_params('comments_form.tmpl'),
149                 # wtf does this do in editpage?
150                 wikiname => $config{wikiname},
151         );
152
153         IkiWiki::decode_form_utf8($form);
154         IkiWiki::run_hooks(formbuilder_setup => sub {
155                         shift->(title => "comment", form => $form, cgi => $cgi,
156                                 session => $session, buttons => \@buttons);
157                 });
158         IkiWiki::decode_form_utf8($form);
159
160         $form->field(name => 'do', type => 'hidden');
161         $form->field(name => 'sid', type => 'hidden', value => $session->id,
162                 force => 1);
163         $form->field(name => 'page', type => 'hidden');
164         $form->field(name => 'subject', type => 'text', size => 72);
165         $form->field(name => 'body', type => 'textarea', rows => 5,
166                 cols => 80);
167
168         # The untaint is OK (as in editpage) because we're about to pass
169         # it to file_pruned anyway
170         my $page = $form->field('page');
171         $page = IkiWiki::possibly_foolish_untaint($page);
172         if (!defined $page || !length $page ||
173                 IkiWiki::file_pruned($page, $config{srcdir})) {
174                 error(gettext("bad page name"));
175         }
176
177         my $allow_directives = $pagestate{$page}{comments}{allowdirectives};
178         my $commit_comments = defined $pagestate{$page}{comments}{commit}
179                 ? $pagestate{$page}{comments}{commit}
180                 : 1;
181
182         # FIXME: is this right? Or should we be using the candidate subpage
183         # (whatever that might mean) as the base URL?
184         my $baseurl = urlto($page, undef, 1);
185
186         $form->title(sprintf(gettext("commenting on %s"),
187                         IkiWiki::pagetitle($page)));
188
189         $form->tmpl_param('helponformattinglink',
190                 htmllink($page, $page, 'ikiwiki/formatting',
191                         noimageinline => 1,
192                         linktext => 'FormattingHelp'),
193                         allowdirectives => $allow_directives);
194
195         if (not exists $pagesources{$page}) {
196                 error(sprintf(gettext(
197                         "page '%s' doesn't exist, so you can't comment"),
198                         $page));
199         }
200         if (not $pagestate{$page}{comments}{comments}) {
201                 error(sprintf(gettext(
202                         "comments are not enabled on page '%s'"),
203                         $page));
204         }
205
206         if ($form->submitted eq CANCEL) {
207                 # bounce back to the page they wanted to comment on, and exit.
208                 # CANCEL need not be considered in future
209                 IkiWiki::redirect($cgi, urlto($page, undef, 1));
210                 exit;
211         }
212
213         IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
214
215         my ($authorurl, $author) = linkuser(getcgiuser($session));
216
217         my $body = $form->field('body') || '';
218         $body =~ s/\r\n/\n/g;
219         $body =~ s/\r/\n/g;
220         $body .= "\n" if $body !~ /\n$/;
221
222         unless ($allow_directives) {
223                 # don't allow new-style directives at all
224                 $body =~ s/(^|[^\\])\[\[!/$1&#91;&#91;!/g;
225
226                 # don't allow [[ unless it begins an old-style
227                 # wikilink, if prefix_directives is off
228                 $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1&#91;&#91;!/g
229                         unless $config{prefix_directives};
230         }
231
232         IkiWiki::run_hooks(sanitize => sub {
233                 # $fake is a possible location for this comment. We don't
234                 # know yet what the comment number *actually* is.
235                 my $fake = "$page/_comment_1";
236                 $body=shift->(
237                         page => $fake,
238                         destpage => $fake,
239                         content => $body,
240                 );
241         });
242
243         # In this template, the [[!meta]] directives should stay at the end,
244         # so that they will override anything the user specifies. (For
245         # instance, [[!meta author="I can fake the author"]]...)
246         my $content_tmpl = template('comments_comment.tmpl');
247         $content_tmpl->param(author => $author);
248         $content_tmpl->param(authorurl => $authorurl);
249         $content_tmpl->param(subject => $form->field('subject'));
250         $content_tmpl->param(body => $body);
251
252         my $content = $content_tmpl->output;
253
254         # This is essentially a simplified version of editpage:
255         # - the user does not control the page that's created, only the parent
256         # - it's always a create operation, never an edit
257         # - this means that conflicts should never happen
258         # - this means that if they do, rocks fall and everyone dies
259
260         if ($form->submitted eq PREVIEW) {
261                 # $fake is a possible location for this comment. We don't
262                 # know yet what the comment number *actually* is.
263                 my $fake = "$page/_comment_1";
264                 my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
265                                 IkiWiki::linkify($page, $page,
266                                         IkiWiki::preprocess($page, $page,
267                                                 IkiWiki::filter($fake, $page,
268                                                         $content),
269                                                 0, 1)));
270                 IkiWiki::run_hooks(format => sub {
271                                 $preview = shift->(page => $page,
272                                         content => $preview);
273                         });
274
275                 my $template = template("comments_display.tmpl");
276                 $template->param(content => $preview);
277                 $template->param(title => $form->field('subject'));
278                 $template->param(ctime => displaytime(time));
279                 $template->param(author => $author);
280                 $template->param(authorurl => $authorurl);
281
282                 $form->tmpl_param(page_preview => $template->output);
283         }
284         else {
285                 $form->tmpl_param(page_preview => "");
286         }
287
288         if ($form->submitted eq POST_COMMENT && $form->validate) {
289                 # Let's get posting. We don't check_canedit here because
290                 # that somewhat defeats the point of this plugin.
291
292                 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
293
294                 # FIXME: check that the wiki is locked right now, because
295                 # if it's not, there are mad race conditions!
296
297                 # FIXME: rather a simplistic way to make the comments...
298                 my $i = 0;
299                 my $file;
300                 do {
301                         $i++;
302                         $file = "$page/_comment_${i}._comment";
303                 } while (-e "$config{srcdir}/$file");
304
305                 # FIXME: could probably do some sort of graceful retry
306                 # if I could be bothered
307                 writefile($file, $config{srcdir}, $content);
308
309                 my $conflict;
310
311                 if ($config{rcs} and $commit_comments) {
312                         my $message = gettext("Added a comment");
313                         if (defined $form->field('subject') &&
314                                 length $form->field('subject')) {
315                                 $message .= ": ".$form->field('subject');
316                         }
317
318                         IkiWiki::rcs_add($file);
319                         IkiWiki::disable_commit_hook();
320                         $conflict = IkiWiki::rcs_commit_staged($message,
321                                 $session->param('name'), $ENV{REMOTE_ADDR});
322                         IkiWiki::enable_commit_hook();
323                         IkiWiki::rcs_update();
324                 }
325
326                 # Now we need a refresh
327                 require IkiWiki::Render;
328                 IkiWiki::refresh();
329                 IkiWiki::saveindex();
330
331                 # this should never happen, unless a committer deliberately
332                 # breaks it or something
333                 error($conflict) if defined $conflict;
334
335                 # Bounce back to where we were, but defeat broken caches
336                 my $anticache = "?updated=$page/_comment_$i";
337                 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
338         }
339         else {
340                 IkiWiki::showform ($form, \@buttons, $session, $cgi,
341                         forcebaseurl => $baseurl);
342         }
343
344         exit;
345 } #}}}
346
347 package IkiWiki::PageSpec;
348
349 sub match_postcomment ($$;@) {
350         my $page = shift;
351         my $glob = shift;
352
353         unless ($page =~ s/\[postcomment\]$//) {
354                 return IkiWiki::FailReason->new("not posting a comment");
355         }
356         return match_glob($page, $glob);
357 }
358
359 1