2 package IkiWiki::Plugin::attachment;
9 hook(type => "getsetup", id => "attachment", call => \&getsetup);
10 hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
11 hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
12 hook(type => "formbuilder", id => "attachment", call => \&formbuilder);
13 IkiWiki::loadplugin("filecheck");
16 sub getsetup () { #{{{
22 allowed_attachments => {
24 example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
25 description => "enhanced PageSpec specifying what attachments are allowed",
26 link => "ikiwiki/PageSpec/attachment",
32 example => "clamdscan -",
33 description => "virus checker program (reads STDIN, returns nonzero if virus found)",
39 sub check_canattach ($$;$) { #{{{
41 my $dest=shift; # where it's going to be put, under the srcdir
42 my $file=shift; # the path to the attachment currently
44 # Don't allow an attachment to be uploaded with the same name as an
46 if (exists $IkiWiki::pagesources{$dest} &&
47 $IkiWiki::pagesources{$dest} ne $dest) {
48 error(sprintf(gettext("there is already a page named %s"), $dest));
51 # Use a special pagespec to test that the attachment is valid.
53 if (defined $config{allowed_attachments} &&
54 length $config{allowed_attachments}) {
55 $allowed=pagespec_match($dest,
56 $config{allowed_attachments},
58 user => $session->param("name"),
59 ip => $ENV{REMOTE_ADDR},
63 # XXX deprecated, should be removed eventually
65 foreach my $admin (@{$config{adminuser}}) {
66 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
67 if (defined $allowed_attachments &&
68 length $allowed_attachments) {
69 $allowed=pagespec_match($dest,
72 user => $session->param("name"),
73 ip => $ENV{REMOTE_ADDR},
81 error(gettext("prohibited by allowed_attachments")." ($allowed)");
88 sub checkconfig () { #{{{
89 $config{cgi_disable_uploads}=0;
92 sub formbuilder_setup (@) { #{{{
94 my $form=$params{form};
97 if (defined $form->field("do") && $form->field("do") eq "edit") {
98 # Add attachment field, set type to multipart.
99 $form->enctype(&CGI::MULTIPART);
100 $form->field(name => 'attachment', type => 'file');
101 # These buttons are not put in the usual place, so
102 # are not added to the normal formbuilder button list.
103 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
104 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
106 # Add the javascript from the toggle plugin;
107 # the attachments interface uses it to toggle visibility.
108 require IkiWiki::Plugin::toggle;
109 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
110 # Start with the attachments interface toggled invisible,
111 # but if it was used, keep it open.
112 if ($form->submitted ne "Upload Attachment" &&
113 (! defined $q->param("attachment_select") ||
114 ! length $q->param("attachment_select"))) {
115 $form->tmpl_param("attachments-class" => "toggleable");
118 $form->tmpl_param("attachments-class" => "toggleable-open");
121 elsif ($form->title eq "preferences") {
122 # XXX deprecated, should remove eventually
123 my $session=$params{session};
124 my $user_name=$session->param("name");
126 $form->field(name => "allowed_attachments", size => 50,
128 comment => "deprecated; please move to allowed_attachments in setup file",
130 if (! IkiWiki::is_admin($user_name)) {
131 $form->field(name => "allowed_attachments", type => "hidden");
133 if (! $form->submitted) {
134 my $value=IkiWiki::userinfo_get($user_name, "allowed_attachments");
136 $form->field(name => "allowed_attachments", force => 1,
137 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
140 $form->field(name => "allowed_attachments", type => "hidden");
143 if ($form->submitted && $form->submitted eq 'Save Preferences') {
144 if (defined $form->field("allowed_attachments")) {
145 IkiWiki::userinfo_set($user_name, "allowed_attachments",
146 $form->field("allowed_attachments")) ||
147 error("failed to set allowed_attachments");
148 if (! length $form->field("allowed_attachments")) {
149 $form->field(name => "allowed_attachments", type => "hidden");
156 sub formbuilder (@) { #{{{
158 my $form=$params{form};
161 return if ! defined $form->field("do") || $form->field("do") ne "edit";
163 my $filename=$q->param('attachment');
164 if (defined $filename && length $filename &&
165 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
166 my $session=$params{session};
168 # This is an (apparently undocumented) way to get the name
169 # of the temp file that CGI writes the upload to.
170 my $tempfile=$q->tmpFileName($filename);
171 if (! defined $tempfile || ! length $tempfile) {
172 # perl 5.8 needs an alternative, awful method
173 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
174 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
175 $tempfile=$q->tmpFileName(\$key);
176 last if defined $tempfile && length $tempfile;
179 if (! defined $tempfile || ! length $tempfile) {
180 error("CGI::tmpFileName failed to return the uploaded file name");
184 $filename=IkiWiki::linkpage(
185 IkiWiki::possibly_foolish_untaint(
186 attachment_location($form->field('page')).
187 IkiWiki::basename($filename)));
188 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
189 error(gettext("bad attachment filename"));
192 # Check that the user is allowed to edit a page with the
193 # name of the attachment.
194 IkiWiki::check_canedit($filename, $q, $session, 1);
195 # And that the attachment itself is acceptable.
196 check_canattach($session, $filename, $tempfile);
198 # Needed for fast_file_copy and for rendering below.
199 require IkiWiki::Render;
201 # Move the attachment into place.
202 # Try to use a fast rename; fall back to copying.
203 IkiWiki::prep_writefile($filename, $config{srcdir});
204 unlink($config{srcdir}."/".$filename);
205 if (rename($tempfile, $config{srcdir}."/".$filename)) {
206 # The temp file has tight permissions; loosen up.
207 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
210 my $fh=$q->upload('attachment');
211 if (! defined $fh || ! ref $fh) {
212 # needed by old CGI versions
213 $fh=$q->param('attachment');
214 if (! defined $fh || ! ref $fh) {
215 # even that doesn't always work,
216 # fall back to opening the tempfile
218 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
222 writefile($filename, $config{srcdir}, undef, 1, sub {
223 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
227 # Check the attachment in and trigger a wiki refresh.
229 IkiWiki::rcs_add($filename);
230 IkiWiki::disable_commit_hook();
231 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
232 IkiWiki::rcs_prepedit($filename),
233 $session->param("name"), $ENV{REMOTE_ADDR});
234 IkiWiki::enable_commit_hook();
235 IkiWiki::rcs_update();
238 IkiWiki::saveindex();
240 elsif ($form->submitted eq "Insert Links") {
241 my $page=quotemeta($q->param("page"));
243 foreach my $f ($q->param("attachment_select")) {
247 $form->field(name => 'editcontent',
248 value => $form->field('editcontent')."\n\n".$add,
249 force => 1) if length $add;
252 # Generate the attachment list only after having added any new
254 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
257 sub attachment_location ($) { #{{{
260 # Put the attachment in a subdir of the page it's attached
261 # to, unless that page is an "index" page.
262 $page=~s/(^|\/)index//;
263 $page.="/" if length $page;
268 sub attachment_list ($) { #{{{
270 my $loc=attachment_location($page);
273 foreach my $f (values %pagesources) {
274 if (! defined IkiWiki::pagetype($f) &&
275 $f=~m/^\Q$loc\E[^\/]+$/ &&
276 -e "$config{srcdir}/$f") {
278 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
279 link => htmllink($page, $page, $f, noimageinline => 1),
280 size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
281 mtime => displaytime($IkiWiki::pagemtime{$f}),
282 mtime_raw => $IkiWiki::pagemtime{$f},
287 # Sort newer attachments to the top of the list, so a newly-added
288 # attachment appears just before the form used to add it.
289 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
292 package IkiWiki::PageSpec;
294 sub match_user ($$;@) { #{{{
299 if (! exists $params{user}) {
300 return IkiWiki::FailReason->new("no user specified");
303 if (defined $params{user} && lc $params{user} eq lc $user) {
304 return IkiWiki::SuccessReason->new("user is $user");
306 elsif (! defined $params{user}) {
307 return IkiWiki::FailReason->new("not logged in");
310 return IkiWiki::FailReason->new("user is $params{user}, not $user");
314 sub match_ip ($$;@) { #{{{
319 if (! exists $params{ip}) {
320 return IkiWiki::FailReason->new("no IP specified");
323 if (defined $params{ip} && lc $params{ip} eq lc $ip) {
324 return IkiWiki::SuccessReason->new("IP is $ip");
327 return IkiWiki::FailReason->new("IP is $params{ip}, not $ip");