2 package IkiWiki::Plugin::attachment;
9 hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
10 hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
11 hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
14 sub checkconfig () { #{{{
15 $config{cgi_disable_uploads}=0;
18 sub formbuilder_setup (@) { #{{{
20 my $form=$params{form};
23 if ($form->field("do") eq "edit") {
24 $form->field(name => 'attachment', type => 'file');
25 # These buttons are not put in the usual place, so
26 # are not added to the normal formbuilder button list.
27 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
28 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
30 # Add the javascript from the toggle plugin;
31 # the attachments interface uses it to toggle visibility.
32 require IkiWiki::Plugin::toggle;
33 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
34 # Start with the attachments interface toggled invisible,
35 # but if it was used, keep it open.
36 if ($form->submitted ne "Upload Attachment" &&
37 ! length $q->param("attachment_select")) {
38 $form->tmpl_param("attachments-class" => "toggleable");
41 $form->tmpl_param("attachments-class" => "toggleable-open");
44 elsif ($form->title eq "preferences") {
45 my $session=$params{session};
46 my $user_name=$session->param("name");
48 $form->field(name => "allowed_attachments", size => 50,
52 "ikiwiki/PageSpec/attachment",
54 linktext => "Enhanced PageSpec",
57 if (! IkiWiki::is_admin($user_name)) {
58 $form->field(name => "allowed_attachments", type => "hidden");
60 if (! $form->submitted) {
61 $form->field(name => "allowed_attachments", force => 1,
62 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
64 if ($form->submitted && $form->submitted eq 'Save Preferences') {
65 if (defined $form->field("allowed_attachments")) {
66 IkiWiki::userinfo_set($user_name, "allowed_attachments",
67 $form->field("allowed_attachments")) ||
68 error("failed to set allowed_attachments");
74 sub formbuilder (@) { #{{{
76 my $form=$params{form};
79 return if $form->field("do") ne "edit";
81 my $filename=$q->param('attachment');
82 if (defined $filename && length $filename &&
83 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
84 my $session=$params{session};
86 # This is an (apparently undocumented) way to get the name
87 # of the temp file that CGI writes the upload to.
88 my $tempfile=$q->tmpFileName($filename);
90 $filename=IkiWiki::titlepage(
91 IkiWiki::possibly_foolish_untaint(
92 attachment_location($form->field('page')).
93 IkiWiki::basename($filename)));
94 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
95 error(gettext("bad attachment filename"));
98 # Check that the user is allowed to edit a page with the
99 # name of the attachment.
100 IkiWiki::check_canedit($filename, $q, $session, 1);
102 # Use a special pagespec to test that the attachment is valid.
104 foreach my $admin (@{$config{adminuser}}) {
105 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
106 if (defined $allowed_attachments &&
107 length $allowed_attachments) {
108 $allowed=pagespec_match($filename,
109 $allowed_attachments,
111 user => $session->param("name"),
112 ip => $ENV{REMOTE_ADDR},
118 error(gettext("attachment rejected")." ($allowed)");
121 # Needed for fast_file_copy and for rendering below.
122 require IkiWiki::Render;
124 # Move the attachment into place.
125 # Try to use a fast rename; fall back to copying.
126 IkiWiki::prep_writefile($filename, $config{srcdir});
127 unlink($config{srcdir}."/".$filename);
128 if (rename($tempfile, $config{srcdir}."/".$filename)) {
129 # The temp file has tight permissions; loosen up.
130 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
133 my $fh=$q->upload('attachment');
134 if (! defined $fh || ! ref $fh) {
135 error("failed to get filehandle");
138 writefile($filename, $config{srcdir}, undef, 1, sub {
139 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
143 # Check the attachment in and trigger a wiki refresh.
145 IkiWiki::rcs_add($filename);
146 IkiWiki::disable_commit_hook();
147 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
148 IkiWiki::rcs_prepedit($filename),
149 $session->param("name"), $ENV{REMOTE_ADDR});
150 IkiWiki::enable_commit_hook();
151 IkiWiki::rcs_update();
154 IkiWiki::saveindex();
156 elsif ($form->submitted eq "Insert Links") {
158 foreach my $f ($q->param("attachment_select")) {
161 $form->field(name => 'editcontent',
162 value => $form->field('editcontent')."\n\n".$add,
163 force => 1) if length $add;
166 # Generate the attachment list only after having added any new
168 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
171 sub attachment_location ($) {
174 # Put the attachment in a subdir of the page it's attached
175 # to, unless that page is an "index" page.
176 $page=~s/(^|\/)index//;
177 $page.="/" if length $page;
182 sub attachment_list ($) {
184 my $loc=attachment_location($page);
187 foreach my $f (values %pagesources) {
188 if (! defined IkiWiki::pagetype($f) &&
189 $f=~m/^\Q$loc\E[^\/]+$/ &&
190 -e "$config{srcdir}/$f") {
192 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
193 link => htmllink($page, $page, $f, noimageinline => 1),
194 size => humansize((stat(_))[7]),
195 mtime => displaytime($IkiWiki::pagemtime{$f}),
196 mtime_raw => $IkiWiki::pagemtime{$f},
201 # Sort newer attachments to the top of the list, so a newly-added
202 # attachment appears just before the form used to add it.
203 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
206 my %units=( # size in bytes
231 zettabyte => 2 ** 70,
234 yottabyte => 2 ** 80,
235 # ikiwiki, if you find you need larger data quantities, either modify
236 # yourself to add them, or travel back in time to 2008 and kill me.
240 sub parsesize ($) { #{{{
244 my $base=$size+0; # force to number
246 foreach my $unit (sort keys %units) {
247 if ($size=~/[0-9\s]\Q$unit\E$/i) {
248 return $base * $units{$unit};
254 sub humansize ($) { #{{{
257 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
258 if ($size / $units{$unit} > 0.25) {
259 return (int($size / $units{$unit} * 10)/10).$unit;
262 return $size; # near zero, or negative
265 package IkiWiki::PageSpec;
267 sub match_maxsize ($$;@) { #{{{
269 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
271 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
275 if (! exists $params{file}) {
276 return IkiWiki::FailReason->new("no file specified");
279 if (-s $params{file} > $maxsize) {
280 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
283 return IkiWiki::SuccessReason->new("file not too large");
287 sub match_minsize ($$;@) { #{{{
289 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
291 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
295 if (! exists $params{file}) {
296 return IkiWiki::FailReason->new("no file specified");
299 if (-s $params{file} < $minsize) {
300 return IkiWiki::FailReason->new("file too small");
303 return IkiWiki::SuccessReason->new("file not too small");
307 sub match_mimetype ($$;@) { #{{{
312 if (! exists $params{file}) {
313 return IkiWiki::FailReason->new("no file specified");
316 # Use ::magic to get the mime type, the idea is to only trust
317 # data obtained by examining the actual file contents.
318 eval q{use File::MimeInfo::Magic};
320 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
322 my $mimetype=File::MimeInfo::Magic::magic($params{file});
323 if (! defined $mimetype) {
327 my $regexp=IkiWiki::glob2re($wanted);
328 if ($mimetype!~/^$regexp$/i) {
329 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
332 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
336 sub match_ispage ($$;@) { #{{{
339 if (defined IkiWiki::pagetype($filename)) {
340 return IkiWiki::SuccessReason->new("file is a wiki page");
343 return IkiWiki::FailReason->new("file is not a wiki page");
347 sub match_user ($$;@) { #{{{
352 if (! exists $params{user}) {
353 return IkiWiki::FailReason->new("no user specified");
356 if (defined $params{user} && lc $params{user} eq lc $user) {
357 return IkiWiki::SuccessReason->new("user is $user");
360 return IkiWiki::FailReason->new("user is $params{user}, not $user");
364 sub match_ip ($$;@) { #{{{
369 if (! exists $params{ip}) {
370 return IkiWiki::FailReason->new("no IP specified");
373 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
374 return IkiWiki::SuccessReason->new("IP is $ip");
377 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");