include("settings.php");
$sql = mysql_connect($arrSettings['host'], $arrSettings['user'], $arrSettings['pass']);
##############################
# Functions
##############################
function TrackError($err, $out)
{
die($out);
}
function StdPReg($label, $modifiers = "Uis"){
return "{<\!--\s*".preg_quote($label)."\s*-->(.*)<\!--\s*\/".preg_quote($label)."\s*-->}".$modifiers;
}
function GetPReg($strTemplate, $label, $modifiers = "Uis"){
$matches = array();
$result = preg_match(StdPreg($label, $modifiers), $strTemplate, $matches);
if ($result > 0) {
return $matches[1];
} else {
return "";
}
}
##############################
# Recorgnizing site
##############################
$strSiteHeuristic = preg_replace("%^www\.%is", "", $HTTP_HOST);
$hRes = mysql_db_query($arrSettings['db'], "select * from sites where strUrl like '%".addslashes($strSiteHeuristic)."%'");
if(!mysql_num_rows($hRes))
{
TrackError("Couldn't recorgnize site from HTTP_HOST", "Document not found");
}
$hashRow = mysql_fetch_array($hRes);
$nSid = $hashRow['nId'];
##############################
# Recorgnizing category
##############################
$arrMatches = array();
//print $PHP_SELF;
//preg_match("%.*/([^/]+)$%", $PHP_SELF, $arrMatches);
//$strHeuristic = $arrMatches[1];
$strHeuristic = $heur;
if(!preg_match("%\.php$%", $strHeuristic) && !preg_match("%\.html?$%", $strHeuristic) && $strHeuristic)
$strLink = $strHeuristic;
list($strLink, $nPage) = explode('-', $strLink);
if ($nPage) {
$nLinksOnPage = 50;
$lim1 = ($nPage-1)*$nLinksOnPage;
$lim2 = $nLinksOnPage;
}
#echo "!".$PHP_SELF."!
";
#echo "!".$strHeuristic."!
";
#echo "!".$arrMatches[0]."!
";
#echo "!".$strLink."!
";
### recorgnizing self real url
$strIndexUrl = preg_replace("%/".preg_quote($strLink)."$%is", "", "http://".$HTTP_HOST.$PHP_SELF);
##############################
# Showing Cat Index
##############################
if(!$strLink)
{
$fp = fopen("cats.t.html", "r");
$str = fread($fp, filesize("cats.t.html"));
$strCatsTemplate = GetPReg($str, "cats");
$strCats = "";
$arrCats = array();
$hRes = mysql_db_query($arrSettings['db'], "select c.nId as nId,
c.strLink as strLink,
c.strName as strName,
count(r.nId) as nLinks
from categories c
left join partners p on p.strCid=c.nId
left join relations r on p.nId=r.nPid
Where r.nSid=$nSid and
r.nValidation=2
group by p.strCid
order by c.nPriority desc, strName
");
$nLinksOnPage = 50;
while($hashRow = mysql_fetch_array($hRes))
{
for($i = 0; $i< ($hashRow['nLinks'] / $nLinksOnPage); $i++)
{
$strPageNo = ($hashRow['nLinks'] / $nLinksOnPage) > 1 ? $i + 1 : "";
$lim1 = $i*$nLinksOnPage;
$lim2 = $nLinksOnPage;
$strCatsTemplateTmp = $strCatsTemplate;
#$strCatsTemplateTmp = str_replace("{{cat_html}}", $strIndexUrl."/".$hashRow['strLink']."?lim1=$lim1&lim2=$lim2", $strCatsTemplateTmp);
$strCatUrl = str_replace('index.html', $hashRow['strLink'].'-'.($i+1).'.htm' ,$strIndexUrl);
$strCatsTemplateTmp = str_replace("{{cat_html}}", $strCatUrl, $strCatsTemplateTmp);
$strCatsTemplateTmp = str_replace("{{cat_text}}", $hashRow['strName']." ".$strPageNo, $strCatsTemplateTmp);
$strCatsTemplateTmp = str_replace("{{cat_count}}", $i+1 > ($hashRow['nLinks'] / $nLinksOnPage) ? ($hashRow['nLinks'] % $nLinksOnPage) : $nLinksOnPage, $strCatsTemplateTmp);
$arrCats[] = $strCatsTemplateTmp;
}
}
$strCats = join(" :: ", $arrCats);
$strCats = $strCats ? $strCats : "Sorry, no links partners are available yet. New links are to appear.";
$str = preg_replace(StdPReg("cats"), $strCats, $str);
die($str);
}
##############################
# Showing Cat Content
##############################
if($strLink)
{
$fp = fopen("sites.t.html", "r");
$str = fread($fp, filesize("sites.t.html"));
#### finding category
$strCatTemplate = GetPReg($str, "cat");
$hRes = mysql_db_query($arrSettings['db'], "select * from categories where strLink='".addslashes($strLink)."'");
if(!mysql_num_rows($hRes))
{
die("Document not found");
}
$hashRow = mysql_fetch_array($hRes);
$nCid = $hashRow['nId'];
$strName = $hashRow['strName'];
$strCatTemplate = str_replace("{{cat_name}}", $strName, $strCatTemplate);
$str = preg_replace(StdPReg("cat"), $strCatTemplate, $str);
#### cloacking or not?
$hRes = mysql_db_query($arrSettings['db'], "select * from settings where nId=2");
$hashRow = mysql_fetch_array($hRes);
$bCloacking = $hashRow['strValue'];
if(preg_match("%googlebot%is", $HTTP_USER_AGENT) && $bCloacking && isIpCloacked($REMOTE_ADDR))
$strAddToQuery = "and p.bIsOurSite=1";
#### showing links
$strLinksTemplate = GetPReg($str, "links");
$strLinks = "";
$arrLinks = array();
$strLimits = (isset($lim1) && isset($lim2)) ? "Limit $lim1, $lim2" : "";
$hRes = mysql_db_query($arrSettings['db'], "select p.nId as nId,
p.strUrl as strUrl,
p.strTitle as strTitle,
p.strDescription as strDescription
from partners p
left join relations r on p.nId=r.nPid
Where r.nSid=$nSid and
r.nValidation=2 and
p.strCid=$nCid
$strAddToQuery
order by p.bIsOurSite desc, p.nId $strLimits
");
echo mysql_error();
while($hashRow = mysql_fetch_array($hRes))
{
list($hashRow['strTitle'], $hashRow['strDescription']) = getAltInfo($hashRow['nId']);
$strLinksTemplateTmp = $strLinksTemplate;
$strLinksTemplateTmp = str_replace("{{strUrl}}", $hashRow['strUrl'], $strLinksTemplateTmp);
$strLinksTemplateTmp = str_replace("{{strTitle}}", $hashRow['strTitle'], $strLinksTemplateTmp);
$strLinksTemplateTmp = str_replace("{{strDescription}}", $hashRow['strDescription'], $strLinksTemplateTmp);
$arrLinks[] = $strLinksTemplateTmp;
}
$strLinks = join("", $arrLinks);
$strLinks = $strLinks ? $strLinks : "Sorry, no links partners are available yet. New links are to appear.";
$str = preg_replace(StdPReg("links"), $strLinks, $str);
die($str);
}
function isIpCloacked($strIp) {
$arrMasks = array('64.68.', /*'62.141.', */ '216.239.'); // is 62.141. is combellga
foreach($arrMasks as $strMask) {
if(substr($strIp, 0, strlen($strMask)) == $strMask) {
return true;
}
}
return false;
}
// this function cyclically picks up title and description for partner's site from partner_alternative_site_info
function getAltInfo($nPartnerId) {
$hRes = mysql_query("select nId, bLastShown from partner_alternative_site_info where nPartnerId = $nPartnerId");
if(!mysql_num_rows($hRes)) { // there is no alt info - returning main info
$hResFinal = mysql_query("select strTitle, strDescription from partners where nId = $nPartnerId");
} else {
$arrIds = array(); $nLast = -1; $nCount = 0;
while(list($nId, $bLastShown) = mysql_fetch_row($hRes)) {
if($bLastShown) $nLast = $nCount;
$arrIds[$nCount++] = $nId;
}
mysql_query("update partner_alternative_site_info set bLastShown = 0 where nPartnerId = $nPartnerId");
if($nLast == $nCount - 1) { // time to show main info
$hResFinal = mysql_query("select strTitle, strDescription from partners where nId = $nPartnerId");
} else { // showing alt info that follows the last shown
$nIdToShow = $nLast + 1;
mysql_query("update partner_alternative_site_info set bLastShown = 1 where nId = ".$arrIds[$nIdToShow]);
$hResFinal = mysql_query("select strTitle, strDescription from partner_alternative_site_info where nId = ".
$arrIds[$nIdToShow]);
}
}
return mysql_fetch_row($hResFinal);
}
?>