toolbox/update-all-config.pl: quiet means quiet
[girocco.git] / cgi / editproj.cgi
blob22b24994d4c2bbb34968e113f3d9607ae38363d7
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 my $escname = $name;
58 $escname =~ s/[+]/%2B/g;
60 my $tzoffset = $cgi->param('tzoffset') || 0;
61 $tzoffset =~ /^[-+]?\d{1,5}$/ or $tzoffset = 0;
62 $tzoffset = 0 + $tzoffset;
63 $tzoffset >= -43200 && $tzoffset <= 43200 or $tzoffset = 0;
65 sub format_epoch_ts {
66 my $es = shift;
67 defined($es) or $es = time();
68 $es += $tzoffset;
69 my $str = strftime("%Y-%m-%d %H:%M:%S ", (gmtime($es))[0..5], -1, -1, -1);
70 if ($tzoffset) {
71 my $moff = int(abs($tzoffset) / 60);
72 $str .= sprintf("%s%02d%02d",
73 ($tzoffset >= 0 ? "+" : "-"),
74 int($moff / 60),
75 $moff - 60 * int($moff / 60));
76 } else {
77 $str .= "UTC";
79 return $str;
82 my $y0 = $cgi->param('y0') || '';
83 my $tok = $cgi->param('token') || '';
84 my $protok = 0;
85 if (($y0 eq 'Update' || $y0 eq 'View All' || $y0 eq 'Restart Mirroring') && $cgi->request_method eq 'POST') {{
86 my $ts = "<span class=\"timestamp\">" . format_epoch_ts() . "</span>";
87 $gcgi->err_prelude("<p class=\"failed\">Project update failed at $ts.</p>\n") unless $y0 eq 'View All';
88 # Check for token validity
89 if (!check_timed_token($tok, "projedit", $proj->{name}, $Girocco::Config::project_edit_timeout)) {
90 $gcgi->err("Session has timed out or is invalid, please try again.");
92 # submitted, let's see
93 my $fillok = $proj->cgi_fill($gcgi, 1);
94 my $authok = $proj->authenticate($gcgi);
95 $authok and $protok = 1;
96 $y0 eq 'View All' and last;
97 if (!$gcgi->err_check && $authok) {
98 $proj->update or last;
99 print "<p class=\"updated\">Project successfully updated at $ts.</p>\n";
100 if ($proj->{clone_failed}) {
101 $proj->clone;
102 print "<p>Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}".
103 "/mirrorproj.cgi?name=$escname\">pass onwards</a>.</p>\n";
104 print "<script language=\"javascript\">document.location=".
105 "'@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname'</script>\n";
106 exit;
111 # $proj may be insane now but that's actually good for us since we'll let the
112 # user fix the invalid values he or she entered
113 my %h = $proj->form_defaults;
115 print <<EOT;
116 <p>Here you may adjust the settings of project $h{name}. Go wild.
118 if ($proj->{mirror}) {
119 print <<EOT;
120 Since this is a mirrored project, you may opt to remove it from the site as well.
121 Just <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">remove it</a>.</p>
123 } else {
124 if ($proj->is_empty) {
125 print <<EOT;
126 Since this is an empty project, you may opt to remove it from the site as well.
127 Just <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">remove it</a>.</p>
129 } else {
130 print <<EOT;
131 You may
132 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">request an authorization code</a> in order
133 to remove this project from the site.</p>
136 print <<EOT;
137 <p>Use the <b>+</b> button to enable access for more than a single user at a time.</p>
141 my $button_label = $proj->{clone_failed} ? 'Restart Mirroring' : 'Update';
142 my $showstatusopt = $proj->{mirror} && !$proj->{clone_failed} && !$proj->{clone_in_progress};
143 my $statuschecked = $proj->{statusupdates} ? 'checked="checked"' : '';
144 my $tokauth = get_token_field("projedit", $h{name}, $Girocco::Config::project_edit_timeout);
145 $tokauth and $tokauth = "\n".$tokauth;
147 print <<EOT;
148 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi">$tokauth
149 <input type="hidden" name="tzoffset" value="0" />
150 <table class="form">
151 <tr><td class="formlabel">Project name:</td><td class="formdata"><a
152 href="@{[url_path($Girocco::Config::gitweburl)]}/$h{name}.git">$h{name}</a>.git
153 <input type="hidden" name="name" value="$h{name}" /></td></tr>
155 if ($Girocco::Config::project_passwords) {
156 my $vab = '';
157 $protok || !%{$Girocco::Config::protect_fields} or
158 $vab = '&#160;&#160;<input type="submit" name="y0" value="View All" />';
159 print <<EOT;
160 <tr><td class="formlabel"><strong>Admin password:</strong></td><td>
161 <input type="password" name="cpwd" /> <sup class="sup"><span><a
162 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname"
163 class="ctxaction">(forgot password?)</a></span></sup>$vab</td></tr>
164 <tr><td class="formlabel">New admin password (twice):<br />
165 <em>(leave empty to keep it the same)</em></td><td>
166 <input type="password" name="pwd" /><br /><input type="password" name="pwd2" /><br />
167 </td></tr>
170 if ($Girocco::Config::project_owners eq 'email') {
171 print <<EOT;
172 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" value="$h{email}" /></td></tr>
176 if ($proj->{mirror}) {
177 print "<tr><td class=\"formlabel\">Repository URL:</td><td><input type=\"text\" name=\"url\" value=\"$h{url}\" /></td></tr>\n";
178 print '<tr><td class="formlabel">Mirror refs:</td><td class="formdatatd">'.
179 '<label title="Unchecking this will mirror the entire refs namespace which is usually unnecessary. '.
180 'Non-git sources always mirror the entire refs namespace regardless of this setting.">'.
181 '<input type="checkbox" name="cleanmirror" value="1" '.($h{'cleanmirror'} ? 'checked="checked" ' : '').
182 'style="vertical-align:middle" /><span style="vertical-align:middle; margin-left:0.5ex">'.
183 'Only mirror <code>refs/heads/*</code>, <code>refs/tags/*</code> and <code>refs/notes/*</code></span></label></td></tr>'."\n"
184 if grep(/cleanmirror/, @Girocco::Config::project_fields);
185 } else {
186 print <<EOT;
187 <tr><td class="formlabel" style="vertical-align:middle">Users:</td><td>
188 <ul>
190 $Girocco::Config::manage_users and print "<p>Only <a href=\"".
191 "@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi\">registered users</a> may push.</p>";
192 if ($Girocco::Config::mob and not grep { $_ eq $Girocco::Config::mob } @{$h{users}}) {
193 print "<p><em>(Please consider adding the <tt>$Girocco::Config::mob</tt> user.\n";
194 print "<sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/mob.html\">(learn more)</a></span></sup>)\n";
195 print "</em></p>\n";
197 foreach my $user (@{$h{users}}) {
198 my $mlm = '';
199 $mlm = " <sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/mob.html\">(learn more)</a></span></sup>"
200 if $Girocco::Config::mob && $user eq $Girocco::Config::mob;
201 print "<li><input type=\"checkbox\" name=\"user\" value=\"$user\" checked=\"1\" /> $user$mlm</li>\n";
203 print <<EOT;
204 <li>Add user: <input type="text" name="user" /></li>
205 <button type="button" onclick="adduseritem(this)"><b>+</b></button>
206 </ul>
207 </td></tr>
211 print '<tr><td class="formlabel">Default branch:</td><td class="formdatatd"><select size="1" name="HEAD">';
212 for ($proj->get_heads) {
213 my $selected = $proj->{HEAD} eq $_ ? ' selected="selected"' : '';
214 print "<option$selected>".Girocco::CGI::html_esc($_)."</option>";
216 print '</select></td></tr>
219 print '<tr><td class="formlabel">Tags (select to delete):</td><td>';
220 print '<select size="6" name="tags" multiple="multiple">';
221 for ($proj->get_ctag_names) {
222 print '<option>'.Girocco::CGI::html_esc($_).'</option>';
224 print '</select></td></tr>
228 $gcgi->print_form_fields($Girocco::Project::metadata_fields,
229 $protok?{}:$Girocco::Config::protect_fields,
230 \%h, @Girocco::Config::project_fields);
231 print <<EOT if $showstatusopt;
232 <tr><td class="formlabel">Enable status update emails:</td>
233 <td class="formdatatd"
234 ><input type="hidden" name="setstatusupdates" value="1"
235 /><input type="checkbox" name="statusupdates" value="1" $statuschecked /></td></tr>
237 print <<EOT;
238 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="$button_label" /></td></tr>
239 </table>
240 </form>
241 <script type="text/javascript">
242 // <![CDATA[
243 (function () {
244 var tzoffset = (new Date).getTimezoneOffset() * -60;
245 var form0 = document.forms[0];
246 if (form0 && form0.tzoffset) {
247 form0.tzoffset.value = tzoffset;
249 })();
250 // ]]>
251 </script>