Stay inside the current URL scheme
[girocco.git] / cgi / delproj.cgi
blob454cfc2c2cee925a1a0a4382bacaba4a960fd3d4
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::valid_name($name)) {
25 print "<p>Invalid project name. Go away, sorcerer.</p>\n";
26 exit;
29 if (!Girocco::Project::does_exist($name)) {
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 $proj->{cpwd} = $cgi->param('cpwd');
37 my $isempty = !$proj->{mirror} && $proj->is_empty;
39 if ($proj->has_forks()) {
40 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";
41 exit;
44 if ($cgi->param('y0') && $proj->authenticate($gcgi)) {
45 # submitted
46 if (!$proj->{mirror} && !$isempty && !$cgi->param('auth')) {
47 my $auth = $proj->gen_auth;
49 # Send auth mail
50 open(MAIL, '|-', '/usr/bin/mail', '-s', "[$Girocco::Config::name] Project removal authorization", $proj->{email}) or
51 die "Sorry, could not send authorization code: $!";
52 print MAIL <<EOT;
53 Hello,
55 somebody requested a project removal authorization code to be sent for
56 project $name on $Girocco::Config::name. Since you are the project admin,
57 you receive the authorization code. If you don't want to actually remove
58 project $name, just ignore this e-mail. Otherwise, use this code
59 within 24 hours:
61 $auth
63 In case you did not request the removal authorization code, we apologize.
65 Should you run into any problems, please let us know.
67 Have fun!
68 EOT
69 close MAIL;
71 print <<EOT;
72 <p>The project admin should shortly receive an e-mail containing a project
73 removal authorization code. Please enter this code below to remove project
74 $name from $Girocco::Config::name. The code will expire in 24 hours or after
75 you have used it.</p>
76 <form method="post" action="delproj.cgi">
77 <input type="hidden" name="name" value="$name" />
78 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
79 <p>Authorization code: <input name="auth" size="40" /></p>
80 <p><input type="submit" name="y0" value="Remove" /></p>
81 </form>
82 EOT
83 exit;
85 if (!$proj->{mirror} && !$isempty) {
86 $proj->{auth} or do {
87 print <<EOT;
88 <p>There currently isn't any project removal authorization code on file for
89 project $name. Please <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$name"
90 >generate one</a>.</p>
91 EOT
92 exit;
94 my $auth = $gcgi->wparam('auth');
95 if ($auth ne $proj->{auth}) {
96 print <<EOT;
97 <p>Invalid authorization code, please re-enter or
98 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$name"
99 >generate a new one</a>.</p>
100 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi">
101 <input type="hidden" name="name" value="$name" />
102 <input type="hidden" name="cpwd" value="$proj->{cpwd}" />
103 <p>Authorization code: <input name="auth" size="40" /></p>
104 <p><input type="submit" name="y0" value="Remove" /></p>
105 </form>
107 exit;
109 $proj->del_auth;
111 if (!$proj->{mirror} && !$isempty) {
112 # archive the non-empty, non-mirror project before calling delete
114 my $destdir = $proj->{base_path};
115 $destdir =~ s,(?<=[^/])/+$,,;
116 $destdir .= "-recyclebin/";
117 $destdir .= $1 if $proj->{name} =~ m,^(.*/)[^/]+$,;
118 my $destbase = $proj->{name};
119 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
120 system('mkdir', '-p', $destdir) == 0 && -d $destdir
121 or die "mkdir -p $destdir failed: $?";
122 my $suffix = '';
123 if (-e "$destdir$destbase.git") {
124 $suffix = 1;
125 while (-e "$destdir$destbase~$suffix.git") {
126 ++$suffix;
127 last if $suffix >= 10000; # don't get too carried away
129 $suffix = '~'.$suffix;
131 not -e "$destdir$destbase$suffix.git"
132 or die "Unable to compute suitable archive path";
133 system('mv', $proj->{path}, "$destdir$destbase$suffix.git") == 0
134 or die "mv $proj->{path} $destdir$destbase$suffix.git failed: $?";
136 $proj->delete;
137 print "<p>Project successfully removed. Have a nice day.</p>\n";
138 exit;
141 my $fetchy = $Girocco::Config::gitpullurl || $Girocco::Config::httppullurl ||
142 $Girocco::Config::pushurl || $Girocco::Config::httpspushurl || $Girocco::Config::gitweburl;
143 my $url = $proj->{mirror} ? $proj->{url} : "$fetchy/$name.git";
144 my $type = $proj->{mirror} ? "mirrored " : ($isempty ? "empty " : "");
145 my $label = ($proj->{mirror} || $isempty) ? "Remove" : "Send authorization code";
147 print <<EOT;
148 <p>Please confirm that you are going to remove ${type}project
149 $name ($url) from the site.</p>
150 <form method="post" action="delproj.cgi">
151 <input type="hidden" name="name" value="$name" />
153 if ($Girocco::Config::project_passwords) {
154 print <<EOT;
155 <p>Admin password: <input type="password" name="cpwd" /> <sup><a
156 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$name">(forgot password?)</a></sup></p>
159 print <<EOT;
160 <p><input type="submit" name="y0" value="$label" /></p>
161 </form>