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);
14 IkiWiki::loadplugin("filecheck");
23 allowed_attachments => {
25 example => "virusfree() and mimetype(image/*) and maxsize(50kb)",
26 description => "enhanced PageSpec specifying what attachments are allowed",
27 link => "ikiwiki/PageSpec/attachment",
33 example => "clamdscan -",
34 description => "virus checker program (reads STDIN, returns nonzero if virus found)",
40 sub check_canattach ($$;$) {
42 my $dest=shift; # where it's going to be put, under the srcdir
43 my $file=shift; # the path to the attachment currently
45 # Don't allow an attachment to be uploaded with the same name as an
47 if (exists $IkiWiki::pagesources{$dest} &&
48 $IkiWiki::pagesources{$dest} ne $dest) {
49 error(sprintf(gettext("there is already a page named %s"), $dest));
52 # Use a special pagespec to test that the attachment is valid.
54 if (defined $config{allowed_attachments} &&
55 length $config{allowed_attachments}) {
56 $allowed=pagespec_match($dest,
57 $config{allowed_attachments},
59 user => $session->param("name"),
60 ip => $ENV{REMOTE_ADDR},
65 error(gettext("prohibited by allowed_attachments")." ($allowed)");
73 $config{cgi_disable_uploads}=0;
76 sub formbuilder_setup (@) {
78 my $form=$params{form};
81 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
82 $form->field("do") eq "create")) {
83 # Add attachment field, set type to multipart.
84 $form->enctype(&CGI::MULTIPART);
85 $form->field(name => 'attachment', type => 'file');
86 # These buttons are not put in the usual place, so
87 # are not added to the normal formbuilder button list.
88 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
89 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
91 # Add the toggle javascript; the attachments interface uses
92 # it to toggle visibility.
93 require IkiWiki::Plugin::toggle;
94 $form->tmpl_param("javascript" => IkiWiki::Plugin::toggle::include_javascript($params{page}, 1));
95 # Start with the attachments interface toggled invisible,
96 # but if it was used, keep it open.
97 if ($form->submitted ne "Upload Attachment" &&
98 (! defined $q->param("attachment_select") ||
99 ! length $q->param("attachment_select"))) {
100 $form->tmpl_param("attachments-class" => "toggleable");
103 $form->tmpl_param("attachments-class" => "toggleable-open");
108 sub formbuilder (@) {
110 my $form=$params{form};
113 return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
115 my $filename=$q->param('attachment');
116 if (defined $filename && length $filename &&
117 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
118 my $session=$params{session};
120 # This is an (apparently undocumented) way to get the name
121 # of the temp file that CGI writes the upload to.
122 my $tempfile=$q->tmpFileName($filename);
123 if (! defined $tempfile || ! length $tempfile) {
124 # perl 5.8 needs an alternative, awful method
125 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
126 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
127 $tempfile=$q->tmpFileName(\$key);
128 last if defined $tempfile && length $tempfile;
131 if (! defined $tempfile || ! length $tempfile) {
132 error("CGI::tmpFileName failed to return the uploaded file name");
136 $filename=linkpage(IkiWiki::possibly_foolish_untaint(
137 attachment_location($form->field('page')).
138 IkiWiki::basename($filename)));
139 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
140 error(gettext("bad attachment filename"));
143 # Check that the user is allowed to edit a page with the
144 # name of the attachment.
145 IkiWiki::check_canedit($filename, $q, $session, 1);
146 # And that the attachment itself is acceptable.
147 check_canattach($session, $filename, $tempfile);
149 # Needed for fast_file_copy and for rendering below.
150 require IkiWiki::Render;
152 # Move the attachment into place.
153 # Try to use a fast rename; fall back to copying.
154 IkiWiki::prep_writefile($filename, $config{srcdir});
155 unlink($config{srcdir}."/".$filename);
156 if (rename($tempfile, $config{srcdir}."/".$filename)) {
157 # The temp file has tight permissions; loosen up.
158 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
161 my $fh=$q->upload('attachment');
162 if (! defined $fh || ! ref $fh) {
163 # needed by old CGI versions
164 $fh=$q->param('attachment');
165 if (! defined $fh || ! ref $fh) {
166 # even that doesn't always work,
167 # fall back to opening the tempfile
169 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
173 writefile($filename, $config{srcdir}, undef, 1, sub {
174 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
178 # Check the attachment in and trigger a wiki refresh.
180 IkiWiki::rcs_add($filename);
181 IkiWiki::disable_commit_hook();
182 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
183 IkiWiki::rcs_prepedit($filename),
184 $session->param("name"), $ENV{REMOTE_ADDR});
185 IkiWiki::enable_commit_hook();
186 IkiWiki::rcs_update();
189 IkiWiki::saveindex();
191 elsif ($form->submitted eq "Insert Links") {
192 my $page=quotemeta($q->param("page"));
194 foreach my $f ($q->param("attachment_select")) {
198 $form->field(name => 'editcontent',
199 value => $form->field('editcontent')."\n\n".$add,
200 force => 1) if length $add;
203 # Generate the attachment list only after having added any new
205 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
208 sub attachment_location ($) {
211 # Put the attachment in a subdir of the page it's attached
212 # to, unless that page is an "index" page.
213 $page=~s/(^|\/)index//;
214 $page.="/" if length $page;
219 sub attachment_list ($) {
221 my $loc=attachment_location($page);
224 foreach my $f (values %pagesources) {
225 if (! defined pagetype($f) &&
226 $f=~m/^\Q$loc\E[^\/]+$/ &&
227 -e "$config{srcdir}/$f") {
229 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
230 link => htmllink($page, $page, $f, noimageinline => 1),
231 size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
232 mtime => displaytime($IkiWiki::pagemtime{$f}),
237 # Sort newer attachments to the top of the list, so a newly-added
238 # attachment appears just before the form used to add it.
239 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;