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


    if(!$Id)
    {
        Header("HTTP/1.1 404 Missing 'Id' argument");
        return;
    }

    $result = mysql_db_query($db_database,
                             "SELECT Texts.Title, Texts.AuthorId, Authors.Name, CommentThreadId " .
                             "FROM Texts  " .
                             "LEFT JOIN Authors ON Authors.Id = Texts.AuthorId " .
                             "WHERE Texts.Id = " . addslashes($Id),
                             $link);
    $row = mysql_fetch_array($result);

    $title = iconv($OBEC_CHARSET, "utf-8", htmlspecialchars("OBEC.3BOH comments for " . trim($row[2]) . " text '" . trim($row[0]) . "'", ENT_QUOTES));
    $thread_id=$row[3];

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

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

    $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>" . $title . "</title>\r\n");
    print("<link>" . "$PROGRAM_URL" . "thread.php?thread_id=" . $thread_id . "</link>\r\n");
    print("<language>ru</language>\r\n");
    print("<description>" . $title .  "</description>");

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

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

</channel>
</rss>

