forked from rumbero71/pihole-shallalist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cgi
171 lines (157 loc) · 4.6 KB
/
index.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
#!/usr/bin/perl -w
use CGI; # load CGI routines
$CGI::POST_MAX = 160 * 40; # max 160rows/40cols post
$CGI::DISABLE_UPLOADS = 1; # no uploads
# File Paths
$SHALLAPATH="/var/www/shalla";
$CATEGORYFILE=$SHALLAPATH."/shallacategories.txt";
$BLOCKFILE=$SHALLAPATH."/shallablock.txt";
$WHITELISTFILE=$SHALLAPATH."/whitelist.txt";
$BLACKLISTFILE=$SHALLAPATH."/blacklist.txt";
# Regexps for sanitation
$BLOCKOK='-a-zA-Z0-9/';
$EXPLAINOK='-a-zA-Z0-9/.@ ';
$LISTOK=$EXPLAINOK.'\n';
$q = CGI->new;
print $q->header, # create the HTTP header
$q->start_html('Shalla List Config'); # start the HTML
# Error if there is no category file
sub nocategories {
print $q->h1('no category file'); # level 1 header
print $q->end_html; # end the HTML
die ("File $CATEGORYFILE not found !\n");
}
# Read category list
open(my $fh, '<', $CATEGORYFILE) or nocategories();
while ($inline=<$fh>){
chomp $inline;
@cats=split / /,$inline;
$mycat=shift @cats;
$mycat=~s/[^$BLOCKOK]//go;
$category{$mycat}=0;
$inline=join " ",@cats;
$inline=~s/[^$EXPLAINOK]//go;
$explain{$mycat}="<span title=\"".$inline."\">".$mycat."</span>";
}
close $fh;
# Read list of currently blocked categories if present
if ( -f $BLOCKFILE ){
open(my $fh, '<', $BLOCKFILE);
while ($inline=<$fh>){
chomp $inline;
@bla=split / /,$inline;
$bla[0]=~s/[^$BLOCKOK]//go;
push @blocked,$bla[0];
}
close $fh;
} else {
@blocked="";
}
# Read current whitelist if present
if ( -f $WHITELISTFILE ){
open(my $fh, '<', $WHITELISTFILE);
while ($inline=<$fh>){
chomp $inline;
@bla=split / /,$inline;
$bla[0]=~s/[^$LISTOK]//go;
push @whitelist,$bla[0];
}
close $fh;
} else {
@whitelist="";
}
$mywhitelist="";
while($dummy=shift @whitelist){
$mywhitelist.=$dummy."\n";
}
chomp $mywhitelist;
# Read current blacklist if present
if ( -f $BLACKLISTFILE ){
open(my $fh, '<', $BLACKLISTFILE);
while ($inline=<$fh>){
chomp $inline;
@bla=split / /,$inline;
$bla[0]=~s/[^$LISTOK]//go;
push @blacklist,$bla[0];
}
close $fh;
} else {
@blacklist="";
}
$myblacklist="";
while($dummy=shift @blacklist){
$myblacklist.=$dummy."\n";
}
chomp $myblacklist;
print $q->h1('Web Blocker');
print $q->h2('Blocked categories');
print $q->h4('Hover over a categories\' name to see a short explanation');
print $q->start_form;
# If there are no parameters, show a formto select categories
if (!$q->param) {
print CGI::unescapeHTML($q->checkbox_group(
-name => 'blocked',
-values => [sort keys %category],
-labels => \%explain,
-defaults => \@blocked,
-columns => 6,
));
print $q->h2('Whitelist');
print $q->textarea(
-name => 'whitelist',
-value => $mywhitelist,
-cols => 60,
-rows => 3,
);
print $q->h2('Blacklist');
print $q->textarea(
-name => 'blacklist',
-value => $myblacklist,
-cols => 60,
-rows => 3,
);
print $q->submit(-value=>'Submit categories');
# If parameters are present, show selected stuff, write files and go back
} else {
# Write list of currently blocked categories
open(my $fh, '>', $BLOCKFILE) or die ("Could not write Blockfile !\n");
print $q->h1('The following categories are blocked:');
@blocked = $q->param('blocked');
print "<BLOCKQUOTE>\n";
foreach $block (sort @blocked) {
$block=~s/[^$BLOCKOK]//go;
print $explain{$block}."<BR>";
print $fh $block."\n";
}
print "</BLOCKQUOTE>\n";
close $fh;
# Write current whitelist
open(my $fh, '>', $WHITELISTFILE) or die ("Could not write Whitelistfile !\n");
print $q->h1('Whitelisted sites:');
print "<BLOCKQUOTE>\n";
$mywhitelist=$q->param('whitelist');
$mywhitelist=~s/[^$LISTOK]//go;
@bla=split /[[:space:]]+/,$mywhitelist;
while ($dummy=shift @bla){
print $dummy."<br>\n";
print $fh $dummy."\n";
}
print "</BLOCKQUOTE>\n";
close $fh;
# Write current blacklist
open(my $fh, '>', $BLACKLISTFILE) or die ("Could not write Blacklistfile !\n");
print $q->h1('Blacklisted sites:');
print "<BLOCKQUOTE>\n";
$myblacklist=$q->param('blacklist');
$myblacklist=~s/[^$LISTOK]//go;
@bla=split /[[:space:]]+/,$myblacklist;
while ($dummy=shift @bla){
print $dummy."<br>\n";
print $fh $dummy."\n";
}
print "</BLOCKQUOTE>\n";
close $fh;
print $q->submit(-value=>'OK');
}
print $q->end_form;
print $q->end_html; # end the HTML