redirect.cgi: Introduce a small template engine for the error pages.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Aug 2013 14:11:55 +0000 (16:11 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Aug 2013 14:12:22 +0000 (16:12 +0200)
html/html/redirect-templates/legacy/template.html [new file with mode: 0644]
html/html/redirect.cgi

diff --git a/html/html/redirect-templates/legacy/template.html b/html/html/redirect-templates/legacy/template.html
new file mode 100644 (file)
index 0000000..b5fb61e
--- /dev/null
@@ -0,0 +1,55 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
+<html>
+       <head>
+               <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
+               <title>ACCESS MESSAGE</title>
+       </head>
+       <body>
+               <table width="100%" height='100%' border="0">
+                       <tr>
+                               <td colspan='3' width='100%' height='130' align="center" background="<TMPL_VAR NAME="ADDRESS">/images/background.gif">
+                       <tr>
+                               <td width='10%'>
+                               <td align='center' bgcolor='#CC000000' width='80%'>
+                                       <font face="verdana, arial, sans serif" color="#FFFFFF" size="5">
+                                               <b><TMPL_VAR NAME="MSG_TEXT_1"></b>
+                                       </font>
+                               <td width='10%'>
+
+                       <TMPL_IF NAME="CATEGORY">
+                               <tr>
+                                       <td colspan='3' align='center'>
+                                               <font face="verdana, arial, sans serif" color="#CC000000" size="1">
+                                                       <b>[<TMPL_VAR NAME="CATEGORY">]</b>
+                                               </font>
+                       </TMPL_IF>
+
+                       <tr>
+                               <td colspan='3' align="center">
+                                       <font face="verdana, arial, sans serif" color="#000000" size="4">
+                                               <b><TMPL_VAR NAME="MSG_TEXT_2"></b>
+                                       </font>
+                                       <font face="verdana,arial,sans serif" color="#000000" size="2">
+                                               <TMPL_IF NAME="URL">
+                                                       <p>URL: <a href="<TMPL_VAR NAME="URL">"><TMPL_VAR NAME="URL"></a>
+                                               </TMPL_IF>
+
+                                               <TMPL_IF NAME="IP_ADDRESS">
+                                                       <p>Client IP address: <i><TMPL_VAR NAME="IP_ADDRESS"></i>
+                                               </TMPL_IF>
+
+                                               <br><p><TMPL_VAR NAME="MSG_TEXT_3">
+                                       </font>
+
+                       <tr>
+                               <td colspan='3' height='60%' valign="bottom" align="right">
+                                       <font face="verdana,arial,sans serif" color="#656565" size="1">
+                                               Web Filtering by
+                                       </font>
+                                       <a href="http://www.ipfire.org" target="_blank">
+                                               <font face="verdana,arial,sans serif" color="#656565" size="1"><b>IPFire</b></font>
+                                       </a>
+
+               </table>
+       </body>
+</html>
index 93f1ea4a96aece6bdc9064120b614ab086f49ee6..17ea641e4eec801b74410ee389ffb0f79ba97460 100644 (file)
 #                                                                             #
 ###############################################################################
 
-use CGI qw(param);
+use CGI;
+use HTML::Entities;
+use HTML::Template;
 
-$swroot="/var/ipfire";
+my $swroot="/var/ipfire";
+my $templateroot = "/srv/web/ipfire/html/redirect-templates";
 
 my %netsettings;
 my %filtersettings;
@@ -29,117 +32,75 @@ my %filtersettings;
 &readhash("$swroot/ethernet/settings", \%netsettings);
 &readhash("$swroot/urlfilter/settings", \%filtersettings);
 
-$category=param("category");
-$url=param("url");
-$ip=param("ip");
+# Read the template file.
+my $template = $filtersettings{'ERROR_TEMPLATE'};
+if (($template eq '') || (! -e "$templateroot/$template")) {
+       $template = "legacy";
+}
+my $tmpl = HTML::Template->new(
+       filename => "$templateroot/$template/template.html",
+       die_on_bad_params => 0
+);
+
+# Address where to load more resources from.
+$tmpl->param(ADDRESS => "http://$netsettings{'GREEN_ADDRESS'}:81");
 
-if ($filtersettings{'MSG_TEXT_1'} eq '') {
+# Message text 1
+my $msgtext1 = $filtersettings{'MSG_TEXT_1'};
+if ($msgtext1 eq '') {
        $msgtext1 = "A C C E S S &nbsp;&nbsp; D E N I E D";
-} else { $msgtext1 = $filtersettings{'MSG_TEXT_1'}; }
-if ($filtersettings{'MSG_TEXT_2'} eq '') {
+}
+$tmpl->param(MSG_TEXT_1 => $msgtext1);
+
+# Message text 2
+my $msgtext2 = $filtersettings{'MSG_TEXT_2'};
+if ($msgtext2 eq '') {
        $msgtext2 = "Access to the requested page has been denied";
-} else { $msgtext2 = $filtersettings{'MSG_TEXT_2'}; }
-if ($filtersettings{'MSG_TEXT_3'} eq '') {
+}
+$tmpl->param(MSG_TEXT_2 => $msgtext2);
+
+# Message text 3
+my $msgtext3 = $filtersettings{'MSG_TEXT_3'};
+if ($msgtext3 eq '') {
        $msgtext3 = "Please contact the Network Administrator if you think there has been an error";
-} else { $msgtext3 = $filtersettings{'MSG_TEXT_3'}; }
+}
+$tmpl->param(MSG_TEXT_3 => $msgtext3);
+
+# Category
+my $category = CGI::param("category");
+$tmpl->param(CATEGORY => &escape($category));
+
+# URL
+my $url = CGI::param("url");
+$tmpl->param(URL => &escape($url));
 
-if ($category eq '') { $category = '&nbsp;'; } else { $category = '['.$category.']'; }
+# IP address
+my $ip_address = CGI::param("ip");
+$tmpl->param(IP_ADDRESS => &escape($ip_address));
 
+# Print header
 print "Pragma: no-cache\n";
 print "Cache-control: no-cache\n";
 print "Connection: close\n";
 print "Content-type: text/html\n\n";
+print $tmpl->output;
 
-print <<END
-
-<html>
-<head>
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
-<title>ACCESS MESSAGE</title>
-</head>
-
-<body>
-<table width="100%" height='100%' border="0">
-<tr>
-               <td colspan='3' width='100%' height='130' align="center" background="http://$netsettings{'GREEN_ADDRESS'}:81/images/background.gif">
-<tr>           <td width='10%'><td align='center' bgcolor='#CC000000' width='80%'><font face="verdana, arial, sans serif" color="#FFFFFF" size="5">
-                                       <b>$msgtext1</b>
-                                       </font>
-               <td width='10%'>
-END
-;
-
-if (!($category eq ""))
-{
-       print <<END
-       <tr>            <td colspan='3' align='center'>
-                               <font face="verdana, arial, sans serif" color="#CC000000" size="1">
-                                       <b>$category</b>
-                               </font>
-END
-;
+sub escape($) {
+       my $s = shift;
+       return HTML::Entities::encode_entities($s);
 }
-print <<END
-<tr>
-                       <td colspan='3' align="center">
-                               <font face="verdana, arial, sans serif" color="#000000" size="4">
-                               <b>$msgtext2</b>
-                               </font>
-                               <font face="verdana,arial,sans serif" color="#000000" size="2">
-END
-;
-
-if (!($url eq ""))
-{
-print <<END
-                                       <p>URL: <a href="$url">$url</a>
-END
-;
-}
-
-if (!($ip eq ""))
-{
-print <<END
-                                       <p>Client IP address: <i>$ip</i>
-END
-;
-}
-
-print <<END
-                                       <br><p>$msgtext3
-                                       </font>
 
-<tr>
-       <td colspan='3' height='60%' valign="bottom" align="right">
-               <font face="verdana,arial,sans serif" color="#656565" size="1">Web Filtering by
-               </font>
-               <a href="http://www.ipfire.org" target="_blank"><b>
-               <font face="verdana,arial,sans serif" color="#656565" size="1">IPFire</b></a>
-               </font>
-
-</table>
-</body>
-
-</html>
-END
-;
-
-sub readhash
-{
+sub readhash {
        my $filename = $_[0];
        my $hash = $_[1];
        my ($var, $val);
 
-       if (-e $filename)
-       {
+       if (-e $filename) {
                open(FILE, $filename) or die "Unable to read file $filename";
-               while (<FILE>)
-               {
+               while (<FILE>) {
                        chop;
                        ($var, $val) = split /=/, $_, 2;
-                       if ($var)
-                       {
+                       if ($var) {
                                $val =~ s/^\'//g;
                                $val =~ s/\'$//g;
        
@@ -149,6 +110,7 @@ sub readhash
                                $hash->{$var} = $val;
                        }
                }
+
                close FILE;
        }
 }