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