#!/usr/bin/perl

use XML::Parser;

package CheatConfig;


sub new {
	my $class = shift;
	my $groupfile = shift;
	unless ( $groupfile && ( -f $groupfile ) ) {
		$groupfile = "admin/groups.xml";
	}
	unless ( -f $groupfile )  {
		$groupfile = $ENV{DOCUMENT_ROOT} . "/megacheats/admin/groups.xml";
	}

	if ( ! -f $groupfile ) {
		die "unable to find file $groupfile";
	}

	# Parse the groups file
	$groupparse = new XML::Parser(Handlers => { Start => \&groupstart },
				ProtocolEncoding => 'ISO-8859-1' );
	$groupparse->parsefile($groupfile);
	undef $groupparse;
	return bless {}, $class;
}

sub getData {
	my $self = shift;
	my $key = shift;
	return $groups{$key};
}

# The subroutine for group parsing
sub groupstart;


sub groupstart {
  my $object = shift;
  my $name = shift;
  push @{$groups{$name}}, { @_ };
}

# Make sure we're authorized.
sub authcheck {
  my $redirect = shift;
  unless ($cookies{AUTHORIZATION}) {
    print "Location: login.cgi?return=$redirect\n\n";
    exit;
  }
}

sub auth {
  my $level = shift;
  return 1 if ($cookies{AUTHORIZATION} eq $level);
  return 0;
}

sub urlencode {
  
  return map { s/ /+/g; s/([^+[:alnum:]])/%pack('C', $1)/g } @_ if (wantarray);
  my $woof = shift;
  $woof =~ s/([^[:alnum:]])/%pack('C', $1)/g;
  $woof =~ s/ /+/g;
  return $woof;
}

sub querystring {
  my $accrue;
  my $first = 1;
  foreach (keys %params) {
    if ($first) {
      $accrue .= "?";
    } else {
      $accrue .= "&";
    }
    $accrue .= "$_=";
    $accrue .= urlencode $params{$_};
  }
}

1;

