<?php
// ++=========================================================================++
// || vBadvanced CMPS v1.0.1
// || © 2003-2004 vBadvanced.com & PlurPlanet, LLC - All Rights Reserved
// || This file may not be redistributed in whole or significant part.
// || http://vbadvanced.com
// || Downloaded 08:55, Fri Jan 28th 2005 - 5149
// || 
// ++ ========================================================================++

error_reporting(E_ALL & ~E_NOTICE);

// ################## Adv No Permissons Page #######################
if (!function_exists('print_adv_no_permission'))
{
	function print_adv_no_permission()
	{
		global $header, $footer, $headinclude, $vboptions, $vbphrase, $bbuserinfo, $stylevar, $session;
	
		if ($bbuserinfo['userid'])
		{
			eval('$errormessage = "' . fetch_phrase('nopermission_loggedin', PHRASETYPEID_ERROR) . '";');
		}
		else
		{
			$logincode = construct_login_code();
			$scriptpath = htmlspecialchars_uni(SCRIPTPATH);
			$show['permission_error'] = true;
		}
		eval('$content = "' . fetch_template('STANDARD_ERROR') . '";');
	
		$content = str_replace(
			array(
				'"register.php',
				'"login.php'
			),
			array(
				'"' . $vboptions['bburl'] . '/register.php',
				'"' . $vboptions['bburl'] . '/login.php'
			),
			$content
		);
	
		eval(print_output($content));
		exit;
	}
}

// Deny Access if no permissions
if (!$allowview)
{
	print_adv_no_permission();
}

// ######################## Construct Replacement Array #######################
if (!function_exists('construct_replacement_array'))
{
	function construct_replacement_array($optreplace, $replacearray)
	{
		global $vboptions;
		if (!empty($optreplace))
		{
			foreach ($optreplace AS $filename)
			{
				if (trim($filename))
				{
					$replacearray['find'][] = '"' . $filename;
					$replacearray['replace'][] = '"' . $vboptions['bburl'] . '/' . $filename;
				}
			}
		}
		return array_merge($optreplace, $replacearray);
	}
}

// ######################### Function to Print The Page Out ##########################
function print_portal_output(&$cmpsoutput, $nonindex = false)
{
	global $pages, $stylevar, $headinclude, $header, $footer, $vboptions, $vbphrase, $bbuserinfo, $stylevar, $show, $pmbox, $vba_options, $navbar, $cusid, $_REQUEST, $gobutton, $pagetitle;

	if ($vba_options['portal_outputglobals'])
	{
		foreach (explode("\r\n", $vba_options['portal_outputglobals']) AS $glob)
		{
			global ${$glob};
		}
	}

	if (!$pagetitle AND $pages['name'] != 'home')
	{
		$pagetitle = $pages['title'];
	}

	if ($nonindex)
	{
		$bodytag = strpos($nonindex, '<body');

		if ($bodytag)
		{
			$bodycut = strpos($nonindex, '>', $bodytag);

			$nonindex = substr($nonindex, $bodycut + 1);

			$nonindex = str_replace(
				array(
					$header,
					$navbar,
					$footer,
					$headinclude,
					'</body>',
					'</html>',
				),
				'',
				$nonindex
			);

		}
		
		$cmpsoutput["$cusid"]['content'] = $nonindex;
	}

	if (!empty($cmpsoutput))
	{
		function order_mods($a, $b) 
		{ 
			return ($a['displayorder'] < $b['displayorder']) ? -1 : 1;
		} 

		usort($cmpsoutput, 'order_mods'); 
		
		$show['right_column'] = false;
		$show['left_column'] = false;
		$show['center_column'] = false;
		
		// Sort out the sides.
		foreach ($cmpsoutput AS $key => $blocks)
		{
			switch ($blocks['column'])
			{
				case 1:
					$home['centerblocks'] .= $blocks['content'];
					$show['center_column'] = true;
					break;
				case 0:
					$home['leftblocks'] .= $blocks['content'];
					$show['left_column'] = true;
					break;
				case 2:
					$home['rightblocks'] .= $blocks['content'];
					$show['right_column'] = true;
					break;
			}
		}
	}

	if ($vba_options['portal_shownavbar'] AND empty($navbar))
	{
		if ($pages['name'] != 'home')
		{
			$navbits[''] = iif($pagetitle, $pagetitle, $pages['title']);
		}
		if ($navbits)
		{
			$navbits = construct_navbits($navbits);
		}
		eval('$navbar = "' . fetch_template('navbar') . '";');
	}

	eval('print_output("' . fetch_template('adv_portal') . '");');
}

$iconcache = unserialize($datastore['iconcache']);
$attachmentcache = unserialize($datastore['attachmentcache']);

include_once('./includes/functions_bbcodeparse.php');
include_once('./includes/functions_forumdisplay.php');

// Get Advanced Page Options
if ($pages['advanced'])
{
	foreach (unserialize($pages['advanced']) AS $varname => $value)
	{
		$vba_options[$varname] = $value;
	}
}

if ($foruminfo == 0)
{
	unset($foruminfo);
}

// ######################### Forum Permissions #########################
$fperms = array();
foreach ($forumcache AS $finfo)
{
	$forumperms = fetch_permissions($finfo['forumid']);
	
	if ((!($forumperms & CANVIEW) OR !($forumperms & CANVIEWOTHERS)) AND $finfo['forumid'])
	{	
		$fperms[] = $finfo['forumid'];
	}
}

$forumperms = array_merge($fperms, iif($vba_options['portal_threads_exclude'], explode(',', $vba_options['portal_threads_exclude']) ,array()));
//echo sizeof($fperms);
if (!empty($forumperms))
{
	$iforumperms = 'AND thread.forumid NOT IN(' . implode(',', $forumperms) .')';
}

unset($forum, $fperms, $excludedforums);

// No Deleted Threads
$notdeleted = 'AND deletionlog.primaryid IS NULL';
$deljoin = 'LEFT JOIN ' . TABLE_PREFIX . 'deletionlog AS deletionlog ON (thread.threadid = deletionlog.primaryid AND type = \'thread\')';

// Process Active Modules
if (is_array($modules))
{
	foreach ($modules AS $mods)
	{
		if (in_array($mods['modid'], explode(',', $pages['modules'])))
		{
			$home["$mods[modid]"]['displayorder'] = $mods['displayorder'];
			$home["$mods[modid]"]['column'] = $mods['modcol'];
			$getbgrow = exec_switch_bg();
	
			if ($mods['inctype'] == 0)
			{
				require_once('./modules/' . $mods['filename']);
			}
			else
			{
				if ($mods['identifier'] == 'custompage')
				{
					$cusid = $mods['modid'];
					if ($pages['template'])
					{
						eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template($pages['template']) . '";');
					}
				}
				else
				{
					eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_' . $mods['filename']) . '";');
				}
			}
		}
	}
}

unset($modules, $forumperms, $mods, $notdeleted, $deljoin);

$headinclude = str_replace(
	array(
		'"clientscript',
		'url(images/',
		'name="generator" content="'
	),
	array(
		'"' . $vboptions['bburl'] . '/clientscript',
		'url(' . $vboptions['bburl'] . '/images/',
		'name="generator" content="vBadvanced, '
	),
	$headinclude
);

// $navbar replacement
if (THIS_SCRIPT == 'adv_index')
{
	$navreplacearray = explode("\r\n", $vba_options['global_navbar_replace']);
	
	$navreplace['find'] = array(
		'"clientscript',
		'\'misc.php',
		'>$vboptions[bbtitle]',
		'"$vboptions[forumhome].php'
	);
	
	$navreplace['replace'] = array(
		'"' . $vboptions['bburl'] . '/clientscript',
		'\'' . $vboptions['bburl'] . '/misc.php',
		'>$vboptions[hometitle]',
		'"$vboptions[homeurl]'
	);	
	
	$navreplace = construct_replacement_array($navreplacearray, $navreplace);
	
	$templatecache['navbar'] = str_replace($navreplace['find'], $navreplace['replace'], $templatecache['navbar']);
}

// $footer replacement
$footerreplacearray = explode("\r\n", $vba_options['global_footer_replace']);

$footerreplace['find'] = array(
	'"' . $admincpdir . '',
	'"' . $modcpdir . '',
);

$footerreplace['replace'] = array(
	'"' . $vboptions['bburl'] . '/' . $admincpdir . '',
	'"' . $vboptions['bburl'] . '/' . $modcpdir . '',
);

$footerreplace = construct_replacement_array($footerreplacearray, $footerreplace);

eval('$footer = "' . fetch_template('adv_portal_footer') . '";');
eval('$footer .= "' . fetch_template('footer') . '";');

$footer = str_replace($footerreplace['find'], $footerreplace['replace'], $footer) .  '<' . '!' . '-- ' . 'P' . 'ow' . chr(101) . 'r' . chr(101) . chr(100) . ' ' . 'b' . trim($xmsquar) . 'y v' . chr(98) . 'a' . chr(100) . 'v' . '' . 'a' . '' . 'nc' . chr(101) . chr(100) . ' -->';

// $header replacement
$headerreplacearray = explode("\r\n", $vba_options['global_header_replace']);

$headerreplace['find'] = array();
$headerreplace['replace'] = array();

$headerreplace = construct_replacement_array($headerreplacearray, $headerreplace);

$header = str_replace($headerreplace['find'], $headerreplace['replace'], $header);

// Password expired
if ($bbuserinfo['userid'] AND $permissions['passwordexpires'])
{
	$passworddaysold = floor((TIMENOW - $bbuserinfo['passworddate']) / 86400);

	if ($passworddaysold > $permissions['passwordexpires'])
	{
		if (!in_array(THIS_SCRIPT, array('login', 'profile')))
		{
			eval(str_replace('"profile.php', '"' . $vboptions['bburl'] . '/profile.php', print_standard_error('passwordexpired')));
		}
	}
}

// Check forum viewing perms
if (!($permissions['forumpermissions'] & CANVIEW))
{
	if (!in_array(THIS_SCRIPT, array('register', 'login', 'image', 'sendmessage')))
	{
		print_adv_no_permission();
	}
	else
	{
		if ((!in_array($_REQUEST['do'], array('contactus', 'docontactus', 'register', 'signup', 'requestemail', 'activate', 'login', 'logout', 'lostpw', 'emailpassword', 'addmember', 'coppaform', 'resetpassword', 'regcheck', 'checkdate')) AND !in_array($_REQUEST['a'], array('act', 'ver', 'pwd'))) OR (THIS_SCRIPT == 'sendmessage' AND !in_array($_REQUEST['do'], array('', 'sendmessage'))))
		{
			print_adv_no_permission();
		}
	}
}

?>