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 attachment_location ($) {
21 # Put the attachment in a subdir of the page it's attached
22 # to, unless that page is an "index" page.
23 $page=~s/(^|\/)index//;
24 $page.="/" if length $page;
29 sub attachment_list ($) {
31 my $loc=attachment_location($page);
34 foreach my $f (values %pagesources) {
35 if (! defined IkiWiki::pagetype($f) &&
36 $f=~m/^\Q$loc\E[^\/]+$/ &&
37 -e "$config{srcdir}/$f") {
39 "field-select" => '<input type="checkbox" name="attachment_select" value="'.$f.'">',
40 link => htmllink($page, $page, $f, noimageinline => 1),
41 size => humansize((stat(_))[7]),
42 mtime => displaytime($IkiWiki::pagemtime{$f}),
50 sub formbuilder_setup (@) { #{{{
52 my $form=$params{form};
54 if ($form->field("do") eq "edit") {
55 $form->field(name => 'attachment', type => 'file');
56 $form->tmpl_param("attachment_list" => [attachment_list($form->field('page'))]);
58 # These buttons are not put in the usual place, so
59 # is not added to the normal formbuilder button list.
60 $form->tmpl_param("field-upload" => '<input name="_submit" type="submit" value="Upload Attachment" />');
61 $form->tmpl_param("field-link" => '<input name="_submit" type="submit" value="Insert Links" />');
63 elsif ($form->title eq "preferences") {
64 my $session=$params{session};
65 my $user_name=$session->param("name");
67 $form->field(name => "allowed_attachments", size => 50,
69 comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
70 if (! IkiWiki::is_admin($user_name)) {
71 $form->field(name => "allowed_attachments", type => "hidden");
73 if (! $form->submitted) {
74 $form->field(name => "allowed_attachments", force => 1,
75 value => IkiWiki::userinfo_get($user_name, "allowed_attachments"));
77 if ($form->submitted && $form->submitted eq 'Save Preferences') {
78 if (defined $form->field("allowed_attachments")) {
79 IkiWiki::userinfo_set($user_name, "allowed_attachments",
80 $form->field("allowed_attachments")) ||
81 error("failed to set allowed_attachments");
87 sub formbuilder (@) { #{{{
89 my $form=$params{form};
92 return if $form->field("do") ne "edit";
94 if ($form->submitted eq "Upload" || $form->submitted eq "Save Page") {
95 my $session=$params{session};
97 my $filename=$q->param('attachment');
98 if (! defined $filename || ! length $filename) {
99 # no file, so do nothing
103 # This is an (apparently undocumented) way to get the name
104 # of the temp file that CGI writes the upload to.
105 my $tempfile=$q->tmpFileName($filename);
107 $filename=IkiWiki::titlepage(
108 IkiWiki::possibly_foolish_untaint(
109 attachment_location($form->field('page')).
110 IkiWiki::basename($filename)));
111 if (IkiWiki::file_pruned($filename, $config{srcdir})) {
112 error(gettext("bad attachment filename"));
115 # Check that the user is allowed to edit a page with the
116 # name of the attachment.
117 IkiWiki::check_canedit($filename, $q, $session, 1);
119 # Use a special pagespec to test that the attachment is valid.
121 foreach my $admin (@{$config{adminuser}}) {
122 my $allowed_attachments=IkiWiki::userinfo_get($admin, "allowed_attachments");
123 if (defined $allowed_attachments &&
124 length $allowed_attachments) {
125 $allowed=pagespec_match($filename,
126 $allowed_attachments,
132 error(gettext("attachment rejected")." ($allowed)");
135 # Needed for fast_file_copy and for rendering below.
136 require IkiWiki::Render;
138 # Move the attachment into place.
139 # Try to use a fast rename; fall back to copying.
140 IkiWiki::prep_writefile($filename, $config{srcdir});
141 unlink($config{srcdir}."/".$filename);
142 if (! rename($tempfile, $config{srcdir}."/".$filename)) {
143 my $fh=$q->upload('attachment');
144 if (! defined $fh || ! ref $fh) {
145 error("failed to get filehandle");
148 writefile($filename, $config{srcdir}, undef, 1, sub {
149 IkiWiki::fast_file_copy($tempfile, $filename, $fh, @_);
153 # Check the attachment in and trigger a wiki refresh.
155 IkiWiki::rcs_add($filename);
156 IkiWiki::disable_commit_hook();
157 IkiWiki::rcs_commit($filename, gettext("attachment upload"),
158 IkiWiki::rcs_prepedit($filename),
159 $session->param("name"), $ENV{REMOTE_ADDR});
160 IkiWiki::enable_commit_hook();
161 IkiWiki::rcs_update();
164 IkiWiki::saveindex();
166 elsif ($form->submitted eq "Insert Links") {
168 foreach my $f ($q->param("attachment_select")) {
171 $form->field(name => 'editcontent',
172 value => $form->field('editcontent')."\n\n".$add,
177 my %units=( # size in bytes
202 zettabyte => 2 ** 70,
205 yottabyte => 2 ** 80,
206 # ikiwiki, if you find you need larger data quantities, either modify
207 # yourself to add them, or travel back in time to 2008 and kill me.
211 sub parsesize ($) { #{{{
215 my $base=$size+0; # force to number
217 foreach my $unit (sort keys %units) {
218 if ($size=~/\Q$unit\E/i) {
219 return $base * $units{$unit};
225 sub humansize ($) { #{{{
228 foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
229 if ($size / $units{$unit} > 0.25) {
230 return (int($size / $units{$unit} * 10)/10)."$unit";
233 return $size; # near zero, or negative
236 package IkiWiki::PageSpec;
238 sub match_maxsize ($$;@) { #{{{
240 my $maxsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
242 return IkiWiki::FailReason->new("unable to parse maxsize (or number too large)");
246 if (! exists $params{file}) {
247 return IkiWiki::FailReason->new("no file specified");
250 if (-s $params{file} > $maxsize) {
251 return IkiWiki::FailReason->new("file too large");
254 return IkiWiki::SuccessReason->new("file not too large");
258 sub match_minsize ($$;@) { #{{{
260 my $minsize=eval{IkiWiki::Plugin::attachment::parsesize(shift)};
262 return IkiWiki::FailReason->new("unable to parse minsize (or number too large)");
266 if (! exists $params{file}) {
267 return IkiWiki::FailReason->new("no file specified");
270 if (-s $params{file} < $minsize) {
271 return IkiWiki::FailReason->new("file too small");
274 return IkiWiki::SuccessReason->new("file not too small");
278 sub match_ispage ($$;@) { #{{{
281 if (defined IkiWiki::pagetype($filename)) {
282 return IkiWiki::SuccessReason->new("file is a wiki page");
285 return IkiWiki::FailReason->new("file is not a wiki page");