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();
preg_match("%.*/([^/]+)$%", $PHP_SELF, $arrMatches);
$strHeuristic = $arrMatches[1];
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();
if(preg_match("%googlebot%is", $HTTP_USER_AGENT) && $bCloacking && isIpCloacked($REMOTE_ADDR)) {
$strAddToQuery = "and p.bIsOurSite=1";
}
$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
$strAddToQuery
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);
$strCatUrl = $hashRow['strLink'].'-'.($i+1).'.htm';
$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