-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-create.cgi
executable file
·280 lines (255 loc) · 6.08 KB
/
git-create.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/perl -w
BEGIN {
# make the environment safe
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{PATH} = "";
}
use strict;
use CGI;
#use Path::Class qw(file);
use CGI::Carp qw(fatalsToBrowser);
#use Apache::Htpasswd;
my $cgi = new CGI;
$|++;
$ENV{LANG} = 'en_US.UTF-8';
my $color = $cgi->param("color") || "#e1e5ff";
my $css = <<EOT;
body{
background:$color;
font-family:sans-serif;
font-size:11pt;
padding:0;
margin:0;
}
#content{
background:#fff;
padding:10px;
margin:0 5%;
}
#menu{
text-align:center;
margin-top:30px;
}
a{
text-decoration:none;
font-weight:bold;
}
h1{
font-size:16pt;
text-align:center;
margin:5px 100px;
border:2px solid $color;
}
h2{
border-bottom:2px solid $color;
}
p.note{
border:1px solid #ccc;
padding:5px;
background:#eee;
}
table{
border:1px solid #ccc;
border-collapse:collapse;
background:#FFF;
margin:5px 0;
width:100%;
font-size:small;
}
tr{
border:1px dotted #ccc;
}
tr:hover{
background:#eee;
}
th{
background:#eee;
}
td, th{
padding:3px 5px;
}
td.emphase{
background-color:#eee;
font-weight:bold;
}
ul {
list-style-type:none;
}
EOT
my %settings = (title => "Git Creation Page",
gitdir => "/var/www/git/",
gitcmd => "/usr/bin/git",
withtrac => "false",
gitalias => "gitdir",
traccmd => "/usr/bin/trac-admin",
tracdir => "/var/www/trac/",
fields => [ "newgitname", "newgitdesc" ],
);
$settings{user} = $ENV{REMOTE_USER};
if ( $ENV{GITDIR} ){
$settings{gitdir} = $ENV{GITDIR};
}
if ( $ENV{GITCMD} ){
$settings{gitcmd} = $ENV{GITCMD};
}
if ( $ENV{WITHTRAC} ){
$settings{withtrac} = $ENV{WITHTRAC};
}
if ( $ENV{TRACCMD} ){
$settings{traccmd} = $ENV{TRACCMD};
}
if ( $ENV{TRACDIR} ){
$settings{tracdir} = $ENV{TRACDIR};
}
if ( $ENV{GITALIAS} ){
$settings{gitalias} = $ENV{GITALIAS};
}
our $proto = 'http://';
if ( $ENV{HTTPS} eq 'on' ){
$proto = 'https://';
}
my $gitpath = $settings{gitalias};
if ($ENV{REQUEST_METHOD} eq 'GET'){
print_page_headers();
print_form();
print_git_repos();
print_end();
}
if ($ENV{REQUEST_METHOD} eq 'POST'){
print_page_headers();
process_form();
return_link();
print_end();
}
sub fail{
print "$_";
exit 1;
}
sub process_form{
my %data;
for my $field ($cgi->param()){
if ( scalar grep /^\Q$field\E$/, @{$settings{fields}} ){
# its a field we know about
my $tmp = substr($cgi->param($field), 0, 50);
$tmp = lc($tmp) if ( $field eq "change_user_name" );
$data{$field} = $tmp || '';
}
}
if ( "$data{newgitname}" eq ''){
print "Missing New Git Name.";
return;
}
if ( $data{newgitname} =~ m/^\..*/
or $data{newgitname} =~ m/.*\/.*/
or $data{newgitname} =~ m/.*\ .*/
or $data{newgitname} !~ m/^[\d\w_\-.]*$/ ) {
print "Forbidden character(s) in new Git name.";
return;
}
$data{newgitdesc} =~ s/'/’/g;
if ( -d "$settings{gitdir}/$data{newgitname}/" ){
print "Git repository '$data{newgitname}' already exists.";
return;
}
mkdir "$settings{gitdir}/$data{newgitname}/" or die $!;
chdir "$settings{gitdir}/$data{newgitname}/" or die $!;
system("$settings{gitcmd} init --bare") and die $!;
system("$settings{gitcmd} update-server-info") and die $!;
open(my $fh, '>', "$settings{gitdir}/$data{newgitname}/description") or die $!;
print $fh "$data{newgitdesc}" or die $!;
close $fh;
if ($settings{withtrac} eq 'true'){
system("$settings{traccmd}", "$settings{tracdir}",
"repository", "add", "$data{newgitname}",
"$settings{gitdir}/$data{newgitname}/", "git") and die $!;
system("$settings{traccmd}",
"$settings{tracdir}", "repository",
"set", "$data{newgitname}",
"description", "$data{newgitdesc}") and die $!;
}
return;
}
sub return_link{
print "<br><a href=\"$ENV{URI}\">Return</a>";
return;
}
# print page header
sub print_page_headers {
my $title = $settings{title} || "Page without a title";
print $cgi->header(-charset => 'utf-8');
print $cgi->start_html( -title => "$title",
-style => { -code => $css });
print $cgi->h2($title);
print $cgi->hr();
return;
}
# scan the git root directory for git repository
sub print_git_repos {
my $gitdir = $settings{gitdir} || die "Missing GIT Directory Parameter";
print $cgi->h3("Existing Git Repositories");
print $cgi->hr();
print $cgi->start_table({-border=>1});
print $cgi->Tr(
$cgi->th(['Name', 'URL', 'Description'])
);
opendir(dh, $settings{gitdir}) || die;
my @gitrepo = readdir(dh);
@gitrepo = sort @gitrepo;
while( my $dir = shift @gitrepo ) {
my $desc = '';
if ( -f "$settings{gitdir}/$dir/config"){
if( -f "$settings{gitdir}/$dir/description"){
open(fh, "$settings{gitdir}/$dir/description");
$desc = <fh>;
}
print $cgi->Tr(
$cgi->td([$dir, "${proto}$ENV{SERVER_NAME}/${gitpath}/${dir}", "${desc}"])
);
}
}
print $cgi->end_table();
print $cgi->hr();
return;
}
# Form to create a new git repository
sub print_form {
for (@{$settings{fields}} ){
$cgi->delete($_);
}
print $cgi->h3("Create a new git repository");
print $cgi->hr();
print $cgi->start_form();
print $cgi->table({-border=>0},
$cgi->Tr($cgi->td('New Git Name:'),
$cgi->td($cgi->textfield(-name => 'newgitname',
-value => '',
-size => 20,
-maxlength => 18)),),
$cgi->Tr($cgi->td('New Git Description:'),
$cgi->td($cgi->textfield(-name => 'newgitdesc',
-value => '',
-size => 80,
-maxlength => 78)),),
$cgi->Tr($cgi->td($cgi->submit(-name => 'gitcreate',
-value => 'Create Git'))),);
print $cgi->end_form(), $cgi->hr();
}
# print end of the page
sub print_end {
print $cgi->end_html();
return;
}
# small function printing all http headers
sub print_header {
print $cgi->h3("HTTP Headers");
foreach(sort keys %ENV) {
print "<table>";
print "<tr>
<td >$_</td>
<td ><tt>$ENV{$_}</tt></td>
<td ></tr>";
print "</table>";
}
print $cgi->hr();
}
exit;