redirect.cgi: Introduce a small template engine for the error pages.
[people/teissler/ipfire-2.x.git] / html / html / redirect.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use CGI;
23 use HTML::Entities;
24 use HTML::Template;
25
26 my $swroot="/var/ipfire";
27 my $templateroot = "/srv/web/ipfire/html/redirect-templates";
28
29 my %netsettings;
30 my %filtersettings;
31
32 &readhash("$swroot/ethernet/settings", \%netsettings);
33 &readhash("$swroot/urlfilter/settings", \%filtersettings);
34
35 # Read the template file.
36 my $template = $filtersettings{'ERROR_TEMPLATE'};
37 if (($template eq '') || (! -e "$templateroot/$template")) {
38 $template = "legacy";
39 }
40 my $tmpl = HTML::Template->new(
41 filename => "$templateroot/$template/template.html",
42 die_on_bad_params => 0
43 );
44
45 # Address where to load more resources from.
46 $tmpl->param(ADDRESS => "http://$netsettings{'GREEN_ADDRESS'}:81");
47
48 # Message text 1
49 my $msgtext1 = $filtersettings{'MSG_TEXT_1'};
50 if ($msgtext1 eq '') {
51 $msgtext1 = "A C C E S S &nbsp;&nbsp; D E N I E D";
52 }
53 $tmpl->param(MSG_TEXT_1 => $msgtext1);
54
55 # Message text 2
56 my $msgtext2 = $filtersettings{'MSG_TEXT_2'};
57 if ($msgtext2 eq '') {
58 $msgtext2 = "Access to the requested page has been denied";
59 }
60 $tmpl->param(MSG_TEXT_2 => $msgtext2);
61
62 # Message text 3
63 my $msgtext3 = $filtersettings{'MSG_TEXT_3'};
64 if ($msgtext3 eq '') {
65 $msgtext3 = "Please contact the Network Administrator if you think there has been an error";
66 }
67 $tmpl->param(MSG_TEXT_3 => $msgtext3);
68
69 # Category
70 my $category = CGI::param("category");
71 $tmpl->param(CATEGORY => &escape($category));
72
73 # URL
74 my $url = CGI::param("url");
75 $tmpl->param(URL => &escape($url));
76
77 # IP address
78 my $ip_address = CGI::param("ip");
79 $tmpl->param(IP_ADDRESS => &escape($ip_address));
80
81 # Print header
82 print "Pragma: no-cache\n";
83 print "Cache-control: no-cache\n";
84 print "Connection: close\n";
85 print "Content-type: text/html\n\n";
86 print $tmpl->output;
87
88 sub escape($) {
89 my $s = shift;
90 return HTML::Entities::encode_entities($s);
91 }
92
93 sub readhash {
94 my $filename = $_[0];
95 my $hash = $_[1];
96 my ($var, $val);
97
98 if (-e $filename) {
99 open(FILE, $filename) or die "Unable to read file $filename";
100 while (<FILE>) {
101 chop;
102 ($var, $val) = split /=/, $_, 2;
103 if ($var) {
104 $val =~ s/^\'//g;
105 $val =~ s/\'$//g;
106
107 # Untaint variables read from hash
108 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
109 $val =~ /([\w\W]*)/; $val = $1;
110 $hash->{$var} = $val;
111 }
112 }
113
114 close FILE;
115 }
116 }