#!/usr/bin/perl
# ==================================================================
# Gossamer Forum -
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info :                          
#   Revision : $Id: gforum.cgi,v 1.52.2.5 2003/10/10 20:30:01 jagerman Exp $
# 
# Copyright (c) 2003 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================
use strict;
use lib '/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin';
use GForum qw/:forum :user $DB $IN $CFG $USER $GUEST %HIDDEN $TEMPLATE_SET/;
use GForum::Template;
use GForum::Authenticate;

GForum::init('/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin');

main();

sub main {
# -------------------------------------------------------------------
# Display whatever page the user has requested
#
    local $SIG{__DIE__} = \&GForum::fatal;

    # Show the disabled page if the forum has been disabled
    if ($CFG->{disabled} == 1) {
        print $IN->header;
        my $message = $CFG->{disabled_message};
        $message =~ s/\n/<br>\n/g;
        return GForum::Template->parse_print("disabled.html" => { message => \$message });
    }
    {
        # If the user is banned, simply deny them access
        my @expanded_bans = @{$CFG->{bans}};
        for (@expanded_bans) {
            if (/^(\d+\.\d+\.\d+\.)(\d+)-(\d+)$/ and $2 < $3) {
                # Allows you to specify '123.45.67.89-123' to ban that range of IP's
                for ($2 .. $3) {
                    push @expanded_bans, "$1$_";
                }
                next;
            }
            # Turn a ban into a regexp
            my $ban = quotemeta($_);
            # *'s match anything
            $ban =~ s/\\\*/.*/g;
            # ?'s match any single character
            $ban =~ s/\\\?/./g;
            if ($ENV{REMOTE_HOST} and $ENV{REMOTE_HOST} =~ /^$ban$/i or $ENV{REMOTE_ADDR} =~ /^$ban$/i) {
                print $IN->header;
                return GForum::Template->parse_print($CFG->{functions}->{banned}->{page}, { error => GForum::language('USER_BANNED') });
            }
        }
    }
    GForum::authenticate() or return; # False = stop!

    if ($CFG->{disabled} == 2 and (not $USER or $USER->{user_status} != ADMINISTRATOR)) {
        print $IN->header;
        my $message = $CFG->{disabled_message};
        $message =~ s/\n/<br>\n/g;
        return GForum::Template->parse_print("disabled.html" => { message => \$message });
    }

    my $template_set = $IN->param('t');
    if (not $template_set or $template_set !~ /^[\w-]+$/ or not -d "$CFG->{admin_root_path}/templates/$template_set" or $template_set =~ /^(?:help|admin|fileman|CVS)$/) {
        $template_set = '';
    }
    else { # It's good!
        $HIDDEN{t} = $template_set;
    }

    if ($USER) {
        if ($USER->{user_show_racetags}) {
            $DB->table('User')->update({ user_show_racetags => 0 }, { user_id => $USER->{user_id} });
            print $IN->header . qq~Hidden <a href="#" onclick="return status_toggle()">(click to unhide)</a>~;
        }
        else {
            $DB->table('User')->update({ user_show_racetags => 1 }, { user_id => $USER->{user_id} });
            print $IN->header . qq~Viewable <a href="#" onclick="return status_toggle()">(click to hide)</a>~;
        }
    }
}

1;
