###############################################################################
# HelpCentre.pl                                                               #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 3                                                 #
# Released:       June 20, 2005                                               #
# 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.         #
###############################################################################

$helpcentreplver = 'YaBB 2 RC 3 $Revision: 1.60 $';
if($action eq 'detailedversion') { return 1; }

LoadLanguage("HelpCentre");

$yytitle = "$helptxt{'1'}";

sub SectionDecide {

## This bit decides what section we are in and sets the background accordingly
## Also sets the variables are used to open up the correct Help Directory
$moderator_class = "catbg";
$admin_class = "catbg";
$global_mod_class = "catbg";
$UserClass = "catbg";

if(-e("$vardir/HelpSettings.txt")) { require "$vardir/HelpSettings.txt"; }
else { $UseHelp_Perms = 0; }

if($UseHelp_Perms) {
$ismod = 0;
unless (exists $memberinfo{$username}) { &LoadUser($username);&LoadMiniUser($username); }
foreach $catid (@categoryorder){
	if($ismod) { last; }
	$boardlist = $cat{$catid};
	(@bdlist) = split(/\,/, $boardlist);
	foreach $curboard (@bdlist) {
		if($ismod) { last; }
		$bdmods = ${$uid.$curboard}{'mods'};
		$bdmods =~ s/ //g;
		foreach $curuser (split(/\,/, $bdmods)) {
			if($curuser eq $username) { $ismod = 1; last; }
		}
		$bdmodsgrp = ${$uid.$curboard}{'modgroups'};
		$bdmodsgrp =~ s/ //g;
		foreach $curusergrp (split(/\,/, $bdmodsgrp)) {
			if($curusergrp eq ${$uid.$username}{'position'}) { $ismod = 1; last; }
		}
	}
}
}

if($INFO{'section'} eq "admin") {
if($UseHelp_Perms && !$iamadmin) { &fatal_error("$maintxt{'1'}"); }
${$INFO{'section'}._class} = "titlebg";
$help_area = "Admin";
	} elsif($INFO{'section'} eq "moderator") {
if($UseHelp_Perms && !$ismod && !$iamgmod && !$iamadmin) { &fatal_error("$maintxt{'1'}"); }
${$INFO{'section'}._class} = "titlebg";
$help_area = "Moderator";
	} elsif($INFO{'section'} eq "global_mod") {
if($UseHelp_Perms && !$iamgmod && !$iamadmin) { &fatal_error("$maintxt{'1'}"); }
${$INFO{'section'}._class} = "titlebg";
$help_area = "Gmod";
	} else {
$UserClass = "titlebg";
$help_area = "User";
	}

}

sub SectionPrint {
## Prints the navigation bar for the help section
$userhlp = qq~<a href="$scripturl?action=help">$helptxt{'3'}</a>~;
if($UseHelp_Perms) {
	if(!$ismod && !$iamgmod && !$iamadmin) { return }
	if($ismod || $iamgmod || $iamadmin) {
		$modhlp = qq~<a href="$scripturl?action=help;section=moderator">$helptxt{'4'}</a>~;
	} else { $modhlp = "&nbsp;"; }
	if($iamgmod || $iamadmin) {
		$gmodhlp = qq~<a href="$scripturl?action=help;section=global_mod">$helptxt{'5'}</a>~;
	} else { $gmodhlp = "&nbsp;"; }
	if($iamadmin) {
		$adminhlp = qq~<a href="$scripturl?action=help;section=admin">$helptxt{'6'}</a>~;
	} else { $adminhlp = "&nbsp;"; }
}
else {
	$modhlp = qq~<a href="$scripturl?action=help;section=moderator">$helptxt{'4'}</a>~;
	$gmodhlp = qq~<a href="$scripturl?action=help;section=global_mod">$helptxt{'5'}</a>~;
	$adminhlp = qq~<a href="$scripturl?action=help;section=admin">$helptxt{'6'}</a>~;
}

$HelpNavBar =~ s/<user menu>/$userhlp/g;
$HelpNavBar =~ s/<moderator menu>/$modhlp/g;
$HelpNavBar =~ s/<global mod menu>/$gmodhlp/g;
$HelpNavBar =~ s/<admin menu>/$adminhlp/g;
$HelpNavBar =~ s/<user class>/$UserClass/g;
$HelpNavBar =~ s/<moderator class>/$moderator_class/g;
$HelpNavBar =~ s/<global mod class>/$global_mod_class/g;
$HelpNavBar =~ s/<admin class>/$admin_class/g;
$yymain .= $HelpNavBar;

}

sub GetHelpFiles {

unless ($HelpTemplateLoaded) {
	if(-e ("$templatesdir/$usestyle/HelpCentre.template")) {
		require "$templatesdir/$usestyle/HelpCentre.template";
	}
	else {
		require "$templatesdir/default/HelpCentre.template";
	}
}

&SectionDecide;

# This determines if the order file is present and if it isn't
# It creates a new one, in default alphabetical order

if(!-e ("$helpfile/$help_area.helporder")) {
	&CreateOrderFile;
}

fopen(HELPORDER, "$helpfile/$help_area.helporder"); 
@helporderlist = <HELPORDER>; 
fclose(HELPORDER);

foreach $line (@helporderlist){
	chomp $line;
	if(-e ("$helpfile/$language/$help_area/$line.help")) {
		require "$helpfile/$language/$help_area/$line.help";
	}
	elsif(-e ("$helpfile/English/$help_area/$line.help")) {
		require "$helpfile/English/$help_area/$line.help";
	}
	else {
		next;
	}

	&MainHelp;
	&DoContents;
}

&SectionPrint;
&ContentContainer;

&template;
}

sub MainHelp {

$TempParse = $BodyHeader;
$TempParse =~ s/<yabb section_anchor>/$SectionName/g;
$SectionNam = $SectionName;
$SectionNam =~ s/_/ /g;
$TempParse =~ s/<yabb section_name>/$SectionNam/g;
$Body .= qq~$TempParse~;

$i = 1;
while (${SectionSub.$i}) {

if(${SectionExcl.$i} eq "yabbc" && (!$enable_ubbc || !$showyabbcbutt)) { $i++; next; }

$TempParse = $BodySubHeader;
$SectionAnchor = ${SectionSub.$i};
$SectionSub = ${SectionSub.$i};
$SectionSub =~ s/_/ /g;
$TempParse =~ s/<yabb section_anchor>/$SectionAnchor/g;
$TempParse =~ s/<yabb section_sub>/$SectionSub/g;
$Body .= qq~$TempParse~;

$message = ${SectionBody.$i};
&FromHTML($message);
$message =~ s~&#64;~@~g;

&wrap;
$message =~ s~\[helpcode\]~ \[helpcode\]~ig;
$message =~ s~\[/helpcode\]~ \[/helpcode\]~ig;
$message =~ s~\[helpcode\]\n*(.+?)\n*\[/helpcode\]~&codehlp($1)~eisg;
$displayname = ${$uid.$username}{'realname'};
if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC;
$message =~ s~\[yabbimg\](\S+?\.\S{3,4})\[/yabbimg\]~<img src="$imagesdir/$1" border="0" alt="" />~ig;
if ($parseflash == 1){
	$message =~ s~\[yabbflash\=(\S+?),(\S+?)](\S+?)\[\/yabbflash\]~<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=$1 height=$2><param name=movie value=$imagesdir\/$3><param name=play value=true><param name=loop value=true><param name=quality value=high><embed src=$imagesdir\/$3 width=$1 height=$2 play=true loop=true quality=high></embed></object>~g;
} else {
	$message =~ s~\[yabbflash\=(\S+?),(\S+?)](\S+?)\[\/yabbflash\]~\[yabbflash\=$1,$2\]$imagesdir\/$3\[\/yabbflash\]~;
	$message =~ s~\[yabbflash\=(\S+?),(\S+?)](\S+?)\[\/yabbflash\]~<b>$helptxt{'769'} ($1 x $2):</b> <a href="$3" target="_blank" onClick="window.open('$3', 'flash', 'resizable,width=$1,height=$2'); return false;">http://www.your-domain.com/yabbchelp.swf</a>~;
}

&wrap2;

$TempParse = $BodyItem;
$TempParse =~ s/<yabb item>/$message/g;
$Body .= qq~$TempParse~;
$i++;
}
$Body .= qq~$BodyFooter~;
}

{
	my %hpkillhash = (
	';' => '&#059;',
	'!' => '&#33;',
	'(' => '&#40;',
	')' => '&#41;',
	'-' => '&#45;',
	'.' => '&#46;',
	'/' => '&#47;',
	':' => '&#58;',
	'?' => '&#63;',
	'[' => '&#91;',
	'\\' => '&#92;',
	']' => '&#93;',
	'^' => '&#94;'
	);

sub codehlp {
	my $hcode = $_[0];
	if($hcode !~ /&\S*;/) { $hcode =~ s/;/&#059;/g; }
	$hcode =~ s~([\(\)\-\:\\\/\?\!\]\[\.\^])~$hpkillhash{$1}~g;
	$hcode =~ s~(&#91\;.+?&#93\;)~<span style="color: #ff0000;">$1</span>~isg;
	$hcode =~ s~(&#91\;&#47\;.+?&#93\;)~<span style="color: #ff0000;">$1</span>~isg;
	return $hcode;
}
}

sub ContentContainer {

$MainLayout =~ s/<yabb contents>/$Contents/g;
$MainLayout =~ s/<yabb body>/$Body/g;

$yymain .= qq~$MainLayout~;

}

sub DoContents {
$TempParse = $ContentHeader;

$TempParse =~ s/<yabb section_anchor>/$SectionName/g;
$SectionNam = $SectionName;
$SectionNam =~ s/_/ /g;
$TempParse =~ s/<yabb section_name>/$SectionNam/g;
$Contents .= qq~$TempParse~;

$Contents .= qq~<ul style="list-style: none; margin: 0; padding: 2px; border: none;">~;
$i = 1;
while (${SectionSub.$i}) {

if(${SectionExcl.$i} eq "yabbc" && (!$enable_ubbc || !$showyabbcbutt)) { $i++; next; }

$SectionAnchor = ${SectionSub.$i};
${SectionSub.$i} =~ s/_/ /g;

$TempParse = $ContentItem;
$TempParse =~ s/<yabb anchor>/$SectionAnchor/g;
$TempParse =~ s/<yabb content>/${SectionSub.$i}/g;

$Contents .= qq~$TempParse~;
${SectionSub.$i} = "";
$i++;
}
$Contents .= qq~</ul>~;
}

sub CreateOrderFile {
	opendir(HELPDIR, "$helpfile/$language/$help_area");
		@contents = readdir(HELPDIR);
	closedir(HELPDIR);

	foreach $line (sort {uc($a) cmp uc($b)} @contents){
	($name, $extension) = split (/\./, $line);
	if ($extension !~ /help/i) { next; }

	$order_list .= "$name\n";

	}

	fopen(HELPORDER, ">$helpfile/$help_area.helporder") || die("couldn't write order file - check permissions on $helpfile");
	print HELPORDER qq~$order_list~;
	fclose(HELPORDER);

}


1;