repository; edit this file to name it for gitweb.
summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: b2243ce4c85e3938483e4656746cf93d0b2f6821 (plain)
  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. use warnings;
  8. use strict;
  9. use IkiWiki 3.00;
  10. use Encode;
  11. use POSIX qw(strftime);
  12. use constant PREVIEW => "Preview";
  13. use constant POST_COMMENT => "Post comment";
  14. use constant CANCEL => "Cancel";
  15. my $postcomment;
  16. my %commentstate;
  17. sub import {
  18. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  19. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  20. hook(type => "preprocess", id => '_comment', call => \&preprocess);
  21. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  22. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  23. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  24. hook(type => "cgi", id => "comments", call => \&linkcgi);
  25. hook(type => "formbuilder_setup", id => "comments", call => \&formbuilder_setup);
  26. IkiWiki::loadplugin("inline");
  27. }
  28. sub getsetup () {
  29. return
  30. plugin => {
  31. safe => 1,
  32. rebuild => 1,
  33. },
  34. comments_pagespec => {
  35. type => 'pagespec',
  36. example => 'blog/* and !*/Discussion',
  37. description => 'PageSpec of pages where comments are allowed',
  38. link => 'ikiwiki/PageSpec',
  39. safe => 1,
  40. rebuild => 1,
  41. },
  42. comments_closed_pagespec => {
  43. type => 'pagespec',
  44. example => 'blog/controversial or blog/flamewar',
  45. description => 'PageSpec of pages where posting new comments is not allowed',
  46. link => 'ikiwiki/PageSpec',
  47. safe => 1,
  48. rebuild => 1,
  49. },
  50. comments_pagename => {
  51. type => 'string',
  52. default => 'comment_',
  53. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  54. safe => 0, # manual page moving required
  55. rebuild => undef,
  56. },
  57. comments_allowdirectives => {
  58. type => 'boolean',
  59. example => 0,
  60. description => 'Interpret directives in comments?',
  61. safe => 1,
  62. rebuild => 0,
  63. },
  64. comments_allowauthor => {
  65. type => 'boolean',
  66. example => 0,
  67. description => 'Allow anonymous commenters to set an author name?',
  68. safe => 1,
  69. rebuild => 0,
  70. },
  71. comments_commit => {
  72. type => 'boolean',
  73. example => 1,
  74. description => 'commit comments to the VCS',
  75. # old uncommitted comments are likely to cause
  76. # confusion if this is changed
  77. safe => 0,
  78. rebuild => 0,
  79. },
  80. }
  81. sub checkconfig () {
  82. $config{comments_commit} = 1
  83. unless defined $config{comments_commit};
  84. $config{comments_pagespec} = ''
  85. unless defined $config{comments_pagespec};
  86. $config{comments_closed_pagespec} = ''
  87. unless defined $config{comments_closed_pagespec};
  88. $config{comments_pagename} = 'comment_'
  89. unless defined $config{comments_pagename};
  90. }
  91. sub htmlize {
  92. my %params = @_;
  93. return $params{content};
  94. }
  95. # FIXME: copied verbatim from meta
  96. sub safeurl ($) {
  97. my $url=shift;
  98. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  99. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  100. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  101. }
  102. else {
  103. return 1;
  104. }
  105. }
  106. sub preprocess {
  107. my %params = @_;
  108. my $page = $params{page};
  109. my $format = $params{format};
  110. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  111. error(sprintf(gettext("unsupported page format %s"), $format));
  112. }
  113. my $content = $params{content};
  114. if (! defined $content) {
  115. error(gettext("comment must have content"));
  116. }
  117. $content =~ s/\\"/"/g;
  118. $content = IkiWiki::filter($page, $params{destpage}, $content);
  119. if ($config{comments_allowdirectives}) {
  120. $content = IkiWiki::preprocess($page, $params{destpage},
  121. $content);
  122. }
  123. # no need to bother with htmlize if it's just HTML
  124. $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
  125. if defined $format;
  126. IkiWiki::run_hooks(sanitize => sub {
  127. $content = shift->(
  128. page => $page,
  129. destpage => $params{destpage},
  130. content => $content,
  131. );
  132. });
  133. # set metadata, possibly overriding [[!meta]] directives from the
  134. # comment itself
  135. my $commentuser;
  136. my $commentip;
  137. my $commentauthor;
  138. my $commentauthorurl;
  139. my $commentopenid;
  140. if (defined $params{username}) {
  141. $commentuser = $params{username};
  142. my $oiduser = eval { IkiWiki::openiduser($commentuser) };
  143. if (defined $oiduser) {
  144. # looks like an OpenID
  145. $commentauthorurl = $commentuser;
  146. $commentauthor = $oiduser;
  147. $commentopenid = $commentuser;
  148. }
  149. else {
  150. $commentauthorurl = IkiWiki::cgiurl(
  151. do => 'commenter',
  152. page => (length $config{userdir}
  153. ? "$config{userdir}/$commentuser"
  154. : "$commentuser"));
  155. $commentauthor = $commentuser;
  156. }
  157. }
  158. else {
  159. if (defined $params{ip}) {
  160. $commentip = $params{ip};
  161. }
  162. $commentauthor = gettext("Anonymous");
  163. }
  164. $commentstate{$page}{commentuser} = $commentuser;
  165. $commentstate{$page}{commentopenid} = $commentopenid;
  166. $commentstate{$page}{commentip} = $commentip;
  167. $commentstate{$page}{commentauthor} = $commentauthor;
  168. $commentstate{$page}{commentauthorurl} = $commentauthorurl;
  169. if (! defined $pagestate{$page}{meta}{author}) {
  170. $pagestate{$page}{meta}{author} = $commentauthor;
  171. }
  172. if (! defined $pagestate{$page}{meta}{authorurl}) {
  173. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  174. }
  175. if ($config{comments_allowauthor}) {
  176. if (defined $params{claimedauthor}) {
  177. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  178. }
  179. if (defined $params{url}) {
  180. my $url=$params{url};
  181. eval q{use URI::Heuristic};
  182. if (! $@) {
  183. $url=URI::Heuristic::uf_uristr($url);
  184. }
  185. if (safeurl($url)) {
  186. $pagestate{$page}{meta}{authorurl} = $url;
  187. }
  188. }
  189. }
  190. else {
  191. $pagestate{$page}{meta}{author} = $commentauthor;
  192. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  193. }
  194. if (defined $params{subject}) {
  195. $pagestate{$page}{meta}{title} = $params{subject};
  196. }
  197. if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
  198. $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
  199. "#".$params{page};
  200. }
  201. eval q{use Date::Parse};
  202. if (! $@) {
  203. my $time = str2time($params{date});
  204. $IkiWiki::pagectime{$page} = $time if defined $time;
  205. }
  206. return $content;
  207. }
  208. # This is exactly the same as recentchanges_link :-(
  209. sub linkcgi ($) {
  210. my $cgi=shift;
  211. if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
  212. my $page=decode_utf8($cgi->param("page"));
  213. if (! defined $page) {
  214. error("missing page parameter");
  215. }
  216. IkiWiki::loadindex();
  217. my $link=bestlink("", $page);
  218. if (! length $link) {
  219. print "Content-type: text/html\n\n";
  220. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  221. "<p>".
  222. sprintf(gettext("The page %s does not exist."),
  223. htmllink("", "", $page)).
  224. "</p>");
  225. }
  226. else {
  227. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  228. }
  229. exit;
  230. }
  231. }
  232. sub sessioncgi ($$) {
  233. my $cgi=shift;
  234. my $session=shift;
  235. my $do = $cgi->param('do');
  236. if ($do eq 'comment') {
  237. editcomment($cgi, $session);
  238. }
  239. elsif ($do eq 'commentmoderation') {
  240. commentmoderation($cgi, $session);
  241. }
  242. }
  243. # Mostly cargo-culted from IkiWiki::plugin::editpage
  244. sub editcomment ($$) {
  245. my $cgi=shift;
  246. my $session=shift;
  247. IkiWiki::decode_cgi_utf8($cgi);
  248. eval q{use CGI::FormBuilder};
  249. error($@) if $@;
  250. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  251. my $form = CGI::FormBuilder->new(
  252. fields => [qw{do sid page subject editcontent type author url}],
  253. charset => 'utf-8',
  254. method => 'POST',
  255. required => [qw{editcontent}],
  256. javascript => 0,
  257. params => $cgi,
  258. action => $config{cgiurl},
  259. header => 0,
  260. table => 0,
  261. template => scalar IkiWiki::template_params('editcomment.tmpl'),
  262. );
  263. IkiWiki::decode_form_utf8($form);
  264. IkiWiki::run_hooks(formbuilder_setup => sub {
  265. shift->(title => "comment", form => $form, cgi => $cgi,
  266. session => $session, buttons => \@buttons);
  267. });
  268. IkiWiki::decode_form_utf8($form);
  269. my $type = $form->param('type');
  270. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  271. $type = IkiWiki::possibly_foolish_untaint($type);
  272. }
  273. else {
  274. $type = $config{default_pageext};
  275. }
  276. my @page_types;
  277. if (exists $IkiWiki::hooks{htmlize}) {
  278. @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
  279. }
  280. $form->field(name => 'do', type => 'hidden');
  281. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  282. force => 1);
  283. $form->field(name => 'page', type => 'hidden');
  284. $form->field(name => 'subject', type => 'text', size => 72);
  285. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  286. $form->field(name => "type", value => $type, force => 1,
  287. type => 'select', options => \@page_types);
  288. $form->tmpl_param(username => $session->param('name'));
  289. if ($config{comments_allowauthor} and
  290. ! defined $session->param('name')) {
  291. $form->tmpl_param(allowauthor => 1);
  292. $form->field(name => 'author', type => 'text', size => '40');
  293. $form->field(name => 'url', type => 'text', size => '40');
  294. }
  295. else {
  296. $form->tmpl_param(allowauthor => 0);
  297. $form->field(name => 'author', type => 'hidden', value => '',
  298. force => 1);
  299. $form->field(name => 'url', type => 'hidden', value => '',
  300. force => 1);
  301. }
  302. # The untaint is OK (as in editpage) because we're about to pass
  303. # it to file_pruned anyway
  304. my $page = $form->field('page');
  305. $page = IkiWiki::possibly_foolish_untaint($page);
  306. if (! defined $page || ! length $page ||
  307. IkiWiki::file_pruned($page, $config{srcdir})) {
  308. error(gettext("bad page name"));
  309. }
  310. my $baseurl = urlto($page, undef, 1);
  311. $form->title(sprintf(gettext("commenting on %s"),
  312. IkiWiki::pagetitle($page)));
  313. $form->tmpl_param('helponformattinglink',
  314. htmllink($page, $page, 'ikiwiki/formatting',
  315. noimageinline => 1,
  316. linktext => 'FormattingHelp'),
  317. allowdirectives => $config{allow_directives});
  318. if ($form->submitted eq CANCEL) {
  319. # bounce back to the page they wanted to comment on, and exit.
  320. # CANCEL need not be considered in future
  321. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  322. exit;
  323. }
  324. if (not exists $pagesources{$page}) {
  325. error(sprintf(gettext(
  326. "page '%s' doesn't exist, so you can't comment"),
  327. $page));
  328. }
  329. if (pagespec_match($page, $config{comments_closed_pagespec},
  330. location => $page)) {
  331. error(sprintf(gettext(
  332. "comments on page '%s' are closed"),
  333. $page));
  334. }
  335. # Set a flag to indicate that we're posting a comment,
  336. # so that postcomment() can tell it should match.
  337. $postcomment=1;
  338. IkiWiki::check_canedit($page, $cgi, $session);
  339. $postcomment=0;
  340. my $location=unique_comment_location($page, $config{srcdir});
  341. my $content = "[[!_comment format=$type\n";
  342. # FIXME: handling of double quotes probably wrong?
  343. if (defined $session->param('name')) {
  344. my $username = $session->param('name');
  345. $username =~ s/"/&quot;/g;
  346. $content .= " username=\"$username\"\n";
  347. }
  348. elsif (defined $ENV{REMOTE_ADDR}) {
  349. my $ip = $ENV{REMOTE_ADDR};
  350. if ($ip =~ m/^([.0-9]+)$/) {
  351. $content .= " ip=\"$1\"\n";
  352. }
  353. }
  354. if ($config{comments_allowauthor}) {
  355. my $author = $form->field('author');
  356. if (defined $author && length $author) {
  357. $author =~ s/"/&quot;/g;
  358. $content .= " claimedauthor=\"$author\"\n";
  359. }
  360. my $url = $form->field('url');
  361. if (defined $url && length $url) {
  362. $url =~ s/"/&quot;/g;
  363. $content .= " url=\"$url\"\n";
  364. }
  365. }
  366. my $subject = $form->field('subject');
  367. if (defined $subject && length $subject) {
  368. $subject =~ s/"/&quot;/g;
  369. $content .= " subject=\"$subject\"\n";
  370. }
  371. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  372. my $editcontent = $form->field('editcontent') || '';
  373. $editcontent =~ s/\r\n/\n/g;
  374. $editcontent =~ s/\r/\n/g;
  375. $editcontent =~ s/"/\\"/g;
  376. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  377. # This is essentially a simplified version of editpage:
  378. # - the user does not control the page that's created, only the parent
  379. # - it's always a create operation, never an edit
  380. # - this means that conflicts should never happen
  381. # - this means that if they do, rocks fall and everyone dies
  382. if ($form->submitted eq PREVIEW) {
  383. my $preview=previewcomment($content, $location, $page, time);
  384. IkiWiki::run_hooks(format => sub {
  385. $preview = shift->(page => $page,
  386. content => $preview);
  387. });
  388. $form->tmpl_param(page_preview => $preview);
  389. }
  390. else {
  391. $form->tmpl_param(page_preview => "");
  392. }
  393. if ($form->submitted eq POST_COMMENT && $form->validate) {
  394. IkiWiki::checksessionexpiry($cgi, $session);
  395. $postcomment=1;
  396. my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
  397. subject => $form->field('subject'),
  398. $config{comments_allowauthor} ? (
  399. author => $form->field('author'),
  400. url => $form->field('url'),
  401. ) : (),
  402. page => $location,
  403. cgi => $cgi,
  404. session => $session,
  405. nonfatal => 1,
  406. );
  407. $postcomment=0;
  408. if (! $ok) {
  409. my $penddir=$config{wikistatedir}."/comments_pending";
  410. $location=unique_comment_location($page, $penddir);
  411. writefile("$location._comment", $penddir, $content);
  412. IkiWiki::printheader($session);
  413. print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
  414. "<p>".
  415. gettext("Your comment will be posted after moderator review"),
  416. "</p>");
  417. exit;
  418. }
  419. # FIXME: could probably do some sort of graceful retry
  420. # on error? Would require significant unwinding though
  421. my $file = "$location._comment";
  422. writefile($file, $config{srcdir}, $content);
  423. my $conflict;
  424. if ($config{rcs} and $config{comments_commit}) {