<?PHP
ob_start();
header("Content-Type: text/xml");
//DYNAMIC RSS FEED
//age=0,1,2,3,4,,5,etc 
//rule=best,new,staged
//search=search term
//w=Width of columns
//h=Height or number of rows
//depth=How many (optional)
//nats=sets up nats cookie for code exchange.
//
function cleanit($v){
    $v=str_replace("'",'',$v);
    $v=str_replace('"','',$v);
    $v=str_replace('<','_',$v);
	$v=str_replace('pass','',$v);
    $v=str_replace("join",'',$v);
	return str_replace('>','_',$v);
}
if (version_compare(phpversion(), "5.0.0", ">=")) { 
  // you're on 5.0.0 or later 
	$HTTP_COOKIE_VARS=&$_COOKIE;
	$HTTP_GET_VARS=&$_GET;
	$HTTP_POST_VARS=&$_POST;
	$HTTP_SERVER_VARS=&$_SERVER;
}else{
	$_COOKIE=&$HTTP_COOKIE_VARS;
	$_GET=&$HTTP_GET_VARS;
	$_POST=&$HTTP_POST_VARS;
	$_SERVER=&$HTTP_SERVER_VARS;
}

foreach($_COOKIE as $k=>$v){
   $$k=cleanit($v);
}
foreach($_GET as $k=>$v){
   $$k=cleanit($v);
}
foreach($_POST as $k=>$v){
   $$k=cleanit($v);
}
foreach($_SERVER as $k=>$v){
   $$k=cleanit($v);
}

include('includes/config.inc.php');
include('cf.php');
$sitepath=substr($_SERVER[PHP_SELF],0,strpos($_SERVER[PHP_SELF],'rss.php'));
$sitepath=substr($sitepath,0,-1);


if(intval($_GET['w'])==0){
	$width=6;
}else{
	$width=$w;
}
if(intval($_GET['h'])==0){
	$height=4;
}else{
	$height=$h;
}

if(intval($depth)==0){
	$depth=$width*$height;
}

if(intval($age)<1){
	$age=1;
}
$age=intval($age);

$skimratio='100'; //set this to the default skim ratio

if(!is_dir('cache')){
	mkdir('cache');
//	@c h m o d('cache',0777);
}

$xsearch=stripslashes(trim(strtolower($search)));
$xsearch=str_replace(",",'_',$xsearch);
$xsearch=str_replace(".",'_',$xsearch);
$fn='cache/rss'.date('Ymd').$xsearch.$age.$width.$height.'.xml';
if(is_file($fn) && $flush!=1){
	$fh=fopen($fn,'r');
	$output=fread($fh,filesize($fn));
	fclose($fh);
	echo $output;
	exit();
}

switch($rule){
	default:
	case 'best':
		$orderby="if(periodsshown>$age,periodsshown-99999999,periodsshown),clicks/exposure desc,rand() ";
	break;
	case 'new':
		$orderby="periodsshown,rand() ";
	break;
	case 'staged':
		$orderby="clickperiods ";
	break;
}

$search=stripslashes(trim(strtolower($search)));
$search=str_replace('"','',$search);
$search=str_replace("'",'',$search);
$search=str_replace(",",' ',$search);
$search=str_replace("join",'',$search);
$q="SELECT category,url,id,hasvideo,thumbname,thumbsize,thumbwidth,thumbheight,keywords,description from tblTgp WHERE 
 !(category like '%hidden%') AND 
   periodsshown>$age AND
  (accept='Approved' or accept='Archive') 
   ";
if($search!=''){
	$q.="AND (id='$search' ";
	$s=$search;
	$searchterms=explode(' ',$search);
	foreach($searchterms as $key=>$search){  
		$q.=" or (url like '%$search%' OR description like '%$search%' OR category like '%$search%' or keywords like '%$search%') ";
	}
	$q.=") ";
}
$q.=" ORDER BY $orderby LIMIT 0,".($depth);
  
$output ="<"."?xml version="1.0"?".">
";
$output .="<!--  RSS generated by Comus Thumbs 2.01 at $sitename on ".date("F j, Y, g:i a")." -->
"; 
$output .="<!--  $q -->
"; 

$doNats='';
/* 
 //Uncomment this block if you want to allow your traders 
 //to get sales from your Nats galleries

$nats=stripslashes($_GET['nats']);
if($nats!=''){ //Convert the nats codes on all galleries.
	$maindomain=$_SERVER[HTTP_HOST];
 	$cookiedomain=str_replace('www.','',$maindomain);
	if(strpos($cookiedomain,':')!=''){
		$cd=explode(':',$cookiedomain);
		$cookiedomain=$cd[0];
	}
	if(substr($cookiedomain,0,1)!='.'){
		$cookiedomain=".{$cookiedomain}";
	}
	echo "
<SCRIPT language=JavaScript>
<!--
document.cookie = 'nats={$nats}; path=/; domain={$cookiedomain};'
// -->
</SCRIPT>
";
}
if($_GET['nats']!=''){
	$doNats='&nats='.htmlspecialchars($_GET['nats']);
}
*/

			echo <<<END
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
 <channel>
  <title>{$sitetitle}</title>
  <link>http://{$sitename}/</link>
  <description>{$sitedesc} Todays Categories: ,BigTits,Orgy,BannersHidden,Creampies,Milfs
  </description>
  <language>en-us</language>
  <webMaster>{$tgpemail}</webMaster>
  <generator>Comus Thumbs 2.51 http://comusthumbs.com/ </generator>
  <rating>Adult</rating>
  <docs>http://www.rssboard.org/rss-specification#ltenclosuregtSubelementOfLtitemgt</docs>
  <image>
   <link>http://www.{$sitename}/</link>
   <title>{$category}: {$description}</title>
   <url>http://www.{$sitename}{$sitepath}/thumbs/{$thumbname}</url>
   <description>{$sitedesc}
   Todays Categories: ,BigTits,Orgy,BannersHidden,Creampies,Milfs
   </description>
   <width>$thumbwidth</width>
   <height>$thumbheight</height>
  </image>
  <ttl>1440</ttl>
 <item>
  <title>$sitename</title>
  <link>http://www.{$sitename}/Ebony.php</link>
  <description>$sitename</description>
  <enclosure url="http://www.{$sitename}{$sitepath}/thumbs/{$thumbname}" length="{$thumbsize}" type="text/jpeg" />
  <author>{$tgpemail}</author>
  <source url="http://www.{$sitename}{$sitepath}/rss.php">{$sitename}</source>
  <guid>http://{$sitename}/</guid>
  <content:encoded><![CDATA[
END;
if(is_file('templates/header.inc')){
	include('templates/header.inc');
}else{
	echo "Need to set up global $sitename Header - See [Templates] section <br>";
}
$output .= ob_get_contents();
ob_end_clean();

ob_start();
$template1="
<td align='center'>
 <a href='http://www.{$sitename}{$sitepath}/cx.php?i=%id%{$doNats}&s={$skimratio}&t=1&nc=1' target='_blank'>
  <img src='http://www.{$sitename}{$sitepath}/thumbs/%thumbname%' alt='%description% %keywords%' border='0'>
 </a>
 <br>
 <a href='http://www.{$sitename}{$sitepath}/search.php?search=%urlcat%'>%cat%</a>
</td>";
		
$r=mysql_query($q);
$search=$s;
$categories=array();
if($r){
    $i=0;
    while($rw=mysql_fetch_assoc($r)){
        extract($rw);
		if(intval($category)==0){
			$categories[$category]=1;
		}
		if($i==0){
			$output.="<center>";
			$output.="<table style='border: 1px solid black;' width='600'>";
		}

		if(($i % $width)==0){
			$output .= "<tr>";
		}
		$keywords=str_replace(',',' ',$keywords);
		$keywords=str_replace('"','',$keywords);
		$keywords=str_replace("'",'',$keywords);
		$keywords=str_replace('-',' ',$keywords);
		$keys=explode(' ',$keywords);
		foreach($keys as $k=>$idx){
			if(intval($k)==0 && $k!='' && $k!=0){
				$categories[$k]=1;
			}
		}
		$t=$template1;
		$t=str_replace('%id%',$id,$t);
		$t=str_replace('%thumbname%',$thumbname,$t);
		$t=str_replace('%thumbsize%',$thumbsize,$t);
		$t=str_replace('%keywords%',$keywords,$t);
		$t=str_replace('%url%',$url,$t);
	    $t=str_replace('%urlcat%',rawurlencode($category),$t);
	    
		if($hasvideo){
			$category=$category." (Video) ";	
		}	
		$t=str_replace('%cat%',$category,$t);
		$desc = substr($description, 0, $descleng);
            switch($changedesc){
                case '0':
                default:
                    //echo 'No';
                break;
                case '1':
                    //echo 'First word upper';
                    $desc = ucfirst(strtolower($desc));
                break;
                case '2':
                    //echo 'Each Word Upper';
                    $desc = ucwords(strtolower($desc));
                break;
                case '3':
                    //echo 'ALL UPPER';
                    $desc = strtoupper($desc);
                break;
                case '4':
                    //echo 'all lower';
                    $desc = strtolower($desc);
                break;
            }
		$t=str_replace('%desc%',$desc,$t);
		$t=str_replace('%description%',$description,$t);
		$output .= $t;
        $i++;
		if(($i % $width)==0){
			$output .= "</tr>";
		}
    }
}else{ //nothing found...
	$output.="
<center>
<table style='border: 1px solid black;' width='600'>
<tr><td colspan='6'>
 <a href='http://{$sitename}'>Ooops.. there seems to be a problem today, the 
 database is not working - No Images found - Try visiting our site directly.</a>
</td></tr>";
}  


$output.="
 <tr><td colspan=6>
 <center><a href='http://comusthumbs.com' target=_blank><font size=-2>Powered by Comus Thumbs RSS Feeder</font></a></center>
 </td></tr>
 </table>
</center>
 ";

if(is_file('templates/footer.inc')){
	include('templates/footer.inc');
}else{
	echo "Need to set up Global $sitename Footer - See [Templates] section <br>";
}
echo "
   ]]></content:encoded>
  </item>
";
$output .= ob_get_contents();
ob_end_clean();

$output .="<dc:creator>Comus Thumbs 2.51 $tgpemail</dc:creator>n";
$output .= "<pubDate>".date("D, d M Y h:m:i +0000")."</pubDate>";
foreach($categories as $key=>$index){
 	$output .= "<dc:subject>{$key}</dc:subject>";
	$cats.="$key,";
}	
$output=str_replace(',BigTits,Orgy,BannersHidden,Creampies,Milfs',$cats,$output);
$output.="  </channel>
</rss>
";

$fh=fopen($fn,'w+');
fwrite($fh,$output);
fclose($fh);
echo $output;
?>