<?php
include "common.inc";
include "config.inc";
include "db_connect.inc" ;
include "misc.inc";


$headers = getallheaders();
if(array_key_exists("If-None-Match", $headers))
{
    $since   = $headers["If-None-Match"];
} else
{
    $since = NULL;
}

$is_brief = isset($brief);

if($since)
{
    $since = trim($since);
    $sql   = "select MAX(Id) FROM Texts WHERE Id>".addslashes($since);
} else
{
    $sql   = "select MAX(Id) FROM Texts";
}

$res   = mysql_db_query($db_database, $sql , $link);
if($res) 
{
    if( $row = mysql_fetch_array( $res ) ) 
    {
        if($row[0])
        {
            Header("Etag: $row[0]");
        } else
        {
            if(isset($since))
            {
                Header("Etag: $since");
                Header("HTTP/1.1 304 Not Modified");
                return;
            }
        }
    } 
    mysql_free_result ( $res );
}

Header("Content-type: text/xml; charset=\"utf-8\"");

print("<?xml version='1.0' encoding='utf-8'?>\r\n");

print("<rss xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" version='2.0'>\r\n");

print("<channel>\r\n");
print("<title>OBEC.PACTET</title>\r\n");
print("<link>http://obec.org.ru/</link>\r\n");
print("<language>ru</language>\r\n");
print("<description>Russian-language literary works for OBEC.PACTET FIDOnet conference</description>");


if($is_brief)
{
    $text_sql="NULL";
} else
{
    $text_sql="Texts.Text";
}
  
if(isset($since))
{
    $sql = "SELECT STRAIGHT_JOIN Texts.Id, Texts.Title, Authors.Name, Texts.AuthorId, UNIX_TIMESTAMP(Texts.PublishedAt), Authors.Email, Authors.Name, Texts.CommentThreadId, " . $text_sql . " FROM Texts as T1, Texts, Authors WHERE T1.Id>" . addslashes($since) . " AND T1.Id=Texts.Id AND Texts.AuthorId=Authors.Id ORDER BY T1.Id DESC LIMIT " . $RSS_MAX_RECORDS;
} else
{
    $sql = "SELECT STRAIGHT_JOIN Texts.Id, Texts.Title, Authors.Name, Texts.AuthorId, UNIX_TIMESTAMP(Texts.PublishedAt), Authors.Email, Authors.Name, Texts.CommentThreadId, " . $text_sql . " FROM Texts as T1, Texts, Authors WHERE T1.Id=Texts.Id AND Texts.AuthorId=Authors.Id ORDER BY T1.Id DESC LIMIT " . $RSS_MAX_RECORDS;
}

$result = mysql_db_query($db_database, $sql , $link);
while($row = mysql_fetch_array($result))
{
    print("<item>\r\n");
    print("<title>" .
          iconv($OBEC_CHARSET, "utf-8", htmlspecialchars($row[2] . ": " . $row[1], ENT_QUOTES)) .
          "</title>\r\n"
    );
    print("<link>" . $PROGRAM_URL . "text.php?Id=" . $row[0] . "</link>\r\n");
    if($row[7]!=NULL)
    {
        print("<comments>" . $PROGRAM_URL . "thread.php?thread_id=" . $row[7] . "</comments>\r\n");
    }
    if($row[8]!=NULL)
    {
        print("<content:encoded><![CDATA[<pre>" .
              str_replace("]]","&93;&93;",
                          iconv($OBEC_CHARSET, "utf-8", 
                                htmlspecialchars(strip_invalid_chars($row[8], $OBEC_CHARSET), ENT_QUOTES))) .
              "</pre>]]></content:encoded>\r\n");
    }
    print("<pubDate>" . htmlspecialchars(date("r", $row[4])) . "</pubDate>\r\n");
    print("<author>" . iconv($OBEC_CHARSET, "utf-8", htmlspecialchars($row[5] . " (" . $row[6] . ")", ENT_QUOTES)) . "</author>\r\n");

    print("<wfw:commentRss xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\">" . $PROGRAM_URL . "commentsrss.php?Id=" . $row[0] . "</wfw:commentRss>\r\n");
 
    print("</item>\r\n\r\n");
}
mysql_free_result($result);
?>

</channel>
</rss>

