#!/usr/bin/perl
# The shebang is only there for mcedit syntax highlights, as I'm too lazy to 
# change the configfile. It won't hurt performance
use URI::Escape;
use strict;
use vars qw(%header);

sub HiddenFields($@){
    my $hash = shift;
    my %saves;
    $saves{@_} = ();
    for (keys %$hash){
	print qq{<input type=hidden name="$_" value="$hash->{$_}">}
	    unless exists $saves{$_};
    }
}

sub Entity(@){
    my $ref;
    my @copy;    
    if (defined wantarray){
	@copy = @_;
	$ref = \@copy;
    }else{
	$ref = \@_;
    }
    for (@$ref){
	eval {
	    s/&/&amp;/g;
	    s/\"/&quot;/g;
	    s/</&lt;/g;
	    s/>/&gt;/g;
	    s/\n/<br>\n/g;
	    s/\t/    /eg;
	    s/  /&nbsp;&nbsp;/g;
	};
	if ($@){ return defined wantarray ? @_ : undef }
    }
    return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
}

# Browsers do s/ /+/ - I don't care about RFC's, but I do care about real-life
# situations.
sub DecodeURI(@){
    my @r;    
    for (@_){
	s/\+/%20/g;
	my $dec = uri_unescape($_);
	if (defined wantarray){
	    push @r, $dec;
	}else{
	    eval {$_ = $dec}; 
	    return undef if $@; # ;DecodeURI("foo");
	}
    }
    return defined wantarray ? (wantarray ? @r : "@r") : undef;
}
sub EncodeURI(@){
    my @r;
    for (@_){
        my $esc = uri_escape($_, '^;\/?:@&=\$,A-Za-z0-9\-_.!~*\'()');
        if (defined wantarray){
            push @r, $esc;
        }else{
	    eval {$_ = $esc};
	    return undef if $@; # ;EncodeURI("foo");
	}
    }
    return defined wantarray ? (wantarray ? @r : "@r") : undef;
}

sub AddCookie($){
    if ($header{'set-cookie'}){
	$header{'set-cookie'} .= "\nset-cookie: $_[0]";
    }else{
	$header{'set-cookie'} = $_[0];
    }
}

sub ReadFile($){
    my $o = $/; undef $/;    
    open (READFILE, $_[0]);
    my $r = <READFILE>;
    close READFILE;
    $/ = $o;
    return $r;
}

sub WriteFile($$){
    open (WRITEFILE, ">$_[0]");
    flock WRITEFILE, 2;
    print WRITEFILE $_[1];
    close WRITEFILE;
}

sub Counter($){
    my $o = $/; undef $/;
    open           COUNTER, "+<$_[0]";
    flock          COUNTER, 2;
    seek           COUNTER, 0, 0;
    my $counter = <COUNTER>;
    seek           COUNTER, 0, 0;
    truncate       COUNTER, 0;
    print          COUNTER ++$counter;
    close          COUNTER;
    $/ = $o;
    return $counter;
}

sub AutoURL($){
    my $ref;    
    if (defined wantarray){
	$ref = \(my $copy = $_[0]);
    }else{
	$ref = \$_[0];
    }
    eval {
	my ($p, $b, $c);
	$$ref =~ s/&quot;/"\cC"/g;
	$$ref =~ s/&gt;/>\cC>/g;
	$$ref =~ s/&lt;/<\cC</g;
	# Now this is a big, ugly regex! But hey - it works :)    
	$$ref =~ s{((\w+://|www\.|WWW\.)[a-zA-Z0-9\.\@:-]+[^\"\'>< \r\t\n]*)}{
	    local $_ = $1, $p = $2, ((($b) = /([\.,!\?\(\)\[\]]+$)/) ? s/// :
	    undef), s/&(?!\x23?\w+;)/&amp;/g, s/\"/&quot;/g, $c = 
	    ($p eq 'www.' || $p eq 'WWW.' ? "http://$_" : $_),
	    qq{<a href="$c" target="_blank">$_</a>$b}
	}eg;


	$$ref =~ s/"\cC"/&quot;/g;
	$$ref =~ s/>\cC>/&gt;/g;
	$$ref =~ s/<\cC</&lt;/g;
    };
    if ($@){ return defined wantarray ? @_ : undef }
    return defined wantarray ? $$ref : undef;
}
1;