Girocco/Util.pm: add from_json function
[girocco/readme.git] / cgi / editproj.cgi
blob2080ace086096ccaa57ef5c8d9943e2cc6d8ece0
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 if (($y0 eq 'Update' || $y0 eq 'Restart Mirroring') && $cgi->request_method eq 'POST') {
84 # submitted, let's see
85 my $ts = "<span class=\"timestamp\">" . format_epoch_ts() . "</span>";
86 $gcgi->err_prelude("<p class=\"failed\">Project update failed at $ts.</p>\n");
87 if ($proj->cgi_fill($gcgi) and $proj->authenticate($gcgi) and $proj->update) {
88 print "<p class=\"updated\">Project successfully updated at $ts.</p>\n";
89 if ($proj->{clone_failed}) {
90 $proj->clone;
91 print "<p>Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}".
92 "/mirrorproj.cgi?name=$escname\">pass onwards</a>.</p>\n";
93 print "<script language=\"javascript\">document.location=".
94 "'@{[url_path($Girocco::Config::webadmurl)]}/mirrorproj.cgi?name=$escname'</script>\n";
95 exit;
100 # $proj may be insane now but that's actually good for us since we'll let the
101 # user fix the invalid values he or she entered
102 my %h = $proj->form_defaults;
104 print <<EOT;
105 <p>Here you may adjust the settings of project $h{name}. Go wild.
107 if ($proj->{mirror}) {
108 print <<EOT;
109 Since this is a mirrored project, you may opt to remove it from the site as well.
110 Just <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">remove it</a>.</p>
112 } else {
113 if ($proj->is_empty) {
114 print <<EOT;
115 Since this is an empty project, you may opt to remove it from the site as well.
116 Just <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">remove it</a>.</p>
118 } else {
119 print <<EOT;
120 You may
121 <a href="@{[url_path($Girocco::Config::webadmurl)]}/delproj.cgi?name=$escname">request an authorization code</a> in order
122 to remove this project from the site.</p>
125 print <<EOT;
126 <p>Use the <b>+</b> button to enable access for more than a single user at a time.</p>
130 my $button_label = $proj->{clone_failed} ? 'Restart Mirroring' : 'Update';
131 my $showstatusopt = $proj->{mirror} && !$proj->{clone_failed} && !$proj->{clone_in_progress};
132 my $statuschecked = $proj->{statusupdates} ? 'checked="checked"' : '';
134 print <<EOT;
135 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/editproj.cgi">
136 <input type="hidden" name="tzoffset" value="0" />
137 <table class="form">
138 <tr><td class="formlabel">Project name:</td><td class="formdata"><a
139 href="@{[url_path($Girocco::Config::gitweburl)]}/$h{name}.git">$h{name}</a>.git
140 <input type="hidden" name="name" value="$h{name}" /></td></tr>
142 if ($Girocco::Config::project_passwords) {
143 print <<EOT;
144 <tr><td class="formlabel"><strong>Admin password:</strong></td><td>
145 <input type="password" name="cpwd" /> <sup class="sup"><span><a
146 href="@{[url_path($Girocco::Config::webadmurl)]}/pwproj.cgi?name=$escname"
147 class="ctxaction">(forgot password?)</a></span></sup></td></tr>
148 <tr><td class="formlabel">New admin password (twice):<br />
149 <em>(leave empty to keep it the same)</em></td><td>
150 <input type="password" name="pwd" /><br /><input type="password" name="pwd2" /><br />
151 </td></tr>
154 if ($Girocco::Config::project_owners eq 'email') {
155 print <<EOT;
156 <tr><td class="formlabel">E-mail contact:</td><td><input type="text" name="email" value="$h{email}" /></td></tr>
160 if ($proj->{mirror}) {
161 print "<tr><td class=\"formlabel\">Repository URL:</td><td><input type=\"text\" name=\"url\" value=\"$h{url}\" /></td></tr>\n";
162 print '<tr><td class="formlabel">Mirror refs:</td><td class="formdatatd">'.
163 '<label title="Unchecking this will mirror the entire refs namespace which is usually unnecessary. '.
164 'Non-git sources always mirror the entire refs namespace regardless of this setting.">'.
165 '<input type="checkbox" name="cleanmirror" value="1" '.($h{'cleanmirror'} ? 'checked="checked" ' : '').
166 'style="vertical-align:middle" /><span style="vertical-align:middle; margin-left:0.5ex">'.
167 'Only mirror <code>refs/heads/*</code>, <code>refs/tags/*</code> and <code>refs/notes/*</code></span></label></td></tr>'."\n"
168 if grep(/cleanmirror/, @Girocco::Config::project_fields);
169 } else {
170 print <<EOT;
171 <tr><td class="formlabel" style="vertical-align:middle">Users:</td><td>
172 <ul>
174 $Girocco::Config::manage_users and print "<p>Only <a href=\"".
175 "@{[url_path($Girocco::Config::webadmurl)]}/reguser.cgi\">registered users</a> may push.</p>";
176 if ($Girocco::Config::mob and not grep { $_ eq $Girocco::Config::mob } @{$h{users}}) {
177 print "<p><em>(Please consider adding the <tt>$Girocco::Config::mob</tt> user.\n";
178 print "<sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/mob.html\">(learn more)</a></span></sup>)\n";
179 print "</em></p>\n";
181 foreach my $user (@{$h{users}}) {
182 my $mlm = '';
183 $mlm = " <sup class=\"sup\"><span><a href=\"@{[url_path($Girocco::Config::htmlurl)]}/mob.html\">(learn more)</a></span></sup>"
184 if $Girocco::Config::mob && $user eq $Girocco::Config::mob;
185 print "<li><input type=\"checkbox\" name=\"user\" value=\"$user\" checked=\"1\" /> $user$mlm</li>\n";
187 print <<EOT;
188 <li>Add user: <input type="text" name="user" /></li>
189 <button type="button" onclick="adduseritem(this)"><b>+</b></button>
190 </ul>
191 </td></tr>
195 print '<tr><td class="formlabel">Default branch:</td><td><select size="1" name="HEAD">';
196 for ($proj->get_heads) {
197 my $selected = $proj->{HEAD} eq $_ ? ' selected="selected"' : '';
198 print "<option$selected>".Girocco::CGI::html_esc($_)."</option>";
200 print '</select></td></tr>
203 print '<tr><td class="formlabel">Tags (select to delete):</td><td>';
204 print '<select size="6" name="tags" multiple="multiple">';
205 for ($proj->get_ctag_names) {
206 print '<option>'.Girocco::CGI::html_esc($_).'</option>';
208 print '</select></td></tr>
212 $gcgi->print_form_fields($Girocco::Project::metadata_fields, \%h, @Girocco::Config::project_fields);
213 print <<EOT if $showstatusopt;
214 <tr><td class="formlabel">Enable status update emails:</td>
215 <td class="formdatatd"
216 ><input type="hidden" name="setstatusupdates" value="1"
217 /><input type="checkbox" name="statusupdates" value="1" $statuschecked /></td></tr>
219 print <<EOT;
220 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="$button_label" /></td></tr>
221 </table>
222 </form>
223 <script type="text/javascript">
224 // <![CDATA[
225 (function () {
226 var tzoffset = (new Date).getTimezoneOffset() * -60;
227 var form0 = document.forms[0];
228 if (form0 && form0.tzoffset) {
229 form0.tzoffset.value = tzoffset;
231 })();
232 // ]]>
233 </script>