names: tighten up the requirements for project and user names
[girocco.git] / cgi / regproj.cgi
blob63ca47fa520ce0e97731a7a5c94167479fc36dee
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 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 notifymail => '',
37 notifyjson => '',
38 notifycia => '',
39 README => '',
40 source => 'Anywhere',
41 url => '',
42 Anywhere_url => '',
43 GitHub_i0 => '',
44 GitHub_i1 => '',
45 Gitorious_i0 => '',
46 Gitorious_i1 => '',
48 $values{'mirror'} = 0 unless $Girocco::Config::mirror;
49 $values{'mirror'} = 0 if $Girocco::Config::push && $name =~ m#/#;
51 my $y0 = $cgi->param('y0') || '';
52 if ($cgi->param('mode') && $y0 eq 'Register' && $cgi->request_method eq 'POST') {
53 # submitted, let's see
54 # FIXME: racy, do a lock
55 my $validname = 1;
56 if (Girocco::Project::valid_name($name)) {
57 Girocco::Project::does_exist($name,1)
58 and $gcgi->err("Project with the name '$name' already exists.");
59 } else {
60 $validname = 0;
61 if ($name =~ /^(.*)\.git$/i && Girocco::Project::valid_name($1)) {
62 $gcgi->err("Project name should not end with <tt>.git</tt> - I'll add that automagically.");
63 } else {
64 my $htmlname = html_esc($name);
65 $gcgi->err("Invalid project name \"$htmlname\" (contains bad characters or is a reserved project name).");
69 my $check = $cgi->param('mail');
70 $check =~ tr/ \t/ /s; $check =~ s/^ //; $check =~ s/ $//;
71 if ($check !~ /^(?:(?:(?:the )?sun)|(?:sol))$/i) {
72 $gcgi->err("Sorry, invalid captcha check.");
75 foreach my $key (keys(%values)) {
76 $values{$key} = html_esc($cgi->param($key));
78 my $mirror = ($cgi->param('mode')||'') eq 'mirror';
79 $values{'mirror'} = $Girocco::Config::mirror && $mirror ? 1 : 0;
81 if ($mirror and $Girocco::Config::mirror_sources and not $cgi->param('url')) {
82 my $src = $cgi->param('source'); $src ||= '';
83 my $source; $src and $source = (grep { $_->{label} eq $src } @$Girocco::Config::mirror_sources)[0];
84 $source or $gcgi->err("Invalid or no mirror source $src specified");
86 my $n = $source->{label};
87 my $u = $source->{url};
88 if ($source->{inputs}) {
89 for my $i (0..$#{$source->{inputs}}) {
90 my $v = $cgi->param($n.'_i'.$i);
91 unless ($v) {
92 $gcgi->err("Source specifier '".$source->{inputs}->[$i]->{label}."' not filled.");
93 next;
95 my $ii = $i + 1;
96 $u =~ s/%$ii/$v/g;
98 } else {
99 $u = $cgi->param($n.'_url');
100 $u or $gcgi->err("Source URL not specified");
102 $cgi->param('url', $u);
105 my $proj = Girocco::Project->ghost($name, $mirror) if $validname;
106 if ($validname && $proj->cgi_fill($gcgi)) {
107 if ($mirror) {
108 unless ($Girocco::Config::mirror) {
109 $gcgi->err("Mirroring mode is not enabled at this site.");
110 exit;
112 $proj->premirror;
113 $proj->clone;
114 print "<p>Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname\">pass onwards</a>.</p>\n";
115 print "<script language=\"javascript\">document.location='@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname'</script>\n";
117 } else {
118 unless ($Girocco::Config::push) {
119 $gcgi->err("Push mode is not enabled at this site.");
120 exit;
122 $proj->conjure;
123 print <<EOT;
124 <p>Project <a href="@{[url_path($Girocco::Config::gitweburl)]}/$name.git">$name</a> successfully set up.</p>
126 my @pushurls = ();
127 push(@pushurls, "<tt>$Girocco::Config::pushurl/$name.git</tt>") if $Girocco::Config::pushurl;
128 push(@pushurls, "<tt>$Girocco::Config::httpspushurl/$name.git</tt> " .
129 "<sup><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">(learn more)</a></sup>")
130 if $Girocco::Config::httpspushurl;
131 print "<p>The push URL(s) for the project: " . join(", ", @pushurls) . "</p>" if @pushurls;
132 my @pullurls = ();
133 push(@pullurls, $Girocco::Config::gitpullurl) if $Girocco::Config::gitpullurl;
134 push(@pullurls, $Girocco::Config::httppullurl) if $Girocco::Config::httppullurl;
135 print "<p>The read-only URL(s) for the project: <tt>" .
136 join("/$name.git</tt>, <tt>", @pullurls) .
137 "/$name.git</tt></p>" if @pullurls;
138 my $regnotice = '';
139 if ($Girocco::Config::manage_users) {
140 $regnotice = <<EOT;
141 Everyone who wants to push must <a href="@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi">register oneself as a user</a> first.
142 (One user can have push access to multiple projects and multiple users can have push access to one project.)
145 my $pushy = $Girocco::Config::pushurl || $Girocco::Config::httpspushurl;
146 my $pushyhint = '';
147 $pushyhint = " # <span style='font-family:sans-serif'><sup style='position:fixed'>" .
148 "<a href=\"@{[url_path($Girocco::Config::htmlurl)]}/httpspush.html\">(learn more)</a></sup>" .
149 "</span>" if $pushy =~ /^https:/i;
150 print <<EOT;
151 <p>You can <a href="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi?name=$escname">assign users</a> now
152 - don't forget to assign yourself as a user as well if you want to push!
153 $regnotice
154 </p>
155 <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.
156 To import a new project, the procedure is roughly as follows:
157 <pre>
158 \$ git init
159 \$ git add
160 \$ git commit
161 \$ git remote add origin $pushy/$name.git$pushyhint
162 \$ git push --all origin
163 </pre>
164 </p>
165 <p>Enjoy yourself, and have a lot of fun!</p>
168 exit;
172 my $mirror_mode = {
173 name => 'mirror',
174 desc => 'our dedicated git monkeys will check another repository at a given URL every hour and mirror any new updates',
175 pwpurp => 'mirroring URL'
177 my $push_mode = {
178 name => 'push',
179 desc => 'registered users with appropriate permissions will be able to push to the repository',
180 pwpurp => 'list of users allowed to push'
183 my $me = $Girocco::Config::mirror ? $mirror_mode : undef;
184 my $pe = $Girocco::Config::push ? $push_mode : undef;
185 if ($me and $pe) {
186 print <<EOT;
187 <p>At this site, you can host a project in one of two modes: $me->{name} mode and $pe->{name} mode.
188 In the <b>$me->{name} mode</b>, $me->{desc}.
189 In the <b>$pe->{name} mode</b>, $pe->{desc}.
190 You currently cannot switch freely between those two modes;
191 if you want to switch from mirroring to push mode or vice versa just delete and recreate
192 the project.</p>
194 } else {
195 my $mode = $me ? $me : $pe;
196 print "<p>This site will host your project in a <b>$mode->{name} mode</b>: $mode->{desc}.</p>\n";
199 my @pwpurp = ();
200 push @pwpurp, $me->{pwpurp} if $me;
201 push @pwpurp, $pe->{pwpurp} if $pe;
202 my $pwpurp = join(', ', @pwpurp);
204 if ($Girocco::Config::project_passwords) {
205 print <<EOT;
206 <p>You will need the admin password to adjust the project settings later
207 ($pwpurp, project description, ...).</p>
211 unless ($name =~ m#/#) {
212 print <<EOT;
213 <p>Note that if your project is a <strong>fork of an existing project</strong>
214 (this does not mean anything socially bad), please instead go to the project's
215 gitweb page and click the 'fork' link in the top bar. This way, all of us
216 will save bandwidth and more importantly, your project will be properly categorized.</p>
218 $me and print <<EOT;
219 <p>If your project is a fork but the existing project is not registered here yet, please
220 consider registering it first; you do not have to be involved in the project
221 in order to register it here as a mirror.</p>
223 } else {
224 my $xname = $name; $xname =~ s#/$#.git#; #
225 my ($pushnote1, $pushnote2);
226 if ($pe) {
227 $pushnote1 = " and you will need to push only the data <em>you</em> created, not the whole project";
228 $pushnote2 = <<EOT;
229 (That will be done automagically, you do not need to specify any extra arguments during the push.)
232 print <<EOT;
233 <p>Great, your project will be created as a subproject of the '$xname' project.
234 This means that it will be properly categorized$pushnote1.$pushnote2</p>
238 my $modechooser;
239 my $mirrorentry = '';
240 if ($me) {
241 $mirrorentry = '<tr id="mirror_url"><td class="formlabel" style="vertical-align:middle">Mirror source:</td><td>';
242 if (!$Girocco::Config::mirror_sources) {
243 $mirrorentry .= "<input type='text' name='url' value=%values{'url'}/>";
244 } else {
245 $mirrorentry .= "<table>"."\n";
246 foreach my $source (@$Girocco::Config::mirror_sources) {
247 my $n = $source->{label};
248 $mirrorentry .= '<tr><td class="formlabel">';
249 $mirrorentry .= '<p><input type="radio" class="mirror_sources" name="source" value="'.$n.'"'.
250 ($n eq $values{'source'} ? ' checked="checked"' : '').' />';
251 $mirrorentry .= $n;
252 $mirrorentry .= '</p></td><td>';
253 if ($source->{desc}) {
254 $mirrorentry .= '<p>';
255 $source->{link} and $mirrorentry .= '<a href="'.$source->{link}.'">';
256 $mirrorentry .= $source->{desc};
257 $source->{link} and $mirrorentry .= '</a>';
258 $mirrorentry .= '</p>';
260 if (!$source->{inputs}) {
261 $mirrorentry .= '<p>URL: <input type="text" name="'.$n.'_url" '.
262 'value="'.$values{$n.'_url'}.
263 '" onchange="set_mirror_source('."'".$n."'".')" /></p>';
264 } else {
265 $mirrorentry .= '<p>';
266 my $i = 0;
267 foreach my $input (@{$source->{inputs}}) {
268 $mirrorentry .= $input->{label};
269 my ($l, $v) = ($n.'_i'.$i, '');
270 if ($cgi->param($l)) {
271 $v = ' value="'.html_esc($cgi->param($l)).'"';
273 $mirrorentry .= ' <input type="text" name="'.$l.'"'.$v.
274 ' onchange="set_mirror_source('."'".$n."'".')" />';
275 $mirrorentry .= $input->{suffix} if $input->{suffix};
276 $mirrorentry .= '&nbsp; &nbsp;';
277 } continue { $i++; }
278 $mirrorentry .= '</p>';
280 $mirrorentry .= '</td></tr>'."\n";
282 $mirrorentry .= "</table>";
284 $mirrorentry .= '</td></tr>';
286 if ($me and $pe) {
287 $modechooser = <<EOT;
288 <tr><td class="formlabel" style="vertical-align:middle">Hosting mode:</td><td><p>
289 <input type="radio" name="mode" value="mirror" id="mirror_radio"@{[$values{'mirror'}?' checked="checked"':'']} />Mirror mode<br />
290 <input type="radio" name="mode" value="push" id="push_radio"@{[$values{'mirror'}?'':' checked="checked"']} />Push mode
291 </p></td></tr>
293 } else {
294 $modechooser = '<input type="hidden" name="mode" value="'.($me ? $me->{name} : $pe->{name}).'" />';
297 my $forkentry = '';
298 if ($name =~ m#/#) {
299 $name =~ s#^(.*)/##;
300 $forkentry = '<input type="hidden" name="fork" value="'.$1.'" /><span class="formdata" style="padding-left:0.5ex">' .
301 html_esc($1) . '/</span>';
303 $name = html_esc($name);
305 print <<EOT;
306 $Girocco::Config::legalese
307 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/regproj.cgi">
308 <table class="form">
309 <tr><td class="formlabel">Project name:</td>
310 <td>$forkentry<input type="text" name="name" value="$name" /><span class="formdata" style="padding-left:0">.git</span></td></tr>
312 if ($Girocco::Config::project_passwords) {
313 print <<EOT;
314 <tr><td class="formlabel">Admin password (twice):</td><td><input type="password" name="pwd" /><br /><input type="password" name="pwd2" /></td></tr>
317 if ($Girocco::Config::project_owners eq 'email') {
318 print <<EOT;
319 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" value="@{[$values{'email'}]}" /></td></tr>
322 print $modechooser;
323 print $mirrorentry;
325 $gcgi->print_form_fields($Girocco::Project::metadata_fields, \%values, @Girocco::Config::project_fields);
327 print <<EOT;
330 print <<EOT;
331 <tr><td class="formlabel" style="line-height:inherit">Anti-captcha - please<br />enter name of our nearest star:</td>
332 <td style="vertical-align:middle"><input type="text" name="mail" /></td></tr>
333 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Register" /></td></tr>
334 </table>
335 </form>