]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/attachment.pm
basic attachment list
[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 $loc=attachment_location(shift);
31
32         my @ret;
33         foreach my $f (values %pagesources) {
34                 print STDERR ">>$f\n" if ! defined IkiWiki::pagetype($f);
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 => $f,
41                                 size => (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
91         return if $form->field("do") ne "edit";
92
93         if ($form->submitted eq "Upload" || $form->submitted eq "Save Page") {
94                 my $q=$params{cgi};
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 } # }}}
167
168 package IkiWiki::PageSpec;
169
170 sub parsesize ($) { #{{{
171         my $size=shift;
172         no warnings;
173         my $base=$size+0; # force to number
174         use warnings;
175         my $multiple=1;
176         if ($size=~/kb?$/i) {
177                 $multiple=2**10;
178         }
179         elsif ($size=~/mb?$/i) {
180                 $multiple=2**20;
181         }
182         elsif ($size=~/gb?$/i) {
183                 $multiple=2**30;
184         }
185         elsif ($size=~/tb?$/i) {
186                 $multiple=2**40;
187         }
188         return $base * $multiple;
189 } #}}}
190
191 sub match_maxsize ($$;@) { #{{{
192         shift;
193         my $maxsize=eval{parsesize(shift)};
194         if ($@) {
195                 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
196         }
197
198         my %params=@_;
199         if (! exists $params{file}) {
200                 return IkiWiki::FailReason->new("no file specified");
201         }
202
203         if (-s $params{file} > $maxsize) {
204                 return IkiWiki::FailReason->new("file too large");
205         }
206         else {
207                 return IkiWiki::SuccessReason->new("file not too large");
208         }
209 } #}}}
210
211 sub match_minsize ($$;@) { #{{{
212         shift;
213         my $minsize=eval{parsesize(shift)};
214         if ($@) {
215                 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
216         }
217
218         my %params=@_;
219         if (! exists $params{file}) {
220                 return IkiWiki::FailReason->new("no file specified");
221         }
222
223         if (-s $params{file} < $minsize) {
224                 return IkiWiki::FailReason->new("file too small");
225         }
226         else {
227                 return IkiWiki::SuccessReason->new("file not too small");
228         }
229 } #}}}
230
231 sub match_ispage ($$;@) { #{{{
232         my $filename=shift;
233
234         if (defined IkiWiki::pagetype($filename)) {
235                 return IkiWiki::SuccessReason->new("file is a wiki page");
236         }
237         else {
238                 return IkiWiki::FailReason->new("file is not a wiki page");
239         }
240 } #}}}
241
242 1