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