install.sh: expand @cfg_name@ in processed scripts
[girocco.git] / cgi / regproj.cgi
blob60965aed1d060e61fa567fde1f048398e11d8703
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 jsontype => 'application/json',
45 rmtype => 'Markdown',
46 source => 'Anywhere',
47 url => '',
48 Anywhere_url => '',
49 GitHub_i0 => '',
50 GitHub_i1 => '',
51 Gitorious_i0 => '',
52 Gitorious_i1 => '',
54 $values{'mirror'} = 0 unless $Girocco::Config::mirror;
55 $values{'mirror'} = 0 if $Girocco::Config::push && $name =~ m#/#;
56 if (@{[$name =~ m#/#g]} > 5) {
57 $gcgi->err("Unable to create a fork more than five levels deep, please fork the parent project instead.");
58 exit;
60 my $y0 = $cgi->param('y0') || '';
61 my $tok = $cgi->param('token') || '';
62 if ($cgi->param('mode') && $y0 eq 'Register' && $cgi->request_method eq 'POST') {
63 # Check for token validity
64 if (!check_timed_token($tok, "projedit", "", $Girocco::Config::project_edit_timeout)) {
65 $gcgi->err("Session has timed out or is invalid, please try again.");
67 # submitted, let's see
68 # FIXME: racy, do a lock
69 my $validname = 1;
70 if (Girocco::Project::valid_name($name)) {
71 Girocco::Project::does_exist($name,1)
72 and $gcgi->err("Project with the name '$name' already exists.");
73 } else {
74 $validname = 0;
75 if ($name =~ /^(.*)\.git$/i && Girocco::Project::valid_name($1)) {
76 $gcgi->err("Project name should not end with <tt>.git</tt> - I'll add that automagically.");
77 } else {
78 my $htmlname = html_esc($name);
79 $gcgi->err(
80 "Invalid project name \"$htmlname\" ".
81 "(contains bad characters or is a reserved project name). ".
82 "See <a href=\"@{[url_path($Girocco::Config::htmlurl)]}/names.html\">names</a>.");
86 my $check = $cgi->param('mail');
87 $check =~ tr/ \t/ /s; $check =~ s/^ //; $check =~ s/ $//;
88 if ($check !~ /^(?:(?:(?:the )?sun)|(?:sol))$/i) {
89 $gcgi->err("Sorry, invalid captcha check.");
92 foreach my $key (keys(%values)) {
93 $values{$key} = html_esc($cgi->param($key));
95 my $mirror = ($cgi->param('mode')||'') eq 'mirror';
96 $values{'mirror'} = $Girocco::Config::mirror && $mirror ? 1 : 0;
98 if ($mirror and $Girocco::Config::mirror_sources and not $cgi->param('url')) {
99 my $src = $cgi->param('source'); $src ||= '';
100 my $source; $src and $source = (grep { $_->{label} eq $src } @$Girocco::Config::mirror_sources)[0];
101 $source or $gcgi->err("Invalid or no mirror source $src specified");
103 my $n = $source->{label};
104 my $u = $source->{url};
105 if ($source->{inputs}) {
106 for my $i (0..$#{$source->{inputs}}) {
107 my $v = $cgi->param($n.'_i'.$i);
108 unless ($v) {
109 $gcgi->err("Source specifier '".$source->{inputs}->[$i]->{label}."' not filled.");
110 next;
112 my $ii = $i + 1;
113 $u =~ s/%$ii/$v/g;
115 } else {
116 $u = $cgi->param($n.'_url');
117 $u or $gcgi->err("Source URL not specified");
119 $cgi->param('url', $u);
122 my $proj = Girocco::Project->ghost($name, $mirror) if $validname;
123 if ($validname && $proj->cgi_fill($gcgi)) {
124 if ($mirror) {
125 unless ($Girocco::Config::mirror) {
126 $gcgi->err("Mirroring mode is not enabled at this site.");
127 exit;
129 $proj->premirror;
130 $proj->clone;
131 print "<p>Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname\">pass onwards</a>.</p>\n";
132 print "<script language=\"javascript\">document.location='@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname'</script>\n";
134 } else {
135 unless ($Girocco::Config::push) {
136 $gcgi->err("Push mode is not enabled at this site.");
137 exit;
139 $proj->conjure;
140 print <<EOT;
141 <p>Project <a href="@{[url_path($Girocco::Config::gitweburl)]}/$name.git">$name</a> successfully set up.</p>
143 my @pushurls = ();
144 push(@pushurls, "<tt>$Girocco::Config::pushurl/$name.git</tt>") if $Girocco::Config::pushurl;
145 push(@pushurls, "<tt>$Girocco::Config::httpspushurl/$name.git</tt> " .
146 "<sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">(learn more)</a></span></sup>")
147 if $Girocco::Config::httpspushurl;
148 print "<p>The push URL(s) for the project: " . join(", ", @pushurls) . "</p>" if @pushurls;
149 my @pullurls = ();
150 push(@pullurls, $Girocco::Config::gitpullurl) if $Girocco::Config::gitpullurl;
151 push(@pullurls, $Girocco::Config::httppullurl) if $Girocco::Config::httppullurl;
152 print "<p>The read-only URL(s) for the project: <tt>" .
153 join("/$name.git</tt>, <tt>", @pullurls) .
154 "/$name.git</tt></p>" if @pullurls;
155 my $regnotice = '';
156 if ($Girocco::Config::manage_users) {
157 $regnotice = <<EOT;
158 Everyone who wants to push must <a href="@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi">register oneself as a user</a> first.
159 (One user can have push access to multiple projects and multiple users can have push access to one project.)
162 my $pushy = $Girocco::Config::pushurl || $Girocco::Config::httpspushurl;
163 my $pushyhint = '';
164 $pushyhint = " # <span style='font-family:sans-serif;font-size:smaller;position:relative;bottom:1pt'>" .
165 "<a href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">(learn more)</a></span>"
166 if $pushy =~ /^https:/i;
167 print <<EOT;
168 <p>You can <a href="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi?name=$escname">assign users</a> now
169 - don't forget to assign yourself as a user as well if you want to push!
170 $regnotice
171 </p>
172 <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.
173 To import a new project, the procedure is roughly as follows:
174 <pre>
175 \$ git init
176 \$ git add
177 \$ git commit
178 \$ git remote add origin $pushy/$name.git$pushyhint
179 \$ git push --all origin
180 </pre>
181 </p>
182 <p>Enjoy yourself, and have a lot of fun!</p>
185 exit;
189 my $mirror_mode = {
190 name => 'mirror',
191 desc => 'our dedicated git monkeys will check another repository at a given URL every hour and mirror any new updates',
192 pwpurp => 'mirroring URL'
194 my $push_mode = {
195 name => 'push',
196 desc => 'registered users with appropriate permissions will be able to push to the repository',
197 pwpurp => 'list of users allowed to push'
200 my $me = $Girocco::Config::mirror ? $mirror_mode : undef;
201 my $pe = $Girocco::Config::push ? $push_mode : undef;
202 if ($me and $pe) {
203 print <<EOT;
204 <p>At this site, you can host a project in one of two modes: $me->{name} mode and $pe->{name} mode.
205 In the <b>$me->{name} mode</b>, $me->{desc}.
206 In the <b>$pe->{name} mode</b>, $pe->{desc}.
207 You currently cannot switch freely between those two modes;
208 if you want to switch from mirroring to push mode or vice versa just delete and recreate
209 the project.</p>
211 } else {
212 my $mode = $me ? $me : $pe;
213 print "<p>This site will host your project in a <b>$mode->{name} mode</b>: $mode->{desc}.</p>\n";
216 my @pwpurp = ();
217 push @pwpurp, $me->{pwpurp} if $me;
218 push @pwpurp, $pe->{pwpurp} if $pe;
219 my $pwpurp = join(', ', @pwpurp);
221 if ($Girocco::Config::project_passwords) {
222 print <<EOT;
223 <p>You will need the admin password to adjust the project settings later
224 ($pwpurp, project description, ...).</p>
228 unless ($name =~ m#/#) {
229 print <<EOT;
230 <p>Note that if your project is a <strong>fork of an existing project</strong>
231 (this does not mean anything socially bad), please instead go to the project's
232 gitweb page and click the 'fork' link in the top bar. This way, all of us
233 will save bandwidth and more importantly, your project will be properly categorized.</p>
235 $me and print <<EOT;
236 <p>If your project is a fork but the existing project is not registered here yet, please
237 consider registering it first; you do not have to be involved in the project
238 in order to register it here as a mirror.</p>
240 } else {
241 my $xname = $name; $xname =~ s#/$#.git#; #
242 my ($pushnote1, $pushnote2);
243 if ($pe) {
244 $pushnote1 = " and you will need to push only the data <em>you</em> created, not the whole project";
245 $pushnote2 = <<EOT;
246 (That will be done automagically, you do not need to specify any extra arguments during the push.)
249 print <<EOT;
250 <p>Great, your project will be created as a subproject of the '$xname' project.
251 This means that it will be properly categorized$pushnote1.$pushnote2</p>
255 my $modechooser;
256 my $mirrorentry = '';
257 if ($me) {
258 $mirrorentry = '<tr id="mirror_url"><td class="formlabel" style="vertical-align:middle">Mirror source:</td><td>';
259 if (!$Girocco::Config::mirror_sources) {
260 $mirrorentry .= "<input type='text' name='url' value='%values{'url'}' />";
261 } else {
262 $mirrorentry .= "<table>"."\n";
263 foreach my $source (@$Girocco::Config::mirror_sources) {
264 my $n = $source->{label};
265 $mirrorentry .= '<tr><td class="formlabel">';
266 $mirrorentry .= '<p><label><input type="radio" class="mirror_sources" name="source" value="'.$n.'"'.
267 ($n eq $values{'source'} ? ' checked="checked"' : '').' />';
268 $mirrorentry .= $n;
269 $mirrorentry .= '</label></p></td><td>';
270 if ($source->{desc}) {
271 $mirrorentry .= '<p>';
272 $source->{link} and $mirrorentry .= '<a href="'.$source->{link}.'">';
273 $mirrorentry .= $source->{desc};
274 $source->{link} and $mirrorentry .= '</a>';
275 $mirrorentry .= '</p>';
277 if (!$source->{inputs}) {
278 $mirrorentry .= '<p>URL: <input type="text" name="'.$n.'_url" '.
279 'value="'.$values{$n.'_url'}.
280 '" onchange="set_mirror_source('."'".$n."'".')" /></p>';
281 } else {
282 $mirrorentry .= '<p>';
283 my $i = 0;
284 foreach my $input (@{$source->{inputs}}) {
285 $mirrorentry .= $input->{label};
286 my ($l, $v) = ($n.'_i'.$i, '');
287 if ($cgi->param($l)) {
288 $v = ' value="'.html_esc($cgi->param($l)).'"';
290 $mirrorentry .= ' <input type="text" name="'.$l.'"'.$v.
291 ' onchange="set_mirror_source('."'".$n."'".')" />';
292 $mirrorentry .= $input->{suffix} if $input->{suffix};
293 $mirrorentry .= '&#160; &#160;';
294 } continue { $i++; }
295 $mirrorentry .= '</p>';
297 $mirrorentry .= '</td></tr>'."\n";
299 $mirrorentry .= "</table>";
301 $mirrorentry .= '</td></tr>'."\n";;
302 $mirrorentry .= '<tr id="mirror_refs"><td class="formlabel">Mirror refs:</td><td class="formdatatd">'.
303 '<label title="Unchecking this will mirror the entire refs namespace which is usually unnecessary. '.
304 'Non-git sources always mirror the entire refs namespace regardless of this setting.">'.
305 '<input type="checkbox" name="cleanmirror" value="1" '.($values{'cleanmirror'} ? 'checked="checked" ' : '').
306 'style="vertical-align:middle" /><span style="vertical-align:middle; margin-left:0.5ex">'.
307 'Only mirror <code>refs/heads/*</code>, <code>refs/tags/*</code> and <code>refs/notes/*</code></span></label></td></tr>'."\n"
308 if grep(/cleanmirror/, @Girocco::Config::project_fields);
310 if ($me and $pe) {
311 $modechooser = <<EOT;
312 <tr><td class="formlabel" style="vertical-align:middle">Hosting mode:</td><td><p>
313 <label><input type="radio" name="mode" value="mirror" id="mirror_radio"@{[$values{'mirror'}?' checked="checked"':'']} />Mirror mode</label><br />
314 <label><input type="radio" name="mode" value="push" id="push_radio"@{[$values{'mirror'}?'':' checked="checked"']} />Push mode</label>
315 </p></td></tr>
317 } else {
318 $modechooser = '<input type="hidden" name="mode" value="'.($me ? $me->{name} : $pe->{name}).'" />';
321 my $forkentry = '';
322 if ($name =~ m#/#) {
323 $name =~ s#^(.*)/##;
324 $forkentry = '<input type="hidden" name="fork" value="'.$1.'" /><span class="formdata" style="padding-left:0.5ex">' .
325 html_esc($1) . '/</span>';
327 $name = html_esc($name);
328 my $tokauth = get_token_field("projedit", "", $Girocco::Config::project_edit_timeout);
329 $tokauth and $tokauth = "\n".$tokauth;
331 print <<EOT;
332 $Girocco::Config::legalese
333 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/regproj.cgi">$tokauth
334 <table class="form">
335 <tr><td class="formlabel">Project name:</td>
336 <td>$forkentry<input type="text" name="name" value="$name" /><span class="formdata" style="padding-left:0">.git</span></td></tr>
338 if ($Girocco::Config::project_passwords) {
339 print <<EOT;
340 <tr><td class="formlabel">Admin password (twice):</td><td><input type="password" name="pwd" /><br /><input type="password" name="pwd2" /></td></tr>
343 if ($Girocco::Config::project_owners eq 'email') {
344 print <<EOT;
345 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" value="@{[$values{'email'}]}" /></td></tr>
348 print $modechooser;
349 print $mirrorentry;
351 $gcgi->print_form_fields($Girocco::Project::metadata_fields, \%values, @Girocco::Config::project_fields);
353 print <<EOT;
356 print <<EOT;
357 <tr><td class="formlabel" style="line-height:inherit">Anti-captcha &#x2013; please<br />enter name of our nearest star:</td>
358 <td style="vertical-align:middle"><input type="text" name="mail" /></td></tr>
359 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Register" /></td></tr>
360 </table>
361 </form>