]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/attachment.pm
only show one sig fig
[ikiwiki.git] / IkiWiki / Plugin / attachment.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::attachment;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
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);
12 } # }}}
13
14 sub checkconfig () { #{{{
15         $config{cgi_disable_uploads}=0;
16 } #}}}
17
18 sub attachment_location ($) {
19         my $page=shift;
20         
21         # Put the attachment in a subdir of the page it's attached
22         # to, unless that page is an "index" page.
23         $page=~s/(^|\/)index//;
24         $page.="/" if length $page;
25         
26         return $page;
27 }
28
29 sub attachment_list ($) {
30         my $page=shift;
31         my $loc=attachment_location($page);
32
33         my @ret;
34         foreach my $f (values %pagesources) {
35                 if (! defined IkiWiki::pagetype($f) &&
36                     $f=~m/^\Q$loc\E[^\/]+$/ &&
37                     -e "$config{srcdir}/$f") {
38                         push @ret, {
39                                 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">',
40                                 link => htmllink($page, $page, $f, noimageinline => 1),
41                                 size => humansize((stat(_))[7]),
42                                 mtime => displaytime($IkiWiki::pagemtime{$f}),
43                         };
44                 }
45         }
46
47         return @ret;
48 }
49
50 sub formbuilder_setup (@) { #{{{
51         my %params=@_;
52         my $form=$params{form};
53
54         if ($form->field("do") eq "edit") {
55                 $form->field(name => 'attachment', type => 'file');
56                 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
57
58                 # These buttons are not put in the usual place, so
59                 # is not added to the normal formbuilder button list.
60                 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
61                 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
62         }
63         elsif ($form->title eq "preferences") {
64                 my $session=$params{session};
65                 my $user_name=$session->param("name");
66
67                 $form->field(name => "allowed_attachments", size => 50,
68                         fieldset => "admin",
69                         comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
70                 if (! IkiWiki::is_admin($user_name)) {
71                         $form->field(name => "allowed_attachments", type => "hidden");
72                 }
73                 if (! $form->submitted) {
74                         $form->field(name => "allowed_attachments", force => 1,
75                                 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
76                 }
77                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
78                         if (defined $form->field("allowed_attachments")) {
79                                 IkiWiki::userinfo_set($user_name, "allowed_attachments",
80                                 $form->field("allowed_attachments")) ||
81                                         error("failed to set allowed_attachments");
82                         }
83                 }
84         }
85 } #}}}
86
87 sub formbuilder (@) { #{{{
88         my %params=@_;
89         my $form=$params{form};
90         my $q=$params{cgi};
91
92         return if $form->field("do") ne "edit";
93
94         if ($form->submitted eq "Upload" || $form->submitted eq "Save Page") {
95                 my $session=$params{session};
96
97                 my $filename=$q->param('attachment');
98                 if (! defined $filename || ! length $filename) {
99                         # no file, so do nothing
100                         return;
101                 }
102                 
103                 # This is an (apparently undocumented) way to get the name
104                 # of the temp file that CGI writes the upload to.
105                 my $tempfile=$q->tmpFileName($filename);
106                 
107                 $filename=IkiWiki::titlepage(
108                         IkiWiki::possibly_foolish_untaint(
109                                 attachment_location($form->field('page')).
110                                 IkiWiki::basename($filename)));
111                 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
112                         error(gettext("bad attachment filename"));
113                 }
114                 
115                 # Check that the user is allowed to edit a page with the
116                 # name of the attachment.
117                 IkiWiki::check_canedit($filename, $q, $session, 1);
118                 
119                 # Use a special pagespec to test that the attachment is valid.
120                 my $allowed=1;
121                 foreach my $admin (@{$config{adminuser}}) {
122                         my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
123                         if (defined $allowed_attachments &&
124                             length $allowed_attachments) {
125                                 $allowed=pagespec_match($filename,
126                                         $allowed_attachments,
127                                         file => $tempfile);
128                                 last if $allowed;
129                         }
130                 }
131                 if (! $allowed) {
132                         error(gettext("attachment rejected")." ($allowed)");
133                 }
134
135                 # Needed for fast_file_copy and for rendering below.
136                 require IkiWiki::Render;
137
138                 # Move the attachment into place.
139                 # Try to use a fast rename; fall back to copying.
140                 IkiWiki::prep_writefile($filename, $config{srcdir});
141                 unlink($config{srcdir}."/".$filename);
142                 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
143                         my $fh=$q->upload('attachment');
144                         if (! defined $fh || ! ref $fh) {
145                                 error("failed to get filehandle");
146                         }
147                         binmode($fh);
148                         writefile($filename, $config{srcdir}, undef, 1, sub {
149                                 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
150                         });
151                 }
152
153                 # Check the attachment in and trigger a wiki refresh.
154                 if ($config{rcs}) {
155                         IkiWiki::rcs_add($filename);
156                         IkiWiki::disable_commit_hook();
157                         IkiWiki::rcs_commit($filename, gettext("attachment upload"),
158                                 IkiWiki::rcs_prepedit($filename),
159                                 $session->param("name"), $ENV{REMOTE_ADDR});
160                         IkiWiki::enable_commit_hook();
161                         IkiWiki::rcs_update();
162                 }
163                 IkiWiki::refresh();
164                 IkiWiki::saveindex();
165         }
166         elsif ($form->submitted eq "Insert Links") {
167                 my $add="";
168                 foreach my $f ($q->param("attachment_select")) {
169                         $add.="[[$f]]\n";
170                 }
171                 $form->field(name => 'editcontent',
172                         value => $form->field('editcontent')."\n\n".$add,
173                         force => 1);
174         }
175 } # }}}
176
177 my %units=(             # size in bytes
178         B               => 1,
179         byte            => 1,
180         KB              => 2 ** 10,
181         kilobyte        => 2 ** 10,
182         K               => 2 ** 10,
183         KB              => 2 ** 10,
184         kilobyte        => 2 ** 10,
185         M               => 2 ** 20,
186         MB              => 2 ** 20,
187         megabyte        => 2 ** 20,
188         G               => 2 ** 30,
189         GB              => 2 ** 30,
190         gigabyte        => 2 ** 30,
191         T               => 2 ** 40,
192         TB              => 2 ** 40,
193         terabyte        => 2 ** 40,
194         P               => 2 ** 50,
195         PB              => 2 ** 50,
196         petabyte        => 2 ** 50,
197         E               => 2 ** 60,
198         EB              => 2 ** 60,
199         exabyte         => 2 ** 60,
200         Z               => 2 ** 70,
201         ZB              => 2 ** 70,
202         zettabyte       => 2 ** 70,
203         Y               => 2 ** 80,
204         YB              => 2 ** 80,
205         yottabyte       => 2 ** 80,
206         # ikiwiki, if you find you need larger data quantities, either modify
207         # yourself to add them, or travel back in time to 2008 and kill me.
208         #   -- Joey
209 );
210
211 sub parsesize ($) { #{{{
212         my $size=shift;
213
214         no warnings;
215         my $base=$size+0; # force to number
216         use warnings;
217         foreach my $unit (sort keys %units) {
218                 if ($size=~/\Q$unit\E/i) {
219                         return $base * $units{$unit};
220                 }
221         }
222         return $base;
223 } #}}}
224
225 sub humansize ($) { #{{{
226         my $size=shift;
227
228         foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
229                 if ($size / $units{$unit} > 0.25) {
230                         return (int($size / $units{$unit} * 10)/10)."$unit";
231                 }
232         }
233         return $size; # near zero, or negative
234 } #}}}
235
236 package IkiWiki::PageSpec;
237
238 sub match_maxsize ($$;@) { #{{{
239         shift;
240         my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
241         if ($@) {
242                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
243         }
244
245         my %params=@_;
246         if (! exists $params{file}) {
247                 return IkiWiki::FailReason->new("no file specified");
248         }
249
250         if (-s $params{file} > $maxsize) {
251                 return IkiWiki::FailReason->new("file too large");
252         }
253         else {
254                 return IkiWiki::SuccessReason->new("file not too large");
255         }
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
265         my %params=@_;
266         if (! exists $params{file}) {
267                 return IkiWiki::FailReason->new("no file specified");
268         }
269
270         if (-s $params{file} < $minsize) {
271                 return IkiWiki::FailReason->new("file too small");
272         }
273         else {
274                 return IkiWiki::SuccessReason->new("file not too small");
275         }
276 } #}}}
277
278 sub match_ispage ($$;@) { #{{{
279         my $filename=shift;
280
281         if (defined IkiWiki::pagetype($filename)) {
282                 return IkiWiki::SuccessReason->new("file is a wiki page");
283         }
284         else {
285                 return IkiWiki::FailReason->new("file is not a wiki page");
286         }
287 } #}}}
288
289 1