redirect.cgi: Introduce a small template engine for the error pages.
[people/teissler/ipfire-2.x.git] / html / html / redirect.cgi
index 6d3aca85b923b2db4abedc1347052f0b4f8543ed..17ea641e4eec801b74410ee389ffb0f79ba97460 100644 (file)
@@ -1,12 +1,30 @@
 #!/usr/bin/perl
-
-#
-# $Id: redirect.cgi,v 0.2 2006/06/12 00:00:00 modyfy bye linuxadmin
-#
-
-use CGI qw(param);
-
-$swroot="/var/ipfire";
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
+#                                                                             #
+# 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 CGI;
+use HTML::Entities;
+use HTML::Template;
+
+my $swroot="/var/ipfire";
+my $templateroot = "/srv/web/ipfire/html/redirect-templates";
 
 my %netsettings;
 my %filtersettings;
@@ -14,148 +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));
 
-if ($category eq '') { $category = '&nbsp;'; } else { $category = '['.$category.']'; }
+# URL
+my $url = CGI::param("url");
+$tmpl->param(URL => &escape($url));
 
+# 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>
-
-END
-;
-
-if (($filtersettings{'ENABLE_JPEG'} eq 'on') && (-e "/home/httpd/html/images/urlfilter/background.jpg"))
-{
-print <<END
-<body background="http://$netsettings{'GREEN_ADDRESS'}:81//images/urlfilter/background.jpg" bgcolor="#FFFFFF">
-END
-;
-} else {
-print <<END
-<img src="http://$netsettings{'GREEN_ADDRESS'}:81//images/logo_ipfire.gif" ALT="IPFire-Logo">
-<body bgcolor="#455B72">
-END
-;
-}
-
-print <<END
-
-
-<center>
-
-<table width="80%" cellspacing="10" cellpadding="5" border="1">
-<tr>
-               <td nowrap align="right" background="http://$netsettings{'GREEN_ADDRESS'}:81//images/urlfilter/bgcool.gif">
-               <font face="verdana, arial, sans serif" color="#CC000000" size="1">
-               <b> INFO: $category</b>
-               </font>
-       </td>
-</tr>
-<tr>
-       <td bgcolor="#F4F4F4" align="center">
-               <table width="100%" cellspacing="20" cellpadding="18" border="1">
-                       <tr>
-                               <td nowrap align="center" background="http://$netsettings{'GREEN_ADDRESS'}:81//images/urlfilter/bgcool.gif">
-                                       <font face="verdana, arial, sans serif" color="#CC000000" size="5">
-                                       <b>$msgtext1</b>
-                                       </font>
-                               </td>
-                       </tr>
-                       <tr>
-                               <td bgcolor="#E2E2E2" 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
-;
+sub escape($) {
+       my $s = shift;
+       return HTML::Entities::encode_entities($s);
 }
 
-print <<END
-                                       <br><p>$msgtext3
-                                       </font>
-                               </td>
-                       </tr>
-       </td>
-</tr>
-</table>
-
-<tr>
-       <td nowrap align="right" background="http://$netsettings{'GREEN_ADDRESS'}:81//images/urlfilter/bgcool.gif">
-               <font face="verdana,arial,sans serif" color="#656565" size="1">Web Filtering by
-               </font>
-               <a href="http://www.ipfire.eu" target="_blank"><b>
-               <font face="verdana,arial,sans serif" color="#656565" size="1">IPFire</b></a> and
-               <a href="http://www.squidguard.org" target="_blank"><b>
-               <font face="verdana,arial,sans serif" color="#656565" size="1">SquidGuard
-               </font></b></a>
-       </td>
-</tr>
-
-</table>
-
-<img src="http://$netsettings{'GREEN_ADDRESS'}:81//images/urlfilter/bg_cool_tux.jpg">
-</center>
-
-</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;
        
@@ -165,6 +110,7 @@ sub readhash
                                $hash->{$var} = $val;
                        }
                }
+
                close FILE;
        }
 }