update.sh: do nothing if .nofetch exists
[girocco/readme.git] / cgi / delproj.cgi
blob8ddc514ae8636b4aa4192b8e66192c41e26f61df
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # GPLv2
5 use strict;
6 use warnings;
8 use lib ".";
9 use Girocco::CGI;
10 use Girocco::Config;
11 use Girocco::Project;
12 use Girocco::Util;
14 my $gcgi = Girocco::CGI->new('Project Removal');
15 my $cgi = $gcgi->cgi;
17 my $name = $cgi->param('name');
19 unless (defined $name) {
20 print "<p>I need the project name as an argument now.</p>\n";
21 exit;
24 if (!Girocco::Project::does_exist($name,1) && !Girocco::Project::valid_name($name)) {
25 print "<p>Invalid project name. Go away, sorcerer.</p>\n";
26 exit;
29 if (!Girocco::Project::does_exist($name,1)) {
30 print "<p>Sorry but this project does not exist. Now, how did you <em>get</em> here?!</p>\n";
31 exit;
34 my $proj = Girocco::Project->load($name);
35 if (!$proj) {
36 print "<p>not found project $name, that's really weird!</p>\n";
37 exit;
39 my $escname = $name;
40 $escname =~ s/[+]/%2B/g;
41 $proj->{cpwd} = $cgi->param('cpwd');
42 my $isempty = !$proj->{mirror} && $proj->is_empty;
44 if ($proj->has_forks()) {
45 print "<p>Sorry but this project has forks associated. Such projects cannot be removed. Please tell the administrator if you really want to.</p>\n";
46 exit;
49 my $y0 = $cgi->param('y0') || '';
50 if ($y0 && $cgi->request_method eq 'POST' && $proj->authenticate($gcgi)) {
51 # submitted
52 if (!$proj->{mirror} && !$isempty && !$cgi->param('auth')) {
53 if ($y0 ne 'Send authorization code') {
54 print "<p>Invalid data. Go away, sorcerer.</p>\n";
55 exit;
58 valid_email($proj->{email}) or die "Sorry, this project cannot be removed.";
60 my $auth = $proj->gen_auth('DEL');
62 # Send auth mail
63 defined(my $MAIL = mailer_pipe '-s', "[$Girocco::Config::name] Project removal authorization", $proj->{email}) or
64 die "Sorry, could not send authorization code: $!";
65 print $MAIL <<EOT;
66 Hello,
68 Somebody requested a project removal authorization code to be sent for
69 project $name on $Girocco::Config::name. Since you are the project admin,
70 you receive the authorization code. If you don't want to actually remove
71 project $name, just ignore this e-mail. Otherwise, use this code
72 within 24 hours:
74 $auth
76 In case you did not request the removal authorization code, we apologize.
78 Should you run into any problems, please let us know.
80 Have fun!
81 EOT
82 close $MAIL;
84 print <<EOT;
85 <p>The project admin should shortly receive an e-mail containing a project
86 removal authorization code. Please enter this code below to remove project
87 $name from $Girocco::Config::name. The code will expire in 24 hours or after
88 you have used it.</p>
89 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
90 <input type="hidden" name="name" value="$name" />
91 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
92 <p>Authorization code: <input name="auth" size="50" /></p>
93 <p><input type="submit" name="y0" value="Remove" /></p>
94 </form>
95 EOT
96 exit;
98 if ($y0 ne "Remove") {
99 print "<p>Invalid data. Go away, sorcerer.</p>\n";
100 exit;
102 if (!$proj->{mirror} && !$isempty) {
103 $proj->{auth} && $proj->{authtype} && $proj->{authtype} eq 'DEL' or do {
104 print <<EOT;
105 <p>There currently isn't any project removal authorization code on file for
106 project $name. Please <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname"
107 >generate one</a>.</p>
109 exit;
111 my $auth = $gcgi->wparam('auth');
112 if ($auth ne $proj->{auth}) {
113 print <<EOT;
114 <p>Invalid authorization code, please re-enter or
115 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname"
116 >generate a new one</a>.</p>
117 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
118 <input type="hidden" name="name" value="$name" />
119 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
120 <p>Authorization code: <input name="auth" size="50" /></p>
121 <p><input type="submit" name="y0" value="Remove" /></p>
122 </form>
124 exit;
126 $proj->del_auth;
128 if (!$proj->{mirror} && !$isempty) {
129 # archive the non-empty, non-mirror project before calling delete
131 use POSIX qw(strftime);
132 my $destdir = $proj->{base_path};
133 $destdir =~ s,(?<=[^/])/+$,,;
134 $destdir .= "/_recyclebin/";
135 $destdir .= $1 if $proj->{name} =~ m,^(.*/)[^/]+$,;
136 my $destbase = $proj->{name};
137 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
138 my $oldmask = umask();
139 umask($oldmask & ~0070);
140 system('mkdir', '-p', $destdir) == 0 && -d $destdir
141 or die "mkdir -p $destdir failed: $?";
142 umask($oldmask);
143 my $suffix = '';
144 if (-e "$destdir$destbase.git") {
145 $suffix = 1;
146 while (-e "$destdir$destbase~$suffix.git") {
147 ++$suffix;
148 last if $suffix >= 10000; # don't get too carried away
150 $suffix = '~'.$suffix;
152 not -e "$destdir$destbase$suffix.git"
153 or die "Unable to compute suitable archive path";
154 system('mv', $proj->{path}, "$destdir$destbase$suffix.git") == 0
155 or die "mv $proj->{path} $destdir$destbase$suffix.git failed: $?";
156 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
157 my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
158 # We ought to do something if this fails, but the project has already been moved
159 # so there's really nothing to be done at this point.
160 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
161 'config', 'girocco.recycletime', $recycletime);
163 $proj->delete;
164 print "<p>Project successfully removed. Have a nice day.</p>\n";
165 exit;
168 my $fetchy = $Girocco::Config::gitpullurl || $Girocco::Config::httppullurl ||
169 $Girocco::Config::pushurl || $Girocco::Config::httpspushurl || $Girocco::Config::gitweburl;
170 my $url = $proj->{mirror} ? $proj->{url} : "$fetchy/$name.git";
171 my $type = $proj->{mirror} ? "mirrored " : ($isempty ? "empty " : "");
172 my $label = ($proj->{mirror} || $isempty) ? "Remove" : "Send authorization code";
174 print <<EOT;
175 <p>Please confirm that you are going to remove ${type}project
176 $name ($url) from the site.</p>
177 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
178 <input type="hidden" name="name" value="$name" />
180 if ($Girocco::Config::project_passwords) {
181 print <<EOT;
182 <p>Admin password: <input type="password" name="cpwd" /> <sup><a
183 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname">(forgot password?)</a></sup></p>
186 print <<EOT;
187 <p><input type="submit" name="y0" value="$label" /></p>
188 </form>