# This module contains our utility functions
package ZeddUtil;

use ZeddWeb;

# ------------- Package Definition / Export -------------------
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);

use Exporter;
$VERSION = 1.00;              # Or higher
@ISA = qw(Exporter);

# Symbols to autoexport (:DEFAULT tag)
@EXPORT      = qw( absolutify_urls checkHtml isGoodTag getHtmlRegion
		   $legaltags_g $legalMustMatchTags_g );

%EXPORT_TAGS = ( );

# Symbols to export on request
@EXPORT_OK   = qw( absolutify_urls checkHtml isGoodTag getHtmlRegion
                   $legaltags_g $legalMustMatchTags_g );

use vars qw( $legaltags_g $legalMustMatchTags_g );

# ------------- Package Definition / Export -------------------

$legaltags_g = {
                 'B' => 1,
                 'I' => 1,
                 'P' => 1,
                 'A' => 1,
                 'LI' => 1,
                 'OL' => 1,
                 'UL' => 1,
                 'EM' => 1,
                 'BR' => 1,
                 'TT' => 1,
                 'STRONG' => 1,
		 'BLOCKQUOTE' => 1,
                 'DIV' => 1,
             };

$legalMustMatchTags_g  = { 'B' => 1,
                           'I' => 1,
                           'P' => 1,
                           'A' => 1,
                           'LI' => 1,
                           'OL' => 1,
                           'UL' => 1,
                           'EM' => 1,
                           'TT' => 1,
                           'STRONG' => 1,
                           'BLOCKQUOTE' => 1,
                           'DIV' => 1
			   };

sub absolutify_urls
{
    my($data) = shift;
    my($target) = shift;

    $data =~ s/
             # First look for an a, img, or area, followed later by an
             # href or src
             (<\s*(?:a|img|area)\b[^>]*(?:href|src)\s*=\s*
             # Then an optional quote
             ['"]?)
             # Then the interesting part
             ([^(http|ftp|telnet|gopher|archie)'"> ]+)
             # Then another optional quote
             (['"]?
             # And the left-overs
             [^>]*>)
             /
             # Then construct the new link from the prefix and suffix
             $1.sprintf("%s%s",$target,$2).$3
		 /segix;

    return $data;
}

sub checkHtml
{
    my( $html ) = shift;
    my( $str );
    my( $tagOpen );
    my( $tagClose );
    my( $re );
    my( $key );

    # Make some permanent changes to the returned html ...
    $html =~ s/<(?!.*?>)//gs;
    $html =~ s/<(.*?)>/isGoodTag($1)/sge;

    # Condition the input html before running it through the tag
    # matching below, in this case get rid of anything inside the
    # tags that isn't needed. These changes are *not* permanent so
    # use our copy of the input html.

    $str = $html;
    $str =~ s/<\s*?(\w+)(.*?)>/<$1>/gsi;
    $str =~ s/<\s*?\/(\w+)(.*?)>/<\/$1>/gsi;

    foreach $key (keys %$legalMustMatchTags_g)
    {
        $tagOpen = "<$key>";
        $tagClose = "</$key>";

        $re = quotemeta($str);
        $re =~ s/\\<$key\\>/$tagOpen(/gis;
				     $re =~ s/\\\<\\\/$key\\\>/)$tagClose/gis;

        my @a = eval { $str =~ $re };

        if( $@ )
        {
            return "Error: Mismatched Tags";
        }
    }

    return $html;
}

sub isGoodTag
{
    my( $tag ) = shift;
    my( $closeTag );
    my( $tagType ) = 0;

    $tag =~ s/^\s*?(.*)\s*?$/$1/;

    # Take care of Links
    if ($tag =~ /href=(.*)/i)
    {
        my $url = $1;
        $url =~ s/[\" ]//g;
        $url =~ s|^\s*\w+script\b.*$||i;
        return qq!<A HREF="$url">!;
    }

    # Validate tags
    if( $tag =~ /\/(.*)/ )
    {
        $tag = $1;
        $tagType = 1;
    }
    $tag = uc( $tag );

    if( $$legaltags_g{"$tag"} == 1 )
    {
        if( $tagType == 0 )
        {
            return "<$tag>";
        }
        else
        {
            return "</$tag>";
        }
    }
}

sub getHtmlRegion
{
    my($html) = shift;
    my($regex) = shift;
    my($returnValue) = "";

    ZeddWeb::logMsg( "======== Enter getHtmlRegion ========" );

    if( $html =~ s/($regex)//s )
    {
        $returnValue = $1;
    }

    ZeddWeb::logMsg( "======== Exit getHtmlRegion ========" );

    return $returnValue;
}


1;
