summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: f1a9b19394701ea82558513ecc423b451ba2ddc4 (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=IkiWiki::linkpage(
  167. IkiWiki::possibly_foolish_untaint(
  168. attachment_location($form->field('page')).
  169. IkiWiki::basename($filename)));
  170. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  171. error(gettext("bad attachment filename"));
  172. }
  173. # Check that the user is allowed to edit a page with the
  174. # name of the attachment.
  175. IkiWiki::check_canedit($filename, $q, $session, 1);
  176. # And that the attachment itself is acceptable.
  177. check_canattach($session, $filename, $tempfile);
  178. # Needed for fast_file_copy and for rendering below.
  179. require IkiWiki::Render;
  180. # Move the attachment into place.
  181. # Try to use a fast rename; fall back to copying.
  182. IkiWiki::prep_writefile($filename, $config{srcdir});
  183. unlink($config{srcdir}."/".$filename);
  184. if (rename($tempfile, $config{srcdir}."/".$filename)) {
  185. # The temp file has tight permissions; loosen up.
  186. chmod(0666 & ~umask, $config{srcdir}."/".$filename);
  187. }
  188. else {
  189. my $fh=$q->upload('attachment');
  190. if (! defined $fh || ! ref $fh) {
  191. # needed by old CGI versions
  192. $fh=$q->param('attachment');
  193. if (! defined $fh || ! ref $fh) {
  194. # even that doesn't always work,
  195. # fall back to opening the tempfile
  196. $fh=undef;
  197. open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
  198. }
  199. }
  200. binmode($fh);
  201. writefile($filename, $config{srcdir}, undef, 1, sub {
  202. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  203. });
  204. }
  205. # Check the attachment in and trigger a wiki refresh.
  206. if ($config{rcs}) {
  207. IkiWiki::rcs_add($filename);
  208. IkiWiki::disable_commit_hook();
  209. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  210. IkiWiki::rcs_prepedit($filename),
  211. $session->param("name"), $ENV{REMOTE_ADDR});
  212. IkiWiki::enable_commit_hook();
  213. IkiWiki::rcs_update();
  214. }
  215. IkiWiki::refresh();
  216. IkiWiki::saveindex();
  217. }
  218. elsif ($form->submitted eq "Insert Links") {
  219. my $page=quotemeta($q->param("page"));
  220. my $add="";
  221. foreach my $f ($q->param("attachment_select")) {
  222. $f=~s/^$page\///;
  223. $add.="[[$f]]\n";
  224. }
  225. $form->field(name => 'editcontent',
  226. value => $form->field('editcontent')."\n\n".$add,
  227. force => 1) if length $add;
  228. }
  229. # Generate the attachment list only after having added any new
  230. # attachments.
  231. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  232. } # }}}
  233. sub attachment_location ($) { #{{{
  234. my $page=shift;
  235. # Put the attachment in a subdir of the page it's attached
  236. # to, unless that page is an "index" page.
  237. $page=~s/(^|\/)index//;
  238. $page.="/" if length $page;
  239. return $page;
  240. } #}}}
  241. sub attachment_list ($) { #{{{
  242. my $page=shift;
  243. my $loc=attachment_location($page);
  244. my @ret;
  245. foreach my $f (values %pagesources) {
  246. if (! defined IkiWiki::pagetype($f) &&
  247. $f=~m/^\Q$loc\E[^\/]+$/ &&
  248. -e "$config{srcdir}/$f") {
  249. push @ret, {
  250. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
  251. link => htmllink($page, $page, $f, noimageinline => 1),
  252. size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
  253. mtime => displaytime($IkiWiki::pagemtime{$f}),
  254. mtime_raw => $IkiWiki::pagemtime{$f},
  255. };
  256. }
  257. }
  258. # Sort newer attachments to the top of the list, so a newly-added
  259. # attachment appears just before the form used to add it.
  260. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  261. } #}}}
  262. package IkiWiki::PageSpec;
  263. sub match_user ($$;@) { #{{{
  264. shift;
  265. my $user=shift;
  266. my %params=@_;
  267. if (! exists $params{user}) {
  268. return IkiWiki::FailReason->new("no user specified");
  269. }
  270. if (defined $params{user} && lc $params{user} eq lc $user) {
  271. return IkiWiki::SuccessReason->new("user is $user");
  272. }
  273. elsif (! defined $params{user}) {
  274. return IkiWiki::FailReason->new("not logged in");
  275. }
  276. else {
  277. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  278. }
  279. } #}}}
  280. sub match_admin ($$;@) { #{{{
  281. shift;
  282. shift;
  283. my %params=@_;
  284. if (! exists $params{user}) {
  285. return IkiWiki::FailReason->new("no user specified");
  286. }
  287. if (defined $params{user} && IkiWiki::is_admin($params{user})) {
  288. return IkiWiki::SuccessReason->new("user is an admin");
  289. }
  290. elsif (! defined $params{user}) {
  291. return IkiWiki::FailReason->new("not logged in");
  292. }
  293. else {
  294. return IkiWiki::FailReason->new("user is not an admin");
  295. }
  296. } #}}}
  297. sub match_ip ($$;@) { #{{{
  298. shift;
  299. my $ip=shift;
  300. my %params=@_;
  301. if (! exists $params{ip}) {
  302. return IkiWiki::FailReason->new("no IP specified");
  303. }
  304. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  305. return IkiWiki::SuccessReason->new("IP is $ip");
  306. }
  307. else {
  308. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  309. }
  310. } #}}}
  311. 1