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