<?php

# Settings
$productsPerPage=50;

# Set Access to the database
$conn = mysql_connect("localhost", "speakt5_speak", "50cents"); 
mysql_select_db("speakt5_products",$conn);

# Set Page Default 
if (empty($tag)){$tag='arrived';}


# Get the items for this room
$sql = "SELECT taggings.item, taggings.tag, product.activated, product.descrip, product.pagetext, product.price, product.varietyof, product.oldprice,product.class "
        . " FROM taggings, product"
        . " WHERE taggings.tag = '$tag' AND taggings.item = product.item AND product.activated > 0"
        . " ORDER BY product.activated DESC "
	. " LIMIT 0 , $productsPerPage";
$result=mysql_query($sql,$conn) or die(mysql_error()); 
$rows=mysql_num_rows($result); 



# Get the tagroom metadata
$sql = "SELECT  * FROM  `tags` WHERE tag =  '$tag'";
$tagroomResult=mysql_query($sql,$conn) or die(mysql_error());
$r = mysql_fetch_array($tagroomResult);
$active_items=$r['active_items'];
$customHeader=$r['customHeader'];
$caption=$r['caption'];

if ($tag=='arrived') {$title="What's New";}
else {$title=$caption;}

$fixedTag=str_replace (" ", "+", $tag );

# --------------------------GENERATE THE RSS 2.0 FEED ----------------------------------
header("Content-type: text/xml");
?>
<rss version="2.0">
<channel>
<title>Speak To Me Catalog: <?php echo htmlspecialchars($title) ?></title>
<link>http://speaktomecatalog.com/room.php?tag=<?php echo $fixedTag ?></link>
<description>Wacky or practical items that talk, sing, or play music: clocks, magnets, watches, key chains, cards, music boxes, stuffed animals, games, and digital  recorders. Give the gift that says something. </description>
<language>en-US</language>
<copyright>Speak To Me Catalog</copyright>
<lastBuildDate><?php echo date("D, d M Y H:i:s -0800"); ?></lastBuildDate>
<image>
	<url>http://www.speaktomecatalog.com/images/logo.jpg</url>
	<title>lips guy logo</title>
	<link>http://speaktomecatalog.com/</link>
</image>
<?php
while ($r = mysql_fetch_array($result)) {
	$item = $r['item'];
	$descrip = strip_tags($r['descrip']); 
	$pagetext = strip_tags($r['pagetext']);
	$varietyof = $r['varietyof'];
	$price = $r['price'];
	$oldprice = $r['oldprice'];
	$class=$r['class'];
	$activated = $r['activated'];
	$pubDate=date("D, d M Y H:i:s -0800", strtotime($activated));

	# claculate the encousure for sound demos
	$enclosedFile="";
	$demofile="mp3/".$item.".mp3";
	if (file_exists($demofile)) {$enclosedFile=$demofile;}
	else {	$demofile="mp3/".strtolower($item).".mp3";
		if (file_exists($demofile)){$enclosedFile=$demofile;}}
	if (!empty($enclosedFile)){
		$enc=sprintf('<enclosure url="http://www.speaktomecatalog.com/%s" length="%s" type="audio/mpeg" />',$enclosedFile,filesize($enclosedFile));
		$demo=sprintf("<p><a href='http://www.speaktomecatalog.com/%s'><img src='http://www.speaktomecatalog.com/images/sounddemo.gif'  border='0' width='80' height='67' alt='listen' ALIGN='MIDDL'></a>",$demofile);}
	else {	$enc="";
		$demo="";
	}
	

	# claculate content
	$thumb="thumb/".$item.".jpg";
	$picture="";
	if (file_exists($thumb)) {
		$img=getimagesize($thumb);
		$width=$img[0];
		$height=$img[1];
		$picture=sprintf('<img src="http://www.speaktomecatalog.com/%s" width="%s" height="%s"  border="0" align="right" hspace="10" vspace="5" alt="product picture">',$thumb,$width, $height);}
	else {$picture="";}
	$content=htmlspecialchars($picture.$pagetext.$demo,ENT_QUOTES);

	?>
	<item>
	<title><?print htmlentities($descrip);?></title>
	<link>http://www.speaktomecatalog.com/individual.php?item=<?php echo $item; ?></link>
	<description><?php  echo $content ?></description>
	<category><?php echo $class ?></category>
	<?php if($class<>$tag) printf("<category>%s</category>",$tag); ?>
	<pubDate><?php echo $pubDate?></pubDate>
	<guid>http://www.speaktomecatalog.com/individual.php?item=<?php echo $item; ?></guid>
	<?php echo $enc ?>	
	 </item>
<?php } ?>   
</channel>
</rss>

