Added reCAPTCHA support to both user and project portions, but only the project porti...
[girocco.git] / cgi / regproj.cgi
bloba8edb1f46086d95f6c1123907dc7cfd9fa94059e
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;
13 use Captcha::reCAPTCHA;
15 my $gcgi = Girocco::CGI->new('Project Registration');
16 my $cgi = $gcgi->cgi;
18 my $captcha = Captcha::reCAPTCHA->new();
19 my %keys = Girocco::Util::get_keys();
21 my $name = $cgi->param('name');
22 $name ||= '';
24 my $fork = $cgi->param('fork');
25 if ($fork) {
26 $fork =~ s/\.git$//;
27 $name = "$fork/$name";
30 if ($cgi->param('mode')) {
31 # submitted, let's see
32 # FIXME: racy, do a lock
33 Girocco::Project::valid_name($name)
34 and Girocco::Project::does_exist($name)
35 and $gcgi->err("Project with the name '$name' already exists.");
36 $name =~ /\.git$/
37 and $gcgi->err("Project name should not end with <tt>.git</tt> - I'll add that automagically.");
39 my $challenge = $gcgi->cgi->param('recaptcha_challenge_field');
40 my $response = $gcgi->cgi->param('recaptcha_response_field');
41 my $result = $captcha->check_answer(%keys{private}, $ENV{'REMOTE_ADDR'}, $challenge, $response);
43 unless ($result->{is_valid}) {
44 print "<p>Failed captcha check, exiting.</p>";
45 exit;
48 my $mirror = $cgi->param('mode') eq 'mirror';
50 if ($mirror and $Girocco::Config::mirror_sources and not $cgi->param('url')) {
51 my $src = $cgi->param('source'); $src ||= '';
52 my $source; $src and $source = (grep { $_->{label} eq $src } @$Girocco::Config::mirror_sources)[0];
53 $source or $gcgi->err("Invalid or no mirror source $src specified");
55 my $n = $source->{label};
56 my $u = $source->{url};
57 if ($source->{inputs}) {
58 for my $i (0..$#{$source->{inputs}}) {
59 my $v = $cgi->param($n.'_i'.$i);
60 unless ($v) {
61 $gcgi->err("Source specifier '".$source->{inputs}->[$i]->{label}."' not filled.");
62 next;
64 my $ii = $i + 1;
65 $u =~ s/%$ii/$v/g;
67 } else {
68 $u = $cgi->param($n.'_url');
69 $u or $gcgi->err("Source URL not specified");
71 $cgi->param('url', $u);
74 my $proj = Girocco::Project->ghost($name, $mirror);
75 if ($proj->cgi_fill($gcgi)) {
76 if ($mirror) {
77 unless ($Girocco::Config::mirror) {
78 $gcgi->err("Mirroring mode is not enabled at this site.");
79 exit;
81 $proj->premirror;
82 print "<p>Please <a href=\"mirrorproj.cgi?name=$name\">pass onwards</a>.</p>\n";
83 print "<script language=\"javascript\">document.location='mirrorproj.cgi?name=$name'</script>\n";
85 } else {
86 unless ($Girocco::Config::push) {
87 $gcgi->err("Push mode is not enabled at this site.");
88 exit;
90 $proj->conjure;
91 print <<EOT;
92 <p>
93 Project <a href="$Girocco::Config::gitweburl/$name.git">$name</a> successfuly set up.</p>
94 EOT
95 print "<p>The push URL for the project is <tt>$Girocco::Config::pushurl/$name.git</tt>.</p>";
96 print "<p>The read-only URL for the project is <tt>" .
97 join("/$name.git</tt>, <tt>", $Girocco::Config::gitpullurl, $Girocco::Config::httppullurl) .
98 "/$name.git</tt>.</p>" if $Girocco::Config::gitpullurl or $Girocco::Config::httppullurl;
99 my $regnotice = '';
100 if ($Girocco::Config::manage_users) {
101 $regnotice = <<EOT;
102 Everyone who wants to push must <a href="reguser.cgi">register himself as a user</a> first.
103 (One user can have push access to multiple projects and multiple users can have push access to one project.)
106 print <<EOT;
107 <p>You can <a href="editproj.cgi?name=$name">assign users</a> now
108 - don't forget to assign yourself as a user as well if you want to push!
109 $regnotice
110 </p>
111 <p>Note that you cannot clone an empty repository since it contains no branches; you need to make the first push from an existing repository.
112 To import a new project, the procedure is roughly as follows:
113 <pre>
114 \$ git init
115 \$ git add
116 \$ git commit
117 \$ git remote add origin $Girocco::Config::pushurl/$name.git
118 \$ git push --all origin
119 </pre>
120 </p>
121 <p>You may experience permission problems if you try to push right now.
122 If so, that should get fixed automagically in few minutes, please be patient.</p>
123 <p>Enjoy yourself, and have a lot of fun!</p>
126 exit;
130 my $mirror_mode = {
131 name => 'mirror',
132 desc => 'our dedicated git monkeys will check another repository at a given URL every hour and mirror any new updates',
133 pwpurp => 'mirroring URL'
135 my $push_mode = {
136 name => 'push',
137 desc => 'registered users with appropriate permissions will be able to push to the repository',
138 pwpurp => 'list of users allowed to push'
141 my $me = $Girocco::Config::mirror ? $mirror_mode : undef;
142 my $pe = $Girocco::Config::push ? $push_mode : undef;
143 if ($me and $pe) {
144 print <<EOT;
145 <p>At this site, you can host a project in one of two modes: $me->{name} mode and $pe->{name} mode.
146 In the <b>$me->{name} mode</b>, $me->{desc}.
147 In the <b>$pe->{name} mode</b>, $pe->{desc}.
148 You currently cannot switch freely between those two modes;
149 if you want to switch from mirroring to push mode, just delete and recreate
150 the project. If you want to switch the other way, please contact the administrator.</p>
152 } else {
153 my $mode = $me ? $me : $pe;
154 print "<p>This site will host your project in a <b>$mode->{name} mode</b>: $mode->{desc}.</p>\n";
157 my @pwpurp = ();
158 push @pwpurp, $me->{pwpurp} if $me;
159 push @pwpurp, $pe->{pwpurp} if $pe;
160 my $pwpurp = join(', ', @pwpurp);
162 if ($Girocco::Config::project_passwords) {
163 print <<EOT;
164 <p>You will need the admin password to adjust the project settings later
165 ($pwpurp, project description, ...).</p>
169 unless ($name =~ m#/#) {
170 print <<EOT;
171 <p>Note that if your project is a <strong>fork of an existing project</strong>
172 (this does not mean anything socially bad), please instead go to the project's
173 gitweb page and click the 'fork' link in the top bar. This way, all of us
174 will save bandwidth and more importantly, your project will be properly categorized.
176 $me and print <<EOT;
177 If your project is a fork but the existing project is not registered here yet, please
178 consider registering it first; you do not have to be involved in the project
179 in order to register it here as a mirror.</p>
181 } else {
182 my $xname = $name; $xname =~ s#/$#.git#; #
183 my ($pushnote1, $pushnote2);
184 if ($pe) {
185 $pushnote1 = " and you will need to push only the data <em>you</em> created, not the whole project";
186 $pushnote2 = <<EOT;
187 (That will be done automagically, you do not need to specify any extra arguments during the push.
190 print <<EOT;
191 <p>Great, your project will be created as a subproject of the '$xname' project.
192 This means that it will be properly categorized$pushnote1. $pushnote2</p>
196 my $modechooser;
197 my $mirrorentry = '';
198 if ($me) {
199 $mirrorentry = '<tr id="mirror_url"><td class="formlabel">Mirror source:</td><td>';
200 if (!$Girocco::Config::mirror_sources) {
201 $mirrorentry .= '<input type="text" name="url" />';
202 } else {
203 $mirrorentry .= "<table>"."\n";
204 my $checked = ' checked=checked';
205 foreach my $source (@$Girocco::Config::mirror_sources) {
206 my $n = $source->{label};
207 $mirrorentry .= '<tr><td class="formlabel">';
208 $mirrorentry .= '<p><input type="radio" name="source" value="'.$n.'" '.$checked.' />';
209 $mirrorentry .= $n;
210 $mirrorentry .= '</p></td><td>';
211 $checked = '';
212 if ($source->{desc}) {
213 $mirrorentry .= '<p>';
214 $source->{link} and $mirrorentry .= '<a href="'.$source->{link}.'">';
215 $mirrorentry .= $source->{desc};
216 $source->{link} and $mirrorentry .= '</a>';
217 $mirrorentry .= '</p>';
219 if (!$source->{inputs}) {
220 $mirrorentry .= '<p>URL: <input type="text" name="'.$n.'_url" /></p>';
221 } else {
222 $mirrorentry .= '<p>';
223 my $i = 0;
224 foreach my $input (@{$source->{inputs}}) {
225 $mirrorentry .= $input->{label};
226 my ($l, $v) = ($n.'_i'.$i, '');
227 if ($cgi->param($l)) {
228 $v = ' value="'.html_esc($cgi->param($l)).'"';
230 $mirrorentry .= ' <input type="text" name="'.$l.'"'.$v.' />';
231 $mirrorentry .= $input->{suffix} if $input->{suffix};
232 $mirrorentry .= '&nbsp; &nbsp;';
233 } continue { $i++; }
234 $mirrorentry .= '</p>';
236 $mirrorentry .= '</td></tr>'."\n";
238 $mirrorentry .= "</table>";
240 $mirrorentry .= '</td></tr>';
242 if ($me and $pe) {
243 $modechooser = <<EOT;
244 <tr><td class="formlabel">Hosting mode:</td><td><p>
245 <input type="radio" name="mode" value="mirror" id="mirror_radio" checked="checked" />Mirror mode<br />
246 <input type="radio" name="mode" value="push" id="push_radio" />Push mode
247 </p></td></tr>
249 } else {
250 $modechooser = '<input type="hidden" name="mode" value="'.($me ? $me->{name} : $pe->{name}).'" />';
253 my $forkentry = '';
254 if ($name =~ m#/#) {
255 $name =~ s#^(.*)/##;
256 $forkentry = '<input type="hidden" name="fork" value="'.$1.'" />'.$1.'/'
259 print <<EOT;
260 $Girocco::Config::legalese
261 <form method="post">
262 <table class="form">
263 <tr><td class="formlabel">Project name:</td><td>$forkentry<input type="text" name="name" value="$name" />.git</td></tr>
265 if ($Girocco::Config::project_passwords) {
266 print <<EOT;
267 <tr><td class="formlabel">Admin password (twice):</td><td><input type="password" name="pwd" /><br /><input type="password" name="pwd2" /></td></tr>
270 if ($Girocco::Config::project_owners eq 'email') {
271 print <<EOT;
272 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" /></td></tr>
275 print $modechooser;
276 print $mirrorentry;
278 $gcgi->print_form_fields($Girocco::Project::metadata_fields, undef, @Girocco::Config::project_fields);
280 print <<EOT;
283 print <<EOT;
284 <tr><td class="formlabel">Captcha:</td><td>
287 print $captcha->get_html(%keys{public});
289 print <<EOT;
290 <tr><td></td><td><input type="submit" name="y0" value="Register" /></td></tr>
291 </table>
292 </form>