summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/attachment.pm
blob: 2a662c0d12e949ae27f5ab10982e733fd0d6cee4 (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 => "checkconfig", id => "attachment", call => \&checkconfig);
  8. hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
  9. hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
  10. } # }}}
  11. sub check_canattach ($$;$) {
  12. my $session=shift;
  13. my $dest=shift; # where it's going to be put, under the srcdir
  14. my $file=shift; # the path to the attachment currently
  15. # Use a special pagespec to test that the attachment is valid.
  16. my $allowed=1;
  17. foreach my $admin (@{$config{adminuser}}) {
  18. my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
  19. if (defined $allowed_attachments &&
  20. length $allowed_attachments) {
  21. $allowed=pagespec_match($dest,
  22. $allowed_attachments,
  23. file => $file,
  24. user => $session->param("name"),
  25. ip => $ENV{REMOTE_ADDR},
  26. );
  27. last if $allowed;
  28. }
  29. }
  30. if (! $allowed) {
  31. error(gettext("attachment rejected")." ($allowed)");
  32. }
  33. else {
  34. return 1;
  35. }
  36. }
  37. sub checkconfig () { #{{{
  38. $config{cgi_disable_uploads}=0;
  39. } #}}}
  40. sub formbuilder_setup (@) { #{{{
  41. my %params=@_;
  42. my $form=$params{form};
  43. my $q=$params{cgi};
  44. if (defined $form->field("do") && $form->field("do") eq "edit") {
  45. # Add attachment field, set type to multipart.
  46. $form->enctype(&CGI::MULTIPART);
  47. $form->field(name => 'attachment', type => 'file');
  48. # These buttons are not put in the usual place, so
  49. # are not added to the normal formbuilder button list.
  50. $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
  51. $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
  52. # Add the javascript from the toggle plugin;
  53. # the attachments interface uses it to toggle visibility.
  54. require IkiWiki::Plugin::toggle;
  55. $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
  56. # Start with the attachments interface toggled invisible,
  57. # but if it was used, keep it open.
  58. if ($form->submitted ne "Upload Attachment" &&
  59. (! defined $q->param("attachment_select") ||
  60. ! length $q->param("attachment_select"))) {
  61. $form->tmpl_param("attachments-class" => "toggleable");
  62. }
  63. else {
  64. $form->tmpl_param("attachments-class" => "toggleable-open");
  65. }
  66. }
  67. elsif ($form->title eq "preferences") {
  68. my $session=$params{session};
  69. my $user_name=$session->param("name");
  70. $form->field(name => "allowed_attachments", size => 50,
  71. fieldset => "admin",
  72. comment => "(".
  73. htmllink("", "",
  74. "ikiwiki/PageSpec/attachment",
  75. noimageinline => 1,
  76. linktext => "Enhanced PageSpec",
  77. ).")"
  78. );
  79. if (! IkiWiki::is_admin($user_name)) {
  80. $form->field(name => "allowed_attachments", type => "hidden");
  81. }
  82. if (! $form->submitted) {
  83. $form->field(name => "allowed_attachments", force => 1,
  84. value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
  85. }
  86. if ($form->submitted && $form->submitted eq 'Save Preferences') {
  87. if (defined $form->field("allowed_attachments")) {
  88. IkiWiki::userinfo_set($user_name, "allowed_attachments",
  89. $form->field("allowed_attachments")) ||
  90. error("failed to set allowed_attachments");
  91. }
  92. }
  93. }
  94. } #}}}
  95. sub formbuilder (@) { #{{{
  96. my %params=@_;
  97. my $form=$params{form};
  98. my $q=$params{cgi};
  99. return if ! defined $form->field("do") || $form->field("do") ne "edit";
  100. my $filename=$q->param('attachment');
  101. if (defined $filename && length $filename &&
  102. ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
  103. my $session=$params{session};
  104. # This is an (apparently undocumented) way to get the name
  105. # of the temp file that CGI writes the upload to.
  106. my $tempfile=$q->tmpFileName($filename);
  107. if (! defined $tempfile || ! length $tempfile) {
  108. # perl 5.8 needs an alternative, awful method
  109. if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
  110. foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
  111. $tempfile=$q->tmpFileName(\$key);
  112. last if defined $tempfile && length $tempfile;
  113. }
  114. }
  115. if (! defined $tempfile || ! length $tempfile) {
  116. error("CGI::tmpFileName failed to return the uploaded file name");
  117. }
  118. }
  119. $filename=IkiWiki::titlepage(
  120. IkiWiki::possibly_foolish_untaint(
  121. attachment_location($form->field('page')).
  122. IkiWiki::basename($filename)));
  123. if (IkiWiki::file_pruned($filename, $config{srcdir})) {
  124. error(gettext("bad attachment filename"));
  125. }
  126. # Check that the user is allowed to edit a page with the
  127. # name of the attachment.
  128. IkiWiki::check_canedit($filename, $q, $session, 1);
  129. # And that the attachment itself is acceptable.
  130. check_canattach($session, $filename, $tempfile);
  131. # Needed for fast_file_copy and for rendering below.
  132. require IkiWiki::Render;
  133. # Move the attachment into place.
  134. # Try to use a fast rename; fall back to copying.
  135. IkiWiki::prep_writefile($filename, $config{srcdir});
  136. unlink($config{srcdir}."/".$filename);
  137. if (rename($tempfile, $config{srcdir}."/".$filename)) {
  138. # The temp file has tight permissions; loosen up.
  139. chmod(0666 & ~umask, $config{srcdir}."/".$filename);
  140. }
  141. else {
  142. my $fh=$q->upload('attachment');
  143. if (! defined $fh || ! ref $fh) {
  144. # needed by old CGI versions
  145. $fh=$q->param('attachment');
  146. if (! defined $fh || ! ref $fh) {
  147. # even that doesn't always work,
  148. # fall back to opening the tempfile
  149. $fh=undef;
  150. open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
  151. }
  152. }
  153. binmode($fh);
  154. writefile($filename, $config{srcdir}, undef, 1, sub {
  155. IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
  156. });
  157. }
  158. # Check the attachment in and trigger a wiki refresh.
  159. if ($config{rcs}) {
  160. IkiWiki::rcs_add($filename);
  161. IkiWiki::disable_commit_hook();
  162. IkiWiki::rcs_commit($filename, gettext("attachment upload"),
  163. IkiWiki::rcs_prepedit($filename),
  164. $session->param("name"), $ENV{REMOTE_ADDR});
  165. IkiWiki::enable_commit_hook();
  166. IkiWiki::rcs_update();
  167. }
  168. IkiWiki::refresh();
  169. IkiWiki::saveindex();
  170. }
  171. elsif ($form->submitted eq "Insert Links") {
  172. my $add="";
  173. foreach my $f ($q->param("attachment_select")) {
  174. $add.="[[$f]]\n";
  175. }
  176. $form->field(name => 'editcontent',
  177. value => $form->field('editcontent')."\n\n".$add,
  178. force => 1) if length $add;
  179. }
  180. # Generate the attachment list only after having added any new
  181. # attachments.
  182. $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
  183. } # }}}
  184. sub attachment_location ($) {
  185. my $page=shift;
  186. # Put the attachment in a subdir of the page it's attached
  187. # to, unless that page is an "index" page.
  188. $page=~s/(^|\/)index//;
  189. $page.="/" if length $page;
  190. return $page;
  191. }
  192. sub attachment_list ($) {
  193. my $page=shift;
  194. my $loc=attachment_location($page);
  195. my @ret;
  196. foreach my $f (values %pagesources) {
  197. if (! defined IkiWiki::pagetype($f) &&
  198. $f=~m/^\Q$loc\E[^\/]+$/ &&
  199. -e "$config{srcdir}/$f") {
  200. push @ret, {
  201. "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
  202. link => htmllink($page, $page, $f, noimageinline => 1),
  203. size => humansize((stat(_))[7]),
  204. mtime => displaytime($IkiWiki::pagemtime{$f}),
  205. mtime_raw => $IkiWiki::pagemtime{$f},
  206. };
  207. }
  208. }
  209. # Sort newer attachments to the top of the list, so a newly-added
  210. # attachment appears just before the form used to add it.
  211. return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
  212. }
  213. my %units=( # size in bytes
  214. B => 1,
  215. byte => 1,
  216. KB => 2 ** 10,
  217. kilobyte => 2 ** 10,
  218. K => 2 ** 10,
  219. KB => 2 ** 10,
  220. kilobyte => 2 ** 10,
  221. M => 2 ** 20,
  222. MB => 2 ** 20,
  223. megabyte => 2 ** 20,
  224. G => 2 ** 30,
  225. GB => 2 ** 30,
  226. gigabyte => 2 ** 30,
  227. T => 2 ** 40,
  228. TB => 2 ** 40,
  229. terabyte => 2 ** 40,
  230. P => 2 ** 50,
  231. PB => 2 ** 50,
  232. petabyte => 2 ** 50,
  233. E => 2 ** 60,
  234. EB => 2 ** 60,
  235. exabyte => 2 ** 60,
  236. Z => 2 ** 70,
  237. ZB => 2 ** 70,
  238. zettabyte => 2 ** 70,
  239. Y => 2 ** 80,
  240. YB => 2 ** 80,
  241. yottabyte => 2 ** 80,
  242. # ikiwiki, if you find you need larger data quantities, either modify
  243. # yourself to add them, or travel back in time to 2008 and kill me.
  244. # -- Joey
  245. );
  246. sub parsesize ($) { #{{{
  247. my $size=shift;
  248. no warnings;
  249. my $base=$size+0; # force to number
  250. use warnings;
  251. foreach my $unit (sort keys %units) {
  252. if ($size=~/[0-9\s]\Q$unit\E$/i) {
  253. return $base * $units{$unit};
  254. }
  255. }
  256. return $base;
  257. } #}}}
  258. sub humansize ($) { #{{{
  259. my $size=shift;
  260. foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
  261. if ($size / $units{$unit} > 0.25) {
  262. return (int($size / $units{$unit} * 10)/10).$unit;
  263. }
  264. }
  265. return $size; # near zero, or negative
  266. } #}}}
  267. package IkiWiki::PageSpec;
  268. sub match_maxsize ($$;@) { #{{{
  269. shift;
  270. my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  271. if ($@) {
  272. return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
  273. }
  274. my %params=@_;
  275. if (! exists $params{file}) {
  276. return IkiWiki::FailReason->new("no file specified");
  277. }
  278. if (-s $params{file} > $maxsize) {
  279. return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
  280. }
  281. else {
  282. return IkiWiki::SuccessReason->new("file not too large");
  283. }
  284. } #}}}
  285. sub match_minsize ($$;@) { #{{{
  286. shift;
  287. my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
  288. if ($@) {
  289. return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
  290. }
  291. my %params=@_;
  292. if (! exists $params{file}) {
  293. return IkiWiki::FailReason->new("no file specified");
  294. }
  295. if (-s $params{file} < $minsize) {
  296. return IkiWiki::FailReason->new("file too small");
  297. }
  298. else {
  299. return IkiWiki::SuccessReason->new("file not too small");
  300. }
  301. } #}}}
  302. sub match_mimetype ($$;@) { #{{{
  303. shift;
  304. my $wanted=shift;
  305. my %params=@_;
  306. if (! exists $params{file}) {
  307. return IkiWiki::FailReason->new("no file specified");
  308. }
  309. # Use ::magic to get the mime type, the idea is to only trust
  310. # data obtained by examining the actual file contents.
  311. eval q{use File::MimeInfo::Magic};
  312. if ($@) {
  313. return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
  314. }
  315. my $mimetype=File::MimeInfo::Magic::magic($params{file});
  316. if (! defined $mimetype) {
  317. $mimetype="unknown";
  318. }
  319. my $regexp=IkiWiki::glob2re($wanted);
  320. if ($mimetype!~/^$regexp$/i) {
  321. return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
  322. }
  323. else {
  324. return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
  325. }
  326. } #}}}
  327. sub match_virusfree ($$;@) { #{{{
  328. shift;
  329. my $wanted=shift;
  330. my %params=@_;
  331. if (! exists $params{file}) {
  332. return IkiWiki::FailReason->new("no file specified");
  333. }
  334. if (! exists $IkiWiki::config{virus_checker} ||
  335. ! length $IkiWiki::config{virus_checker}) {
  336. return IkiWiki::FailReason->new("no virus_checker configured");
  337. }
  338. # The file needs to be fed into the virus checker on stdin,
  339. # because the file is not world-readable, and if clamdscan is
  340. # used, clamd would fail to read it.
  341. eval q{use IPC::Open2};
  342. error($@) if $@;
  343. open (IN, "<", $params{file}) || return IkiWiki::FailReason->new("failed to read file");
  344. binmode(IN);
  345. my $sigpipe=0;
  346. $SIG{PIPE} = sub { $sigpipe=1 };
  347. my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
  348. my $reason=<CHECKER_OUT>;
  349. chomp $reason;
  350. 1 while (<CHECKER_OUT>);
  351. close(CHECKER_OUT);
  352. waitpid $pid, 0;
  353. $SIG{PIPE}="DEFAULT";
  354. if ($sigpipe || $?) {
  355. if (! length $reason) {
  356. $reason="virus checker $IkiWiki::config{virus_checker}; failed with no output";
  357. }
  358. return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
  359. }
  360. else {
  361. return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
  362. }
  363. } #}}}
  364. sub match_ispage ($$;@) { #{{{
  365. my $filename=shift;
  366. if (defined IkiWiki::pagetype($filename)) {
  367. return IkiWiki::SuccessReason->new("file is a wiki page");
  368. }
  369. else {
  370. return IkiWiki::FailReason->new("file is not a wiki page");
  371. }
  372. } #}}}
  373. sub match_user ($$;@) { #{{{
  374. shift;
  375. my $user=shift;
  376. my %params=@_;
  377. if (! exists $params{user}) {
  378. return IkiWiki::FailReason->new("no user specified");
  379. }
  380. if (defined $params{user} && lc $params{user} eq lc $user) {
  381. return IkiWiki::SuccessReason->new("user is $user");
  382. }
  383. elsif (! defined $params{user}) {
  384. return IkiWiki::FailReason->new("not logged in");
  385. }
  386. else {
  387. return IkiWiki::FailReason->new("user is $params{user}, not $user");
  388. }
  389. } #}}}
  390. sub match_ip ($$;@) { #{{{
  391. shift;
  392. my $ip=shift;
  393. my %params=@_;
  394. if (! exists $params{ip}) {
  395. return IkiWiki::FailReason->new("no IP specified");
  396. }
  397. if (defined $params{ip} && lc $params{ip} eq lc $ip) {
  398. return IkiWiki::SuccessReason->new("IP is $ip");
  399. }
  400. else {
  401. return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");
  402. }
  403. } #}}}
  404. 1