2 package IkiWiki::Plugin::attachment;
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);
14 sub checkconfig () { #{{{
15 $config{cgi_disable_uploads}=0;
18 sub formbuilder_setup (@) { #{{{
20 my $form=$params{form};
23 if ($form->field("do") eq "edit") {
24 $form->field(name => 'attachment', type => 'file');
25 # These buttons are not put in the usual place, so
26 # are not added to the normal formbuilder button list.
27 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
28 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
30 # Add the javascript from the toggle plugin;
31 # the attachments interface uses it to toggle visibility.
32 require IkiWiki::Plugin::toggle;
33 $form->tmpl_param("javascript" => $IkiWiki::Plugin::toggle::javascript);
34 # Start with the attachments interface toggled invisible,
35 # but if it was used, keep it open.
36 if ($form->submitted ne "Upload Attachment" &&
37 ! length $q->param("attachment_select")) {
38 $form->tmpl_param("attachments-class" => "toggleable");
41 $form->tmpl_param("attachments-class" => "toggleable-open");
44 elsif ($form->title eq "preferences") {
45 my $session=$params{session};
46 my $user_name=$session->param("name");
48 $form->field(name => "allowed_attachments", size => 50,
50 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
51 if (! IkiWiki::is_admin($user_name)) {
52 $form->field(name => "allowed_attachments", type => "hidden");
54 if (! $form->submitted) {
55 $form->field(name => "allowed_attachments", force => 1,
56 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
58 if ($form->submitted && $form->submitted eq 'Save Preferences') {
59 if (defined $form->field("allowed_attachments")) {
60 IkiWiki::userinfo_set($user_name, "allowed_attachments",
61 $form->field("allowed_attachments")) ||
62 error("failed to set allowed_attachments");
68 sub formbuilder (@) { #{{{
70 my $form=$params{form};
73 return if $form->field("do") ne "edit";
75 my $filename=$q->param('attachment');
76 if (defined $filename && length $filename &&
77 ($form->submitted eq "Upload Attachment" || $form->submitted eq "Save Page")) {
78 my $session=$params{session};
80 # This is an (apparently undocumented) way to get the name
81 # of the temp file that CGI writes the upload to.
82 my $tempfile=$q->tmpFileName($filename);
84 $filename=IkiWiki::titlepage(
85 IkiWiki::possibly_foolish_untaint(
86 attachment_location($form->field('page')).
87 IkiWiki::basename($filename)));
88 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
89 error(gettext("bad attachment filename"));
92 # Check that the user is allowed to edit a page with the
93 # name of the attachment.
94 IkiWiki::check_canedit($filename, $q, $session, 1);
96 # Use a special pagespec to test that the attachment is valid.
98 foreach my $admin (@{$config{adminuser}}) {
99 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
100 if (defined $allowed_attachments &&
101 length $allowed_attachments) {
102 $allowed=pagespec_match($filename,
103 $allowed_attachments,
109 error(gettext("attachment rejected")." ($allowed)");
112 # Needed for fast_file_copy and for rendering below.
113 require IkiWiki::Render;
115 # Move the attachment into place.
116 # Try to use a fast rename; fall back to copying.
117 IkiWiki::prep_writefile($filename, $config{srcdir});
118 unlink($config{srcdir}."/".$filename);
119 if (rename($tempfile, $config{srcdir}."/".$filename)) {
120 # The temp file has tight permissions; loosen up.
121 chmod(0666 & ~umask, $config{srcdir}."/".$filename);
124 my $fh=$q->upload('attachment');
125 if (! defined $fh || ! ref $fh) {
126 error("failed to get filehandle");
129 writefile($filename, $config{srcdir}, undef, 1, sub {
130 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
134 # Check the attachment in and trigger a wiki refresh.
136 IkiWiki::rcs_add($filename);
137 IkiWiki::disable_commit_hook();
138 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
139 IkiWiki::rcs_prepedit($filename),
140 $session->param("name"), $ENV{REMOTE_ADDR});
141 IkiWiki::enable_commit_hook();
142 IkiWiki::rcs_update();
145 IkiWiki::saveindex();
147 elsif ($form->submitted eq "Insert Links") {
149 foreach my $f ($q->param("attachment_select")) {
152 $form->field(name => 'editcontent',
153 value => $form->field('editcontent')."\n\n".$add,
154 force => 1) if length $add;
157 # Generate the attachment list only after having added any new
159 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
162 sub attachment_location ($) {
165 # Put the attachment in a subdir of the page it's attached
166 # to, unless that page is an "index" page.
167 $page=~s/(^|\/)index//;
168 $page.="/" if length $page;
173 sub attachment_list ($) {
175 my $loc=attachment_location($page);
178 foreach my $f (values %pagesources) {
179 if (! defined IkiWiki::pagetype($f) &&
180 $f=~m/^\Q$loc\E[^\/]+$/ &&
181 -e "$config{srcdir}/$f") {
183 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">',
184 link => htmllink($page, $page, $f, noimageinline => 1),
185 size => humansize((stat(_))[7]),
186 mtime => displaytime($IkiWiki::pagemtime{$f}),
187 mtime_raw => $IkiWiki::pagemtime{$f},
192 # Sort newer attachments to the top of the list, so a newly-added
193 # attachment appears just before the form used to add it.
194 return sort { $b->{mtime_raw} <=> $a->{mtime_raw} || $a->{link} cmp $b->{link} } @ret;
197 my %units=( # size in bytes
222 zettabyte => 2 ** 70,
225 yottabyte => 2 ** 80,
226 # ikiwiki, if you find you need larger data quantities, either modify
227 # yourself to add them, or travel back in time to 2008 and kill me.
231 sub parsesize ($) { #{{{
235 my $base=$size+0; # force to number
237 foreach my $unit (sort keys %units) {
238 if ($size=~/[0-9\s]\Q$unit\E$/i) {
239 return $base * $units{$unit};
245 sub humansize ($) { #{{{
248 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
249 if ($size / $units{$unit} > 0.25) {
250 return (int($size / $units{$unit} * 10)/10).$unit;
253 return $size; # near zero, or negative
256 package IkiWiki::PageSpec;
258 sub match_maxsize ($$;@) { #{{{
260 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
262 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
266 if (! exists $params{file}) {
267 return IkiWiki::FailReason->new("no file specified");
270 if (-s $params{file} > $maxsize) {
271 return IkiWiki::FailReason->new("file too large (".(-s $params{file})." > $maxsize)");
274 return IkiWiki::SuccessReason->new("file not too large");
278 sub match_minsize ($$;@) { #{{{
280 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
282 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
286 if (! exists $params{file}) {
287 return IkiWiki::FailReason->new("no file specified");
290 if (-s $params{file} < $minsize) {
291 return IkiWiki::FailReason->new("file too small");
294 return IkiWiki::SuccessReason->new("file not too small");
298 sub match_ispage ($$;@) { #{{{
301 if (defined IkiWiki::pagetype($filename)) {
302 return IkiWiki::SuccessReason->new("file is a wiki page");
305 return IkiWiki::FailReason->new("file is not a wiki page");