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");
17 sub getsetup () { #{{{
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},
64 # XXX deprecated, should be removed eventually
66 foreach my $admin (@{$config{adminuser}}) {
67 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
68 if (defined $allowed_attachments &&
69 length $allowed_attachments) {
70 $allowed=pagespec_match($dest,
73 user => $session->param("name"),
74 ip => $ENV{REMOTE_ADDR},
82 error(gettext("prohibited by allowed_attachments")." ($allowed)");
89 sub checkconfig () { #{{{
90 $config{cgi_disable_uploads}=0;
93 sub formbuilder_setup (@) { #{{{
95 my $form=$params{form};
98 if (defined $form->field("do") && ($form->field("do") eq "edit" ||
99 $form->field("do") eq "create")) {
100 # Add attachment field, set type to multipart.
101 $form->enctype(&CGI::MULTIPART);
102 $form->field(name => 'attachment', type => 'file');
103 # These buttons are not put in the usual place, so
104 # are not added to the normal formbuilder button list.
105 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
106 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
108 # Add the toggle javascript; the attachments interface uses
109 # it to toggle visibility.
110 require IkiWiki::Plugin::toggle;
111 $form->tmpl_param("javascript" => IkiWiki::Plugin::toggle::include_javascript($params{page}, 1));
112 # Start with the attachments interface toggled invisible,
113 # but if it was used, keep it open.
114 if ($form->submitted ne "Upload Attachment" &&
115 (! defined $q->param("attachment_select") ||
116 ! length $q->param("attachment_select"))) {
117 $form->tmpl_param("attachments-class" => "toggleable");
120 $form->tmpl_param("attachments-class" => "toggleable-open");
123 elsif ($form->title eq "preferences") {
124 # XXX deprecated, should remove eventually
125 my $session=$params{session};
126 my $user_name=$session->param("name");
128 $form->field(name => "allowed_attachments", size => 50,
130 comment => "deprecated; please move to allowed_attachments in setup file",
132 if (! IkiWiki::is_admin($user_name)) {
133 $form->field(name => "allowed_attachments", type => "hidden");
135 if (! $form->submitted) {
136 my $value=IkiWiki::userinfo_get($user_name, "allowed_attachments");
138 $form->field(name => "allowed_attachments", force => 1,
139 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
142 $form->field(name => "allowed_attachments", type => "hidden");
145 if ($form->submitted && $form->submitted eq 'Save Preferences') {
146 if (defined $form->field("allowed_attachments")) {
147 IkiWiki::userinfo_set($user_name, "allowed_attachments",
148 $form->field("allowed_attachments")) ||
149 error("failed to set allowed_attachments");
150 if (! length $form->field("allowed_attachments")) {
151 $form->field(name => "allowed_attachments", type => "hidden");
158 sub formbuilder (@) { #{{{
160 my $form=$params{form};
163 return if ! defined $form->field("do") || ($form->field("do") ne "edit" && $form->field("do") ne "create") ;
165 my $filename=$q->param('attachment');
166 if (defined $filename && length $filename &&
167 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
168 my $session=$params{session};
170 # This is an (apparently undocumented) way to get the name
171 # of the temp file that CGI writes the upload to.
172 my $tempfile=$q->tmpFileName($filename);
173 if (! defined $tempfile || ! length $tempfile) {
174 # perl 5.8 needs an alternative, awful method
175 if ($q =~ /HASH/ && exists $q->{'.tmpfiles'}) {
176 foreach my $key (keys(%{$q->{'.tmpfiles'}})) {
177 $tempfile=$q->tmpFileName(\$key);
178 last if defined $tempfile && length $tempfile;
181 if (! defined $tempfile || ! length $tempfile) {
182 error("CGI::tmpFileName failed to return the uploaded file name");
186 $filename=linkpage(IkiWiki::possibly_foolish_untaint(
187 attachment_location($form->field('page')).
188 IkiWiki::basename($filename)));
189 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
190 error(gettext("bad attachment filename"));
193 # Check that the user is allowed to edit a page with the
194 # name of the attachment.
195 IkiWiki::check_canedit($filename, $q, $session, 1);
196 # And that the attachment itself is acceptable.
197 check_canattach($session, $filename, $tempfile);
199 # Needed for fast_file_copy and for rendering below.
200 require IkiWiki::Render;
202 # Move the attachment into place.
203 # Try to use a fast rename; fall back to copying.
204 IkiWiki::prep_writefile($filename, $config{srcdir});
205 unlink($config{srcdir}."/".$filename);
206 if (rename($tempfile, $config{srcdir}."/".$filename)) {
207 # The temp file has tight permissions; loosen up.
208 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
211 my $fh=$q->upload('attachment');
212 if (! defined $fh || ! ref $fh) {
213 # needed by old CGI versions
214 $fh=$q->param('attachment');
215 if (! defined $fh || ! ref $fh) {
216 # even that doesn't always work,
217 # fall back to opening the tempfile
219 open($fh, "<", $tempfile) || error("failed to open \"$tempfile\": $!");
223 writefile($filename, $config{srcdir}, undef, 1, sub {
224 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
228 # Check the attachment in and trigger a wiki refresh.
230 IkiWiki::rcs_add($filename);
231 IkiWiki::disable_commit_hook();
232 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
233 IkiWiki::rcs_prepedit($filename),
234 $session->param("name"), $ENV{REMOTE_ADDR});
235 IkiWiki::enable_commit_hook();
236 IkiWiki::rcs_update();
239 IkiWiki::saveindex();
241 elsif ($form->submitted eq "Insert Links") {
242 my $page=quotemeta($q->param("page"));
244 foreach my $f ($q->param("attachment_select")) {
248 $form->field(name => 'editcontent',
249 value => $form->field('editcontent')."\n\n".$add,
250 force => 1) if length $add;
253 # Generate the attachment list only after having added any new
255 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
258 sub attachment_location ($) { #{{{
261 # Put the attachment in a subdir of the page it's attached
262 # to, unless that page is an "index" page.
263 $page=~s/(^|\/)index//;
264 $page.="/" if length $page;
269 sub attachment_list ($) { #{{{
271 my $loc=attachment_location($page);
274 foreach my $f (values %pagesources) {
275 if (! defined pagetype($f) &&
276 $f=~m/^\Q$loc\E[^\/]+$/ &&
277 -e "$config{srcdir}/$f") {
279 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'" />',
280 link => htmllink($page, $page, $f, noimageinline => 1),
281 size => IkiWiki::Plugin::filecheck::humansize((stat(_))[7]),
282 mtime => displaytime($IkiWiki::pagemtime{$f}),
283 mtime_raw => $IkiWiki::pagemtime{$f},
288 # Sort newer attachments to the top of the list, so a newly-added
289 # attachment appears just before the form used to add it.
290 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;