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