summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/editpage.pm
blob: d12c6732de2b317da24fa0360967ca6fb19acb00 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::editpage;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use open qw{:utf8 :std};
  7. sub import { #{{{
  8. hook(type => "getsetup", id => "editpage", call => \&getsetup);
  9. hook(type => "refresh", id => "editpage", call => \&refresh);
  10. hook(type => "sessioncgi", id => "editpage", call => \&IkiWiki::cgi_editpage);
  11. } # }}}
  12. sub getsetup () { #{{{
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 1,
  17. },
  18. } #}}}
  19. sub refresh () {
  20. if (exists $wikistate{editpage} && exists $wikistate{editpage}{previews}) {
  21. # Expire old preview files after one hour.
  22. my $expire=time - (60 * 60);
  23. my @previews;
  24. foreach my $file (@{$wikistate{editpage}{previews}}) {
  25. my $mtime=(stat("$config{destdir}/$file"))[9];
  26. if (defined $mtime && $mtime <= $expire) {
  27. debug(sprintf(gettext("removing old preview %s"), $file));
  28. IkiWiki::prune("$config{destdir}/$file");
  29. }
  30. elsif (defined $mtime) {
  31. push @previews, $file;
  32. }
  33. }
  34. $wikistate{editpage}{previews}=\@previews;
  35. }
  36. }
  37. # Back to ikiwiki namespace for the rest, this code is very much
  38. # internal to ikiwiki even though it's separated into a plugin,
  39. # and other plugins use the functions below.
  40. package IkiWiki;
  41. sub check_canedit ($$$;$) { #{{{
  42. my $page=shift;
  43. my $q=shift;
  44. my $session=shift;
  45. my $nonfatal=shift;
  46. my $canedit;
  47. run_hooks(canedit => sub {
  48. return if defined $canedit;
  49. my $ret=shift->($page, $q, $session);
  50. if (defined $ret) {
  51. if ($ret eq "") {
  52. $canedit=1;
  53. }
  54. elsif (ref $ret eq 'CODE') {
  55. $ret->() unless $nonfatal;
  56. $canedit=0;
  57. }
  58. elsif (defined $ret) {
  59. error($ret) unless $nonfatal;
  60. $canedit=0;
  61. }
  62. }
  63. });
  64. return $canedit;
  65. } #}}}
  66. sub cgi_editpage ($$) { #{{{
  67. my $q=shift;
  68. my $session=shift;
  69. my $do=$q->param('do');
  70. return unless $do eq 'create' || $do eq 'edit';
  71. decode_cgi_utf8($q);
  72. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  73. my @buttons=("Save Page", "Preview", "Cancel");
  74. eval q{use CGI::FormBuilder};
  75. error($@) if $@;
  76. my $form = CGI::FormBuilder->new(
  77. fields => \@fields,
  78. charset => "utf-8",
  79. method => 'POST',
  80. required => [qw{editcontent}],
  81. javascript => 0,
  82. params => $q,
  83. action => $config{cgiurl},
  84. header => 0,
  85. table => 0,
  86. template => scalar template_params("editpage.tmpl"),
  87. wikiname => $config{wikiname},
  88. );
  89. decode_form_utf8($form);
  90. run_hooks(formbuilder_setup => sub {
  91. shift->(form => $form, cgi => $q, session => $session,
  92. buttons => \@buttons);
  93. });
  94. decode_form_utf8($form);
  95. # This untaint is safe because we check file_pruned and
  96. # wiki_file_regexp.
  97. my ($page)=$form->field('page')=~/$config{wiki_file_regexp}/;
  98. $page=possibly_foolish_untaint($page);
  99. my $absolute=($page =~ s#^/+##);
  100. if (! defined $page || ! length $page ||
  101. file_pruned($page, $config{srcdir})) {
  102. error("bad page name");
  103. }
  104. my $baseurl = urlto($page, undef, 1);
  105. my $from;
  106. if (defined $form->field('from')) {
  107. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  108. }
  109. my $file;
  110. my $type;
  111. if (exists $pagesources{$page} && $form->field("do") ne "create") {
  112. $file=$pagesources{$page};
  113. $type=pagetype($file);
  114. if (! defined $type || $type=~/^_/) {
  115. error(sprintf(gettext("%s is not an editable page"), $page));
  116. }
  117. if (! $form->submitted) {
  118. $form->field(name => "rcsinfo",
  119. value => rcs_prepedit($file), force => 1);
  120. }
  121. $form->field(name => "editcontent", validate => '/.*/');
  122. }
  123. else {
  124. $type=$form->param('type');
  125. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  126. $type=possibly_foolish_untaint($type);
  127. }
  128. elsif (defined $from && exists $pagesources{$from}) {
  129. # favor the type of linking page
  130. $type=pagetype($pagesources{$from});
  131. }
  132. $type=$config{default_pageext} unless defined $type;
  133. $file=$page.".".$type;
  134. if (! $form->submitted) {
  135. $form->field(name => "rcsinfo", value => "", force => 1);
  136. }
  137. $form->field(name => "editcontent", validate => '/.+/');
  138. }
  139. $form->field(name => "do", type => 'hidden');
  140. $form->field(name => "sid", type => "hidden", value => $session->id,
  141. force => 1);
  142. $form->field(name => "from", type => 'hidden');
  143. $form->field(name => "rcsinfo", type => 'hidden');
  144. $form->field(name => "subpage", type => 'hidden');
  145. $form->field(name => "page", value => $page, force => 1);
  146. $form->field(name => "type", value => $type, force => 1);
  147. $form->field(name => "comments", type => "text", size => 80);
  148. $form->field(name => "editcontent", type => "textarea", rows => 20,
  149. cols => 80);
  150. $form->tmpl_param("can_commit", $config{rcs});
  151. $form->tmpl_param("indexlink", indexlink());
  152. $form->tmpl_param("helponformattinglink",
  153. htmllink($page, $page, "ikiwiki/formatting",
  154. noimageinline => 1,
  155. linktext => "FormattingHelp"));
  156. if ($form->submitted eq "Cancel") {
  157. if ($form->field("do") eq "create" && defined $from) {
  158. redirect($q, urlto($from, undef, 1));
  159. }
  160. elsif ($form->field("do") eq "create") {
  161. redirect($q, $config{url});
  162. }
  163. else {
  164. redirect($q, urlto($page, undef, 1));
  165. }
  166. exit;
  167. }
  168. elsif ($form->submitted eq "Preview") {
  169. my $new=not exists $pagesources{$page};
  170. if ($new) {
  171. # temporarily record its type
  172. $pagesources{$page}=$page.".".$type;
  173. }
  174. my %wasrendered=map { $_ => 1 } @{$renderedfiles{$page}};
  175. my $content=$form->field('editcontent');
  176. run_hooks(editcontent => sub {
  177. $content=shift->(
  178. content => $content,
  179. page => $page,
  180. cgi => $q,
  181. session => $session,
  182. );
  183. });
  184. my $preview=htmlize($page, $page, $type,
  185. linkify($page, $page,
  186. preprocess($page, $page,
  187. filter($page, $page, $content), 0, 1)));
  188. run_hooks(format => sub {
  189. $preview=shift->(
  190. page => $page,
  191. content => $preview,
  192. );
  193. });
  194. $form->tmpl_param("page_preview", $preview);
  195. if ($new) {
  196. delete $pagesources{$page};
  197. }
  198. # Previewing may have created files on disk.
  199. # Keep a list of these to be deleted later.
  200. my %previews = map { $_ => 1 } @{$wikistate{editpage}{previews}};
  201. foreach my $file (@{$renderedfiles{$page}}) {
  202. $previews{$file}=1 unless $wasrendered{$file};
  203. }
  204. @{$wikistate{editpage}{previews}} = keys %previews;
  205. saveindex();
  206. }
  207. elsif ($form->submitted eq "Save Page") {
  208. $form->tmpl_param("page_preview", "");
  209. }
  210. if ($form->submitted ne "Save Page" || ! $form->validate) {
  211. if ($form->field("do") eq "create") {
  212. my @page_locs;
  213. my $best_loc;
  214. if (! defined $from || ! length $from ||
  215. $from ne $form->field('from') ||
  216. file_pruned($from, $config{srcdir}) ||
  217. $from=~/^\// ||
  218. $absolute ||
  219. $form->submitted eq "Preview") {
  220. @page_locs=$best_loc=$page;
  221. }
  222. else {
  223. my $dir=$from."/";
  224. $dir=~s![^/]+/+$!!;
  225. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  226. $page eq gettext('discussion')) {
  227. $best_loc="$from/$page";
  228. }
  229. else {
  230. $best_loc=$dir.$page;
  231. }
  232. push @page_locs, $dir.$page;
  233. push @page_locs, "$from/$page";
  234. while (length $dir) {
  235. $dir=~s![^/]+/+$!!;
  236. push @page_locs, $dir.$page;
  237. }
  238. push @page_locs, "$config{userdir}/$page"
  239. if length $config{userdir};
  240. }
  241. @page_locs = grep {
  242. ! exists $pagecase{lc $_}
  243. } @page_locs;
  244. if (! @page_locs) {
  245. # hmm, someone else made the page in the
  246. # meantime?
  247. if ($form->submitted eq "Preview") {
  248. # let them go ahead with the edit
  249. # and resolve the conflict at save
  250. # time
  251. @page_locs=$page;
  252. }
  253. else {
  254. redirect($q, urlto($page, undef, 1));
  255. exit;
  256. }
  257. }
  258. my @editable_locs = grep {
  259. check_canedit($_, $q, $session, 1)
  260. } @page_locs;
  261. if (! @editable_locs) {
  262. # let it throw an error this time
  263. map { check_canedit($_, $q, $session) } @page_locs;
  264. }
  265. my @page_types;
  266. if (exists $hooks{htmlize}) {
  267. @page_types=grep { !/^_/ }
  268. keys %{$hooks{htmlize}};
  269. }
  270. $form->tmpl_param("page_select", 1);
  271. $form->field(name => "page", type => 'select',
  272. options => [ map { [ $_, pagetitle($_, 1) ] } @editable_locs ],
  273. value => $best_loc);
  274. $form->field(name => "type", type => 'select',
  275. options => \@page_types);
  276. $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
  277. }
  278. elsif ($form->field("do") eq "edit") {
  279. check_canedit($page, $q, $session);
  280. if (! defined $form->field('editcontent') ||
  281. ! length $form->field('editcontent')) {
  282. my $content="";
  283. if (exists $pagesources{$page}) {
  284. $content=readfile(srcfile($pagesources{$page}));
  285. $content=~s/\n/\r\n/g;
  286. }
  287. $form->field(name => "editcontent", value => $content,
  288. force => 1);
  289. }
  290. $form->tmpl_param("page_select", 0);
  291. $form->field(name => "page", type => 'hidden');
  292. $form->field(name => "type", type => 'hidden');
  293. $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
  294. }
  295. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  296. }
  297. else {
  298. # save page
  299. check_canedit($page, $q, $session);
  300. # The session id is stored on the form and checked to
  301. # guard against CSRF. But only if the user is logged in,
  302. # as anonok can allow anonymous edits.
  303. if (defined $session->param("name")) {
  304. my $sid=$q->param('sid');
  305. if (! defined $sid || $sid ne $session->id) {
  306. error(gettext("Your login session has expired."));
  307. }
  308. }
  309. my $exists=-e "$config{srcdir}/$file";
  310. if ($form->field("do") ne "create" && ! $exists &&
  311. ! defined srcfile($file, 1)) {
  312. $form->tmpl_param("message", template("editpagegone.tmpl")->output);
  313. $form->field(name => "do", value => "create", force => 1);
  314. $form->tmpl_param("page_select", 0);
  315. $form->field(name => "page", type => 'hidden');
  316. $form->field(name => "type", type => 'hidden');
  317. $form->title(sprintf(gettext("editing %s"), $page));
  318. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  319. exit;
  320. }
  321. elsif ($form->field("do") eq "create" && $exists) {
  322. $form->tmpl_param("message", template("editcreationconflict.tmpl")->output);
  323. $form->field(name => "do", value => "edit", force => 1);
  324. $form->tmpl_param("page_select", 0);
  325. $form->field(name => "page", type => 'hidden');
  326. $form->field(name => "type", type => 'hidden');
  327. $form->title(sprintf(gettext("editing %s"), $page));
  328. $form->field("editcontent",
  329. value => readfile("$config{srcdir}/$file").
  330. "\n\n\n".$form->field("editcontent"),
  331. force => 1);
  332. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  333. exit;
  334. }
  335. my $content=$form->field('editcontent');
  336. run_hooks(editcontent => sub {
  337. $content=shift->(
  338. content => $content,
  339. page => $page,
  340. cgi => $q,
  341. session => $session,
  342. );
  343. });
  344. $content=~s/\r\n/\n/g;
  345. $content=~s/\r/\n/g;
  346. $content.="\n" if $content !~ /\n$/;
  347. $config{cgi}=0; # avoid cgi error message
  348. eval { writefile($file, $config{srcdir}, $content) };
  349. $config{cgi}=1;
  350. if ($@) {
  351. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  352. force => 1);
  353. my $mtemplate=template("editfailedsave.tmpl");
  354. $mtemplate->param(error_message => $@);
  355. $form->tmpl_param("message", $mtemplate->output);
  356. $form->field("editcontent", value => $content, force => 1);
  357. $form->tmpl_param("page_select", 0);
  358. $form->field(name => "page", type => 'hidden');
  359. $form->field(name => "type", type => 'hidden');
  360. $form->title(sprintf(gettext("editing %s"), $page));
  361. showform($form, \@buttons, $session, $q,
  362. forcebaseurl => $baseurl);
  363. exit;
  364. }
  365. my $conflict;
  366. if ($config{rcs}) {
  367. my $message="";
  368. if (defined $form->field('comments') &&
  369. length $form->field('comments')) {
  370. $message=$form->field('comments');
  371. }
  372. if (! $exists) {
  373. rcs_add($file);
  374. }
  375. # Prevent deadlock with post-commit hook by
  376. # signaling to it that it should not try to
  377. # do anything.
  378. disable_commit_hook();
  379. $conflict=rcs_commit($file, $message,
  380. $form->field("rcsinfo"),
  381. $session->param("name"), $ENV{REMOTE_ADDR});
  382. enable_commit_hook();
  383. rcs_update();
  384. }
  385. # Refresh even if there was a conflict, since other changes
  386. # may have been committed while the post-commit hook was
  387. # disabled.
  388. require IkiWiki::Render;
  389. refresh();
  390. saveindex();
  391. if (defined $conflict) {
  392. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  393. force => 1);
  394. $form->tmpl_param("message", template("editconflict.tmpl")->output);
  395. $form->field("editcontent", value => $conflict, force => 1);
  396. $form->field("do", "edit", force => 1);
  397. $form->tmpl_param("page_select", 0);
  398. $form->field(name => "page", type => 'hidden');
  399. $form->field(name => "type", type => 'hidden');
  400. $form->title(sprintf(gettext("editing %s"), $page));
  401. showform($form, \@buttons, $session, $q,
  402. forcebaseurl => $baseurl);
  403. }
  404. else {
  405. # The trailing question mark tries to avoid broken
  406. # caches and get the most recent version of the page.
  407. redirect($q, urlto($page, undef, 1)."?updated");
  408. }
  409. }
  410. exit;
  411. } #}}}
  412. 1