<cfsetting enablecfoutputonly=true>

<!---
 | check for new content since last request - all update code should store
 | now() in application.lastchangedate so we can check that here, and thus
 | avoid actually hitting the database unneccessarily
 |
 | code adapted and extended from Pete Freitag's example at:
 | http://www.petefreitag.com/item/235.cfm
 !--->

<cfset checkdate="">

<cftry>
 <!--- need to initialise application.lastchangedate after a server restart --->
 <cfif not structkeyexists(application, 'lastchangedate')>
  <cfquery name="getlastchange" datasource=#request.dsn#>
   SELECT max(uploaded) AS uploaded FROM recipes
  </cfquery>

  <cfset application.lastchangedate=parsedatetime(getlastchange.uploaded)>
 </cfif>

 <cfheader name="Last-Modified" value=#gethttptimestring(application.lastchangedate)#>

 <cfset headers=gethttprequestdata().headers>

 <cfif structkeyexists(headers, "If-Modified-Since")>
  <cfset checkdate=parsedatetime(headers["If-Modified-Since"])>
  <cfset changedate=application.lastchangedate>

  <cfif datediff("n", changedate, checkdate) gt 0>
   <cfheader statuscode="304" statustext="Not Modified"> 
   <cfabort>
  </cfif>
 </cfif>

 <cfcatch type="any"></cfcatch>
</cftry>

<!--- end of unchanged content check --->

<cfset NumberOfFeedItems = 20>

<!---<cfquery name="getContent" datasource=#request.dsn# cachedwithin=#createtimespan(0, 0, 20, 0)#>--->
<cfquery name="getContent" datasource=#request.dsn#>
 SELECT
  recipes.id, title AS rsstitle, tags AS rsstags, uploaded, whattype, description,
  username, security.id as userid
 FROM recipes, security
 WHERE
  recipes.user=security.id AND recipes.approved='y'
  <cfif checkdate neq "">AND uploaded>#checkdate#</cfif>
 ORDER BY uploaded DESC
 LIMIT #numberOfFeedItems# 
</cfquery>

<cfscript>
 tz=gettimezoneinfo();
 tz=tz.isDSTon ? "+0100" : "+0000";
 now=now();
 now="#dateformat(now, 'ddd, dd mmm yyyy')# #timeformat(now, 'HH:mm:ss')# #tz#";
</cfscript>

<cfsavecontent variable="theXML">

<cfoutput><?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="http://#cgi.server_name#/css/rss.css" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns##">

<channel>
<ttl>120</ttl>
 <title>the latest from Nibblous.com</title>
 <link>http://www.nibblous.com/</link>
 <image>
  <url>http://www.nibblous.com/resources/nibblousicon2007.png</url>
  <title>the latest from Nibblous.com</title>
  <link>http://www.nibblous.com/</link>
 </image>
 <description>latest Nibblous nibblings</description>
 <language>en</language>
 <copyright>copyright Nibblous.com</copyright>
 <pubDate>#now#</pubDate>
</cfoutput>


<cfloop query="getContent">
 <cfoutput>
<cfsavecontent variable="therecipe">
<p>posted by <a href="http://www.nibblous.com/recipes/user.#userid#">#username#</a></p>
#getContent.description#
<cfquery name="keywords" datasource="#request.dsn#">
SELECT tag FROM recipetags WHERE recipe=#id#
</cfquery>
<cfif keywords.recordcount gt 0>
<cfquery name="gettags" datasource="#request.dsn#">
select name from rtagnames, recipetags where recipe=#id# and tag=rtagnames.id
order by name
</cfquery>
 <cfquery name="similar" datasource="#request.dsn#">
    SELECT recipes.id, recipes.title, count(recipetags.recipe) AS
    quality FROM recipetags, recipes 
    WHERE recipetags.recipe!=#id#
    AND recipetags.tag IN (#valuelist(keywords.tag)#) AND recipes.id=recipetags.recipe
GROUP BY recipetags.recipe
    ORDER BY quality DESC, rand() DESC LIMIT 6
    </cfquery>
	<cfif gettags.recordcount neq 0>tagged with: <cfloop query="gettags"><a href="http://www.nibblous.com/recipes/tag.#name#">#name#</a>&nbsp;</cfloop></cfif>
	<cfif similar.recordcount neq 0>
		<h3>You might also like:</h3>
	<cfloop query="similar">
	<a href="http://www.nibblous.com/recipe/#id#">#title#</a><br />
	</cfloop>
	</cfif>
</cfif>
</cfsavecontent>
</cfoutput>
 <cfscript>
  title=server.entity(lcase(rereplace(rsstitle, "<[^>]*>", "", "ALL")));
  summary=server.entity(rereplace(description, "<[^>]*>", "", "ALL"));
  content=server.entity(rereplacenocase(description, "<img [^>]*>", "", "ALL"));
  date=dateformat(uploaded, "ddd, dd mmm yyyy");
  time=timeformat(uploaded, "HH:mm:ss");
  pubDate=date & " " & time & " " & tz;
  link="http://www.nibblous.com/recipe/#id#";
 </cfscript>

 <cfoutput>
 <item>
 
  <title>#title#</title>
   <description>#summary#</description>
  <content:encoded><![CDATA[#therecipe#]]></content:encoded>
    <guid isPermaLink="true">#link#</guid>
  <pubDate>#pubDate#</pubDate>
 </item>
 </cfoutput>
</cfloop>

<cfoutput>
</channel>
</rss>
</cfoutput>

</cfsavecontent>

<cfcontent type="text/xml">
<cfheader name="Content-Type" value="text/xml; charset=utf-8">
<cfoutput>#thexml#</cfoutput>