Merge branch 'master' into rorcz
[girocco.git] / cgi / editproj.cgi
blob8a2dd03aa9d427c5c4a623153ec6f13742ef2363
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;
13 use POSIX qw(strftime);
15 my $script = <<'EOT';
16 <script type="text/javascript">
17 // <![CDATA[
18 function adduseritem(elem)
20 var inp = document.createElement('input');
21 inp.type = 'text';
22 inp.name = 'user';
23 var li = document.createElement('li');
24 li.appendChild(document.createTextNode('Add user: '));
25 li.appendChild(inp);
26 elem.parentNode.insertBefore(li, elem);
28 // ]]>
29 </script>
30 EOT
31 my $gcgi = Girocco::CGI->new('Project Settings', undef, $script);
32 my $cgi = $gcgi->cgi;
34 my $name = $cgi->param('name');
35 $name =~ s#\.git$## if $name; #
37 unless (defined $name) {
38 print "<p>I need the project name as an argument now.</p>\n";
39 exit;
42 if (!Girocco::Project::does_exist($name,1) && !Girocco::Project::valid_name($name)) {
43 print "<p>Invalid project name. Go away, sorcerer.</p>\n";
44 exit;
47 if (!Girocco::Project::does_exist($name,1)) {
48 print "<p>Sorry but the project $name does not exist. Now, how did you <em>get</em> here?!</p>\n";
49 exit;
52 my $proj = Girocco::Project->load($name);
53 if (!$proj) {
54 print "<p>not found project $name, that's really weird!</p>\n";
55 exit;
57 push(@Girocco::Config::project_fields, "notifycia")
58 if $proj->{notifycia} && !grep(/notifycia/, @Girocco::Config::project_fields);
59 my $escname = $name;
60 $escname =~ s/[+]/%2B/g;
62 my $tzoffset = $cgi->param('tzoffset') || 0;
63 $tzoffset =~ /^[-+]?\d{1,5}$/ or $tzoffset = 0;
64 $tzoffset = 0 + $tzoffset;
65 $tzoffset >= -43200 && $tzoffset <= 43200 or $tzoffset = 0;
67 sub format_epoch_ts {
68 my $es = shift;
69 defined($es) or $es = time();
70 $es += $tzoffset;
71 my $str = strftime("%Y-%m-%d %H:%M:%S ", (gmtime($es))[0..5], -1, -1, -1);
72 if ($tzoffset) {
73 my $moff = int(abs($tzoffset) / 60);
74 $str .= sprintf("%s%02d%02d",
75 ($tzoffset >= 0 ? "+" : "-"),
76 int($moff / 60),
77 $moff - 60 * int($moff / 60));
78 } else {
79 $str .= "UTC";
81 return $str;
84 my $y0 = $cgi->param('y0') || '';
85 my $tok = $cgi->param('token') || '';
86 my $protok = 0;
87 if (($y0 eq 'Update' || $y0 eq 'View All' || $y0 eq 'Restart Mirroring') && $cgi->request_method eq 'POST') {{
88 my $ts = "<span class=\"timestamp\">" . format_epoch_ts() . "</span>";
89 $gcgi->err_prelude("<p class=\"failed\">Project update failed at $ts.</p>\n") unless $y0 eq 'View All';
90 # Check for token validity
91 if (!check_timed_token($tok, "projedit", $proj->{name}, $Girocco::Config::project_edit_timeout)) {
92 $gcgi->err("Session has timed out or is invalid, please try again.");
94 # submitted, let's see
95 my $fillok = $proj->cgi_fill($gcgi, 1);
96 my $authok = $proj->authenticate($gcgi);
97 $authok and $protok = 1;
98 $y0 eq 'View All' and last;
99 if (!$gcgi->err_check && $authok) {
100 $proj->update or last;
101 print "<p class=\"updated\">Project successfully updated at $ts.</p>\n";
102 if ($proj->{clone_failed}) {
103 $proj->clone;
104 print "<p>Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}".
105 "/mirrorproj.cgi?name=$escname\">pass onwards</a>.</p>\n";
106 print "<script language=\"javascript\">document.location=".
107 "'@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname'</script>\n";
108 exit;
113 # $proj may be insane now but that's actually good for us since we'll let the
114 # user fix the invalid values he or she entered
115 my %h = $proj->form_defaults;
117 print <<EOT;
118 <p>Here you may adjust the settings of project $h{name}. Go wild.
120 if ($proj->{mirror}) {
121 print <<EOT;
122 Since this is a mirrored project, you may opt to remove it from the site as well.
123 Just <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">remove it</a>.</p>
125 } else {
126 if ($proj->is_empty) {
127 print <<EOT;
128 Since this is an empty project, you may opt to remove it from the site as well.
129 Just <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">remove it</a>.</p>
131 } else {
132 print <<EOT;
133 You may
134 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">request an authorization code</a> in order
135 to remove this project from the site.</p>
138 print <<EOT;
139 <p>Use the <b>+</b> button to enable access for more than a single user at a time.</p>
143 my $button_label = $proj->{clone_failed} ? 'Restart Mirroring' : 'Update';
144 my $showstatusopt = $proj->{mirror} && !$proj->{clone_failed} && !$proj->{clone_in_progress};
145 my $statuschecked = $proj->{statusupdates} ? 'checked="checked"' : '';
146 my $tokauth = get_token_field("projedit", $h{name}, $Girocco::Config::project_edit_timeout);
147 $tokauth and $tokauth = "\n".$tokauth;
149 print <<EOT;
150 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi">$tokauth
151 <input type="hidden" name="tzoffset" value="0" />
152 <table class="form">
153 <tr><td class="formlabel">Project name:</td><td class="formdata"><a
154 href="@{[url_path($Girocco::Config::gitweburl)]}/$h{name}.git">$h{name}</a>.git
155 <input type="hidden" name="name" value="$h{name}" /></td></tr>
157 if ($Girocco::Config::project_passwords) {
158 my $vab = '';
159 $protok || !%{$Girocco::Config::protect_fields} or
160 $vab = '&#160;&#160;<input type="submit" name="y0" value="View All" />';
161 print <<EOT;
162 <tr><td class="formlabel"><strong>Admin password:</strong></td><td>
163 <input type="password" name="cpwd" /> <sup class="sup"><span><a
164 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname"
165 class="ctxaction">(forgot password?)</a></span></sup>$vab</td></tr>
166 <tr><td class="formlabel">New admin password (twice):<br />
167 <em>(leave empty to keep it the same)</em></td><td>
168 <input type="password" name="pwd" /><br /><input type="password" name="pwd2" /><br />
169 </td></tr>
172 if ($Girocco::Config::project_owners eq 'email') {
173 print <<EOT;
174 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" value="$h{email}" /></td></tr>
178 if ($proj->{mirror}) {
179 print "<tr><td class=\"formlabel\">Repository URL:</td><td><input type=\"text\" name=\"url\" value=\"$h{url}\" /></td></tr>\n";
180 print '<tr><td class="formlabel">Mirror refs:</td><td class="formdatatd">'.
181 '<label title="Unchecking this will mirror the entire refs namespace which is usually unnecessary. '.
182 'Non-git sources always mirror the entire refs namespace regardless of this setting.">'.
183 '<input type="checkbox" name="cleanmirror" value="1" '.($h{'cleanmirror'} ? 'checked="checked" ' : '').
184 'style="vertical-align:middle" /><span style="vertical-align:middle; margin-left:0.5ex">'.
185 'Only mirror <code>refs/heads/*</code>, <code>refs/tags/*</code> and <code>refs/notes/*</code></span></label></td></tr>'."\n"
186 if grep(/cleanmirror/, @Girocco::Config::project_fields);
187 } else {
188 print <<EOT;
189 <tr><td class="formlabel" style="vertical-align:middle">Users:</td><td>
190 <ul>
192 $Girocco::Config::manage_users and print "<p>Only <a href=\"".
193 "@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi\">registered users</a> may push.</p>";
194 if ($Girocco::Config::mob and not grep { $_ eq $Girocco::Config::mob } @{$h{users}}) {
195 print "<p><em>(Please consider adding the <tt>$Girocco::Config::mob</tt> user.\n";
196 print "<sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/mob.html\">(learn more)</a></span></sup>)\n";
197 print "</em></p>\n";
199 foreach my $user (@{$h{users}}) {
200 my $mlm = '';
201 $mlm = " <sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/mob.html\">(learn more)</a></span></sup>"
202 if $Girocco::Config::mob && $user eq $Girocco::Config::mob;
203 print "<li><input type=\"checkbox\" name=\"user\" value=\"$user\" checked=\"1\" /> $user$mlm</li>\n";
205 print <<EOT;
206 <li>Add user: <input type="text" name="user" /></li>
207 <button type="button" onclick="adduseritem(this)"><b>+</b></button>
208 </ul>
209 </td></tr>
213 print '<tr><td class="formlabel">Default branch:</td><td class="formdatatd"><select size="1" name="HEAD">';
214 for ($proj->get_heads) {
215 my $selected = $proj->{HEAD} eq $_ ? ' selected="selected"' : '';
216 print "<option$selected>".Girocco::CGI::html_esc($_)."</option>";
218 print '</select></td></tr>
221 print '<tr><td class="formlabel">Tags (select to delete):</td><td>';
222 print '<select size="6" name="tags" multiple="multiple">';
223 for ($proj->get_ctag_names) {
224 print '<option>'.Girocco::CGI::html_esc($_).'</option>';
226 print '</select></td></tr>
230 $gcgi->print_form_fields($Girocco::Project::metadata_fields,
231 $protok?{}:$Girocco::Config::protect_fields,
232 \%h, @Girocco::Config::project_fields);
233 print <<EOT if $showstatusopt;
234 <tr><td class="formlabel">Enable status update emails:</td>
235 <td class="formdatatd"
236 ><input type="hidden" name="setstatusupdates" value="1"
237 /><input type="checkbox" name="statusupdates" value="1" $statuschecked /></td></tr>
239 print <<EOT;
240 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="$button_label" /></td></tr>
241 </table>
242 </form>
243 <script type="text/javascript">
244 // <![CDATA[
245 (function () {
246 var tzoffset = (new Date).getTimezoneOffset() * -60;
247 var form0 = document.forms[0];
248 if (form0 && form0.tzoffset) {
249 form0.tzoffset.value = tzoffset;
251 })();
252 // ]]>
253 </script>