<?php
    //
    // feedwriter.php
    // writes RSS feeds 
    //
     
     
     
    //---------------------------------------------------------------------------------------
    //
    //
    //---------------------------------------------------------------------------------------
    function generate_content_rss()
    {
        global $siteSettings;
        $siteurl = "";
         
        //Grab the website name (siteSettings may already contain it, if slickurls is true)
        if ($siteSettings['slickurls'] == FALSE )
        {
            $file = explode("/" , $_SERVER['SCRIPT_NAME'] );
            array_pop($file );
            $path = implode("/", $file );
             
            if ($_SERVER['SERVER_PORT'] == '80' )
                $siteurl = "http://" . $_SERVER['SERVER_NAME'] . $path;
            else
                $siteurl = "http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path;
        }
        else
            $siteurl = $siteSettings['siteurl'];
         
         
         
        $rssfile = "engine/shards/xmlcache/articles.rss";
        $rss = fopen($rssfile , "w" );
        if ($rss != FALSE )
        {
            //print document header
            fwrite($rss , "<?xml version=\"1.0\"?>\n");
            fwrite($rss , "<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\" \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">\n");
            fwrite($rss , "<rss version=\"0.91\">\n");
             
            //print channel information
            fwrite($rss , "<channel>\n");
            fwrite($rss , "<title>" . $siteSettings['titlebase'] . "</title>\n");
            fwrite($rss , "<link>" . $siteurl . "/content</link>\n");
            fwrite($rss , "<description>LOTS of good stuff</description>\n");
            fwrite($rss , "<language>en-us</language>\n");
             
             
             
            $query = "SELECT ID, title, summary FROM content ORDER BY date DESC LIMIT 0,10";
            $entries = mysql_query($query );
             
            while ($row = mysql_fetch_array($entries ))
            {
                $title = $row['title'];
                $desc = $row['summary'];
                $id = $row['ID'];
                 
                 
                if ($siteSettings['slickurls'] == TRUE )
                    $url = $siteurl . "/content/" . $row['ID'] . "/" . make_url_friendly($row['title']);
                else
                    $url = $siteurl . "/index.php?shard=content&amp;action=g_viewContent&amp;ID=" . $row['ID'];
                 
                $itemstring = "<item>\n<title>" . $title . "</title>\n" . "<link>" . $url . "</link>\n" . "<description>" . $desc . "</description>\n" . "</item>\n";
                 
                fwrite($rss , $itemstring );
                 
            }
             
             
            fwrite($rss , "</channel>\n</rss>\n" );
            fclose($rss );
             
        }
    }
     
     
     
    //---------------------------------------------------------------------------------------
    //
    //
    //---------------------------------------------------------------------------------------
    function generate_blog_rss()
    {
        global $siteSettings;
        $siteurl = "";
         
        //Grab the website name (siteSettings may already contain it, if slickurls is true)
        if ($siteSettings['slickurls'] == FALSE )
        {
            $file = explode("/" , $_SERVER['SCRIPT_NAME'] );
            array_pop($file );
            $path = implode("/", $file );
             
            if ($_SERVER['SERVER_PORT'] == '80' )
                $siteurl = "http://" . $_SERVER['SERVER_NAME'] . $path;
            else
                $siteurl = "http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path;
        }
        else
            $siteurl = $siteSettings['siteurl'];
         
         
         
        $rssfile = "engine/shards/xmlcache/blog.rss";
        $rss = fopen($rssfile , "w" );
        if ($rss != FALSE )
        {
            //print document header
            fwrite($rss , "<?xml version=\"1.0\"?>\n");
            fwrite($rss , "<!DOCTYPE rss PUBLIC \"-//Netscape Communications//DTD RSS 0.91//EN\" \"http://my.netscape.com/publish/formats/rss-0.91.dtd\">\n");
            fwrite($rss , "<rss version=\"0.91\">\n");
             
            //print channel information
            fwrite($rss , "<channel>\n");
            fwrite($rss , "<title>" . $siteSettings['titlebase'] . "</title>\n");
            fwrite($rss , "<link>" . $siteurl . "/content</link>\n");
            fwrite($rss , "<description>LOTS of good stuff</description>\n");
            fwrite($rss , "<language>en-us</language>\n");
             
             
             
            $query = "SELECT ID, title, body FROM blog WHERE threadID=0 ORDER BY date DESC LIMIT 0,10";
            $entries = mysql_query($query );
             
            while ($row = mysql_fetch_array($entries ))
            {
                $title = $row['title'];
                $desc = substr($row['body'] , 0 , 252 ); //grab the first 255 characters of the body as the description
				$desc = $desc . '...';	//likely, this isn't the *whole* post
				$desc = format_blurcode( $desc ); //do not syndicate the BlurCode stuff, transform it to HTML tags
                $id = $row['ID'];
                 
                if ($siteSettings['slickurls'] == TRUE )
                    $url = $siteurl . "/blog/" . $row['ID'];
                else
                    $url = $siteurl . "/index.php?shard=blog&amp;action=g_viewPost&amp;ID=" . $row['ID'];
                 
                $itemstring = "<item>\n<title>" . $title . "</title>\n" . "<link>" . $url . "</link>\n" . "<description>" . $desc . "</description>\n" . "</item>\n";
                 
                fwrite($rss , $itemstring );
                 
            }
             
             
            fwrite($rss , "</channel>\n</rss>\n" );
            fclose($rss );
             
        }
    }
     
     
     
