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