include temp file for attachment change too
[ikiwiki.git] / IkiWiki / Receive.pm
1 #!/usr/bin/perl
2
3 package IkiWiki::Receive;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 sub getuser () { #{{{
10         my $user=(getpwuid($<))[0];
11         if (! defined $user) {
12                 error("cannot determine username for $<");
13         }
14         return $user;
15 } #}}}
16
17 sub trusted () { #{{{
18         my $user=getuser();
19         return ! ref $config{untrusted_committers} ||
20                 ! grep { $_ eq $user } @{$config{untrusted_committers}};
21 } #}}}
22
23 sub test () { #{{{
24         exit 0 if trusted();
25         
26         # Dummy up a cgi environment to use when calling check_canedit
27         # and friends.
28         eval q{use CGI};
29         error($@) if $@;
30         my $cgi=CGI->new;
31         require IkiWiki::CGI;
32         my $session=IkiWiki::cgi_getsession($cgi);
33         $session->param("name", getuser());
34         $ENV{REMOTE_ADDR}='unknown' unless exists $ENV{REMOTE_ADDR};
35
36         IkiWiki::lockwiki();
37         IkiWiki::loadindex();
38
39         my %newfiles;
40
41         foreach my $change (IkiWiki::rcs_receive()) {
42                 # This untaint is safe because we check file_pruned and
43                 # wiki_file_regexp.
44                 my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
45                 $file=IkiWiki::possibly_foolish_untaint($file);
46                 if (! defined $file || ! length $file ||
47                     IkiWiki::file_pruned($file, $config{srcdir})) {
48                         error(gettext("bad file name %s"), $file);
49                 }
50
51                 my $type=pagetype($file);
52                 my $page=pagename($file) if defined $type;
53                 
54                 if ($change->{action} eq 'add') {
55                         $newfiles{$file}=1;
56                 }
57
58                 if ($change->{action} eq 'change' ||
59                     $change->{action} eq 'add') {
60                         if (defined $page) {
61                                 if (IkiWiki->can("check_canedit")) {
62                                     IkiWiki::check_canedit($page, $cgi, $session);
63                                     next;
64                                 }
65                         }
66                         else {
67                                 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
68                                         IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
69                                         next;
70                                 }
71                         }
72                 }
73                 elsif ($change->{action} eq 'remove') {
74                         # check_canremove tests to see if the file is present
75                         # on disk. This will fail is a single commit adds a
76                         # file and then removes it again. Avoid the problem
77                         # by not testing the removal in such pairs of changes.
78                         # (The add is still tested, just to make sure that
79                         # no data is added to the repo that a web edit
80                         # could add.)
81                         next if $newfiles{$file};
82
83                         if (IkiWiki::Plugin::remove->can("check_canremove")) {
84                                 IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
85                                 next;
86                         }
87                 }
88                 else {
89                         error "unknown action ".$change->{action};
90                 }
91                 
92                 error sprintf(gettext("you are not allowed to change %s"), $file);
93         }
94
95         exit 0;
96 } #}}}
97
98 1