<?php
//This code is copyright (c) ViciSoft SL, all rights reserved.
//The contents of this file are protected under law as the intellectual property 
//of ViciSoft SL. Any use, reproduction, disclosure or copying of any kind 
//without the express and written permission of ViciSoft SL is forbidden.
//Author: Vince Reid, vincereid@gmail.com
if(!@$GLOBALS['incfunctionsdefined']){print 'No incfunctions.php file';exit;}
global $topsectionids,$manufacturers,$explicitid,$seocategoryurls,$seourlsthrow301,$seocaturlpattern,$seoprodurlpattern,$seomanufacturerpattern,$usecategoryname,$usecsslayout,$categorycolumns,$manufacturerpageurl,$usecategoryformat,$catseparator,$catalogroot;
$catname=$caturl=$catrootsection=''; $catrootsection=0;
$GLOBALS['alreadygotadmin']=getadminsettings();
$dothrow301=FALSE;
$hascustomcatlayout=trim_(@$GLOBALS['categorypagelayout'])!='';
if($hascustomcatlayout) $customlayoutarray=explode(',',@$GLOBALS['categorypagelayout']);
if(getget('cat')!='') $theid=trim(getget('cat')); else $theid='';
if(@$GLOBALS['iswordpress'] && get_query_var( 'ectcat' ) != '') $theid=urldecode(get_query_var('ectcat'));
if(@$seocategoryurls){$usecategoryname=TRUE;$theid=str_replace(@$GLOBALS['detlinkspacechar'],' ',$theid);}
if(is_numeric(@$explicitid))
	$theid=@$explicitid;
elseif(@$usecategoryname && $theid!=''){
	$sSQL='SELECT sectionID FROM sections WHERE '.(@$seocategoryurls?getlangid('sectionurl',2048)."='".escape_string($theid)."' OR (":'').getlangid('sectionName',256)."='".escape_string($theid)."'".(@$seocategoryurls?' AND '.getlangid('sectionurl',2048)."='')":'');
	$result=ect_query($sSQL) or ect_error();
	if($rs=ect_fetch_assoc($result)){
		$catname=$theid;
		$theid=$rs['sectionID'];
	}elseif(! is_numeric($theid)){
		$theid=-1;
	}
	ect_free_result($result);
}
if((@$seocategoryurls || @$usecategoryname) && $catname=='' && is_numeric($theid)){
	$sSQL='SELECT sectionID,'.getlangid('sectionName',256).','.getlangid('sectionName',256).','.getlangid('sectionurl',2048).",rootSection FROM sections WHERE sectionID='".escape_string($theid)."'";
	$result=ect_query($sSQL) or ect_error();
	if($rs=ect_fetch_assoc($result)){ $dothrow301=@$explicitid==''; $catname=$rs[getlangid('sectionName',256)]; $theid=$rs['sectionID']; $caturl=trim($rs[getlangid('sectionurl',2048)]); $catrootsection=$rs['rootSection']; }
	ect_free_result($result);
}
if(@$manufacturerpageurl==''){
	$manufacturerpageurl='manufacturers'.$GLOBALS['extension'];
	if(@$GLOBALS['seomanufacturerpattern']!='')$manufacturerpageurl=str_replace('%s','',$GLOBALS['seomanufacturerpattern']);
}
if(strpos(strtolower(@$_SERVER['PHP_SELF']), strtolower($manufacturerpageurl))!==FALSE||getget('man')=='all'||(@$GLOBALS['iswordpress']&&@$GLOBALS['ismanufacturer'])) $manufacturers=TRUE; else $manufacturers=FALSE;
if(((@$seocategoryurls && (@$_SERVER['REDIRECT_URL'].@$_SERVER['HTTP_X_ORIGINAL_URL'])=='') || $dothrow301) && @$seourlsthrow301 && ! is_numeric(@$explicitid) && !@$GLOBALS['recommendedcategories']){
	$newloc=getfullurl(getcategoryurl($theid,$catname,$caturl,$catrootsection));
	$addand=$newqs='';
	foreach($_GET as $key=>$val){
		if($key!='cat'){ $newqs.=$addand.$key.'='.urlencode($val); $addand='&'; }
	}
	ob_end_clean();
	header('HTTP/1.1 301 Moved Permanently');
	header('Location: '.$newloc.($newqs!=''?'?'.$newqs:''));
	exit;
}
if(@$GLOBALS['bmlbannercategories']!='' && @$GLOBALS['paypalpublisherid']!='') displaybmlbanner($GLOBALS['paypalpublisherid'],$GLOBALS['bmlbannercategories']);
if(! is_numeric($theid)) $theid=$catalogroot;
if(! is_numeric(@$categorycolumns) || $categorycolumns=='') $categorycolumns=1;
$cellwidth=(int)(100/$categorycolumns);
if(@$usecsslayout){
	$usecategoryformat=1;
	$afterimage='';
	$beforedesc='';
}elseif(@$usecategoryformat==3){
	$afterimage='<br />';
	$beforedesc='';
}elseif(@$usecategoryformat==2){
	$afterimage='';
	$beforedesc='';
}else{
	$usecategoryformat=1;
	$afterimage='';
	$beforedesc='</td></tr><tr><td class="catdesc" colspan="2">';
}
$border=0;
if(! @isset($catseparator)) $catseparator=@$usecsslayout?'':'<br />&nbsp;';
$tslist='';
$thetopts=$theid;
$topsectionids=$theid;
$success=TRUE;
$categorydepth=0;
$sectionheader=$sectionfooter='';
if(@$_SESSION['clientID']!='' && @$_SESSION['clientLoginLevel']!='') $minloglevel=$_SESSION['clientLoginLevel']; else $minloglevel=0;
$columncount=0;
if(@$GLOBALS['recommendedcategories']){
	// Nothing
}elseif($manufacturers){
	$tslist='<a class="ectlink" href="'.$GLOBALS['storehomeurl'].'">'.$GLOBALS['xxHome'].'</a> &raquo; ' . $GLOBALS['xxManuf'];
	$GLOBALS['xxAlProd']='';
}else{
	for($categorydepth=0; $categorydepth<=10; $categorydepth++){
		if($thetopts==$catalogroot){
			$caturl=$GLOBALS['storehomeurl'];
			$sSQL='SELECT sectionID,topSection,'.getlangid('sectionName',256).',rootSection,sectionDisabled,'.getlangid('sectionurl',2048) . ','.getlangid('sectionHeader',524288) . ','.getlangid('sectionFooter',524288) . " FROM sections WHERE sectionID='" . escape_string($catalogroot) . "'";
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result)){
				$GLOBALS['xxHome']=$rs[getlangid('sectionName',256)];
				if(trim($rs[getlangid('sectionurl',2048)])!='') $caturl=$rs[getlangid('sectionurl',2048)];
				if($rs['sectionID']==(int)$theid){
					$sectionheader=$rs[getlangid('sectionHeader',524288)];
					$sectionfooter=$rs[getlangid('sectionFooter',524288)];
					if($sectionheader!='') print '<div class="catheader">' . $sectionheader . '</div>';
				}
			}
			ect_free_result($result);
			if($theid==$catalogroot) $tslist=$GLOBALS['xxHome'].$tslist; else $tslist='<a class="ectlink" href="'.$caturl.'">'.$GLOBALS['xxHome'].'</a>'.$tslist;
			break;
		}elseif($categorydepth==10){
			$tslist='<strong>Loop</strong>' . $tslist;
		}else{
			$sSQL='SELECT sectionID,topSection,'.getlangid('sectionName',256).',rootSection,sectionDisabled,'.getlangid('sectionurl',2048).' AS sectionurl' . ($theid==$thetopts?','.getlangid('sectionHeader',524288).','.getlangid('sectionFooter',524288):'') . " FROM sections WHERE sectionID='" . escape_string($thetopts) . "'";
			$result2=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result2) > 0){
				$rs=ect_fetch_assoc($result2);
				if($rs['sectionDisabled'] > $minloglevel){
					$GLOBALS['xxNoCats']=$GLOBALS['xxCatRes'];
					$GLOBALS['xxPagNoA']=$GLOBALS['xxLogVie'];
					if(@$GLOBALS['enableclientlogin']){
						displaysoftlogin();
						if(trim_(@$_SESSION['clientID'])=='') $GLOBALS['xxPagNoA'].='<div style="margin:20px 0 50px 0">' . imageorbuttontag(@$GLOBALS['imgloginaccount'],@$GLOBALS['xxLogAcc'],'logintoaccount',"displayloginaccount()",TRUE) . '</div>';
					}
					$success=FALSE;
					if($rs['sectionDisabled']>=127) $GLOBALS['nothrow404']=TRUE;
				}elseif($rs['sectionID']==(int)$theid){
					if(@$GLOBALS['dynamicbreadcrumbs'])
						$tslist='<div class="ectbreadcrumb">&raquo; ' . breadcrumbselect($rs['sectionID'],$rs['topSection']) . '</div>' . $tslist;
					else
						$tslist='<div class="ectbreadcrumb">&raquo; ' . $rs[getlangid('sectionName',256)] . '</div>' . $tslist;
					$sectionheader=$rs[getlangid('sectionHeader',524288)];
					$sectionfooter=$rs[getlangid('sectionFooter',524288)];
					if($sectionheader!='') print '<div class="catheader">' . $sectionheader . '</div>';
					if(@$explicitid=='' && trim($rs['sectionurl'])!='' && @$redirecttostatic==TRUE){
						ob_end_clean();
						header('HTTP/1.1 301 Moved Permanently');
						header('Location: '.getfullurl($rs['sectionurl']));
						exit;
					}
				}else{
					if(@$GLOBALS['dynamicbreadcrumbs'])
						$tslist='<div class="ectbreadcrumb">&raquo; ' . breadcrumbselect($rs['sectionID'],$rs['topSection']) . '</div>' . $tslist;
					else
						$tslist='<div class="ectbreadcrumb">&raquo; <a class="ectlink" href="' . getcategoryurl($rs['sectionID'],$rs[getlangid('sectionName',256)],$rs['sectionurl'],$rs['rootSection']) . '">' . $rs[getlangid('sectionName',256)] . '</a></div>' . $tslist;
				}
				$thetopts=$rs['topSection'];
				$topsectionids.=',' . $thetopts;
			}else{
				$tslist='Top Section Deleted' . $tslist;
				break;
			}
			ect_free_result($result2);
		}
	}
}
if(@$GLOBALS['xxAlProd']!='') $tslist.='<div class="ectbreadcrumb">&raquo; <a class="ectlink" href="' . (@$seocategoryurls?'':'products'.$GLOBALS['extension']) . ($theid=='0'||$theid==$catalogroot ? (@$seocategoryurls?str_replace('%s','',$seoprodurlpattern):'') : (@$seocategoryurls?'':'?cat=') . getcatid($theid,$catname,$seoprodurlpattern)) . '">' . $GLOBALS['xxAlProd'] . '</a></div>';
if($manufacturers==TRUE){
	$showdiscounts=FALSE;
	$sSQL='SELECT scID AS sectionID,'.getlangid('scName',131072).' AS sectionName,\'\' AS sCustomCSS,'.getlangid('scDescription',16384).' AS sectionDescription,scLogo AS sectionImage,scOrder AS sectionOrder,1 AS rootSection,'.getlangid('scURL',8192).' AS sectionurl FROM searchcriteria WHERE scGroup=0 ORDER BY scOrder,' . getlangid('scName',131072);
}else
	$sSQL='SELECT sectionID,'.getlangid('sectionName',256).' AS sectionName,sCustomCSS,'.(@$nocategorydescription==TRUE?"''":getlangid('sectionDescription',512)).' AS sectionDescription,sectionImage,sectionOrder,rootSection,'.getlangid('sectionurl',2048)." AS sectionurl FROM sections WHERE sectionID<>0 AND " . (@$GLOBALS['recommendedcategories']?'sRecommend<>0':"topSection='" . escape_string($theid) . "'") . " AND sectionDisabled<=" . $minloglevel . ' ORDER BY ' . (@$GLOBALS['sortcategoriesalphabetically'] ? getlangid('sectionName',256) : 'sectionOrder');
$result=ect_query($sSQL) or ect_error();
if(!$success || ect_num_rows($result)==0){
	$success=FALSE;
	$mess1=$GLOBALS['xxNoCats'];
	$tslist='<a class="ectlink" href="'.$GLOBALS['storehomeurl'].'">'.$GLOBALS['xxHome'].'</a> ';
	if(@$GLOBALS['xxAlProd']!='') $tslist.=' &raquo; <a class="ectlink" href="' . (@$seocategoryurls?str_replace('%s','',$seoprodurlpattern):'products'.$GLOBALS['extension']) . '">' . $GLOBALS['xxAlProd'] . '</a>';
	if(@$usecsslayout) print '<div class="catnavigation">' . $tslist . '</div>'; else print '<td class="catnavigation"><p class="catnavigation"><strong>' . $tslist . '</strong></p>';	
	if(!@$GLOBALS['recommendedcategories']&&ect_num_rows($result)==0) header('HTTP/1.1 404 Not Found');
}else{
	$success=TRUE;
	$mess1='';
}
if($usecategoryformat==1 || $usecategoryformat==2) $numcolumns=2*$categorycolumns; else $numcolumns=$categorycolumns;
if(! @$usecsslayout) $headtable='<table width="100%" border="0" cellspacing="3" cellpadding="3">'; else $headtable='';
if($mess1!=''&&@$GLOBALS['recommendedcategories']!=TRUE){
	print $headtable; $headtable='';
	if(! @$usecsslayout) print '<tr><td align="center"' . ($numcolumns>1 ? ' colspan="' . $numcolumns . '"' : '') . '>';
	notfoundmarkup($GLOBALS['xxNoCats'],FALSE);
	if(! @$usecsslayout) print '</td></tr>';
}
if(@$nowholesalediscounts==TRUE && @$_SESSION['clientUser']!='')
	if((($_SESSION['clientActions'] & 8)==8) || (($_SESSION['clientActions'] & 16)==16)) $GLOBALS['noshowdiscounts']=TRUE;
if($success){
	if(($GLOBALS['adminPayLater'] & 2)==2){
		if(getpayprovdetx(27,$data1,$data2,$data3,$data4,$data5,$data6,$ppflag1,$ppflag2,$ppflag3,$ppbits,$demomode,$ppmethod)){
			print '<script src="https://www.paypal.com/sdk/js?client-id=' . $data1 . '&currency=' . $GLOBALS['countryCurrency'] . '&components=messages" data-partner-attribution-id="EcommerceTemplates_Ecom_PPCP"></script>'."\n";
			print '<div data-pp-message data-pp-placement="product" data-pp-amount="" data-pp-style-layout="flex" data-pp-style-ratio="20x1" class="paylaterbanner detailpaylaterbanner"></div>';
		}
	}
	if(@$GLOBALS['noshowdiscounts']!=TRUE&&@$GLOBALS['recommendedcategories']!=TRUE){
		if($theid=='0')
			$sSQL='SELECT DISTINCT '.getlangid('cpnName',1024)." FROM coupons WHERE (cpnSitewide=1 OR cpnSitewide=2)";
		else
			$sSQL='SELECT DISTINCT '.getlangid('cpnName',1024)." FROM coupons LEFT OUTER JOIN cpnassign ON coupons.cpnID=cpnassign.cpaCpnID WHERE (((cpnSitewide=0 OR cpnSitewide=3 OR cpnSitewide=4) AND cpaType=1 AND cpaAssignment IN ('" . str_replace(',',"','",$topsectionids) . "')) OR cpnSitewide=1 OR cpnSitewide=2)";
		$sSQL.=" AND cpnNumAvail>0 AND cpnStartDate<='" . date('Y-m-d',time()) ."' AND cpnEndDate>='" . date('Y-m-d',time()) ."' AND cpnIsCoupon=0 AND ((cpnLoginLevel>=0 AND cpnLoginLevel<=".$minloglevel.') OR (cpnLoginLevel<0 AND -1-cpnLoginLevel='.$minloglevel.'))';
		$result2=ect_query($sSQL) or ect_error();
		if(ect_num_rows($result2) > 0){
			print $headtable; $headtable='';
			if(! @$usecsslayout) print '<tr><td align="left" class="allcatdiscounts"' . ($numcolumns>1 ? ' colspan=" ' . $numcolumns . '"' : '') . '>';
			print '<div class="discountsapply allcatdiscounts">' . $GLOBALS['xxDsCat'] . '</div><div class="catdiscounts allcatdiscounts">';
			while($rs=ect_fetch_assoc($result2)){
				print $rs[getlangid('cpnName',1024)] . '<br />';
			}
			print '&nbsp;</div>';
			if(! @$usecsslayout) print '</td></tr>';
		}
		ect_free_result($result2);
	}
	if(! @$usecsslayout&&$headtable=='') print '</table>';
	if((! (@isset($showcategories) && @$showcategories==FALSE))&&@$GLOBALS['recommendedcategories']!=TRUE){
		if(! (@$GLOBALS['nobuyorcheckout'] || @$GLOBALS['nocheckoutbutton'])) print '<div class="catnavandcheckout catnavcategories">';
		print '<div class="catnavigation catnavcategories">' . $tslist . "</div>\n";
		if(! (@$GLOBALS['nobuyorcheckout'] || @$GLOBALS['nocheckoutbutton'])) print '<div class="catnavcheckout">' . imageorbutton(@$GLOBALS['imgcheckoutbutton'],@$GLOBALS['xxCOTxt'],'checkoutbutton',$GLOBALS['pathtohere'] . 'cart'.$GLOBALS['extension'], FALSE) . "</div></div>\n";
	}
	if(@$usecsslayout) print '<div class="categories">'; else print '<table width="98%" border="0" cellspacing="' . ($usecategoryformat==1 && $categorycolumns>1 ? 0 : 3) . '" cellpadding="' . ($usecategoryformat==1 && $categorycolumns>1 ? 0 : 3) . '">';
	while($rs=ect_fetch_assoc($result)){
		$startlink='<a class="ectlink" href="' . getcategoryurl($rs['sectionID'],$rs['sectionName'],$rs['sectionurl'],$rs['rootSection']) . '">';
		$sSQL='SELECT DISTINCT '.getlangid('cpnName',1024)." FROM coupons LEFT OUTER JOIN cpnassign ON coupons.cpnID=cpnassign.cpaCpnID WHERE (cpnSitewide=0 OR cpnSitewide=3 OR cpnSitewide=4) AND cpnNumAvail>0 AND cpnStartDate<='" . date('Y-m-d',time()) ."' AND cpnEndDate>='" . date('Y-m-d',time()) ."' AND cpnIsCoupon=0 AND cpaType=".($manufacturers?3:1)." AND cpaAssignment='" . $rs['sectionID'] . "'" .
			' AND ((cpnLoginLevel>=0 AND cpnLoginLevel<='.$minloglevel.') OR (cpnLoginLevel<0 AND -1-cpnLoginLevel='.$minloglevel.'))';
		$alldiscounts='';
		if(!@$GLOBALS['noshowdiscounts']){
			$result2=ect_query($sSQL) or ect_error();
			while($rs2=ect_fetch_assoc($result2))
				$alldiscounts.=$rs2[getlangid('cpnName',1024)] . '<br />';
			ect_free_result($result2);
		}
		$secdesc=trim_($rs['sectionDescription']);
		$noimage=(trim_($rs['sectionImage'])=='');
		if(@$usecsslayout)
			print '<div class="' . trim('category ' . $rs['sCustomCSS']) . '">';
		else{
			if($columncount==0) print '<tr>';
			if($usecategoryformat==1 && $categorycolumns>1) print '<td width="' . $cellwidth . '%" valign="top"><table width="100%" border="0" cellspacing="3" cellpadding="3"><tr>';
		}
		if($hascustomcatlayout){
			foreach($customlayoutarray as $layoutoption){
				$layoutoption=trim(strtolower($layoutoption));
				if($layoutoption=='catimage'){
					if(! $noimage) print '<div class="catimage">' . $startlink . '<img alt="' . htmlspecials($rs['sectionName']) . '" class="catimage" src="' . $rs['sectionImage'] . '" /></a></div>';
				}elseif($layoutoption=='catname'){
					print '<div class="catname">' . $startlink . $rs['sectionName'] . '</a></div>';
				}elseif($layoutoption=='discounts'){
					if($alldiscounts!='') print '<div class="eachcatdiscountsapply eachcatdiscount">' . $GLOBALS['xxDsApp'] . '</div><div class="catdiscounts eachcatdiscount">' . $alldiscounts . '</div>';
				}elseif($layoutoption=='description'){
					if($secdesc!='') print '<div class="catdesc">' . $secdesc . '</div>';
				}
			}
		}else{
			if(($usecategoryformat==1 || $usecategoryformat==2) && ! $noimage){
				$cellwidth -= 5;
				print (@$usecsslayout ? '<div' : '<td width="5%" align="right"') . ' class="catimage">' . $startlink . '<img alt="' . str_replace('"','',$rs['sectionName']) . '" class="catimage" src="' . $rs['sectionImage'] . '" /></a>' . $afterimage . (@$usecsslayout ? '</div>' : '</td>');
			}
			if(! @$usecsslayout) print '<td class="catname" width="' . ($usecategoryformat==1 && $categorycolumns>1 ? 95 : $cellwidth) . '%"' . (($usecategoryformat==1 || $usecategoryformat==2) && $noimage ? ' colspan="2"' : "") . '>';
			if(($usecategoryformat==1 || $usecategoryformat==2) && ! $noimage) $cellwidth+=5;
			if($usecategoryformat != 1 && $usecategoryformat != 2 && ! $noimage) print $startlink . '<img alt="' . str_replace('"','',$rs['sectionName']) . '" class="catimage" src="' . $rs['sectionImage'] . '" /></a>' . $afterimage;
			if(@$nocategoryname!=TRUE) print (@$usecsslayout ? '<div class="catname">' : '<p class="catname"><strong>') . $startlink . $rs['sectionName'] . '</a>' . $GLOBALS['xxDot'] . (! @$usecsslayout ? '</strong>' : '');
			if($alldiscounts!='') print ' <div class="eachcatdiscountsapply eachcatdiscount">' . $GLOBALS['xxDsApp'] . '</div><div class="catdiscounts eachcatdiscount">' . $alldiscounts . '</div>';
			if($secdesc=='') print @$catseparator;
			if(@$nocategoryname!=TRUE) print (@$usecsslayout ? '</div>' : '</p>');
			if($secdesc!='') print (@$usecsslayout ? '<div' : $beforedesc . '<p') . ' class="catdesc">' . $secdesc . $catseparator . (@$usecsslayout ? '</div>' : '</p>');
		}
		print (@$usecsslayout ? '</div>' : '</td>');
		if($usecategoryformat==1 && $categorycolumns>1 AND ! @$usecsslayout) print '</tr></table></td>';
		$columncount++;
		if($columncount==$categorycolumns && ! @$usecsslayout){
			print '</tr>';
			$columncount=0;
		}
	}
	if($columncount<$categorycolumns && $columncount != 0 && ! @$usecsslayout){
		while($columncount<$categorycolumns){
			print '<td ' . ($usecategoryformat==2 ? ' colspan="2"' : '') . '>&nbsp;</td>';
			$columncount++;
		}
		print '</tr>';
	}
	if(@$usecsslayout) print "</div>";
}
if(! @$usecsslayout) print "</table>";
if($sectionfooter!='') print '<div class="catfooter">' . $sectionfooter . '</div>';
ect_free_result($result);
?>