#!/usr/bin/perl --

###############################################################################
# AdminIndex.pl                                                               #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 1                                                 #
# Released:       December 27, 2004                                           #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

### Version Info ###
$indexplver = 'YaBB 2 RC 1 $Revision: 1.28 $';
$indexplver =~ s/\$Revision\: (.*?) \$/Build $1/ig;

use CGI::Carp qw(fatalsToBrowser);

# Used for bundled perl modules, if they are not installed
# Some servers do not like one or the other.
# make sure one has a # before it, and the other one doesn't
# Using both at the same time can cause trouble...

push(@INC, "./Modules");
#use lib "./Modules";

if( $ENV{'SERVER_SOFTWARE'} =~ /IIS/ ) {
	$yyIIS = 1;
	$0 =~ m~(.*)(\\|/)~;
	$yypath = $1;
	$yypath =~ s~\\~/~g;
	chdir($yypath);
	push(@INC,$yypath);
}

$adminscreen = "1";

### Requirements and Errors ###
require "Paths.pl";

if (eval "require Time::HiRes"){
	use Time::HiRes qw(time); #benchmarking
	$START_TIME = time;
}

require "Settings.pl";
require "$sourcedir/Subs.pl";
require "$sourcedir/System.pl";
require "$sourcedir/DateTime.pl";
require "$sourcedir/Load.pl";
require "$vardir/modsettings.txt";

if(!$ENV{'HTTP_USER_AGENT'}) {
	&spoofed;
} 

&LoadCookie;		# Load the user's cookie (or set to guest)
&LoadUserSettings;	# Load user settings
&WhatTemplate;		# Figure out which template to be using.
&WhatLanguage;		# Figure out which language file we should be using! :D

if($referersecurity){
	&referer_check; 	# check if the action is allowed from an external domain
}

if(! -e "$boardsdir/forum.totals") {
	&BoardTotals("convert");
}

require "$sourcedir/Security.pl";

$adminurl = "$boardurl/AdminIndex.$yyaext";

&banning;		# Check for banned people
&WriteLog;		# Write to the log

$action = $INFO{'action'};
$SIG{__WARN__} = sub { &admin_fatal_error( "@_" ); };
eval { &yymain; };
if ($@) { &admin_fatal_error("Untrapped Error:<br />$@"); }

sub yymain {
#### Choose what to do based on the form action ####
if ($maintenance == 1 && $action eq 'login2') { require "$admindir/LogInOut.pl"; &Login2; }
if ($maintenance == 1 && !$iamadmin ) { require "$admindir/Maintenance.pl"; &InMaintenance; }
### Guest can do the very few following actions.
	if($iamguest || !$iamadmin && !$iamgmod) {
		$yySetLocation = qq~$scripturl~;
		&redirectexit;
	}

	if ($iamgmod) {
	require "$vardir/gmodsettings.txt";
		if (!$allow_gmod_admin) {
			$yySetLocation = qq~$scripturl~;
			&redirectexit;
		}
	}

if ($action ne "") {
	require "$admindir/AdminSubList.pl";
	if ($director{$action}) {
		@act = split(/&/,$director{$action});
		$aa = $act[1];
		require "$admindir/$act[0]";
		&$aa;
	} else {
		require "$admindir/Admin.pl"; &Admin;
	}
} else {
	&TrackAdminLogins;
	require "$admindir/Admin.pl"; &Admin;
}

exit;
}

sub ParseNavArray {
	foreach $element (@_){
		
		chomp $element;
		($action_to_take, $vistext, $whatitdoes, $isheader) = split(/\|/, $element);

		if ($action_area eq "$action_to_take") {
			$currentclass = "class=\"current\"";
		} else {
			$currentclass = "";
		}

		if ($isheader) {
			$started_ul = 1;
			$leftmenu .= qq~		<h3><a href="javascript:toggleList('$isheader')" title="$whatitdoes">$vistext</a></h3>
		  <ul id="$isheader">
~;
			next;
		}

		if ($iamgmod && $gmod_access{"$action_to_take"} ne "on") {
			next;
		}

		$leftmenu .= qq~			<li><a href="$adminurl?action=$action_to_take" title="$whatitdoes" $currentclass>$vistext</a></li>~;
	}

	if ($started_ul) {
		$leftmenu .= qq~
		  </ul>
~;
	}
}

sub AdminTemplate {
	$template = ${$uid.$username}{'template'};
	if(!-d "$adminstylesdir/$template" || $template eq "") { $template = "default"; }

	$adminstyle = qq~<link rel="stylesheet" href="$adminstylesurl/$template.css" type="text/css" />~;
	$adminstyle =~ s~$template\/~~g;

	$adminimages = qq~$adminstylesurl/$template~;
	$adminimages =~ s~$template\/~~g;
	require "$templatesdir/$template.AdminCentre.template";
	require "$vardir/gmodsettings.txt";


@forum_settings = (
"|$admintxt{'7'}|section of main settings - clicking will collapse|a1",
"modsettings|$admintxt{'8'}|edits settings|",
"modulesettings|$admintxt{'9'}|edits mods settings|",
"smilies|$asmtxt{'01'}|blah|",
"helpadmin|Help Centre Configuration||",
"gmodaccess|Global Moderator Access||",
"setcensor|$admintxt{'10'}||",
"detailedversion|$admintxt{'11'}||",
);

@forum_controls = (
"|Forum Controls||a2",
"managecats|Edit Categories||",
"manageboards|Edit Boards||",
"editnews|Edit YaBB News||",
"modagreement|Edit Registration Agreement||",
"referer_control|Referer Security||",
"setup_guardian|The Guardian&trade;||",
);

@forum_layout = (
"|Forum Layout||a3",
"modskin|Template Configuration||",
"modcss|Edit Style Sheet||",
"modtemp|Edit Template||",
);

@member_controls = (
"|Member Controls||a4",
"addmember|$admintxt{'17a'}||",
"viewmembers|$admintxt{'17'}||",
"modmemgr|Edit Member Groups||",
"mailing|Email Your Members||",
"ipban|Ban Members||",
"setreserve|Set Reserved Names||",
);

@maintence_controls = (
"|Maintenance Controls||a5",
"clean_log|Clean Users Online Log||",
"boardrecount|Recount Board Totals||",
"membershiprecount|Recount Membership||",
"rebuildmemlist|Rebuild Members List||",
"deleteoldthreads|Remove Old Threads||",
"manageattachments|Attachment Functions||",
);

@forum_stats = (
"|Forum Stats||a6",
"stats|Forum Statistics||",
"showclicks|View Forum Log||",
"errorlog|View Errors||",
"view_reglog|View Registration Log||",
);

	&ParseNavArray(@forum_settings);
	&ParseNavArray(@forum_controls);
	&ParseNavArray(@forum_layout);
	&ParseNavArray(@member_controls);
	&ParseNavArray(@maintence_controls);
	&ParseNavArray(@forum_stats);

	$topmenu_one = qq~<span style="font-size: 12px; font-family: tahoma, sans-serif;"><a href="$boardurl/YaBB.$yyext">Return to your forum</a></span>~;
	$topmenu_two = qq~<span style="font-size: 12px; font-family: tahoma, sans-serif;"><a href="$adminurl">Return to the Admin Center</a></span>~;
	$topmenu_tree = qq~<span style="font-size: 12px; font-family: tahoma, sans-serif;"><a href="$scripturl?action=help">View help files</a></span>~;
	$topmenu_four = qq~<span style="font-size: 12px; font-family: tahoma, sans-serif;"><a href="http://www.yabbforum.com">Visit YaBBforum.com</a></span>~;

	print qq~Content-Type: text/html\n\n~;
	$header =~ s/<yabb style>/$adminstyle/g;
	print qq~$header~;
	$leftmenutop =~ s/<yabb images>/$adminimages/g;
	$topnav =~ s/<yabb topmenu_one>/$topmenu_one/g;
	$topnav =~ s/<yabb topmenu_two>/$topmenu_two/;
	$topnav =~ s/<yabb topmenu_tree>/$topmenu_tree/;
	$topnav =~ s/<yabb topmenu_four>/$topmenu_four/;
	print qq~$leftmenutop~;
	print qq~$leftmenu~;
	print qq~$leftmenubottom~;
	print qq~$topnav~;
	&AdminDebug;
	$mainbody =~ s/<yabb main>/$yymain/g;
	$mainbody =~ s/<yabb_admin debug>/$yydebug/g;
	print qq~$mainbody~;

exit;
}

sub TrackAdminLogins {
	fopen(ADMINLOG, "$vardir/adminlog.txt") || &admin_fatal_error("213 $admintxt{'106'}: $admintxt{'23'} $vardir/adminlog.txt");
	my @adminlog = <ADMINLOG>;
	fclose (ADMINLOG);
		$a = 0;
		for ($i = 0; ($i < 10);  $i++) {
			if ($adminlog[$i] =~ /^----------/) {
				$a++;
				if ($i < 5) {
					$adminlog[$i] = qq~$username|$user_ip|$date\n~;
					$adminlog[$i+1] = qq~----------\n~;
					last;
				} else {
					$adminlog[$i] = qq~~;
					$adminlog[0] = qq~$username|$user_ip|$date\n----------\n~;
					last;
				}
			}
		}
		if ($a == 0) {
			$adminlog[0] = qq~$username|$user_ip|$date\n----------\n~;
		}

		fopen(ADMINLOG, ">$vardir/adminlog.txt") || &admin_fatal_error("213 $admintxt{'106'}: $admintxt{'23'} $vardir/adminlog.txt");
		foreach (@adminlog) {
			print ADMINLOG qq~$_~;
		}
		fclose(ADMINLOG);
}


sub AdminDebug {
	if ($debug == 1){
		if (eval "require Time::HiRes"){ 
			$time_running = time - $START_TIME;
			if($time_running > 1000){
				$yytimeclock = "Your server probably does not have Time::Hires installed or does not support the local module for it";
			} else {
				$time_running = sprintf("%.4f",$time_running);
				$yytimeclock = "Page completed in $time_running seconds";
			}
		}
		$yyfileactions = "Opened $file_open files and Closed $file_close files. (should be equal numbers)";
		$openfiles =~ s~\+\<~~g;
		$yyfilenames = $openfiles;	
		$yydebug = qq~<span class="small"><br /><u>Debugging Information</u><br /><br /><u>benchmarking</u><br />$yytimeclock<br /><br /><u>Your IP address is</u><br />$user_ip<br /><br /><u>Your Browser Agent:</u><br />$ENV{'HTTP_USER_AGENT'}<br /><br /><u>File Check on Open/Close</u><br />$yyfileactions<br /><br /><u>Filehandle/Files Opened:</u><br />$openfiles</span>~;
	}
}

1;