<?php

/**
 * /rss2/blog.xml
 *
 * This generates RSS XML data the 10 most recently published Blog posts.
 */

require_once('core.inc');

////////////////////////////////////////////////////////////////////////////////

$cond = array();
$cond[] = "post_family = 'blog'";
$cond[] = "post_status = 'published'";

core_tool('uriutil');
$member = URIUtil::from_get('member');

$user = new User();
if ($user->load_by_nick($member)) {
  if ($user->can_submit_blog())
    $cond[] = "post_author = '{$user->id}'";
  else
    $member = NULL;
}

$cond = implode(' AND ', $cond);

////////////////////////////////////////////////////////////////////////////////

core_tool('rss2blog');

$rss2cache = new RSS2Cache(5);
if ($rss2cache->dump())
  exit;

$who = !empty($member) ? "by {$member}" : '';

$settings_conf = LangConf::get_settings_instance();
$chan = new RSS2Channel("Latest Blog Entries {$who} at {$settings_conf->SiteName}");
$chan->pubDate = DB::get_var("SELECT UNIX_TIMESTAMP(MAX(post_published)) FROM `Posts` WHERE {$cond}");
$chan->ttl = 15;

core_class('url');
$url = URL::get_instance();
$chan->link = $url->get_www_blog($member);

////////////////////////////////////////////////////////////////////////////////

$feed = new RSS2BlogFeed($chan);

DB::query("SELECT post_id FROM `Posts` WHERE {$cond} ORDER BY post_published DESC LIMIT 10");
$feed->populate(DB::get_result());

$xml_data = $feed->dump(TRUE);
$rss2cache->save($xml_data);
echo $xml_data;

?>

