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