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