summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 8379928418fd997922a574bc59f68c79415251d4 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::attachment;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "getsetup", id => "attachment", call => \&getsetup);
  8. hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
  9. hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
  10. hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
  11. IkiWiki::loadplugin("filecheck");
  12. } # }}}
  13. sub getsetup () { #{{{
  14. return
  15. plugin => {
  16. safe => 1,
  17. rebuild => 0,
  18. },
  19. allowed_attachments => {
  20. type => "pagespec",
  21. example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
  22. description => "enhanced PageSpec specifying what attachments are allowed",
  23. link => "ikiwiki/PageSpec/attachment",
  24. safe => 1,
  25. rebuild => 0,
  26. },
  27. virus_checker => {
  28. type => "string",
  29. example => "clamdscan -",
  30. description => "virus checker program (reads STDIN, returns nonzero if virus found)",
  31. safe => 0, # executed
  32. rebuild => 0,
  33. },
  34. } #}}}
  35. sub check_canattach ($$;$) { #{{{
  36. my $session=shift;
  37. my $dest=shift; # where it's going to be put, under the srcdir
  38. my $file=shift; # the path to the attachment currently
  39. # Don't allow an attachment to be uploaded with the same name as an
  40. # existing page.
  41. if (exists $IkiWiki::pagesources{$dest} &&
  42. $IkiWiki::pagesources{$dest} ne $dest) {
  43. error(sprintf(gettext("there is already a page named %s"), $dest));
  44. }
  45. # Use a special pagespec to test that the attachment is valid.
  46. my $allowed=1;
  47. if (defined $config{allowed_attachments} &&
  48. length $config{allowed_attachments}) {
  49. $allowed=pagespec_match($dest,
  50. $config{allowed_attachments},
  51. file => $file,
  52. user => $session->param("name"),
  53. ip => $ENV{REMOTE_ADDR},
  54. );
  55. }
  56. # XXX deprecated, should be removed eventually
  57. if ($allowed) {
  58. foreach my $admin (@{$config{adminuser}}) {
  59. my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
  60. if (defined $allowed_attachments &&
  61. length $allowed_attachments) {
  62. $allowed=pagespec_match($dest,
  63. $allowed_attachments,
  64. file => $file,
  65. user => $session->param("name"),
  66. ip => $ENV{REMOTE_ADDR},
  67. );
  68. last if $allowed;
  69. }
  70. }
  71. }
  72. if (! $allowed) {
  73. error(gettext("prohibited by allowed_attachments")." ($allowed)");
  74. }
  75. else {
  76. return 1;
  77. }
  78. } #}}}
  79. sub checkconfig () { #{{{
  80. $config{cgi_disable_uploads}=0;
  81. } #}}}
  82. sub formbuilder_setup (@) { #{{{
  83. my %params=@_;
  84. my $form=$params{form};
  85. my $q=$params{cgi};
  86. if (defined $form->field("do") && $form->field("do") eq "edit") {
  87. # Add attachment field, set type to multipart.
  88. $form->enctype(&CGI::MULTIPART);
  89. $form->field(name => 'attachment', type => 'file');
  90. # These buttons are not put in the usual place, so
  91. # are not added to the normal formbuilder button list.
  92. $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
  93. $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
  94. # Add the javascript from the toggle plugin;
  95. # the attachments interface uses it to toggle visibility.
  96. require IkiWiki::Plugin::toggle;
  97. $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
  98. # Start with the attachments interface toggled invisible,
  99. # but if it was used, keep it open.
  100. if ($form->submitted ne "Upload Attachment" &&
  101. (! defined $q->param("attachment_select") ||
  102. ! length $q->param("attachment_select"))) {
  103. $form->tmpl_param("attachments-class" => "toggleable");
  104. }
  105. else {
  106. $form->tmpl_param("attachments-class" => "toggleable-open");
  107. }
  108. }
  109. elsif ($form->title eq "preferences") {
  110. # XXX deprecated, should remove eventually
  111. my $session=$params{session};
  112. my $user_name=$session->param("name");
  113. $form->field(name => "allowed_attachments", size => 50,
  114. fieldset => "admin",
  115. comment => "deprecated; please move to allowed_attachments in setup file",
  116. );
  117. if (! IkiWiki::is_admin($user_name)) {
  118. $form->field(name => "allowed_attachments", type => "hidden");
  119. }
  120. if (! $form->submitted) {
  121. my $value=IkiWiki::userinfo_get($user_name, "allowed_attachments");
  122. if (length $value) {
  123. $form->field(name => "allowed_attachments", force => 1,
  124. value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
  125. }
  126. else {
  127. $form->field(name => "allowed_attachments", type => "hidden");
  128. }
  129. }
  130. if ($form->submitted && $form->submitted eq 'Save Preferences') {
  131. if (defined $form->field("allowed_attachments")) {
  132. IkiWiki::userinfo_set($user_name, "allowed_attachments",
  133. $form->field("allowed_attachments")) ||
  134. error("failed to set allowed_attachments");
  135. if (! length $form->field("allowed_attachments")) {
  136. $form->field(name => "allowed_attachments", type => "hidden");
  137. }
  138. }
  139. }
  140. }
  141. } #}}}
  142. sub formbuilder (@) { #{{{
  143. my %params=@_;
  144. my $form=$params{form};
  145. my $q=$params{cgi};
  146. return if ! defined $form->field("do") || $form->field("do") ne "edit";
  147. my $filename=$q->param('attachment');
  148. if (defined $filename && length $filename &&
  149. ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
  150. my $session=$params{session};
  151. # This is an (apparently undocumented) way to get the name
  152. # of the temp file that CGI writes the upload to.
  153. my $tempfile=$q->tmpFileName($filename);
  154. if (! defined $tempfile || ! length $tempfile) {
  155. # perl 5.8 needs an alternative, awful method
  156. if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
  157. foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
  158. $tempfile=$q->tmpFileName(\$key);
  159. last if defined $tempfile && length $tempfile;
  160. }
  161. }
  162. if (! defined $tempfile || ! length $tempfile) {
  163. error("CGI::tmpFileName failed to return the uploaded file name");
  164. }
  165. }
  166. $filename=linkpage(IkiWiki::possibly_foolish_untaint(
  167. attachment_location($form->field('page')).
  168. IkiWiki::basename($filename)));
  169. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  170. error(gettext("bad attachment filename"));
  171. }
  172. # Check that the user is allowed to edit a page with the
  173. # name of the attachment.
  174. IkiWiki::check_canedit($filename, $q, $session, 1);
  175. # And that the attachment itself is acceptable.
  176. check_canattach($session, $filename, $tempfile);
  177. # Needed for fast_file_copy and for rendering below.
  178. require IkiWiki::Render;
  179. # Move the attachment into place.
  180. # Try to use a fast rename; fall back to copying.
  181. IkiWiki::prep_writefile($filename, $config{srcdir});
  182. unlink($config{srcdir}."/".$filename);
  183. if (rename($tempfile, $config{srcdir}."/".$filename)) {
  184. # The temp file has tight permissions; loosen up.
  185. chmod(0666 & ~umask, $config{srcdir}."/".$filename);
  186. }
  187. else {
  188. my $fh=$q->upload('attachment');
  189. if (! defined $fh || ! ref $fh) {
  190. # needed by old CGI versions
  191. $fh=$q->param('attachment');
  192. if (! defined $fh || ! ref $fh) {
  193. # even that doesn't always work,
  194. # fall back to opening the tempfile
  195. $fh=undef;
  196. open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
  197. }
  198. }
  199. binmode($fh);
  200. writefile($filename, $config{srcdir}, undef, 1, sub {
  201. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  202. });
  203. }
  204. # Check the attachment in and trigger a wiki refresh.
  205. if ($config{rcs}) {
  206. IkiWiki::rcs_add($filename);
  207. IkiWiki::disable_commit_hook();
  208. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  209. IkiWiki::rcs_prepedit($filename),
  210. $session->param("name"), $ENV{REMOTE_ADDR});
  211. IkiWiki::enable_commit_hook();
  212. IkiWiki::rcs_update();
  213. }
  214. IkiWiki::refresh();
  215. IkiWiki::saveindex();
  216. }
  217. elsif ($form->submitted eq "Insert Links") {
  218. my $page=quotemeta($q->param("page"));
  219. my $add="";
  220. foreach my $f ($q->param("attachment_select")) {
  221. $f=~s/^$page\///;
  222. $add.="[[$f]]\n";
  223. }
  224. $form->field(name => 'editcontent',
  225. value => $form->field('editcontent')."\n\n".$add,
  226. force => 1) if length $add;
  227. }
  228. # Generate the attachment list only after having added any new
  229. # attachments.
  230. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  231. } # }}}
  232. sub attachment_location ($) { #{{{
  233. my $page=shift;
  234. # Put the attachment in a subdir of the page it's attached
  235. # to, unless that page is an "index" page.
  236. $page=~s/(^|\/)index//;
  237. $page.="/" if length $page;
  238. return $page;
  239. } #}}}
  240. sub attachment_list ($) { #{{{
  241. my $page=shift;
  242. my $loc=attachment_location($page);
  243. my @ret;
  244. foreach my $f (values %pagesources) {
  245. if (! defined pagetype($f) &&
  246. $f=~m/^\Q$loc\E[^\/]+$/ &&
  247. -e "$config{srcdir}/$f") {
  248. push @ret, {
  249. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
  250. link => htmllink($page, $page, $f, noimageinline => 1),
  251. size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
  252. mtime => displaytime($IkiWiki::pagemtime{$f}),
  253. mtime_raw => $IkiWiki::pagemtime{$f},
  254. };
  255. }
  256. }
  257. # Sort newer attachments to the top of the list, so a newly-added
  258. # attachment appears just before the form used to add it.
  259. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  260. } #}}}
  261. package IkiWiki::PageSpec;
  262. sub match_user ($$;@) { #{{{
  263. shift;
  264. my $user=shift;
  265. my %params=@_;
  266. if (! exists $params{user}) {
  267. return IkiWiki::FailReason->new("no user specified");
  268. }
  269. if (defined $params{user} && lc $params{user} eq lc $user) {
  270. return IkiWiki::SuccessReason->new("user is $user");
  271. }
  272. elsif (! defined $params{user}) {
  273. return IkiWiki::FailReason->new("not logged in");
  274. }
  275. else {
  276. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  277. }
  278. } #}}}
  279. sub match_admin ($$;@) { #{{{
  280. shift;
  281. shift;
  282. my %params=@_;
  283. if (! exists $params{user}) {
  284. return IkiWiki::FailReason->new("no user specified");
  285. }
  286. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  287. return IkiWiki::SuccessReason->new("user is an admin");
  288. }
  289. elsif (! defined $params{user}) {
  290. return IkiWiki::FailReason->new("not logged in");
  291. }
  292. else {
  293. return IkiWiki::FailReason->new("user is not an admin");
  294. }
  295. } #}}}
  296. sub match_ip ($$;@) { #{{{
  297. shift;
  298. my $ip=shift;
  299. my %params=@_;
  300. if (! exists $params{ip}) {
  301. return IkiWiki::FailReason->new("no IP specified");
  302. }
  303. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  304. return IkiWiki::SuccessReason->new("IP is $ip");
  305. }
  306. else {
  307. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  308. }
  309. } #}}}
  310. 1