package webconf_base;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(decode_string encode_string make_html print_head print_caption print_foot printErr printErrSimple);

#----------------------------------------------------------------------------
# /var/config/www/cgi-bin/webconf_base.pm - basic functions for webconf
#
# Creation:     07.03.2002  chriswue
# Last Update:  $Id$
#
# Copyright (c) 2001-2005  eisfair webconf team
#
# 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 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

use strict;
use CGI::Carp qw(fatalsToBrowser);

our $version;

no strict "subs";
do "conf.pl";
use strict "subs";

# sub decode_string($form_string) {{{
sub decode_string {
   my $string = shift;

   $string =~ tr/+/ /;
   $string =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   
   return $string;
}
# }}}

# sub encode_string($form_string) {{{
sub encode_string {
   my $string = shift;

   return "" if (!$string);

   $string =~ s/([^\w ])/"%".unpack("H*", $1)/eg;
   $string =~ tr/ /+/;
   
   return $string;
}
# }}}

# sub make_html($string) {{{
sub make_html
{
    my $string = shift;

    return "" if (!$string);

    $string =~ s/\\</&lt;/g;
    $string =~ s/\\>/&gt;/g;
    $string =~ s/\\&/&amp;/g;
    $string =~ s/\\"/&quot;/g;
    $string =~ s/ä/&auml;/g;
    $string =~ s/Ä/&Auml;/g;
    $string =~ s/ö/&ouml;/g;
    $string =~ s/Ö/&Ouml;/g;
    $string =~ s/ü/&uuml;/g;
    $string =~ s/Ü/&Uuml;/g;
    $string =~ s/ß/&szlig;/g;

    return $string;
}
# }}}



# print_head description {{{
#######################################
# sub print_head($title, $class)
# - prints the head of the HTML-page
# IN:  $title   - title of the page
#      $package - current package
#      $class   - CSS-class for the body
# OUT: -
#######################################
# }}}
# sub print_head($title, $package, $class) {{{
sub print_head {
   my ($title, $package, $class) = @_;

   $title =~ s/\$PACKAGE/$package/g;

   print "Content-type: text/html\n\n";
   print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Strict//EN\" ";
   print "\"http://www.w3.org/TR/html4/strict.dtd\">\n";
   print "<html><head>\n";
   print "<title>$title</title>\n";
   print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../CSS/style.css\">\n";
   print "</head><body $class>\n";
}
# }}}

# print_caption description {{{
############################################################################
# sub print_caption($caption, $class, $no_min)
# - prints caption 
# IN:  $caption  - caption
#      $package  - current package
#      $class    - CSS-class for the body, hr, and h1
# OUT: -
############################################################################
# }}}
# sub print_caption($caption, $package, $class) {{{
sub print_caption
{
    my ($caption, $package, $class) = @_;

    $caption =~ s/\$PACKAGE/$package/g;

    print "<div class=\"caption\">\n";
    print "<div style=\"float: left; padding-top: 1em; font-size: 110%\"><em>webconf</em><br />\n";
    print "<h1 style=\"margin-top: 0em;\">$caption</h1>\n";
    print "</div>\n";
    print "<div style=\"text-align: right; padding: 1em;\"><img src=\"../eisfair_logo_klein.gif\" /></div></div>\n";
}
# }}}

# print_foot description {{{
############################################################################
# sub print_foot()
############################################################################
# }}}
# sub print_foot() {{{
sub print_foot
{
    my ($class) = @_;

    print "</div>\n";
    print "<div class=\"footer\">\n";
    print "<hr style=\"display: none\">\n";
    print "webconf $version\n";
    print "</div>\n";
    print "</body>\n";
    print "</html>\n";
}
# }}}


# error handling
############################################################################ 


# printErrSimple description {{{
############################################################################
# sub printErrSimple($description)
# - prints the error message $description
# IN:  $description - error message
# OUT: -
############################################################################
# }}}
# sub printErrSimple($description) {{{
sub printErrSimple {
   my $desc = shift;
   
   $desc =~ s/</&lt;/g;
   $desc =~ s/>/&gt;/g;

   print "<br><pre style=\"background-color:#FF0000; color:#FFFFFF;";
   print "font-family:'lucidabright', 'Lucida Console', Lucida, Courier, monospace; font-size:110%;\">\n";
   print "<b>Error: $desc ";
   print "</b></pre><br>\n";
}
# }}}

# printErr description {{{
################################################################
# sub printErr($ff, $where, $expected, $found)
# - prints an errormessage
# IN:  $ff       - name of the formfile where the error occured
#      $where    - linenumber
#      $expected - message, what was expected
#      $found    - message, what has been found
# OUT: -
################################################################
# }}}
# sub printErr($ff, $where, $expected, $found) {{{
sub printErr {
   my ($ff, $where, $expected, $found) = @_;
   
   $expected =~ s/</&lt;/g;
   $expected =~ s/>/&gt;/g;
   $found =~ s/</&lt;/g;
   $found =~ s/>/&gt;/g;
   $found =~ s/^(')?[\s]+/$1/;
   
   print "<br><pre style=\"background-color:#FF0000; color:#FFFFFF;";
   print "font-family:'lucidabright', 'Lucida Console', Lucida, Courier, monospace; font-size:110%;\">\n";
   print "<b>Error in file '$ff' at line $where!\n";
   print "-&gt; expected $expected\n";
   print "-&gt; found $found\n";
   print "</b></pre><br>\n";
   
   return 1;
}
# }}}

1;
