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