toolbox/update-all-config.pl: quiet means quiet
[girocco.git] / cgi / regproj.cgi
blobed24703b3c2914a51be152d5215fbddf7206691c
1 #!/usr/bin/perl
2 # (c) Petr Baudis <pasky@suse.cz>
3 # GPLv2
5 use strict;
6 use warnings;
8 use lib "__BASEDIR__";
9 use Girocco::CGI;
10 use Girocco::Config;
11 use Girocco::Project;
12 use Girocco::Util;
14 my $gcgi = Girocco::CGI->new('Project Registration');
15 my $cgi = $gcgi->cgi;
17 my $name = $cgi->param('name');
18 defined($name) or $name = '';
20 my $fork = $cgi->param('fork');
21 if (defined($fork)) {
22 $fork =~ s/\.git$//;
23 $name = "$fork/$name";
25 my $escname = $name;
26 $escname =~ s/[+]/%2B/g;
27 my $mirror_mode_set = 1;
28 if ($Girocco::Config::mirror && $Girocco::Config::push) {
29 $mirror_mode_set = 0 unless ($Girocco::Config::initial_regproj_mode||'') eq 'mirror';
31 my %values = (
32 desc => '',
33 email => '',
34 hp => '',
35 mirror => $mirror_mode_set,
36 cleanmirror => 1,
37 notifymail => '',
38 reverseorder => 1,
39 summaryonly => '',
40 notifytag => '',
41 notifyjson => '',
42 notifycia => '',
43 README => '',
44 rmtype => 'Markdown',
45 source => 'Anywhere',
46 url => '',
47 Anywhere_url => '',
48 GitHub_i0 => '',
49 GitHub_i1 => '',
50 Gitorious_i0 => '',
51 Gitorious_i1 => '',
53 $values{'mirror'} = 0 unless $Girocco::Config::mirror;
54 $values{'mirror'} = 0 if $Girocco::Config::push && $name =~ m#/#;
55 if (@{[$name =~ m#/#g]} > 5) {
56 $gcgi->err("Unable to create a fork more than five levels deep, please fork the parent project instead.");
57 exit;
59 my $y0 = $cgi->param('y0') || '';
60 my $tok = $cgi->param('token') || '';
61 if ($cgi->param('mode') && $y0 eq 'Register' && $cgi->request_method eq 'POST') {
62 # Check for token validity
63 if (!check_timed_token($tok, "projedit", "", $Girocco::Config::project_edit_timeout)) {
64 $gcgi->err("Session has timed out or is invalid, please try again.");
66 # submitted, let's see
67 # FIXME: racy, do a lock
68 my $validname = 1;
69 if (Girocco::Project::valid_name($name)) {
70 Girocco::Project::does_exist($name,1)
71 and $gcgi->err("Project with the name '$name' already exists.");
72 } else {
73 $validname = 0;
74 if ($name =~ /^(.*)\.git$/i && Girocco::Project::valid_name($1)) {
75 $gcgi->err("Project name should not end with <tt>.git</tt> - I'll add that automagically.");
76 } else {
77 my $htmlname = html_esc($name);
78 $gcgi->err(
79 "Invalid project name \"$htmlname\" ".
80 "(contains bad characters or is a reserved project name). ".
81 "See <a href=\"@{[url_path($Girocco::Config::htmlurl)]}/names.html\">names</a>.");
85 my $check = $cgi->param('mail');
86 $check =~ tr/ \t/ /s; $check =~ s/^ //; $check =~ s/ $//;
87 if ($check !~ /^(?:(?:(?:the )?sun)|(?:sol))$/i) {
88 $gcgi->err("Sorry, invalid captcha check.");
91 foreach my $key (keys(%values)) {
92 $values{$key} = html_esc($cgi->param($key));
94 my $mirror = ($cgi->param('mode')||'') eq 'mirror';
95 $values{'mirror'} = $Girocco::Config::mirror && $mirror ? 1 : 0;
97 if ($mirror and $Girocco::Config::mirror_sources and not $cgi->param('url')) {
98 my $src = $cgi->param('source'); $src ||= '';
99 my $source; $src and $source = (grep { $_->{label} eq $src } @$Girocco::Config::mirror_sources)[0];
100 $source or $gcgi->err("Invalid or no mirror source $src specified");
102 my $n = $source->{label};
103 my $u = $source->{url};
104 if ($source->{inputs}) {
105 for my $i (0..$#{$source->{inputs}}) {
106 my $v = $cgi->param($n.'_i'.$i);
107 unless ($v) {
108 $gcgi->err("Source specifier '".$source->{inputs}->[$i]->{label}."' not filled.");
109 next;
111 my $ii = $i + 1;
112 $u =~ s/%$ii/$v/g;
114 } else {
115 $u = $cgi->param($n.'_url');
116 $u or $gcgi->err("Source URL not specified");
118 $cgi->param('url', $u);
121 my $proj = Girocco::Project->ghost($name, $mirror) if $validname;
122 if ($validname && $proj->cgi_fill($gcgi)) {
123 if ($mirror) {
124 unless ($Girocco::Config::mirror) {
125 $gcgi->err("Mirroring mode is not enabled at this site.");
126 exit;
128 $proj->premirror;
129 $proj->clone;
130 print "<p>Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname\">pass onwards</a>.</p>\n";
131 print "<script language=\"javascript\">document.location='@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname'</script>\n";
133 } else {
134 unless ($Girocco::Config::push) {
135 $gcgi->err("Push mode is not enabled at this site.");
136 exit;
138 $proj->conjure;
139 print <<EOT;
140 <p>Project <a href="@{[url_path($Girocco::Config::gitweburl)]}/$name.git">$name</a> successfully set up.</p>
142 my @pushurls = ();
143 push(@pushurls, "<tt>$Girocco::Config::pushurl/$name.git</tt>") if $Girocco::Config::pushurl;
144 push(@pushurls, "<tt>$Girocco::Config::httpspushurl/$name.git</tt> " .
145 "<sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">(learn more)</a></span></sup>")
146 if $Girocco::Config::httpspushurl;
147 print "<p>The push URL(s) for the project: " . join(", ", @pushurls) . "</p>" if @pushurls;
148 my @pullurls = ();
149 push(@pullurls, $Girocco::Config::gitpullurl) if $Girocco::Config::gitpullurl;
150 push(@pullurls, $Girocco::Config::httppullurl) if $Girocco::Config::httppullurl;
151 print "<p>The read-only URL(s) for the project: <tt>" .
152 join("/$name.git</tt>, <tt>", @pullurls) .
153 "/$name.git</tt></p>" if @pullurls;
154 my $regnotice = '';
155 if ($Girocco::Config::manage_users) {
156 $regnotice = <<EOT;
157 Everyone who wants to push must <a href="@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi">register oneself as a user</a> first.
158 (One user can have push access to multiple projects and multiple users can have push access to one project.)
161 my $pushy = $Girocco::Config::pushurl || $Girocco::Config::httpspushurl;
162 my $pushyhint = '';
163 $pushyhint = " # <span style='font-family:sans-serif;font-size:smaller;position:relative;bottom:1pt'>" .
164 "<a href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">(learn more)</a></span>"
165 if $pushy =~ /^https:/i;
166 print <<EOT;
167 <p>You can <a href="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi?name=$escname">assign users</a> now
168 - don't forget to assign yourself as a user as well if you want to push!
169 $regnotice
170 </p>
171 <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.
172 To import a new project, the procedure is roughly as follows:
173 <pre>
174 \$ git init
175 \$ git add
176 \$ git commit
177 \$ git remote add origin $pushy/$name.git$pushyhint
178 \$ git push --all origin
179 </pre>
180 </p>
181 <p>Enjoy yourself, and have a lot of fun!</p>
184 exit;
188 my $mirror_mode = {
189 name => 'mirror',
190 desc => 'our dedicated git monkeys will check another repository at a given URL every hour and mirror any new updates',
191 pwpurp => 'mirroring URL'
193 my $push_mode = {
194 name => 'push',
195 desc => 'registered users with appropriate permissions will be able to push to the repository',
196 pwpurp => 'list of users allowed to push'
199 my $me = $Girocco::Config::mirror ? $mirror_mode : undef;
200 my $pe = $Girocco::Config::push ? $push_mode : undef;
201 if ($me and $pe) {
202 print <<EOT;
203 <p>At this site, you can host a project in one of two modes: $me->{name} mode and $pe->{name} mode.
204 In the <b>$me->{name} mode</b>, $me->{desc}.
205 In the <b>$pe->{name} mode</b>, $pe->{desc}.
206 You currently cannot switch freely between those two modes;
207 if you want to switch from mirroring to push mode or vice versa just delete and recreate
208 the project.</p>
210 } else {
211 my $mode = $me ? $me : $pe;
212 print "<p>This site will host your project in a <b>$mode->{name} mode</b>: $mode->{desc}.</p>\n";
215 my @pwpurp = ();
216 push @pwpurp, $me->{pwpurp} if $me;
217 push @pwpurp, $pe->{pwpurp} if $pe;
218 my $pwpurp = join(', ', @pwpurp);
220 if ($Girocco::Config::project_passwords) {
221 print <<EOT;
222 <p>You will need the admin password to adjust the project settings later
223 ($pwpurp, project description, ...).</p>
227 unless ($name =~ m#/#) {
228 print <<EOT;
229 <p>Note that if your project is a <strong>fork of an existing project</strong>
230 (this does not mean anything socially bad), please instead go to the project's
231 gitweb page and click the 'fork' link in the top bar. This way, all of us
232 will save bandwidth and more importantly, your project will be properly categorized.</p>
234 $me and print <<EOT;
235 <p>If your project is a fork but the existing project is not registered here yet, please
236 consider registering it first; you do not have to be involved in the project
237 in order to register it here as a mirror.</p>
239 } else {
240 my $xname = $name; $xname =~ s#/$#.git#; #
241 my ($pushnote1, $pushnote2);
242 if ($pe) {
243 $pushnote1 = " and you will need to push only the data <em>you</em> created, not the whole project";
244 $pushnote2 = <<EOT;
245 (That will be done automagically, you do not need to specify any extra arguments during the push.)
248 print <<EOT;
249 <p>Great, your project will be created as a subproject of the '$xname' project.
250 This means that it will be properly categorized$pushnote1.$pushnote2</p>
254 my $modechooser;
255 my $mirrorentry = '';
256 if ($me) {
257 $mirrorentry = '<tr id="mirror_url"><td class="formlabel" style="vertical-align:middle">Mirror source:</td><td>';
258 if (!$Girocco::Config::mirror_sources) {
259 $mirrorentry .= "<input type='text' name='url' value='%values{'url'}' />";
260 } else {
261 $mirrorentry .= "<table>"."\n";
262 foreach my $source (@$Girocco::Config::mirror_sources) {
263 my $n = $source->{label};
264 $mirrorentry .= '<tr><td class="formlabel">';
265 $mirrorentry .= '<p><label><input type="radio" class="mirror_sources" name="source" value="'.$n.'"'.
266 ($n eq $values{'source'} ? ' checked="checked"' : '').' />';
267 $mirrorentry .= $n;
268 $mirrorentry .= '</label></p></td><td>';
269 if ($source->{desc}) {
270 $mirrorentry .= '<p>';
271 $source->{link} and $mirrorentry .= '<a href="'.$source->{link}.'">';
272 $mirrorentry .= $source->{desc};
273 $source->{link} and $mirrorentry .= '</a>';
274 $mirrorentry .= '</p>';
276 if (!$source->{inputs}) {
277 $mirrorentry .= '<p>URL: <input type="text" name="'.$n.'_url" '.
278 'value="'.$values{$n.'_url'}.
279 '" onchange="set_mirror_source('."'".$n."'".')" /></p>';
280 } else {
281 $mirrorentry .= '<p>';
282 my $i = 0;
283 foreach my $input (@{$source->{inputs}}) {
284 $mirrorentry .= $input->{label};
285 my ($l, $v) = ($n.'_i'.$i, '');
286 if ($cgi->param($l)) {
287 $v = ' value="'.html_esc($cgi->param($l)).'"';
289 $mirrorentry .= ' <input type="text" name="'.$l.'"'.$v.
290 ' onchange="set_mirror_source('."'".$n."'".')" />';
291 $mirrorentry .= $input->{suffix} if $input->{suffix};
292 $mirrorentry .= '&#160; &#160;';
293 } continue { $i++; }
294 $mirrorentry .= '</p>';
296 $mirrorentry .= '</td></tr>'."\n";
298 $mirrorentry .= "</table>";
300 $mirrorentry .= '</td></tr>'."\n";;
301 $mirrorentry .= '<tr id="mirror_refs"><td class="formlabel">Mirror refs:</td><td class="formdatatd">'.
302 '<label title="Unchecking this will mirror the entire refs namespace which is usually unnecessary. '.
303 'Non-git sources always mirror the entire refs namespace regardless of this setting.">'.
304 '<input type="checkbox" name="cleanmirror" value="1" '.($values{'cleanmirror'} ? 'checked="checked" ' : '').
305 'style="vertical-align:middle" /><span style="vertical-align:middle; margin-left:0.5ex">'.
306 'Only mirror <code>refs/heads/*</code>, <code>refs/tags/*</code> and <code>refs/notes/*</code></span></label></td></tr>'."\n"
307 if grep(/cleanmirror/, @Girocco::Config::project_fields);
309 if ($me and $pe) {
310 $modechooser = <<EOT;
311 <tr><td class="formlabel" style="vertical-align:middle">Hosting mode:</td><td><p>
312 <label><input type="radio" name="mode" value="mirror" id="mirror_radio"@{[$values{'mirror'}?' checked="checked"':'']} />Mirror mode</label><br />
313 <label><input type="radio" name="mode" value="push" id="push_radio"@{[$values{'mirror'}?'':' checked="checked"']} />Push mode</label>
314 </p></td></tr>
316 } else {
317 $modechooser = '<input type="hidden" name="mode" value="'.($me ? $me->{name} : $pe->{name}).'" />';
320 my $forkentry = '';
321 if ($name =~ m#/#) {
322 $name =~ s#^(.*)/##;
323 $forkentry = '<input type="hidden" name="fork" value="'.$1.'" /><span class="formdata" style="padding-left:0.5ex">' .
324 html_esc($1) . '/</span>';
326 $name = html_esc($name);
327 my $tokauth = get_token_field("projedit", "", $Girocco::Config::project_edit_timeout);
328 $tokauth and $tokauth = "\n".$tokauth;
330 print <<EOT;
331 $Girocco::Config::legalese
332 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/regproj.cgi">$tokauth
333 <table class="form">
334 <tr><td class="formlabel">Project name:</td>
335 <td>$forkentry<input type="text" name="name" value="$name" /><span class="formdata" style="padding-left:0">.git</span></td></tr>
337 if ($Girocco::Config::project_passwords) {
338 print <<EOT;
339 <tr><td class="formlabel">Admin password (twice):</td><td><input type="password" name="pwd" /><br /><input type="password" name="pwd2" /></td></tr>
342 if ($Girocco::Config::project_owners eq 'email') {
343 print <<EOT;
344 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" value="@{[$values{'email'}]}" /></td></tr>
347 print $modechooser;
348 print $mirrorentry;
350 $gcgi->print_form_fields($Girocco::Project::metadata_fields, \%values, @Girocco::Config::project_fields);
352 print <<EOT;
355 print <<EOT;
356 <tr><td class="formlabel" style="line-height:inherit">Anti-captcha &#x2013; please<br />enter name of our nearest star:</td>
357 <td style="vertical-align:middle"><input type="text" name="mail" /></td></tr>
358 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Register" /></td></tr>
359 </table>
360 </form>