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,
50 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
51 if (! IkiWiki::is_admin($user_name)) {
52 $form->field(name => "allowed_attachments", type => "hidden");
54 if (! $form->submitted) {
55 $form->field(name => "allowed_attachments", force => 1,
56 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
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");
68 sub formbuilder (@) { #{{{
70 my $form=$params{form};
73 return if $form->field("do") ne "edit";
75 my $filename=$q->param('attachment');
76 if (defined $filename && length $filename &&
77 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
78 my $session=$params{session};
80 # This is an (apparently undocumented) way to get the name
81 # of the temp file that CGI writes the upload to.
82 my $tempfile=$q->tmpFileName($filename);
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"));
92 # Check that the user is allowed to edit a page with the
93 # name of the attachment.
94 IkiWiki::check_canedit($filename, $q, $session, 1);
96 # Use a special pagespec to test that the attachment is valid.
98 foreach my $admin (@{$config{adminuser}}) {
99 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
100 if (defined $allowed_attachments &&
101 length $allowed_attachments) {
102 $allowed=pagespec_match($filename,
103 $allowed_attachments,
105 user => $session->param("name"),
106 ip => $ENV{REMOTE_ADDR},
112 error(gettext("attachment rejected")." ($allowed)");
115 # Needed for fast_file_copy and for rendering below.
116 require IkiWiki::Render;
118 # Move the attachment into place.
119 # Try to use a fast rename; fall back to copying.
120 IkiWiki::prep_writefile($filename, $config{srcdir});
121 unlink($config{srcdir}."/".$filename);
122 if (rename($tempfile, $config{srcdir}."/".$filename)) {
123 # The temp file has tight permissions; loosen up.
124 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
127 my $fh=$q->upload('attachment');
128 if (! defined $fh || ! ref $fh) {
129 error("failed to get filehandle");
132 writefile($filename, $config{srcdir}, undef, 1, sub {
133 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
137 # Check the attachment in and trigger a wiki refresh.
139 IkiWiki::rcs_add($filename);
140 IkiWiki::disable_commit_hook();
141 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
142 IkiWiki::rcs_prepedit($filename),
143 $session->param("name"), $ENV{REMOTE_ADDR});
144 IkiWiki::enable_commit_hook();
145 IkiWiki::rcs_update();
148 IkiWiki::saveindex();
150 elsif ($form->submitted eq "Insert Links") {
152 foreach my $f ($q->param("attachment_select")) {
155 $form->field(name => 'editcontent',
156 value => $form->field('editcontent')."\n\n".$add,
157 force => 1) if length $add;
160 # Generate the attachment list only after having added any new
162 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
165 sub attachment_location ($) {
168 # Put the attachment in a subdir of the page it's attached
169 # to, unless that page is an "index" page.
170 $page=~s/(^|\/)index//;
171 $page.="/" if length $page;
176 sub attachment_list ($) {
178 my $loc=attachment_location($page);
181 foreach my $f (values %pagesources) {
182 if (! defined IkiWiki::pagetype($f) &&
183 $f=~m/^\Q$loc\E[^\/]+$/ &&
184 -e "$config{srcdir}/$f") {
186 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
187 link => htmllink($page, $page, $f, noimageinline => 1),
188 size => humansize((stat(_))[7]),
189 mtime => displaytime($IkiWiki::pagemtime{$f}),
190 mtime_raw => $IkiWiki::pagemtime{$f},
195 # Sort newer attachments to the top of the list, so a newly-added
196 # attachment appears just before the form used to add it.
197 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
200 my %units=( # size in bytes
225 zettabyte => 2 ** 70,
228 yottabyte => 2 ** 80,
229 # ikiwiki, if you find you need larger data quantities, either modify
230 # yourself to add them, or travel back in time to 2008 and kill me.
234 sub parsesize ($) { #{{{
238 my $base=$size+0; # force to number
240 foreach my $unit (sort keys %units) {
241 if ($size=~/[0-9\s]\Q$unit\E$/i) {
242 return $base * $units{$unit};
248 sub humansize ($) { #{{{
251 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
252 if ($size / $units{$unit} > 0.25) {
253 return (int($size / $units{$unit} * 10)/10).$unit;
256 return $size; # near zero, or negative
259 package IkiWiki::PageSpec;
261 sub match_maxsize ($$;@) { #{{{
263 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
265 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
269 if (! exists $params{file}) {
270 return IkiWiki::FailReason->new("no file specified");
273 if (-s $params{file} > $maxsize) {
274 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
277 return IkiWiki::SuccessReason->new("file not too large");
281 sub match_minsize ($$;@) { #{{{
283 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
285 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
289 if (! exists $params{file}) {
290 return IkiWiki::FailReason->new("no file specified");
293 if (-s $params{file} < $minsize) {
294 return IkiWiki::FailReason->new("file too small");
297 return IkiWiki::SuccessReason->new("file not too small");
301 sub match_mimetype ($$;@) { #{{{
306 if (! exists $params{file}) {
307 return IkiWiki::FailReason->new("no file specified");
310 # Use ::magic to get the mime type, the idea is to only trust
311 # data obtained by examining the actual file contents.
312 eval q{use File::MimeInfo::Magic};
314 return IkiWiki::FailReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
316 my $mimetype=File::MimeInfo::Magic::magic($params{file});
317 if (! defined $mimetype) {
321 # turn glob into a safe regexp
322 my $regexp=quotemeta($wanted);
323 $regexp=~s/\\\*/.*/g;
326 if ($mimetype!~/^$regexp$/i) {
327 return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
330 return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
334 sub match_ispage ($$;@) { #{{{
337 if (defined IkiWiki::pagetype($filename)) {
338 return IkiWiki::SuccessReason->new("file is a wiki page");
341 return IkiWiki::FailReason->new("file is not a wiki page");
345 sub match_user ($$;@) { #{{{
350 if (! exists $params{user}) {
351 return IkiWiki::FailReason->new("no user specified");
354 if (defined $params{user} && lc $params{user} eq lc $user) {
355 return IkiWiki::SuccessReason->new("user is $user");
358 return IkiWiki::FailReason->new("user is $params{user}, not $user");
362 sub match_ip ($$;@) { #{{{
367 if (! exists $params{ip}) {
368 return IkiWiki::FailReason->new("no IP specified");
371 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
372 return IkiWiki::SuccessReason->new("IP is $ip");
375 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");