#!/usr/bin/perl -w

# Starburst CMS | Perl Content Management System by Kirsle #
# -------------------------------------------------------- #
# This is a stripped-down edition of index.cgi meant to be #
# used as an RSS feed script for the SB-Blog plugin.       #

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use URI::Escape;
use Digest::MD5 qw(md5_hex);
use Data::Dumper;
use lib "./lib";
use Time::Format qw(time_format);
use Time::Local;
use Time::Zone;
our $root  = {
	# Base for pvt data. If you change this, also change it below at the
	# require statements.
	base    => "./sb-pvt",
	version => '0.01',
	header  => 0,       # 1 = the HTTP headers were already sent
	conf    => {},      # Main configuration data.
	me      => 'guest', # Our username (default=guest)
	auth    => 0,       # 1 = we're logged in, 0 = otherwise
	users   => {},      # Loaded user(s) information.
	cookie  => {},      # Loaded cookies.
	meta    => {},      # Header fields on pages.
	content => '',      # Eventual page contents.
	perl    => '',      # Any embedded Perl code in the pages.
};
our $cgi   = new CGI;
our $query = {
	p => 'index',
};

# Define a fallback CGI::Carp failure page.
sub panic {
	my $msg = shift;
	if ($root->{header} == 0) {
		print "Content-Type: text/html\n\n";
	}
	print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
		. "\"http://www.w3.org/TR/html4/strict.dtd\">\n"
		. "<html>\n"
		. "<head>\n"
		. "<title>500 Internal Server Error</title>\n"
		. "<style type=\"text/css\">\n"
		. "body {\n"
		. " background-color: #FFFFFF;\n"
		. " font-family: Verdana,Arial,Helvetica,sans-serif;\n"
		. " font-size: small;\n"
		. " color: #000000;\n"
		. "}\n"
		. "h1 {\n"
		. " display: block;\n"
		. " font-size: xx-large;\n"
		. " font-weight: bold;\n"
		. " color: #000000;\n"
		. " margin-top: 0px;\n"
		. " margin-bottom: 20px\n"
		. "}\n"
		. "pre {\n"
		. " font-family: \"Lucida Console\",\"DejaVu LGC Sans Mono\","
		. "\"Bitstream Vera Sans Mono\",monospace;\n"
		. " font-size: small;\n"
		. " color: #000000;\n"
		. " display: block;\n"
		. " border: 1px dashed #999999;\n"
		. " padding: 5px\n"
		. "}\n"
		. "</style>\n"
		. "</head>\n"
		. "<body>\n"
		. "<h1>Internal Server Error</h1>\n\n"
		. "This page has generated an error. The text of the error is "
		. "as follows:<p>\n\n"
		. "<pre>$msg</pre><p>\n\n"
		. "Furthermore, there was no 500 Internal Server Error page to "
		. "handle this error.\n"
		. "<hr>\n"
		. "Starburst CMS\n"
		. "</body>\n"
		. "</html>";
	exit(0);
}
CGI::Carp::set_message (sub {
	$root->{header} = 1;
	&panic(shift);
});

# Load additional source files.
require "./sb-pvt/inc/config.lib";
require "./sb-pvt/inc/cgi.lib";
require "./sb-pvt/inc/users.lib";
require "./sb-pvt/inc/methods.lib";
require "./sb-pvt/inc/blog.lib";
require "./sb-pvt/inc/photos.lib";

# Load our configuration.
$root->{conf} = &readConfig("site.conf");
$root->{blogcfg} = &readConfig("blog.conf");

################################################################################
## Attempt to get a page for the user.                                        ##
################################################################################

# Set the time zone to GMT.
$root->{conf}->{site}->{timezone} = 'GMT';

my $rss = {
	channel => {
		title => $root->{conf}->{site}->{title},
		lastBuildDate => &timeFormat('Day, dd Mon yyyy hh:mm:ss GMT',time()),
	},
};

# Filter the RSS values.
foreach my $top (keys %{$rss}) {
	foreach my $key (keys %{$rss->{$top}}) {
		$rss->{$top}->{$key} =~ s/\&/&amp;/ig;
		$rss->{$top}->{$key} =~ s/\"/&quot;/ig;
		$rss->{$top}->{$key} =~ s/\'/&apos;/ig;
		$rss->{$top}->{$key} =~ s/</&lt;/ig;
		$rss->{$top}->{$key} =~ s/>/&gt;/ig;
	}
}

print "Content-Type: application/rss+xml\n\n";
print qq~<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
	<title>$rss->{channel}->{title}</title>
	<link>http://$ENV{SERVER_NAME}/</link>
	<description>Powered by Starburst Web Blog Software</description>
	<lastBuildDate>$rss->{channel}->{lastBuildDate}</lastBuildDate>
	<generator>Starubrst CMS / Cuvou.com</generator>
	<image>
		<url>http://$ENV{SERVER_NAME}/sb-public/avatars/default.png</url>
		<title>$rss->{channel}->{title}</title>
		<link>http://$ENV{SERVER_NAME}/</link>
		<width>100</width>
		<height>100</height>
	</image>
~;

# Get the RSS entries.
my @entries = &getEntries(0,$root->{blogcfg}->{blog}->{postsperfeed});
foreach my $id (@entries) {
	my $entry = &readEntry($id);
	if (not exists $root->{users}->{$entry->{author}}) {
		&readUser($entry->{author});
	}
	my $nick = &getNickname($entry->{author});
	my $stamp = &timeFormat('Day, dd Mon yyyy hh:mm:ss GMT',$entry->{time});

	foreach my $key (keys %{$entry}) {
		$entry->{$key} =~ s/\&/&amp;/ig;
		$entry->{$key} =~ s/\"/&quot;/ig;
		$entry->{$key} =~ s/\'/&apos;/ig;
		$entry->{$key} =~ s/</&lt;/ig;
		$entry->{$key} =~ s/>/&gt;/ig;
	}

	print qq~
	<item>
		<guid isPermaLink="true">http://$ENV{SERVER_NAME}/?p=blog;id=$id</guid>
		<pubDate>$stamp</pubDate>
		<title>$entry->{subject}</title>
		<description>$entry->{body}</description>
		<comments>http://$ENV{SERVER_NAME}/?p=blog;id=$id</comments>
	</item>~;
}

print qq~
</channel>
</rss>~;
