4ff9129ce806202565cf93fda629aa8f77c25eb4
[girocco.git] / cgi / delproj.cgi
blob4ff9129ce806202565cf93fda629aa8f77c25eb4
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 $proj or die "not found project $name, that's really weird!";
36 my $escname = $name;
37 $escname =~ s/[+]/%2B/g;
38 $proj->{cpwd} = $cgi->param('cpwd');
39 my $isempty = !$proj->{mirror} && $proj->is_empty;
41 if ($proj->has_forks()) {
42 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";
43 exit;
46 my $y0 = $cgi->param('y0') || '';
47 if ($y0 && $cgi->request_method eq 'POST' && $proj->authenticate($gcgi)) {
48 # submitted
49 if (!$proj->{mirror} && !$isempty && !$cgi->param('auth')) {
50 if ($y0 ne 'Send authorization code') {
51 print "<p>Invalid data. Go away, sorcerer.</p>\n";
52 exit;
55 my $auth = $proj->gen_auth;
57 # Send auth mail
58 defined(my $MAIL = mailer_pipe '-s', "[$Girocco::Config::name] Project removal authorization", $proj->{email}) or
59 die "Sorry, could not send authorization code: $!";
60 print $MAIL <<EOT;
61 Hello,
63 Somebody requested a project removal authorization code to be sent for
64 project $name on $Girocco::Config::name. Since you are the project admin,
65 you receive the authorization code. If you don't want to actually remove
66 project $name, just ignore this e-mail. Otherwise, use this code
67 within 24 hours:
69 $auth
71 In case you did not request the removal authorization code, we apologize.
73 Should you run into any problems, please let us know.
75 Have fun!
76 EOT
77 close $MAIL;
79 print <<EOT;
80 <p>The project admin should shortly receive an e-mail containing a project
81 removal authorization code. Please enter this code below to remove project
82 $name from $Girocco::Config::name. The code will expire in 24 hours or after
83 you have used it.</p>
84 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
85 <input type="hidden" name="name" value="$name" />
86 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
87 <p>Authorization code: <input name="auth" size="50" /></p>
88 <p><input type="submit" name="y0" value="Remove" /></p>
89 </form>
90 EOT
91 exit;
93 if ($y0 ne "Remove") {
94 print "<p>Invalid data. Go away, sorcerer.</p>\n";
95 exit;
97 if (!$proj->{mirror} && !$isempty) {
98 $proj->{auth} or do {
99 print <<EOT;
100 <p>There currently isn't any project removal authorization code on file for
101 project $name. Please <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname"
102 >generate one</a>.</p>
104 exit;
106 my $auth = $gcgi->wparam('auth');
107 if ($auth ne $proj->{auth}) {
108 print <<EOT;
109 <p>Invalid authorization code, please re-enter or
110 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname"
111 >generate a new one</a>.</p>
112 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
113 <input type="hidden" name="name" value="$name" />
114 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
115 <p>Authorization code: <input name="auth" size="50" /></p>
116 <p><input type="submit" name="y0" value="Remove" /></p>
117 </form>
119 exit;
121 $proj->del_auth;
123 if (!$proj->{mirror} && !$isempty) {
124 # archive the non-empty, non-mirror project before calling delete
126 my $destdir = $proj->{base_path};
127 $destdir =~ s,(?<=[^/])/+$,,;
128 $destdir .= "-recyclebin/";
129 $destdir .= $1 if $proj->{name} =~ m,^(.*/)[^/]+$,;
130 my $destbase = $proj->{name};
131 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
132 system('mkdir', '-p', $destdir) == 0 && -d $destdir
133 or die "mkdir -p $destdir failed: $?";
134 my $suffix = '';
135 if (-e "$destdir$destbase.git") {
136 $suffix = 1;
137 while (-e "$destdir$destbase~$suffix.git") {
138 ++$suffix;
139 last if $suffix >= 10000; # don't get too carried away
141 $suffix = '~'.$suffix;
143 not -e "$destdir$destbase$suffix.git"
144 or die "Unable to compute suitable archive path";
145 system('mv', $proj->{path}, "$destdir$destbase$suffix.git") == 0
146 or die "mv $proj->{path} $destdir$destbase$suffix.git failed: $?";
148 $proj->delete;
149 print "<p>Project successfully removed. Have a nice day.</p>\n";
150 exit;
153 my $fetchy = $Girocco::Config::gitpullurl || $Girocco::Config::httppullurl ||
154 $Girocco::Config::pushurl || $Girocco::Config::httpspushurl || $Girocco::Config::gitweburl;
155 my $url = $proj->{mirror} ? $proj->{url} : "$fetchy/$name.git";
156 my $type = $proj->{mirror} ? "mirrored " : ($isempty ? "empty " : "");
157 my $label = ($proj->{mirror} || $isempty) ? "Remove" : "Send authorization code";
159 print <<EOT;
160 <p>Please confirm that you are going to remove ${type}project
161 $name ($url) from the site.</p>
162 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
163 <input type="hidden" name="name" value="$name" />
165 if ($Girocco::Config::project_passwords) {
166 print <<EOT;
167 <p>Admin password: <input type="password" name="cpwd" /> <sup><a
168 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname">(forgot password?)</a></sup></p>
171 print <<EOT;
172 <p><input type="submit" name="y0" value="$label" /></p>
173 </form>