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