<?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
global $explicitid,$explicitmanid,$seocategoryurls,$detlinkspacechar,$prodid,$catid,$manid,$usemetalongdescription,$usecategoryname,$usepnamefordetaillinks,$seodetailurls,$pagetitle,$sectionname,$sectiondescription,$topsection,$productid,$productname,$productdescription,$topsectionid;
$magicq=FALSE;
if(is_null(@$prodid)) $prodid='';
if(is_null(@$catid)) $catid='';
if(is_null(@$manid)) $manid='';
if(is_null(@$explicitid)) $explicitid='';
if(is_null(@$explicitmanid)) $explicitmanid='';
if(is_null(@$detlinkspacechar)) $detlinkspacechar='';
if(trim(@$prodid)!=''&&trim(@$explicitid)=='') $explicitid=$prodid;
if(trim(@$catid)!=''&&trim(@$explicitid)=='') $explicitid=$catid;
if(trim(@$manid)!=''&&trim(@$explicitmanid)=='') $explicitmanid=$manid;
if(trim(@$prodid)=='' && gettype(@$_GET['prod'])=='string') $prodid=str_replace(@$detlinkspacechar,' ',mi_unstripslashes((string)@$_GET['prod']));
if(@$GLOBALS['iswordpress'] && get_query_var( 'ectprod' ) != '') $prodid=str_replace(@$detlinkspacechar,' ',mi_unstripslashes(urldecode(get_query_var('ectprod'))));
if(trim(@$catid=='') && gettype(@$_GET['cat'])=='string') $catid=mi_unstripslashes(@$_GET['cat']);
if(@$GLOBALS['iswordpress'] && get_query_var( 'ectcat' ) != '') $catid=mi_unstripslashes(urldecode(get_query_var('ectcat')));
if(trim(@$manid=='') && gettype(@$_GET['man'])=='string') $manid=mi_unstripslashes(@$_GET['man']);
if(@$seocategoryurls){$usecategoryname=TRUE;$catid=str_replace(@$detlinkspacechar,' ',$catid);$manid=str_replace(@$detlinkspacechar,' ',$manid);}
if(@$seodetailurls)$usepnamefordetaillinks=TRUE;
if(trim(@$explicitid)!='')$usepnamefordetaillinks=FALSE;
$GLOBALS['toptopsectionid']=$GLOBALS['canonicalurlcats']=$GLOBALS['canonicalurlprods']=$GLOBALS['canonicalurl']=$GLOBALS['metaogimage']=$GLOBALS['canonicalnopage']=$sectionid=$topsection=$productid=$productname=$productdescription=$sectionname=$sectiondescription=$topsection=$pagetitle=$metadescription=$pmetadesc=$smetadesc='';
$topsectionid=0;
$sntxt='sectionName';
$sutxt='sectionURL';
$sdtxt='sectionDescription';
$pntxt='pName';
$scnametxt='scName';
$pttxt='pTitle';
$pmtxt='pMetaDesc';
$sttxt='sTitle';
$smtxt='sMetaDesc';
$sturltxt='pStaticURL';
if(@$usemetalongdescription==TRUE) $pdtxt='pLongDescription'; else $pdtxt='pDescription';
if(@$_GET['pg']!=''){
	$GLOBALS['canonicalnopage']='<link rel="canonical" href="' . str_replace('"','',strip_tags(@$_SERVER['PHP_SELF']));
	$canonqs='';
	$addsep='?';
	foreach(@$_GET as $objitem => $objvalue){
		if($objitem!='pg'){
			$canonqs.=$addsep . urlencode(strip_tags($objitem)) . '=' . urlencode(strip_tags($objvalue));
			$addsep='&';
		}
	}
	$GLOBALS['canonicalnopage'].=$canonqs . "\">\n";
}
function mi_unstripslashes($slashedText){
	global $magicq;
	return($magicq?trim(stripslashes((string)$slashedText)):trim((string)$slashedText));
}
function mi_query($ectsql){
	return(@$GLOBALS['ectdatabase']?$GLOBALS['ectdatabase']->query($ectsql):mysql_query($ectsql));
}
function mi_fetch_assoc($ectres){
	return(@$GLOBALS['ectdatabase']?$ectres->fetch_assoc():mysql_fetch_assoc($ectres));
}
function mi_error(){
	print(@$GLOBALS['ectdatabase']?$GLOBALS['ectdatabase']->error:mysql_error());
}
function mi_free_result($ectres){
	@$GLOBALS['ectdatabase']?$ectres->free_result():mysql_free_result($ectres);
}
function mi_escape_string($estr){
	return(@$GLOBALS['ectdatabase']?$GLOBALS['ectdatabase']->real_escape_string($estr):mysql_real_escape_string($estr));
}
if(function_exists('getadminsettings')){
	$GLOBALS['alreadygotadmin']=getadminsettings();
	$sntxt=getlangid('sectionName',256);
	$sutxt=getlangid('sectionURL',2048);
	$sdtxt=getlangid('sectionDescription',512);
	$pntxt=getlangid('pName',1);
	$pttxt=getlangid('pTitle',2097152);
	$pmtxt=getlangid('pMetaDesc',2097152);
	$scnametxt=getlangid('scName',131072);
	$sttxt=getlangid('sTitle',2097152);
	$smtxt=getlangid('sMetaDesc',2097152);
	$sturltxt=getlangid('pStaticURL',8388608);
	if(@$usemetalongdescription==TRUE) $pdtxt=getlangid('pLongDescription',4); else $pdtxt=getlangid('pDescription',2);
}
if(@$usecategoryname && $catid!=''){
	$sSQL='SELECT sectionID FROM sections WHERE '.(@$seocategoryurls?$sutxt."='".mi_escape_string($catid)."' OR (":'').$sntxt."='".mi_escape_string($catid)."'".(@$seocategoryurls?' AND '.$sutxt."='')":'');
	$result=mi_query($sSQL) or mi_error();
	if($rs=mi_fetch_assoc($result)){ $catname=$catid; $catid=$rs['sectionID']; }
	mi_free_result($result);
}
if(@$usecategoryname && $manid!=''){
	$sSQL='SELECT scID FROM searchcriteria WHERE '.$scnametxt."='".mi_escape_string(str_replace(@$GLOBALS['detlinkspacechar'],' ',$manid))."'";
	$result=mi_query($sSQL) or mi_error();
	if($rs=mi_fetch_assoc($result)){ $manname=$manid; $manid=$rs['scID']; }
	mi_free_result($result);
}
$mi_manufacturerpageurl=@$GLOBALS['manufacturerpageurl'];
if($mi_manufacturerpageurl=='') $mi_manufacturerpageurl='manufacturers'.@$GLOBALS['extension'];
if(strpos(strtolower(@$_SERVER['PHP_SELF']), strtolower($mi_manufacturerpageurl))!==FALSE||@$_GET['man']=='all'||(@$GLOBALS['iswordpress']&&@$GLOBALS['ismanufacturer'])) $manid='all';
if($prodid!=''){
	$result=mi_query('SELECT pID,'.$pntxt.',sectionID,topSection,pStaticPage,'.$sturltxt.','.$pdtxt.','.$sntxt.','.$pttxt.','.$pmtxt.' FROM products INNER JOIN sections ON products.pSection=sections.sectionID WHERE ' . (@$usepnamefordetaillinks&&trim((string)$prodid)!=''?$pntxt:'pID') . "='" . mi_escape_string($prodid) . "'" . (@$seodetailurls?" OR " . $sturltxt . "='".mi_escape_string($prodid)."'":'')) or mi_error();
	if($rs=mi_fetch_assoc($result)){
		$productid=str_replace('"','&quot;',strip_tags($rs['pID']));
		$productname=str_replace('"','&quot;',strip_tags($rs[$pntxt]));
		$sectionid=$rs['sectionID'];
		$topsectionid=$rs['topSection'];
		$productdescription=str_replace('"','&quot;',strip_tags($rs[$pdtxt]));
		$sectionname=str_replace('"','&quot;',strip_tags($rs[$sntxt]));
		$pagetitle=is_null($rs[$pttxt]) ? '' : trim($rs[$pttxt]);
		$pmetadesc=is_null($rs[$pmtxt]) ? '' : trim($rs[$pmtxt]);
		if($pmetadesc!='') $productdescription=str_replace('"','&quot;',$pmetadesc); else $productdescription=trim(str_replace(array('"',"\r","\n","\t"),array('&quot;','',' ',' '),strip_tags($rs[$pdtxt])));
		if(function_exists('getdetailsurl'))
			$GLOBALS['canonicalurl']=($seodetailurls ? 'http' . ($_SERVER['HTTPS']=='on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] : @$GLOBALS['storeurl']) . getdetailsurl($rs['pID'],$rs['pStaticPage'],$rs[$pntxt],$rs[$sturltxt],'','');
		else
			$GLOBALS['canonicalurl']='Your incfunctions.php file should come before the metainfo.php include file.';
	}
	if(is_numeric($catid)){
		$result=mi_query('SELECT '.$sntxt." FROM sections WHERE sectionID='" . mi_escape_string($catid) . "'") or mi_error();
		if($rs=mi_fetch_assoc($result)) $sectionname=str_replace('"','&quot;',strip_tags($rs[$sntxt]));
	}
	//if(strpos(@$GLOBALS['detailpagelayout'],'socialmedia')!==FALSE){
	if(!@$GLOBALS['nometaogimage']){
		$result=mi_query("SELECT imageSrc FROM productimages WHERE imageProduct='" . mi_escape_string($productid) . "' AND imageType IN (0,1" . (@$GLOBALS['usegiantogimage']?',2':'') . ') ORDER BY imageType DESC,imageNumber LIMIT 0,1') or mi_error();
		if($rs=mi_fetch_assoc($result)) $GLOBALS['metaogimage']=$rs['imageSrc'];
		if($GLOBALS['metaogimage']==''&&(@$GLOBALS['defaultproductimage']!=''||@$GLOBALS['defaultdetailimage']!=''))$GLOBALS['metaogimage']=(@$GLOBALS['defaultdetailimage']!=''?@$GLOBALS['defaultdetailimage']:@$GLOBALS['defaultproductimage']);
		if($GLOBALS['metaogimage']!='') $GLOBALS['metaogimage']='<meta property="og:image" content="' . (strpos($GLOBALS['metaogimage'],'://')===FALSE?@$GLOBALS['storeurl']:'') . ltrim($GLOBALS['metaogimage'],'/') . '">' . "\n";
	}
}elseif(is_numeric($manid)||$manid=='all'){
	$sdtxt='scDescription';
	$sntxt='scName';
	$snurl='scName';
	if(function_exists('getadminsettings')){
		$sdtxt=getlangid('scDescription',16384);
		$sntxt=getlangid('scName',131072);
		$snurl=getlangid('scURL',8192);
	}
	if($manid=='all'){
		if(function_exists('getfullurl')){
			if(@$GLOBALS['seomanufacturerpattern']!=''){
				$GLOBALS['canonicalurlcats']=getfullurl(str_replace('%s','',@$GLOBALS['seomanufacturerpattern']));
			}else{
				$mffileexists=file_exists('manufacturers.php');
				$GLOBALS['canonicalurlcats']=getfullurl($mffileexists?'manufacturers'.@$GLOBALS['extension']:'categories'.@$GLOBALS['extension'].'?man=all');
			}
		}else
			$GLOBALS['canonicalurlcats']='Your incfunctions.php file should come before the metainfo.php include file.';
	}else{
		$result=mi_query('SELECT '.$sntxt.','.$sdtxt.','.$snurl." FROM searchcriteria WHERE scID='".mi_escape_string($manid)."'") or mi_error();
		if($rs=mi_fetch_assoc($result)){
			$sectionid=$manid;
			$sectionname=str_replace('"','&quot;',strip_tags($rs[$sntxt]));
			$sectiondescription=str_replace('"','&quot;',strip_tags($rs[$sdtxt]));
			$GLOBALS['manufacturers']=TRUE;
			if(function_exists('getfullurl'))
				$GLOBALS['canonicalurlprods']=getfullurl(getcategoryurl($sectionid,$rs[$sntxt],trim($rs[$snurl]),1));
			else
				$GLOBALS['canonicalurlprods']='Your incfunctions.php file should come before the metainfo.php include file.';
		}
	}
}else{ // if(is_numeric($catid) || @$usecategoryname)
	if($catid=='') $catid=0;
	if(is_numeric($catid)) $sSQL="sectionID='".mi_escape_string($catid)."'"; else $sSQL="sectionName='".mi_escape_string($catid)."'";
	$result=mi_query('SELECT sectionID,'.$sntxt.','.$sdtxt.','.$sutxt.',topSection,rootSection,'.$sttxt.','.$smtxt.' FROM sections WHERE ' . $sSQL) or mi_error();
	if($rs=mi_fetch_assoc($result)){
		$sectionid=$rs['sectionID'];
		$sectionname=str_replace('"','&quot;',strip_tags($rs[$sntxt]));
		$sectiondescription=str_replace('"','&quot;',strip_tags($rs[$sdtxt]));
		$topsectionid=$rs['topSection'];
		$pagetitle=$rs[$sttxt];
		$smetadesc=is_null($rs[$smtxt]) ? '' : trim($rs[$smtxt]);
		if(trim(is_null($rs[$smtxt]) ? '' : $rs[$smtxt])!='')$sectiondescription=str_replace('"','&quot;',$rs[$smtxt]);
		if(function_exists('getfullurl')){
			$GLOBALS['canonicalurlcats']=getfullurl(getcategoryurl($sectionid,$rs[$sntxt],trim($rs[$sutxt]),0));
			$GLOBALS['canonicalurlprods']=getfullurl(getcategoryurl($sectionid,$rs[$sntxt],trim($rs[$sutxt]),1));
		}else{
			$GLOBALS['canonicalurlcats']='Your incfunctions.php file should come before the metainfo.php include file.';
			$GLOBALS['canonicalurlprods']='Your incfunctions.php file should come before the metainfo.php include file.';
		}
	}else{
		if(function_exists('getfullurl')){
			$GLOBALS['canonicalurlcats']=getfullurl(@$GLOBALS['storehomeurl']);
			$GLOBALS['canonicalurlprods']=getfullurl((@$seocategoryurls?'':'products'.@$GLOBALS['extension']) . (@$GLOBALS['seocategoryurls']?str_replace('%s','',@$GLOBALS['seoprodurlpattern']):''));
		}else{
			$GLOBALS['canonicalurlcats']='Your incfunctions.php file should come before the metainfo.php include file.';
			$GLOBALS['canonicalurlprods']='Your incfunctions.php file should come before the metainfo.php include file.';
		}
	}
	if($topsectionid!=0){
		$result=mi_query('SELECT '.$sntxt.",topSection FROM sections WHERE sectionID='" . mi_escape_string($topsectionid) . "'") or mi_error();
		if($rs=mi_fetch_assoc($result)){
			$topsection=str_replace('"','&quot;',strip_tags($rs[$sntxt]));
			$GLOBALS['toptopsectionid']=$rs['topSection'];
		}
	}
}
if(FALSE){
	print 'canonicalurlcats: '.$GLOBALS['canonicalurlcats'].'<br>';
	print 'canonicalurlprods: '.$GLOBALS['canonicalurlprods'].'<br>';
	print 'canonicalurl: '.$GLOBALS['canonicalurl'].'<br>';
}
?>