*.cgi: improve "not found that's really wierd" error
[girocco.git] / cgi / delproj.cgi
blob3e729cc600ae6378a45021a2330f821030840e37
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 my $auth = $proj->gen_auth('DEL');
60 # Send auth mail
61 defined(my $MAIL = mailer_pipe '-s', "[$Girocco::Config::name] Project removal authorization", $proj->{email}) or
62 die "Sorry, could not send authorization code: $!";
63 print $MAIL <<EOT;
64 Hello,
66 Somebody requested a project removal authorization code to be sent for
67 project $name on $Girocco::Config::name. Since you are the project admin,
68 you receive the authorization code. If you don't want to actually remove
69 project $name, just ignore this e-mail. Otherwise, use this code
70 within 24 hours:
72 $auth
74 In case you did not request the removal authorization code, we apologize.
76 Should you run into any problems, please let us know.
78 Have fun!
79 EOT
80 close $MAIL;
82 print <<EOT;
83 <p>The project admin should shortly receive an e-mail containing a project
84 removal authorization code. Please enter this code below to remove project
85 $name from $Girocco::Config::name. The code will expire in 24 hours or after
86 you have used it.</p>
87 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
88 <input type="hidden" name="name" value="$name" />
89 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
90 <p>Authorization code: <input name="auth" size="50" /></p>
91 <p><input type="submit" name="y0" value="Remove" /></p>
92 </form>
93 EOT
94 exit;
96 if ($y0 ne "Remove") {
97 print "<p>Invalid data. Go away, sorcerer.</p>\n";
98 exit;
100 if (!$proj->{mirror} && !$isempty) {
101 $proj->{auth} && $proj->{authtype} && $proj->{authtype} eq 'DEL' or do {
102 print <<EOT;
103 <p>There currently isn't any project removal authorization code on file for
104 project $name. Please <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname"
105 >generate one</a>.</p>
107 exit;
109 my $auth = $gcgi->wparam('auth');
110 if ($auth ne $proj->{auth}) {
111 print <<EOT;
112 <p>Invalid authorization code, please re-enter or
113 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname"
114 >generate a new one</a>.</p>
115 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
116 <input type="hidden" name="name" value="$name" />
117 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
118 <p>Authorization code: <input name="auth" size="50" /></p>
119 <p><input type="submit" name="y0" value="Remove" /></p>
120 </form>
122 exit;
124 $proj->del_auth;
126 if (!$proj->{mirror} && !$isempty) {
127 # archive the non-empty, non-mirror project before calling delete
129 my $destdir = $proj->{base_path};
130 $destdir =~ s,(?<=[^/])/+$,,;
131 $destdir .= "-recyclebin/";
132 $destdir .= $1 if $proj->{name} =~ m,^(.*/)[^/]+$,;
133 my $destbase = $proj->{name};
134 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
135 system('mkdir', '-p', $destdir) == 0 && -d $destdir
136 or die "mkdir -p $destdir failed: $?";
137 my $suffix = '';
138 if (-e "$destdir$destbase.git") {
139 $suffix = 1;
140 while (-e "$destdir$destbase~$suffix.git") {
141 ++$suffix;
142 last if $suffix >= 10000; # don't get too carried away
144 $suffix = '~'.$suffix;
146 not -e "$destdir$destbase$suffix.git"
147 or die "Unable to compute suitable archive path";
148 system('mv', $proj->{path}, "$destdir$destbase$suffix.git") == 0
149 or die "mv $proj->{path} $destdir$destbase$suffix.git failed: $?";
151 $proj->delete;
152 print "<p>Project successfully removed. Have a nice day.</p>\n";
153 exit;
156 my $fetchy = $Girocco::Config::gitpullurl || $Girocco::Config::httppullurl ||
157 $Girocco::Config::pushurl || $Girocco::Config::httpspushurl || $Girocco::Config::gitweburl;
158 my $url = $proj->{mirror} ? $proj->{url} : "$fetchy/$name.git";
159 my $type = $proj->{mirror} ? "mirrored " : ($isempty ? "empty " : "");
160 my $label = ($proj->{mirror} || $isempty) ? "Remove" : "Send authorization code";
162 print <<EOT;
163 <p>Please confirm that you are going to remove ${type}project
164 $name ($url) from the site.</p>
165 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
166 <input type="hidden" name="name" value="$name" />
168 if ($Girocco::Config::project_passwords) {
169 print <<EOT;
170 <p>Admin password: <input type="password" name="cpwd" /> <sup><a
171 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname">(forgot password?)</a></sup></p>
174 print <<EOT;
175 <p><input type="submit" name="y0" value="$label" /></p>
176 </form>