2 package IkiWiki::Plugin::attachment;
9 add_underlay("javascript");
10 hook(type => "getsetup", id => "attachment", call => \&getsetup);
11 hook(type => "checkconfig", id => "attachment", call => \&checkconfig);
12 hook(type => "formbuilder_setup", id => "attachment", call => \&formbuilder_setup);
13 hook(type => "formbuilder", id => "attachment", call => \&formbuilder, last => 1);
14 IkiWiki::loadplugin("filecheck");
24 allowed_attachments => {
26 example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
27 description => "enhanced PageSpec specifying what attachments are allowed",
28 link => "ikiwiki/PageSpec/attachment",
34 example => "clamdscan -",
35 description => "virus checker program (reads STDIN, returns nonzero if virus found)",
41 sub check_canattach ($$;$) {
43 my $dest=shift; # where it's going to be put, under the srcdir
44 my $file=shift; # the path to the attachment currently
46 # Don't allow an attachment to be uploaded with the same name as an
48 if (exists $IkiWiki::pagesources{$dest} &&
49 $IkiWiki::pagesources{$dest} ne $dest) {
50 error(sprintf(gettext("there is already a page named %s"), $dest));
53 # Use a special pagespec to test that the attachment is valid.
55 if (defined $config{allowed_attachments} &&
56 length $config{allowed_attachments}) {
57 $allowed=pagespec_match($dest,
58 $config{allowed_attachments},
60 user => $session->param("name"),
61 ip => $session->remote_addr(),
66 error(gettext("prohibited by allowed_attachments")." ($allowed)");
74 $config{cgi_disable_uploads}=0;
77 sub formbuilder_setup (@) {
79 my $form=$params{form};
82 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
83 $form->field("do") eq "create")) {
84 # Add attachment field, set type to multipart.
85 $form->enctype(&CGI::MULTIPART);
86 $form->field(name => 'attachment', type => 'file');
87 # These buttons are not put in the usual place, so
88 # are not added to the normal formbuilder button list.
89 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
90 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
92 # Add the toggle javascript; the attachments interface uses
93 # it to toggle visibility.
94 require IkiWiki::Plugin::toggle;
95 $form->tmpl_param("javascript" => IkiWiki::Plugin::toggle::include_javascript($params{page}));
96 # Start with the attachments interface toggled invisible,
97 # but if it was used, keep it open.
98 if ($form->submitted ne "Upload Attachment" &&
99 (! defined $q->param("attachment_select") ||
100 ! length $q->param("attachment_select"))) {
101 $form->tmpl_param("attachments-class" => "toggleable");
104 $form->tmpl_param("attachments-class" => "toggleable-open");
107 # Save attachments in holding area before previewing so
108 # they can be seen in the preview.
109 if ($form->submitted eq "Preview") {
110 attachments_save($form, $params{session});
115 sub formbuilder (@) {
117 my $form=$params{form};
120 return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
122 my $filename=Encode::decode_utf8($q->param('attachment'));
123 if (defined $filename && length $filename &&
124 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
125 attachment_store($filename, $form, $q, $params{session});
127 if ($form->submitted eq "Save Page") {
128 attachments_save($form, $params{session});
131 if ($form->submitted eq "Insert Links") {
132 my $page=quotemeta(Encode::decode_utf8($q->param("page")));
134 foreach my $f ($q->param("attachment_select")) {
135 $f=Encode::decode_utf8($f);
137 if (IkiWiki::isinlinableimage($f) &&
138 UNIVERSAL::can("IkiWiki::Plugin::img", "import")) {
139 $add.='[[!img '.$f.' align="right" size="" alt=""]]';
146 $form->field(name => 'editcontent',
147 value => $form->field('editcontent')."\n\n".$add,
148 force => 1) if length $add;
151 # Generate the attachment list only after having added any new
153 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
156 sub attachment_holding_dir {
157 my $page=attachment_location(shift);
159 return $config{wikistatedir}."/attachments/".
160 IkiWiki::possibly_foolish_untaint(linkpage($page));
163 sub remove_held_attachment {
164 my $attachment=shift;
166 my $f=attachment_holding_dir($attachment);
169 require IkiWiki::Render;
178 # Stores the attachment in a holding area, not yet in the wiki proper.
179 sub attachment_store {
185 # This is an (apparently undocumented) way to get the name
186 # of the temp file that CGI writes the upload to.
187 my $tempfile=$q->tmpFileName($filename);
188 if (! defined $tempfile || ! length $tempfile) {
189 # perl 5.8 needs an alternative, awful method
190 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
191 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
192 $tempfile=$q->tmpFileName(\$key);
193 last if defined $tempfile && length $tempfile;
196 if (! defined $tempfile || ! length $tempfile) {
197 error("CGI::tmpFileName failed to return the uploaded file name");
201 $filename=IkiWiki::basename($filename);
202 $filename=~s/.*\\+(.+)/$1/; # hello, windows
203 $filename=IkiWiki::possibly_foolish_untaint(linkpage($filename));
205 # Check that the user is allowed to edit the attachment.
207 linkpage(IkiWiki::possibly_foolish_untaint(
208 attachment_location($form->field('page')))).
210 if (IkiWiki::file_pruned($final_filename)) {
211 error(gettext("bad attachment filename"));
213 IkiWiki::check_canedit($final_filename, $q, $session);
214 # And that the attachment itself is acceptable.
215 check_canattach($session, $final_filename, $tempfile);
217 # Move the attachment into holding directory.
218 # Try to use a fast rename; fall back to copying.
219 my $dest=attachment_holding_dir($form->field('page'));
220 IkiWiki::prep_writefile($filename, $dest);
221 unlink($dest."/".$filename);
222 if (rename($tempfile, $dest."/".$filename)) {
223 # The temp file has tight permissions; loosen up.
224 chmod(0666 & ~umask, $dest."/".$filename);
227 my $fh=$q->upload('attachment');
228 if (! defined $fh || ! ref $fh) {
229 # needed by old CGI versions
230 $fh=$q->param('attachment');
231 if (! defined $fh || ! ref $fh) {
232 # even that doesn't always work,
233 # fall back to opening the tempfile
235 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
239 require IkiWiki::Render;
240 writefile($filename, $dest, undef, 1, sub {
241 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
246 # Save all stored attachments for a page.
247 sub attachments_save {
251 # Move attachments out of holding directory.
253 my $dir=attachment_holding_dir($form->field('page'));
254 foreach my $filename (glob("$dir/*")) {
255 next unless -f $filename;
256 my $dest=$config{srcdir}."/".
257 linkpage(IkiWiki::possibly_foolish_untaint(
258 attachment_location($form->field('page')))).
259 IkiWiki::basename($filename);
261 rename($filename, $dest);
262 push @attachments, $dest;
264 return unless @attachments;
265 require IkiWiki::Render;
266 IkiWiki::prune($dir);
268 # Check the attachments in and trigger a wiki refresh.
270 IkiWiki::rcs_add($_) foreach @attachments;
271 IkiWiki::disable_commit_hook();
272 IkiWiki::rcs_commit_staged(
273 message => gettext("attachment upload"),
276 IkiWiki::enable_commit_hook();
277 IkiWiki::rcs_update();
280 IkiWiki::saveindex();
283 sub attachment_location ($) {
286 # Put the attachment in a subdir of the page it's attached
287 # to, unless that page is an "index" page.
288 $page=~s/(^|\/)index//;
289 $page.="/" if length $page;
294 sub attachment_list ($) {
296 my $loc=attachment_location($page);
303 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$file.'" />',
304 size => IkiWiki::Plugin::filecheck::humansize($size),
305 mtime => displaytime($mtime),
309 # attachments already in the wiki
311 foreach my $f (values %pagesources) {
312 if (! defined pagetype($f) &&
313 $f=~m/^\Q$loc\E[^\/]+$/) {
315 $std->($f, $IkiWiki::pagemtime{$f}, (stat($f))[7]),
316 link => htmllink($page, $page, $f, noimageinline => 1),
321 # attachments in holding directory
322 my $dir=attachment_holding_dir($page);
323 my $heldmsg=gettext("this attachment is not yet saved");
324 foreach my $file (glob("$dir/*")) {
325 next unless -f $file;
326 my $mtime=(stat(_))[9];
327 my $base=IkiWiki::basename($file);
330 $std->($f, (stat($file))[9], (stat(_))[7]),
331 link => "<span title=\"$heldmsg\">$base</span>",
335 # Sort newer attachments to the top of the list, so a newly-added
336 # attachment appears just before the form used to add it.
337 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} }