This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgitwebedit.cgi
executable file
·535 lines (494 loc) · 12.7 KB
/
tgitwebedit.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/perl
# A tiny web-based editor
# Copyright (C) Eskild Hustvedt 2009
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use CGI;
use FindBin;
use lib "$FindBin::RealBin/lib/";
use Try::Tiny;
use File::Basename qw(basename dirname);
use autouse 'Cwd' => qw(realpath);
use autouse 'HTML::Entities' => qw(encode_entities);
use constant { true => 1, false => 0 };
my $q;
my $reqHeader = false;
my $VERSION = '0.1';
my $instDir = dirname($0);
my $menuSlurp = false;
my %conf;
# Purpose: Load a configuration file
# Usage: LoadConfigFile(/FILE, \%ConfigHash, \%OptionRegexHash, OnlyValidOptions?);
# OptionRegeXhash can be available for only a select few of the config options
# or skipped completely (by replacing it by undef).
# If OnlyValidOptions is true it will cause LoadConfigFile to skip options
# not in the OptionRegexHash.
sub LoadConfigFile
{
my ($File, $ConfigHash, $OptionRegex, $OnlyValidOptions) = @_;
open(my $CONFIG, '<', "$File") or do {
warn(sprintf('Unable to read the configuration settings from %s: %s', $File, $!));
return(0);
};
while(<$CONFIG>) {
next if m/^\s*(#.*)?$/;
next if not m/=/;
chomp;
my $Option = $_;
my $Value = $_;
$Option =~ s/^\s*(\S+)\s*=.*/$1/;
$Value =~ s/^\s*\S+\s*=\s*(.*)\s*/$1/;
if($OnlyValidOptions) {
unless(defined($OptionRegex->{$Option})) {
warn("Unknown configuration option \"$Option\" (=$Value) in $File: Ignored.");
next;
}
}
unless(defined($Value)) {
warn("Empty value for option $Option in $File");
}
if(defined($OptionRegex) and defined($OptionRegex->{$Option})) {
my $MustMatch = $OptionRegex->{$Option};
unless ($Value =~ /$MustMatch/) {
warn("Invalid setting of $Option (=$Value) in the config file: Must match $OptionRegex->{Option}.");
next;
}
}
$ConfigHash->{$Option} = $Value;
}
close($CONFIG);
}
sub slurp
{
my $file = shift;
my $o = $/;
undef $/;
open(my $i,'<',$file) or return;
my $r = <$i>;
$/ = $o;
close($i);
return $r;
}
sub confVal
{
my $val = shift;
if(not keys %conf)
{
LoadConfigFile('./tgitwebedit.conf',\%conf);
if(not $conf{restrictedPath} =~ /^\//)
{
$conf{restrictedPath} = realpath($conf{restrictedPath});
}
if(not $conf{restrictedPath})
{
error('restrictedPath config option is missing');
}
}
return $conf{$val};
}
sub header
{
my $title = shift;
my $o;
$o = $q->header();
$o .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";
$o .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>';
$o .= '<title>TGitWebEdit';
if ($title)
{
$o .= ' - '.$title;
}
$o .= '</title>';
$o .= '<meta name="robots" content="noindex, nofollow" />';
$o .= '<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />';
# YUI
$o .= '<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.0r4/build/assets/skins/sam/skin.css" />';
$o .= '<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js"></script>';
$o .= '<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/element/element-min.js"></script>';
$o .= '<script src="http://yui.yahooapis.com/2.8.0r4/build/container/container_core-min.js"></script>';
$o .= '<script src="http://yui.yahooapis.com/2.8.0r4/build/editor/simpleeditor-min.js"></script>';
# Scripts
$o .= '<script type="text/javascript">function tglog(msg) { if(typeof(msg) == "object") { msg = "Exception: "+msg.message }; if(console && console.log) { console.log(msg); } }</script>';
$o .= '<script type="text/javascript">var $ = function (i) { return document.getElementById(i); };</script>';
$o .= '<script type="text/javascript">var runToggleRTE = false; function onloadRunner () { if (runToggleRTE) { toggleRTE(); } };</script>';
$o .= '</head><body class="yui-skin-sam" onload="onloadRunner();">';
$o .= '<div id="header"><h3>TGitWebEdit</h3>';
$o .= '<span id="menu">';
if (not $menuSlurp and -e $instDir.'/custom_menu.html')
{
$menuSlurp = true;
$o .= slurp($instDir.'/custom_menu.html') or error('Failed to slurp the menu: '.$!);
}
else
{
if (-e $instDir.'/custom_index.html')
{
$o .= '<a href="'.$q->url().'">Main page</a> - ';
}
$o .= '<a href="'.$q->url().'?type=file_list">File list</a>';
}
$o .= '<hr /><br /></div><div id="primaryContent">';
$reqHeader = true;
return $o;
}
sub footer
{
my $o = '</div><div id="footer"><br /><br /><small>Generated by TGitWebEdit version '.$VERSION.'</small></div>';
$o .= '</body></html>';
return $o;
}
sub editFile
{
my $file = $q->param('filePath');
if(not defined $file or not length $file)
{
error('No filePath supplied');
}
$file = realpath(confVal('restrictedPath').'/'.$file);
if(not defined $file or not length $file)
{
error('Illegal path');
}
my $c = '';
my $canSave = true;
if (-e $file)
{
if(-d $file)
{
error($file.': is a directory');
}
elsif(-x $file)
{
error($file.': is executable. Refusing to edit an executeable file.');
}
open(my $i,'<',$file);
undef $/;
$c = <$i>;
close($i);
if(not -w $file)
{
$canSave = false;
}
}
print header('Editing '.basename($file));
if(not $canSave)
{
print '<b>WARNING: </b>This file is not writeable, you will not be able to save any changes!<br /><br />';
}
print '<form name="editor" method="post" action="'.$q->url().'?type=file_save'.'">';
print '<input type="hidden" name="type" value="file_save" />';
print '<input type="hidden" name="filePath" value="'.relativeRestrictedPath($file).'" />';
print textEditor($c,$file);
print '<br />';
if ($canSave)
{
print '<input type="submit" value="Save file" /> ';
}
# FIXME: Add some magic so that if the back fails, we still forward
# to url()
print '<a href="'.$q->url().'"><input type="button" value="Cancel and discard changes" onclick="window.history.back(); return false;"></a>';
print '</form>';
print footer();
}
sub saveFile
{
my $content = $q->param('mainEditor');
if(not defined $content)
{
error('No content submitted');
}
my $errc = '<br /><br />The data you submitted is included below so that you may save it some other way until you are able to fix this issue.<br /><textarea rows="5" cols="50">'.$content.'</textarea>';
my $file = $q->param('filePath');
if(not defined $file or not length $file)
{
error('saveFile(): no filePath!'.$errc);
}
$file = realpath(confVal('restrictedPath').'/'.$file);
if(not defined $file or not length $file)
{
error('Illegal path'.$errc);
}
if (-e $file)
{
if(-d $file)
{
error($file.': is a directory'.$errc);
}
elsif(-x $file)
{
error($file.': is executable. Refusing to edit an executeable file.'.$errc);
}
elsif(not -w $file)
{
error($file.': is not writeable by tgitwebedit (running as UID '.$<.'). Data could not be saved.'.$errc);
}
}
open(my $out,'>',$file) or error('Failed to open '.$file.' for writing: '.$!);
print {$out} $content;
close($out);
if (confVal('enableGit'))
{
system('git','add',$file);
system('git','commit','-m', 'Changes made by '.$q->remote_host());
}
print header();
print 'The file was saved successfully';
print footer();
}
sub textEditor
{
my $content = shift;
my $file = shift;
my $o = '';
$o .= '<script type="text/javascript">
var rteON = false;
var RTE = null;
function toggleRTE()
{
if (rteON)
{
if(RTE)
{
RTE.saveHTML();
RTE.destroy();
RTE = null;
}
rteON = false;
$("rtestatus").innerHTML = "on";
}
else
{
rteON = true;
try
{
RTE = new YAHOO.widget.SimpleEditor("mainEditor", { handleSubmit: true });
}
catch(error)
{
tglog(error);
}
RTE.render();
$("rtestatus").innerHTML = "off";
}
}</script>';
$o .= '<b>'.basename($file).'</b>:<br />';
$o .= '<a href="#" onclick="try { toggleRTE(); } catch(e) {tglog(e);}; return false">Toggle graphical (HTML) editor <span id="rtestatus">on</span></a></a><br />';
$o .= '<textarea name="mainEditor" id="mainEditor" cols="100" rows=30">'.$content.'</textarea>';
if ($content =~ /<\s*br\s*[^>]>/i)
{
$o .= '<script type="text/javascript">runToggleRTE = true;</script>';
}
return $o;
}
sub URL_fileSelector
{
my $file = shift;
$file = relativeRestrictedPath($file);
my $p = $q->url().'?type=file_list&dirPath='.$file;
return encode_entities($p);
}
sub URL_editFile
{
my $file = shift;
$file = relativeRestrictedPath($file);
my $p = $q->url().'?type=file_edit&filePath='.$file;
return encode_entities($p);
}
sub fileListing
{
my $dir = shift;
my $l = '<b>'.$dir.'</b>:<br />';
$l .= '<table style="border:0px;">';
my @dirs = sort(glob($dir.'/*'));
if(not realpath($dir) eq realpath(confVal('restrictedPath')))
{
unshift(@dirs,{ url => URL_fileSelector($dir.'/../'), label => '[DIR]', name => '.. (one level up)' });
}
else
{
unshift(@dirs,{ url => '',label => ' ', name => '' });
}
foreach my $p (@dirs)
{
my ($label,$url,$name);
if(ref($p))
{
$label = $p->{label};
$url = $p->{url};
$name = $p->{name};
}
else
{
my $b = basename($p);
if (-d $p)
{
$label = '[DIR]';
$url = URL_fileSelector($p);
}
else
{
$label = '[FILE]';
if (not -x $p)
{
$url = URL_editFile($p);
}
}
$name = $b;
}
$l .= '<tr><td>';
$l .= $label;
$l .= '</td><td>';
if(defined $url and length $url)
{
$name = '<a href="'.$url.'">'.$name.'</a>';
}
$l .= $name;
$l .= '</a></td></tr>';
}
$l .= '</table>';
return $l;
}
sub fileSelector
{
my $path = $q->param('dirPath');
$path = defined $path ? safePath($path) : '.';
$path = realpath(confVal('restrictedPath').'/'.$path);
if(not defined $path or not length $path)
{
error('Illegal path');
}
elsif(not -e $path)
{
error($path.': does not exist');
}
elsif(not -d $path)
{
error($path.': is not a directory');
}
print header()."\n";
print fileListing($path)."\n";
print footer();
}
sub relativeRestrictedPath
{
my $path = shift;
$path = realpath($path);
my $rpath = confVal('restrictedPath');
$path =~ s/$rpath//;
return $path;
}
sub safePath
{
my $path = shift;
$path =~ s/\.//g;
return $path;
}
sub error
{
my $e = shift;
if(not $reqHeader)
{
print header('Error');
}
print '<b>Error: </b>';
print $e;
print footer();
exit(1);
}
sub defaultPage
{
if (-e $instDir.'/custom_index.html')
{
print header();
my $cust = slurp($instDir.'/custom_index.html') or error('Failed to slupr the custom index: '.$!);
print $cust;
print footer();
}
else
{
fileSelector();
}
}
sub main
{
$q = CGI->new;
my $type = $q->param('type');
$type = defined $type ? $type : 'default';
if(not -e $instDir.'/.htaccess')
{
error($instDir.'/.htaccess: does not exist, refusing to continue.<br />tgitwebedit does not contain any authentication support, and you must therefore use HTTP auth. When .htaccess does not exist, tgitwebedit assumes no authentication is being used and refuses to work.');
}
if ($type eq 'default')
{
defaultPage();
}
elsif($type eq 'file_list')
{
fileSelector();
}
elsif($type eq 'file_edit')
{
editFile();
}
elsif($type eq 'file_save')
{
saveFile();
}
elsif(defined $type and length($type))
{
error('The type "'.$type.'" is unknown. Bailing out.');
}
else
{
error('No type= submitted');
}
if(not $reqHeader)
{
error('Internal error, no request headers sent at end of app.');
}
}
try
{
main();
}
catch
{
my $e = $_;
try
{
error('main() error: '.$e);
}
catch
{
die('Error when running main(): '.$e."\n\n".'Error during error() as well: '.$_);
};
};
__END__
=head1 NAME
=head1 DESCRIPTION
=head1 CONFIGURATION
=head1 CUSTOMIZATION
tgitwebedit can have additional customizations applied, in addition to
what is already offered by the configuration options. You can create a
custom menu, or a custom entry page.
=head2 Custom menu
By default tgitwebedit will create its own menu. However, if you want to
replace it with a custom menu, just create I<custom_menu.html>
=head2 Custom entry page
The entry page is the page that is first showed when you enter tgitwebedit.
By default the root file list will be showed, however if you edit/create
I<custom_index.html> you can add any custom welcome page you want.
=head1 LICENSE AND COPYRIGHT